diff --git a/src/main/java/de/doggoat/growableRebalance/GrowableRebalance.java b/src/main/java/de/doggoat/growableRebalance/GrowableRebalance.java index 146ab81..e5e72b6 100644 --- a/src/main/java/de/doggoat/growableRebalance/GrowableRebalance.java +++ b/src/main/java/de/doggoat/growableRebalance/GrowableRebalance.java @@ -1,6 +1,9 @@ package de.doggoat.growableRebalance; +import de.doggoat.growableRebalance.listeners.BlockGrowEventListener; +import de.doggoat.growableRebalance.listeners.BlockSpreadEventListener; import de.doggoat.growableRebalance.listeners.GrowthEventListener; +import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; public final class GrowableRebalance extends JavaPlugin { @@ -8,7 +11,10 @@ public final class GrowableRebalance extends JavaPlugin { @Override public void onEnable() { // Plugin startup logic - getServer().getPluginManager().registerEvents(new GrowthEventListener(), this); + PluginManager pluginManager = getServer().getPluginManager(); + pluginManager.registerEvents(new GrowthEventListener(this), this); + pluginManager.registerEvents(new BlockGrowEventListener(this), this); + pluginManager.registerEvents(new BlockSpreadEventListener(this), this); } @Override diff --git a/src/main/java/de/doggoat/growableRebalance/PlantUtils.java b/src/main/java/de/doggoat/growableRebalance/PlantUtils.java new file mode 100644 index 0000000..5eac8f8 --- /dev/null +++ b/src/main/java/de/doggoat/growableRebalance/PlantUtils.java @@ -0,0 +1,19 @@ +package de.doggoat.growableRebalance; + +import org.bukkit.Location; +import org.bukkit.Material; + +public class PlantUtils { + public static Location getVerticalPlantGround(Location plantStart) { + Material plantMaterial = plantStart.getBlock().getType(); + Location plantGround = plantStart; + while (plantStart.getBlock().getType() == plantMaterial) { + plantGround = plantGround.add(0, -1, 0); + } + return plantGround; + } + + public static Location getSingleBlockGround(Location plantStart) { + return plantStart.add(0, -1, 0); + } +} diff --git a/src/main/java/de/doggoat/growableRebalance/listeners/BlockGrowEventListener.java b/src/main/java/de/doggoat/growableRebalance/listeners/BlockGrowEventListener.java new file mode 100644 index 0000000..42f20c2 --- /dev/null +++ b/src/main/java/de/doggoat/growableRebalance/listeners/BlockGrowEventListener.java @@ -0,0 +1,70 @@ +package de.doggoat.growableRebalance.listeners; + +import de.doggoat.growableRebalance.PlantUtils; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockGrowEvent; +import org.bukkit.plugin.java.JavaPlugin; +import java.util.logging.Logger; +import java.util.Arrays; +import java.util.List; + +public class BlockGrowEventListener implements Listener { + + List forbiddenGrowthBlocks = Arrays.asList( + Material.MOSS_BLOCK, Material.MUD, + Material.MUDDY_MANGROVE_ROOTS, Material.SUSPICIOUS_GRAVEL, + Material.SUSPICIOUS_SAND + ); + List allowedGourdGrowthBlocks = Arrays.asList(Material.GRASS_BLOCK, Material.DIRT, Material.FARMLAND); + JavaPlugin plugin; + Logger log; + + public BlockGrowEventListener(JavaPlugin plugin) { + this.plugin = plugin; + this.log = plugin.getLogger(); + } + + @EventHandler + public void onBlockGrow(BlockGrowEvent e) { + switch (e.getNewState().getType()) { + case SWEET_BERRY_BUSH: + this.onGroundBelow(e); + break; + case SUGAR_CANE: + case CACTUS: + this.onStackableGrow(e); + break; + case MELON: + case PUMPKIN: + this.onGourdGrow(e); + break; + } + + } + + public void onGourdGrow(BlockGrowEvent e) { + Location ground = PlantUtils.getSingleBlockGround(e.getBlock().getLocation()); + if (!allowedGourdGrowthBlocks.contains(ground.getBlock().getType())) { + e.setCancelled(true); + } + } + + public void onGroundBelow(BlockGrowEvent e) { + Location ground = PlantUtils.getVerticalPlantGround(e.getBlock().getLocation()); + if (forbiddenGrowthBlocks.contains(ground.getBlock().getType())) { + e.setCancelled(true); + } + } + + public void onStackableGrow(BlockGrowEvent e) { + Location plantStart = e.getBlock().getLocation().add(0, -1, 0); + Location ground = PlantUtils.getVerticalPlantGround(plantStart); + if (forbiddenGrowthBlocks.contains(ground.getBlock().getType())) { + e.setCancelled(true); + } + } + +} diff --git a/src/main/java/de/doggoat/growableRebalance/listeners/BlockSpreadEventListener.java b/src/main/java/de/doggoat/growableRebalance/listeners/BlockSpreadEventListener.java new file mode 100644 index 0000000..a2efa43 --- /dev/null +++ b/src/main/java/de/doggoat/growableRebalance/listeners/BlockSpreadEventListener.java @@ -0,0 +1,62 @@ +package de.doggoat.growableRebalance.listeners; + +import de.doggoat.growableRebalance.PlantUtils; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.event.Listener; +import org.bukkit.event.EventHandler; +import org.bukkit.event.block.BlockSpreadEvent; +import org.bukkit.plugin.java.JavaPlugin; +import java.util.logging.Logger; +import java.util.Arrays; +import java.util.List; + +public class BlockSpreadEventListener implements Listener { + + JavaPlugin plugin; + Logger log; + List allowedKelpGrowthBlocks = Arrays.asList( + Material.DIRT, Material.SAND, Material.RED_SAND + ); + List forbiddenBambooGrowthBlocks = Arrays.asList( + Material.MOSS_BLOCK, Material.MUD, Material.MUDDY_MANGROVE_ROOTS, + Material.GRAVEL, Material.SUSPICIOUS_GRAVEL, + Material.SAND, Material.SUSPICIOUS_SAND + ); + + public BlockSpreadEventListener(JavaPlugin plugin) { + this.plugin = plugin; + this.log = plugin.getLogger(); + } + + @EventHandler + public void onBlockSpread(BlockSpreadEvent e) { + switch (e.getSource().getType()) { + case Material.KELP: + this.onKelpSpread(e); + break; + case Material.BAMBOO: + this.onBambooSpread(e); + break; + default: + log.info("BlockSpreadEvent: Source " + e.getSource().getType()); + break; + } + } + + public void onKelpSpread(BlockSpreadEvent e) { + Location plantStart = e.getSource().getLocation(); + Location plantGround = PlantUtils.getVerticalPlantGround(plantStart); + if (!allowedKelpGrowthBlocks.contains(plantGround.getBlock().getType())) { + e.setCancelled(true); + } + } + + public void onBambooSpread(BlockSpreadEvent e) { + Location plantStart = e.getBlock().getLocation().add(0, -1, 0); + Location plantGround = PlantUtils.getVerticalPlantGround(plantStart); + if (forbiddenBambooGrowthBlocks.contains(plantGround.getBlock().getType())) { + e.setCancelled(true); + } + } +} diff --git a/src/main/java/de/doggoat/growableRebalance/listeners/GrowthEventListener.java b/src/main/java/de/doggoat/growableRebalance/listeners/GrowthEventListener.java index 30f584f..407ad9f 100644 --- a/src/main/java/de/doggoat/growableRebalance/listeners/GrowthEventListener.java +++ b/src/main/java/de/doggoat/growableRebalance/listeners/GrowthEventListener.java @@ -5,7 +5,8 @@ import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.world.StructureGrowEvent; - +import org.bukkit.plugin.java.JavaPlugin; +import java.util.logging.Logger; import java.util.Arrays; import java.util.List; @@ -13,12 +14,19 @@ public class GrowthEventListener implements Listener { List dirtBlocks = Arrays.asList(Material.ROOTED_DIRT, Material.PODZOL); List unreplaceableBlocks = Arrays.asList(Material.MOSS_BLOCK, Material.MUD, Material.MUDDY_MANGROVE_ROOTS); + JavaPlugin plugin; + Logger log; + + public GrowthEventListener(JavaPlugin plugin) { + this.plugin = plugin; + log = plugin.getLogger(); + } @EventHandler public void onStructureGrow(StructureGrowEvent e) { Player p = e.getPlayer(); List forbiddenBlocks = e.getBlocks().stream() - .filter(blockState-> unreplaceableBlocks.contains(blockState.getBlock().getType())) + .filter(blockState -> unreplaceableBlocks.contains(blockState.getBlock().getType())) .filter(blockState -> dirtBlocks.contains(blockState.getType())) .toList();