Remove code smells.

This commit is contained in:
tastybento 2019-11-05 23:11:56 -08:00
parent ef67079831
commit 21916b5a59
3 changed files with 20 additions and 25 deletions

View File

@ -30,15 +30,15 @@ import world.bentobox.bentobox.lists.Flags;
*/ */
public class BlockInteractionListener extends FlagListener { public class BlockInteractionListener extends FlagListener {
private static Map<Material, Flag> inHandItems; private final Map<Material, Flag> inHandItems;
private static Map<Material, Flag> clickedBlocks; private final Map<Material, Flag> clickedBlocks;
public BlockInteractionListener() { public BlockInteractionListener() {
inHandItems = new EnumMap<Material, Flag>(Material.class); inHandItems = new EnumMap<>(Material.class);
inHandItems.put(Material.ENDER_PEARL, Flags.ENDER_PEARL); inHandItems.put(Material.ENDER_PEARL, Flags.ENDER_PEARL);
inHandItems.put(Material.BONE_MEAL, Flags.PLACE_BLOCKS); inHandItems.put(Material.BONE_MEAL, Flags.PLACE_BLOCKS);
clickedBlocks = new EnumMap<Material, Flag>(Material.class); clickedBlocks = new EnumMap<>(Material.class);
clickedBlocks.put(Material.ANVIL, Flags.ANVIL); clickedBlocks.put(Material.ANVIL, Flags.ANVIL);
clickedBlocks.put(Material.CHIPPED_ANVIL, Flags.ANVIL); clickedBlocks.put(Material.CHIPPED_ANVIL, Flags.ANVIL);
clickedBlocks.put(Material.DAMAGED_ANVIL, Flags.ANVIL); clickedBlocks.put(Material.DAMAGED_ANVIL, Flags.ANVIL);
@ -167,12 +167,10 @@ public class BlockInteractionListener extends FlagListener {
// Boats // Boats
if (e.getItem().getType().name().endsWith("_BOAT")) { if (e.getItem().getType().name().endsWith("_BOAT")) {
checkIsland(e, e.getPlayer(), e.getClickedBlock().getLocation(), Flags.PLACE_BLOCKS); checkIsland(e, e.getPlayer(), e.getClickedBlock().getLocation(), Flags.PLACE_BLOCKS);
return;
} }
// Spawn eggs // Spawn eggs
else if (e.getItem().getType().name().endsWith("_SPAWN_EGG")) { else if (e.getItem().getType().name().endsWith("_SPAWN_EGG")) {
checkIsland(e, e.getPlayer(), e.getClickedBlock().getLocation(), Flags.SPAWN_EGGS); checkIsland(e, e.getPlayer(), e.getClickedBlock().getLocation(), Flags.SPAWN_EGGS);
return;
} }
// Other items // Other items
else if (inHandItems.containsKey(e.getItem().getType())) { else if (inHandItems.containsKey(e.getItem().getType())) {
@ -237,14 +235,14 @@ public class BlockInteractionListener extends FlagListener {
/** /**
* @return the inHandItems * @return the inHandItems
*/ */
public static Map<Material, Flag> getInHandItems() { public Map<Material, Flag> getInHandItems() {
return inHandItems; return inHandItems;
} }
/** /**
* @return the clickedBlocks * @return the clickedBlocks
*/ */
public static Map<Material, Flag> getClickedBlocks() { public Map<Material, Flag> getClickedBlocks() {
return clickedBlocks; return clickedBlocks;
} }
} }

View File

@ -34,19 +34,16 @@ public class EntityInteractListener extends FlagListener {
public void onPlayerInteractEntity(PlayerInteractEntityEvent e) { public void onPlayerInteractEntity(PlayerInteractEntityEvent e) {
if (e.getRightClicked() instanceof Vehicle) { if (e.getRightClicked() instanceof Vehicle) {
// Animal riding // Animal riding
if (e.getRightClicked() instanceof Animals if (e.getRightClicked() instanceof Animals) {
&& !checkIsland(e, e.getPlayer(), e.getRightClicked().getLocation(), Flags.RIDING)) { checkIsland(e, e.getPlayer(), e.getRightClicked().getLocation(), Flags.RIDING);
return;
} }
// Minecart riding // Minecart riding
else if (e.getRightClicked() instanceof RideableMinecart else if (e.getRightClicked() instanceof RideableMinecart) {
&& !checkIsland(e, e.getPlayer(), e.getRightClicked().getLocation(), Flags.MINECART)) { checkIsland(e, e.getPlayer(), e.getRightClicked().getLocation(), Flags.MINECART);
return;
} }
// Boat riding // Boat riding
else if (e.getRightClicked() instanceof Boat else if (e.getRightClicked() instanceof Boat) {
&& !checkIsland(e, e.getPlayer(), e.getRightClicked().getLocation(), Flags.BOAT)) { checkIsland(e, e.getPlayer(), e.getRightClicked().getLocation(), Flags.BOAT);
return;
} }
} }
// Villager trading // Villager trading

View File

@ -224,14 +224,14 @@ public class BlockInteractionListenerTest {
public void testOnPlayerInteractNothingInHandNotAllowed() { public void testOnPlayerInteractNothingInHandNotAllowed() {
int count = 0; int count = 0;
int worldSettingCount = 0; int worldSettingCount = 0;
for (Material bm : BlockInteractionListener.getClickedBlocks().keySet()) { for (Material bm : bil.getClickedBlocks().keySet()) {
when(clickedBlock.getType()).thenReturn(bm); when(clickedBlock.getType()).thenReturn(bm);
PlayerInteractEvent e = new PlayerInteractEvent(player, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST, hand); PlayerInteractEvent e = new PlayerInteractEvent(player, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST, hand);
bil.onPlayerInteract(e); bil.onPlayerInteract(e);
assertTrue("Failure " + bm, e.useInteractedBlock().equals(Event.Result.DENY)); assertTrue("Failure " + bm, e.useInteractedBlock().equals(Event.Result.DENY));
if (BlockInteractionListener.getClickedBlocks().get(bm).getType().equals(Type.PROTECTION)) { if (bil.getClickedBlocks().get(bm).getType().equals(Type.PROTECTION)) {
count++; count++;
} else if (BlockInteractionListener.getClickedBlocks().get(bm).getType().equals(Type.WORLD_SETTING)) { } else if (bil.getClickedBlocks().get(bm).getType().equals(Type.WORLD_SETTING)) {
worldSettingCount++; worldSettingCount++;
} }
verify(notifier, times(count)).notify(any(), eq("protection.protected")); verify(notifier, times(count)).notify(any(), eq("protection.protected"));
@ -245,12 +245,12 @@ public class BlockInteractionListenerTest {
@Test @Test
public void testOnPlayerInteractNothingInHandAllowed() { public void testOnPlayerInteractNothingInHandAllowed() {
when(island.isAllowed(any(), any())).thenReturn(true); when(island.isAllowed(any(), any())).thenReturn(true);
for (Material bm : BlockInteractionListener.getClickedBlocks().keySet()) { for (Material bm : bil.getClickedBlocks().keySet()) {
// Allow flags // Allow flags
if (BlockInteractionListener.getClickedBlocks().get(bm).getType().equals(Type.PROTECTION)) { if (bil.getClickedBlocks().get(bm).getType().equals(Type.PROTECTION)) {
when(island.isAllowed(any(), eq(BlockInteractionListener.getClickedBlocks().get(bm)))).thenReturn(true); when(island.isAllowed(any(), eq(bil.getClickedBlocks().get(bm)))).thenReturn(true);
} else if (BlockInteractionListener.getClickedBlocks().get(bm).getType().equals(Type.WORLD_SETTING)) { } else if (bil.getClickedBlocks().get(bm).getType().equals(Type.WORLD_SETTING)) {
BlockInteractionListener.getClickedBlocks().get(bm).setSetting(world, true); bil.getClickedBlocks().get(bm).setSetting(world, true);
} }
when(clickedBlock.getType()).thenReturn(bm); when(clickedBlock.getType()).thenReturn(bm);
PlayerInteractEvent e = new PlayerInteractEvent(player, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST, hand); PlayerInteractEvent e = new PlayerInteractEvent(player, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST, hand);