diff --git a/src/main/java/us/tastybento/bskyblock/api/flags/Flag.java b/src/main/java/us/tastybento/bskyblock/api/flags/Flag.java index 3f5564043..184bb693d 100644 --- a/src/main/java/us/tastybento/bskyblock/api/flags/Flag.java +++ b/src/main/java/us/tastybento/bskyblock/api/flags/Flag.java @@ -13,11 +13,15 @@ public class Flag { private PanelItem icon; private Listener listener; private boolean defaultSetting; + + public Flag() {} public Flag(BSkyBlock plugin, String id, PanelItem icon, Listener listener, boolean defaultSetting) { this.id = id; this.icon = icon; this.listener = listener; + //System.out.println("DEBUG: " + plugin); + //System.out.println("DEBUG: " + plugin.getFlagsManager()); plugin.getFlagsManager().registerFlag(this); } diff --git a/src/main/java/us/tastybento/bskyblock/database/objects/Island.java b/src/main/java/us/tastybento/bskyblock/database/objects/Island.java index ce5abc8cf..171903864 100755 --- a/src/main/java/us/tastybento/bskyblock/database/objects/Island.java +++ b/src/main/java/us/tastybento/bskyblock/database/objects/Island.java @@ -151,7 +151,7 @@ public class Island implements DataObject { * @param flag * @return flag rank. Players must have at least this rank to bypass this flag */ - public int getFlag(Flag flag){ + public int getFlagReq(Flag flag){ if(flags.containsKey(flag)) { return flags.get(flag); } else { @@ -411,7 +411,7 @@ public class Island implements DataObject { * @return true if allowed, false if not */ public boolean isAllowed(Flag flag) { - return this.getFlag(flag) >= 0 ? true : false; + return this.getFlagReq(flag) >= 0 ? true : false; } /** @@ -421,7 +421,8 @@ public class Island implements DataObject { * @return true if allowed, false if not */ public boolean isAllowed(User user, Flag flag) { - return (this.getRank(user) >= this.getFlag(flag)) ? true : false; + Bukkit.getLogger().info("DEBUG: " + flag.getID() + " user score = " + getRank(user) + " flag req = "+ this.getFlagReq(flag)); + return (this.getRank(user) >= this.getFlagReq(flag)) ? true : false; } /** diff --git a/src/main/java/us/tastybento/bskyblock/lists/Flags.java b/src/main/java/us/tastybento/bskyblock/lists/Flags.java index 5f0121128..7ee39a30f 100644 --- a/src/main/java/us/tastybento/bskyblock/lists/Flags.java +++ b/src/main/java/us/tastybento/bskyblock/lists/Flags.java @@ -29,101 +29,148 @@ import us.tastybento.bskyblock.listeners.flags.TeleportationListener; * */ public class Flags { - private static BSkyBlock p; - + public static Flag ANVIL; + public static Flag ARMOR_STAND; + public static Flag BEACON; + public static Flag BED; + public static Flag BREAK_BLOCKS; + public static Flag BREEDING; + public static Flag BREWING; + public static Flag BUCKET; + public static Flag CHEST; + public static Flag CHORUS_FRUIT; + public static Flag COLLECT_LAVA; + public static Flag COLLECT_WATER; + public static Flag CRAFTING; + public static Flag CROP_TRAMPLE; + public static Flag DOOR; + public static Flag EGGS; + public static Flag ENCHANTING; + public static Flag ENDER_PEARL; + public static Flag ENTER_EXIT_MESSAGES; + public static Flag FIRE; + public static Flag FIRE_EXTINGUISH; + public static Flag FIRE_SPREAD; + public static Flag FURNACE; + public static Flag GATE; + public static Flag HURT_MOBS; + public static Flag HURT_MONSTERS; + public static Flag ITEM_DROP; + public static Flag ITEM_PICKUP; + public static Flag LEASH; + public static Flag LEVER_BUTTON; + public static Flag MILKING; + public static Flag MOB_SPAWN; + public static Flag MONSTER_SPAWN; + public static Flag MOUNT_INVENTORY; + public static Flag MUSIC; + public static Flag PLACE_BLOCKS; + public static Flag PORTAL; + public static Flag PRESSURE_PLATE; + public static Flag PVP_END; + public static Flag PVP_NETHER; + public static Flag PVP_OVERWORLD; + public static Flag REDSTONE; + public static Flag RIDING; + public static Flag SHEARING; + public static Flag SPAWN_EGGS; + public static Flag TRADING; + + private BSkyBlock p; + public Flags(BSkyBlock plugin) { p = plugin; + + // Break and place blocks + BREAK_BLOCKS = new FlagBuilder().id("BREAK_BLOCKS").icon(Material.STONE).listener(new BreakBlocksListener(p)).build(p); + PLACE_BLOCKS = new FlagBuilder().id("PLACE_BLOCKS").icon(Material.DIRT).listener(new PlaceBlocksListener(p)).build(p); + + // Block interactions - all use BlockInteractionListener() + ANVIL = new FlagBuilder().id("ANVIL").icon(Material.ANVIL).listener(new BlockInteractionListener(p)).build(p); + BEACON = new FlagBuilder().id("BEACON").icon(Material.BEACON).build(p); + BED = new FlagBuilder().id("BED").icon(Material.BED).build(p); + BREWING = new FlagBuilder().id("BREWING").icon(Material.BREWING_STAND_ITEM).build(p); + CHEST = new FlagBuilder().id("CHEST").icon(Material.CHEST).build(p); + DOOR = new FlagBuilder().id("DOOR").allowedByDefault(true).icon(Material.WOODEN_DOOR).build(p); + CRAFTING = new FlagBuilder().id("CRAFTING").allowedByDefault(true).icon(Material.WORKBENCH).build(p); + ENCHANTING = new FlagBuilder().id("ENCHANTING").allowedByDefault(true).icon(Material.ENCHANTMENT_TABLE).build(p); + FURNACE = new FlagBuilder().id("FURNACE").icon(Material.FURNACE).build(p); + GATE = new FlagBuilder().id("GATE").allowedByDefault(true).icon(Material.FENCE_GATE).build(p); + MUSIC = new FlagBuilder().id("MUSIC").icon(Material.JUKEBOX).build(p); + LEVER_BUTTON = new FlagBuilder().id("LEVER_BUTTON").icon(Material.LEVER).build(p); + REDSTONE = new FlagBuilder().id("REDSTONE").icon(Material.REDSTONE).build(p); + SPAWN_EGGS = new FlagBuilder().id("SPAWN_EGGS").icon(Material.MONSTER_EGG).build(p); + + // Entity interactions + ARMOR_STAND = new FlagBuilder().id("ARMOR_STAND").icon(Material.ARMOR_STAND).listener(new EntityInteractListener(p)).build(p); + RIDING = new FlagBuilder().id("RIDING").icon(Material.GOLD_BARDING).build(p); + TRADING = new FlagBuilder().id("TRADING").allowedByDefault(true).icon(Material.EMERALD).build(p); + + // Breeding + BREEDING = new FlagBuilder().id("BREEDING").icon(Material.CARROT).listener(new BreedingListener(p)).build(p); + + // Buckets. All bucket use is covered by one listener + BUCKET = new FlagBuilder().id("BUCKET").icon(Material.BUCKET).listener(new BucketListener(p)).build(p); + COLLECT_LAVA = new FlagBuilder().id("COLLECT_LAVA").icon(Material.LAVA_BUCKET).build(p); + COLLECT_WATER = new FlagBuilder().id("COLLECT_WATER").icon(Material.WATER_BUCKET).build(p); + MILKING = new FlagBuilder().id("MILKING").icon(Material.MILK_BUCKET).build(p); + + // Chorus Fruit and Enderpearls + CHORUS_FRUIT = new FlagBuilder().id("CHORUS_FRUIT").icon(Material.CHORUS_FRUIT).listener(new TeleportationListener(p)).build(p); + ENDER_PEARL = new FlagBuilder().id("ENDER_PEARL").icon(Material.ENDER_PEARL).build(p); + + // Physical interactions + CROP_TRAMPLE = new FlagBuilder().id("CROP_TRAMPLE").icon(Material.WHEAT).listener(new PhysicalInteractionListener(p)).build(p); + PRESSURE_PLATE = new FlagBuilder().id("PRESSURE_PLATE").icon(Material.GOLD_PLATE).build(p); + + // Egg throwing + EGGS = new FlagBuilder().id("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 + */ + FIRE = new FlagBuilder().id("FIRE").icon(Material.FLINT_AND_STEEL).listener(new FireListener(p)).build(p); + FIRE_EXTINGUISH = new FlagBuilder().id("FIRE_EXTINGUISH").icon(Material.POTION).build(p); + FIRE_SPREAD = new FlagBuilder().id("FIRE_SPREAD").icon(Material.FIREWORK_CHARGE).build(p); + + // Inventories + MOUNT_INVENTORY = new FlagBuilder().id("MOUNT_INVENTORY").icon(Material.IRON_BARDING).listener(new InventoryListener(p)).build(p); + + // Hurting things + HURT_MOBS = new FlagBuilder().id("HURT_MOBS").icon(Material.STONE_SWORD).listener(new HurtingListener(p)).build(p); + HURT_MONSTERS = new FlagBuilder().id("HURT_MONSTERS").icon(Material.WOOD_SWORD).build(p); + + // Leashes + LEASH = new FlagBuilder().id("LEASH").icon(Material.LEASH).listener(new LeashListener(p)).build(p); + + // Portal use protection + PORTAL = new FlagBuilder().id("PORTAL").icon(Material.OBSIDIAN).listener(new PortalListener(p)).build(p); + + // PVP + PVP_OVERWORLD = new FlagBuilder().id("PVP_OVERWORLD").icon(Material.ARROW).listener(new PVPListener(p)).build(p); + PVP_NETHER = new FlagBuilder().id("PVP_NETHER").icon(Material.IRON_AXE).build(p); + PVP_END = new FlagBuilder().id("PVP_END").icon(Material.END_CRYSTAL).build(p); + + // Shearing + SHEARING = new FlagBuilder().id("SHEARING").icon(Material.SHEARS).listener(new ShearingListener(p)).build(p); + + // Item pickup or drop + ITEM_DROP = new FlagBuilder().id("ITEM_DROP").icon(Material.DIRT).allowedByDefault(true).listener(new ItemDropPickUpListener(p)).build(p); + ITEM_PICKUP = new FlagBuilder().id("ITEM_PICKUP").icon(Material.DIRT).build(p); + + /* + * Non-protection flags + */ + + ENTER_EXIT_MESSAGES = new FlagBuilder().id("ENTER_EXIT_MESSAGES").icon(Material.DIRT).allowedByDefault(true).build(p); + MOB_SPAWN = new FlagBuilder().id("MOB_SPAWN").icon(Material.APPLE).allowedByDefault(true).build(p); + MONSTER_SPAWN = new FlagBuilder().id("MONSTER_SPAWN").icon(Material.MOB_SPAWNER).allowedByDefault(true).build(p); } - // Break and place blocks - public static final Flag BREAK_BLOCKS = new FlagBuilder().id("BREAK_BLOCKS").icon(Material.STONE).listener(new BreakBlocksListener(p)).build(p); - public static final Flag PLACE_BLOCKS = new FlagBuilder().id("PLACE_BLOCKS").icon(Material.DIRT).listener(new PlaceBlocksListener(p)).build(p); - - // Block interactions - all use BlockInteractionListener() - public static final Flag ANVIL = new FlagBuilder().id("ANVIL").icon(Material.ANVIL).listener(new BlockInteractionListener(p)).build(p); - public static final Flag BEACON = new FlagBuilder().id("BEACON").icon(Material.BEACON).build(p); - public static final Flag BED = new FlagBuilder().id("BED").icon(Material.BED).build(p); - public static final Flag BREWING = new FlagBuilder().id("BREWING").icon(Material.BREWING_STAND_ITEM).build(p); - public static final Flag CHEST = new FlagBuilder().id("CHEST").icon(Material.CHEST).build(p); - public static final Flag DOOR = new FlagBuilder().id("DOOR").allowedByDefault(true).icon(Material.WOODEN_DOOR).build(p); - public static final Flag CRAFTING = new FlagBuilder().id("CRAFTING").allowedByDefault(true).icon(Material.WORKBENCH).build(p); - public static final Flag ENCHANTING = new FlagBuilder().id("ENCHANTING").allowedByDefault(true).icon(Material.ENCHANTMENT_TABLE).build(p); - public static final Flag FURNACE = new FlagBuilder().id("FURNACE").icon(Material.FURNACE).build(p); - public static final Flag GATE = new FlagBuilder().id("GATE").allowedByDefault(true).icon(Material.FENCE_GATE).build(p); - public static final Flag MUSIC = new FlagBuilder().id("MUSIC").icon(Material.JUKEBOX).build(p); - public static final Flag LEVER_BUTTON = new FlagBuilder().id("LEVER_BUTTON").icon(Material.LEVER).build(p); - public static final Flag REDSTONE = new FlagBuilder().id("REDSTONE").icon(Material.REDSTONE).build(p); - public static final Flag SPAWN_EGGS = new FlagBuilder().id("SPAWN_EGGS").icon(Material.MONSTER_EGG).build(p); - - // Entity interactions - public static final Flag ARMOR_STAND = new FlagBuilder().id("ARMOR_STAND").icon(Material.ARMOR_STAND).listener(new EntityInteractListener(p)).build(p); - public static final Flag RIDING = new FlagBuilder().id("RIDING").icon(Material.GOLD_BARDING).build(p); - public static final Flag TRADING = new FlagBuilder().id("TRADING").allowedByDefault(true).icon(Material.EMERALD).build(p); - - // Breeding - public static final Flag BREEDING = new FlagBuilder().id("BREEDING").icon(Material.CARROT).listener(new BreedingListener(p)).build(p); - - // Buckets. All bucket use is covered by one listener - public static final Flag BUCKET = new FlagBuilder().id("BUCKET").icon(Material.BUCKET).listener(new BucketListener(p)).build(p); - public static final Flag COLLECT_LAVA = new FlagBuilder().id("COLLECT_LAVA").icon(Material.LAVA_BUCKET).build(p); - public static final Flag COLLECT_WATER = new FlagBuilder().id("COLLECT_WATER").icon(Material.WATER_BUCKET).build(p); - public static final Flag MILKING = new FlagBuilder().id("MILKING").icon(Material.MILK_BUCKET).build(p); - - // Chorus Fruit and Enderpearls - public static final Flag CHORUS_FRUIT = new FlagBuilder().id("CHORUS_FRUIT").icon(Material.CHORUS_FRUIT).listener(new TeleportationListener(p)).build(p); - public static final Flag ENDER_PEARL = new FlagBuilder().id("ENDER_PEARL").icon(Material.ENDER_PEARL).build(p); - - // Physical interactions - public static final Flag CROP_TRAMPLE = new FlagBuilder().id("CROP_TRAMPLE").icon(Material.WHEAT).listener(new PhysicalInteractionListener(p)).build(p); - public static final Flag PRESSURE_PLATE = new FlagBuilder().id("PRESSURE_PLATE").icon(Material.GOLD_PLATE).build(p); - - // Egg throwing - public static final Flag EGGS = new FlagBuilder().id("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 - */ - public static final Flag FIRE = new FlagBuilder().id("FIRE").icon(Material.FLINT_AND_STEEL).listener(new FireListener(p)).build(p); - public static final Flag FIRE_EXTINGUISH = new FlagBuilder().id("FIRE_EXTINGUISH").icon(Material.POTION).build(p); - public static final Flag FIRE_SPREAD = new FlagBuilder().id("FIRE_SPREAD").icon(Material.FIREWORK_CHARGE).build(p); - - // Inventories - public static final Flag MOUNT_INVENTORY = new FlagBuilder().id("MOUNT_INVENTORY").icon(Material.IRON_BARDING).listener(new InventoryListener(p)).build(p); - - // Hurting things - public static final Flag HURT_MOBS = new FlagBuilder().id("HURT_MOBS").icon(Material.STONE_SWORD).listener(new HurtingListener(p)).build(p); - public static final Flag HURT_MONSTERS = new FlagBuilder().id("HURT_MONSTERS").icon(Material.WOOD_SWORD).build(p); - - // Leashes - public static final Flag LEASH = new FlagBuilder().id("LEASH").icon(Material.LEASH).listener(new LeashListener(p)).build(p); - - // Portal use protection - public static final Flag PORTAL = new FlagBuilder().id("PORTAL").icon(Material.OBSIDIAN).listener(new PortalListener(p)).build(p); - - // PVP - public static final Flag PVP_OVERWORLD = new FlagBuilder().id("PVP_OVERWORLD").icon(Material.ARROW).listener(new PVPListener(p)).build(p); - public static final Flag PVP_NETHER = new FlagBuilder().id("PVP_NETHER").icon(Material.IRON_AXE).build(p); - public static final Flag PVP_END = new FlagBuilder().id("PVP_END").icon(Material.END_CRYSTAL).build(p); - - // Shearing - public static final Flag SHEARING = new FlagBuilder().id("SHEARING").icon(Material.SHEARS).listener(new ShearingListener(p)).build(p); - - // Item pickup or drop - public static final Flag ITEM_DROP = new FlagBuilder().id("ITEM_DROP").icon(Material.DIRT).allowedByDefault(true).listener(new ItemDropPickUpListener(p)).build(p); - public static final Flag ITEM_PICKUP = new FlagBuilder().id("ITEM_PICKUP").icon(Material.DIRT).build(p); - - /* - * Non-protection flags - */ - - public static final Flag ENTER_EXIT_MESSAGES = new FlagBuilder().id("ENTER_EXIT_MESSAGES").icon(Material.DIRT).allowedByDefault(true).build(p); - public static final Flag MOB_SPAWN = new FlagBuilder().id("MOB_SPAWN").icon(Material.APPLE).allowedByDefault(true).build(p); - public static final Flag MONSTER_SPAWN = new FlagBuilder().id("MONSTER_SPAWN").icon(Material.MOB_SPAWNER).allowedByDefault(true).build(p); - } diff --git a/src/test/java/bskyblock/TestBSkyBlock.java b/src/test/java/bskyblock/TestBSkyBlock.java index d17969031..c6eac0659 100644 --- a/src/test/java/bskyblock/TestBSkyBlock.java +++ b/src/test/java/bskyblock/TestBSkyBlock.java @@ -24,9 +24,15 @@ import org.bukkit.Server; import org.bukkit.World; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemFactory; +import org.bukkit.inventory.meta.ItemMeta; import org.junit.BeforeClass; import org.junit.Test; +import org.junit.runner.RunWith; import org.mockito.Mockito; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; import us.tastybento.bskyblock.BSkyBlock; import us.tastybento.bskyblock.Constants; @@ -35,13 +41,15 @@ import us.tastybento.bskyblock.api.commands.User; import us.tastybento.bskyblock.api.events.IslandBaseEvent; import us.tastybento.bskyblock.api.events.team.TeamEvent; import us.tastybento.bskyblock.database.objects.Island; +import us.tastybento.bskyblock.lists.Flags; import us.tastybento.bskyblock.managers.FlagsManager; import us.tastybento.bskyblock.managers.RanksManager; import us.tastybento.bskyblock.util.Util; -//@RunWith(PowerMockRunner.class) +@RunWith(PowerMockRunner.class) //@SuppressStaticInitializationFor("us.tastybento.BSkyBlock") -//@PrepareForTest( { BSkyBlock.class }) +//@PrepareForTest( { Bukkit.class }) +@PrepareForTest( { Flags.class }) public class TestBSkyBlock { private final UUID playerUUID = UUID.randomUUID(); private static CommandSender sender; @@ -52,6 +60,7 @@ public class TestBSkyBlock { @BeforeClass public static void setUp() { + //PowerMockito.mockStatic(Bukkit.class); //Mockito.doReturn(plugin).when(BSkyBlock.getPlugin()); //Mockito.when().thenReturn(plugin); World world = mock(World.class); @@ -69,7 +78,7 @@ public class TestBSkyBlock { player = mock(Player.class); Mockito.when(player.hasPermission(Constants.PERMPREFIX + "default.permission")).thenReturn(true); - plugin = mock(BSkyBlock.class); + //Mockito.when(plugin.getServer()).thenReturn(server); location = mock(Location.class); @@ -78,18 +87,17 @@ public class TestBSkyBlock { Mockito.when(location.getBlockY()).thenReturn(0); Mockito.when(location.getBlockZ()).thenReturn(0); - // This doesn't work! - /* - mockStatic(Bukkit.class); + // Mock itemFactory for ItemStack ItemFactory itemFactory = PowerMockito.mock(ItemFactory.class); PowerMockito.when(Bukkit.getItemFactory()).thenReturn(itemFactory); - PowerMockito.when(itemFactory.getItemMeta(any())).thenReturn(PowerMockito.mock(ItemMeta.class)); - - mockStatic(BSkyBlock.class); - flagsManager = mock(FlagsManager.class); - PowerMockito.when(BSkyBlock.getInstance()).thenReturn(plugin); + ItemMeta itemMeta = PowerMockito.mock(ItemMeta.class); + PowerMockito.when(itemFactory.getItemMeta(Mockito.any())).thenReturn(itemMeta); + + PowerMockito.mockStatic(Flags.class); + + plugin = Mockito.mock(BSkyBlock.class); + flagsManager = Mockito.mock(FlagsManager.class); Mockito.when(plugin.getFlagsManager()).thenReturn(flagsManager); - */ } @Test @@ -342,11 +350,22 @@ public class TestBSkyBlock { assertFalse(island.getBanned().contains(member1)); // Protection - // Set up protection settings - members can break blocks, visitors and place blocks - // These tests do not work because of static method calls in the code and Bukkit. - /* + new Flags(plugin); + // Check default settings + // Owner should be able to do anything + assertTrue(island.isAllowed(owner, Flags.PLACE_BLOCKS)); + assertTrue(island.isAllowed(owner, Flags.BREAK_BLOCKS)); + + // Visitor can do nothing + assertFalse(island.isAllowed(visitor, Flags.PLACE_BLOCKS)); + assertFalse(island.isAllowed(visitor, Flags.BREAK_BLOCKS)); + + // Set up protection settings - members can break blocks, visitors and place blocks island.setFlag(Flags.BREAK_BLOCKS, RanksManager.MEMBER_RANK); + assertFalse(island.isAllowed(visitor, Flags.BREAK_BLOCKS)); + island.setFlag(Flags.PLACE_BLOCKS, RanksManager.VISITOR_RANK); + assertFalse(island.isAllowed(visitor, Flags.BREAK_BLOCKS)); // Owner should be able to do anything assertTrue(island.isAllowed(owner, Flags.PLACE_BLOCKS)); @@ -357,21 +376,22 @@ public class TestBSkyBlock { assertFalse(island.isAllowed(visitor, Flags.BREAK_BLOCKS)); // Check if the members have capability - User mem1 = User.getInstance(member1); - User mem2 = User.getInstance(member2); - User mem3 = User.getInstance(member3); + User mem1 = User.getInstance(member1); // Visitor + User mem2 = User.getInstance(member2); // Member + island.addToBanList(member3); + User mem3 = User.getInstance(member3); // Banned assertTrue(island.isAllowed(mem1, Flags.PLACE_BLOCKS)); - assertTrue(island.isAllowed(mem1, Flags.BREAK_BLOCKS)); + assertFalse(island.isAllowed(mem1, Flags.BREAK_BLOCKS)); assertTrue(island.isAllowed(mem2, Flags.PLACE_BLOCKS)); assertTrue(island.isAllowed(mem2, Flags.BREAK_BLOCKS)); // Member 3 is no longer a member and is a visitor - assertTrue(island.isAllowed(mem3, Flags.PLACE_BLOCKS)); - assertTrue(island.isAllowed(mem3, Flags.BREAK_BLOCKS)); + assertFalse(island.isAllowed(mem3, Flags.PLACE_BLOCKS)); + assertFalse(island.isAllowed(mem3, Flags.BREAK_BLOCKS)); + -*/ /* * * Score approach: