diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/OfflineGrowthListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/OfflineGrowthListener.java new file mode 100644 index 000000000..5a86d5caf --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/OfflineGrowthListener.java @@ -0,0 +1,31 @@ +package world.bentobox.bentobox.listeners.flags.worldsettings; + +import org.bukkit.block.data.Ageable; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.block.BlockGrowEvent; +import world.bentobox.bentobox.api.flags.FlagListener; +import world.bentobox.bentobox.lists.Flags; + +/** + * Handles {@link Flags#OFFLINE_GROWTH} flag. + * @author Poslovitch + * @since 1.4.0 + */ +public class OfflineGrowthListener extends FlagListener { + + @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) + public void onCropGrow(BlockGrowEvent e) { + if (!getIWM().inWorld(e.getBlock().getWorld()) || Flags.OFFLINE_GROWTH.isSetForWorld(e.getBlock().getWorld())) { + // We do not want to run any check if this is not the right world or if it is allowed. + return; + } + + if (!(e.getBlock() instanceof Ageable)) { + // Do nothing if the block is not "ageable" (and therefore not a crop). + return; + } + + e.setCancelled(true); + } +} diff --git a/src/main/java/world/bentobox/bentobox/lists/Flags.java b/src/main/java/world/bentobox/bentobox/lists/Flags.java index 8885c1632..5bb75f8c3 100644 --- a/src/main/java/world/bentobox/bentobox/lists/Flags.java +++ b/src/main/java/world/bentobox/bentobox/lists/Flags.java @@ -49,6 +49,7 @@ import world.bentobox.bentobox.listeners.flags.worldsettings.ItemFrameListener; import world.bentobox.bentobox.listeners.flags.worldsettings.LiquidsFlowingOutListener; import world.bentobox.bentobox.listeners.flags.worldsettings.NaturalSpawningOutsideRangeListener; import world.bentobox.bentobox.listeners.flags.worldsettings.ObsidianScoopingListener; +import world.bentobox.bentobox.listeners.flags.worldsettings.OfflineGrowthListener; import world.bentobox.bentobox.listeners.flags.worldsettings.OfflineRedstoneListener; import world.bentobox.bentobox.listeners.flags.worldsettings.PistonPushListener; import world.bentobox.bentobox.listeners.flags.worldsettings.RemoveMobsListener; @@ -322,6 +323,14 @@ public final class Flags { public static final Flag OFFLINE_REDSTONE = new Flag.Builder("OFFLINE_REDSTONE", Material.COMPARATOR).type(Type.WORLD_SETTING) .listener(new OfflineRedstoneListener()).defaultSetting(true).build(); + /** + * If disabled, prevents crops/plants from growing on islands whose members are offline. + * @since 1.4.0 + * @see OfflineGrowthListener + */ + public static final Flag OFFLINE_GROWTH = new Flag.Builder("OFFLINE_GROWTH", Material.WHEAT_SEEDS).type(Type.WORLD_SETTING) + .listener(new OfflineGrowthListener()).defaultSetting(true).build(); + public static final Flag CLEAN_SUPER_FLAT = new Flag.Builder("CLEAN_SUPER_FLAT", Material.BEDROCK).type(Type.WORLD_SETTING) .listener(new CleanSuperFlatListener()).build();