Fixed code smells.

This commit is contained in:
tastybento 2018-08-16 15:52:23 -07:00
parent 471cb30a87
commit b80928184e
5 changed files with 21 additions and 31 deletions

View File

@ -39,9 +39,6 @@ public class BentoBox extends JavaPlugin {
private PlayersManager playersManager; private PlayersManager playersManager;
private IslandsManager islandsManager; private IslandsManager islandsManager;
// bStats
private BStats bStats;
// Managers // Managers
private CommandsManager commandsManager; private CommandsManager commandsManager;
private LocalesManager localesManager; private LocalesManager localesManager;
@ -89,7 +86,7 @@ public class BentoBox extends JavaPlugin {
headGetter = new HeadGetter(this); headGetter = new HeadGetter(this);
// Load metrics // Load metrics
bStats = new BStats(this); BStats bStats = new BStats(this);
// Load Notifier // Load Notifier
notifier = new Notifier(); notifier = new Notifier();

View File

@ -15,6 +15,7 @@ import org.bukkit.plugin.InvalidDescriptionException;
import org.bukkit.util.permissions.DefaultPermissions; import org.bukkit.util.permissions.DefaultPermissions;
import world.bentobox.bentobox.api.addons.AddonDescription.AddonDescriptionBuilder; import world.bentobox.bentobox.api.addons.AddonDescription.AddonDescriptionBuilder;
import world.bentobox.bentobox.api.addons.exception.InvalidAddonFormatException;
import world.bentobox.bentobox.api.addons.exception.InvalidAddonInheritException; import world.bentobox.bentobox.api.addons.exception.InvalidAddonInheritException;
import world.bentobox.bentobox.managers.AddonsManager; import world.bentobox.bentobox.managers.AddonsManager;
@ -43,7 +44,7 @@ public class AddonClassLoader extends URLClassLoader {
String mainClass = data.getString("main"); String mainClass = data.getString("main");
javaClass = Class.forName(mainClass, true, this); javaClass = Class.forName(mainClass, true, this);
if(mainClass.startsWith("world.bentobox.bentobox")){ if(mainClass.startsWith("world.bentobox.bentobox")){
throw new Exception("Packages declaration cannot start with 'world.bentobox.bentobox'"); throw new InvalidAddonFormatException("Packages declaration cannot start with 'world.bentobox.bentobox'");
} }
} catch (Exception e) { } catch (Exception e) {
throw new InvalidDescriptionException("Could not load '" + path.getName() + "' in folder '" + path.getParent() + "' - " + e.getMessage()); throw new InvalidDescriptionException("Could not load '" + path.getName() + "' in folder '" + path.getParent() + "' - " + e.getMessage());

View File

@ -2,6 +2,7 @@ package world.bentobox.bentobox.api.commands;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.LinkedList; import java.util.LinkedList;
@ -37,6 +38,8 @@ import world.bentobox.bentobox.util.Util;
*/ */
public abstract class CompositeCommand extends Command implements PluginIdentifiableCommand, BentoBoxCommand { public abstract class CompositeCommand extends Command implements PluginIdentifiableCommand, BentoBoxCommand {
private static final String COMMANDS = "commands.";
private final BentoBox plugin; private final BentoBox plugin;
/** /**
@ -127,8 +130,8 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
plugin.getCommandsManager().registerCommand(this); plugin.getCommandsManager().registerCommand(this);
} }
// Default references to description and parameters // Default references to description and parameters
setDescription("commands." + label + ".description"); setDescription(COMMANDS + label + ".description");
setParametersHelp("commands." + label + ".parameters"); setParametersHelp(COMMANDS + label + ".parameters");
setup(); setup();
if (!getSubCommand("help").isPresent() && !label.equals("help")) { if (!getSubCommand("help").isPresent() && !label.equals("help")) {
new DefaultHelpCommand(this); new DefaultHelpCommand(this);
@ -192,8 +195,8 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
p = p.getParent(); p = p.getParent();
index++; index++;
} }
setDescription("commands." + reference.toString() + ".description"); setDescription(COMMANDS + reference.toString() + ".description");
setParametersHelp("commands." + reference.toString() + ".parameters"); setParametersHelp(COMMANDS + reference.toString() + ".parameters");
setup(); setup();
// If this command does not define its own help class, then use the default help command // If this command does not define its own help class, then use the default help command
if (!getSubCommand("help").isPresent() && !label.equals("help")) { if (!getSubCommand("help").isPresent() && !label.equals("help")) {
@ -569,30 +572,19 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
} }
// Add any tab completion from the subcommand // Add any tab completion from the subcommand
options.addAll(cmd.tabComplete(User.getInstance(sender), alias, new LinkedList<>(Arrays.asList(args))).orElse(new ArrayList<>())); options.addAll(cmd.tabComplete(User.getInstance(sender), alias, new LinkedList<>(Arrays.asList(args))).orElse(new ArrayList<>()));
// Add any sub-commands automatically
if (cmd.hasSubCommands()) { if (cmd.hasSubCommands()) {
// Check if subcommands are visible to this sender options.addAll(getSubCommandLabels(sender, cmd));
for (CompositeCommand subCommand: cmd.getSubCommands().values()) {
if (sender instanceof Player) {
// Player
if (subCommand.getPermission().isEmpty() || sender.hasPermission(subCommand.getPermission()) || sender.isOp()) {
// Permission is okay
options.add(subCommand.getLabel());
}
} else {
// Console
if (!subCommand.onlyPlayer) {
// Not a player command
options.add(subCommand.getLabel());
}
}
}
} }
String lastArg = args.length != 0 ? args[args.length - 1] : ""; String lastArg = args.length != 0 ? args[args.length - 1] : "";
return Util.tabLimit(options, lastArg).stream().sorted().collect(Collectors.toList()); return Util.tabLimit(options, lastArg).stream().sorted().collect(Collectors.toList());
} }
private List<String> getSubCommandLabels(CommandSender sender, CompositeCommand cmd) {
return cmd.getSubCommands().values().stream().filter(c -> !c.isOnlyPlayer() || sender.isOp()
|| (sender instanceof Player && (c.getPermission().isEmpty() || sender.hasPermission(c.getPermission()))) )
.map(CompositeCommand::getLabel).collect(Collectors.toList());
}
/** /**
* Show help * Show help
* @param command - command that this help is for * @param command - command that this help is for

View File

@ -18,10 +18,11 @@ import world.bentobox.bentobox.api.user.User;
*/ */
public class AdminRangeDisplayCommand extends CompositeCommand { public class AdminRangeDisplayCommand extends CompositeCommand {
private static final String DISPLAY = "display";
private Map<User, Integer> display = new HashMap<>(); private Map<User, Integer> display = new HashMap<>();
public AdminRangeDisplayCommand(CompositeCommand parent) { public AdminRangeDisplayCommand(CompositeCommand parent) {
super(parent, "display", "show", "hide"); super(parent, DISPLAY, "show", "hide");
} }
@Override @Override
@ -40,7 +41,7 @@ public class AdminRangeDisplayCommand extends CompositeCommand {
if (!display.containsKey(user)) { if (!display.containsKey(user)) {
switch (label) { switch (label) {
case "display": case DISPLAY:
case "show": case "show":
showZones(user); showZones(user);
break; break;
@ -53,7 +54,7 @@ public class AdminRangeDisplayCommand extends CompositeCommand {
} }
} else { } else {
switch (label) { switch (label) {
case "display": case DISPLAY:
case "hide": case "hide":
hideZones(user); hideZones(user);
break; break;

View File

@ -136,7 +136,6 @@ public class FlatFileDatabaseConnector implements DatabaseConnector {
Files.delete(commentedFile.toPath()); Files.delete(commentedFile.toPath());
} catch (IOException e1) { } catch (IOException e1) {
plugin.logError("Could not comment config file " + file.getName() + " " + e1.getMessage()); plugin.logError("Could not comment config file " + file.getName() + " " + e1.getMessage());
e1.printStackTrace();
} }
} }