Prevent growth as mentioned in design doc for most plants

This commit is contained in:
Leon Grünewald 2025-02-24 01:11:46 +01:00
parent 762bcd5a50
commit 6048116504
5 changed files with 168 additions and 3 deletions

View file

@ -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

View file

@ -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);
}
}

View file

@ -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<Material> forbiddenGrowthBlocks = Arrays.asList(
Material.MOSS_BLOCK, Material.MUD,
Material.MUDDY_MANGROVE_ROOTS, Material.SUSPICIOUS_GRAVEL,
Material.SUSPICIOUS_SAND
);
List<Material> 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);
}
}
}

View file

@ -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<Material> allowedKelpGrowthBlocks = Arrays.asList(
Material.DIRT, Material.SAND, Material.RED_SAND
);
List<Material> 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);
}
}
}

View file

@ -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<Material> dirtBlocks = Arrays.asList(Material.ROOTED_DIRT, Material.PODZOL);
List<Material> 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<BlockState> 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();