Minor code smell reduction

This commit is contained in:
tastybento 2023-07-22 16:03:55 -07:00
parent fec43adc35
commit 3e1dc81592
6 changed files with 44 additions and 48 deletions

View File

@ -100,7 +100,7 @@ public class BlockInteractionListener extends FlagListener
return; return;
} }
if (block.getState() instanceof BrushableBlock bb && BlockInteractionListener.holds(player, Material.BRUSH)) { if (block.getState() instanceof BrushableBlock && BlockInteractionListener.holds(player, Material.BRUSH)) {
// Protect this using break blocks flag for now. Maybe in the future it can have its own flag. // Protect this using break blocks flag for now. Maybe in the future it can have its own flag.
this.checkIsland(e, player, loc, Flags.BREAK_BLOCKS); this.checkIsland(e, player, loc, Flags.BREAK_BLOCKS);
return; return;

View File

@ -35,22 +35,20 @@ public class BreakBlocksListener extends FlagListener {
Player p = e.getPlayer(); Player p = e.getPlayer();
Location l = e.getBlock().getLocation(); Location l = e.getBlock().getLocation();
Material m = e.getBlock().getType(); Material m = e.getBlock().getType();
switch (m) if (m.equals(Material.MELON) || m.equals(Material.PUMPKIN)) {
{ this.checkIsland(e, p, l, Flags.HARVEST);
case MELON, PUMPKIN -> this.checkIsland(e, p, l, Flags.HARVEST); } else {
default -> {
// Crops // Crops
if (Tag.CROPS.isTagged(m) if (Tag.CROPS.isTagged(m)
&& !m.equals(Material.MELON_STEM) && !m.equals(Material.MELON_STEM)
&& !m.equals(Material.PUMPKIN_STEM) && !m.equals(Material.PUMPKIN_STEM)
&& !m.equals(Material.ATTACHED_MELON_STEM) && !m.equals(Material.ATTACHED_MELON_STEM)
&& !m.equals(Material.ATTACHED_PUMPKIN_STEM)) { && !m.equals(Material.ATTACHED_PUMPKIN_STEM)) {
this.checkIsland(e, p, l, Flags.HARVEST); this.checkIsland(e, p, l, Flags.HARVEST);
} else { } else {
checkIsland(e, p, l, Flags.BREAK_BLOCKS); checkIsland(e, p, l, Flags.BREAK_BLOCKS);
} }
} }
}
} }
/** /**

View File

@ -90,7 +90,7 @@ public class PVPListener extends FlagListener {
user.notify(getFlag(player.getWorld()).getHintReference()); user.notify(getFlag(player.getWorld()).getHintReference());
e.setCancelled(true); e.setCancelled(true);
} }
} else if (damager instanceof Projectile && ((Projectile)damager).getShooter() instanceof Player shooter) { } else if (damager instanceof Projectile projectile && projectile.getShooter() instanceof Player shooter) {
// Find out who fired the arrow // Find out who fired the arrow
processDamage(e, damager, shooter, hurtEntity, flag); processDamage(e, damager, shooter, hurtEntity, flag);
} else if (damager instanceof Firework && firedFireworks.containsKey(damager)) { } else if (damager instanceof Firework && firedFireworks.containsKey(damager)) {

View File

@ -66,7 +66,7 @@ public class ItemParser {
try { try {
// Because I am lazy, and do not want to rewrite every parser, I will just add custom data as // Because I am lazy, and do not want to rewrite every parser, I will just add custom data as
// parameter and remove that array part form input data. // parameter and remove that array part form input data.
Optional<String> first = Arrays.stream(part).filter(field -> field.matches("(CMD-[0-9]*)")).findFirst(); Optional<String> first = Arrays.stream(part).filter(field -> field.matches("(CMD-\\d*)")).findFirst();
Integer customModelData = null; Integer customModelData = null;
if (first.isPresent()) { if (first.isPresent()) {
@ -75,7 +75,7 @@ public class ItemParser {
int j = 0; int j = 0;
for (String field : part) { for (String field : part) {
if (!field.matches("(CMD-[0-9]*)")) { if (!field.matches("(CMD-\\d*)")) {
copyParts[j++] = field; copyParts[j++] = field;
} }
} }
@ -182,11 +182,11 @@ public class ItemParser {
/** /**
* This method parses array of 6 items into an item stack. * This method parses array of 6 items into an item stack.
* Format: * Format:
* <pre>{@code * <pre>{@code
* POTION:NAME:<LEVEL>:<EXTENDED>:<SPLASH/LINGER>:QTY * POTION:NAME:<LEVEL>:<EXTENDED>:<SPLASH/LINGER>:QTY
* }</pre> * }</pre>
* Example: * Example:
* <pre>{@code * <pre>{@code
* POTION:STRENGTH:1:EXTENDED:SPLASH:1 * POTION:STRENGTH:1:EXTENDED:SPLASH:1
* }</pre> * }</pre>
* @param part String array that contains 6 elements. * @param part String array that contains 6 elements.
@ -261,13 +261,13 @@ public class ItemParser {
/** /**
* This method parses array of 2 to 3 elements that represents player head. * This method parses array of 2 to 3 elements that represents player head.
* Format: * Format:
* <pre>{@code * <pre>{@code
* PLAYER_HEAD:<STRING/Trimmed UUID/UUID/Texture>:QTY * PLAYER_HEAD:<STRING/Trimmed UUID/UUID/Texture>:QTY
* PLAYER_HEAD:<STRING/Trimmed UUID/UUID/Texture> * PLAYER_HEAD:<STRING/Trimmed UUID/UUID/Texture>
* PLAYER_HEAD:QTY * PLAYER_HEAD:QTY
* }</pre> * }</pre>
* Example: * Example:
* <pre>{@code * <pre>{@code
* PLAYER_HEAD:1 * PLAYER_HEAD:1
* PLAYER_HEAD:BONNe1704 * PLAYER_HEAD:BONNe1704
* PLAYER_HEAD:eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvYWY1ZjE1OTg4NmNjNTMxZmZlYTBkOGFhNWY5MmVkNGU1ZGE2NWY3MjRjMDU3MGFmODZhOTBiZjAwYzY3YzQyZSJ9fX0:1 * PLAYER_HEAD:eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvYWY1ZjE1OTg4NmNjNTMxZmZlYTBkOGFhNWY5MmVkNGU1ZGE2NWY3MjRjMDU3MGFmODZhOTBiZjAwYzY3YzQyZSJ9fX0:1

View File

@ -313,7 +313,6 @@ public class ClosestSafeSpotTeleport
{ {
// Try to teleport player to the highest block. // Try to teleport player to the highest block.
this.asyncTeleport(highestBlock.getLocation().add(new Vector(0.5D, 0D, 0.5D))); this.asyncTeleport(highestBlock.getLocation().add(new Vector(0.5D, 0D, 0.5D)));
return;
} }
else if (!this.plugin.getIWM().inWorld(this.entity.getLocation())) else if (!this.plugin.getIWM().inWorld(this.entity.getLocation()))
{ {

View File

@ -11,7 +11,6 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -103,7 +102,7 @@ public class AdminTeleportCommandTest {
when(p.getUniqueId()).thenReturn(uuid); when(p.getUniqueId()).thenReturn(uuid);
when(p.hasPermission("admin.tp")).thenReturn(true); when(p.hasPermission("admin.tp")).thenReturn(true);
when(p.hasPermission("admin")).thenReturn(false); when(p.hasPermission("admin")).thenReturn(false);
when(user.getUniqueId()).thenReturn(uuid); when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(p); when(user.getPlayer()).thenReturn(p);
when(user.getName()).thenReturn("tastybento"); when(user.getName()).thenReturn("tastybento");
@ -153,7 +152,7 @@ public class AdminTeleportCommandTest {
when(lm.get(any(), any())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(1, String.class)); when(lm.get(any(), any())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(1, String.class));
when(plugin.getLocalesManager()).thenReturn(lm); when(plugin.getLocalesManager()).thenReturn(lm);
when(user.getTranslation(Mockito.anyString(),Mockito.anyString(), Mockito.anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class)); when(user.getTranslation(anyString(),anyString(), anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class));
// Island location // Island location
Location location = mock(Location.class); Location location = mock(Location.class);
@ -178,7 +177,7 @@ public class AdminTeleportCommandTest {
// Util // Util
PowerMockito.mockStatic(Util.class, Mockito.RETURNS_MOCKS); PowerMockito.mockStatic(Util.class, Mockito.RETURNS_MOCKS);
when(Util.getUUID(anyString())).thenCallRealMethod(); when(Util.getUUID(anyString())).thenCallRealMethod();
// Placeholder manager // Placeholder manager
when(plugin.getPlaceholdersManager()).thenReturn(phm); when(plugin.getPlaceholdersManager()).thenReturn(phm);
} }
@ -215,16 +214,16 @@ public class AdminTeleportCommandTest {
@Test @Test
public void testExecuteUserStringListOfStringUnknownTarget() { public void testExecuteUserStringListOfStringUnknownTarget() {
AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tp"); AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tp");
assertFalse(atc.canExecute(user, "tp", Collections.singletonList("tastybento"))); assertFalse(atc.canExecute(user, "tp", List.of("tastybento")));
verify(user).sendMessage(eq("general.errors.unknown-player"), eq(TextVariables.NAME), eq("tastybento")); verify(user).sendMessage(eq("general.errors.unknown-player"), eq(TextVariables.NAME), eq("tastybento"));
} }
@Test @Test
public void testExecuteUserStringListOfStringKnownTargetNoIsland() { public void testExecuteUserStringListOfStringKnownTargetNoIsland() {
when(pm.getUUID(eq("tastybento"))).thenReturn(notUUID); when(pm.getUUID("tastybento")).thenReturn(notUUID);
when(im.hasIsland(any(), any(UUID.class))).thenReturn(false); when(im.hasIsland(any(), any(UUID.class))).thenReturn(false);
AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tp"); AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tp");
assertFalse(atc.canExecute(user, "tp", Collections.singletonList("tastybento"))); assertFalse(atc.canExecute(user, "tp", List.of("tastybento")));
verify(user).sendMessage(eq("general.errors.player-has-no-island")); verify(user).sendMessage(eq("general.errors.player-has-no-island"));
} }
@ -233,12 +232,12 @@ public class AdminTeleportCommandTest {
*/ */
@Test @Test
public void testExecuteUserStringListOfStringKnownTargetHasIsland() { public void testExecuteUserStringListOfStringKnownTargetHasIsland() {
when(pm.getUUID(eq("tastybento"))).thenReturn(notUUID); when(pm.getUUID("tastybento")).thenReturn(notUUID);
when(im.hasIsland(any(), any(UUID.class))).thenReturn(true); when(im.hasIsland(any(), any(UUID.class))).thenReturn(true);
AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tp"); AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tp");
assertTrue(atc.canExecute(user, "tp", Collections.singletonList("tastybento"))); assertTrue(atc.canExecute(user, "tp", List.of("tastybento")));
assertTrue(atc.execute(user, "tp", Collections.singletonList("tastybento"))); assertTrue(atc.execute(user, "tp", List.of("tastybento")));
verify(user).getTranslation(eq("commands.admin.tp.manual"), eq("[location]"), eq("0 0 0")); verify(user).getTranslation("commands.admin.tp.manual", "[location]", "0 0 0");
} }
/** /**
@ -247,56 +246,56 @@ public class AdminTeleportCommandTest {
@Test @Test
public void testExecuteUserStringListOfStringKnownTargetHasIslandSpawnPoint() { public void testExecuteUserStringListOfStringKnownTargetHasIslandSpawnPoint() {
when(island.getSpawnPoint(any())).thenReturn(spawnPoint); when(island.getSpawnPoint(any())).thenReturn(spawnPoint);
when(pm.getUUID(eq("tastybento"))).thenReturn(notUUID); when(pm.getUUID("tastybento")).thenReturn(notUUID);
when(im.hasIsland(any(), any(UUID.class))).thenReturn(true); when(im.hasIsland(any(), any(UUID.class))).thenReturn(true);
AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tp"); AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tp");
assertTrue(atc.canExecute(user, "tp", Collections.singletonList("tastybento"))); assertTrue(atc.canExecute(user, "tp", List.of("tastybento")));
assertTrue(atc.execute(user, "tp", Collections.singletonList("tastybento"))); assertTrue(atc.execute(user, "tp", List.of("tastybento")));
verify(user).getTranslation(eq("commands.admin.tp.manual"), eq("[location]"), eq("0 0 0")); verify(user).getTranslation("commands.admin.tp.manual", "[location]", "0 0 0");
} }
@Test @Test
public void testExecuteUserStringListOfStringKnownTargetIsTeamMember() { public void testExecuteUserStringListOfStringKnownTargetIsTeamMember() {
when(pm.getUUID(eq("tastybento"))).thenReturn(notUUID); when(pm.getUUID("tastybento")).thenReturn(notUUID);
when(im.hasIsland(any(), any(UUID.class))).thenReturn(false); when(im.hasIsland(any(), any(UUID.class))).thenReturn(false);
when(im.inTeam(any(), any(UUID.class))).thenReturn(true); when(im.inTeam(any(), any(UUID.class))).thenReturn(true);
AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tp"); AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tp");
assertTrue(atc.canExecute(user, "tp", Collections.singletonList("tastybento"))); assertTrue(atc.canExecute(user, "tp", List.of("tastybento")));
assertTrue(atc.execute(user, "tp", Collections.singletonList("tastybento"))); assertTrue(atc.execute(user, "tp", List.of("tastybento")));
verify(iwm, Mockito.never()).getNetherWorld(any()); verify(iwm, Mockito.never()).getNetherWorld(any());
verify(iwm, Mockito.never()).getEndWorld(any()); verify(iwm, Mockito.never()).getEndWorld(any());
verify(user).getTranslation(eq("commands.admin.tp.manual"), eq("[location]"), eq("0 0 0")); verify(user).getTranslation("commands.admin.tp.manual", "[location]", "0 0 0");
} }
@Test @Test
public void testExecuteUserStringListOfStringKnownTargetHasIslandNether() { public void testExecuteUserStringListOfStringKnownTargetHasIslandNether() {
when(pm.getUUID(eq("tastybento"))).thenReturn(notUUID); when(pm.getUUID("tastybento")).thenReturn(notUUID);
when(im.hasIsland(any(), any(UUID.class))).thenReturn(true); when(im.hasIsland(any(), any(UUID.class))).thenReturn(true);
AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tpnether"); AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tpnether");
assertTrue(atc.canExecute(user, "tpnether", Collections.singletonList("tastybento"))); assertTrue(atc.canExecute(user, "tpnether", List.of("tastybento")));
assertTrue(atc.execute(user, "tpnether", Collections.singletonList("tastybento"))); assertTrue(atc.execute(user, "tpnether", List.of("tastybento")));
verify(iwm).getNetherWorld(any()); verify(iwm).getNetherWorld(any());
verify(iwm, Mockito.never()).getEndWorld(any()); verify(iwm, Mockito.never()).getEndWorld(any());
verify(user).getTranslation(eq("commands.admin.tp.manual"), eq("[location]"), eq("0 0 0")); verify(user).getTranslation("commands.admin.tp.manual", "[location]", "0 0 0");
} }
@Test @Test
public void testExecuteUserStringListOfStringKnownTargetHasIslandEnd() { public void testExecuteUserStringListOfStringKnownTargetHasIslandEnd() {
when(pm.getUUID(eq("tastybento"))).thenReturn(notUUID); when(pm.getUUID("tastybento")).thenReturn(notUUID);
when(im.hasIsland(any(), any(UUID.class))).thenReturn(true); when(im.hasIsland(any(), any(UUID.class))).thenReturn(true);
AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tpend"); AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tpend");
assertTrue(atc.canExecute(user, "tpend", Collections.singletonList("tastybento"))); assertTrue(atc.canExecute(user, "tpend", List.of("tastybento")));
assertTrue(atc.execute(user, "tpend", Collections.singletonList("tastybento"))); assertTrue(atc.execute(user, "tpend", List.of("tastybento")));
verify(iwm, Mockito.never()).getNetherWorld(any()); verify(iwm, Mockito.never()).getNetherWorld(any());
verify(iwm).getEndWorld(any()); verify(iwm).getEndWorld(any());
verify(user).getTranslation(eq("commands.admin.tp.manual"), eq("[location]"), eq("0 0 0")); verify(user).getTranslation("commands.admin.tp.manual", "[location]", "0 0 0");
} }
@Test @Test
public void testPermissionsNoRootPermission() { public void testPermissionsNoRootPermission() {
when(p.hasPermission("admin.tp")).thenReturn(true); when(p.hasPermission("admin.tp")).thenReturn(true);
when(p.hasPermission("admin")).thenReturn(false); when(p.hasPermission("admin")).thenReturn(false);
when(pm.getUUID(eq("tastybento"))).thenReturn(notUUID); when(pm.getUUID("tastybento")).thenReturn(notUUID);
when(im.hasIsland(any(), any(UUID.class))).thenReturn(true); when(im.hasIsland(any(), any(UUID.class))).thenReturn(true);
AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tpend"); AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tpend");
assertTrue(atc.canExecute(user, "tpend", List.of("tastybento"))); assertTrue(atc.canExecute(user, "tpend", List.of("tastybento")));
@ -306,12 +305,12 @@ public class AdminTeleportCommandTest {
// Should fail // Should fail
assertFalse(atc.execute(p, "tpend", list)); assertFalse(atc.execute(p, "tpend", list));
} }
@Test @Test
public void testPermissionsHasRootPermission() { public void testPermissionsHasRootPermission() {
when(p.hasPermission("admin.tp")).thenReturn(true); when(p.hasPermission("admin.tp")).thenReturn(true);
when(p.hasPermission("admin")).thenReturn(true); when(p.hasPermission("admin")).thenReturn(true);
when(pm.getUUID(eq("tastybento"))).thenReturn(notUUID); when(pm.getUUID("tastybento")).thenReturn(notUUID);
when(im.hasIsland(any(), any(UUID.class))).thenReturn(true); when(im.hasIsland(any(), any(UUID.class))).thenReturn(true);
AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tpend"); AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tpend");
assertTrue(atc.canExecute(user, "tpend", List.of("tastybento"))); assertTrue(atc.canExecute(user, "tpend", List.of("tastybento")));