Bug fixes to avoid NPE's and Optional gets without isPresent

This commit is contained in:
tastybento 2018-02-06 13:21:08 -08:00
parent ace6294e23
commit 1ddf706c68
11 changed files with 52 additions and 59 deletions

View File

@ -157,7 +157,7 @@ public class Metrics {
}
});
}
}, 1000*60*5, 1000*60*30);
}, 1000*60*5L, 1000*60*30L);
// Submit the data every 30 minutes, first time after 5 minutes to give other plugins enough time to start
// WARNING: Changing the frequency has no effect but your plugin WILL be blocked/deleted!
// WARNING: Just don't do it!
@ -283,7 +283,7 @@ public class Metrics {
// Compress the data to save bandwidth
byte[] compressedData = compress(data.toString());
if (compressedData == null) throw new Exception();
// Add headers
connection.setRequestMethod("POST");
connection.addRequestProperty("Accept", "application/json");

View File

@ -157,36 +157,32 @@ public abstract class Addon implements AddonInterface {
}
jarResource = jarResource.replace('\\', '/');
InputStream in = null;
try {
JarFile jar = new JarFile(file);
try (JarFile jar = new JarFile(file)) {
JarEntry config = jar.getJarEntry(jarResource);
if (config != null) {
in = jar.getInputStream(config);
try (InputStream in = jar.getInputStream(config)) {
if (in == null) {
jar.close();
throw new IllegalArgumentException("The embedded resource '" + jarResource + "' cannot be found in " + jar.getName());
}
// There are two options, use the path of the resource or not
File outFile = new File(destinationFolder, jarResource);
if (noPath) {
outFile = new File(destinationFolder, outFile.getName());
}
// Make any dirs that need to be made
outFile.getParentFile().mkdirs();
if (DEBUG) {
Bukkit.getLogger().info("DEBUG: outFile = " + outFile.getAbsolutePath());
Bukkit.getLogger().info("DEBUG: outFile name = " + outFile.getName());
}
if (!outFile.exists() || replace) {
java.nio.file.Files.copy(in, outFile.toPath());
}
}
}
if (in == null) {
jar.close();
throw new IllegalArgumentException("The embedded resource '" + jarResource + "' cannot be found in " + jar.getName());
}
// There are two options, use the path of the resource or not
File outFile = new File(destinationFolder, jarResource);
if (noPath) {
outFile = new File(destinationFolder, outFile.getName());
}
// Make any dirs that need to be made
outFile.getParentFile().mkdirs();
if (DEBUG) {
Bukkit.getLogger().info("DEBUG: outFile = " + outFile.getAbsolutePath());
Bukkit.getLogger().info("DEBUG: outFile name = " + outFile.getName());
}
if (!outFile.exists() || replace) {
java.nio.file.Files.copy(in, outFile.toPath());
}
in.close();
jar.close();
} catch (IOException ex) {
ex.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

View File

@ -25,13 +25,17 @@ public class AddonClassLoader extends URLClassLoader {
public Addon addon;
private AddonsManager loader;
public AddonClassLoader(AddonsManager addonsManager, Map<String, String>data, File path, BufferedReader reader, ClassLoader parent) throws InvalidAddonInheritException, MalformedURLException, InvalidAddonFormatException, InvalidDescriptionException {
public AddonClassLoader(AddonsManager addonsManager, Map<String, String>data, File path, BufferedReader reader, ClassLoader parent)
throws InvalidAddonInheritException,
MalformedURLException,
InvalidAddonFormatException,
InvalidDescriptionException,
InstantiationException,
IllegalAccessException {
super(new URL[]{path.toURI().toURL()}, parent);
this.loader = addonsManager;
Addon addon = null;
Class<?> javaClass = null;
try {
//Bukkit.getLogger().info("data " + data.get("main"));
@ -51,15 +55,11 @@ public class AddonClassLoader extends URLClassLoader {
Class<? extends Addon> addonClass;
try{
addonClass = javaClass.asSubclass(Addon.class);
}catch(ClassCastException e){
} catch(ClassCastException e){
throw new InvalidAddonInheritException("Main class doesn't not extends super class 'Addon'");
}
try {
addon = addonClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
Addon addon = addonClass.newInstance();
addon.setDescription(this.asDescription(data));

View File

@ -195,8 +195,8 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
// get the subcommand corresponding to the arg
if (subCommand.hasSubCommmands()) {
if (DEBUG)
Bukkit.getLogger().info("DEBUG: This command has subcommands");
if (subCommand.hasSubCommand(args[i])) {
Bukkit.getLogger().info("DEBUG: This command has subcommands");
if (subCommand.getSubCommand(args[i]).isPresent()) {
// Step down one
subCommand = subCommand.getSubCommand(args[i]).get();
if (DEBUG)
@ -337,7 +337,7 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
* @param subCommand
* @return true if this command has this sub command
*/
private boolean hasSubCommand(String subCommand) {
protected boolean hasSubCommand(String subCommand) {
return subCommands.containsKey(subCommand) || subCommandAliases.containsKey(subCommand);
}

View File

@ -74,14 +74,11 @@ public class DefaultHelpCommand extends CompositeCommand {
// Ignore the help command
if (!subCommand.getLabel().equals("help")) {
// Every command should have help because every command has a default help
if (subCommand.getSubCommand("help").isPresent()) {
// This sub-sub command has a help, so use it
subCommand.getSubCommand("help").get().execute(user, Arrays.asList(String.valueOf(newDepth)));
}
subCommand.getSubCommand("help").ifPresent(sub -> execute(user, Arrays.asList(String.valueOf(newDepth))));
}
}
}
if (depth == 0) {
user.sendMessage("commands.help.end");
}

View File

@ -27,7 +27,8 @@ public class AdminCommand extends CompositeCommand {
@Override
public boolean execute(User user, List<String> args) {
return this.getSubCommand("help").get().execute(user, args);
// By default run the attached help command, if it exists (it should)
return this.getSubCommand("help").map(help -> execute(user, args)).orElse(false);
}
}

View File

@ -43,11 +43,12 @@ public class IslandCommand extends CompositeCommand {
@Override
public boolean execute(User user, List<String> args) {
// If this player does not have an island, create one
if (!getPlugin().getIslands().hasIsland(user.getUniqueId())) {
return this.getSubCommand("create").get().execute(user, args);
return this.getSubCommand("create").map(command -> execute(user, args)).orElse(false);
}
// Currently, just go home
return this.getSubCommand("go").get().execute(user, args);
// Otherwise, currently, just go home
return this.getSubCommand("go").map(command -> execute(user, args)).orElse(false);
}
}

View File

@ -49,10 +49,10 @@ public class IslandTeamInviteCommand extends AbstractIslandTeamCommand {
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());
} else {
this.getSubCommand("help").get().execute(user, args);
return true;
}
return true;
// Show help
return this.getSubCommand("help").map(command -> execute(user, args)).orElse(false);
} else {
// Only online players can be invited
UUID invitedPlayerUUID = getPlayers().getUUID(args.get(0));

View File

@ -40,8 +40,7 @@ public class IslandTeamKickCommand extends AbstractIslandTeamCommand {
}
// If args are not right, show help
if (args.size() != 1) {
this.getSubCommand("help").get().execute(user, new ArrayList<>());
return true;
return this.getSubCommand("help").map(command -> execute(user, new ArrayList<>())).orElse(false);
}
// Get target
UUID targetUUID = getPlayers().getUUID(args.get(0));

View File

@ -41,8 +41,7 @@ public class IslandTeamSetownerCommand extends AbstractIslandTeamCommand {
}
// If args are not right, show help
if (args.size() != 1) {
this.getSubCommand("help").get().execute(user, new ArrayList<>());
return true;
return this.getSubCommand("help").map(command -> execute(user, new ArrayList<>())).orElse(false);
}
//getPlugin().getLogger().info("DEBUG: arg[0] = " + args.get(0));
UUID targetUUID = getPlayers().getUUID(args.get(0));

View File

@ -148,7 +148,7 @@ public final class AddonsManager {
// Close the jar
jar.close();
} catch (IOException e) {
} catch (Exception e) {
if (DEBUG) {
plugin.getLogger().info(f.getName() + "is not a jarfile, ignoring...");
}