From f61b1090601e834fe197ee73786dd3a4fbfe000b Mon Sep 17 00:00:00 2001 From: Florian CUNY Date: Wed, 12 Dec 2018 20:45:55 +0100 Subject: [PATCH] Added EXPERIENCE_PICKUP flag #217 * Added EXPERIENCE_PICKUP flag (as a PROTECTION flag) * Created ExperiencePickupListener * Updated en-US locale accordingly --- .../flags/ExperiencePickupListener.java | 30 +++++++++++++++ .../world/bentobox/bentobox/lists/Flags.java | 38 ++----------------- src/main/resources/locales/en-US.yml | 4 ++ 3 files changed, 38 insertions(+), 34 deletions(-) create mode 100644 src/main/java/world/bentobox/bentobox/listeners/flags/ExperiencePickupListener.java diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/ExperiencePickupListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/ExperiencePickupListener.java new file mode 100644 index 000000000..73b2991b6 --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/ExperiencePickupListener.java @@ -0,0 +1,30 @@ +package world.bentobox.bentobox.listeners.flags; + +import org.bukkit.entity.ExperienceOrb; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.entity.EntityTargetLivingEntityEvent; +import world.bentobox.bentobox.api.flags.FlagListener; +import world.bentobox.bentobox.api.user.User; +import world.bentobox.bentobox.lists.Flags; + +/** + * Handles the {@link world.bentobox.bentobox.lists.Flags#EXPERIENCE_PICKUP} flag. + * + * @author Poslovitch + */ +public class ExperiencePickupListener extends FlagListener { + + @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) + public void onExperienceOrbTargetPlayer(EntityTargetLivingEntityEvent e) { + // Make sure the target is a Player and the entity is an experience orb + if (e.getTarget() instanceof Player && e.getEntity() instanceof ExperienceOrb) { + setUser(User.getInstance((Player) e.getTarget())); + if (!checkIsland(e, e.getEntity().getLocation(), Flags.EXPERIENCE_PICKUP)) { + // Cancelling the event won't be enough, we need to explicitly set the target to null + e.setTarget(null); + } + } + } +} diff --git a/src/main/java/world/bentobox/bentobox/lists/Flags.java b/src/main/java/world/bentobox/bentobox/lists/Flags.java index 60f645c9a..323150960 100644 --- a/src/main/java/world/bentobox/bentobox/lists/Flags.java +++ b/src/main/java/world/bentobox/bentobox/lists/Flags.java @@ -11,40 +11,7 @@ import world.bentobox.bentobox.api.flags.Flag.Type; import world.bentobox.bentobox.api.flags.FlagBuilder; import world.bentobox.bentobox.api.flags.clicklisteners.CycleClick; -import world.bentobox.bentobox.listeners.flags.BlockInteractionListener; -import world.bentobox.bentobox.listeners.flags.BreakBlocksListener; -import world.bentobox.bentobox.listeners.flags.BreedingListener; -import world.bentobox.bentobox.listeners.flags.BucketListener; -import world.bentobox.bentobox.listeners.flags.ChestDamageListener; -import world.bentobox.bentobox.listeners.flags.CleanSuperFlatListener; -import world.bentobox.bentobox.listeners.flags.CoarseDirtTillingListener; -import world.bentobox.bentobox.listeners.flags.CreeperListener; -import world.bentobox.bentobox.listeners.flags.EggListener; -import world.bentobox.bentobox.listeners.flags.EnderChestListener; -import world.bentobox.bentobox.listeners.flags.EndermanListener; -import world.bentobox.bentobox.listeners.flags.EnterExitListener; -import world.bentobox.bentobox.listeners.flags.EntityInteractListener; -import world.bentobox.bentobox.listeners.flags.FireListener; -import world.bentobox.bentobox.listeners.flags.GeoLimitMobsListener; -import world.bentobox.bentobox.listeners.flags.HurtingListener; -import world.bentobox.bentobox.listeners.flags.InventoryListener; -import world.bentobox.bentobox.listeners.flags.InvincibleVisitorsListener; -import world.bentobox.bentobox.listeners.flags.IslandRespawnListener; -import world.bentobox.bentobox.listeners.flags.ItemDropPickUpListener; -import world.bentobox.bentobox.listeners.flags.ItemFrameListener; -import world.bentobox.bentobox.listeners.flags.LeashListener; -import world.bentobox.bentobox.listeners.flags.LockAndBanListener; -import world.bentobox.bentobox.listeners.flags.MobSpawnListener; -import world.bentobox.bentobox.listeners.flags.OfflineRedstoneListener; -import world.bentobox.bentobox.listeners.flags.PVPListener; -import world.bentobox.bentobox.listeners.flags.PhysicalInteractionListener; -import world.bentobox.bentobox.listeners.flags.PistonPushListener; -import world.bentobox.bentobox.listeners.flags.PlaceBlocksListener; -import world.bentobox.bentobox.listeners.flags.PortalListener; -import world.bentobox.bentobox.listeners.flags.RemoveMobsListener; -import world.bentobox.bentobox.listeners.flags.ShearingListener; -import world.bentobox.bentobox.listeners.flags.TNTListener; -import world.bentobox.bentobox.listeners.flags.TeleportationListener; +import world.bentobox.bentobox.listeners.flags.*; import world.bentobox.bentobox.listeners.flags.clicklisteners.CommandRankClickListener; import world.bentobox.bentobox.listeners.flags.clicklisteners.GeoLimitClickListener; import world.bentobox.bentobox.managers.RanksManager; @@ -142,6 +109,9 @@ private Flags() {} public static final Flag ITEM_DROP = new FlagBuilder().id("ITEM_DROP").icon(Material.BEETROOT_SOUP).allowedByDefault(true).listener(new ItemDropPickUpListener()).build(); public static final Flag ITEM_PICKUP = new FlagBuilder().id("ITEM_PICKUP").icon(Material.BEETROOT_SEEDS).build(); + // Experience + public static final Flag EXPERIENCE_PICKUP = new FlagBuilder().id("EXPERIENCE_PICKUP").icon(Material.EXPERIENCE_BOTTLE).listener(new ExperiencePickupListener()).build(); + // TNT public static final Flag TNT = new FlagBuilder().id("TNT").icon(Material.TNT).listener(new TNTListener()).allowedByDefault(false).build(); diff --git a/src/main/resources/locales/en-US.yml b/src/main/resources/locales/en-US.yml index dbc9445ea..95ab19b3f 100644 --- a/src/main/resources/locales/en-US.yml +++ b/src/main/resources/locales/en-US.yml @@ -711,6 +711,10 @@ protection: description: "Toggle Frost Walker enchantment" name: "Frost Walker" hint: "Frost Walker cannot be used here" + EXPERIENCE_PICKUP: + name: "Experience pickup" + description: "Toggle experience orb pickup" + hint: "cannot pickup experience orb" locked: "&cThis island is locked!" protected: "&cIsland protected: [description]" spawn-protected: "&cSpawn protected: [description]"