BlueGit

timfa copperfarmer

Keep farmer identity across oxidation and forget distant chests on place.

Co-authored-by: Cursor <cursoragent@cursor.com>

timfa Tim Falken · pushed by timfa

Authored 2026-08-14 · 3 months ago

Hash
c44b843dbac26e1cfd901b9fc4a03aa25808d77f
Parent
74e100ea
7 files changed +359 −27
src/main/java/nl/timfalken/copperfarmer/CopperFarmerPlugin.java +1 −1
@@ -60,7 +60,7 @@ public final class CopperFarmerPlugin extends JavaPlugin {
60 60 Bukkit.removeRecipe(keys.recipe);
61 61 ItemStack result = itemCodec.createStatue(new ItemStack(Material.COPPER_HOE));
62 62 ShapelessRecipe recipe = new ShapelessRecipe(keys.recipe, result);
63 recipe.addIngredient(Material.COPPER_GOLEM_STATUE);
63 recipe.addIngredient(CopperStatueVariant.recipeChoice());
64 64 recipe.addIngredient(Material.COPPER_HOE);
65 65 Bukkit.addRecipe(recipe);
66 66 }
@@ -60,7 +60,7 @@ public final class CopperFarmerPlugin extends JavaPlugin {
60 Bukkit.removeRecipe(keys.recipe);
60 Bukkit.removeRecipe(keys.recipe);
61 ItemStack result = itemCodec.createStatue(new ItemStack(Material.COPPER_HOE));
61 ItemStack result = itemCodec.createStatue(new ItemStack(Material.COPPER_HOE));
62 ShapelessRecipe recipe = new ShapelessRecipe(keys.recipe, result);
62 ShapelessRecipe recipe = new ShapelessRecipe(keys.recipe, result);
63 recipe.addIngredient(Material.COPPER_GOLEM_STATUE);
63 recipe.addIngredient(CopperStatueVariant.recipeChoice());
64 recipe.addIngredient(Material.COPPER_HOE);
64 recipe.addIngredient(Material.COPPER_HOE);
65 Bukkit.addRecipe(recipe);
65 Bukkit.addRecipe(recipe);
66 }
66 }
src/main/java/nl/timfalken/copperfarmer/CopperStatueVariant.java added +67 −0
@@ -0,0 +1,67 @@
1 package nl.timfalken.copperfarmer;
2
3 import io.papermc.paper.world.WeatheringCopperState;
4 import org.bukkit.Material;
5 import org.bukkit.entity.CopperGolem;
6 import org.bukkit.inventory.RecipeChoice;
7
8 import java.util.ArrayList;
9 import java.util.List;
10
11 enum CopperStatueVariant {
12 UNAFFECTED(Material.COPPER_GOLEM_STATUE, WeatheringCopperState.UNAFFECTED, false),
13 EXPOSED(Material.EXPOSED_COPPER_GOLEM_STATUE, WeatheringCopperState.EXPOSED, false),
14 WEATHERED(Material.WEATHERED_COPPER_GOLEM_STATUE, WeatheringCopperState.WEATHERED, false),
15 OXIDIZED(Material.OXIDIZED_COPPER_GOLEM_STATUE, WeatheringCopperState.OXIDIZED, false),
16 WAXED_UNAFFECTED(Material.WAXED_COPPER_GOLEM_STATUE, WeatheringCopperState.UNAFFECTED, true),
17 WAXED_EXPOSED(Material.WAXED_EXPOSED_COPPER_GOLEM_STATUE, WeatheringCopperState.EXPOSED, true),
18 WAXED_WEATHERED(Material.WAXED_WEATHERED_COPPER_GOLEM_STATUE, WeatheringCopperState.WEATHERED, true),
19 WAXED_OXIDIZED(Material.WAXED_OXIDIZED_COPPER_GOLEM_STATUE, WeatheringCopperState.OXIDIZED, true);
20
21 private final Material material;
22 private final WeatheringCopperState weathering;
23 private final boolean waxed;
24
25 CopperStatueVariant(Material material, WeatheringCopperState weathering, boolean waxed) {
26 this.material = material;
27 this.weathering = weathering;
28 this.waxed = waxed;
29 }
30
31 Material material() {
32 return material;
33 }
34
35 static boolean isStatue(Material material) {
36 return from(material) != null;
37 }
38
39 static CopperStatueVariant from(Material material) {
40 for (CopperStatueVariant variant : values()) {
41 if (variant.material == material) return variant;
42 }
43 return null;
44 }
45
46 static CopperStatueVariant from(CopperGolem golem) {
47 return from(golem.getWeatheringState(), golem.getOxidizing() instanceof CopperGolem.Oxidizing.Waxed);
48 }
49
50 static CopperStatueVariant from(WeatheringCopperState weathering, boolean waxed) {
51 for (CopperStatueVariant variant : values()) {
52 if (variant.weathering == weathering && variant.waxed == waxed) return variant;
53 }
54 return waxed ? WAXED_UNAFFECTED : UNAFFECTED;
55 }
56
57 static RecipeChoice.MaterialChoice recipeChoice() {
58 List<Material> materials = new ArrayList<>();
59 for (CopperStatueVariant variant : values()) materials.add(variant.material);
60 return new RecipeChoice.MaterialChoice(materials);
61 }
62
63 void applyTo(CopperGolem golem) {
64 golem.setWeatheringState(weathering);
65 golem.setOxidizing(waxed ? CopperGolem.Oxidizing.waxed() : CopperGolem.Oxidizing.unset());
66 }
67 }
@@ -0,0 +1,67 @@
1package nl.timfalken.copperfarmer;
2
3import io.papermc.paper.world.WeatheringCopperState;
4import org.bukkit.Material;
5import org.bukkit.entity.CopperGolem;
6import org.bukkit.inventory.RecipeChoice;
7
8import java.util.ArrayList;
9import java.util.List;
10
11enum CopperStatueVariant {
12 UNAFFECTED(Material.COPPER_GOLEM_STATUE, WeatheringCopperState.UNAFFECTED, false),
13 EXPOSED(Material.EXPOSED_COPPER_GOLEM_STATUE, WeatheringCopperState.EXPOSED, false),
14 WEATHERED(Material.WEATHERED_COPPER_GOLEM_STATUE, WeatheringCopperState.WEATHERED, false),
15 OXIDIZED(Material.OXIDIZED_COPPER_GOLEM_STATUE, WeatheringCopperState.OXIDIZED, false),
16 WAXED_UNAFFECTED(Material.WAXED_COPPER_GOLEM_STATUE, WeatheringCopperState.UNAFFECTED, true),
17 WAXED_EXPOSED(Material.WAXED_EXPOSED_COPPER_GOLEM_STATUE, WeatheringCopperState.EXPOSED, true),
18 WAXED_WEATHERED(Material.WAXED_WEATHERED_COPPER_GOLEM_STATUE, WeatheringCopperState.WEATHERED, true),
19 WAXED_OXIDIZED(Material.WAXED_OXIDIZED_COPPER_GOLEM_STATUE, WeatheringCopperState.OXIDIZED, true);
20
21 private final Material material;
22 private final WeatheringCopperState weathering;
23 private final boolean waxed;
24
25 CopperStatueVariant(Material material, WeatheringCopperState weathering, boolean waxed) {
26 this.material = material;
27 this.weathering = weathering;
28 this.waxed = waxed;
29 }
30
31 Material material() {
32 return material;
33 }
34
35 static boolean isStatue(Material material) {
36 return from(material) != null;
37 }
38
39 static CopperStatueVariant from(Material material) {
40 for (CopperStatueVariant variant : values()) {
41 if (variant.material == material) return variant;
42 }
43 return null;
44 }
45
46 static CopperStatueVariant from(CopperGolem golem) {
47 return from(golem.getWeatheringState(), golem.getOxidizing() instanceof CopperGolem.Oxidizing.Waxed);
48 }
49
50 static CopperStatueVariant from(WeatheringCopperState weathering, boolean waxed) {
51 for (CopperStatueVariant variant : values()) {
52 if (variant.weathering == weathering && variant.waxed == waxed) return variant;
53 }
54 return waxed ? WAXED_UNAFFECTED : UNAFFECTED;
55 }
56
57 static RecipeChoice.MaterialChoice recipeChoice() {
58 List<Material> materials = new ArrayList<>();
59 for (CopperStatueVariant variant : values()) materials.add(variant.material);
60 return new RecipeChoice.MaterialChoice(materials);
61 }
62
63 void applyTo(CopperGolem golem) {
64 golem.setWeatheringState(weathering);
65 golem.setOxidizing(waxed ? CopperGolem.Oxidizing.waxed() : CopperGolem.Oxidizing.unset());
66 }
67}
src/main/java/nl/timfalken/copperfarmer/FarmerItemCodec.java +19 −8
@@ -3,6 +3,7 @@ package nl.timfalken.copperfarmer;
3 3 import org.bukkit.Material;
4 4 import org.bukkit.entity.CopperGolem;
5 5 import org.bukkit.inventory.ItemStack;
6 import org.bukkit.persistence.PersistentDataContainer;
6 7 import org.bukkit.persistence.PersistentDataType;
7 8
8 9 final class FarmerItemCodec {
@@ -13,30 +14,40 @@ final class FarmerItemCodec {
13 14 }
14 15
15 16 ItemStack createStatue(ItemStack sourceTool) {
17 return createStatue(sourceTool, Material.COPPER_GOLEM_STATUE);
18 }
19
20 ItemStack createStatue(ItemStack sourceTool, Material statueType) {
16 21 ItemStack tool = normalizeTool(sourceTool);
17 ItemStack item = new ItemStack(Material.COPPER_GOLEM_STATUE);
22 Material type = CopperStatueVariant.isStatue(statueType) ? statueType : Material.COPPER_GOLEM_STATUE;
23 ItemStack item = new ItemStack(type);
18 24 item.editMeta(meta -> {
19 25 meta.setItemName("Copper Farmer Golem");
20 meta.getPersistentDataContainer().set(keys.farmerItem, PersistentDataType.BYTE, (byte) 1);
21 meta.getPersistentDataContainer().set(keys.tool, PersistentDataType.BYTE_ARRAY, tool.serializeAsBytes());
26 PersistentDataContainer pdc = meta.getPersistentDataContainer();
27 FarmerPersistence.stampItem(pdc, keys);
28 pdc.set(keys.tool, PersistentDataType.BYTE_ARRAY, tool.serializeAsBytes());
22 29 });
23 30 return item;
24 31 }
25 32
33 ItemStack createStatue(FarmerPersistence.FarmerSnapshot snapshot, Material statueType) {
34 ItemStack item = createStatue(decodeTool(snapshot.tool()), statueType);
35 item.editMeta(meta -> snapshot.apply(meta.getPersistentDataContainer(), keys));
36 return item;
37 }
38
26 39 boolean isFarmerItem(ItemStack item) {
27 40 return item != null && !item.isEmpty()
28 && item.getPersistentDataContainer().has(keys.farmerItem, PersistentDataType.BYTE);
41 && FarmerPersistence.isFarmerItem(item.getPersistentDataContainer(), keys);
29 42 }
30 43
31 44 ItemStack readTool(ItemStack farmerItem) {
32 45 if (farmerItem == null || farmerItem.isEmpty()) return defaultTool();
33 byte[] bytes = farmerItem.getPersistentDataContainer().get(keys.tool, PersistentDataType.BYTE_ARRAY);
34 return decodeTool(bytes);
46 return decodeTool(farmerItem.getPersistentDataContainer().get(keys.tool, PersistentDataType.BYTE_ARRAY));
35 47 }
36 48
37 49 ItemStack readTool(CopperGolem golem) {
38 byte[] bytes = golem.getPersistentDataContainer().get(keys.tool, PersistentDataType.BYTE_ARRAY);
39 return decodeTool(bytes);
50 return decodeTool(golem.getPersistentDataContainer().get(keys.tool, PersistentDataType.BYTE_ARRAY));
40 51 }
41 52
42 53 void storeTool(CopperGolem golem, ItemStack sourceTool) {
@@ -3,6 +3,7 @@ package nl.timfalken.copperfarmer;
3import org.bukkit.Material;
3import org.bukkit.Material;
4import org.bukkit.entity.CopperGolem;
4import org.bukkit.entity.CopperGolem;
5import org.bukkit.inventory.ItemStack;
5import org.bukkit.inventory.ItemStack;
6import org.bukkit.persistence.PersistentDataContainer;
6import org.bukkit.persistence.PersistentDataType;
7import org.bukkit.persistence.PersistentDataType;
7
8
8final class FarmerItemCodec {
9final class FarmerItemCodec {
@@ -13,30 +14,40 @@ final class FarmerItemCodec {
13 }
14 }
14
15
15 ItemStack createStatue(ItemStack sourceTool) {
16 ItemStack createStatue(ItemStack sourceTool) {
17 return createStatue(sourceTool, Material.COPPER_GOLEM_STATUE);
18 }
19
20 ItemStack createStatue(ItemStack sourceTool, Material statueType) {
16 ItemStack tool = normalizeTool(sourceTool);
21 ItemStack tool = normalizeTool(sourceTool);
17 ItemStack item = new ItemStack(Material.COPPER_GOLEM_STATUE);
22 Material type = CopperStatueVariant.isStatue(statueType) ? statueType : Material.COPPER_GOLEM_STATUE;
23 ItemStack item = new ItemStack(type);
18 item.editMeta(meta -> {
24 item.editMeta(meta -> {
19 meta.setItemName("Copper Farmer Golem");
25 meta.setItemName("Copper Farmer Golem");
20 meta.getPersistentDataContainer().set(keys.farmerItem, PersistentDataType.BYTE, (byte) 1);
26 PersistentDataContainer pdc = meta.getPersistentDataContainer();
21 meta.getPersistentDataContainer().set(keys.tool, PersistentDataType.BYTE_ARRAY, tool.serializeAsBytes());
27 FarmerPersistence.stampItem(pdc, keys);
28 pdc.set(keys.tool, PersistentDataType.BYTE_ARRAY, tool.serializeAsBytes());
22 });
29 });
23 return item;
30 return item;
24 }
31 }
25
32
33 ItemStack createStatue(FarmerPersistence.FarmerSnapshot snapshot, Material statueType) {
34 ItemStack item = createStatue(decodeTool(snapshot.tool()), statueType);
35 item.editMeta(meta -> snapshot.apply(meta.getPersistentDataContainer(), keys));
36 return item;
37 }
38
26 boolean isFarmerItem(ItemStack item) {
39 boolean isFarmerItem(ItemStack item) {
27 return item != null && !item.isEmpty()
40 return item != null && !item.isEmpty()
28 && item.getPersistentDataContainer().has(keys.farmerItem, PersistentDataType.BYTE);
41 && FarmerPersistence.isFarmerItem(item.getPersistentDataContainer(), keys);
29 }
42 }
30
43
31 ItemStack readTool(ItemStack farmerItem) {
44 ItemStack readTool(ItemStack farmerItem) {
32 if (farmerItem == null || farmerItem.isEmpty()) return defaultTool();
45 if (farmerItem == null || farmerItem.isEmpty()) return defaultTool();
33 byte[] bytes = farmerItem.getPersistentDataContainer().get(keys.tool, PersistentDataType.BYTE_ARRAY);
46 return decodeTool(farmerItem.getPersistentDataContainer().get(keys.tool, PersistentDataType.BYTE_ARRAY));
34 return decodeTool(bytes);
35 }
47 }
36
48
37 ItemStack readTool(CopperGolem golem) {
49 ItemStack readTool(CopperGolem golem) {
38 byte[] bytes = golem.getPersistentDataContainer().get(keys.tool, PersistentDataType.BYTE_ARRAY);
50 return decodeTool(golem.getPersistentDataContainer().get(keys.tool, PersistentDataType.BYTE_ARRAY));
39 return decodeTool(bytes);
40 }
51 }
41
52
42 void storeTool(CopperGolem golem, ItemStack sourceTool) {
53 void storeTool(CopperGolem golem, ItemStack sourceTool) {
src/main/java/nl/timfalken/copperfarmer/FarmerListener.java +176 −14
@@ -2,31 +2,43 @@ package nl.timfalken.copperfarmer;
2 2
3 3 import org.bukkit.Location;
4 4 import org.bukkit.Material;
5 import org.bukkit.Tag;
5 6 import org.bukkit.block.Block;
6 7 import org.bukkit.block.BlockState;
8 import org.bukkit.block.CopperGolemStatue;
7 9 import org.bukkit.entity.CopperGolem;
8 10 import org.bukkit.entity.Entity;
9 11 import org.bukkit.event.EventHandler;
10 12 import org.bukkit.event.EventPriority;
11 13 import org.bukkit.event.Listener;
14 import org.bukkit.event.block.Action;
15 import org.bukkit.event.block.BlockDropItemEvent;
12 16 import org.bukkit.event.block.BlockPlaceEvent;
13 17 import org.bukkit.event.block.CrafterCraftEvent;
18 import org.bukkit.event.entity.EntityChangeBlockEvent;
14 19 import org.bukkit.event.entity.EntityDeathEvent;
15 20 import org.bukkit.event.inventory.PrepareItemCraftEvent;
21 import org.bukkit.event.player.PlayerInteractEvent;
16 22 import org.bukkit.event.world.EntitiesLoadEvent;
17 23 import org.bukkit.event.world.EntitiesUnloadEvent;
24 import org.bukkit.inventory.EntityEquipment;
18 25 import org.bukkit.inventory.InventoryHolder;
19 26 import org.bukkit.inventory.ItemStack;
20 27 import org.bukkit.inventory.Recipe;
21 28 import org.bukkit.inventory.ShapelessRecipe;
22 import org.bukkit.persistence.PersistentDataType;
29 import org.bukkit.persistence.PersistentDataContainer;
23 30 import org.bukkit.plugin.Plugin;
24 31
32 import java.util.HashSet;
33 import java.util.Set;
34 import java.util.UUID;
35
25 36 final class FarmerListener implements Listener {
26 37 private final Plugin plugin;
27 38 private final FarmerKeys keys;
28 39 private final FarmerManager manager;
29 40 private final FarmerItemCodec itemCodec;
41 private final Set<UUID> petrifying = new HashSet<>();
30 42
31 43 FarmerListener(Plugin plugin, FarmerKeys keys, FarmerManager manager, FarmerItemCodec itemCodec) {
32 44 this.plugin = plugin;
@@ -38,8 +50,8 @@ final class FarmerListener implements Listener {
38 50 @EventHandler(priority = EventPriority.HIGHEST)
39 51 public void onPrepareCraft(PrepareItemCraftEvent event) {
40 52 if (!isFarmerRecipe(event.getRecipe())) return;
41 ItemStack hoe = findCopperHoe(event.getInventory().getMatrix());
42 if (hoe != null) event.getInventory().setResult(itemCodec.createStatue(hoe));
53 ItemStack result = farmerResultFrom(event.getInventory().getMatrix());
54 if (result != null) event.getInventory().setResult(result);
43 55 }
44 56
45 57 @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
@@ -47,8 +59,8 @@ final class FarmerListener implements Listener {
47 59 if (!isFarmerRecipe(event.getRecipe())) return;
48 60 BlockState state = event.getBlock().getState();
49 61 if (!(state instanceof InventoryHolder holder)) return;
50 ItemStack hoe = findCopperHoe(holder.getInventory().getContents());
51 if (hoe != null) event.setResult(itemCodec.createStatue(hoe));
62 ItemStack result = farmerResultFrom(holder.getInventory().getContents());
63 if (result != null) event.setResult(result);
52 64 }
53 65
54 66 @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
@@ -56,29 +68,91 @@ final class FarmerListener implements Listener {
56 68 if (!itemCodec.isFarmerItem(event.getItemInHand())) return;
57 69
58 70 Block placed = event.getBlockPlaced();
59 if (!placed.getType().name().endsWith("COPPER_GOLEM_STATUE")) return;
71 CopperStatueVariant variant = CopperStatueVariant.from(placed.getType());
72 if (variant == null) return;
60 73
61 // Capture the exact construction tool now: the crafting recipe consumed it,
62 // and the placed item stack may be mutated by vanilla after this event.
63 ItemStack tool = itemCodec.readTool(event.getItemInHand());
74 ItemStack placedItem = event.getItemInHand();
75 FarmerPersistence.FarmerSnapshot snapshot = forgetDistantChest(
76 FarmerPersistence.capture(placedItem.getPersistentDataContainer(), keys),
77 placed.getLocation()
78 );
64 79
65 // Let successful vanilla placement consume exactly one statue, then replace
66 // that special statue with our tagged real Copper Golem on the next tick.
67 80 Location location = placed.getLocation();
68 81 plugin.getServer().getScheduler().runTask(plugin, () -> {
69 82 Block current = location.getBlock();
70 if (!current.getType().name().endsWith("COPPER_GOLEM_STATUE")) return;
83 if (CopperStatueVariant.from(current.getType()) == null) return;
71 84
72 85 current.setType(Material.AIR, false);
73 86 Location spawn = location.clone().add(0.5, 0.0, 0.5);
74 87 CopperGolem golem = location.getWorld().spawn(spawn, CopperGolem.class, entity -> {
75 entity.getPersistentDataContainer().set(keys.farmerEntity, PersistentDataType.BYTE, (byte) 1);
76 itemCodec.storeTool(entity, tool);
88 PersistentDataContainer pdc = entity.getPersistentDataContainer();
89 FarmerPersistence.stampEntity(pdc, keys);
90 snapshot.apply(pdc, keys);
91 variant.applyTo(entity);
77 92 });
78 93 manager.register(golem);
79 94 });
80 95 }
81 96
97 @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
98 public void onFarmerPetrify(EntityChangeBlockEvent event) {
99 if (!(event.getEntity() instanceof CopperGolem golem) || !manager.isFarmer(golem)) return;
100 if (!CopperStatueVariant.isStatue(event.getTo())) return;
101
102 petrifying.add(golem.getUniqueId());
103 EntityEquipment equipment = golem.getEquipment();
104 if (equipment != null) equipment.setItemInMainHand(null);
105
106 Location dropAt = golem.getLocation();
107 for (ItemStack item : manager.drainCargo(golem)) {
108 if (item != null && !item.isEmpty()) {
109 golem.getWorld().dropItemNaturally(dropAt, item);
110 }
111 }
112
113 FarmerPersistence.FarmerSnapshot snapshot = FarmerPersistence.capture(golem.getPersistentDataContainer(), keys)
114 .withoutCargo();
115 manager.unregister(golem);
116
117 Location location = event.getBlock().getLocation();
118 plugin.getServer().getScheduler().runTask(plugin, () -> {
119 petrifying.remove(golem.getUniqueId());
120 writeSnapshotToStatue(location.getBlock(), snapshot);
121 });
122 }
123
124 @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
125 public void onStatueWaxOrScrape(PlayerInteractEvent event) {
126 if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return;
127 Block block = event.getClickedBlock();
128 if (block == null || !FarmerPersistence.isFarmerStatue(block, keys)) return;
129 ItemStack item = event.getItem();
130 if (!isOxidationTool(item)) return;
131
132 CopperGolemStatue statue = (CopperGolemStatue) block.getState();
133 FarmerPersistence.FarmerSnapshot snapshot = FarmerPersistence.capture(statue.getPersistentDataContainer(), keys);
134 Location location = block.getLocation();
135 restoreAfterVanillaConversion(location, snapshot);
136 plugin.getServer().getScheduler().runTask(plugin, () -> restoreAfterVanillaConversion(location, snapshot));
137 }
138
139 @EventHandler(ignoreCancelled = true)
140 public void onFarmerStatueDrops(BlockDropItemEvent event) {
141 if (!(event.getBlockState() instanceof CopperGolemStatue statue)) return;
142 if (!FarmerPersistence.isFarmerEntity(statue.getPersistentDataContainer(), keys)) return;
143
144 FarmerPersistence.FarmerSnapshot snapshot = FarmerPersistence.capture(statue.getPersistentDataContainer(), keys)
145 .withoutCargo();
146 ItemStack farmer = itemCodec.createStatue(snapshot, event.getBlockState().getType());
147 var drops = event.getItems();
148 if (drops.isEmpty()) {
149 event.getBlock().getWorld().dropItemNaturally(event.getBlock().getLocation().add(0.5, 0.5, 0.5), farmer);
150 return;
151 }
152 drops.getFirst().setItemStack(farmer);
153 while (drops.size() > 1) drops.remove(1);
154 }
155
82 156 @EventHandler
83 157 public void onEntitiesLoad(EntitiesLoadEvent event) {
84 158 for (Entity entity : event.getEntities()) {
@@ -98,9 +172,86 @@ final class FarmerListener implements Listener {
98 172 @EventHandler(priority = EventPriority.HIGHEST)
99 173 public void onDeath(EntityDeathEvent event) {
100 174 if (!(event.getEntity() instanceof CopperGolem golem) || !manager.isFarmer(golem)) return;
175 if (petrifying.remove(golem.getUniqueId())) {
176 event.getDrops().clear();
177 event.setDroppedExp(0);
178 return;
179 }
101 180 event.getDrops().addAll(manager.deathDrops(golem));
102 181 }
103 182
183 private ItemStack farmerResultFrom(ItemStack[] contents) {
184 ItemStack hoe = findCopperHoe(contents);
185 ItemStack statue = findStatue(contents);
186 if (hoe == null || statue == null) return null;
187 if (!itemCodec.isFarmerItem(statue)) {
188 return itemCodec.createStatue(hoe, statue.getType());
189 }
190
191 FarmerPersistence.FarmerSnapshot existing = FarmerPersistence.capture(statue.getPersistentDataContainer(), keys);
192 ItemStack withNewHoe = itemCodec.createStatue(hoe, statue.getType());
193 FarmerPersistence.FarmerSnapshot hoeOnly = FarmerPersistence.capture(withNewHoe.getPersistentDataContainer(), keys);
194 return itemCodec.createStatue(new FarmerPersistence.FarmerSnapshot(
195 hoeOnly.tool(),
196 existing.cargo(),
197 existing.chestWorld(),
198 existing.chestX(),
199 existing.chestY(),
200 existing.chestZ()
201 ), statue.getType());
202 }
203
204 private FarmerPersistence.FarmerSnapshot forgetDistantChest(
205 FarmerPersistence.FarmerSnapshot snapshot,
206 Location place
207 ) {
208 if (snapshot.chestWorld() == null || snapshot.chestX() == null || snapshot.chestZ() == null) {
209 return snapshot;
210 }
211 if (place.getWorld() == null || !place.getWorld().getUID().toString().equals(snapshot.chestWorld())) {
212 return snapshot.withoutChest();
213 }
214 int radius = Math.max(0, plugin.getConfig().getInt("chest-memory-chunk-radius", 3));
215 int dx = Math.abs((snapshot.chestX() >> 4) - (place.getBlockX() >> 4));
216 int dz = Math.abs((snapshot.chestZ() >> 4) - (place.getBlockZ() >> 4));
217 if (Math.max(dx, dz) > radius) return snapshot.withoutChest();
218 return snapshot;
219 }
220
221 private void restoreAfterVanillaConversion(Location location, FarmerPersistence.FarmerSnapshot snapshot) {
222 Block block = location.getBlock();
223 if (block.getState() instanceof CopperGolemStatue statue) {
224 writeSnapshotToStatue(block, snapshot);
225 return;
226 }
227
228 Location center = location.clone().add(0.5, 0.0, 0.5);
229 for (Entity entity : location.getWorld().getNearbyEntities(center, 0.75, 1.0, 0.75)) {
230 if (!(entity instanceof CopperGolem golem)) continue;
231 if (manager.isFarmer(golem) || FarmerPersistence.isFarmerEntity(golem.getPersistentDataContainer(), keys)) {
232 continue;
233 }
234 PersistentDataContainer pdc = golem.getPersistentDataContainer();
235 FarmerPersistence.stampEntity(pdc, keys);
236 snapshot.apply(pdc, keys);
237 manager.register(golem);
238 return;
239 }
240 }
241
242 private void writeSnapshotToStatue(Block block, FarmerPersistence.FarmerSnapshot snapshot) {
243 if (!(block.getState() instanceof CopperGolemStatue statue)) return;
244 PersistentDataContainer pdc = statue.getPersistentDataContainer();
245 FarmerPersistence.stampEntity(pdc, keys);
246 snapshot.apply(pdc, keys);
247 statue.update();
248 }
249
250 private boolean isOxidationTool(ItemStack item) {
251 if (item == null || item.isEmpty()) return false;
252 return item.getType() == Material.HONEYCOMB || Tag.ITEMS_AXES.isTagged(item.getType());
253 }
254
104 255 private boolean isFarmerRecipe(Recipe recipe) {
105 256 return recipe instanceof ShapelessRecipe shapeless && shapeless.getKey().equals(keys.recipe);
106 257 }
@@ -115,4 +266,15 @@ final class FarmerListener implements Listener {
115 266 }
116 267 return null;
117 268 }
269
270 private ItemStack findStatue(ItemStack[] contents) {
271 if (contents == null) return null;
272 for (ItemStack item : contents) {
273 if (item == null || item.isEmpty() || !CopperStatueVariant.isStatue(item.getType())) continue;
274 ItemStack copy = item.clone();
275 copy.setAmount(1);
276 return copy;
277 }
278 return null;
279 }
118 280 }
@@ -2,31 +2,43 @@ package nl.timfalken.copperfarmer;
2
2
3import org.bukkit.Location;
3import org.bukkit.Location;
4import org.bukkit.Material;
4import org.bukkit.Material;
5import org.bukkit.Tag;
5import org.bukkit.block.Block;
6import org.bukkit.block.Block;
6import org.bukkit.block.BlockState;
7import org.bukkit.block.BlockState;
8import org.bukkit.block.CopperGolemStatue;
7import org.bukkit.entity.CopperGolem;
9import org.bukkit.entity.CopperGolem;
8import org.bukkit.entity.Entity;
10import org.bukkit.entity.Entity;
9import org.bukkit.event.EventHandler;
11import org.bukkit.event.EventHandler;
10import org.bukkit.event.EventPriority;
12import org.bukkit.event.EventPriority;
11import org.bukkit.event.Listener;
13import org.bukkit.event.Listener;
14import org.bukkit.event.block.Action;
15import org.bukkit.event.block.BlockDropItemEvent;
12import org.bukkit.event.block.BlockPlaceEvent;
16import org.bukkit.event.block.BlockPlaceEvent;
13import org.bukkit.event.block.CrafterCraftEvent;
17import org.bukkit.event.block.CrafterCraftEvent;
18import org.bukkit.event.entity.EntityChangeBlockEvent;
14import org.bukkit.event.entity.EntityDeathEvent;
19import org.bukkit.event.entity.EntityDeathEvent;
15import org.bukkit.event.inventory.PrepareItemCraftEvent;
20import org.bukkit.event.inventory.PrepareItemCraftEvent;
21import org.bukkit.event.player.PlayerInteractEvent;
16import org.bukkit.event.world.EntitiesLoadEvent;
22import org.bukkit.event.world.EntitiesLoadEvent;
17import org.bukkit.event.world.EntitiesUnloadEvent;
23import org.bukkit.event.world.EntitiesUnloadEvent;
24import org.bukkit.inventory.EntityEquipment;
18import org.bukkit.inventory.InventoryHolder;
25import org.bukkit.inventory.InventoryHolder;
19import org.bukkit.inventory.ItemStack;
26import org.bukkit.inventory.ItemStack;
20import org.bukkit.inventory.Recipe;
27import org.bukkit.inventory.Recipe;
21import org.bukkit.inventory.ShapelessRecipe;
28import org.bukkit.inventory.ShapelessRecipe;
22import org.bukkit.persistence.PersistentDataType;
29import org.bukkit.persistence.PersistentDataContainer;
23import org.bukkit.plugin.Plugin;
30import org.bukkit.plugin.Plugin;
24
31
32import java.util.HashSet;
33import java.util.Set;
34import java.util.UUID;
35
25final class FarmerListener implements Listener {
36final class FarmerListener implements Listener {
26 private final Plugin plugin;
37 private final Plugin plugin;
27 private final FarmerKeys keys;
38 private final FarmerKeys keys;
28 private final FarmerManager manager;
39 private final FarmerManager manager;
29 private final FarmerItemCodec itemCodec;
40 private final FarmerItemCodec itemCodec;
41 private final Set<UUID> petrifying = new HashSet<>();
30
42
31 FarmerListener(Plugin plugin, FarmerKeys keys, FarmerManager manager, FarmerItemCodec itemCodec) {
43 FarmerListener(Plugin plugin, FarmerKeys keys, FarmerManager manager, FarmerItemCodec itemCodec) {
32 this.plugin = plugin;
44 this.plugin = plugin;
@@ -38,8 +50,8 @@ final class FarmerListener implements Listener {
38 @EventHandler(priority = EventPriority.HIGHEST)
50 @EventHandler(priority = EventPriority.HIGHEST)
39 public void onPrepareCraft(PrepareItemCraftEvent event) {
51 public void onPrepareCraft(PrepareItemCraftEvent event) {
40 if (!isFarmerRecipe(event.getRecipe())) return;
52 if (!isFarmerRecipe(event.getRecipe())) return;
41 ItemStack hoe = findCopperHoe(event.getInventory().getMatrix());
53 ItemStack result = farmerResultFrom(event.getInventory().getMatrix());
42 if (hoe != null) event.getInventory().setResult(itemCodec.createStatue(hoe));
54 if (result != null) event.getInventory().setResult(result);
43 }
55 }
44
56
45 @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
57 @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
@@ -47,8 +59,8 @@ final class FarmerListener implements Listener {
47 if (!isFarmerRecipe(event.getRecipe())) return;
59 if (!isFarmerRecipe(event.getRecipe())) return;
48 BlockState state = event.getBlock().getState();
60 BlockState state = event.getBlock().getState();
49 if (!(state instanceof InventoryHolder holder)) return;
61 if (!(state instanceof InventoryHolder holder)) return;
50 ItemStack hoe = findCopperHoe(holder.getInventory().getContents());
62 ItemStack result = farmerResultFrom(holder.getInventory().getContents());
51 if (hoe != null) event.setResult(itemCodec.createStatue(hoe));
63 if (result != null) event.setResult(result);
52 }
64 }
53
65
54 @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
66 @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
@@ -56,29 +68,91 @@ final class FarmerListener implements Listener {
56 if (!itemCodec.isFarmerItem(event.getItemInHand())) return;
68 if (!itemCodec.isFarmerItem(event.getItemInHand())) return;
57
69
58 Block placed = event.getBlockPlaced();
70 Block placed = event.getBlockPlaced();
59 if (!placed.getType().name().endsWith("COPPER_GOLEM_STATUE")) return;
71 CopperStatueVariant variant = CopperStatueVariant.from(placed.getType());
72 if (variant == null) return;
60
73
61 // Capture the exact construction tool now: the crafting recipe consumed it,
74 ItemStack placedItem = event.getItemInHand();
62 // and the placed item stack may be mutated by vanilla after this event.
75 FarmerPersistence.FarmerSnapshot snapshot = forgetDistantChest(
63 ItemStack tool = itemCodec.readTool(event.getItemInHand());
76 FarmerPersistence.capture(placedItem.getPersistentDataContainer(), keys),
77 placed.getLocation()
78 );
64
79
65 // Let successful vanilla placement consume exactly one statue, then replace
66 // that special statue with our tagged real Copper Golem on the next tick.
67 Location location = placed.getLocation();
80 Location location = placed.getLocation();
68 plugin.getServer().getScheduler().runTask(plugin, () -> {
81 plugin.getServer().getScheduler().runTask(plugin, () -> {
69 Block current = location.getBlock();
82 Block current = location.getBlock();
70 if (!current.getType().name().endsWith("COPPER_GOLEM_STATUE")) return;
83 if (CopperStatueVariant.from(current.getType()) == null) return;
71
84
72 current.setType(Material.AIR, false);
85 current.setType(Material.AIR, false);
73 Location spawn = location.clone().add(0.5, 0.0, 0.5);
86 Location spawn = location.clone().add(0.5, 0.0, 0.5);
74 CopperGolem golem = location.getWorld().spawn(spawn, CopperGolem.class, entity -> {
87 CopperGolem golem = location.getWorld().spawn(spawn, CopperGolem.class, entity -> {
75 entity.getPersistentDataContainer().set(keys.farmerEntity, PersistentDataType.BYTE, (byte) 1);
88 PersistentDataContainer pdc = entity.getPersistentDataContainer();
76 itemCodec.storeTool(entity, tool);
89 FarmerPersistence.stampEntity(pdc, keys);
90 snapshot.apply(pdc, keys);
91 variant.applyTo(entity);
77 });
92 });
78 manager.register(golem);
93 manager.register(golem);
79 });
94 });
80 }
95 }
81
96
97 @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
98 public void onFarmerPetrify(EntityChangeBlockEvent event) {
99 if (!(event.getEntity() instanceof CopperGolem golem) || !manager.isFarmer(golem)) return;
100 if (!CopperStatueVariant.isStatue(event.getTo())) return;
101
102 petrifying.add(golem.getUniqueId());
103 EntityEquipment equipment = golem.getEquipment();
104 if (equipment != null) equipment.setItemInMainHand(null);
105
106 Location dropAt = golem.getLocation();
107 for (ItemStack item : manager.drainCargo(golem)) {
108 if (item != null && !item.isEmpty()) {
109 golem.getWorld().dropItemNaturally(dropAt, item);
110 }
111 }
112
113 FarmerPersistence.FarmerSnapshot snapshot = FarmerPersistence.capture(golem.getPersistentDataContainer(), keys)
114 .withoutCargo();
115 manager.unregister(golem);
116
117 Location location = event.getBlock().getLocation();
118 plugin.getServer().getScheduler().runTask(plugin, () -> {
119 petrifying.remove(golem.getUniqueId());
120 writeSnapshotToStatue(location.getBlock(), snapshot);
121 });
122 }
123
124 @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
125 public void onStatueWaxOrScrape(PlayerInteractEvent event) {
126 if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return;
127 Block block = event.getClickedBlock();
128 if (block == null || !FarmerPersistence.isFarmerStatue(block, keys)) return;
129 ItemStack item = event.getItem();
130 if (!isOxidationTool(item)) return;
131
132 CopperGolemStatue statue = (CopperGolemStatue) block.getState();
133 FarmerPersistence.FarmerSnapshot snapshot = FarmerPersistence.capture(statue.getPersistentDataContainer(), keys);
134 Location location = block.getLocation();
135 restoreAfterVanillaConversion(location, snapshot);
136 plugin.getServer().getScheduler().runTask(plugin, () -> restoreAfterVanillaConversion(location, snapshot));
137 }
138
139 @EventHandler(ignoreCancelled = true)
140 public void onFarmerStatueDrops(BlockDropItemEvent event) {
141 if (!(event.getBlockState() instanceof CopperGolemStatue statue)) return;
142 if (!FarmerPersistence.isFarmerEntity(statue.getPersistentDataContainer(), keys)) return;
143
144 FarmerPersistence.FarmerSnapshot snapshot = FarmerPersistence.capture(statue.getPersistentDataContainer(), keys)
145 .withoutCargo();
146 ItemStack farmer = itemCodec.createStatue(snapshot, event.getBlockState().getType());
147 var drops = event.getItems();
148 if (drops.isEmpty()) {
149 event.getBlock().getWorld().dropItemNaturally(event.getBlock().getLocation().add(0.5, 0.5, 0.5), farmer);
150 return;
151 }
152 drops.getFirst().setItemStack(farmer);
153 while (drops.size() > 1) drops.remove(1);
154 }
155
82 @EventHandler
156 @EventHandler
83 public void onEntitiesLoad(EntitiesLoadEvent event) {
157 public void onEntitiesLoad(EntitiesLoadEvent event) {
84 for (Entity entity : event.getEntities()) {
158 for (Entity entity : event.getEntities()) {
@@ -98,9 +172,86 @@ final class FarmerListener implements Listener {
98 @EventHandler(priority = EventPriority.HIGHEST)
172 @EventHandler(priority = EventPriority.HIGHEST)
99 public void onDeath(EntityDeathEvent event) {
173 public void onDeath(EntityDeathEvent event) {
100 if (!(event.getEntity() instanceof CopperGolem golem) || !manager.isFarmer(golem)) return;
174 if (!(event.getEntity() instanceof CopperGolem golem) || !manager.isFarmer(golem)) return;
175 if (petrifying.remove(golem.getUniqueId())) {
176 event.getDrops().clear();
177 event.setDroppedExp(0);
178 return;
179 }
101 event.getDrops().addAll(manager.deathDrops(golem));
180 event.getDrops().addAll(manager.deathDrops(golem));
102 }
181 }
103
182
183 private ItemStack farmerResultFrom(ItemStack[] contents) {
184 ItemStack hoe = findCopperHoe(contents);
185 ItemStack statue = findStatue(contents);
186 if (hoe == null || statue == null) return null;
187 if (!itemCodec.isFarmerItem(statue)) {
188 return itemCodec.createStatue(hoe, statue.getType());
189 }
190
191 FarmerPersistence.FarmerSnapshot existing = FarmerPersistence.capture(statue.getPersistentDataContainer(), keys);
192 ItemStack withNewHoe = itemCodec.createStatue(hoe, statue.getType());
193 FarmerPersistence.FarmerSnapshot hoeOnly = FarmerPersistence.capture(withNewHoe.getPersistentDataContainer(), keys);
194 return itemCodec.createStatue(new FarmerPersistence.FarmerSnapshot(
195 hoeOnly.tool(),
196 existing.cargo(),
197 existing.chestWorld(),
198 existing.chestX(),
199 existing.chestY(),
200 existing.chestZ()
201 ), statue.getType());
202 }
203
204 private FarmerPersistence.FarmerSnapshot forgetDistantChest(
205 FarmerPersistence.FarmerSnapshot snapshot,
206 Location place
207 ) {
208 if (snapshot.chestWorld() == null || snapshot.chestX() == null || snapshot.chestZ() == null) {
209 return snapshot;
210 }
211 if (place.getWorld() == null || !place.getWorld().getUID().toString().equals(snapshot.chestWorld())) {
212 return snapshot.withoutChest();
213 }
214 int radius = Math.max(0, plugin.getConfig().getInt("chest-memory-chunk-radius", 3));
215 int dx = Math.abs((snapshot.chestX() >> 4) - (place.getBlockX() >> 4));
216 int dz = Math.abs((snapshot.chestZ() >> 4) - (place.getBlockZ() >> 4));
217 if (Math.max(dx, dz) > radius) return snapshot.withoutChest();
218 return snapshot;
219 }
220
221 private void restoreAfterVanillaConversion(Location location, FarmerPersistence.FarmerSnapshot snapshot) {
222 Block block = location.getBlock();
223 if (block.getState() instanceof CopperGolemStatue statue) {
224 writeSnapshotToStatue(block, snapshot);
225 return;
226 }
227
228 Location center = location.clone().add(0.5, 0.0, 0.5);
229 for (Entity entity : location.getWorld().getNearbyEntities(center, 0.75, 1.0, 0.75)) {
230 if (!(entity instanceof CopperGolem golem)) continue;
231 if (manager.isFarmer(golem) || FarmerPersistence.isFarmerEntity(golem.getPersistentDataContainer(), keys)) {
232 continue;
233 }
234 PersistentDataContainer pdc = golem.getPersistentDataContainer();
235 FarmerPersistence.stampEntity(pdc, keys);
236 snapshot.apply(pdc, keys);
237 manager.register(golem);
238 return;
239 }
240 }
241
242 private void writeSnapshotToStatue(Block block, FarmerPersistence.FarmerSnapshot snapshot) {
243 if (!(block.getState() instanceof CopperGolemStatue statue)) return;
244 PersistentDataContainer pdc = statue.getPersistentDataContainer();
245 FarmerPersistence.stampEntity(pdc, keys);
246 snapshot.apply(pdc, keys);
247 statue.update();
248 }
249
250 private boolean isOxidationTool(ItemStack item) {
251 if (item == null || item.isEmpty()) return false;
252 return item.getType() == Material.HONEYCOMB || Tag.ITEMS_AXES.isTagged(item.getType());
253 }
254
104 private boolean isFarmerRecipe(Recipe recipe) {
255 private boolean isFarmerRecipe(Recipe recipe) {
105 return recipe instanceof ShapelessRecipe shapeless && shapeless.getKey().equals(keys.recipe);
256 return recipe instanceof ShapelessRecipe shapeless && shapeless.getKey().equals(keys.recipe);
106 }
257 }
@@ -115,4 +266,15 @@ final class FarmerListener implements Listener {
115 }
266 }
116 return null;
267 return null;
117 }
268 }
269
270 private ItemStack findStatue(ItemStack[] contents) {
271 if (contents == null) return null;
272 for (ItemStack item : contents) {
273 if (item == null || item.isEmpty() || !CopperStatueVariant.isStatue(item.getType())) continue;
274 ItemStack copy = item.clone();
275 copy.setAmount(1);
276 return copy;
277 }
278 return null;
279 }
118}
280}
src/main/java/nl/timfalken/copperfarmer/FarmerManager.java +9 −1
@@ -81,7 +81,7 @@ final class FarmerManager {
81 81 this.plantWindupTicks = Math.max(0L, plugin.getConfig().getLong("plant-windup-ticks", 10L));
82 82 this.afterDepositScanTicks = Math.max(1L, plugin.getConfig().getLong("after-deposit-scan-ticks", 20L));
83 83 this.cargoSlots = Math.max(1, plugin.getConfig().getInt("cargo-slots", 9));
84 this.preventOxidation = plugin.getConfig().getBoolean("prevent-oxidation", true);
84 this.preventOxidation = plugin.getConfig().getBoolean("prevent-oxidation", false);
85 85 }
86 86
87 87 void tick(long elapsedTicks) {
@@ -135,6 +135,14 @@ final class FarmerManager {
135 135 }
136 136 }
137 137
138 List<ItemStack> drainCargo(CopperGolem golem) {
139 Brain brain = brains.get(golem.getUniqueId());
140 FarmerCargo cargo = brain != null ? brain.cargo : new FarmerCargo(golem, keys, cargoSlots);
141 List<ItemStack> items = cargo.snapshot();
142 cargo.clear();
143 return items;
144 }
145
138 146 List<ItemStack> deathDrops(CopperGolem golem) {
139 147 Brain brain = brains.remove(golem.getUniqueId());
140 148 if (brain != null) brain.closeOpenedLid();
@@ -81,7 +81,7 @@ final class FarmerManager {
81 this.plantWindupTicks = Math.max(0L, plugin.getConfig().getLong("plant-windup-ticks", 10L));
81 this.plantWindupTicks = Math.max(0L, plugin.getConfig().getLong("plant-windup-ticks", 10L));
82 this.afterDepositScanTicks = Math.max(1L, plugin.getConfig().getLong("after-deposit-scan-ticks", 20L));
82 this.afterDepositScanTicks = Math.max(1L, plugin.getConfig().getLong("after-deposit-scan-ticks", 20L));
83 this.cargoSlots = Math.max(1, plugin.getConfig().getInt("cargo-slots", 9));
83 this.cargoSlots = Math.max(1, plugin.getConfig().getInt("cargo-slots", 9));
84 this.preventOxidation = plugin.getConfig().getBoolean("prevent-oxidation", true);
84 this.preventOxidation = plugin.getConfig().getBoolean("prevent-oxidation", false);
85 }
85 }
86
86
87 void tick(long elapsedTicks) {
87 void tick(long elapsedTicks) {
@@ -135,6 +135,14 @@ final class FarmerManager {
135 }
135 }
136 }
136 }
137
137
138 List<ItemStack> drainCargo(CopperGolem golem) {
139 Brain brain = brains.get(golem.getUniqueId());
140 FarmerCargo cargo = brain != null ? brain.cargo : new FarmerCargo(golem, keys, cargoSlots);
141 List<ItemStack> items = cargo.snapshot();
142 cargo.clear();
143 return items;
144 }
145
138 List<ItemStack> deathDrops(CopperGolem golem) {
146 List<ItemStack> deathDrops(CopperGolem golem) {
139 Brain brain = brains.remove(golem.getUniqueId());
147 Brain brain = brains.remove(golem.getUniqueId());
140 if (brain != null) brain.closeOpenedLid();
148 if (brain != null) brain.closeOpenedLid();
src/main/java/nl/timfalken/copperfarmer/FarmerPersistence.java added +80 −0
@@ -0,0 +1,80 @@
1 package nl.timfalken.copperfarmer;
2
3 import org.bukkit.block.Block;
4 import org.bukkit.block.CopperGolemStatue;
5 import org.bukkit.persistence.PersistentDataContainer;
6 import org.bukkit.persistence.PersistentDataType;
7
8 final class FarmerPersistence {
9 private FarmerPersistence() {}
10
11 static boolean isFarmerItem(PersistentDataContainer pdc, FarmerKeys keys) {
12 return pdc.has(keys.farmerItem, PersistentDataType.BYTE);
13 }
14
15 static boolean isFarmerEntity(PersistentDataContainer pdc, FarmerKeys keys) {
16 return pdc.has(keys.farmerEntity, PersistentDataType.BYTE);
17 }
18
19 static boolean isFarmerStatue(Block block, FarmerKeys keys) {
20 return block.getState() instanceof CopperGolemStatue statue
21 && isFarmerEntity(statue.getPersistentDataContainer(), keys);
22 }
23
24 static FarmerSnapshot capture(PersistentDataContainer pdc, FarmerKeys keys) {
25 return new FarmerSnapshot(
26 pdc.get(keys.tool, PersistentDataType.BYTE_ARRAY),
27 pdc.get(keys.cargo, PersistentDataType.BYTE_ARRAY),
28 pdc.get(keys.chestWorld, PersistentDataType.STRING),
29 pdc.get(keys.chestX, PersistentDataType.INTEGER),
30 pdc.get(keys.chestY, PersistentDataType.INTEGER),
31 pdc.get(keys.chestZ, PersistentDataType.INTEGER)
32 );
33 }
34
35 static void stampItem(PersistentDataContainer pdc, FarmerKeys keys) {
36 pdc.set(keys.farmerItem, PersistentDataType.BYTE, (byte) 1);
37 pdc.remove(keys.farmerEntity);
38 }
39
40 static void stampEntity(PersistentDataContainer pdc, FarmerKeys keys) {
41 pdc.set(keys.farmerEntity, PersistentDataType.BYTE, (byte) 1);
42 pdc.remove(keys.farmerItem);
43 }
44
45 record FarmerSnapshot(
46 byte[] tool,
47 byte[] cargo,
48 String chestWorld,
49 Integer chestX,
50 Integer chestY,
51 Integer chestZ
52 ) {
53 FarmerSnapshot withoutCargo() {
54 return new FarmerSnapshot(tool, null, chestWorld, chestX, chestY, chestZ);
55 }
56
57 FarmerSnapshot withoutChest() {
58 return new FarmerSnapshot(tool, cargo, null, null, null, null);
59 }
60
61 void apply(PersistentDataContainer pdc, FarmerKeys keys) {
62 setOrRemove(pdc, keys.tool, PersistentDataType.BYTE_ARRAY, tool);
63 setOrRemove(pdc, keys.cargo, PersistentDataType.BYTE_ARRAY, cargo);
64 setOrRemove(pdc, keys.chestWorld, PersistentDataType.STRING, chestWorld);
65 setOrRemove(pdc, keys.chestX, PersistentDataType.INTEGER, chestX);
66 setOrRemove(pdc, keys.chestY, PersistentDataType.INTEGER, chestY);
67 setOrRemove(pdc, keys.chestZ, PersistentDataType.INTEGER, chestZ);
68 }
69
70 private static <T> void setOrRemove(
71 PersistentDataContainer pdc,
72 org.bukkit.NamespacedKey key,
73 PersistentDataType<T, T> type,
74 T value
75 ) {
76 if (value == null) pdc.remove(key);
77 else pdc.set(key, type, value);
78 }
79 }
80 }
@@ -0,0 +1,80 @@
1package nl.timfalken.copperfarmer;
2
3import org.bukkit.block.Block;
4import org.bukkit.block.CopperGolemStatue;
5import org.bukkit.persistence.PersistentDataContainer;
6import org.bukkit.persistence.PersistentDataType;
7
8final class FarmerPersistence {
9 private FarmerPersistence() {}
10
11 static boolean isFarmerItem(PersistentDataContainer pdc, FarmerKeys keys) {
12 return pdc.has(keys.farmerItem, PersistentDataType.BYTE);
13 }
14
15 static boolean isFarmerEntity(PersistentDataContainer pdc, FarmerKeys keys) {
16 return pdc.has(keys.farmerEntity, PersistentDataType.BYTE);
17 }
18
19 static boolean isFarmerStatue(Block block, FarmerKeys keys) {
20 return block.getState() instanceof CopperGolemStatue statue
21 && isFarmerEntity(statue.getPersistentDataContainer(), keys);
22 }
23
24 static FarmerSnapshot capture(PersistentDataContainer pdc, FarmerKeys keys) {
25 return new FarmerSnapshot(
26 pdc.get(keys.tool, PersistentDataType.BYTE_ARRAY),
27 pdc.get(keys.cargo, PersistentDataType.BYTE_ARRAY),
28 pdc.get(keys.chestWorld, PersistentDataType.STRING),
29 pdc.get(keys.chestX, PersistentDataType.INTEGER),
30 pdc.get(keys.chestY, PersistentDataType.INTEGER),
31 pdc.get(keys.chestZ, PersistentDataType.INTEGER)
32 );
33 }
34
35 static void stampItem(PersistentDataContainer pdc, FarmerKeys keys) {
36 pdc.set(keys.farmerItem, PersistentDataType.BYTE, (byte) 1);
37 pdc.remove(keys.farmerEntity);
38 }
39
40 static void stampEntity(PersistentDataContainer pdc, FarmerKeys keys) {
41 pdc.set(keys.farmerEntity, PersistentDataType.BYTE, (byte) 1);
42 pdc.remove(keys.farmerItem);
43 }
44
45 record FarmerSnapshot(
46 byte[] tool,
47 byte[] cargo,
48 String chestWorld,
49 Integer chestX,
50 Integer chestY,
51 Integer chestZ
52 ) {
53 FarmerSnapshot withoutCargo() {
54 return new FarmerSnapshot(tool, null, chestWorld, chestX, chestY, chestZ);
55 }
56
57 FarmerSnapshot withoutChest() {
58 return new FarmerSnapshot(tool, cargo, null, null, null, null);
59 }
60
61 void apply(PersistentDataContainer pdc, FarmerKeys keys) {
62 setOrRemove(pdc, keys.tool, PersistentDataType.BYTE_ARRAY, tool);
63 setOrRemove(pdc, keys.cargo, PersistentDataType.BYTE_ARRAY, cargo);
64 setOrRemove(pdc, keys.chestWorld, PersistentDataType.STRING, chestWorld);
65 setOrRemove(pdc, keys.chestX, PersistentDataType.INTEGER, chestX);
66 setOrRemove(pdc, keys.chestY, PersistentDataType.INTEGER, chestY);
67 setOrRemove(pdc, keys.chestZ, PersistentDataType.INTEGER, chestZ);
68 }
69
70 private static <T> void setOrRemove(
71 PersistentDataContainer pdc,
72 org.bukkit.NamespacedKey key,
73 PersistentDataType<T, T> type,
74 T value
75 ) {
76 if (value == null) pdc.remove(key);
77 else pdc.set(key, type, value);
78 }
79 }
80}
src/main/resources/config.yml +7 −3
@@ -1,6 +1,10 @@
1 1 # How many chunks around a newly spawned / chestless farmer are searched.
2 2 chest-search-chunk-radius: 2
3 3
4 # When a farmer statue is placed, forget the bound chest if it is farther than this
5 # Chebyshev chunk distance (default: more than 3 chunks away).
6 chest-memory-chunk-radius: 3
7
4 8 # Crops must be within this Euclidean distance of the bound chest.
5 9 crop-radius: 15
6 10
@@ -35,6 +39,6 @@ after-deposit-scan-ticks: 20
35 39 # Maximum internal cargo stacks. Partial stacks are combined first.
36 40 cargo-slots: 9
37 41
38 # Farmer golems are waxed internally so they do not eventually oxidise into statues.
39 # Set false if you deliberately want normal Copper Golem weathering.
40 prevent-oxidation: true
42 # When true, farmers are kept waxed so they never weather into statues.
43 # Leave false for vanilla oxidation, honeycomb waxing and axe scraping.
44 prevent-oxidation: false
@@ -1,6 +1,10 @@
1# How many chunks around a newly spawned / chestless farmer are searched.
1# How many chunks around a newly spawned / chestless farmer are searched.
2chest-search-chunk-radius: 2
2chest-search-chunk-radius: 2
3
3
4# When a farmer statue is placed, forget the bound chest if it is farther than this
5# Chebyshev chunk distance (default: more than 3 chunks away).
6chest-memory-chunk-radius: 3
7
4# Crops must be within this Euclidean distance of the bound chest.
8# Crops must be within this Euclidean distance of the bound chest.
5crop-radius: 15
9crop-radius: 15
6
10
@@ -35,6 +39,6 @@ after-deposit-scan-ticks: 20
35# Maximum internal cargo stacks. Partial stacks are combined first.
39# Maximum internal cargo stacks. Partial stacks are combined first.
36cargo-slots: 9
40cargo-slots: 9
37
41
38# Farmer golems are waxed internally so they do not eventually oxidise into statues.
42# When true, farmers are kept waxed so they never weather into statues.
39# Set false if you deliberately want normal Copper Golem weathering.
43# Leave false for vanilla oxidation, honeycomb waxing and axe scraping.
40prevent-oxidation: true
44prevent-oxidation: false