Update to Java 8

This commit is contained in:
filoghost 2020-06-05 18:59:19 +02:00
parent b90c431891
commit e303d6969c
17 changed files with 78 additions and 103 deletions

View File

@ -21,7 +21,6 @@ import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import me.filoghost.chestcommands.SimpleUpdater.ResponseHandler;
import me.filoghost.chestcommands.bridge.BarAPIBridge; import me.filoghost.chestcommands.bridge.BarAPIBridge;
import me.filoghost.chestcommands.bridge.EconomyBridge; import me.filoghost.chestcommands.bridge.EconomyBridge;
import me.filoghost.chestcommands.bridge.PlaceholderAPIBridge; import me.filoghost.chestcommands.bridge.PlaceholderAPIBridge;
@ -46,10 +45,10 @@ import me.filoghost.chestcommands.task.RefreshMenusTask;
import me.filoghost.chestcommands.util.BukkitUtils; import me.filoghost.chestcommands.util.BukkitUtils;
import me.filoghost.chestcommands.util.CaseInsensitiveMap; import me.filoghost.chestcommands.util.CaseInsensitiveMap;
import me.filoghost.chestcommands.util.ErrorLogger; import me.filoghost.chestcommands.util.ErrorLogger;
import me.filoghost.chestcommands.util.Utils;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -80,7 +79,7 @@ public class ChestCommands extends JavaPlugin {
instance = this; instance = this;
fileNameToMenuMap = CaseInsensitiveMap.create(); fileNameToMenuMap = CaseInsensitiveMap.create();
commandsToMenuMap = CaseInsensitiveMap.create(); commandsToMenuMap = CaseInsensitiveMap.create();
boundItems = Utils.newHashSet(); boundItems = new HashSet<>();
settings = new Settings(new PluginConfig(this, "config.yml")); settings = new Settings(new PluginConfig(this, "config.yml"));
lang = new Lang(new PluginConfig(this, "lang.yml")); lang = new Lang(new PluginConfig(this, "lang.yml"));
@ -98,21 +97,17 @@ public class ChestCommands extends JavaPlugin {
} }
if (settings.update_notifications) { if (settings.update_notifications) {
new SimpleUpdater(this, 56919).checkForUpdates(new ResponseHandler() { new SimpleUpdater(this, 56919).checkForUpdates((String newVersion) -> {
ChestCommands.newVersion = newVersion;
@Override if (settings.use_console_colors) {
public void onUpdateFound(String newVersion) { Bukkit.getConsoleSender().sendMessage(CHAT_PREFIX + "Found a new version: " + newVersion + ChatColor.WHITE + " (yours: v" + getDescription().getVersion() + ")");
ChestCommands.newVersion = newVersion; Bukkit.getConsoleSender().sendMessage(CHAT_PREFIX + ChatColor.WHITE + "Download it on Bukkit Dev:");
Bukkit.getConsoleSender().sendMessage(CHAT_PREFIX + ChatColor.WHITE + "dev.bukkit.org/bukkit-plugins/chest-commands");
if (settings.use_console_colors) { } else {
Bukkit.getConsoleSender().sendMessage(CHAT_PREFIX + "Found a new version: " + newVersion + ChatColor.WHITE + " (yours: v" + getDescription().getVersion() + ")"); getLogger().info("Found a new version available: " + newVersion);
Bukkit.getConsoleSender().sendMessage(CHAT_PREFIX + ChatColor.WHITE + "Download it on Bukkit Dev:"); getLogger().info("Download it on Bukkit Dev:");
Bukkit.getConsoleSender().sendMessage(CHAT_PREFIX + ChatColor.WHITE + "dev.bukkit.org/bukkit-plugins/chest-commands"); getLogger().info("dev.bukkit.org/bukkit-plugins/chest-commands");
} else {
getLogger().info("Found a new version available: " + newVersion);
getLogger().info("Download it on Bukkit Dev:");
getLogger().info("dev.bukkit.org/bukkit-plugins/chest-commands");
}
} }
}); });
} }
@ -256,7 +251,7 @@ public class ChestCommands extends JavaPlugin {
* Loads all the configuration files recursively into a list. * Loads all the configuration files recursively into a list.
*/ */
private List<PluginConfig> loadMenus(File file) { private List<PluginConfig> loadMenus(File file) {
List<PluginConfig> list = Utils.newArrayList(); List<PluginConfig> list = new ArrayList<>();
if (file.isDirectory()) { if (file.isDirectory()) {
for (File subFile : file.listFiles()) { for (File subFile : file.listFiles()) {
list.addAll(loadMenus(subFile)); list.addAll(loadMenus(subFile));

View File

@ -59,44 +59,38 @@ public final class SimpleUpdater {
* @param responseHandler the response handler * @param responseHandler the response handler
*/ */
public void checkForUpdates(final ResponseHandler responseHandler) { public void checkForUpdates(final ResponseHandler responseHandler) {
Thread updaterThread = new Thread(new Runnable() { Thread updaterThread = new Thread(() -> {
try {
JSONArray filesArray = (JSONArray) readJson("https://api.curseforge.com/servermods/files?projectIds=" + projectId);
@Override if (filesArray.size() == 0) {
public void run() { // The array cannot be empty, there must be at least one file.
// The project ID is not valid or curse returned a wrong response.
try { return;
JSONArray filesArray = (JSONArray) readJson("https://api.curseforge.com/servermods/files?projectIds=" + projectId);
if (filesArray.size() == 0) {
// The array cannot be empty, there must be at least one file.
// The project ID is not valid or curse returned a wrong response.
return;
}
String updateName = (String) ((JSONObject) filesArray.get(filesArray.size() - 1)).get("name");
final PluginVersion remoteVersion = new PluginVersion(updateName);
PluginVersion localVersion = new PluginVersion(plugin.getDescription().getVersion());
if (remoteVersion.isNewerThan(localVersion)) {
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
@Override
public void run() {
responseHandler.onUpdateFound(remoteVersion.toString());
}
});
}
} catch (IOException e) {
plugin.getLogger().warning("Could not contact BukkitDev to check for updates.");
} catch (InvalidVersionException e) {
plugin.getLogger().warning("Could not check for updates because of a version format error: " + e.getMessage() + ".");
plugin.getLogger().warning("Please notify the author of this error.");
} catch (Exception e) {
e.printStackTrace();
plugin.getLogger().warning("Unable to check for updates: unhandled exception.");
} }
String updateName = (String) ((JSONObject) filesArray.get(filesArray.size() - 1)).get("name");
final PluginVersion remoteVersion = new PluginVersion(updateName);
PluginVersion localVersion = new PluginVersion(plugin.getDescription().getVersion());
if (remoteVersion.isNewerThan(localVersion)) {
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
@Override
public void run() {
responseHandler.onUpdateFound(remoteVersion.toString());
}
});
}
} catch (IOException e) {
plugin.getLogger().warning("Could not contact BukkitDev to check for updates.");
} catch (InvalidVersionException e) {
plugin.getLogger().warning("Could not check for updates because of a version format error: " + e.getMessage() + ".");
plugin.getLogger().warning("Please notify the author of this error.");
} catch (Exception e) {
e.printStackTrace();
plugin.getLogger().warning("Unable to check for updates: unhandled exception.");
} }
}); });

View File

@ -28,6 +28,7 @@ import me.filoghost.chestcommands.ChestCommands;
import me.filoghost.chestcommands.internal.VariableManager; import me.filoghost.chestcommands.internal.VariableManager;
import me.filoghost.chestcommands.util.Utils; import me.filoghost.chestcommands.util.Utils;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -59,7 +60,7 @@ public class Icon {
private ItemStack cachedItem; // When there are no variables, we don't recreate the item private ItemStack cachedItem; // When there are no variables, we don't recreate the item
public Icon() { public Icon() {
enchantments = new HashMap<Enchantment, Integer>(); enchantments = new HashMap<>();
closeOnClick = true; closeOnClick = true;
amount = 1; amount = 1;
} }
@ -154,7 +155,7 @@ public class Icon {
} }
public Map<Enchantment, Integer> getEnchantments() { public Map<Enchantment, Integer> getEnchantments() {
return new HashMap<Enchantment, Integer>(enchantments); return new HashMap<>(enchantments);
} }
public void addEnchantment(Enchantment ench) { public void addEnchantment(Enchantment ench) {
@ -244,7 +245,7 @@ public class Icon {
if (hasLore()) { if (hasLore()) {
output = Utils.newArrayList(); output = new ArrayList<>();
if (pov != null && loreLinesWithVariables != null) { if (pov != null && loreLinesWithVariables != null) {
for (int i = 0; i < lore.size(); i++) { for (int i = 0; i < lore.size(); i++) {
@ -263,7 +264,7 @@ public class Icon {
if (material == null) { if (material == null) {
if (output == null) { if (output == null) {
output = Utils.newArrayList(); output = new ArrayList<>();
} }
// Add an error message // Add an error message

View File

@ -24,6 +24,7 @@ import me.filoghost.chestcommands.util.Utils;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
@ -33,7 +34,7 @@ import java.util.Map.Entry;
*/ */
public class AsciiPlaceholders { public class AsciiPlaceholders {
private static Map<String, String> placeholders = Utils.newHashMap(); private static Map<String, String> placeholders = new HashMap<>();
public static void load(ErrorLogger errorLogger) throws IOException, Exception { public static void load(ErrorLogger errorLogger) throws IOException, Exception {

View File

@ -14,14 +14,13 @@
*/ */
package me.filoghost.chestcommands.config; package me.filoghost.chestcommands.config;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import me.filoghost.chestcommands.util.Utils;
public class ConfigUtil { public class ConfigUtil {
public static String getAnyString(ConfigurationSection config, String... paths) { public static String getAnyString(ConfigurationSection config, String... paths) {
@ -75,7 +74,7 @@ public class ConfigUtil {
} }
String[] splitValues = input.split(Pattern.quote(separator)); String[] splitValues = input.split(Pattern.quote(separator));
List<String> values = Utils.newArrayList(); List<String> values = new ArrayList<>();
for (String value : splitValues) { for (String value : splitValues) {
String trimmedValue = value.trim(); String trimmedValue = value.trim();

View File

@ -47,7 +47,7 @@ public class SpecialConfig {
// Check if the configuration was initialized // Check if the configuration was initialized
if (defaultValuesMap == null) { if (defaultValuesMap == null) {
defaultValuesMap = new HashMap<String, Object>(); defaultValuesMap = new HashMap<>();
// Put the values in the default values map // Put the values in the default values map
for (Field field : getClass().getDeclaredFields()) { for (Field field : getClass().getDeclaredFields()) {

View File

@ -38,13 +38,11 @@ public class OpenIconCommand extends IconCommand {
* Delay the task, since this command is executed in ClickInventoryEvent * Delay the task, since this command is executed in ClickInventoryEvent
* and opening another inventory in the same moment is not a good idea. * and opening another inventory in the same moment is not a good idea.
*/ */
Bukkit.getScheduler().scheduleSyncDelayedTask(ChestCommands.getInstance(), new Runnable() { Bukkit.getScheduler().scheduleSyncDelayedTask(ChestCommands.getInstance(), () -> {
public void run() { if (player.hasPermission(menu.getPermission())) {
if (player.hasPermission(menu.getPermission())) { menu.open(player);
menu.open(player); } else {
} else { menu.sendNoPermissionMessage(player);
menu.sendNoPermissionMessage(player);
}
} }
}); });

View File

@ -30,13 +30,12 @@ import me.filoghost.chestcommands.api.IconMenu;
import me.filoghost.chestcommands.internal.BoundItem; import me.filoghost.chestcommands.internal.BoundItem;
import me.filoghost.chestcommands.internal.MenuInventoryHolder; import me.filoghost.chestcommands.internal.MenuInventoryHolder;
import me.filoghost.chestcommands.task.ExecuteCommandsTask; import me.filoghost.chestcommands.task.ExecuteCommandsTask;
import me.filoghost.chestcommands.util.Utils; import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class InventoryListener implements Listener { public class InventoryListener implements Listener {
private static Map<Player, Long> antiClickSpam = Utils.newHashMap(); private static Map<Player, Long> antiClickSpam = new HashMap<>();
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
public void onInteract(PlayerInteractEvent event) { public void onInteract(PlayerInteractEvent event) {

View File

@ -14,6 +14,7 @@
*/ */
package me.filoghost.chestcommands.serializer; package me.filoghost.chestcommands.serializer;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -22,11 +23,10 @@ import java.util.regex.Pattern;
import me.filoghost.chestcommands.internal.icon.IconCommand; import me.filoghost.chestcommands.internal.icon.IconCommand;
import me.filoghost.chestcommands.internal.icon.command.*; import me.filoghost.chestcommands.internal.icon.command.*;
import me.filoghost.chestcommands.util.ErrorLogger; import me.filoghost.chestcommands.util.ErrorLogger;
import me.filoghost.chestcommands.util.Utils;
public class CommandSerializer { public class CommandSerializer {
private static Map<Pattern, Class<? extends IconCommand>> commandTypesMap = Utils.newHashMap(); private static Map<Pattern, Class<? extends IconCommand>> commandTypesMap = new HashMap<>();
static { static {
commandTypesMap.put(commandPattern("console:"), ConsoleIconCommand.class); commandTypesMap.put(commandPattern("console:"), ConsoleIconCommand.class);

View File

@ -24,7 +24,7 @@ import java.util.Map;
public class EnchantmentSerializer { public class EnchantmentSerializer {
private static Map<String, Enchantment> enchantmentsMap = new HashMap<String, Enchantment>(); private static Map<String, Enchantment> enchantmentsMap = new HashMap<>();
static { static {
enchantmentsMap.put(formatLowercase("Protection"), Enchantment.PROTECTION_ENVIRONMENTAL); enchantmentsMap.put(formatLowercase("Protection"), Enchantment.PROTECTION_ENVIRONMENTAL);

View File

@ -14,6 +14,8 @@
*/ */
package me.filoghost.chestcommands.serializer; package me.filoghost.chestcommands.serializer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -34,7 +36,6 @@ import me.filoghost.chestcommands.util.ErrorLogger;
import me.filoghost.chestcommands.util.FormatUtils; import me.filoghost.chestcommands.util.FormatUtils;
import me.filoghost.chestcommands.util.ItemStackReader; import me.filoghost.chestcommands.util.ItemStackReader;
import me.filoghost.chestcommands.util.ItemUtils; import me.filoghost.chestcommands.util.ItemUtils;
import me.filoghost.chestcommands.util.Utils;
import me.filoghost.chestcommands.util.Validate; import me.filoghost.chestcommands.util.Validate;
import me.filoghost.chestcommands.util.nbt.parser.MojangsonParseException; import me.filoghost.chestcommands.util.nbt.parser.MojangsonParseException;
import me.filoghost.chestcommands.util.nbt.parser.MojangsonParser; import me.filoghost.chestcommands.util.nbt.parser.MojangsonParser;
@ -137,7 +138,7 @@ public class IconSerializer {
List<String> serializedEnchantments = ConfigUtil.getStringListOrInlineList(section, ";", Nodes.ENCHANTMENTS); List<String> serializedEnchantments = ConfigUtil.getStringListOrInlineList(section, ";", Nodes.ENCHANTMENTS);
if (serializedEnchantments != null && !serializedEnchantments.isEmpty()) { if (serializedEnchantments != null && !serializedEnchantments.isEmpty()) {
Map<Enchantment, Integer> enchantments = Utils.newHashMap(); Map<Enchantment, Integer> enchantments = new HashMap<>();
for (String serializedEnchantment : serializedEnchantments) { for (String serializedEnchantment : serializedEnchantments) {
if (serializedEnchantment != null && !serializedEnchantment.isEmpty()) { if (serializedEnchantment != null && !serializedEnchantment.isEmpty()) {
@ -190,7 +191,7 @@ public class IconSerializer {
List<String> serializedCommands = ConfigUtil.getStringListOrInlineList(section, ChestCommands.getSettings().multiple_commands_separator, Nodes.ACTIONS); List<String> serializedCommands = ConfigUtil.getStringListOrInlineList(section, ChestCommands.getSettings().multiple_commands_separator, Nodes.ACTIONS);
if (serializedCommands != null && !serializedCommands.isEmpty()) { if (serializedCommands != null && !serializedCommands.isEmpty()) {
List<IconCommand> commands = Utils.newArrayList(); List<IconCommand> commands = new ArrayList<>();
for (String serializedCommand : serializedCommands) { for (String serializedCommand : serializedCommands) {
if (serializedCommand != null && !serializedCommand.isEmpty()) { if (serializedCommand != null && !serializedCommand.isEmpty()) {
@ -220,7 +221,7 @@ public class IconSerializer {
List<String> serializedRequiredItems = ConfigUtil.getStringListOrSingle(section, Nodes.REQUIRED_ITEMS); List<String> serializedRequiredItems = ConfigUtil.getStringListOrSingle(section, Nodes.REQUIRED_ITEMS);
if (serializedRequiredItems != null && !serializedRequiredItems.isEmpty()) { if (serializedRequiredItems != null && !serializedRequiredItems.isEmpty()) {
List<RequiredItem> requiredItems = Utils.newArrayList(); List<RequiredItem> requiredItems = new ArrayList<>();
for (String serializedItem : serializedRequiredItems) { for (String serializedItem : serializedRequiredItems) {
try { try {

View File

@ -14,6 +14,7 @@
*/ */
package me.filoghost.chestcommands.serializer; package me.filoghost.chestcommands.serializer;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
@ -32,7 +33,6 @@ import me.filoghost.chestcommands.util.ClickType;
import me.filoghost.chestcommands.util.ErrorLogger; import me.filoghost.chestcommands.util.ErrorLogger;
import me.filoghost.chestcommands.util.FormatUtils; import me.filoghost.chestcommands.util.FormatUtils;
import me.filoghost.chestcommands.util.ItemStackReader; import me.filoghost.chestcommands.util.ItemStackReader;
import me.filoghost.chestcommands.util.Utils;
public class MenuSerializer { public class MenuSerializer {
@ -119,7 +119,7 @@ public class MenuSerializer {
List<String> serializedOpenCommands = ConfigUtil.getStringListOrInlineList(config, ChestCommands.getSettings().multiple_commands_separator, Nodes.OPEN_ACTIONS); List<String> serializedOpenCommands = ConfigUtil.getStringListOrInlineList(config, ChestCommands.getSettings().multiple_commands_separator, Nodes.OPEN_ACTIONS);
if (serializedOpenCommands != null && !serializedOpenCommands.isEmpty()) { if (serializedOpenCommands != null && !serializedOpenCommands.isEmpty()) {
List<IconCommand> openCommands = Utils.newArrayList(); List<IconCommand> openCommands = new ArrayList<>();
for (String serializedCommand : serializedOpenCommands) { for (String serializedCommand : serializedOpenCommands) {
if (serializedCommand != null && !serializedCommand.isEmpty()) { if (serializedCommand != null && !serializedCommand.isEmpty()) {

View File

@ -20,8 +20,7 @@ import org.bukkit.ChatColor;
import me.filoghost.chestcommands.ChestCommands; import me.filoghost.chestcommands.ChestCommands;
import me.filoghost.chestcommands.util.ErrorLogger; import me.filoghost.chestcommands.util.ErrorLogger;
import me.filoghost.chestcommands.util.StringUtils; import me.filoghost.chestcommands.util.StringUtils;
import me.filoghost.chestcommands.util.Utils; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class ErrorLoggerTask implements Runnable { public class ErrorLoggerTask implements Runnable {
@ -35,7 +34,7 @@ public class ErrorLoggerTask implements Runnable {
@Override @Override
public void run() { public void run() {
List<String> lines = Utils.newArrayList(); List<String> lines = new ArrayList<>();
lines.add(" "); lines.add(" ");
lines.add(ChatColor.RED + "#------------------- Chest Commands Errors -------------------#"); lines.add(ChatColor.RED + "#------------------- Chest Commands Errors -------------------#");

View File

@ -22,14 +22,14 @@ import java.util.List;
*/ */
public class ErrorLogger { public class ErrorLogger {
private List<String> errors = new ArrayList<String>(); private List<String> errors = new ArrayList<>();
public void addError(String error) { public void addError(String error) {
errors.add(error); errors.add(error);
} }
public List<String> getErrors() { public List<String> getErrors() {
return new ArrayList<String>(errors); return new ArrayList<>(errors);
} }
public boolean hasErrors() { public boolean hasErrors() {

View File

@ -31,7 +31,7 @@ public final class MaterialsRegistry {
private static final char[] IGNORE_CHARS = {'-', '_', ' '}; private static final char[] IGNORE_CHARS = {'-', '_', ' '};
// Default material names are ugly // Default material names are ugly
private static final Map<String, Material> MATERIALS_BY_ALIAS = new HashMap<String, Material>(); private static final Map<String, Material> MATERIALS_BY_ALIAS = new HashMap<>();
// Materials that are considered air (with 1.13+ compatibility) // Materials that are considered air (with 1.13+ compatibility)
private static final Collection<Material> AIR_MATERIALS = getExistingMaterials("AIR", "CAVE_AIR", "VOID_AIR"); private static final Collection<Material> AIR_MATERIALS = getExistingMaterials("AIR", "CAVE_AIR", "VOID_AIR");

View File

@ -64,7 +64,7 @@ public final class Utils {
BufferedReader br = null; BufferedReader br = null;
try { try {
List<String> lines = newArrayList(); List<String> lines = new ArrayList<>();
if (!file.exists()) { if (!file.exists()) {
throw new FileNotFoundException(); throw new FileNotFoundException();
@ -89,18 +89,6 @@ public final class Utils {
} }
} }
public static <T> Set<T> newHashSet() {
return new HashSet<T>();
}
public static <T, V> Map<T, V> newHashMap() {
return new HashMap<T, V>();
}
public static <T> List<T> newArrayList() {
return new ArrayList<T>();
}
public static boolean isClassLoaded(String name) { public static boolean isClassLoaded(String name) {
try { try {
Class.forName(name); Class.forName(name);

View File

@ -19,8 +19,8 @@
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.6</maven.compiler.source> <maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target> <maven.compiler.target>1.8</maven.compiler.target>
<maven.test.skip>true</maven.test.skip> <maven.test.skip>true</maven.test.skip>
<spigot-api.version>1.8.8-R0.1-SNAPSHOT</spigot-api.version> <spigot-api.version>1.8.8-R0.1-SNAPSHOT</spigot-api.version>
</properties> </properties>