Code cleanup and bug fixing.

This just cleans up a lot of code that could cause confusion or was
actually wrong.
This commit is contained in:
Tastybento 2018-02-06 23:04:09 -08:00
parent 1db21c10da
commit e5a87ccf7a
7 changed files with 35 additions and 48 deletions

View File

@ -95,16 +95,16 @@ public abstract class Addon implements AddonInterface {
private FileConfiguration loadYamlFile(String file) {
File yamlFile = new File(dataFolder, file);
YamlConfiguration config = null;
YamlConfiguration yamlConfig = null;
if (yamlFile.exists()) {
try {
config = new YamlConfiguration();
config.load(yamlFile);
yamlConfig = new YamlConfiguration();
yamlConfig.load(yamlFile);
} catch (Exception e) {
Bukkit.getLogger().severe("Could not load YAML file: " + file);
}
}
return config;
return yamlConfig;
}
/**
@ -158,9 +158,9 @@ public abstract class Addon implements AddonInterface {
jarResource = jarResource.replace('\\', '/');
try (JarFile jar = new JarFile(file)) {
JarEntry config = jar.getJarEntry(jarResource);
if (config != null) {
try (InputStream in = jar.getInputStream(config)) {
JarEntry jarConfig = jar.getJarEntry(jarResource);
if (jarConfig != null) {
try (InputStream in = jar.getInputStream(jarConfig)) {
if (in == null) {
jar.close();
throw new IllegalArgumentException("The embedded resource '" + jarResource + "' cannot be found in " + jar.getName());

View File

@ -1,6 +1,5 @@
package us.tastybento.bskyblock.api.addons;
import java.io.BufferedReader;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
@ -25,7 +24,7 @@ public class AddonClassLoader extends URLClassLoader {
private Addon addon;
private AddonsManager loader;
public AddonClassLoader(AddonsManager addonsManager, Map<String, String>data, File path, BufferedReader reader, ClassLoader parent)
public AddonClassLoader(AddonsManager addonsManager, Map<String, String>data, File path, ClassLoader parent)
throws InvalidAddonInheritException,
MalformedURLException,
InvalidAddonFormatException,

View File

@ -189,39 +189,22 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
private CompositeCommand getCommandFromArgs(String[] args) {
CompositeCommand subCommand = this;
// Run through any arguments
if (DEBUG) {
Bukkit.getLogger().info("DEBUG: Running through args: " + Arrays.asList(args).toString());
}
if (args.length > 0) {
for (int i = 0; i < args.length; i++) {
if (DEBUG) {
Bukkit.getLogger().info("DEBUG: Argument " + i);
}
// get the subcommand corresponding to the arg
if (subCommand.hasSubCommmands()) {
if (DEBUG) {
Bukkit.getLogger().info("DEBUG: This command has subcommands");
}
Optional<CompositeCommand> sub = subCommand.getSubCommand(args[i]);
if (!sub.isPresent()) {
return subCommand;
}
// Step down one
subCommand = sub.orElse(subCommand);
if (DEBUG) {
Bukkit.getLogger().info("DEBUG: Moved to " + subCommand.getLabel());
}
// Set the label
subCommand.setLabel(args[i]);
} else {
// We are at the end of the walk
if (DEBUG) {
Bukkit.getLogger().info("DEBUG: End of traversal");
}
for (int i = 0; i < args.length; i++) {
// get the subcommand corresponding to the arg
if (subCommand.hasSubCommmands()) {
Optional<CompositeCommand> sub = subCommand.getSubCommand(args[i]);
if (!sub.isPresent()) {
return subCommand;
}
// else continue the loop
// Step down one
subCommand = sub.orElse(subCommand);
// Set the label
subCommand.setLabel(args[i]);
} else {
// We are at the end of the walk
return subCommand;
}
// else continue the loop
}
return subCommand;
}
@ -477,7 +460,7 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
}
return Util.tabLimit(options, lastArg);
}
/**
* Show help
* @param command

View File

@ -16,6 +16,10 @@ public class DefaultHelpCommand extends CompositeCommand {
// TODO: make this a setting
private static final int MAX_DEPTH = 2;
private static final String USAGE_PLACEHOLDER = "[usage]";
private static final String PARAMS_PLACEHOLDER = "[parameters]";
private static final String DESC_PLACEHOLDER = "[description]";
private static final String HELP_SYNTAX_REF = "commands.help.syntax";
public DefaultHelpCommand(CompositeCommand parent) {
super(parent, "help");
@ -39,7 +43,7 @@ public class DefaultHelpCommand extends CompositeCommand {
String usage = user.getTranslation(parent.getUsage());
String params = user.getTranslation("commands.help.parameters");
String desc = user.getTranslation("commands.help.description");
user.sendMessage("commands.help.syntax", "[usage]", usage, "[parameters]", params, "[description]", desc);
user.sendMessage(HELP_SYNTAX_REF, USAGE_PLACEHOLDER, usage, PARAMS_PLACEHOLDER, params, DESC_PLACEHOLDER, desc);
return true;
}
}
@ -57,14 +61,14 @@ public class DefaultHelpCommand extends CompositeCommand {
if (user.isPlayer()) {
// Player. Check perms
if (user.hasPermission(parent.getPermission())) {
user.sendMessage("commands.help.syntax", "[usage]", usage, "[parameters]", params, "[description]", desc);
user.sendMessage(HELP_SYNTAX_REF, USAGE_PLACEHOLDER, usage, PARAMS_PLACEHOLDER, params, DESC_PLACEHOLDER, desc);
} else {
// No permission, nothing to see here. If you don't have permission, you cannot see any sub commands
return true;
}
} else if (!parent.isOnlyPlayer()) {
// Console. Only show if it is a console command
user.sendMessage("commands.help.syntax", "[usage]", usage, "[parameters]", params, "[description]", desc);
user.sendMessage(HELP_SYNTAX_REF, USAGE_PLACEHOLDER, usage, PARAMS_PLACEHOLDER, params, DESC_PLACEHOLDER, desc);
}
}
// Increment the depth

View File

@ -19,6 +19,8 @@ import us.tastybento.bskyblock.util.Util;
public class IslandTeamInviteCommand extends AbstractIslandTeamCommand {
private static final String NAME_PLACEHOLDER = "[name]";
public IslandTeamInviteCommand(IslandTeamCommand islandTeamCommand) {
super(islandTeamCommand, "invite");
}
@ -47,7 +49,7 @@ public class IslandTeamInviteCommand extends AbstractIslandTeamCommand {
// Invite label with no name, i.e., /island invite - tells the player who has invited them so far
if (inviteList.containsKey(playerUUID)) {
OfflinePlayer inviter = getPlugin().getServer().getOfflinePlayer(inviteList.get(playerUUID));
user.sendMessage("commands.island.team.invite.name-has-invited-you", "[name]", inviter.getName());
user.sendMessage("commands.island.team.invite.name-has-invited-you", NAME_PLACEHOLDER, inviter.getName());
return true;
}
// Show help
@ -124,9 +126,9 @@ public class IslandTeamInviteCommand extends AbstractIslandTeamCommand {
// Put the invited player (key) onto the list with inviter (value)
// If someone else has invited a player, then this invite will overwrite the previous invite!
inviteList.put(invitedPlayerUUID, playerUUID);
user.sendMessage("commands.island.team.invite.invitation-sent", "[name]", args.get(0));
user.sendMessage("commands.island.team.invite.invitation-sent", NAME_PLACEHOLDER, args.get(0));
// Send message to online player
invitedPlayer.sendMessage("commands.island.team.invite.name-has-invited-you", "[name]", user.getName());
invitedPlayer.sendMessage("commands.island.team.invite.name-has-invited-you", NAME_PLACEHOLDER, user.getName());
invitedPlayer.sendMessage("commands.island.team.invite.to-accept-or-reject", "[label]", getLabel());
if (getPlayers().hasIsland(invitedPlayer.getUniqueId())) {
invitedPlayer.sendMessage("commands.island.team.invite.you-will-lose-your-island");

View File

@ -1,6 +1,5 @@
package us.tastybento.bskyblock.commands.island.teams;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

View File

@ -116,7 +116,7 @@ public final class AddonsManager {
Map<String, String> data = this.data(reader);
// Load the addon
AddonClassLoader loader = new AddonClassLoader(this, data, f, reader, this.getClass().getClassLoader());
AddonClassLoader loader = new AddonClassLoader(this, data, f, this.getClass().getClassLoader());
// Add to the list of loaders
this.loader.add(loader);