From 029f867a4146bfa97acd10ed625ea6729261fc4a Mon Sep 17 00:00:00 2001 From: wizjany Date: Mon, 27 May 2019 20:42:17 -0400 Subject: [PATCH] Add crop-growth flag/config option. Fixes WORLDGUARD-4088. --- .../worldguard/config/WorldConfiguration.java | 1 + .../worldguard/protection/flags/Flags.java | 1 + .../bukkit/BukkitWorldConfiguration.java | 1 + .../listener/WorldGuardBlockListener.java | 20 +++++++++++++++++++ .../worldguard/bukkit/util/Materials.java | 14 +++++++++++++ 5 files changed, 37 insertions(+) diff --git a/worldguard-core/src/main/java/com/sk89q/worldguard/config/WorldConfiguration.java b/worldguard-core/src/main/java/com/sk89q/worldguard/config/WorldConfiguration.java index 4c09c178..a5a69a67 100644 --- a/worldguard-core/src/main/java/com/sk89q/worldguard/config/WorldConfiguration.java +++ b/worldguard-core/src/main/java/com/sk89q/worldguard/config/WorldConfiguration.java @@ -145,6 +145,7 @@ public abstract class WorldConfiguration { public boolean disableGrassGrowth; public boolean disableMyceliumSpread; public boolean disableVineGrowth; + public boolean disableCropGrowth; public boolean disableEndermanGriefing; public boolean disableSnowmanTrails; public boolean disableSoilDehydration; diff --git a/worldguard-core/src/main/java/com/sk89q/worldguard/protection/flags/Flags.java b/worldguard-core/src/main/java/com/sk89q/worldguard/protection/flags/Flags.java index b9a1b324..cf3cae25 100644 --- a/worldguard-core/src/main/java/com/sk89q/worldguard/protection/flags/Flags.java +++ b/worldguard-core/src/main/java/com/sk89q/worldguard/protection/flags/Flags.java @@ -114,6 +114,7 @@ public final class Flags { public static final StateFlag GRASS_SPREAD = register(new StateFlag("grass-growth", true)); public static final StateFlag MYCELIUM_SPREAD = register(new StateFlag("mycelium-spread", true)); public static final StateFlag VINE_GROWTH = register(new StateFlag("vine-growth", true)); + public static final StateFlag CROP_GROWTH = register(new StateFlag("crop-growth", true)); public static final StateFlag SOIL_DRY = register(new StateFlag("soil-dry", true)); public static final StateFlag WATER_FLOW = register(new StateFlag("water-flow", true)); public static final StateFlag LAVA_FLOW = register(new StateFlag("lava-flow", true)); diff --git a/worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/BukkitWorldConfiguration.java b/worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/BukkitWorldConfiguration.java index 878fed00..1f1421c4 100644 --- a/worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/BukkitWorldConfiguration.java +++ b/worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/BukkitWorldConfiguration.java @@ -244,6 +244,7 @@ public void loadConfiguration() { disableGrassGrowth = getBoolean("dynamics.disable-grass-growth", false); disableMyceliumSpread = getBoolean("dynamics.disable-mycelium-spread", false); disableVineGrowth = getBoolean("dynamics.disable-vine-growth", false); + disableCropGrowth = getBoolean("dynamics.disable-crop-growth", false); disableSoilDehydration = getBoolean("dynamics.disable-soil-dehydration", false); allowedSnowFallOver = new HashSet<>(convertLegacyBlocks(getStringList("dynamics.snow-fall-blocks", null))); diff --git a/worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/listener/WorldGuardBlockListener.java b/worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/listener/WorldGuardBlockListener.java index 63df9daf..b34ca295 100644 --- a/worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/listener/WorldGuardBlockListener.java +++ b/worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/listener/WorldGuardBlockListener.java @@ -45,6 +45,7 @@ import org.bukkit.event.block.BlockFadeEvent; import org.bukkit.event.block.BlockFormEvent; import org.bukkit.event.block.BlockFromToEvent; +import org.bukkit.event.block.BlockGrowEvent; import org.bukkit.event.block.BlockIgniteEvent; import org.bukkit.event.block.BlockIgniteEvent.IgniteCause; import org.bukkit.event.block.BlockPhysicsEvent; @@ -616,6 +617,25 @@ public void onBlockSpread(BlockSpreadEvent event) { } } + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) + public void onBlockGrow(BlockGrowEvent event) { + WorldConfiguration wcfg = getWorldConfig(event.getBlock().getWorld()); + final Material type = event.getBlock().getType(); + + if (Materials.isCrop(type)) { + if (wcfg.disableCropGrowth) { + event.setCancelled(false); + return; + } + + if (wcfg.useRegions && !StateFlag.test(WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery() + .queryState(BukkitAdapter.adapt(event.getBlock().getLocation()), (RegionAssociable) null, Flags.CROP_GROWTH))) { + event.setCancelled(true); + return; + } + } + } + /* * Called when a block fades. */ diff --git a/worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/util/Materials.java b/worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/util/Materials.java index 0a34174f..4eb7fd81 100644 --- a/worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/util/Materials.java +++ b/worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/util/Materials.java @@ -1148,6 +1148,20 @@ public static boolean isBed(Material material) { } } + /** + * Test whether the material is a crop. + * @param type the material + * @return true if the material is a crop + */ + public static boolean isCrop(Material type) { + return type == Material.WHEAT + || type == Material.CARROTS + || type == Material.POTATOES + || type == Material.BEETROOTS + || type == Material.MELON_STEM + || type == Material.PUMPKIN_STEM; + } + /** * Test whether the given material is affected by * {@link Flags#USE}.