Fixes code smells.

This commit is contained in:
tastybento 2019-06-23 19:13:00 -07:00
parent 9798edebd5
commit d6248a3577
4 changed files with 42 additions and 37 deletions

View File

@ -4,6 +4,7 @@ import java.util.List;
import java.util.UUID;
import org.apache.commons.lang.math.NumberUtils;
import org.eclipse.jdt.annotation.NonNull;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.commands.ConfirmableCommand;
@ -14,6 +15,7 @@ import world.bentobox.bentobox.database.objects.Island;
public class AdminSwitchtoCommand extends ConfirmableCommand {
private UUID targetUUID;
private @NonNull List<Island> islands;
/**
* Switch player's island to the numbered one in trash
@ -45,37 +47,35 @@ public class AdminSwitchtoCommand extends ConfirmableCommand {
user.sendMessage("general.errors.unknown-player", TextVariables.NAME, args.get(0));
return false;
}
// Check island number
islands = getIslands().getQuarantinedIslandByUser(getWorld(), targetUUID);
if (islands.isEmpty()) {
user.sendMessage("commands.admin.trash.no-islands-in-trash");
return false;
}
return true;
}
@Override
public boolean execute(User user, String label, List<String> args) {
// Check island number
List<Island> islands = getIslands().getQuarantinedIslandByUser(getWorld(), targetUUID);
if (islands.isEmpty()) {
user.sendMessage("commands.admin.trash.no-islands-in-trash");
return false;
} else {
// Check number
if (NumberUtils.isDigits(args.get(1))) {
try {
Integer n = Integer.valueOf(args.get(1));
if (n < 1 || n > islands.size()) {
user.sendMessage("commands.admin.switchto.out-of-range", TextVariables.NUMBER, String.valueOf(islands.size()), TextVariables.LABEL, getTopLabel());
return false;
}
this.askConfirmation(user, () -> {
if (getIslands().switchIsland(getWorld(), targetUUID, islands.get(n -1))) {
user.sendMessage("general.success");
} else {
user.sendMessage("commands.admin.switchto.cannot-switch");
}
});
return true;
} catch (Exception e) {
showHelp(this, user);
if (NumberUtils.isDigits(args.get(1))) {
try {
Integer n = Integer.valueOf(args.get(1));
if (n < 1 || n > islands.size()) {
user.sendMessage("commands.admin.switchto.out-of-range", TextVariables.NUMBER, String.valueOf(islands.size()), TextVariables.LABEL, getTopLabel());
return false;
}
this.askConfirmation(user, () -> {
if (getIslands().switchIsland(getWorld(), targetUUID, islands.get(n -1))) {
user.sendMessage("general.success");
} else {
user.sendMessage("commands.admin.switchto.cannot-switch");
}
});
return true;
} catch (Exception e) {
showHelp(this, user);
return false;
}
}
return true;

View File

@ -29,19 +29,22 @@ import world.bentobox.bentobox.lists.Flags;
*/
public class BlockInteractionListener extends FlagListener {
private static final String FURNACE = "FURNACE";
private static final String CRAFTING = "CRAFTING";
/**
* These cover materials in another server version.
* This avoids run time errors due to unknown enum values, at the expense of a string comparison
*/
private final Map<String, String> stringFlags = ImmutableMap.<String, String>builder()
.put("CAMPFIRE", "FURNACE")
.put("CARTOGRAPHY_TABLE", "CRAFTING")
.put("GRINDSTONE", "CRAFTING")
.put("CAMPFIRE", FURNACE)
.put("CARTOGRAPHY_TABLE", CRAFTING)
.put("GRINDSTONE", CRAFTING)
.put("LECTERN", "BREAK_BLOCKS")
.put("LOOM", "CRAFTING")
.put("STONECUTTER", "CRAFTING")
.put("SMOKER", "FURNACE")
.put("BLAST_FURNACE", "FURNACE")
.put("LOOM", CRAFTING)
.put("STONECUTTER", CRAFTING)
.put("SMOKER", FURNACE)
.put("BLAST_FURNACE", FURNACE)
.put("COMPOSTER", "CONTAINER")
.build();

View File

@ -54,6 +54,7 @@ public class BlueprintsManager {
public static final String DEFAULT_BUNDLE_NAME = "default";
public static final @NonNull String FOLDER_NAME = "blueprints";
private static final String FOR = "' for ";
/**
* Map of blueprint bundles to game mode addon.
@ -179,7 +180,7 @@ public class BlueprintsManager {
try {
BlueprintBundle bb = gson.fromJson(new FileReader(file), BlueprintBundle.class);
blueprintBundles.get(addon).add(bb);
plugin.log("Loaded Blueprint Bundle '" + bb.getUniqueId() + "' for " + addon.getDescription().getName());
plugin.log("Loaded Blueprint Bundle '" + bb.getUniqueId() + FOR + addon.getDescription().getName());
loaded = true;
} catch (Exception e) {
plugin.logError("Could not load blueprint bundle " + file.getName() + " " + e.getMessage());
@ -253,7 +254,7 @@ public class BlueprintsManager {
bp.setName(fileName);
}
blueprints.get(addon).add(bp);
plugin.log("Loaded blueprint '" + bp.getName() + "' for " + addon.getDescription().getName());
plugin.log("Loaded blueprint '" + bp.getName() + FOR + addon.getDescription().getName());
} catch (Exception e) {
plugin.logError("Could not load blueprint " + fileName + " " + e.getMessage());
plugin.logStacktrace(e);
@ -271,7 +272,7 @@ public class BlueprintsManager {
blueprints.putIfAbsent(addon, new ArrayList<>());
blueprints.get(addon).removeIf(b -> b.getName().equals(bp.getName()));
blueprints.get(addon).add(bp);
plugin.log("Added blueprint '" + bp.getName() + "' for " + addon.getDescription().getName());
plugin.log("Added blueprint '" + bp.getName() + FOR + addon.getDescription().getName());
}
/**

View File

@ -49,6 +49,7 @@ public class BlueprintManagementPanel {
private static final int MAX_WORLD_SLOT = 9;
private static final int MIN_WORLD_SLOT = 0;
public static final int MAX_BP_SLOT = 35;
private static final String INSTRUCTION = "instruction";
private Entry<Integer, Blueprint> selected;
private Map<Integer, Blueprint> blueprints = new HashMap<>();
private final User user;
@ -58,9 +59,9 @@ public class BlueprintManagementPanel {
this.plugin = plugin;
this.user = user;
this.addon = addon;
normalBlueprint = new Blueprint().setIcon(Material.GREEN_STAINED_GLASS_PANE).setName(t("normal")).setDescription(t("instruction"));
netherBlueprint = new Blueprint().setIcon(Material.RED_STAINED_GLASS_PANE).setName(t("nether")).setDescription(t("instruction"));
endBlueprint = new Blueprint().setIcon(Material.YELLOW_STAINED_GLASS_PANE).setName(t("end")).setDescription(t("instruction"));
normalBlueprint = new Blueprint().setIcon(Material.GREEN_STAINED_GLASS_PANE).setName(t("normal")).setDescription(t(INSTRUCTION));
netherBlueprint = new Blueprint().setIcon(Material.RED_STAINED_GLASS_PANE).setName(t("nether")).setDescription(t(INSTRUCTION));
endBlueprint = new Blueprint().setIcon(Material.YELLOW_STAINED_GLASS_PANE).setName(t("end")).setDescription(t(INSTRUCTION));
slotToEnvironment = ImmutableMap.of(3, World.Environment.NORMAL, 5, World.Environment.NETHER, 7, World.Environment.THE_END);
environmentToBlueprint = ImmutableMap.of(World.Environment.NORMAL, normalBlueprint, World.Environment.NETHER, netherBlueprint, World.Environment.THE_END, endBlueprint);
}