Skip to content

Commit

Permalink
Minor code smell reduction
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Jul 22, 2023
1 parent fec43ad commit 3e1dc81
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private void checkClickedBlock(Event e, Player player, Block block)
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,20 @@ public void onBlockBreak(final BlockBreakEvent e) {
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);
}
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private void respond(Cancellable e, Entity damager, Entity hurtEntity, Flag flag
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)) {
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/world/bentobox/bentobox/util/ItemParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static ItemStack parse(@Nullable String text, @Nullable ItemStack default
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()) {
Expand All @@ -75,7 +75,7 @@ public static ItemStack parse(@Nullable String text, @Nullable ItemStack default
int j = 0;

for (String field : part) {
if (!field.matches("(CMD-[0-9]*)")) {
if (!field.matches("(CMD-\\d*)")) {
copyParts[j++] = field;
}
}
Expand Down Expand Up @@ -182,11 +182,11 @@ private static ItemStack parseItemDurabilityAndQuantity(String[] part) {
/**
* 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.
Expand Down Expand Up @@ -261,13 +261,13 @@ private static ItemStack parseBanner(String[] part) {
/**
* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,6 @@ void returnAndTeleport(Player player) {
{
// 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()))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
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;
Expand Down Expand Up @@ -103,7 +102,7 @@ public void setUp() throws Exception {
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");
Expand Down Expand Up @@ -153,7 +152,7 @@ public void setUp() throws Exception {
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);
Expand All @@ -178,7 +177,7 @@ public void setUp() throws Exception {
// Util
PowerMockito.mockStatic(Util.class, Mockito.RETURNS_MOCKS);
when(Util.getUUID(anyString())).thenCallRealMethod();

// Placeholder manager
when(plugin.getPlaceholdersManager()).thenReturn(phm);
}
Expand Down Expand Up @@ -215,16 +214,16 @@ public void testExecuteUserStringListOfStringEmptyArgs() {
@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"));
}

Expand All @@ -233,12 +232,12 @@ public void testExecuteUserStringListOfStringKnownTargetNoIsland() {
*/
@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");
}

/**
Expand All @@ -247,56 +246,56 @@ public void testExecuteUserStringListOfStringKnownTargetHasIsland() {
@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")));
Expand All @@ -306,12 +305,12 @@ public void testPermissionsNoRootPermission() {
// 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")));
Expand Down

0 comments on commit 3e1dc81

Please sign in to comment.