From 645b4eb61088cca09a8bdff4372aed7dda14c27f Mon Sep 17 00:00:00 2001 From: tastybento Date: Mon, 5 Feb 2018 14:51:03 -0800 Subject: [PATCH] Implements flags as enum so flags are protected. --- .../us/tastybento/bskyblock/BSkyBlock.java | 2 - .../tastybento/bskyblock/api/flags/Flag.java | 8 +- .../bskyblock/api/flags/FlagBuilder.java | 6 +- .../bskyblock/database/objects/Island.java | 17 +- .../listeners/flags/AbstractFlagListener.java | 25 ++- .../flags/BlockInteractionListener.java | 3 +- .../listeners/flags/BreakBlocksListener.java | 2 +- .../listeners/flags/FireListener.java | 8 +- .../listeners/flags/HurtingListener.java | 3 +- .../listeners/flags/MobSpawnListener.java | 4 +- .../listeners/flags/PVPListener.java | 11 +- .../us/tastybento/bskyblock/lists/Flaggs.java | 51 ------ .../us/tastybento/bskyblock/lists/Flags.java | 171 +++++------------- .../bskyblock/managers/FlagsManager.java | 107 +++++------ src/test/java/bskyblock/TestBSkyBlock.java | 2 +- 15 files changed, 152 insertions(+), 268 deletions(-) delete mode 100644 src/main/java/us/tastybento/bskyblock/lists/Flaggs.java diff --git a/src/main/java/us/tastybento/bskyblock/BSkyBlock.java b/src/main/java/us/tastybento/bskyblock/BSkyBlock.java index a0f98fe13..1a9455396 100755 --- a/src/main/java/us/tastybento/bskyblock/BSkyBlock.java +++ b/src/main/java/us/tastybento/bskyblock/BSkyBlock.java @@ -11,7 +11,6 @@ import us.tastybento.bskyblock.database.managers.island.IslandsManager; import us.tastybento.bskyblock.generators.IslandWorld; import us.tastybento.bskyblock.listeners.JoinLeaveListener; import us.tastybento.bskyblock.listeners.PanelListenerManager; -import us.tastybento.bskyblock.lists.Flags; import us.tastybento.bskyblock.managers.AddonsManager; import us.tastybento.bskyblock.managers.CommandsManager; import us.tastybento.bskyblock.managers.FlagsManager; @@ -92,7 +91,6 @@ public class BSkyBlock extends JavaPlugin { // Load Flags flagsManager = new FlagsManager(plugin); - new Flags(plugin); // Load islands from database islandsManager.load(); diff --git a/src/main/java/us/tastybento/bskyblock/api/flags/Flag.java b/src/main/java/us/tastybento/bskyblock/api/flags/Flag.java index e134ff67f..b0e8feb59 100644 --- a/src/main/java/us/tastybento/bskyblock/api/flags/Flag.java +++ b/src/main/java/us/tastybento/bskyblock/api/flags/Flag.java @@ -6,16 +6,16 @@ import org.bukkit.event.Listener; import us.tastybento.bskyblock.BSkyBlock; import us.tastybento.bskyblock.api.panels.PanelItem; -import us.tastybento.bskyblock.lists.Flaggs; +import us.tastybento.bskyblock.lists.Flags; public class Flag implements Comparable { - private final Flaggs id; + private final Flags id; private final PanelItem icon; private final Listener listener; private boolean defaultSetting; - public Flag(BSkyBlock plugin, Flaggs id2, PanelItem icon, Listener listener, boolean defaultSetting) { + public Flag(BSkyBlock plugin, Flags id2, PanelItem icon, Listener listener, boolean defaultSetting) { this.id = id2; this.icon = icon; this.listener = listener; @@ -24,7 +24,7 @@ public class Flag implements Comparable { plugin.getFlagsManager().registerFlag(this); } - public Flaggs getID() { + public Flags getID() { return id; } diff --git a/src/main/java/us/tastybento/bskyblock/api/flags/FlagBuilder.java b/src/main/java/us/tastybento/bskyblock/api/flags/FlagBuilder.java index 60149203b..75a454e7d 100644 --- a/src/main/java/us/tastybento/bskyblock/api/flags/FlagBuilder.java +++ b/src/main/java/us/tastybento/bskyblock/api/flags/FlagBuilder.java @@ -7,16 +7,16 @@ import org.bukkit.inventory.ItemStack; import us.tastybento.bskyblock.BSkyBlock; import us.tastybento.bskyblock.api.panels.PanelItem; import us.tastybento.bskyblock.api.panels.builders.PanelItemBuilder; -import us.tastybento.bskyblock.lists.Flaggs; +import us.tastybento.bskyblock.lists.Flags; public class FlagBuilder { - private Flaggs id; + private Flags id; private PanelItem icon; private Listener listener; private boolean defaultSetting; - public FlagBuilder id(Flaggs flag) { + public FlagBuilder id(Flags flag) { this.id = flag; return this; } diff --git a/src/main/java/us/tastybento/bskyblock/database/objects/Island.java b/src/main/java/us/tastybento/bskyblock/database/objects/Island.java index b16961a81..d7cd16234 100755 --- a/src/main/java/us/tastybento/bskyblock/database/objects/Island.java +++ b/src/main/java/us/tastybento/bskyblock/database/objects/Island.java @@ -16,15 +16,14 @@ import org.bukkit.entity.Entity; import us.tastybento.bskyblock.BSkyBlock; import us.tastybento.bskyblock.api.commands.User; -import us.tastybento.bskyblock.api.configuration.ConfigEntry; import us.tastybento.bskyblock.api.events.IslandBaseEvent; import us.tastybento.bskyblock.api.events.island.IslandEvent; import us.tastybento.bskyblock.api.events.island.IslandEvent.IslandLockEvent; import us.tastybento.bskyblock.api.events.island.IslandEvent.IslandUnlockEvent; import us.tastybento.bskyblock.api.events.island.IslandEvent.Reason; -import us.tastybento.bskyblock.api.flags.Flag; import us.tastybento.bskyblock.database.objects.adapters.Adapter; import us.tastybento.bskyblock.database.objects.adapters.FlagSerializer; +import us.tastybento.bskyblock.lists.Flags; import us.tastybento.bskyblock.managers.RanksManager; import us.tastybento.bskyblock.util.Util; @@ -83,7 +82,7 @@ public class Island implements DataObject { //// Protection flags //// @Adapter(FlagSerializer.class) - private HashMap flags = new HashMap<>(); + private HashMap flags = new HashMap<>(); private int levelHandicap; private Location spawnPoint; @@ -158,7 +157,7 @@ public class Island implements DataObject { * @param flag * @return flag rank. Players must have at least this rank to bypass this flag */ - public int getFlagReq(Flag flag){ + public int getFlagReq(Flags flag){ if(flags.containsKey(flag)) { return flags.get(flag); } else { @@ -170,7 +169,7 @@ public class Island implements DataObject { /** * @return the flags */ - public HashMap getFlags() { + public HashMap getFlags() { return flags; } @@ -418,7 +417,7 @@ public class Island implements DataObject { * @param flag * @return true if allowed, false if not */ - public boolean isAllowed(Flag flag) { + public boolean isAllowed(Flags flag) { return this.getFlagReq(flag) >= 0 ? true : false; } @@ -428,7 +427,7 @@ public class Island implements DataObject { * @param flag - flag * @return true if allowed, false if not */ - public boolean isAllowed(User user, Flag flag) { + public boolean isAllowed(User user, Flags flag) { //Bukkit.getLogger().info("DEBUG: " + flag.getID() + " user score = " + getRank(user) + " flag req = "+ this.getFlagReq(flag)); return (this.getRank(user) >= this.getFlagReq(flag)) ? true : false; } @@ -507,14 +506,14 @@ public class Island implements DataObject { * @param flag * @param value - rank value. If the flag applies to the island, a positive number = true, negative = false */ - public void setFlag(Flag flag, int value){ + public void setFlag(Flags flag, int value){ flags.put(flag, value); } /** * @param flags the flags to set */ - public void setFlags(HashMap flags) { + public void setFlags(HashMap flags) { this.flags = flags; } diff --git a/src/main/java/us/tastybento/bskyblock/listeners/flags/AbstractFlagListener.java b/src/main/java/us/tastybento/bskyblock/listeners/flags/AbstractFlagListener.java index 7e133fb9d..eeaa00224 100644 --- a/src/main/java/us/tastybento/bskyblock/listeners/flags/AbstractFlagListener.java +++ b/src/main/java/us/tastybento/bskyblock/listeners/flags/AbstractFlagListener.java @@ -18,6 +18,7 @@ import us.tastybento.bskyblock.BSkyBlock; import us.tastybento.bskyblock.api.commands.User; import us.tastybento.bskyblock.api.flags.Flag; import us.tastybento.bskyblock.database.objects.Island; +import us.tastybento.bskyblock.lists.Flags; /** * Abstract class for flag listeners. Provides common code. @@ -122,23 +123,23 @@ public abstract class AbstractFlagListener implements Listener { * Generic place blocks checker * @param e * @param loc - * @param flag + * @param redstone * @return true if the check is okay, false if it was disallowed */ - public boolean checkIsland(Event e, Location loc, Flag flag) { - return checkIsland(e, loc, flag, false); + public boolean checkIsland(Event e, Location loc, Flags redstone) { + return checkIsland(e, loc, redstone, false); } /** - * Generic place blocks checker + * Check if flag is allowed * @param e * @param loc * @param silent - if true, no attempt is made to tell the user * @return true if the check is okay, false if it was disallowed */ - public boolean checkIsland(Event e, Location loc, Flag flag, boolean silent) { + public boolean checkIsland(Event e, Location loc, Flags flag, boolean silent) { // If the user is not set already, try to get it from the event if (user == null) { @@ -169,7 +170,7 @@ public abstract class AbstractFlagListener implements Listener { } // The player is in the world, but not on an island, so general world settings apply - if (!flag.isAllowed()) { + if (!isAllowed(flag)) { noGo(e, silent); user = null; return false; @@ -179,4 +180,16 @@ public abstract class AbstractFlagListener implements Listener { } } + /** + * Get the flag for this ID + * @param flag + * @return Flag denoted by the id + */ + protected Flag id(Flags flag) { + return plugin.getFlagsManager().getFlagByID(flag); + } + + protected boolean isAllowed(Flags flag) { + return plugin.getFlagsManager().isAllowed(flag); + } } diff --git a/src/main/java/us/tastybento/bskyblock/listeners/flags/BlockInteractionListener.java b/src/main/java/us/tastybento/bskyblock/listeners/flags/BlockInteractionListener.java index c43563b07..f4bd2fe5f 100644 --- a/src/main/java/us/tastybento/bskyblock/listeners/flags/BlockInteractionListener.java +++ b/src/main/java/us/tastybento/bskyblock/listeners/flags/BlockInteractionListener.java @@ -9,7 +9,6 @@ import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerInteractEvent; import us.tastybento.bskyblock.BSkyBlock; -import us.tastybento.bskyblock.lists.Flaggs; import us.tastybento.bskyblock.lists.Flags; /** @@ -33,7 +32,7 @@ public class BlockInteractionListener extends AbstractFlagListener { } switch (e.getClickedBlock().getType()) { case ANVIL: - checkIsland(e, e.getClickedBlock().getLocation(), plugin.getFlagsManager().getFlagByID(Flaggs.ANVIL)); + checkIsland(e, e.getClickedBlock().getLocation(), Flags.ANVIL); break; case BEACON: checkIsland(e, e.getClickedBlock().getLocation(), Flags.BEACON); diff --git a/src/main/java/us/tastybento/bskyblock/listeners/flags/BreakBlocksListener.java b/src/main/java/us/tastybento/bskyblock/listeners/flags/BreakBlocksListener.java index 53a92a78c..451fb3d54 100644 --- a/src/main/java/us/tastybento/bskyblock/listeners/flags/BreakBlocksListener.java +++ b/src/main/java/us/tastybento/bskyblock/listeners/flags/BreakBlocksListener.java @@ -107,7 +107,7 @@ public class BreakBlocksListener extends AbstractFlagListener { }); // The player is in the world, but not on an island, so general world settings apply - if (!Flags.BREAK_BLOCKS.isAllowed()) { + if (!isAllowed(Flags.BREAK_BLOCKS)) { e.setCancelled(true); user.sendMessage("protection.protected"); } diff --git a/src/main/java/us/tastybento/bskyblock/listeners/flags/FireListener.java b/src/main/java/us/tastybento/bskyblock/listeners/flags/FireListener.java index f04cb7071..153dd220c 100644 --- a/src/main/java/us/tastybento/bskyblock/listeners/flags/FireListener.java +++ b/src/main/java/us/tastybento/bskyblock/listeners/flags/FireListener.java @@ -50,7 +50,7 @@ public class FireListener extends AbstractFlagListener { if (!x.isAllowed(Flags.FIRE_SPREAD)) e.setCancelled(true); }); // If not on an island, check the default setting - if (!island.isPresent() && !Flags.FIRE_SPREAD.isAllowed()) e.setCancelled(true); + if (!island.isPresent() && !isAllowed(Flags.FIRE_SPREAD)) e.setCancelled(true); } /** @@ -69,7 +69,7 @@ public class FireListener extends AbstractFlagListener { if (!x.isAllowed(Flags.FIRE_SPREAD)) e.setCancelled(true); }); // If not on an island, check the default setting - if (!island.isPresent() && !Flags.FIRE_SPREAD.isAllowed()) e.setCancelled(true); + if (!island.isPresent() && !isAllowed(Flags.FIRE_SPREAD)) e.setCancelled(true); } } @@ -92,7 +92,7 @@ public class FireListener extends AbstractFlagListener { if (!x.isAllowed(Flags.FIRE)) e.setCancelled(true); }); // If not on an island, check the default setting - if (!island.isPresent() && !Flags.FIRE.isAllowed()) e.setCancelled(true); + if (!island.isPresent() && !isAllowed(Flags.FIRE)) e.setCancelled(true); } @@ -145,7 +145,7 @@ public class FireListener extends AbstractFlagListener { if (!x.isAllowed(Flags.FIRE)) e.setCancelled(true); }); // If not on an island, check the default setting - if (!island.isPresent() && !Flags.FIRE.isAllowed()) e.setCancelled(true); + if (!island.isPresent() && !isAllowed(Flags.FIRE)) e.setCancelled(true); // If either of these canceled the event, return if (e.isCancelled()) return; diff --git a/src/main/java/us/tastybento/bskyblock/listeners/flags/HurtingListener.java b/src/main/java/us/tastybento/bskyblock/listeners/flags/HurtingListener.java index 3ed085f6c..84490f431 100644 --- a/src/main/java/us/tastybento/bskyblock/listeners/flags/HurtingListener.java +++ b/src/main/java/us/tastybento/bskyblock/listeners/flags/HurtingListener.java @@ -32,7 +32,6 @@ import org.bukkit.potion.PotionEffect; import us.tastybento.bskyblock.BSkyBlock; import us.tastybento.bskyblock.api.commands.User; -import us.tastybento.bskyblock.api.flags.Flag; import us.tastybento.bskyblock.lists.Flags; /** @@ -71,7 +70,7 @@ public class HurtingListener extends AbstractFlagListener { * @param damager * @param flag */ - private void respond(Event event, Entity damager, Flag flag) { + private void respond(Event event, Entity damager, Flags flag) { // Get the attacker if (damager instanceof Player) { setUser(User.getInstance(damager)).checkIsland(event, damager.getLocation(), flag); diff --git a/src/main/java/us/tastybento/bskyblock/listeners/flags/MobSpawnListener.java b/src/main/java/us/tastybento/bskyblock/listeners/flags/MobSpawnListener.java index 7c3a7a4d7..613fbb242 100644 --- a/src/main/java/us/tastybento/bskyblock/listeners/flags/MobSpawnListener.java +++ b/src/main/java/us/tastybento/bskyblock/listeners/flags/MobSpawnListener.java @@ -65,13 +65,13 @@ public class MobSpawnListener extends AbstractFlagListener { } else { // Outside of the island if (e.getEntity() instanceof Monster || e.getEntity() instanceof Slime) { - if (!Flags.MOB_SPAWN.isAllowed()) { + if (!isAllowed(Flags.MOB_SPAWN)) { // Mobs not allowed to spawn e.setCancelled(true); return; } } else if (e.getEntity() instanceof Animals) { - if (!Flags.MONSTER_SPAWN.isAllowed()) { + if (!isAllowed(Flags.MONSTER_SPAWN)) { // Mobs not allowed to spawn e.setCancelled(true); return; diff --git a/src/main/java/us/tastybento/bskyblock/listeners/flags/PVPListener.java b/src/main/java/us/tastybento/bskyblock/listeners/flags/PVPListener.java index 2ebb8951a..4cdb49f79 100644 --- a/src/main/java/us/tastybento/bskyblock/listeners/flags/PVPListener.java +++ b/src/main/java/us/tastybento/bskyblock/listeners/flags/PVPListener.java @@ -22,7 +22,6 @@ import org.bukkit.potion.PotionEffect; import us.tastybento.bskyblock.BSkyBlock; import us.tastybento.bskyblock.api.commands.User; -import us.tastybento.bskyblock.api.flags.Flag; import us.tastybento.bskyblock.lists.Flags; /** @@ -49,14 +48,14 @@ public class PVPListener extends AbstractFlagListener { @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) public void onEntityDamage(final EntityDamageByEntityEvent e) { if (e.getEntity() instanceof Player) { - Flag flag = Flags.PVP_OVERWORLD; + Flags flag = Flags.PVP_OVERWORLD; if (e.getEntity().getWorld().equals(plugin.getIslandWorldManager().getNetherWorld())) flag = Flags.PVP_NETHER; else if (e.getEntity().getWorld().equals(plugin.getIslandWorldManager().getEndWorld())) flag = Flags.PVP_END; respond(e, e.getDamager(), flag); } } - private void respond(Event event, Entity damager, Flag flag) { + private void respond(Event event, Entity damager, Flags flag) { // Get the attacker if (damager instanceof Player) { setUser(User.getInstance(damager)).checkIsland(event, damager.getLocation(), flag); @@ -77,7 +76,7 @@ public class PVPListener extends AbstractFlagListener { @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) public void onFishing(PlayerFishEvent e) { if (e.getCaught() != null && e.getCaught() instanceof Player) { - Flag flag = Flags.PVP_OVERWORLD; + Flags flag = Flags.PVP_OVERWORLD; if (e.getCaught().getWorld().equals(plugin.getIslandWorldManager().getNetherWorld())) flag = Flags.PVP_NETHER; else if (e.getCaught().getWorld().equals(plugin.getIslandWorldManager().getEndWorld())) flag = Flags.PVP_END; if (checkIsland(e, e.getCaught().getLocation(), flag)) { @@ -94,7 +93,7 @@ public class PVPListener extends AbstractFlagListener { @EventHandler(priority = EventPriority.LOW, ignoreCancelled=true) public void onSplashPotionSplash(final PotionSplashEvent e) { // Deduce the world - Flag flag = Flags.PVP_OVERWORLD; + Flags flag = Flags.PVP_OVERWORLD; if (e.getPotion().getWorld().equals(plugin.getIslandWorldManager().getNetherWorld())) flag = Flags.PVP_NETHER; else if (e.getPotion().getWorld().equals(plugin.getIslandWorldManager().getEndWorld())) flag = Flags.PVP_END; @@ -142,7 +141,7 @@ public class PVPListener extends AbstractFlagListener { if (e.getCause().equals(DamageCause.ENTITY_ATTACK) && thrownPotions.containsKey(e.getDamager().getEntityId())) { // Deduce the world - Flag flag = Flags.PVP_OVERWORLD; + Flags flag = Flags.PVP_OVERWORLD; if (e.getEntity().getWorld().equals(plugin.getIslandWorldManager().getNetherWorld())) flag = Flags.PVP_NETHER; else if (e.getEntity().getWorld().equals(plugin.getIslandWorldManager().getEndWorld())) flag = Flags.PVP_END; diff --git a/src/main/java/us/tastybento/bskyblock/lists/Flaggs.java b/src/main/java/us/tastybento/bskyblock/lists/Flaggs.java deleted file mode 100644 index d63cd64b4..000000000 --- a/src/main/java/us/tastybento/bskyblock/lists/Flaggs.java +++ /dev/null @@ -1,51 +0,0 @@ -package us.tastybento.bskyblock.lists; - - -public enum Flaggs { - ANVIL, - ARMOR_STAND, - BEACON, - BED, - BREAK_BLOCKS, - BREEDING, - BREWING, - BUCKET, - CHEST, - CHORUS_FRUIT, - COLLECT_LAVA, - COLLECT_WATER, - CRAFTING, - CROP_TRAMPLE, - DOOR, - EGGS, - ENCHANTING, - ENDER_PEARL, - ENTER_EXIT_MESSAGES, - FIRE, - FIRE_EXTINGUISH, - FIRE_SPREAD, - FURNACE, - GATE, - HURT_MOBS, - HURT_MONSTERS, - ITEM_DROP, - ITEM_PICKUP, - LEASH, - LEVER_BUTTON, - MILKING, - MOB_SPAWN, - MONSTER_SPAWN, - MOUNT_INVENTORY, - MUSIC, - PLACE_BLOCKS, - PORTAL, - PRESSURE_PLATE, - PVP_END, - PVP_NETHER, - PVP_OVERWORLD, - REDSTONE, - RIDING, - SHEARING, - SPAWN_EGGS, - TRADING -} diff --git a/src/main/java/us/tastybento/bskyblock/lists/Flags.java b/src/main/java/us/tastybento/bskyblock/lists/Flags.java index 4083a2487..e8e7905a5 100644 --- a/src/main/java/us/tastybento/bskyblock/lists/Flags.java +++ b/src/main/java/us/tastybento/bskyblock/lists/Flags.java @@ -1,128 +1,51 @@ package us.tastybento.bskyblock.lists; -import org.bukkit.Material; -import us.tastybento.bskyblock.BSkyBlock; -import us.tastybento.bskyblock.api.flags.FlagBuilder; -import us.tastybento.bskyblock.listeners.flags.BlockInteractionListener; -import us.tastybento.bskyblock.listeners.flags.BreakBlocksListener; -import us.tastybento.bskyblock.listeners.flags.BreedingListener; -import us.tastybento.bskyblock.listeners.flags.BucketListener; -import us.tastybento.bskyblock.listeners.flags.EggListener; -import us.tastybento.bskyblock.listeners.flags.EntityInteractListener; -import us.tastybento.bskyblock.listeners.flags.FireListener; -import us.tastybento.bskyblock.listeners.flags.HurtingListener; -import us.tastybento.bskyblock.listeners.flags.InventoryListener; -import us.tastybento.bskyblock.listeners.flags.ItemDropPickUpListener; -import us.tastybento.bskyblock.listeners.flags.LeashListener; -import us.tastybento.bskyblock.listeners.flags.PVPListener; -import us.tastybento.bskyblock.listeners.flags.PhysicalInteractionListener; -import us.tastybento.bskyblock.listeners.flags.PlaceBlocksListener; -import us.tastybento.bskyblock.listeners.flags.PortalListener; -import us.tastybento.bskyblock.listeners.flags.ShearingListener; -import us.tastybento.bskyblock.listeners.flags.TeleportationListener; - -/** - * Protection flags - * @author tastybento - * - */ - -public class Flags { - - private BSkyBlock p; - - public Flags(BSkyBlock plugin) { - p = plugin; - - // Break and place blocks - new FlagBuilder().id(Flaggs.BREAK_BLOCKS).icon(Material.STONE).listener(new BreakBlocksListener(p)).build(p); - new FlagBuilder().id(Flaggs.PLACE_BLOCKS).icon(Material.DIRT).listener(new PlaceBlocksListener(p)).build(p); - - // Block interactions - all use BlockInteractionListener() - new FlagBuilder().id(Flaggs.ANVIL).icon(Material.ANVIL).listener(new BlockInteractionListener(p)).build(p); - new FlagBuilder().id(Flaggs.BEACON).icon(Material.BEACON).build(p); - new FlagBuilder().id(Flaggs.BED).icon(Material.BED).build(p); - new FlagBuilder().id(Flaggs.BREWING).icon(Material.BREWING_STAND_ITEM).build(p); - new FlagBuilder().id(Flaggs.CHEST).icon(Material.CHEST).build(p); - new FlagBuilder().id(Flaggs.DOOR).allowedByDefault(true).icon(Material.WOODEN_DOOR).build(p); - new FlagBuilder().id(Flaggs.CRAFTING).allowedByDefault(true).icon(Material.WORKBENCH).build(p); - new FlagBuilder().id(Flaggs.ENCHANTING).allowedByDefault(true).icon(Material.ENCHANTMENT_TABLE).build(p); - new FlagBuilder().id(Flaggs.FURNACE).icon(Material.FURNACE).build(p); - new FlagBuilder().id(Flaggs.GATE).allowedByDefault(true).icon(Material.FENCE_GATE).build(p); - new FlagBuilder().id(Flaggs.MUSIC).icon(Material.JUKEBOX).build(p); - new FlagBuilder().id(Flaggs.LEVER_BUTTON).icon(Material.LEVER).build(p); - new FlagBuilder().id(Flaggs.REDSTONE).icon(Material.REDSTONE).build(p); - new FlagBuilder().id(Flaggs.SPAWN_EGGS).icon(Material.MONSTER_EGG).build(p); - - // Entity interactions - new FlagBuilder().id(Flaggs.ARMOR_STAND).icon(Material.ARMOR_STAND).listener(new EntityInteractListener(p)).build(p); - new FlagBuilder().id(Flaggs.RIDING).icon(Material.GOLD_BARDING).build(p); - new FlagBuilder().id(Flaggs.TRADING).allowedByDefault(true).icon(Material.EMERALD).build(p); - - // Breeding - new FlagBuilder().id(Flaggs.BREEDING).icon(Material.CARROT).listener(new BreedingListener(p)).build(p); - - // Buckets. All bucket use is covered by one listener - new FlagBuilder().id(Flaggs.BUCKET).icon(Material.BUCKET).listener(new BucketListener(p)).build(p); - new FlagBuilder().id(Flaggs.COLLECT_LAVA).icon(Material.LAVA_BUCKET).build(p); - new FlagBuilder().id(Flaggs.COLLECT_WATER).icon(Material.WATER_BUCKET).build(p); - new FlagBuilder().id(Flaggs.MILKING).icon(Material.MILK_BUCKET).build(p); - - // Chorus Fruit and Enderpearls - new FlagBuilder().id(Flaggs.CHORUS_FRUIT).icon(Material.CHORUS_FRUIT).listener(new TeleportationListener(p)).build(p); - new FlagBuilder().id(Flaggs.ENDER_PEARL).icon(Material.ENDER_PEARL).build(p); - - // Physical interactions - new FlagBuilder().id(Flaggs.CROP_TRAMPLE).icon(Material.WHEAT).listener(new PhysicalInteractionListener(p)).build(p); - new FlagBuilder().id(Flaggs.PRESSURE_PLATE).icon(Material.GOLD_PLATE).build(p); - - // Egg throwing - new FlagBuilder().id(Flaggs.EGGS).icon(Material.EGG).listener(new EggListener(p)).build(p); - - /* - * Fire - * I'll take you to burn. - * Fire - * I'll take you to learn. - * You gonna burn, burn, burn - * Fire - * I'll take you to burn - * - The Crazy World of Arthur Brown - */ - new FlagBuilder().id(Flaggs.FIRE).icon(Material.FLINT_AND_STEEL).listener(new FireListener(p)).build(p); - new FlagBuilder().id(Flaggs.FIRE_EXTINGUISH).icon(Material.POTION).build(p); - new FlagBuilder().id(Flaggs.FIRE_SPREAD).icon(Material.FIREWORK_CHARGE).build(p); - - // Inventories - new FlagBuilder().id(Flaggs.MOUNT_INVENTORY).icon(Material.IRON_BARDING).listener(new InventoryListener(p)).build(p); - - // Hurting things - new FlagBuilder().id(Flaggs.HURT_MOBS).icon(Material.STONE_SWORD).listener(new HurtingListener(p)).build(p); - new FlagBuilder().id(Flaggs.HURT_MONSTERS).icon(Material.WOOD_SWORD).build(p); - - // Leashes - new FlagBuilder().id(Flaggs.LEASH).icon(Material.LEASH).listener(new LeashListener(p)).build(p); - - // Portal use protection - new FlagBuilder().id(Flaggs.PORTAL).icon(Material.OBSIDIAN).listener(new PortalListener(p)).build(p); - - // Shearing - new FlagBuilder().id(Flaggs.SHEARING).icon(Material.SHEARS).listener(new ShearingListener(p)).build(p); - - // Item pickup or drop - new FlagBuilder().id(Flaggs.ITEM_DROP).icon(Material.DIRT).allowedByDefault(true).listener(new ItemDropPickUpListener(p)).build(p); - new FlagBuilder().id(Flaggs.ITEM_PICKUP).icon(Material.DIRT).build(p); - - /* - * Non-protection flags - */ - // PVP - new FlagBuilder().id(Flaggs.PVP_OVERWORLD).icon(Material.ARROW).listener(new PVPListener(p)).build(p); - new FlagBuilder().id(Flaggs.PVP_NETHER).icon(Material.IRON_AXE).build(p); - new FlagBuilder().id(Flaggs.PVP_END).icon(Material.END_CRYSTAL).build(p); - new FlagBuilder().id(Flaggs.ENTER_EXIT_MESSAGES).icon(Material.DIRT).allowedByDefault(true).build(p); - new FlagBuilder().id(Flaggs.MOB_SPAWN).icon(Material.APPLE).allowedByDefault(true).build(p); - new FlagBuilder().id(Flaggs.MONSTER_SPAWN).icon(Material.MOB_SPAWNER).allowedByDefault(true).build(p); - } +public enum Flags { + ANVIL, + ARMOR_STAND, + BEACON, + BED, + BREAK_BLOCKS, + BREEDING, + BREWING, + BUCKET, + CHEST, + CHORUS_FRUIT, + COLLECT_LAVA, + COLLECT_WATER, + CRAFTING, + CROP_TRAMPLE, + DOOR, + EGGS, + ENCHANTING, + ENDER_PEARL, + ENTER_EXIT_MESSAGES, + FIRE, + FIRE_EXTINGUISH, + FIRE_SPREAD, + FURNACE, + GATE, + HURT_MOBS, + HURT_MONSTERS, + ITEM_DROP, + ITEM_PICKUP, + LEASH, + LEVER_BUTTON, + MILKING, + MOB_SPAWN, + MONSTER_SPAWN, + MOUNT_INVENTORY, + MUSIC, + PLACE_BLOCKS, + PORTAL, + PRESSURE_PLATE, + PVP_END, + PVP_NETHER, + PVP_OVERWORLD, + REDSTONE, + RIDING, + SHEARING, + SPAWN_EGGS, + TRADING } diff --git a/src/main/java/us/tastybento/bskyblock/managers/FlagsManager.java b/src/main/java/us/tastybento/bskyblock/managers/FlagsManager.java index 4b8620793..f7a414209 100644 --- a/src/main/java/us/tastybento/bskyblock/managers/FlagsManager.java +++ b/src/main/java/us/tastybento/bskyblock/managers/FlagsManager.java @@ -25,7 +25,7 @@ import us.tastybento.bskyblock.listeners.flags.PlaceBlocksListener; import us.tastybento.bskyblock.listeners.flags.PortalListener; import us.tastybento.bskyblock.listeners.flags.ShearingListener; import us.tastybento.bskyblock.listeners.flags.TeleportationListener; -import us.tastybento.bskyblock.lists.Flaggs; +import us.tastybento.bskyblock.lists.Flags; public class FlagsManager { @@ -38,7 +38,7 @@ public class FlagsManager { registerFlags(); } - private HashMap flags = new HashMap<>(); + private HashMap flags = new HashMap<>(); public void registerFlag(Flag flag) { //TODO all the security checks @@ -48,11 +48,11 @@ public class FlagsManager { flag.getListener().ifPresent(l -> p.getServer().getPluginManager().registerEvents(l, p)); } - public HashMap getFlags() { + public HashMap getFlags() { return flags; } - public Flag getFlagByID(Flaggs id) { + public Flag getFlagByID(Flags id) { return flags.get(id); } @@ -66,49 +66,49 @@ public class FlagsManager { private void registerFlags() { // Break and place blocks - new FlagBuilder().id(Flaggs.BREAK_BLOCKS).icon(Material.STONE).listener(new BreakBlocksListener(p)).build(p); - new FlagBuilder().id(Flaggs.PLACE_BLOCKS).icon(Material.DIRT).listener(new PlaceBlocksListener(p)).build(p); + new FlagBuilder().id(Flags.BREAK_BLOCKS).icon(Material.STONE).listener(new BreakBlocksListener(p)).build(p); + new FlagBuilder().id(Flags.PLACE_BLOCKS).icon(Material.DIRT).listener(new PlaceBlocksListener(p)).build(p); // Block interactions - all use BlockInteractionListener() - new FlagBuilder().id(Flaggs.ANVIL).icon(Material.ANVIL).listener(new BlockInteractionListener(p)).build(p); - new FlagBuilder().id(Flaggs.BEACON).icon(Material.BEACON).build(p); - new FlagBuilder().id(Flaggs.BED).icon(Material.BED).build(p); - new FlagBuilder().id(Flaggs.BREWING).icon(Material.BREWING_STAND_ITEM).build(p); - new FlagBuilder().id(Flaggs.CHEST).icon(Material.CHEST).build(p); - new FlagBuilder().id(Flaggs.DOOR).allowedByDefault(true).icon(Material.WOODEN_DOOR).build(p); - new FlagBuilder().id(Flaggs.CRAFTING).allowedByDefault(true).icon(Material.WORKBENCH).build(p); - new FlagBuilder().id(Flaggs.ENCHANTING).allowedByDefault(true).icon(Material.ENCHANTMENT_TABLE).build(p); - new FlagBuilder().id(Flaggs.FURNACE).icon(Material.FURNACE).build(p); - new FlagBuilder().id(Flaggs.GATE).allowedByDefault(true).icon(Material.FENCE_GATE).build(p); - new FlagBuilder().id(Flaggs.MUSIC).icon(Material.JUKEBOX).build(p); - new FlagBuilder().id(Flaggs.LEVER_BUTTON).icon(Material.LEVER).build(p); - new FlagBuilder().id(Flaggs.REDSTONE).icon(Material.REDSTONE).build(p); - new FlagBuilder().id(Flaggs.SPAWN_EGGS).icon(Material.MONSTER_EGG).build(p); + new FlagBuilder().id(Flags.ANVIL).icon(Material.ANVIL).listener(new BlockInteractionListener(p)).build(p); + new FlagBuilder().id(Flags.BEACON).icon(Material.BEACON).build(p); + new FlagBuilder().id(Flags.BED).icon(Material.BED).build(p); + new FlagBuilder().id(Flags.BREWING).icon(Material.BREWING_STAND_ITEM).build(p); + new FlagBuilder().id(Flags.CHEST).icon(Material.CHEST).build(p); + new FlagBuilder().id(Flags.DOOR).allowedByDefault(true).icon(Material.WOODEN_DOOR).build(p); + new FlagBuilder().id(Flags.CRAFTING).allowedByDefault(true).icon(Material.WORKBENCH).build(p); + new FlagBuilder().id(Flags.ENCHANTING).allowedByDefault(true).icon(Material.ENCHANTMENT_TABLE).build(p); + new FlagBuilder().id(Flags.FURNACE).icon(Material.FURNACE).build(p); + new FlagBuilder().id(Flags.GATE).allowedByDefault(true).icon(Material.FENCE_GATE).build(p); + new FlagBuilder().id(Flags.MUSIC).icon(Material.JUKEBOX).build(p); + new FlagBuilder().id(Flags.LEVER_BUTTON).icon(Material.LEVER).build(p); + new FlagBuilder().id(Flags.REDSTONE).icon(Material.REDSTONE).build(p); + new FlagBuilder().id(Flags.SPAWN_EGGS).icon(Material.MONSTER_EGG).build(p); // Entity interactions - new FlagBuilder().id(Flaggs.ARMOR_STAND).icon(Material.ARMOR_STAND).listener(new EntityInteractListener(p)).build(p); - new FlagBuilder().id(Flaggs.RIDING).icon(Material.GOLD_BARDING).build(p); - new FlagBuilder().id(Flaggs.TRADING).allowedByDefault(true).icon(Material.EMERALD).build(p); + new FlagBuilder().id(Flags.ARMOR_STAND).icon(Material.ARMOR_STAND).listener(new EntityInteractListener(p)).build(p); + new FlagBuilder().id(Flags.RIDING).icon(Material.GOLD_BARDING).build(p); + new FlagBuilder().id(Flags.TRADING).allowedByDefault(true).icon(Material.EMERALD).build(p); // Breeding - new FlagBuilder().id(Flaggs.BREEDING).icon(Material.CARROT).listener(new BreedingListener(p)).build(p); + new FlagBuilder().id(Flags.BREEDING).icon(Material.CARROT).listener(new BreedingListener(p)).build(p); // Buckets. All bucket use is covered by one listener - new FlagBuilder().id(Flaggs.BUCKET).icon(Material.BUCKET).listener(new BucketListener(p)).build(p); - new FlagBuilder().id(Flaggs.COLLECT_LAVA).icon(Material.LAVA_BUCKET).build(p); - new FlagBuilder().id(Flaggs.COLLECT_WATER).icon(Material.WATER_BUCKET).build(p); - new FlagBuilder().id(Flaggs.MILKING).icon(Material.MILK_BUCKET).build(p); + new FlagBuilder().id(Flags.BUCKET).icon(Material.BUCKET).listener(new BucketListener(p)).build(p); + new FlagBuilder().id(Flags.COLLECT_LAVA).icon(Material.LAVA_BUCKET).build(p); + new FlagBuilder().id(Flags.COLLECT_WATER).icon(Material.WATER_BUCKET).build(p); + new FlagBuilder().id(Flags.MILKING).icon(Material.MILK_BUCKET).build(p); // Chorus Fruit and Enderpearls - new FlagBuilder().id(Flaggs.CHORUS_FRUIT).icon(Material.CHORUS_FRUIT).listener(new TeleportationListener(p)).build(p); - new FlagBuilder().id(Flaggs.ENDER_PEARL).icon(Material.ENDER_PEARL).build(p); + new FlagBuilder().id(Flags.CHORUS_FRUIT).icon(Material.CHORUS_FRUIT).listener(new TeleportationListener(p)).build(p); + new FlagBuilder().id(Flags.ENDER_PEARL).icon(Material.ENDER_PEARL).build(p); // Physical interactions - new FlagBuilder().id(Flaggs.CROP_TRAMPLE).icon(Material.WHEAT).listener(new PhysicalInteractionListener(p)).build(p); - new FlagBuilder().id(Flaggs.PRESSURE_PLATE).icon(Material.GOLD_PLATE).build(p); + new FlagBuilder().id(Flags.CROP_TRAMPLE).icon(Material.WHEAT).listener(new PhysicalInteractionListener(p)).build(p); + new FlagBuilder().id(Flags.PRESSURE_PLATE).icon(Material.GOLD_PLATE).build(p); // Egg throwing - new FlagBuilder().id(Flaggs.EGGS).icon(Material.EGG).listener(new EggListener(p)).build(p); + new FlagBuilder().id(Flags.EGGS).icon(Material.EGG).listener(new EggListener(p)).build(p); /* * Fire @@ -120,40 +120,40 @@ public class FlagsManager { * I'll take you to burn * - The Crazy World of Arthur Brown */ - new FlagBuilder().id(Flaggs.FIRE).icon(Material.FLINT_AND_STEEL).listener(new FireListener(p)).build(p); - new FlagBuilder().id(Flaggs.FIRE_EXTINGUISH).icon(Material.POTION).build(p); - new FlagBuilder().id(Flaggs.FIRE_SPREAD).icon(Material.FIREWORK_CHARGE).build(p); + new FlagBuilder().id(Flags.FIRE).icon(Material.FLINT_AND_STEEL).listener(new FireListener(p)).build(p); + new FlagBuilder().id(Flags.FIRE_EXTINGUISH).icon(Material.POTION).build(p); + new FlagBuilder().id(Flags.FIRE_SPREAD).icon(Material.FIREWORK_CHARGE).build(p); // Inventories - new FlagBuilder().id(Flaggs.MOUNT_INVENTORY).icon(Material.IRON_BARDING).listener(new InventoryListener(p)).build(p); + new FlagBuilder().id(Flags.MOUNT_INVENTORY).icon(Material.IRON_BARDING).listener(new InventoryListener(p)).build(p); // Hurting things - new FlagBuilder().id(Flaggs.HURT_MOBS).icon(Material.STONE_SWORD).listener(new HurtingListener(p)).build(p); - new FlagBuilder().id(Flaggs.HURT_MONSTERS).icon(Material.WOOD_SWORD).build(p); + new FlagBuilder().id(Flags.HURT_MOBS).icon(Material.STONE_SWORD).listener(new HurtingListener(p)).build(p); + new FlagBuilder().id(Flags.HURT_MONSTERS).icon(Material.WOOD_SWORD).build(p); // Leashes - new FlagBuilder().id(Flaggs.LEASH).icon(Material.LEASH).listener(new LeashListener(p)).build(p); + new FlagBuilder().id(Flags.LEASH).icon(Material.LEASH).listener(new LeashListener(p)).build(p); // Portal use protection - new FlagBuilder().id(Flaggs.PORTAL).icon(Material.OBSIDIAN).listener(new PortalListener(p)).build(p); + new FlagBuilder().id(Flags.PORTAL).icon(Material.OBSIDIAN).listener(new PortalListener(p)).build(p); // Shearing - new FlagBuilder().id(Flaggs.SHEARING).icon(Material.SHEARS).listener(new ShearingListener(p)).build(p); + new FlagBuilder().id(Flags.SHEARING).icon(Material.SHEARS).listener(new ShearingListener(p)).build(p); // Item pickup or drop - new FlagBuilder().id(Flaggs.ITEM_DROP).icon(Material.DIRT).allowedByDefault(true).listener(new ItemDropPickUpListener(p)).build(p); - new FlagBuilder().id(Flaggs.ITEM_PICKUP).icon(Material.DIRT).build(p); + new FlagBuilder().id(Flags.ITEM_DROP).icon(Material.DIRT).allowedByDefault(true).listener(new ItemDropPickUpListener(p)).build(p); + new FlagBuilder().id(Flags.ITEM_PICKUP).icon(Material.DIRT).build(p); /* * Non-protection flags */ // PVP - new FlagBuilder().id(Flaggs.PVP_OVERWORLD).icon(Material.ARROW).listener(new PVPListener(p)).build(p); - new FlagBuilder().id(Flaggs.PVP_NETHER).icon(Material.IRON_AXE).build(p); - new FlagBuilder().id(Flaggs.PVP_END).icon(Material.END_CRYSTAL).build(p); - new FlagBuilder().id(Flaggs.ENTER_EXIT_MESSAGES).icon(Material.DIRT).allowedByDefault(true).build(p); - new FlagBuilder().id(Flaggs.MOB_SPAWN).icon(Material.APPLE).allowedByDefault(true).build(p); - new FlagBuilder().id(Flaggs.MONSTER_SPAWN).icon(Material.MOB_SPAWNER).allowedByDefault(true).build(p); + new FlagBuilder().id(Flags.PVP_OVERWORLD).icon(Material.ARROW).listener(new PVPListener(p)).build(p); + new FlagBuilder().id(Flags.PVP_NETHER).icon(Material.IRON_AXE).build(p); + new FlagBuilder().id(Flags.PVP_END).icon(Material.END_CRYSTAL).build(p); + new FlagBuilder().id(Flags.ENTER_EXIT_MESSAGES).icon(Material.DIRT).allowedByDefault(true).build(p); + new FlagBuilder().id(Flags.MOB_SPAWN).icon(Material.APPLE).allowedByDefault(true).build(p); + new FlagBuilder().id(Flags.MONSTER_SPAWN).icon(Material.MOB_SPAWNER).allowedByDefault(true).build(p); } @@ -163,10 +163,15 @@ public class FlagsManager { * @return Flag or null if not known */ public Flag getFlagByID(String key) { - for (Flaggs flag: Flaggs.values()) { + for (Flags flag: Flags.values()) { if (flag.name().equalsIgnoreCase(key)) return this.getFlagByID(flag); } return null; } + public boolean isAllowed(Flags flag) { + if (flags.containsKey(flag)) return flags.get(flag).isAllowed(); + return false; + } + } diff --git a/src/test/java/bskyblock/TestBSkyBlock.java b/src/test/java/bskyblock/TestBSkyBlock.java index 03b024aef..493f2ebaf 100644 --- a/src/test/java/bskyblock/TestBSkyBlock.java +++ b/src/test/java/bskyblock/TestBSkyBlock.java @@ -411,7 +411,7 @@ public class TestBSkyBlock { assertFalse(island.getBanned().contains(member1)); // Protection - new Flags(plugin); + new FlagsManager(plugin); // Check default settings // Owner should be able to do anything assertTrue(island.isAllowed(owner, Flags.PLACE_BLOCKS));