Came full circle - kept Flags as final statics

This approach simulates an enum, but one that can be extended by others
to add custom flags. I added a handy values() method that uses
reflection to provide a list of all the flags in the class.

See TestBSkyBlock.java test classes for the tests of the default flag
registration and the custom flag registration.
This commit is contained in:
Tastybento 2018-02-05 23:53:07 -08:00
parent be5404e9c2
commit ace6294e23
26 changed files with 255 additions and 387 deletions

View File

@ -5,7 +5,6 @@ import java.util.Optional;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import us.tastybento.bskyblock.api.panels.PanelItem; import us.tastybento.bskyblock.api.panels.PanelItem;
import us.tastybento.bskyblock.lists.Flags;
public class Flag implements Comparable<Flag> { public class Flag implements Comparable<Flag> {
@ -14,20 +13,20 @@ public class Flag implements Comparable<Flag> {
SETTING SETTING
} }
private final Flags id; private final String id;
private final PanelItem icon; private final PanelItem icon;
private final Listener listener; private final Listener listener;
private final FlagType type; private final FlagType type;
private boolean defaultSetting; private boolean defaultSetting;
public Flag(Flags id2, PanelItem icon, Listener listener, boolean defaultSetting, FlagType type) { public Flag(String id2, PanelItem icon, Listener listener, boolean defaultSetting, FlagType type) {
this.id = id2; this.id = id2;
this.icon = icon; this.icon = icon;
this.listener = listener; this.listener = listener;
this.type = type; this.type = type;
} }
public Flags getID() { public String getID() {
return id; return id;
} }

View File

@ -7,18 +7,17 @@ import org.bukkit.inventory.ItemStack;
import us.tastybento.bskyblock.api.flags.Flag.FlagType; import us.tastybento.bskyblock.api.flags.Flag.FlagType;
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.Flags;
public class FlagBuilder { public class FlagBuilder {
private Flags id; private String id;
private PanelItem icon; private PanelItem icon;
private Listener listener; private Listener listener;
private boolean defaultSetting; private boolean defaultSetting;
private FlagType type = FlagType.PROTECTION; private FlagType type = FlagType.PROTECTION;
public FlagBuilder id(Flags flag) { public FlagBuilder id(String string) {
this.id = flag; this.id = string;
return this; return this;
} }
@ -63,4 +62,14 @@ public class FlagBuilder {
this.type = type; this.type = type;
return this; return this;
} }
/**
* Set the id of this flag to the name of this enum value
* @param flag
* @return
*/
public FlagBuilder id(Enum<?> flag) {
this.id = flag.name();
return this;
}
} }

View File

@ -21,9 +21,9 @@ 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;
@ -82,7 +82,7 @@ public class Island implements DataObject {
//// Protection flags //// //// Protection flags ////
@Adapter(FlagSerializer.class) @Adapter(FlagSerializer.class)
private HashMap<Flags, Integer> flags = new HashMap<>(); private HashMap<Flag, Integer> flags = new HashMap<>();
private int levelHandicap; private int levelHandicap;
private Location spawnPoint; private Location spawnPoint;
@ -154,14 +154,14 @@ public class Island implements DataObject {
/** /**
* Get the Island Guard flag ranking * Get the Island Guard flag ranking
* @param flag * @param breakBlocks
* @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(Flags flag){ public int getFlagReq(Flag breakBlocks){
if(flags.containsKey(flag)) { if(flags.containsKey(breakBlocks)) {
return flags.get(flag); return flags.get(breakBlocks);
} else { } else {
flags.put(flag, RanksManager.MEMBER_RANK); flags.put(breakBlocks, RanksManager.MEMBER_RANK);
return RanksManager.MEMBER_RANK; return RanksManager.MEMBER_RANK;
} }
} }
@ -169,7 +169,7 @@ public class Island implements DataObject {
/** /**
* @return the flags * @return the flags
*/ */
public HashMap<Flags, Integer> getFlags() { public HashMap<Flag, Integer> getFlags() {
return flags; return flags;
} }
@ -414,11 +414,11 @@ public class Island implements DataObject {
/** /**
* Check if the flag is allowed or not * Check if the flag is allowed or not
* For flags that are for the island in general and not related to rank * For flags that are for the island in general and not related to rank
* @param flag * @param breakBlocks
* @return true if allowed, false if not * @return true if allowed, false if not
*/ */
public boolean isAllowed(Flags flag) { public boolean isAllowed(Flag breakBlocks) {
return this.getFlagReq(flag) >= 0 ? true : false; return this.getFlagReq(breakBlocks) >= 0 ? true : false;
} }
/** /**
@ -427,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, Flags flag) { public boolean isAllowed(User user, Flag 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;
} }
@ -506,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(Flags flag, int value){ public void setFlag(Flag 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<Flags, Integer> flags) { public void setFlags(HashMap<Flag, Integer> flags) {
this.flags = flags; this.flags = flags;
} }

View File

@ -46,7 +46,7 @@ public class FlagSerializer implements AdapterInterface<HashMap<Flag, Integer>,
return result; return result;
HashMap<Flag, Integer> flags = (HashMap<Flag, Integer>)object; HashMap<Flag, Integer> flags = (HashMap<Flag, Integer>)object;
for (Entry<Flag, Integer> en: flags.entrySet()) { for (Entry<Flag, Integer> en: flags.entrySet()) {
result.put(en.getKey().getID().name(), en.getValue()); result.put(en.getKey().getID(), en.getValue());
} }
return result; return result;
} }

View File

@ -19,7 +19,6 @@ 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.api.flags.Flag.FlagType; import us.tastybento.bskyblock.api.flags.Flag.FlagType;
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.
@ -28,14 +27,17 @@ import us.tastybento.bskyblock.lists.Flags;
*/ */
public abstract class AbstractFlagListener implements Listener { public abstract class AbstractFlagListener implements Listener {
public BSkyBlock plugin; public BSkyBlock plugin = BSkyBlock.getInstance();
private User user = null; private User user = null;
public AbstractFlagListener(BSkyBlock plugin) { /**
super(); * Used for unit testing only to set the plugin
* @param plugin
*/
public void setPlugin(BSkyBlock plugin) {
this.plugin = plugin; this.plugin = plugin;
} }
/** /**
* Sets the player associated with this event. * Sets the player associated with this event.
* If the user is a fake player, they are not counted. * If the user is a fake player, they are not counted.
@ -124,11 +126,11 @@ public abstract class AbstractFlagListener implements Listener {
* Generic place blocks checker * Generic place blocks checker
* @param e * @param e
* @param loc * @param loc
* @param redstone * @param breakBlocks
* @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, Flags redstone) { public boolean checkIsland(Event e, Location loc, Flag breakBlocks) {
return checkIsland(e, loc, redstone, false); return checkIsland(e, loc, breakBlocks, false);
} }
@ -140,7 +142,7 @@ public abstract class AbstractFlagListener implements Listener {
* @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, Flags flag, boolean silent) { public boolean checkIsland(Event e, Location loc, Flag flag, boolean silent) {
// If this is not an Island World, skip // If this is not an Island World, skip
if (!inWorld(loc)) return true; if (!inWorld(loc)) return true;
@ -148,11 +150,9 @@ public abstract class AbstractFlagListener implements Listener {
Optional<Island> island = plugin.getIslands().getIslandAt(loc); Optional<Island> island = plugin.getIslands().getIslandAt(loc);
// Handle Settings Flag // Handle Settings Flag
id(flag); if (flag.getType().equals(FlagType.SETTING)) {
id(flag).getType();
if (id(flag).getType().equals(FlagType.SETTING)) {
// If the island exists, return the setting, otherwise return the default setting for this flag // If the island exists, return the setting, otherwise return the default setting for this flag
return island.map(x -> x.isAllowed(flag)).orElse(isDefaultAllowed(flag)); return island.map(x -> x.isAllowed(flag)).orElse(flag.isDefaultSetting());
} }
// Protection flag // Protection flag
@ -182,7 +182,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 (!isDefaultAllowed(flag)) { if (!flag.isDefaultSetting()) {
noGo(e, silent); noGo(e, silent);
user = null; user = null;
return false; return false;
@ -194,19 +194,11 @@ public abstract class AbstractFlagListener implements Listener {
/** /**
* Get the flag for this ID * Get the flag for this ID
* @param flag * @param id
* @return Flag denoted by the id * @return Flag denoted by the id
*/ */
protected Flag id(Flags flag) { protected Flag id(String id) {
return plugin.getFlagsManager().getFlagByID(flag); return plugin.getFlagsManager().getFlagByID(id);
} }
/**
* Return the default setting for this flag
* @param flag
* @return
*/
protected boolean isDefaultAllowed(Flags flag) {
return plugin.getFlagsManager().isDefaultAllowed(flag);
}
} }

View File

@ -8,7 +8,6 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.block.Action; 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.lists.Flags; import us.tastybento.bskyblock.lists.Flags;
/** /**
@ -17,10 +16,6 @@ import us.tastybento.bskyblock.lists.Flags;
*/ */
public class BlockInteractionListener extends AbstractFlagListener { public class BlockInteractionListener extends AbstractFlagListener {
public BlockInteractionListener(BSkyBlock plugin) {
super(plugin);
}
/** /**
* Handle interaction with blocks * Handle interaction with blocks
* @param e * @param e

View File

@ -17,16 +17,11 @@ import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.vehicle.VehicleDamageEvent; import org.bukkit.event.vehicle.VehicleDamageEvent;
import org.bukkit.util.BlockIterator; import org.bukkit.util.BlockIterator;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.commands.User; import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.lists.Flags; import us.tastybento.bskyblock.lists.Flags;
public class BreakBlocksListener extends AbstractFlagListener { public class BreakBlocksListener extends AbstractFlagListener {
public BreakBlocksListener(BSkyBlock plugin) {
super(plugin);
}
/** /**
* Prevents blocks from being broken * Prevents blocks from being broken
* *
@ -107,7 +102,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 (!isDefaultAllowed(Flags.BREAK_BLOCKS)) { if (!Flags.BREAK_BLOCKS.isDefaultSetting()) {
e.setCancelled(true); e.setCancelled(true);
user.sendMessage("protection.protected"); user.sendMessage("protection.protected");
} }

View File

@ -14,7 +14,6 @@ import org.bukkit.event.player.PlayerInteractAtEntityEvent;
import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.lists.Flags; import us.tastybento.bskyblock.lists.Flags;
/** /**
@ -25,11 +24,6 @@ import us.tastybento.bskyblock.lists.Flags;
*/ */
public class BreedingListener extends AbstractFlagListener { public class BreedingListener extends AbstractFlagListener {
public BreedingListener(BSkyBlock plugin) {
super(plugin);
}
/** /**
* A list of items that cause breeding if a player has them in their hand and they click an animal * A list of items that cause breeding if a player has them in their hand and they click an animal
* This list may need to be extended with future versions of Minecraft. * This list may need to be extended with future versions of Minecraft.

View File

@ -10,7 +10,6 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerBucketEmptyEvent; import org.bukkit.event.player.PlayerBucketEmptyEvent;
import org.bukkit.event.player.PlayerBucketFillEvent; import org.bukkit.event.player.PlayerBucketFillEvent;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.lists.Flags; import us.tastybento.bskyblock.lists.Flags;
/** /**
@ -21,10 +20,6 @@ import us.tastybento.bskyblock.lists.Flags;
*/ */
public class BucketListener extends AbstractFlagListener { public class BucketListener extends AbstractFlagListener {
public BucketListener(BSkyBlock plugin) {
super(plugin);
}
/** /**
* Prevents emptying of buckets * Prevents emptying of buckets
* @param e * @param e

View File

@ -7,7 +7,6 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerEggThrowEvent; import org.bukkit.event.player.PlayerEggThrowEvent;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.lists.Flags; import us.tastybento.bskyblock.lists.Flags;
/** /**
@ -17,10 +16,6 @@ import us.tastybento.bskyblock.lists.Flags;
*/ */
public class EggListener extends AbstractFlagListener { public class EggListener extends AbstractFlagListener {
public EggListener(BSkyBlock plugin) {
super(plugin);
}
/** /**
* Handle visitor chicken egg throwing * Handle visitor chicken egg throwing
* @param e * @param e

View File

@ -12,7 +12,6 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerInteractAtEntityEvent; import org.bukkit.event.player.PlayerInteractAtEntityEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent; import org.bukkit.event.player.PlayerInteractEntityEvent;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.lists.Flags; import us.tastybento.bskyblock.lists.Flags;
/** /**
@ -23,10 +22,6 @@ import us.tastybento.bskyblock.lists.Flags;
*/ */
public class EntityInteractListener extends AbstractFlagListener { public class EntityInteractListener extends AbstractFlagListener {
public EntityInteractListener(BSkyBlock plugin) {
super(plugin);
}
@EventHandler(priority = EventPriority.LOW, ignoreCancelled=true) @EventHandler(priority = EventPriority.LOW, ignoreCancelled=true)
public void onPlayerInteract(final PlayerInteractAtEntityEvent e) { public void onPlayerInteract(final PlayerInteractAtEntityEvent e) {
if (e.getRightClicked() instanceof ArmorStand) { if (e.getRightClicked() instanceof ArmorStand) {

View File

@ -19,7 +19,6 @@ import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.util.BlockIterator; import org.bukkit.util.BlockIterator;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.commands.User; import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.database.objects.Island; import us.tastybento.bskyblock.database.objects.Island;
import us.tastybento.bskyblock.lists.Flags; import us.tastybento.bskyblock.lists.Flags;
@ -31,10 +30,6 @@ import us.tastybento.bskyblock.lists.Flags;
*/ */
public class FireListener extends AbstractFlagListener { public class FireListener extends AbstractFlagListener {
public FireListener(BSkyBlock plugin) {
super(plugin);
}
/** /**
* Prevents fire spread * Prevents fire spread
* @param e * @param e
@ -50,7 +45,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() && !isDefaultAllowed(Flags.FIRE_SPREAD)) e.setCancelled(true); if (!island.isPresent() && !Flags.FIRE_SPREAD.isDefaultSetting()) e.setCancelled(true);
} }
/** /**
@ -69,7 +64,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() && !isDefaultAllowed(Flags.FIRE_SPREAD)) e.setCancelled(true); if (!island.isPresent() && !Flags.FIRE_SPREAD.isDefaultSetting()) e.setCancelled(true);
} }
} }
@ -92,7 +87,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() && !isDefaultAllowed(Flags.FIRE)) e.setCancelled(true); if (!island.isPresent() && !Flags.FIRE.isDefaultSetting()) e.setCancelled(true);
} }
@ -145,7 +140,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() && !isDefaultAllowed(Flags.FIRE)) e.setCancelled(true); if (!island.isPresent() && !Flags.FIRE.isDefaultSetting()) 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

@ -30,8 +30,8 @@ import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
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;
/** /**
@ -41,10 +41,6 @@ import us.tastybento.bskyblock.lists.Flags;
*/ */
public class HurtingListener extends AbstractFlagListener { public class HurtingListener extends AbstractFlagListener {
public HurtingListener(BSkyBlock plugin) {
super(plugin);
}
private HashMap<Integer, UUID> thrownPotions = new HashMap<>(); private HashMap<Integer, UUID> thrownPotions = new HashMap<>();
@ -68,17 +64,17 @@ public class HurtingListener extends AbstractFlagListener {
* Finds the true attacker, even if the attack was via a projectile * Finds the true attacker, even if the attack was via a projectile
* @param event * @param event
* @param damager * @param damager
* @param flag * @param hurtMobs
*/ */
private void respond(Event event, Entity damager, Flags flag) { private void respond(Event event, Entity damager, Flag hurtMobs) {
// 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(), hurtMobs);
} else if (damager instanceof Projectile) { } else if (damager instanceof Projectile) {
// Find out who fired the projectile // Find out who fired the projectile
Projectile p = (Projectile) damager; Projectile p = (Projectile) damager;
if (p.getShooter() instanceof Player) { if (p.getShooter() instanceof Player) {
if (!setUser(User.getInstance((Player)p.getShooter())).checkIsland(event, damager.getLocation(), flag)) { if (!setUser(User.getInstance((Player)p.getShooter())).checkIsland(event, damager.getLocation(), hurtMobs)) {
damager.setFireTicks(0); damager.setFireTicks(0);
damager.remove(); damager.remove();
} }

View File

@ -16,7 +16,6 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryClickEvent;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.commands.User; import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.lists.Flags; import us.tastybento.bskyblock.lists.Flags;
@ -27,10 +26,6 @@ import us.tastybento.bskyblock.lists.Flags;
*/ */
public class InventoryListener extends AbstractFlagListener { public class InventoryListener extends AbstractFlagListener {
public InventoryListener(BSkyBlock plugin) {
super(plugin);
}
/** /**
* Prevents visitors picking items from inventories * Prevents visitors picking items from inventories
* @param e * @param e

View File

@ -9,7 +9,6 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.EntityPickupItemEvent; import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.event.player.PlayerDropItemEvent; import org.bukkit.event.player.PlayerDropItemEvent;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.commands.User; import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.lists.Flags; import us.tastybento.bskyblock.lists.Flags;
@ -19,10 +18,6 @@ import us.tastybento.bskyblock.lists.Flags;
*/ */
public class ItemDropPickUpListener extends AbstractFlagListener { public class ItemDropPickUpListener extends AbstractFlagListener {
public ItemDropPickUpListener(BSkyBlock plugin) {
super(plugin);
}
/* /*
* Handle item drop by visitors * Handle item drop by visitors
*/ */

View File

@ -10,7 +10,6 @@ import org.bukkit.event.entity.PlayerLeashEntityEvent;
import org.bukkit.event.hanging.HangingPlaceEvent; import org.bukkit.event.hanging.HangingPlaceEvent;
import org.bukkit.event.player.PlayerUnleashEntityEvent; import org.bukkit.event.player.PlayerUnleashEntityEvent;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.lists.Flags; import us.tastybento.bskyblock.lists.Flags;
/** /**
@ -19,11 +18,6 @@ import us.tastybento.bskyblock.lists.Flags;
*/ */
public class LeashListener extends AbstractFlagListener { public class LeashListener extends AbstractFlagListener {
public LeashListener(BSkyBlock plugin) {
super(plugin);
}
/** /**
* Prevents leashing * Prevents leashing
* *

View File

@ -13,7 +13,6 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.CreatureSpawnEvent; import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason; import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.database.objects.Island; import us.tastybento.bskyblock.database.objects.Island;
import us.tastybento.bskyblock.lists.Flags; import us.tastybento.bskyblock.lists.Flags;
@ -24,10 +23,6 @@ import us.tastybento.bskyblock.lists.Flags;
*/ */
public class MobSpawnListener extends AbstractFlagListener { public class MobSpawnListener extends AbstractFlagListener {
public MobSpawnListener(BSkyBlock plugin) {
super(plugin);
}
/** /**
* Prevents mobs spawning naturally * Prevents mobs spawning naturally
* *
@ -65,13 +60,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 (!isDefaultAllowed(Flags.MOB_SPAWN)) { if (!Flags.MOB_SPAWN.isDefaultSetting()) {
// 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 (!isDefaultAllowed(Flags.MONSTER_SPAWN)) { if (!Flags.MONSTER_SPAWN.isDefaultSetting()) {
// Mobs not allowed to spawn // Mobs not allowed to spawn
e.setCancelled(true); e.setCancelled(true);
return; return;

View File

@ -20,8 +20,8 @@ import org.bukkit.event.entity.PotionSplashEvent;
import org.bukkit.event.player.PlayerFishEvent; import org.bukkit.event.player.PlayerFishEvent;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
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;
/** /**
@ -32,10 +32,6 @@ import us.tastybento.bskyblock.lists.Flags;
*/ */
public class PVPListener extends AbstractFlagListener { public class PVPListener extends AbstractFlagListener {
public PVPListener(BSkyBlock plugin) {
super(plugin);
}
private HashMap<Integer, UUID> thrownPotions = new HashMap<>(); private HashMap<Integer, UUID> thrownPotions = new HashMap<>();
@ -48,14 +44,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) {
Flags flag = Flags.PVP_OVERWORLD; Flag 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, Flags flag) { private void respond(Event event, Entity damager, Flag 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);
@ -76,7 +72,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) {
Flags flag = Flags.PVP_OVERWORLD; Flag 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)) {
@ -93,7 +89,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
Flags flag = Flags.PVP_OVERWORLD; Flag 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;
@ -141,7 +137,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
Flags flag = Flags.PVP_OVERWORLD; Flag 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

@ -8,7 +8,6 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.block.Action; 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.lists.Flags; import us.tastybento.bskyblock.lists.Flags;
/** /**
@ -17,10 +16,6 @@ import us.tastybento.bskyblock.lists.Flags;
*/ */
public class PhysicalInteractionListener extends AbstractFlagListener { public class PhysicalInteractionListener extends AbstractFlagListener {
public PhysicalInteractionListener(BSkyBlock plugin) {
super(plugin);
}
/** /**
* Handle physical interaction with blocks * Handle physical interaction with blocks
* Crop trample, pressure plates, triggering redstone, tripwires * Crop trample, pressure plates, triggering redstone, tripwires

View File

@ -10,15 +10,10 @@ import org.bukkit.event.block.EntityBlockFormEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent; import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.lists.Flags; import us.tastybento.bskyblock.lists.Flags;
public class PlaceBlocksListener extends AbstractFlagListener { public class PlaceBlocksListener extends AbstractFlagListener {
public PlaceBlocksListener(BSkyBlock plugin) {
super(plugin);
}
/** /**
* Check blocks being placed in general * Check blocks being placed in general
* *

View File

@ -7,7 +7,6 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerPortalEvent; import org.bukkit.event.player.PlayerPortalEvent;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.lists.Flags; import us.tastybento.bskyblock.lists.Flags;
/** /**
@ -17,10 +16,6 @@ import us.tastybento.bskyblock.lists.Flags;
*/ */
public class PortalListener extends AbstractFlagListener { public class PortalListener extends AbstractFlagListener {
public PortalListener(BSkyBlock plugin) {
super(plugin);
}
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onPlayerPortal(PlayerPortalEvent e) { public void onPlayerPortal(PlayerPortalEvent e) {
checkIsland(e, e.getFrom(), Flags.PORTAL); checkIsland(e, e.getFrom(), Flags.PORTAL);

View File

@ -7,7 +7,6 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerShearEntityEvent; import org.bukkit.event.player.PlayerShearEntityEvent;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.lists.Flags; import us.tastybento.bskyblock.lists.Flags;
/** /**
@ -17,10 +16,6 @@ import us.tastybento.bskyblock.lists.Flags;
*/ */
public class ShearingListener extends AbstractFlagListener { public class ShearingListener extends AbstractFlagListener {
public ShearingListener(BSkyBlock plugin) {
super(plugin);
}
// Protect sheep // Protect sheep
@EventHandler(priority = EventPriority.LOW) @EventHandler(priority = EventPriority.LOW)
public void onShear(final PlayerShearEntityEvent e) { public void onShear(final PlayerShearEntityEvent e) {

View File

@ -8,7 +8,6 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.lists.Flags; import us.tastybento.bskyblock.lists.Flags;
/** /**
@ -18,10 +17,6 @@ import us.tastybento.bskyblock.lists.Flags;
*/ */
public class TeleportationListener extends AbstractFlagListener { public class TeleportationListener extends AbstractFlagListener {
public TeleportationListener(BSkyBlock plugin) {
super(plugin);
}
/** /**
* Ender pearl and chorus fruit teleport checks * Ender pearl and chorus fruit teleport checks
* *

View File

@ -1,51 +1,136 @@
package us.tastybento.bskyblock.lists; package us.tastybento.bskyblock.lists;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public enum Flags { import org.bukkit.Material;
ANVIL,
ARMOR_STAND, import us.tastybento.bskyblock.api.flags.Flag;
BEACON, import us.tastybento.bskyblock.api.flags.Flag.FlagType;
BED, import us.tastybento.bskyblock.api.flags.FlagBuilder;
BREAK_BLOCKS, import us.tastybento.bskyblock.listeners.flags.BlockInteractionListener;
BREEDING, import us.tastybento.bskyblock.listeners.flags.BreakBlocksListener;
BREWING, import us.tastybento.bskyblock.listeners.flags.BreedingListener;
BUCKET, import us.tastybento.bskyblock.listeners.flags.BucketListener;
CHEST, import us.tastybento.bskyblock.listeners.flags.EggListener;
CHORUS_FRUIT, import us.tastybento.bskyblock.listeners.flags.EntityInteractListener;
COLLECT_LAVA, import us.tastybento.bskyblock.listeners.flags.FireListener;
COLLECT_WATER, import us.tastybento.bskyblock.listeners.flags.HurtingListener;
CRAFTING, import us.tastybento.bskyblock.listeners.flags.InventoryListener;
CROP_TRAMPLE, import us.tastybento.bskyblock.listeners.flags.ItemDropPickUpListener;
DOOR, import us.tastybento.bskyblock.listeners.flags.LeashListener;
EGGS, import us.tastybento.bskyblock.listeners.flags.PVPListener;
ENCHANTING, import us.tastybento.bskyblock.listeners.flags.PhysicalInteractionListener;
ENDER_PEARL, import us.tastybento.bskyblock.listeners.flags.PlaceBlocksListener;
ENTER_EXIT_MESSAGES, import us.tastybento.bskyblock.listeners.flags.PortalListener;
FIRE, import us.tastybento.bskyblock.listeners.flags.ShearingListener;
FIRE_EXTINGUISH, import us.tastybento.bskyblock.listeners.flags.TeleportationListener;
FIRE_SPREAD,
FURNACE, public class Flags {
GATE,
HURT_MOBS, public static final Flag BREAK_BLOCKS = new FlagBuilder().id("BREAK_BLOCKS").icon(Material.STONE).listener(new BreakBlocksListener()).build();
HURT_MONSTERS, public static final Flag PLACE_BLOCKS = new FlagBuilder().id("PLACE_BLOCKS").icon(Material.DIRT).listener(new PlaceBlocksListener()).build();
ITEM_DROP,
ITEM_PICKUP, // Block interactions - all use BlockInteractionListener()
LEASH, public static final Flag ANVIL = new FlagBuilder().id("ANVIL").icon(Material.ANVIL).listener(new BlockInteractionListener()).build();
LEVER_BUTTON, public static final Flag BEACON = new FlagBuilder().id("BEACON").icon(Material.BEACON).build();
MILKING, public static final Flag BED = new FlagBuilder().id("BED").icon(Material.BED).build();
MOB_SPAWN, public static final Flag BREWING = new FlagBuilder().id("BREWING").icon(Material.BREWING_STAND_ITEM).build();
MONSTER_SPAWN, public static final Flag CHEST = new FlagBuilder().id("CHEST").icon(Material.CHEST).build();
MOUNT_INVENTORY, public static final Flag DOOR = new FlagBuilder().id("DOOR).allowedByDefault(true").icon(Material.WOODEN_DOOR).build();
MUSIC, public static final Flag CRAFTING = new FlagBuilder().id("CRAFTING).allowedByDefault(true").icon(Material.WORKBENCH).build();
PLACE_BLOCKS, public static final Flag ENCHANTING = new FlagBuilder().id("ENCHANTING).allowedByDefault(true").icon(Material.ENCHANTMENT_TABLE).build();
PORTAL, public static final Flag FURNACE = new FlagBuilder().id("FURNACE").icon(Material.FURNACE).build();
PRESSURE_PLATE, public static final Flag GATE = new FlagBuilder().id("GATE).allowedByDefault(true").icon(Material.FENCE_GATE).build();
PVP_END, public static final Flag MUSIC = new FlagBuilder().id("MUSIC").icon(Material.JUKEBOX).build();
PVP_NETHER, public static final Flag LEVER_BUTTON = new FlagBuilder().id("LEVER_BUTTON").icon(Material.LEVER).build();
PVP_OVERWORLD, public static final Flag REDSTONE = new FlagBuilder().id("REDSTONE").icon(Material.REDSTONE).build();
REDSTONE, public static final Flag SPAWN_EGGS = new FlagBuilder().id("SPAWN_EGGS").icon(Material.MONSTER_EGG).build();
RIDING,
SHEARING, // Entity interactions
SPAWN_EGGS, public static final Flag ARMOR_STAND = new FlagBuilder().id("ARMOR_STAND").icon(Material.ARMOR_STAND).listener(new EntityInteractListener()).build();
TRADING public static final Flag RIDING = new FlagBuilder().id("RIDING").icon(Material.GOLD_BARDING).build();
public static final Flag TRADING = new FlagBuilder().id("TRADING).allowedByDefault(true").icon(Material.EMERALD).build();
// Breeding
public static final Flag BREEDING = new FlagBuilder().id("BREEDING").icon(Material.CARROT).listener(new BreedingListener()).build();
// Buckets. All bucket use is covered by one listener
public static final Flag BUCKET = new FlagBuilder().id("BUCKET").icon(Material.BUCKET).listener(new BucketListener()).build();
public static final Flag COLLECT_LAVA = new FlagBuilder().id("COLLECT_LAVA").icon(Material.LAVA_BUCKET).build();
public static final Flag COLLECT_WATER = new FlagBuilder().id("COLLECT_WATER").icon(Material.WATER_BUCKET).build();
public static final Flag MILKING = new FlagBuilder().id("MILKING").icon(Material.MILK_BUCKET).build();
// Chorus Fruit and Enderpearls
public static final Flag CHORUS_FRUIT = new FlagBuilder().id("CHORUS_FRUIT").icon(Material.CHORUS_FRUIT).listener(new TeleportationListener()).build();
public static final Flag ENDER_PEARL = new FlagBuilder().id("ENDER_PEARL").icon(Material.ENDER_PEARL).build();
// Physical interactions
public static final Flag CROP_TRAMPLE = new FlagBuilder().id("CROP_TRAMPLE").icon(Material.WHEAT).listener(new PhysicalInteractionListener()).build();
public static final Flag PRESSURE_PLATE = new FlagBuilder().id("PRESSURE_PLATE").icon(Material.GOLD_PLATE).build();
// Egg throwing
public static final Flag EGGS = new FlagBuilder().id("EGGS").icon(Material.EGG).listener(new EggListener()).build();
/*
* 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
*/
public static final Flag FIRE = new FlagBuilder().id("FIRE").icon(Material.FLINT_AND_STEEL).listener(new FireListener()).build();
public static final Flag FIRE_EXTINGUISH = new FlagBuilder().id("FIRE_EXTINGUISH").icon(Material.POTION).build();
public static final Flag FIRE_SPREAD = new FlagBuilder().id("FIRE_SPREAD").icon(Material.FIREWORK_CHARGE).build();
// Inventories
public static final Flag MOUNT_INVENTORY = new FlagBuilder().id("MOUNT_INVENTORY").icon(Material.IRON_BARDING).listener(new InventoryListener()).build();
// Hurting things
public static final Flag HURT_MOBS = new FlagBuilder().id("HURT_MOBS").icon(Material.STONE_SWORD).listener(new HurtingListener()).build();
public static final Flag HURT_MONSTERS = new FlagBuilder().id("HURT_MONSTERS").icon(Material.WOOD_SWORD).build();
// Leashes
public static final Flag LEASH = new FlagBuilder().id("LEASH").icon(Material.LEASH).listener(new LeashListener()).build();
// Portal use protection
public static final Flag PORTAL = new FlagBuilder().id("PORTAL").icon(Material.OBSIDIAN).listener(new PortalListener()).build();
// Shearing
public static final Flag SHEARING = new FlagBuilder().id("SHEARING").icon(Material.SHEARS).listener(new ShearingListener()).build();
// Item pickup or drop
public static final Flag ITEM_DROP = new FlagBuilder().id("ITEM_DROP").icon(Material.DIRT).allowedByDefault(true).listener(new ItemDropPickUpListener()).build();
public static final Flag ITEM_PICKUP = new FlagBuilder().id("ITEM_PICKUP").icon(Material.DIRT).build();
/*
* Settings flags (not protection flags)
*/
// PVP
public static final Flag PVP_OVERWORLD = new FlagBuilder().id("PVP_OVERWORLD").icon(Material.ARROW).type(FlagType.SETTING).listener(new PVPListener()).build();
public static final Flag PVP_NETHER = new FlagBuilder().id("PVP_NETHER").icon(Material.IRON_AXE).type(FlagType.SETTING).build();
public static final Flag PVP_END = new FlagBuilder().id("PVP_END").icon(Material.END_CRYSTAL).type(FlagType.SETTING).build();
// Others
public static final Flag ENTER_EXIT_MESSAGES = new FlagBuilder().id("ENTER_EXIT_MESSAGES").icon(Material.DIRT).allowedByDefault(true).type(FlagType.SETTING).build();
public static final Flag MOB_SPAWN = new FlagBuilder().id("MOB_SPAWN").icon(Material.APPLE).allowedByDefault(true).type(FlagType.SETTING).build();
public static final Flag MONSTER_SPAWN = new FlagBuilder().id("MONSTER_SPAWN").icon(Material.MOB_SPAWNER).allowedByDefault(true).type(FlagType.SETTING).build();
/**
* @return List of all the flags in this class
*/
public static List<Flag> values() {
return Arrays.asList(Flags.class.getFields()).stream().map(field -> {
try {
return (Flag)field.get(null);
} catch (IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
return null;
}).collect(Collectors.toList());
}
} }

View File

@ -3,45 +3,31 @@ package us.tastybento.bskyblock.managers;
import java.util.HashMap; import java.util.HashMap;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material;
import us.tastybento.bskyblock.BSkyBlock; import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.flags.Flag; import us.tastybento.bskyblock.api.flags.Flag;
import us.tastybento.bskyblock.api.flags.Flag.FlagType;
import us.tastybento.bskyblock.api.flags.FlagBuilder;
import us.tastybento.bskyblock.api.panels.PanelItem; import us.tastybento.bskyblock.api.panels.PanelItem;
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;
import us.tastybento.bskyblock.lists.Flags; import us.tastybento.bskyblock.lists.Flags;
public class FlagsManager { public class FlagsManager {
private BSkyBlock plugin; private BSkyBlock plugin;
private HashMap<Flags, Flag> flags = new HashMap<>(); private HashMap<String, Flag> flags = new HashMap<>();
public FlagsManager(BSkyBlock plugin) { public FlagsManager(BSkyBlock plugin) {
this.plugin = plugin; this.plugin = plugin;
// Register flags // Register default flags
registerFlags(); for (Flag flag : Flags.values()) {
registerFlag(flag);
}
} }
/**
* Register a new flag with BSkyBlock
* @param flag
*/
public void registerFlag(Flag flag) { public void registerFlag(Flag flag) {
//Bukkit.getLogger().info("DEBUG: registering flag " + flag.getID()); //Bukkit.getLogger().info("DEBUG: registering flag " + flag.getID());
flags.put(flag.getID(), flag); flags.put(flag.getID(), flag);
@ -49,11 +35,16 @@ public class FlagsManager {
flag.getListener().ifPresent(l -> Bukkit.getServer().getPluginManager().registerEvents(l, plugin)); flag.getListener().ifPresent(l -> Bukkit.getServer().getPluginManager().registerEvents(l, plugin));
} }
public HashMap<Flags, Flag> getFlags() { public HashMap<String, Flag> getFlags() {
return flags; return flags;
} }
public Flag getFlagByID(Flags id) { /**
* Get flag by string
* @param key - string name same as the enum
* @return Flag or null if not known
*/
public Flag getFlagByID(String id) {
//Bukkit.getLogger().info("DEBUG: requesting " + id + " flags size = " + flags.size()); //Bukkit.getLogger().info("DEBUG: requesting " + id + " flags size = " + flags.size());
return flags.get(id); return flags.get(id);
} }
@ -64,117 +55,5 @@ public class FlagsManager {
} }
return null; return null;
} }
private void registerFlags() {
// Break and place blocks
registerFlag(new FlagBuilder().id(Flags.BREAK_BLOCKS).icon(Material.STONE).listener(new BreakBlocksListener(plugin)).build());
registerFlag(new FlagBuilder().id(Flags.PLACE_BLOCKS).icon(Material.DIRT).listener(new PlaceBlocksListener(plugin)).build());
// Block interactions - all use BlockInteractionListener()
registerFlag(new FlagBuilder().id(Flags.ANVIL).icon(Material.ANVIL).listener(new BlockInteractionListener(plugin)).build());
registerFlag(new FlagBuilder().id(Flags.BEACON).icon(Material.BEACON).build());
registerFlag(new FlagBuilder().id(Flags.BED).icon(Material.BED).build());
registerFlag(new FlagBuilder().id(Flags.BREWING).icon(Material.BREWING_STAND_ITEM).build());
registerFlag(new FlagBuilder().id(Flags.CHEST).icon(Material.CHEST).build());
registerFlag(new FlagBuilder().id(Flags.DOOR).allowedByDefault(true).icon(Material.WOODEN_DOOR).build());
registerFlag(new FlagBuilder().id(Flags.CRAFTING).allowedByDefault(true).icon(Material.WORKBENCH).build());
registerFlag(new FlagBuilder().id(Flags.ENCHANTING).allowedByDefault(true).icon(Material.ENCHANTMENT_TABLE).build());
registerFlag(new FlagBuilder().id(Flags.FURNACE).icon(Material.FURNACE).build());
registerFlag(new FlagBuilder().id(Flags.GATE).allowedByDefault(true).icon(Material.FENCE_GATE).build());
registerFlag(new FlagBuilder().id(Flags.MUSIC).icon(Material.JUKEBOX).build());
registerFlag(new FlagBuilder().id(Flags.LEVER_BUTTON).icon(Material.LEVER).build());
registerFlag(new FlagBuilder().id(Flags.REDSTONE).icon(Material.REDSTONE).build());
registerFlag(new FlagBuilder().id(Flags.SPAWN_EGGS).icon(Material.MONSTER_EGG).build());
// Entity interactions
registerFlag(new FlagBuilder().id(Flags.ARMOR_STAND).icon(Material.ARMOR_STAND).listener(new EntityInteractListener(plugin)).build());
registerFlag(new FlagBuilder().id(Flags.RIDING).icon(Material.GOLD_BARDING).build());
registerFlag(new FlagBuilder().id(Flags.TRADING).allowedByDefault(true).icon(Material.EMERALD).build());
// Breeding
registerFlag(new FlagBuilder().id(Flags.BREEDING).icon(Material.CARROT).listener(new BreedingListener(plugin)).build());
// Buckets. All bucket use is covered by one listener
registerFlag(new FlagBuilder().id(Flags.BUCKET).icon(Material.BUCKET).listener(new BucketListener(plugin)).build());
registerFlag(new FlagBuilder().id(Flags.COLLECT_LAVA).icon(Material.LAVA_BUCKET).build());
registerFlag(new FlagBuilder().id(Flags.COLLECT_WATER).icon(Material.WATER_BUCKET).build());
registerFlag(new FlagBuilder().id(Flags.MILKING).icon(Material.MILK_BUCKET).build());
// Chorus Fruit and Enderpearls
registerFlag(new FlagBuilder().id(Flags.CHORUS_FRUIT).icon(Material.CHORUS_FRUIT).listener(new TeleportationListener(plugin)).build());
registerFlag(new FlagBuilder().id(Flags.ENDER_PEARL).icon(Material.ENDER_PEARL).build());
// Physical interactions
registerFlag(new FlagBuilder().id(Flags.CROP_TRAMPLE).icon(Material.WHEAT).listener(new PhysicalInteractionListener(plugin)).build());
registerFlag(new FlagBuilder().id(Flags.PRESSURE_PLATE).icon(Material.GOLD_PLATE).build());
// Egg throwing
registerFlag(new FlagBuilder().id(Flags.EGGS).icon(Material.EGG).listener(new EggListener(plugin)).build());
/*
* 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
*/
registerFlag(new FlagBuilder().id(Flags.FIRE).icon(Material.FLINT_AND_STEEL).listener(new FireListener(plugin)).build());
registerFlag(new FlagBuilder().id(Flags.FIRE_EXTINGUISH).icon(Material.POTION).build());
registerFlag(new FlagBuilder().id(Flags.FIRE_SPREAD).icon(Material.FIREWORK_CHARGE).build());
// Inventories
registerFlag(new FlagBuilder().id(Flags.MOUNT_INVENTORY).icon(Material.IRON_BARDING).listener(new InventoryListener(plugin)).build());
// Hurting things
registerFlag(new FlagBuilder().id(Flags.HURT_MOBS).icon(Material.STONE_SWORD).listener(new HurtingListener(plugin)).build());
registerFlag(new FlagBuilder().id(Flags.HURT_MONSTERS).icon(Material.WOOD_SWORD).build());
// Leashes
registerFlag(new FlagBuilder().id(Flags.LEASH).icon(Material.LEASH).listener(new LeashListener(plugin)).build());
// Portal use protection
registerFlag(new FlagBuilder().id(Flags.PORTAL).icon(Material.OBSIDIAN).listener(new PortalListener(plugin)).build());
// Shearing
registerFlag(new FlagBuilder().id(Flags.SHEARING).icon(Material.SHEARS).listener(new ShearingListener(plugin)).build());
// Item pickup or drop
registerFlag(new FlagBuilder().id(Flags.ITEM_DROP).icon(Material.DIRT).allowedByDefault(true).listener(new ItemDropPickUpListener(plugin)).build());
registerFlag(new FlagBuilder().id(Flags.ITEM_PICKUP).icon(Material.DIRT).build());
/*
* Settings flags (not protection flags)
*/
// PVP
registerFlag(new FlagBuilder().id(Flags.PVP_OVERWORLD).icon(Material.ARROW).type(FlagType.SETTING).listener(new PVPListener(plugin)).build());
registerFlag(new FlagBuilder().id(Flags.PVP_NETHER).icon(Material.IRON_AXE).type(FlagType.SETTING).build());
registerFlag(new FlagBuilder().id(Flags.PVP_END).icon(Material.END_CRYSTAL).type(FlagType.SETTING).build());
// Others
registerFlag(new FlagBuilder().id(Flags.ENTER_EXIT_MESSAGES).icon(Material.DIRT).allowedByDefault(true).type(FlagType.SETTING).build());
registerFlag(new FlagBuilder().id(Flags.MOB_SPAWN).icon(Material.APPLE).allowedByDefault(true).type(FlagType.SETTING).build());
registerFlag(new FlagBuilder().id(Flags.MONSTER_SPAWN).icon(Material.MOB_SPAWNER).allowedByDefault(true).type(FlagType.SETTING).build());
}
/**
* Get flag by string
* @param key - string name same as the enum
* @return Flag or null if not known
*/
public Flag getFlagByID(String key) {
for (Flags flag: Flags.values()) {
if (flag.name().equalsIgnoreCase(key)) return this.getFlagByID(flag);
}
return null;
}
public boolean isDefaultAllowed(Flags flag) {
if (flags.containsKey(flag)) return flags.get(flag).isDefaultSetting();
return false;
}
} }

View File

@ -10,6 +10,7 @@ import static org.mockito.Mockito.mock;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
@ -22,6 +23,7 @@ import java.util.logging.Logger;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
@ -48,6 +50,8 @@ import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.commands.User; import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.api.events.IslandBaseEvent; import us.tastybento.bskyblock.api.events.IslandBaseEvent;
import us.tastybento.bskyblock.api.events.team.TeamEvent; import us.tastybento.bskyblock.api.events.team.TeamEvent;
import us.tastybento.bskyblock.api.flags.Flag;
import us.tastybento.bskyblock.api.flags.FlagBuilder;
import us.tastybento.bskyblock.database.managers.island.IslandsManager; import us.tastybento.bskyblock.database.managers.island.IslandsManager;
import us.tastybento.bskyblock.database.objects.Island; import us.tastybento.bskyblock.database.objects.Island;
import us.tastybento.bskyblock.generators.IslandWorld; import us.tastybento.bskyblock.generators.IslandWorld;
@ -462,48 +466,7 @@ public class TestBSkyBlock {
@Test @Test
public void TestEventProtection() { public void TestEventProtection() {
// Test events
/*
*
* Score approach:
*
* Rank definitions are global and apply to all islands
*
* There are 4 hard-coded ranks:
*
* Owner is the highest rank = 1000
*
* Member ranks are >= 900
*
* Visitors = 0
*
* Banned = -1
*
* Owners have full admin capability over the island. Members are required to give up their own island to be a member.
* Visitors are everyone else.
*
* After those 3, it's possible to have custom ranks, e.g.
*
* Trustees = 750
* Coops = 500
* etc.
*
*
* Each flag has a bypass score.
* If the user's rank is higher or equal to the bypass score, they will bypass the protection.
* Owners can disable/enable the flags.
*
* Each island will track the rank score for each player on the island.
* Unknown players have a rank of 0.
*
*
* Admins will be able to define groups and their rank value.
* During the game, the players will never see the rank value. They will only see the ranks.
*
* It will be possible to island owners to promote or demote players up and down the ranks.
*/
// Now test events
FlagListener fl = new FlagListener(plugin); FlagListener fl = new FlagListener(plugin);
Bukkit.getLogger().info("SETUP: owner UUID = " + ownerOfIsland.getUniqueId()); Bukkit.getLogger().info("SETUP: owner UUID = " + ownerOfIsland.getUniqueId());
@ -525,11 +488,42 @@ public class TestBSkyBlock {
} }
private class FlagListener extends AbstractFlagListener { @Test
public void TestDefaultFlags() {
// Check all the default flags
FlagsManager fm = new FlagsManager(plugin);
Collection<Flag> defaultFlags = Flags.values();
Collection<Flag> f = fm.getFlags().values();
for (Flag flag : defaultFlags) {
assertTrue(flag.getID(), f.contains(flag));
}
for (Flag flag : f) {
assertTrue(flag.getID(), defaultFlags.contains(flag));
}
}
public FlagListener(BSkyBlock plugin) { @Test
super(plugin); public void TestCustomFlags() {
// Custom
FlagListener fl = new FlagListener(plugin);
Flag customFlag = new FlagBuilder().id("CUSTOM_FLAG").icon(Material.DIAMOND).listener(fl).build();
assertEquals("CUSTOM_FLAG", customFlag.getID());
assertEquals(Material.DIAMOND, customFlag.getIcon().getItem().getType());
assertEquals(fl, customFlag.getListener().get());
// Add it to the Flag Manager
flagsManager.registerFlag(customFlag);
assertEquals(customFlag, flagsManager.getFlagByID("CUSTOM_FLAG"));
assertEquals(customFlag, flagsManager.getFlagByIcon(customFlag.getIcon()));
}
/**
* Dummy flag listener
*
*/
private class FlagListener extends AbstractFlagListener {
FlagListener(BSkyBlock plugin) {
// Set the plugin explicitly
setPlugin(plugin);
} }
} }