mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2024-12-21 16:47:39 +01:00
Added ItemFrame protection from mobs flag and test class.
This commit is contained in:
parent
71e79db0a9
commit
984de1cd07
@ -401,6 +401,17 @@ protection:
|
||||
description: "Toggle use/crafting"
|
||||
name: "Ender Chests"
|
||||
hint: "Ender chests are disabled in this world"
|
||||
ENDERMAN_DEATH_DROP:
|
||||
description: |
|
||||
Endermen will drop
|
||||
any block they are
|
||||
holding if killed.
|
||||
name: "Enderman Death Drop"
|
||||
ENDERMAN_GRIEFING:
|
||||
description: |
|
||||
Endermen can remove
|
||||
blocks from islands
|
||||
name: "Enderman griefing"
|
||||
ENDER_PEARL:
|
||||
description: "Toggle use"
|
||||
name: "EnderPearls"
|
||||
@ -443,6 +454,11 @@ protection:
|
||||
description: "Toggle hurting"
|
||||
name: "Hurt villagers"
|
||||
hint: "No villager hurting"
|
||||
ITEM_FRAME_DAMAGE:
|
||||
description: |-
|
||||
&aMobs can damage
|
||||
&aitem frames
|
||||
name: "Item Frame Damage"
|
||||
INVINCIBLE_VISITORS:
|
||||
description: |-
|
||||
&aConfigure invincible visitor
|
||||
|
@ -542,13 +542,6 @@ public class Island implements DataObject {
|
||||
spawn = isSpawn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the flags to their default as set in config.yml for the spawn
|
||||
*/
|
||||
public void setSpawnFlagsDefaults(){
|
||||
//TODO default flags
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default spawn location for this island. Note that this may only be valid
|
||||
* after the initial pasting because the player can change the island after that point
|
||||
|
@ -0,0 +1,50 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package us.tastybento.bskyblock.listeners.flags;
|
||||
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.ItemFrame;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.entity.Projectile;
|
||||
import org.bukkit.event.Cancellable;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
||||
import org.bukkit.event.hanging.HangingBreakByEntityEvent;
|
||||
|
||||
import us.tastybento.bskyblock.api.flags.AbstractFlagListener;
|
||||
import us.tastybento.bskyblock.lists.Flags;
|
||||
|
||||
/**
|
||||
* Protects item frames from damage by mobs
|
||||
* @author tastybento
|
||||
*
|
||||
*/
|
||||
public class ItemFrameListener extends AbstractFlagListener {
|
||||
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
||||
public void onItemFrameDamage(final EntityDamageByEntityEvent e) {
|
||||
check(e, e.getEntity(), e.getDamager());
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
||||
public void onItemFrameDamage(final HangingBreakByEntityEvent e) {
|
||||
check(e, e.getEntity(), e.getRemover());
|
||||
}
|
||||
|
||||
private void check(Cancellable e, Entity entity, Entity damager) {
|
||||
if (entity instanceof ItemFrame
|
||||
&& getIWM().inWorld(entity.getLocation())
|
||||
&& !Flags.ITEM_FRAME_DAMAGE.isSetForWorld(entity.getWorld())
|
||||
&& !(damager instanceof Player)) {
|
||||
if (damager instanceof Projectile) {
|
||||
if (!(((Projectile) damager).getShooter() instanceof Player)) {
|
||||
e.setCancelled(true);
|
||||
}
|
||||
} else {
|
||||
e.setCancelled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -28,7 +28,6 @@ public class TNTListener extends AbstractFlagListener {
|
||||
/**
|
||||
* Protect TNT from being set light by a fire arrow
|
||||
* @param e - event
|
||||
* @return true if cancelled
|
||||
*/
|
||||
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
|
||||
public void onTNTDamage(EntityChangeBlockEvent e) {
|
||||
@ -47,11 +46,9 @@ public class TNTListener extends AbstractFlagListener {
|
||||
// Remove the arrow
|
||||
projectile.remove();
|
||||
e.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
@ -28,6 +28,7 @@ import us.tastybento.bskyblock.listeners.flags.InventoryListener;
|
||||
import us.tastybento.bskyblock.listeners.flags.InvincibleVisitorsListener;
|
||||
import us.tastybento.bskyblock.listeners.flags.IslandRespawnListener;
|
||||
import us.tastybento.bskyblock.listeners.flags.ItemDropPickUpListener;
|
||||
import us.tastybento.bskyblock.listeners.flags.ItemFrameListener;
|
||||
import us.tastybento.bskyblock.listeners.flags.LeashListener;
|
||||
import us.tastybento.bskyblock.listeners.flags.LockAndBanListener;
|
||||
import us.tastybento.bskyblock.listeners.flags.MobSpawnListener;
|
||||
@ -192,6 +193,9 @@ public class Flags {
|
||||
public static final Flag REMOVE_MOBS = new FlagBuilder().id("REMOVE_MOBS").icon(Material.GLOWSTONE_DUST).type(Type.WORLD_SETTING)
|
||||
.listener(new RemoveMobsListener()).allowedByDefault(true).build();
|
||||
|
||||
public static final Flag ITEM_FRAME_DAMAGE = new FlagBuilder().id("ITEM_FRAME_DAMAGE").icon(Material.ITEM_FRAME).type(Type.WORLD_SETTING)
|
||||
.listener(new ItemFrameListener()).allowedByDefault(false).build();
|
||||
|
||||
public static final Flag ISLAND_RESPAWN = new FlagBuilder().id("ISLAND_RESPAWN").icon(Material.TORCH).type(Type.WORLD_SETTING)
|
||||
.listener(new IslandRespawnListener()).allowedByDefault(true).build();
|
||||
|
||||
|
@ -0,0 +1,167 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package us.tastybento.bskyblock.listeners.flags;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Enderman;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.ItemFrame;
|
||||
import org.bukkit.entity.Slime;
|
||||
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
||||
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
|
||||
import org.bukkit.inventory.ItemFactory;
|
||||
import org.bukkit.inventory.meta.SkullMeta;
|
||||
import org.bukkit.material.MaterialData;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.junit.Before;
|
||||
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 org.powermock.reflect.Whitebox;
|
||||
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
import us.tastybento.bskyblock.Settings;
|
||||
import us.tastybento.bskyblock.api.configuration.WorldSettings;
|
||||
import us.tastybento.bskyblock.database.objects.Island;
|
||||
import us.tastybento.bskyblock.lists.Flags;
|
||||
import us.tastybento.bskyblock.managers.FlagsManager;
|
||||
import us.tastybento.bskyblock.managers.IslandWorldManager;
|
||||
import us.tastybento.bskyblock.managers.IslandsManager;
|
||||
import us.tastybento.bskyblock.util.Util;
|
||||
|
||||
/**
|
||||
* @author tastybento
|
||||
*
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest( {BSkyBlock.class, Flags.class, Util.class, Bukkit.class} )
|
||||
public class ItemFrameListenerTest {
|
||||
|
||||
private static Location location;
|
||||
private static BSkyBlock plugin;
|
||||
private static IslandWorldManager iwm;
|
||||
private static IslandsManager im;
|
||||
private static World world;
|
||||
private static Enderman enderman;
|
||||
private static Slime slime;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
// Set up plugin
|
||||
plugin = mock(BSkyBlock.class);
|
||||
Whitebox.setInternalState(BSkyBlock.class, "instance", plugin);
|
||||
|
||||
Server server = mock(Server.class);
|
||||
world = mock(World.class);
|
||||
when(server.getLogger()).thenReturn(Logger.getAnonymousLogger());
|
||||
when(server.getWorld("world")).thenReturn(world);
|
||||
when(server.getVersion()).thenReturn("BSB_Mocking");
|
||||
|
||||
PluginManager pluginManager = mock(PluginManager.class);
|
||||
when(server.getPluginManager()).thenReturn(pluginManager);
|
||||
|
||||
ItemFactory itemFactory = mock(ItemFactory.class);
|
||||
when(server.getItemFactory()).thenReturn(itemFactory);
|
||||
|
||||
PowerMockito.mockStatic(Bukkit.class);
|
||||
when(Bukkit.getServer()).thenReturn(server);
|
||||
|
||||
SkullMeta skullMeta = mock(SkullMeta.class);
|
||||
when(itemFactory.getItemMeta(any())).thenReturn(skullMeta);
|
||||
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
|
||||
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
|
||||
location = mock(Location.class);
|
||||
when(location.getWorld()).thenReturn(world);
|
||||
when(location.getBlockX()).thenReturn(0);
|
||||
when(location.getBlockY()).thenReturn(0);
|
||||
when(location.getBlockZ()).thenReturn(0);
|
||||
PowerMockito.mockStatic(Flags.class);
|
||||
|
||||
FlagsManager flagsManager = new FlagsManager(plugin);
|
||||
when(plugin.getFlagsManager()).thenReturn(flagsManager);
|
||||
|
||||
|
||||
// Worlds
|
||||
iwm = mock(IslandWorldManager.class);
|
||||
when(iwm.getBSBIslandWorld()).thenReturn(world);
|
||||
when(iwm.getBSBNetherWorld()).thenReturn(world);
|
||||
when(iwm.getBSBEndWorld()).thenReturn(world);
|
||||
when(iwm.inWorld(any())).thenReturn(true);
|
||||
when(plugin.getIWM()).thenReturn(iwm);
|
||||
|
||||
// Monsters and animals
|
||||
enderman = mock(Enderman.class);
|
||||
when(enderman.getLocation()).thenReturn(location);
|
||||
when(enderman.getWorld()).thenReturn(world);
|
||||
when(enderman.getCarriedMaterial()).thenReturn(new MaterialData(Material.STONE));
|
||||
slime = mock(Slime.class);
|
||||
when(slime.getLocation()).thenReturn(location);
|
||||
|
||||
// Fake players
|
||||
Settings settings = mock(Settings.class);
|
||||
Mockito.when(plugin.getSettings()).thenReturn(settings);
|
||||
Mockito.when(settings.getFakePlayers()).thenReturn(new HashSet<String>());
|
||||
|
||||
// World Settings
|
||||
WorldSettings ws = mock(WorldSettings.class);
|
||||
when(iwm.getWorldSettings(Mockito.any())).thenReturn(ws);
|
||||
Map<String, Boolean> worldFlags = new HashMap<>();
|
||||
when(ws.getWorldFlags()).thenReturn(worldFlags);
|
||||
|
||||
// Island manager
|
||||
im = mock(IslandsManager.class);
|
||||
when(plugin.getIslands()).thenReturn(im);
|
||||
Island island = mock(Island.class);
|
||||
Optional<Island> optional = Optional.of(island);
|
||||
when(im.getProtectedIslandAt(Mockito.any())).thenReturn(optional);
|
||||
|
||||
PowerMockito.mockStatic(Util.class);
|
||||
when(Util.getWorld(Mockito.any())).thenReturn(mock(World.class));
|
||||
// Not allowed to start
|
||||
Flags.ITEM_FRAME_DAMAGE.setSetting(world, false);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.listeners.flags.ItemFrameListener#onItemFrameDamage(org.bukkit.event.entity.EntityDamageByEntityEvent)}.
|
||||
*/
|
||||
@Test
|
||||
public void testOnItemFrameDamageEntityDamageByEntityEvent() {
|
||||
ItemFrameListener ifl = new ItemFrameListener();
|
||||
Entity entity = mock(ItemFrame.class);
|
||||
DamageCause cause = DamageCause.ENTITY_ATTACK;
|
||||
@SuppressWarnings("deprecation")
|
||||
EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(entity, enderman, cause , 0);
|
||||
ifl.onItemFrameDamage(e);
|
||||
assertTrue(e.isCancelled());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.listeners.flags.ItemFrameListener#onItemFrameDamage(org.bukkit.event.hanging.HangingBreakByEntityEvent)}.
|
||||
*/
|
||||
@Test
|
||||
public void testOnItemFrameDamageHangingBreakByEntityEvent() {
|
||||
fail("Not yet implemented"); // TODO
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user