From 83d08484295223f068194c44e27f51364d885a88 Mon Sep 17 00:00:00 2001 From: Tastybento Date: Sat, 3 Feb 2018 20:42:12 -0800 Subject: [PATCH] Fixed bugs with protection. Added serializer adapter for the Flags hashmap in Island. Teams don't work. Need to work out why. PVP doesn't work correctly. It allows members to hit visitors anytime, but visitors can only hit others if PVP is off. This isn't how it is supposed to work! --- .../us/tastybento/bskyblock/BSkyBlock.java | 8 ++-- .../bskyblock/api/configuration/Adapter.java | 7 +++ .../api/configuration/ConfigEntry.java | 2 +- .../database/objects/FlagSerializer.java | 46 +++++++++++++++++++ .../bskyblock/database/objects/Island.java | 6 ++- .../listeners/flags/AbstractFlagListener.java | 11 ++--- .../listeners/flags/InventoryListener.java | 37 +++++++-------- .../flags/ItemDropPickUpListener.java | 4 +- .../listeners/flags/PVPListener.java | 1 + src/test/java/bskyblock/TestBSkyBlock.java | 1 - 10 files changed, 91 insertions(+), 32 deletions(-) create mode 100644 src/main/java/us/tastybento/bskyblock/database/objects/FlagSerializer.java diff --git a/src/main/java/us/tastybento/bskyblock/BSkyBlock.java b/src/main/java/us/tastybento/bskyblock/BSkyBlock.java index b17943fca..b2ed664d9 100755 --- a/src/main/java/us/tastybento/bskyblock/BSkyBlock.java +++ b/src/main/java/us/tastybento/bskyblock/BSkyBlock.java @@ -89,6 +89,11 @@ public class BSkyBlock extends JavaPlugin { islandWorldManager = new IslandWorld(plugin); getServer().getScheduler().runTask(plugin, () -> { + + // Load Flags + flagsManager = new FlagsManager(plugin); + new Flags(plugin); + // Load islands from database islandsManager.load(); @@ -98,9 +103,6 @@ public class BSkyBlock extends JavaPlugin { // Register Listeners registerListeners(); - // Load Flags - flagsManager = new FlagsManager(plugin); - new Flags(plugin); // Load addons addonsManager = new AddonsManager(plugin); diff --git a/src/main/java/us/tastybento/bskyblock/api/configuration/Adapter.java b/src/main/java/us/tastybento/bskyblock/api/configuration/Adapter.java index f7016896b..fd2d14c6b 100644 --- a/src/main/java/us/tastybento/bskyblock/api/configuration/Adapter.java +++ b/src/main/java/us/tastybento/bskyblock/api/configuration/Adapter.java @@ -1,6 +1,13 @@ package us.tastybento.bskyblock.api.configuration; +/** + * Convert from to S or to V + * @author tastybento + * + * @param + * @param + */ public interface Adapter { /** diff --git a/src/main/java/us/tastybento/bskyblock/api/configuration/ConfigEntry.java b/src/main/java/us/tastybento/bskyblock/api/configuration/ConfigEntry.java index cf138bc84..38137d22a 100644 --- a/src/main/java/us/tastybento/bskyblock/api/configuration/ConfigEntry.java +++ b/src/main/java/us/tastybento/bskyblock/api/configuration/ConfigEntry.java @@ -16,7 +16,7 @@ import us.tastybento.bskyblock.Constants.GameType; @Target(ElementType.FIELD) public @interface ConfigEntry { - String path(); + String path() default ""; String since() default "1.0"; boolean overrideOnChange() default false; boolean experimental() default false; diff --git a/src/main/java/us/tastybento/bskyblock/database/objects/FlagSerializer.java b/src/main/java/us/tastybento/bskyblock/database/objects/FlagSerializer.java new file mode 100644 index 000000000..bbd692dad --- /dev/null +++ b/src/main/java/us/tastybento/bskyblock/database/objects/FlagSerializer.java @@ -0,0 +1,46 @@ +package us.tastybento.bskyblock.database.objects; + +import java.util.HashMap; +import java.util.Map.Entry; + +import org.bukkit.Bukkit; +import org.bukkit.configuration.MemorySection; + +import us.tastybento.bskyblock.BSkyBlock; +import us.tastybento.bskyblock.api.configuration.Adapter; +import us.tastybento.bskyblock.api.flags.Flag; + +/** + * Serializes the {@link us.tastybento.bskyblock.database.objects.Island#getFlags() getFlags()} and + * {@link us.tastybento.bskyblock.database.objects.Island#setFlags() setFlags()} + * in {@link us.tastybento.bskyblock.database.objects.Island} + * @author tastybento + * + */ +public class FlagSerializer implements Adapter, HashMap> { + + @Override + public HashMap convertFrom(Object from) { + HashMap result = new HashMap<>(); + MemorySection section = (MemorySection) from; + for (String key : section.getKeys(false)) { + Bukkit.getLogger().info("DEBUG: " + key + " = " + section.getInt(key)); + + result.put(BSkyBlock.getInstance().getFlagsManager().getFlagByID(key), section.getInt(key)); + } + return result; + } + + @Override + public HashMap convertTo(Object to) { + HashMap result = new HashMap<>(); + HashMap flags = (HashMap)to; + for (Entry en: flags.entrySet()) { + result.put(en.getKey().getID(), en.getValue()); + } + return result; + } + + + +} diff --git a/src/main/java/us/tastybento/bskyblock/database/objects/Island.java b/src/main/java/us/tastybento/bskyblock/database/objects/Island.java index 9d9506315..6ac43d5bf 100755 --- a/src/main/java/us/tastybento/bskyblock/database/objects/Island.java +++ b/src/main/java/us/tastybento/bskyblock/database/objects/Island.java @@ -16,6 +16,7 @@ import org.bukkit.entity.Entity; import us.tastybento.bskyblock.BSkyBlock; import us.tastybento.bskyblock.api.commands.User; +import us.tastybento.bskyblock.api.configuration.ConfigEntry; import us.tastybento.bskyblock.api.events.IslandBaseEvent; import us.tastybento.bskyblock.api.events.island.IslandEvent; import us.tastybento.bskyblock.api.events.island.IslandEvent.IslandLockEvent; @@ -77,8 +78,11 @@ public class Island implements DataObject { private boolean spawn = false; private boolean purgeProtected = false; - //// Protection //// + + //// Protection flags //// + @ConfigEntry(adapter = FlagSerializer.class) private HashMap flags = new HashMap<>(); + private int levelHandicap; private Location spawnPoint; diff --git a/src/main/java/us/tastybento/bskyblock/listeners/flags/AbstractFlagListener.java b/src/main/java/us/tastybento/bskyblock/listeners/flags/AbstractFlagListener.java index d9e6ff20f..7e133fb9d 100644 --- a/src/main/java/us/tastybento/bskyblock/listeners/flags/AbstractFlagListener.java +++ b/src/main/java/us/tastybento/bskyblock/listeners/flags/AbstractFlagListener.java @@ -43,14 +43,13 @@ public abstract class AbstractFlagListener implements Listener { private boolean createEventUser(Event e) { try { // Use reflection to get the getPlayer method if it exists + Method getPlayer = e.getClass().getMethod("getPlayer"); if (getPlayer != null) { setUser(User.getInstance((Player)getPlayer.invoke(e))); return true; } - } catch (Exception e1) { - e1.printStackTrace(); - } + } catch (Exception e1) { e1.printStackTrace();} return false; } @@ -76,7 +75,7 @@ public abstract class AbstractFlagListener implements Listener { } /** - * Cancels the event and sends the island public message to user unless silent is true + * Cancels the event and sends the island protected message to user unless silent is true * @param e Event * @param silent - if true, message is not sent */ @@ -85,7 +84,7 @@ public abstract class AbstractFlagListener implements Listener { ((Cancellable)e).setCancelled(true); if (user != null) { if (!silent) - user.sendMessage("protection.public"); + user.sendMessage("protection.protected"); user.updateInventory(); } } @@ -147,7 +146,7 @@ public abstract class AbstractFlagListener implements Listener { if (!createEventUser(e)) { // The user is not set, and the event does not hold a getPlayer, so return false // TODO: is this the correct handling here? - Bukkit.getLogger().severe("Check island had no associated user!"); + Bukkit.getLogger().severe("Check island had no associated user! " + e.getEventName()); return false; } } diff --git a/src/main/java/us/tastybento/bskyblock/listeners/flags/InventoryListener.java b/src/main/java/us/tastybento/bskyblock/listeners/flags/InventoryListener.java index a245693ae..c351d7d45 100644 --- a/src/main/java/us/tastybento/bskyblock/listeners/flags/InventoryListener.java +++ b/src/main/java/us/tastybento/bskyblock/listeners/flags/InventoryListener.java @@ -17,6 +17,7 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.inventory.InventoryClickEvent; import us.tastybento.bskyblock.BSkyBlock; +import us.tastybento.bskyblock.api.commands.User; import us.tastybento.bskyblock.lists.Flags; /** @@ -32,31 +33,31 @@ public class InventoryListener extends AbstractFlagListener { /** * Prevents visitors picking items from inventories - * @param event + * @param e */ @EventHandler(priority = EventPriority.LOW, ignoreCancelled=true) - public void onMountInventoryClick(InventoryClickEvent event) { - if (event.getInventory().getHolder() == null) { + public void onMountInventoryClick(InventoryClickEvent e) { + if (e.getInventory().getHolder() == null) { return; + } + if (e.getInventory().getHolder() instanceof Animals) { + checkIsland(e, e.getInventory().getLocation(), Flags.MOUNT_INVENTORY); } - if (event.getInventory().getHolder() instanceof Animals) { - checkIsland(event, event.getInventory().getLocation(), Flags.MOUNT_INVENTORY); + else if (e.getInventory().getHolder() instanceof Chest + || e.getInventory().getHolder() instanceof Dispenser + || e.getInventory().getHolder() instanceof Hopper + || e.getInventory().getHolder() instanceof Dropper + || e.getInventory().getHolder() instanceof ShulkerBox) { + setUser(User.getInstance(e.getWhoClicked())).checkIsland(e, e.getInventory().getLocation(), Flags.CHEST); } - else if (event.getInventory().getHolder() instanceof Chest - || event.getInventory().getHolder() instanceof Dispenser - || event.getInventory().getHolder() instanceof Hopper - || event.getInventory().getHolder() instanceof Dropper - || event.getInventory().getHolder() instanceof ShulkerBox) { - checkIsland(event, event.getInventory().getLocation(), Flags.CHEST); + else if (e.getInventory().getHolder() instanceof Furnace) { + setUser(User.getInstance(e.getWhoClicked())).checkIsland(e, e.getInventory().getLocation(), Flags.FURNACE); } - else if (event.getInventory().getHolder() instanceof Furnace) { - checkIsland(event, event.getInventory().getLocation(), Flags.FURNACE); + else if (e.getInventory().getHolder() instanceof BrewingStand) { + setUser(User.getInstance(e.getWhoClicked())).checkIsland(e, e.getInventory().getLocation(), Flags.BREWING); } - else if (event.getInventory().getHolder() instanceof BrewingStand) { - checkIsland(event, event.getInventory().getLocation(), Flags.BREWING); - } - else if (event.getInventory().getHolder() instanceof Beacon) { - checkIsland(event, event.getInventory().getLocation(), Flags.BEACON); + else if (e.getInventory().getHolder() instanceof Beacon) { + setUser(User.getInstance(e.getWhoClicked())).checkIsland(e, e.getInventory().getLocation(), Flags.BEACON); } } diff --git a/src/main/java/us/tastybento/bskyblock/listeners/flags/ItemDropPickUpListener.java b/src/main/java/us/tastybento/bskyblock/listeners/flags/ItemDropPickUpListener.java index 41c334068..c77dff2ac 100644 --- a/src/main/java/us/tastybento/bskyblock/listeners/flags/ItemDropPickUpListener.java +++ b/src/main/java/us/tastybento/bskyblock/listeners/flags/ItemDropPickUpListener.java @@ -35,10 +35,10 @@ public class ItemDropPickUpListener extends AbstractFlagListener { * Handle item pickup by visitors */ @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) - public void onVisitorDrop(EntityPickupItemEvent e) { + public void onVisitorPickup(EntityPickupItemEvent e) { if (e.getEntity() instanceof Player) { // Disallow, but don't tell the player an error - setUser(User.getInstance(e.getEntity())).checkIsland(e, e.getItem().getLocation(), Flags.ITEM_PICKUP, false); + setUser(User.getInstance(e.getEntity())).checkIsland(e, e.getItem().getLocation(), Flags.ITEM_PICKUP, true); } } } diff --git a/src/main/java/us/tastybento/bskyblock/listeners/flags/PVPListener.java b/src/main/java/us/tastybento/bskyblock/listeners/flags/PVPListener.java index e92019d10..2ebb8951a 100644 --- a/src/main/java/us/tastybento/bskyblock/listeners/flags/PVPListener.java +++ b/src/main/java/us/tastybento/bskyblock/listeners/flags/PVPListener.java @@ -26,6 +26,7 @@ import us.tastybento.bskyblock.api.flags.Flag; import us.tastybento.bskyblock.lists.Flags; /** + * TODO: PVP is different to other flags - it's either allowed for everyone or not allowed for everyone. Currently owners can hit visitors. * Handles PVP * @author tastybento * diff --git a/src/test/java/bskyblock/TestBSkyBlock.java b/src/test/java/bskyblock/TestBSkyBlock.java index c48b9ee71..03b024aef 100644 --- a/src/test/java/bskyblock/TestBSkyBlock.java +++ b/src/test/java/bskyblock/TestBSkyBlock.java @@ -512,7 +512,6 @@ public class TestBSkyBlock { Event e = new BlockBreakEvent(block, ownerOfIsland); Assert.assertTrue(fl.checkIsland(e, location, Flags.BREAK_BLOCKS, true)); - // Set up an event with a random player Bukkit.getLogger().info("DEBUG: checking events - member"); Event e2 = new BlockBreakEvent(block, player);