Fix potential NPE and properly log exceptions

This commit is contained in:
Phoenix616 2023-03-01 17:42:35 +01:00
parent f547995164
commit fbfe789bfa
No known key found for this signature in database
GPG Key ID: 40E2321E71738EB0
2 changed files with 19 additions and 17 deletions

View File

@ -179,9 +179,11 @@ public class ChestShop extends JavaPlugin {
private void registerCommand(String name, CommandExecutor executor, Permission permission) {
PluginCommand command = getCommand(name);
command.setExecutor(executor);
command.setPermission(permission.toString());
commands.add(command);
if (command != null) {
command.setExecutor(executor);
command.setPermission(permission.toString());
commands.add(command);
}
}
public void loadConfig() {
@ -244,7 +246,7 @@ public class ChestShop extends JavaPlugin {
try {
previousVersion.save(versionFile);
} catch (IOException e) {
e.printStackTrace();
getLogger().log(java.util.logging.Level.SEVERE, "Unable to save new database version " + Migrations.CURRENT_DATABASE_VERSION, e);
}
}
@ -261,7 +263,7 @@ public class ChestShop extends JavaPlugin {
try {
previousVersion.save(versionFile);
} catch (IOException e) {
e.printStackTrace();
getLogger().log(java.util.logging.Level.SEVERE, "Unable to save new database version " + newVersion, e);
}
}
return true;
@ -282,7 +284,7 @@ public class ChestShop extends JavaPlugin {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
getBukkitLogger().log(java.util.logging.Level.SEVERE, "Unable to load file " + file.getName(), e);
}
}
@ -295,7 +297,7 @@ public class ChestShop extends JavaPlugin {
try {
handler = new FileHandler(path, true);
} catch (IOException ex) {
ex.printStackTrace();
getBukkitLogger().log(java.util.logging.Level.SEVERE, "Unable to load handler " + path, ex);
}
return handler;
@ -437,8 +439,8 @@ public class ChestShop extends JavaPlugin {
private void startStatistics() {
Metrics bStats = new Metrics(this, 1109);
try {
String dist = new JarFile(this.getFile()).getManifest().getMainAttributes().getValue("Distribution-Type");
try (JarFile jarFile = new JarFile(this.getFile())) {
String dist = jarFile.getManifest().getMainAttributes().getValue("Distribution-Type");
bStats.addCustomChart(new SimplePie("distributionType", () -> dist));
} catch (IOException ignored) {}

View File

@ -70,8 +70,10 @@ public class ItemDatabase {
private int getCurrentMetadataVersion() {
ItemStack item = new ItemStack(Material.STONE);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName("GetCurrentMetadataVersion");
item.setItemMeta(meta);
if (meta != null) {
meta.setDisplayName("GetCurrentMetadataVersion");
item.setItemMeta(meta);
}
Map<String, Object> serialized = item.serialize();
return (int) serialized.getOrDefault("v", -1);
}
@ -117,7 +119,7 @@ public class ItemDatabase {
return true;
});
} catch (Exception e) {
e.printStackTrace();
ChestShop.getBukkitLogger().log(Level.SEVERE, "Unable to update metadata version of all items from " + previousVersion + " to " + newVersion, e);
return false;
} finally {
it.closeQuietly();
@ -153,10 +155,8 @@ public class ItemDatabase {
itemDao.create(itemEntity);
}
return Base62.encode(itemEntity.getId());
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException | IOException e) {
ChestShop.getBukkitLogger().log(Level.SEVERE, "Unable to get code of item " + item, e);
}
return null;
@ -196,7 +196,7 @@ public class ItemDatabase {
return null;
}
private class YamlBukkitConstructor extends YamlConstructor {
private static class YamlBukkitConstructor extends YamlConstructor {
public YamlBukkitConstructor() {
this.yamlConstructors.put(new Tag(Tag.PREFIX + "org.bukkit.inventory.ItemStack"), yamlConstructors.get(Tag.MAP));
}