From a60e4910dd61c256e9fa602f036a4aa43805e5a2 Mon Sep 17 00:00:00 2001 From: Stefatorus Date: Sun, 18 Apr 2021 13:55:37 +0300 Subject: [PATCH] Farm stacker - additional commit --- .../microfeatures/GrowableStack.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/cx/sfy/LagAssist/microfeatures/GrowableStack.java diff --git a/src/cx/sfy/LagAssist/microfeatures/GrowableStack.java b/src/cx/sfy/LagAssist/microfeatures/GrowableStack.java new file mode 100644 index 0000000..15cc184 --- /dev/null +++ b/src/cx/sfy/LagAssist/microfeatures/GrowableStack.java @@ -0,0 +1,54 @@ +package cx.sfy.LagAssist.microfeatures; + +import java.util.WeakHashMap; + +import org.bukkit.Chunk; +import org.bukkit.Material; +import org.bukkit.block.BlockState; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockGrowEvent; +import org.bukkit.inventory.ItemStack; + +import cx.sfy.LagAssist.Main; + +public class GrowableStack implements Listener { + private static WeakHashMap> counts = new WeakHashMap<>(); + + @EventHandler + public void onCropGrowdth(BlockGrowEvent e) { + if (!Main.config.getBoolean("microfeatures.stack-growables.enable")) { + return; + } + + BlockState b = e.getNewState(); + Chunk chk = b.getChunk(); + + Material mat = b.getType(); + + if (!Main.config.getStringList("microfeatures.stack-growables.blocks").contains(mat.toString())) { + return; + } + + int current = counts.getOrDefault(chk, new WeakHashMap<>()).getOrDefault(mat, 0) + 1; + int stacksize = Main.config.getInt("microfeatures.stack-growables.stacksize"); + + e.setCancelled(true); + + if (current % stacksize == 0) { + b.getLocation().getWorld().dropItemNaturally(b.getLocation(), new ItemStack(mat, stacksize)); + // Main.sendDebug("Cactus stack in chunk " + chk.getX() + " " + chk.getZ() + " at " + current + " for " + mat.toString(), 2); + } + + counts.compute(chk, (k, v) -> { + if (v == null) { + v = new WeakHashMap<>(); + } + + v.put(mat, current); + + return v; + }); + } + +}