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

View File

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

View File

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