Skip to content

Commit

Permalink
feat: optimize spread growth like bamboo
Browse files Browse the repository at this point in the history
  • Loading branch information
alvindimas05 committed Jan 17, 2025
1 parent 10b722e commit 9dc0c16
Showing 1 changed file with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,43 @@
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockGrowEvent;
import org.bukkit.event.block.BlockSpreadEvent;
import org.bukkit.inventory.ItemStack;

public class GrowableStack implements Listener {
private static WeakHashMap<Chunk, WeakHashMap<Material, Integer>> counts = new WeakHashMap<>();

@EventHandler
public void onCropGrowdth(BlockGrowEvent e) {
public <T> void onCropGrow(T e) {
if (!Main.config.getBoolean("microfeatures.stack-growables.enable")) {
return;
}

BlockState b = e.getNewState();
Chunk chk = b.getChunk();
BlockState b = null;
if(e instanceof BlockGrowEvent) {
b = ((BlockGrowEvent) e).getNewState();
}
if(e instanceof BlockSpreadEvent) {
b = ((BlockSpreadEvent) e).getNewState();
}

assert b != null;
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(e instanceof BlockGrowEvent) {
((BlockGrowEvent) e).setCancelled(true);
}
if(e instanceof BlockSpreadEvent) {
((BlockSpreadEvent) 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);
Expand All @@ -50,4 +62,13 @@ public void onCropGrowdth(BlockGrowEvent e) {
});
}

@EventHandler
public void onBlockGrow(BlockGrowEvent e) {
onCropGrow(e);
}

@EventHandler
public void onBlockSpread(BlockSpreadEvent e) {
onCropGrow(e);
}
}

0 comments on commit 9dc0c16

Please sign in to comment.