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;
}
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.
this.checkIsland(e, player, loc, Flags.BREAK_BLOCKS);
return;

View File

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

View File

@ -90,7 +90,7 @@ public class PVPListener extends FlagListener {
user.notify(getFlag(player.getWorld()).getHintReference());
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
processDamage(e, damager, shooter, hurtEntity, flag);
} else if (damager instanceof Firework && firedFireworks.containsKey(damager)) {

View File

@ -66,7 +66,7 @@ public class ItemParser {
try {
// 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.
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;
if (first.isPresent()) {
@ -75,7 +75,7 @@ public class ItemParser {
int j = 0;
for (String field : part) {
if (!field.matches("(CMD-[0-9]*)")) {
if (!field.matches("(CMD-\\d*)")) {
copyParts[j++] = field;
}
}
@ -182,11 +182,11 @@ public class ItemParser {
/**
* This method parses array of 6 items into an item stack.
* Format:
* <pre>{@code
* <pre>{@code
* POTION:NAME:<LEVEL>:<EXTENDED>:<SPLASH/LINGER>:QTY
* }</pre>
* Example:
* <pre>{@code
* <pre>{@code
* POTION:STRENGTH:1:EXTENDED:SPLASH:1
* }</pre>
* @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.
* Format:
* <pre>{@code
* <pre>{@code
* PLAYER_HEAD:<STRING/Trimmed UUID/UUID/Texture>:QTY
* PLAYER_HEAD:<STRING/Trimmed UUID/UUID/Texture>
* PLAYER_HEAD:QTY
* }</pre>
* Example:
* <pre>{@code
* <pre>{@code
* PLAYER_HEAD:1
* PLAYER_HEAD:BONNe1704
* PLAYER_HEAD:eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvYWY1ZjE1OTg4NmNjNTMxZmZlYTBkOGFhNWY5MmVkNGU1ZGE2NWY3MjRjMDU3MGFmODZhOTBiZjAwYzY3YzQyZSJ9fX0:1

View File

@ -313,7 +313,6 @@ public class ClosestSafeSpotTeleport
{
// Try to teleport player to the highest block.
this.asyncTeleport(highestBlock.getLocation().add(new Vector(0.5D, 0D, 0.5D)));
return;
}
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 java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
@ -103,7 +102,7 @@ public class AdminTeleportCommandTest {
when(p.getUniqueId()).thenReturn(uuid);
when(p.hasPermission("admin.tp")).thenReturn(true);
when(p.hasPermission("admin")).thenReturn(false);
when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(p);
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(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
Location location = mock(Location.class);
@ -178,7 +177,7 @@ public class AdminTeleportCommandTest {
// Util
PowerMockito.mockStatic(Util.class, Mockito.RETURNS_MOCKS);
when(Util.getUUID(anyString())).thenCallRealMethod();
// Placeholder manager
when(plugin.getPlaceholdersManager()).thenReturn(phm);
}
@ -215,16 +214,16 @@ public class AdminTeleportCommandTest {
@Test
public void testExecuteUserStringListOfStringUnknownTarget() {
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"));
}
@Test
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);
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"));
}
@ -233,12 +232,12 @@ public class AdminTeleportCommandTest {
*/
@Test
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);
AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tp");
assertTrue(atc.canExecute(user, "tp", Collections.singletonList("tastybento")));
assertTrue(atc.execute(user, "tp", Collections.singletonList("tastybento")));
verify(user).getTranslation(eq("commands.admin.tp.manual"), eq("[location]"), eq("0 0 0"));
assertTrue(atc.canExecute(user, "tp", List.of("tastybento")));
assertTrue(atc.execute(user, "tp", List.of("tastybento")));
verify(user).getTranslation("commands.admin.tp.manual", "[location]", "0 0 0");
}
/**
@ -247,56 +246,56 @@ public class AdminTeleportCommandTest {
@Test
public void testExecuteUserStringListOfStringKnownTargetHasIslandSpawnPoint() {
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);
AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tp");
assertTrue(atc.canExecute(user, "tp", Collections.singletonList("tastybento")));
assertTrue(atc.execute(user, "tp", Collections.singletonList("tastybento")));
verify(user).getTranslation(eq("commands.admin.tp.manual"), eq("[location]"), eq("0 0 0"));
assertTrue(atc.canExecute(user, "tp", List.of("tastybento")));
assertTrue(atc.execute(user, "tp", List.of("tastybento")));
verify(user).getTranslation("commands.admin.tp.manual", "[location]", "0 0 0");
}
@Test
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.inTeam(any(), any(UUID.class))).thenReturn(true);
AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tp");
assertTrue(atc.canExecute(user, "tp", Collections.singletonList("tastybento")));
assertTrue(atc.execute(user, "tp", Collections.singletonList("tastybento")));
assertTrue(atc.canExecute(user, "tp", List.of("tastybento")));
assertTrue(atc.execute(user, "tp", List.of("tastybento")));
verify(iwm, Mockito.never()).getNetherWorld(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
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);
AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tpnether");
assertTrue(atc.canExecute(user, "tpnether", Collections.singletonList("tastybento")));
assertTrue(atc.execute(user, "tpnether", Collections.singletonList("tastybento")));
assertTrue(atc.canExecute(user, "tpnether", List.of("tastybento")));
assertTrue(atc.execute(user, "tpnether", List.of("tastybento")));
verify(iwm).getNetherWorld(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
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);
AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tpend");
assertTrue(atc.canExecute(user, "tpend", Collections.singletonList("tastybento")));
assertTrue(atc.execute(user, "tpend", Collections.singletonList("tastybento")));
assertTrue(atc.canExecute(user, "tpend", List.of("tastybento")));
assertTrue(atc.execute(user, "tpend", List.of("tastybento")));
verify(iwm, Mockito.never()).getNetherWorld(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
public void testPermissionsNoRootPermission() {
when(p.hasPermission("admin.tp")).thenReturn(true);
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);
AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tpend");
assertTrue(atc.canExecute(user, "tpend", List.of("tastybento")));
@ -306,12 +305,12 @@ public class AdminTeleportCommandTest {
// Should fail
assertFalse(atc.execute(p, "tpend", list));
}
@Test
public void testPermissionsHasRootPermission() {
when(p.hasPermission("admin.tp")).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);
AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tpend");
assertTrue(atc.canExecute(user, "tpend", List.of("tastybento")));