Code smell cleanup

This commit is contained in:
tastybento 2019-07-27 17:19:04 -07:00
parent c3e8f9e3be
commit 65aee40533
5 changed files with 121 additions and 87 deletions

View File

@ -3,6 +3,8 @@ package world.bentobox.bentobox.api.commands.island;
import java.io.IOException;
import java.util.List;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.events.island.IslandEvent.Reason;
@ -19,8 +21,6 @@ import world.bentobox.bentobox.panels.IslandCreationPanel;
*/
public class IslandCreateCommand extends CompositeCommand {
private Island island;
/**
* Command to create an island
* @param islandCommand - parent command
@ -40,7 +40,8 @@ public class IslandCreateCommand extends CompositeCommand {
@Override
public boolean canExecute(User user, String label, List<String> args) {
// Check if the island is reserved
island = getIslands().getIsland(getWorld(), user);
@Nullable
Island island = getIslands().getIsland(getWorld(), user);
if (island != null) {
// Reserved islands can be made
if (island.isReserved()) {

View File

@ -37,9 +37,9 @@ public class IslandTeamInviteCommand extends CompositeCommand {
setConfigurableRankCommand();
}
@Override
public boolean execute(User user, String label, List<String> args) {
UUID playerUUID = user.getUniqueId();
public boolean canExecute(User user, String label, List<String> args) {
// Player issuing the command must have an island or be in a team
if (!getIslands().inTeam(getWorld(), user.getUniqueId()) && !getIslands().hasIsland(getWorld(), user.getUniqueId())) {
user.sendMessage("general.errors.no-island");
@ -51,6 +51,7 @@ public class IslandTeamInviteCommand extends CompositeCommand {
user.sendMessage("general.errors.no-permission");
return false;
}
UUID playerUUID = user.getUniqueId();
if (args.isEmpty() || args.size() > 1) {
// Invite label with no name, i.e., /island invite - tells the player who has invited them so far
if (inviteList.containsKey(playerUUID)) {
@ -61,34 +62,41 @@ public class IslandTeamInviteCommand extends CompositeCommand {
// Show help
showHelp(this, user);
return false;
} else {
// Only online players can be invited
UUID invitedPlayerUUID = getPlayers().getUUID(args.get(0));
if (invitedPlayerUUID == null) {
user.sendMessage("general.errors.unknown-player", TextVariables.NAME, args.get(0));
return false;
}
User invitedPlayer = User.getInstance(invitedPlayerUUID);
if (!invitedPlayer.isOnline()) {
user.sendMessage("general.errors.offline-player");
return false;
}
// Player cannot invite themselves
if (playerUUID.equals(invitedPlayerUUID)) {
user.sendMessage("commands.island.team.invite.errors.cannot-invite-self");
return false;
}
// Check cool down
if (getSettings().getInviteCooldown() > 0 && checkCooldown(user, getIslands().getIsland(getWorld(), user).getUniqueId(), invitedPlayerUUID.toString())) {
return false;
}
// Player cannot invite someone already on a team
if (getIslands().inTeam(getWorld(), invitedPlayerUUID)) {
user.sendMessage("commands.island.team.invite.errors.already-on-team");
return false;
}
return invite(user,invitedPlayer);
}
return true;
}
@Override
public boolean execute(User user, String label, List<String> args) {
UUID playerUUID = user.getUniqueId();
// Only online players can be invited
UUID invitedPlayerUUID = getPlayers().getUUID(args.get(0));
if (invitedPlayerUUID == null) {
user.sendMessage("general.errors.unknown-player", TextVariables.NAME, args.get(0));
return false;
}
User invitedPlayer = User.getInstance(invitedPlayerUUID);
if (!invitedPlayer.isOnline()) {
user.sendMessage("general.errors.offline-player");
return false;
}
// Player cannot invite themselves
if (playerUUID.equals(invitedPlayerUUID)) {
user.sendMessage("commands.island.team.invite.errors.cannot-invite-self");
return false;
}
// Check cool down
if (getSettings().getInviteCooldown() > 0 && checkCooldown(user, getIslands().getIsland(getWorld(), user).getUniqueId(), invitedPlayerUUID.toString())) {
return false;
}
// Player cannot invite someone already on a team
if (getIslands().inTeam(getWorld(), invitedPlayerUUID)) {
user.sendMessage("commands.island.team.invite.errors.already-on-team");
return false;
}
return invite(user,invitedPlayer);
}
private boolean invite(User user, User invitedPlayer) {

View File

@ -64,6 +64,7 @@ public class BlueprintClipboard {
private Map<Vector, List<BlueprintEntity>> bpEntities = new LinkedHashMap<>();
private Map<Vector, BlueprintBlock> bpAttachable = new LinkedHashMap<>();
private Map<Vector, BlueprintBlock> bpBlocks = new LinkedHashMap<>();
private BentoBox plugin = BentoBox.getInstance();
/**
* Create a clipboard for blueprint
@ -115,49 +116,49 @@ public class BlueprintClipboard {
blueprint.setySize((int)toCopy.getHeight());
blueprint.setzSize((int)toCopy.getWidthZ());
BentoBox plugin = BentoBox.getInstance();
final int speed = plugin.getSettings().getPasteSpeed();
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
final List<Vector> vectorsToCopy = getVectors(toCopy);
copying = false;
copyTask = Bukkit.getScheduler().runTaskTimer(plugin, () -> {
if (copying) {
return;
}
copying = true;
vectorsToCopy.stream().skip(index).limit(speed).forEach(v -> {
List<LivingEntity> ents = world.getLivingEntities().stream()
.filter(Objects::nonNull)
.filter(e -> !(e instanceof Player))
.filter(e -> new Vector(Math.rint(e.getLocation().getX()),
Math.rint(e.getLocation().getY()),
Math.rint(e.getLocation().getZ())).equals(v))
.collect(Collectors.toList());
if (copyBlock(v.toLocation(world), origin, copyAir, ents)) {
count++;
}
});
index += speed;
int percent = (int)(index * 100 / (double)vectorsToCopy.size());
if (percent != lastPercentage && percent % 10 == 0) {
user.sendMessage("commands.admin.blueprint.copied-percent", TextVariables.NUMBER, String.valueOf(percent));
lastPercentage = percent;
}
if (index > vectorsToCopy.size()) {
copyTask.cancel();
blueprint.setAttached(bpAttachable);
blueprint.setBlocks(bpBlocks);
blueprint.setEntities(bpEntities);
user.sendMessage("general.success");
user.sendMessage("commands.admin.blueprint.copied-blocks", TextVariables.NUMBER, String.valueOf(count));
}
copying = false;
}, 0L, 1L);
});
int speed = plugin.getSettings().getPasteSpeed();
List<Vector> vectorsToCopy = getVectors(toCopy);
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> copyAsync(world, user, vectorsToCopy, speed, copyAir));
return true;
}
private void copyAsync(World world, User user, List<Vector> vectorsToCopy, int speed, boolean copyAir) {
copying = false;
copyTask = Bukkit.getScheduler().runTaskTimer(plugin, () -> {
if (copying) {
return;
}
copying = true;
vectorsToCopy.stream().skip(index).limit(speed).forEach(v -> {
List<LivingEntity> ents = world.getLivingEntities().stream()
.filter(Objects::nonNull)
.filter(e -> !(e instanceof Player))
.filter(e -> new Vector(Math.rint(e.getLocation().getX()),
Math.rint(e.getLocation().getY()),
Math.rint(e.getLocation().getZ())).equals(v))
.collect(Collectors.toList());
if (copyBlock(v.toLocation(world), origin, copyAir, ents)) {
count++;
}
});
index += speed;
int percent = (int)(index * 100 / (double)vectorsToCopy.size());
if (percent != lastPercentage && percent % 10 == 0) {
user.sendMessage("commands.admin.blueprint.copied-percent", TextVariables.NUMBER, String.valueOf(percent));
lastPercentage = percent;
}
if (index > vectorsToCopy.size()) {
copyTask.cancel();
blueprint.setAttached(bpAttachable);
blueprint.setBlocks(bpBlocks);
blueprint.setEntities(bpEntities);
user.sendMessage("general.success");
user.sendMessage("commands.admin.blueprint.copied-blocks", TextVariables.NUMBER, String.valueOf(count));
}
copying = false;
}, 0L, 1L);
}
/**
* Get all the x,y,z coords that must be copied
* @param b - bounding box

View File

@ -1,6 +1,3 @@
/**
*
*/
package world.bentobox.bentobox.api.commands.island.team;
import static org.junit.Assert.assertEquals;
@ -8,6 +5,7 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.any;
import java.util.Collections;
import java.util.HashMap;
@ -23,6 +21,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@ -33,6 +32,9 @@ import com.google.common.collect.HashBiMap;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.events.IslandBaseEvent;
import world.bentobox.bentobox.api.events.team.TeamEvent;
import world.bentobox.bentobox.api.events.team.TeamEvent.TeamEventBuilder;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.CommandsManager;
@ -47,7 +49,7 @@ import world.bentobox.bentobox.managers.RanksManager;
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class, User.class })
@PrepareForTest({Bukkit.class, BentoBox.class, User.class, TeamEvent.class })
public class IslandTeamInviteAcceptCommandTest {
private IslandTeamCommand ic;
@ -216,6 +218,28 @@ public class IslandTeamInviteAcceptCommandTest {
Mockito.verify(pim).callEvent(Mockito.any());
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteAcceptCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testCanExecuteEventBlocked() {
biMap.put(uuid, notUUID);
when(im.inTeam(Mockito.any(), Mockito.any())).thenReturn(false);
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true);
// Block event
PowerMockito.mockStatic(TeamEvent.class);
TeamEventBuilder teb = mock(TeamEventBuilder.class);
when(teb.island(any())).thenReturn(teb);
when(teb.involvedPlayer(any())).thenReturn(teb);
when(teb.reason(any())).thenReturn(teb);
IslandBaseEvent ibe = mock(IslandBaseEvent.class);
when(ibe.isCancelled()).thenReturn(true);
when(teb.build()).thenReturn(ibe);
when(TeamEvent.builder()).thenReturn(teb);
assertFalse(c.canExecute(user, "accept", Collections.emptyList()));
Mockito.verify(pim).callEvent(Mockito.any());
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteAcceptCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/

View File

@ -127,41 +127,41 @@ public class IslandTeamInviteCommandTest {
}
/**
* Test method for .
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteCommand#canExecute(User, String, java.util.List)}.
*/
@Test
public void testExecuteNoIsland() {
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(false);
when(im.inTeam(Mockito.any(), Mockito.any(UUID.class))).thenReturn(false);
IslandTeamInviteCommand itl = new IslandTeamInviteCommand(ic);
assertFalse(itl.execute(user, itl.getLabel(), new ArrayList<>()));
assertFalse(itl.canExecute(user, itl.getLabel(), new ArrayList<>()));
Mockito.verify(user).sendMessage(Mockito.eq("general.errors.no-island"));
}
/**
* Test method for .
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteCommand#canExecute(User, String, java.util.List)}.
*/
@Test
public void testExecuteLowRank() {
when(island.getRank(Mockito.any())).thenReturn(RanksManager.MEMBER_RANK);
when(island.getRankCommand(anyString())).thenReturn(RanksManager.OWNER_RANK);
IslandTeamInviteCommand itl = new IslandTeamInviteCommand(ic);
assertFalse(itl.execute(user, itl.getLabel(), new ArrayList<>()));
assertFalse(itl.canExecute(user, itl.getLabel(), new ArrayList<>()));
Mockito.verify(user).sendMessage(Mockito.eq("general.errors.no-permission"));
}
/**
* Test method for .
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteCommand#canExecute(User, String, java.util.List)}.
*/
@Test
public void testExecuteNoTarget() {
IslandTeamInviteCommand itl = new IslandTeamInviteCommand(ic);
assertFalse(itl.execute(user, itl.getLabel(), new ArrayList<>()));
assertFalse(itl.canExecute(user, itl.getLabel(), new ArrayList<>()));
// Show help
}
/**
* Test method for .
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteCommand#execute(User, String, java.util.List)}.
*/
@Test
public void testExecuteUnknownPlayer() {
@ -174,7 +174,7 @@ public class IslandTeamInviteCommandTest {
/**
* Test method for .
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteCommand#execute(User, String, java.util.List)}.
*/
@Test
public void testExecuteOfflinePlayer() {
@ -189,7 +189,7 @@ public class IslandTeamInviteCommandTest {
}
/**
* Test method for .
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteCommand#execute(User, String, java.util.List)}.
*/
@Test
public void testExecuteSamePlayer() {
@ -205,7 +205,7 @@ public class IslandTeamInviteCommandTest {
/**
* Test method for .
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteCommand#execute(User, String, java.util.List)}.
*/
@Test
public void testExecuteDifferentPlayerInTeam() {
@ -221,7 +221,7 @@ public class IslandTeamInviteCommandTest {
}
/**
* Test method for .
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteCommand#execute(User, String, java.util.List)}.
*/
@Test
public void testExecuteCoolDownActive() {