Implements flags as enum so flags are protected.

This commit is contained in:
tastybento 2018-02-05 14:51:03 -08:00
parent ffbf938a32
commit 645b4eb610
15 changed files with 152 additions and 268 deletions

View File

@ -11,7 +11,6 @@ import us.tastybento.bskyblock.database.managers.island.IslandsManager;
import us.tastybento.bskyblock.generators.IslandWorld; import us.tastybento.bskyblock.generators.IslandWorld;
import us.tastybento.bskyblock.listeners.JoinLeaveListener; import us.tastybento.bskyblock.listeners.JoinLeaveListener;
import us.tastybento.bskyblock.listeners.PanelListenerManager; import us.tastybento.bskyblock.listeners.PanelListenerManager;
import us.tastybento.bskyblock.lists.Flags;
import us.tastybento.bskyblock.managers.AddonsManager; import us.tastybento.bskyblock.managers.AddonsManager;
import us.tastybento.bskyblock.managers.CommandsManager; import us.tastybento.bskyblock.managers.CommandsManager;
import us.tastybento.bskyblock.managers.FlagsManager; import us.tastybento.bskyblock.managers.FlagsManager;
@ -92,7 +91,6 @@ public class BSkyBlock extends JavaPlugin {
// Load Flags // Load Flags
flagsManager = new FlagsManager(plugin); flagsManager = new FlagsManager(plugin);
new Flags(plugin);
// Load islands from database // Load islands from database
islandsManager.load(); islandsManager.load();

View File

@ -6,16 +6,16 @@ import org.bukkit.event.Listener;
import us.tastybento.bskyblock.BSkyBlock; import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.panels.PanelItem; import us.tastybento.bskyblock.api.panels.PanelItem;
import us.tastybento.bskyblock.lists.Flaggs; import us.tastybento.bskyblock.lists.Flags;
public class Flag implements Comparable<Flag> { public class Flag implements Comparable<Flag> {
private final Flaggs id; private final Flags id;
private final PanelItem icon; private final PanelItem icon;
private final Listener listener; private final Listener listener;
private boolean defaultSetting; 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.id = id2;
this.icon = icon; this.icon = icon;
this.listener = listener; this.listener = listener;
@ -24,7 +24,7 @@ public class Flag implements Comparable<Flag> {
plugin.getFlagsManager().registerFlag(this); plugin.getFlagsManager().registerFlag(this);
} }
public Flaggs getID() { public Flags getID() {
return id; return id;
} }

View File

@ -7,16 +7,16 @@ import org.bukkit.inventory.ItemStack;
import us.tastybento.bskyblock.BSkyBlock; import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.panels.PanelItem; import us.tastybento.bskyblock.api.panels.PanelItem;
import us.tastybento.bskyblock.api.panels.builders.PanelItemBuilder; import us.tastybento.bskyblock.api.panels.builders.PanelItemBuilder;
import us.tastybento.bskyblock.lists.Flaggs; import us.tastybento.bskyblock.lists.Flags;
public class FlagBuilder { public class FlagBuilder {
private Flaggs id; private Flags id;
private PanelItem icon; private PanelItem icon;
private Listener listener; private Listener listener;
private boolean defaultSetting; private boolean defaultSetting;
public FlagBuilder id(Flaggs flag) { public FlagBuilder id(Flags flag) {
this.id = flag; this.id = flag;
return this; return this;
} }

View File

@ -16,15 +16,14 @@ import org.bukkit.entity.Entity;
import us.tastybento.bskyblock.BSkyBlock; import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.commands.User; 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.IslandBaseEvent;
import us.tastybento.bskyblock.api.events.island.IslandEvent; 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.IslandLockEvent;
import us.tastybento.bskyblock.api.events.island.IslandEvent.IslandUnlockEvent; import us.tastybento.bskyblock.api.events.island.IslandEvent.IslandUnlockEvent;
import us.tastybento.bskyblock.api.events.island.IslandEvent.Reason; 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.Adapter;
import us.tastybento.bskyblock.database.objects.adapters.FlagSerializer; import us.tastybento.bskyblock.database.objects.adapters.FlagSerializer;
import us.tastybento.bskyblock.lists.Flags;
import us.tastybento.bskyblock.managers.RanksManager; import us.tastybento.bskyblock.managers.RanksManager;
import us.tastybento.bskyblock.util.Util; import us.tastybento.bskyblock.util.Util;
@ -83,7 +82,7 @@ public class Island implements DataObject {
//// Protection flags //// //// Protection flags ////
@Adapter(FlagSerializer.class) @Adapter(FlagSerializer.class)
private HashMap<Flag, Integer> flags = new HashMap<>(); private HashMap<Flags, Integer> flags = new HashMap<>();
private int levelHandicap; private int levelHandicap;
private Location spawnPoint; private Location spawnPoint;
@ -158,7 +157,7 @@ public class Island implements DataObject {
* @param flag * @param flag
* @return flag rank. Players must have at least this rank to bypass this 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)) { if(flags.containsKey(flag)) {
return flags.get(flag); return flags.get(flag);
} else { } else {
@ -170,7 +169,7 @@ public class Island implements DataObject {
/** /**
* @return the flags * @return the flags
*/ */
public HashMap<Flag, Integer> getFlags() { public HashMap<Flags, Integer> getFlags() {
return flags; return flags;
} }
@ -418,7 +417,7 @@ public class Island implements DataObject {
* @param flag * @param flag
* @return true if allowed, false if not * @return true if allowed, false if not
*/ */
public boolean isAllowed(Flag flag) { public boolean isAllowed(Flags flag) {
return this.getFlagReq(flag) >= 0 ? true : false; return this.getFlagReq(flag) >= 0 ? true : false;
} }
@ -428,7 +427,7 @@ public class Island implements DataObject {
* @param flag - flag * @param flag - flag
* @return true if allowed, false if not * @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)); //Bukkit.getLogger().info("DEBUG: " + flag.getID() + " user score = " + getRank(user) + " flag req = "+ this.getFlagReq(flag));
return (this.getRank(user) >= this.getFlagReq(flag)) ? true : false; return (this.getRank(user) >= this.getFlagReq(flag)) ? true : false;
} }
@ -507,14 +506,14 @@ public class Island implements DataObject {
* @param flag * @param flag
* @param value - rank value. If the flag applies to the island, a positive number = true, negative = false * @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); flags.put(flag, value);
} }
/** /**
* @param flags the flags to set * @param flags the flags to set
*/ */
public void setFlags(HashMap<Flag, Integer> flags) { public void setFlags(HashMap<Flags, Integer> flags) {
this.flags = flags; this.flags = flags;
} }

View File

@ -18,6 +18,7 @@ import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.commands.User; import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.api.flags.Flag; import us.tastybento.bskyblock.api.flags.Flag;
import us.tastybento.bskyblock.database.objects.Island; import us.tastybento.bskyblock.database.objects.Island;
import us.tastybento.bskyblock.lists.Flags;
/** /**
* Abstract class for flag listeners. Provides common code. * Abstract class for flag listeners. Provides common code.
@ -122,23 +123,23 @@ public abstract class AbstractFlagListener implements Listener {
* Generic place blocks checker * Generic place blocks checker
* @param e * @param e
* @param loc * @param loc
* @param flag * @param redstone
* @return true if the check is okay, false if it was disallowed * @return true if the check is okay, false if it was disallowed
*/ */
public boolean checkIsland(Event e, Location loc, Flag flag) { public boolean checkIsland(Event e, Location loc, Flags redstone) {
return checkIsland(e, loc, flag, false); return checkIsland(e, loc, redstone, false);
} }
/** /**
* Generic place blocks checker * Check if flag is allowed
* @param e * @param e
* @param loc * @param loc
* @param silent - if true, no attempt is made to tell the user * @param silent - if true, no attempt is made to tell the user
* @return true if the check is okay, false if it was disallowed * @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 the user is not set already, try to get it from the event
if (user == null) { 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 // 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); noGo(e, silent);
user = null; user = null;
return false; 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);
}
} }

View File

@ -9,7 +9,6 @@ import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import us.tastybento.bskyblock.BSkyBlock; import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.lists.Flaggs;
import us.tastybento.bskyblock.lists.Flags; import us.tastybento.bskyblock.lists.Flags;
/** /**
@ -33,7 +32,7 @@ public class BlockInteractionListener extends AbstractFlagListener {
} }
switch (e.getClickedBlock().getType()) { switch (e.getClickedBlock().getType()) {
case ANVIL: case ANVIL:
checkIsland(e, e.getClickedBlock().getLocation(), plugin.getFlagsManager().getFlagByID(Flaggs.ANVIL)); checkIsland(e, e.getClickedBlock().getLocation(), Flags.ANVIL);
break; break;
case BEACON: case BEACON:
checkIsland(e, e.getClickedBlock().getLocation(), Flags.BEACON); checkIsland(e, e.getClickedBlock().getLocation(), Flags.BEACON);

View File

@ -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 // 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); e.setCancelled(true);
user.sendMessage("protection.protected"); user.sendMessage("protection.protected");
} }

View File

@ -50,7 +50,7 @@ public class FireListener extends AbstractFlagListener {
if (!x.isAllowed(Flags.FIRE_SPREAD)) e.setCancelled(true); if (!x.isAllowed(Flags.FIRE_SPREAD)) e.setCancelled(true);
}); });
// If not on an island, check the default setting // 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 (!x.isAllowed(Flags.FIRE_SPREAD)) e.setCancelled(true);
}); });
// If not on an island, check the default setting // 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 (!x.isAllowed(Flags.FIRE)) e.setCancelled(true);
}); });
// If not on an island, check the default setting // 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 (!x.isAllowed(Flags.FIRE)) e.setCancelled(true);
}); });
// If not on an island, check the default setting // 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 either of these canceled the event, return
if (e.isCancelled()) return; if (e.isCancelled()) return;

View File

@ -32,7 +32,6 @@ import org.bukkit.potion.PotionEffect;
import us.tastybento.bskyblock.BSkyBlock; import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.commands.User; import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.api.flags.Flag;
import us.tastybento.bskyblock.lists.Flags; import us.tastybento.bskyblock.lists.Flags;
/** /**
@ -71,7 +70,7 @@ public class HurtingListener extends AbstractFlagListener {
* @param damager * @param damager
* @param flag * @param flag
*/ */
private void respond(Event event, Entity damager, Flag flag) { private void respond(Event event, Entity damager, Flags flag) {
// Get the attacker // Get the attacker
if (damager instanceof Player) { if (damager instanceof Player) {
setUser(User.getInstance(damager)).checkIsland(event, damager.getLocation(), flag); setUser(User.getInstance(damager)).checkIsland(event, damager.getLocation(), flag);

View File

@ -65,13 +65,13 @@ public class MobSpawnListener extends AbstractFlagListener {
} else { } else {
// Outside of the island // Outside of the island
if (e.getEntity() instanceof Monster || e.getEntity() instanceof Slime) { if (e.getEntity() instanceof Monster || e.getEntity() instanceof Slime) {
if (!Flags.MOB_SPAWN.isAllowed()) { if (!isAllowed(Flags.MOB_SPAWN)) {
// Mobs not allowed to spawn // Mobs not allowed to spawn
e.setCancelled(true); e.setCancelled(true);
return; return;
} }
} else if (e.getEntity() instanceof Animals) { } else if (e.getEntity() instanceof Animals) {
if (!Flags.MONSTER_SPAWN.isAllowed()) { if (!isAllowed(Flags.MONSTER_SPAWN)) {
// Mobs not allowed to spawn // Mobs not allowed to spawn
e.setCancelled(true); e.setCancelled(true);
return; return;

View File

@ -22,7 +22,6 @@ import org.bukkit.potion.PotionEffect;
import us.tastybento.bskyblock.BSkyBlock; import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.commands.User; import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.api.flags.Flag;
import us.tastybento.bskyblock.lists.Flags; import us.tastybento.bskyblock.lists.Flags;
/** /**
@ -49,14 +48,14 @@ public class PVPListener extends AbstractFlagListener {
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onEntityDamage(final EntityDamageByEntityEvent e) { public void onEntityDamage(final EntityDamageByEntityEvent e) {
if (e.getEntity() instanceof Player) { 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; 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; else if (e.getEntity().getWorld().equals(plugin.getIslandWorldManager().getEndWorld())) flag = Flags.PVP_END;
respond(e, e.getDamager(), flag); 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 // Get the attacker
if (damager instanceof Player) { if (damager instanceof Player) {
setUser(User.getInstance(damager)).checkIsland(event, damager.getLocation(), flag); setUser(User.getInstance(damager)).checkIsland(event, damager.getLocation(), flag);
@ -77,7 +76,7 @@ public class PVPListener extends AbstractFlagListener {
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onFishing(PlayerFishEvent e) { public void onFishing(PlayerFishEvent e) {
if (e.getCaught() != null && e.getCaught() instanceof Player) { 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; 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; else if (e.getCaught().getWorld().equals(plugin.getIslandWorldManager().getEndWorld())) flag = Flags.PVP_END;
if (checkIsland(e, e.getCaught().getLocation(), flag)) { if (checkIsland(e, e.getCaught().getLocation(), flag)) {
@ -94,7 +93,7 @@ public class PVPListener extends AbstractFlagListener {
@EventHandler(priority = EventPriority.LOW, ignoreCancelled=true) @EventHandler(priority = EventPriority.LOW, ignoreCancelled=true)
public void onSplashPotionSplash(final PotionSplashEvent e) { public void onSplashPotionSplash(final PotionSplashEvent e) {
// Deduce the world // 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; 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; 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())) { if (e.getCause().equals(DamageCause.ENTITY_ATTACK) && thrownPotions.containsKey(e.getDamager().getEntityId())) {
// Deduce the world // 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; 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; else if (e.getEntity().getWorld().equals(plugin.getIslandWorldManager().getEndWorld())) flag = Flags.PVP_END;

View File

@ -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
}

View File

@ -1,128 +1,51 @@
package us.tastybento.bskyblock.lists; package us.tastybento.bskyblock.lists;
import org.bukkit.Material;
import us.tastybento.bskyblock.BSkyBlock; public enum Flags {
import us.tastybento.bskyblock.api.flags.FlagBuilder; ANVIL,
import us.tastybento.bskyblock.listeners.flags.BlockInteractionListener; ARMOR_STAND,
import us.tastybento.bskyblock.listeners.flags.BreakBlocksListener; BEACON,
import us.tastybento.bskyblock.listeners.flags.BreedingListener; BED,
import us.tastybento.bskyblock.listeners.flags.BucketListener; BREAK_BLOCKS,
import us.tastybento.bskyblock.listeners.flags.EggListener; BREEDING,
import us.tastybento.bskyblock.listeners.flags.EntityInteractListener; BREWING,
import us.tastybento.bskyblock.listeners.flags.FireListener; BUCKET,
import us.tastybento.bskyblock.listeners.flags.HurtingListener; CHEST,
import us.tastybento.bskyblock.listeners.flags.InventoryListener; CHORUS_FRUIT,
import us.tastybento.bskyblock.listeners.flags.ItemDropPickUpListener; COLLECT_LAVA,
import us.tastybento.bskyblock.listeners.flags.LeashListener; COLLECT_WATER,
import us.tastybento.bskyblock.listeners.flags.PVPListener; CRAFTING,
import us.tastybento.bskyblock.listeners.flags.PhysicalInteractionListener; CROP_TRAMPLE,
import us.tastybento.bskyblock.listeners.flags.PlaceBlocksListener; DOOR,
import us.tastybento.bskyblock.listeners.flags.PortalListener; EGGS,
import us.tastybento.bskyblock.listeners.flags.ShearingListener; ENCHANTING,
import us.tastybento.bskyblock.listeners.flags.TeleportationListener; ENDER_PEARL,
ENTER_EXIT_MESSAGES,
/** FIRE,
* Protection flags FIRE_EXTINGUISH,
* @author tastybento FIRE_SPREAD,
* FURNACE,
*/ GATE,
HURT_MOBS,
public class Flags { HURT_MONSTERS,
ITEM_DROP,
private BSkyBlock p; ITEM_PICKUP,
LEASH,
public Flags(BSkyBlock plugin) { LEVER_BUTTON,
p = plugin; MILKING,
MOB_SPAWN,
// Break and place blocks MONSTER_SPAWN,
new FlagBuilder().id(Flaggs.BREAK_BLOCKS).icon(Material.STONE).listener(new BreakBlocksListener(p)).build(p); MOUNT_INVENTORY,
new FlagBuilder().id(Flaggs.PLACE_BLOCKS).icon(Material.DIRT).listener(new PlaceBlocksListener(p)).build(p); MUSIC,
PLACE_BLOCKS,
// Block interactions - all use BlockInteractionListener() PORTAL,
new FlagBuilder().id(Flaggs.ANVIL).icon(Material.ANVIL).listener(new BlockInteractionListener(p)).build(p); PRESSURE_PLATE,
new FlagBuilder().id(Flaggs.BEACON).icon(Material.BEACON).build(p); PVP_END,
new FlagBuilder().id(Flaggs.BED).icon(Material.BED).build(p); PVP_NETHER,
new FlagBuilder().id(Flaggs.BREWING).icon(Material.BREWING_STAND_ITEM).build(p); PVP_OVERWORLD,
new FlagBuilder().id(Flaggs.CHEST).icon(Material.CHEST).build(p); REDSTONE,
new FlagBuilder().id(Flaggs.DOOR).allowedByDefault(true).icon(Material.WOODEN_DOOR).build(p); RIDING,
new FlagBuilder().id(Flaggs.CRAFTING).allowedByDefault(true).icon(Material.WORKBENCH).build(p); SHEARING,
new FlagBuilder().id(Flaggs.ENCHANTING).allowedByDefault(true).icon(Material.ENCHANTMENT_TABLE).build(p); SPAWN_EGGS,
new FlagBuilder().id(Flaggs.FURNACE).icon(Material.FURNACE).build(p); TRADING
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);
}
} }

View File

@ -25,7 +25,7 @@ import us.tastybento.bskyblock.listeners.flags.PlaceBlocksListener;
import us.tastybento.bskyblock.listeners.flags.PortalListener; import us.tastybento.bskyblock.listeners.flags.PortalListener;
import us.tastybento.bskyblock.listeners.flags.ShearingListener; import us.tastybento.bskyblock.listeners.flags.ShearingListener;
import us.tastybento.bskyblock.listeners.flags.TeleportationListener; import us.tastybento.bskyblock.listeners.flags.TeleportationListener;
import us.tastybento.bskyblock.lists.Flaggs; import us.tastybento.bskyblock.lists.Flags;
public class FlagsManager { public class FlagsManager {
@ -38,7 +38,7 @@ public class FlagsManager {
registerFlags(); registerFlags();
} }
private HashMap<Flaggs, Flag> flags = new HashMap<>(); private HashMap<Flags, Flag> flags = new HashMap<>();
public void registerFlag(Flag flag) { public void registerFlag(Flag flag) {
//TODO all the security checks //TODO all the security checks
@ -48,11 +48,11 @@ public class FlagsManager {
flag.getListener().ifPresent(l -> p.getServer().getPluginManager().registerEvents(l, p)); flag.getListener().ifPresent(l -> p.getServer().getPluginManager().registerEvents(l, p));
} }
public HashMap<Flaggs, Flag> getFlags() { public HashMap<Flags, Flag> getFlags() {
return flags; return flags;
} }
public Flag getFlagByID(Flaggs id) { public Flag getFlagByID(Flags id) {
return flags.get(id); return flags.get(id);
} }
@ -66,49 +66,49 @@ public class FlagsManager {
private void registerFlags() { private void registerFlags() {
// Break and place blocks // Break and place blocks
new FlagBuilder().id(Flaggs.BREAK_BLOCKS).icon(Material.STONE).listener(new BreakBlocksListener(p)).build(p); new FlagBuilder().id(Flags.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.PLACE_BLOCKS).icon(Material.DIRT).listener(new PlaceBlocksListener(p)).build(p);
// Block interactions - all use BlockInteractionListener() // Block interactions - all use BlockInteractionListener()
new FlagBuilder().id(Flaggs.ANVIL).icon(Material.ANVIL).listener(new BlockInteractionListener(p)).build(p); new FlagBuilder().id(Flags.ANVIL).icon(Material.ANVIL).listener(new BlockInteractionListener(p)).build(p);
new FlagBuilder().id(Flaggs.BEACON).icon(Material.BEACON).build(p); new FlagBuilder().id(Flags.BEACON).icon(Material.BEACON).build(p);
new FlagBuilder().id(Flaggs.BED).icon(Material.BED).build(p); new FlagBuilder().id(Flags.BED).icon(Material.BED).build(p);
new FlagBuilder().id(Flaggs.BREWING).icon(Material.BREWING_STAND_ITEM).build(p); new FlagBuilder().id(Flags.BREWING).icon(Material.BREWING_STAND_ITEM).build(p);
new FlagBuilder().id(Flaggs.CHEST).icon(Material.CHEST).build(p); new FlagBuilder().id(Flags.CHEST).icon(Material.CHEST).build(p);
new FlagBuilder().id(Flaggs.DOOR).allowedByDefault(true).icon(Material.WOODEN_DOOR).build(p); new FlagBuilder().id(Flags.DOOR).allowedByDefault(true).icon(Material.WOODEN_DOOR).build(p);
new FlagBuilder().id(Flaggs.CRAFTING).allowedByDefault(true).icon(Material.WORKBENCH).build(p); new FlagBuilder().id(Flags.CRAFTING).allowedByDefault(true).icon(Material.WORKBENCH).build(p);
new FlagBuilder().id(Flaggs.ENCHANTING).allowedByDefault(true).icon(Material.ENCHANTMENT_TABLE).build(p); new FlagBuilder().id(Flags.ENCHANTING).allowedByDefault(true).icon(Material.ENCHANTMENT_TABLE).build(p);
new FlagBuilder().id(Flaggs.FURNACE).icon(Material.FURNACE).build(p); new FlagBuilder().id(Flags.FURNACE).icon(Material.FURNACE).build(p);
new FlagBuilder().id(Flaggs.GATE).allowedByDefault(true).icon(Material.FENCE_GATE).build(p); new FlagBuilder().id(Flags.GATE).allowedByDefault(true).icon(Material.FENCE_GATE).build(p);
new FlagBuilder().id(Flaggs.MUSIC).icon(Material.JUKEBOX).build(p); new FlagBuilder().id(Flags.MUSIC).icon(Material.JUKEBOX).build(p);
new FlagBuilder().id(Flaggs.LEVER_BUTTON).icon(Material.LEVER).build(p); new FlagBuilder().id(Flags.LEVER_BUTTON).icon(Material.LEVER).build(p);
new FlagBuilder().id(Flaggs.REDSTONE).icon(Material.REDSTONE).build(p); new FlagBuilder().id(Flags.REDSTONE).icon(Material.REDSTONE).build(p);
new FlagBuilder().id(Flaggs.SPAWN_EGGS).icon(Material.MONSTER_EGG).build(p); new FlagBuilder().id(Flags.SPAWN_EGGS).icon(Material.MONSTER_EGG).build(p);
// Entity interactions // Entity interactions
new FlagBuilder().id(Flaggs.ARMOR_STAND).icon(Material.ARMOR_STAND).listener(new EntityInteractListener(p)).build(p); new FlagBuilder().id(Flags.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(Flags.RIDING).icon(Material.GOLD_BARDING).build(p);
new FlagBuilder().id(Flaggs.TRADING).allowedByDefault(true).icon(Material.EMERALD).build(p); new FlagBuilder().id(Flags.TRADING).allowedByDefault(true).icon(Material.EMERALD).build(p);
// Breeding // 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 // 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(Flags.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(Flags.COLLECT_LAVA).icon(Material.LAVA_BUCKET).build(p);
new FlagBuilder().id(Flaggs.COLLECT_WATER).icon(Material.WATER_BUCKET).build(p); new FlagBuilder().id(Flags.COLLECT_WATER).icon(Material.WATER_BUCKET).build(p);
new FlagBuilder().id(Flaggs.MILKING).icon(Material.MILK_BUCKET).build(p); new FlagBuilder().id(Flags.MILKING).icon(Material.MILK_BUCKET).build(p);
// Chorus Fruit and Enderpearls // Chorus Fruit and Enderpearls
new FlagBuilder().id(Flaggs.CHORUS_FRUIT).icon(Material.CHORUS_FRUIT).listener(new TeleportationListener(p)).build(p); new FlagBuilder().id(Flags.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.ENDER_PEARL).icon(Material.ENDER_PEARL).build(p);
// Physical interactions // Physical interactions
new FlagBuilder().id(Flaggs.CROP_TRAMPLE).icon(Material.WHEAT).listener(new PhysicalInteractionListener(p)).build(p); new FlagBuilder().id(Flags.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.PRESSURE_PLATE).icon(Material.GOLD_PLATE).build(p);
// Egg throwing // 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 * Fire
@ -120,40 +120,40 @@ public class FlagsManager {
* I'll take you to burn * I'll take you to burn
* - The Crazy World of Arthur Brown * - 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(Flags.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(Flags.FIRE_EXTINGUISH).icon(Material.POTION).build(p);
new FlagBuilder().id(Flaggs.FIRE_SPREAD).icon(Material.FIREWORK_CHARGE).build(p); new FlagBuilder().id(Flags.FIRE_SPREAD).icon(Material.FIREWORK_CHARGE).build(p);
// Inventories // 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 // Hurting things
new FlagBuilder().id(Flaggs.HURT_MOBS).icon(Material.STONE_SWORD).listener(new HurtingListener(p)).build(p); new FlagBuilder().id(Flags.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_MONSTERS).icon(Material.WOOD_SWORD).build(p);
// Leashes // 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 // 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 // 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 // Item pickup or drop
new FlagBuilder().id(Flaggs.ITEM_DROP).icon(Material.DIRT).allowedByDefault(true).listener(new ItemDropPickUpListener(p)).build(p); new FlagBuilder().id(Flags.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_PICKUP).icon(Material.DIRT).build(p);
/* /*
* Non-protection flags * Non-protection flags
*/ */
// PVP // PVP
new FlagBuilder().id(Flaggs.PVP_OVERWORLD).icon(Material.ARROW).listener(new PVPListener(p)).build(p); new FlagBuilder().id(Flags.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(Flags.PVP_NETHER).icon(Material.IRON_AXE).build(p);
new FlagBuilder().id(Flaggs.PVP_END).icon(Material.END_CRYSTAL).build(p); new FlagBuilder().id(Flags.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(Flags.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(Flags.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.MONSTER_SPAWN).icon(Material.MOB_SPAWNER).allowedByDefault(true).build(p);
} }
@ -163,10 +163,15 @@ public class FlagsManager {
* @return Flag or null if not known * @return Flag or null if not known
*/ */
public Flag getFlagByID(String key) { public Flag getFlagByID(String key) {
for (Flaggs flag: Flaggs.values()) { for (Flags flag: Flags.values()) {
if (flag.name().equalsIgnoreCase(key)) return this.getFlagByID(flag); if (flag.name().equalsIgnoreCase(key)) return this.getFlagByID(flag);
} }
return null; return null;
} }
public boolean isAllowed(Flags flag) {
if (flags.containsKey(flag)) return flags.get(flag).isAllowed();
return false;
}
} }

View File

@ -411,7 +411,7 @@ public class TestBSkyBlock {
assertFalse(island.getBanned().contains(member1)); assertFalse(island.getBanned().contains(member1));
// Protection // Protection
new Flags(plugin); new FlagsManager(plugin);
// Check default settings // Check default settings
// Owner should be able to do anything // Owner should be able to do anything
assertTrue(island.isAllowed(owner, Flags.PLACE_BLOCKS)); assertTrue(island.isAllowed(owner, Flags.PLACE_BLOCKS));