From 770d8b0bef11575a3b2588424891708862ed6a0f Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 14 Feb 2021 12:52:51 -0800 Subject: [PATCH] Harvest listener --- .../flags/protection/HarvestListener.java | 26 +++++++++++++++++++ .../world/bentobox/bentobox/lists/Flags.java | 7 +++++ src/main/resources/locales/en-US.yml | 6 +++++ .../bentobox/managers/FlagsManagerTest.java | 2 +- 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 src/main/java/world/bentobox/bentobox/listeners/flags/protection/HarvestListener.java diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/protection/HarvestListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/protection/HarvestListener.java new file mode 100644 index 000000000..89ae6a5ae --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/protection/HarvestListener.java @@ -0,0 +1,26 @@ +package world.bentobox.bentobox.listeners.flags.protection; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.player.PlayerEggThrowEvent; +import org.bukkit.event.player.PlayerHarvestBlockEvent; + +import world.bentobox.bentobox.api.flags.FlagListener; +import world.bentobox.bentobox.lists.Flags; + +/** + * Handles harvesting + * @author tastybento + * @since 1.16.0 + */ +public class HarvestListener extends FlagListener { + + /** + * Handle visitor harvesting, e.g. honey + * @param e - event + */ + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + public boolean onHarvest(PlayerHarvestBlockEvent e) { + return checkIsland(e, e.getPlayer(), e.getHarvestedBlock().getLocation(), Flags.HARVEST); + } +} diff --git a/src/main/java/world/bentobox/bentobox/lists/Flags.java b/src/main/java/world/bentobox/bentobox/lists/Flags.java index 5b07a3bac..2edf42fb7 100644 --- a/src/main/java/world/bentobox/bentobox/lists/Flags.java +++ b/src/main/java/world/bentobox/bentobox/lists/Flags.java @@ -23,6 +23,7 @@ import world.bentobox.bentobox.listeners.flags.protection.ElytraListener; import world.bentobox.bentobox.listeners.flags.protection.EntityInteractListener; import world.bentobox.bentobox.listeners.flags.protection.ExperiencePickupListener; import world.bentobox.bentobox.listeners.flags.protection.FireListener; +import world.bentobox.bentobox.listeners.flags.protection.HarvestListener; import world.bentobox.bentobox.listeners.flags.protection.HurtingListener; import world.bentobox.bentobox.listeners.flags.protection.InventoryListener; import world.bentobox.bentobox.listeners.flags.protection.ItemDropPickUpListener; @@ -524,6 +525,12 @@ public final class Flags { */ public static final Flag PETS_STAY_AT_HOME = new Flag.Builder("PETS_STAY_AT_HOME", Material.TROPICAL_FISH).listener(new PetTeleportListener()).type(Type.WORLD_SETTING).defaultSetting(true).build(); + /** + * Protects against harvesting, e.g. honey + * @since 1.16.0 + * @see HarvestListener + */ + public static final Flag HARVEST = new Flag.Builder("HARVEST", Material.HONEY_BOTTLE).listener(new HarvestListener()).type(Type.PROTECTION).build(); /** * Provides a list of all the Flag instances contained in this class using reflection. * Deprecated Flags are ignored. diff --git a/src/main/resources/locales/en-US.yml b/src/main/resources/locales/en-US.yml index 4d4dd2a5c..c4fcb7d11 100644 --- a/src/main/resources/locales/en-US.yml +++ b/src/main/resources/locales/en-US.yml @@ -952,6 +952,12 @@ protection: &a outside protected &a island space name: "&e Limit mobs to island" + HARVEST: + description: |- + &a Toggle harvesting, e.g. + &a honey harvesting + name: "Harvesting" + hint: "Harvesting disabled" HURT_ANIMALS: description: "Toggle hurting" name: "Hurt animals" diff --git a/src/test/java/world/bentobox/bentobox/managers/FlagsManagerTest.java b/src/test/java/world/bentobox/bentobox/managers/FlagsManagerTest.java index 3008449ce..677202abd 100644 --- a/src/test/java/world/bentobox/bentobox/managers/FlagsManagerTest.java +++ b/src/test/java/world/bentobox/bentobox/managers/FlagsManagerTest.java @@ -46,7 +46,7 @@ public class FlagsManagerTest { /** * Update this value if the number of registered listeners changes */ - private static final int NUMBER_OF_LISTENERS = 48; + private static final int NUMBER_OF_LISTENERS = 50; @Mock private BentoBox plugin; @Mock