From 1ca4ceafb5a57a2a13f9155e57fd6e0759024778 Mon Sep 17 00:00:00 2001 From: xGinko Date: Sat, 30 Dec 2023 22:48:46 +0100 Subject: [PATCH] improve loading --- .../AnarchyExploitFixes.java | 44 ++++++++--------- .../config/LanguageCache.java | 4 +- .../AnarchyExploitFixes.java | 47 +++++++++---------- .../config/LanguageCache.java | 4 +- 4 files changed, 48 insertions(+), 51 deletions(-) diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/AnarchyExploitFixes.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/AnarchyExploitFixes.java index 943d0182b..11084e71c 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/AnarchyExploitFixes.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/AnarchyExploitFixes.java @@ -20,6 +20,8 @@ import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; +import java.util.zip.ZipEntry; public class AnarchyExploitFixes extends JavaPlugin { @@ -27,7 +29,7 @@ public class AnarchyExploitFixes extends JavaPlugin { private static HashMap languageCacheMap; private static Config config; private static Logger logger; - private static boolean isServerFolia; + private static boolean isServerFolia, foundProtocolLib; public TPSCache tpsCache; public final HashSet NEW_CHUNK_PLAYERS = new HashSet<>(); @@ -63,8 +65,12 @@ public void onEnable() { } logger.info("Detected Version 1." + PaperLib.getMinecraftVersion() + "." + PaperLib.getMinecraftPatchVersion()); + foundProtocolLib = getServer().getPluginManager().isPluginEnabled("ProtocolLib"); + logger.info(foundProtocolLib ? "Found ProtocolLib" : "Could not find ProtocolLib"); + try { Class.forName("io.papermc.paper.threadedregions.RegionizedServer"); + logger.info("Detected Folia server"); isServerFolia = true; } catch (ClassNotFoundException e) { isServerFolia = false; @@ -107,10 +113,11 @@ public static LanguageCache getLang(CommandSender commandSender) { return commandSender instanceof Player player ? getLang(player.locale()) : getLang(config.default_lang); } public static LanguageCache getLang(String lang) { - return config.auto_lang ? languageCacheMap.getOrDefault(lang.replace("-", "_"), languageCacheMap.get(config.default_lang.toString().toLowerCase())) : languageCacheMap.get(config.default_lang.toString().toLowerCase()); + if (!config.auto_lang) return languageCacheMap.get(config.default_lang.toString().toLowerCase()); + return languageCacheMap.getOrDefault(lang.replace("-", "_"), languageCacheMap.get(config.default_lang.toString().toLowerCase())); } public static boolean isProtocolLibInstalled() { - return instance.getServer().getPluginManager().isPluginEnabled("ProtocolLib"); + return foundProtocolLib; } public static boolean isServerFolia() { return isServerFolia; @@ -136,23 +143,21 @@ private void reloadConfiguration() { private void reloadLang() { languageCacheMap = new HashMap<>(); try { - File langDirectory = new File(getDataFolder() + "/lang"); + File langDirectory = new File(getDataFolder() + File.separator + "lang"); Files.createDirectories(langDirectory.toPath()); for (String fileName : getDefaultLanguageFiles()) { - String localeString = fileName.substring(fileName.lastIndexOf('/') + 1, fileName.lastIndexOf('.')); + final String localeString = fileName.substring(fileName.lastIndexOf(File.separator) + 1, fileName.lastIndexOf('.')); logger.info("Found language file for " + localeString); - LanguageCache langCache = new LanguageCache(localeString); - languageCacheMap.put(localeString, langCache); + languageCacheMap.put(localeString, new LanguageCache(localeString)); } Pattern langPattern = Pattern.compile("([a-z]{1,3}_[a-z]{1,3})(\\.yml)", Pattern.CASE_INSENSITIVE); for (File langFile : langDirectory.listFiles()) { - Matcher langMatcher = langPattern.matcher(langFile.getName()); + final Matcher langMatcher = langPattern.matcher(langFile.getName()); if (langMatcher.find()) { - String localeString = langMatcher.group(1).toLowerCase(); + final String localeString = langMatcher.group(1).toLowerCase(); if (!languageCacheMap.containsKey(localeString)) { // make sure it wasn't a default file that we already loaded logger.info("Found language file for " + localeString); - LanguageCache langCache = new LanguageCache(localeString); - languageCacheMap.put(localeString, langCache); + languageCacheMap.put(localeString, new LanguageCache(localeString)); } } } @@ -163,17 +168,14 @@ private void reloadLang() { } private Set getDefaultLanguageFiles() { - Set languageFiles = new HashSet<>(); - try (JarFile jarFile = new JarFile(this.getFile())) { - jarFile.entries().asIterator().forEachRemaining(jarEntry -> { - final String path = jarEntry.getName(); - if (path.startsWith("lang/") && path.endsWith(".yml")) - languageFiles.add(path); - }); + try (final JarFile pluginJarFile = new JarFile(this.getFile())) { + return pluginJarFile.stream() + .map(ZipEntry::getName) + .filter(name -> name.startsWith("lang" + File.separator) && name.endsWith(".yml")) + .collect(Collectors.toSet()); } catch (IOException e) { - logger.severe("Error while getting default language file names! - " + e.getLocalizedMessage()); - e.printStackTrace(); + logger.severe("Failed getting default lang files! - "+e.getLocalizedMessage()); + return Collections.emptySet(); } - return languageFiles; } } \ No newline at end of file diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/config/LanguageCache.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/config/LanguageCache.java index febe5dd70..6fb41c05f 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/config/LanguageCache.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/config/LanguageCache.java @@ -32,7 +32,7 @@ public LanguageCache(String locale) throws Exception { AnarchyExploitFixes.getLog().severe("Unable to create lang directory."); // Check if the file already exists and save the one from the plugins resources folder if it does not if (!langYML.exists()) - plugin.saveResource("lang/" + locale + ".yml", false); + plugin.saveResource("lang" + File.separator + locale + ".yml", false); // Finally load the lang file with configmaster this.lang = ConfigFile.loadConfig(langYML); @@ -127,7 +127,7 @@ public LanguageCache(String locale) throws Exception { try { lang.save(); } catch (Exception e) { - AnarchyExploitFixes.getLog().severe("Failed to save language file: "+ lang.getFile().getName() +" - " + e.getLocalizedMessage()); + AnarchyExploitFixes.getLog().severe("Failed to save language file: "+ langYML.getName() +" - " + e.getLocalizedMessage()); } } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/AnarchyExploitFixes.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/AnarchyExploitFixes.java index 525f29ee2..678923b5e 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/AnarchyExploitFixes.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/AnarchyExploitFixes.java @@ -15,11 +15,12 @@ import java.io.IOException; import java.nio.file.Files; import java.util.*; -import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; +import java.util.zip.ZipEntry; public class AnarchyExploitFixes extends JavaPlugin { @@ -28,6 +29,7 @@ public class AnarchyExploitFixes extends JavaPlugin { private static Config config; private static Logger logger; private static int minorMCVersion; + private static boolean foundProtocolLib; public TPSCache tpsCache; public final HashSet CONNECTION_MSG_TOGGLE = new HashSet<>(); @@ -64,6 +66,8 @@ public void onEnable() { minorMCVersion = PaperLib.getMinecraftVersion(); logger.info("Detected Version 1." + minorMCVersion + "." + PaperLib.getMinecraftPatchVersion()); + foundProtocolLib = getServer().getPluginManager().isPluginEnabled("ProtocolLib"); + logger.info(foundProtocolLib ? "Found ProtocolLib" : "Could not find ProtocolLib"); if (minorMCVersion < 12) { logger.warning("##########################################################"); @@ -113,17 +117,14 @@ public static LanguageCache getLang(CommandSender commandSender) { return commandSender instanceof Player ? getLang(((Player) commandSender).getLocale()) : getLang(config.default_lang); } public static LanguageCache getLang(String lang) { - if (config.auto_lang) { - return languageCacheMap.getOrDefault(lang.replace("-", "_"), languageCacheMap.get(config.default_lang.toString().toLowerCase())); - } else { - return languageCacheMap.get(config.default_lang.toString().toLowerCase()); - } + if (!config.auto_lang) return languageCacheMap.get(config.default_lang.toString().toLowerCase()); + return languageCacheMap.getOrDefault(lang.replace("-", "_"), languageCacheMap.get(config.default_lang.toString().toLowerCase())); } public static int getMCVersion() { return minorMCVersion; } public static boolean isProtocolLibInstalled() { - return instance.getServer().getPluginManager().isPluginEnabled("ProtocolLib"); + return foundProtocolLib; } public void reloadPlugin() { @@ -146,23 +147,21 @@ private void reloadConfiguration() { private void reloadLang() { languageCacheMap = new HashMap<>(); try { - File langDirectory = new File(getDataFolder() + "/lang"); + File langDirectory = new File(getDataFolder() + File.separator + "lang"); Files.createDirectories(langDirectory.toPath()); for (String fileName : getDefaultLanguageFiles()) { - String localeString = fileName.substring(fileName.lastIndexOf('/') + 1, fileName.lastIndexOf('.')); + final String localeString = fileName.substring(fileName.lastIndexOf(File.separator) + 1, fileName.lastIndexOf('.')); logger.info("Found language file for " + localeString); - LanguageCache langCache = new LanguageCache(localeString); - languageCacheMap.put(localeString, langCache); + languageCacheMap.put(localeString, new LanguageCache(localeString)); } Pattern langPattern = Pattern.compile("([a-z]{1,3}_[a-z]{1,3})(\\.yml)", Pattern.CASE_INSENSITIVE); for (File langFile : langDirectory.listFiles()) { - Matcher langMatcher = langPattern.matcher(langFile.getName()); + final Matcher langMatcher = langPattern.matcher(langFile.getName()); if (langMatcher.find()) { - String localeString = langMatcher.group(1).toLowerCase(); + final String localeString = langMatcher.group(1).toLowerCase(); if (!languageCacheMap.containsKey(localeString)) { // make sure it wasn't a default file that we already loaded logger.info("Found language file for " + localeString); - LanguageCache langCache = new LanguageCache(localeString); - languageCacheMap.put(localeString, langCache); + languageCacheMap.put(localeString, new LanguageCache(localeString)); } } } @@ -173,18 +172,14 @@ private void reloadLang() { } private Set getDefaultLanguageFiles() { - Set languageFiles = new HashSet<>(); - try (JarFile jarFile = new JarFile(this.getFile())) { - Enumeration entries = jarFile.entries(); - while (entries.hasMoreElements()) { - final String path = entries.nextElement().getName(); - if (path.startsWith("lang/") && path.endsWith(".yml")) - languageFiles.add(path); - } + try (final JarFile pluginJarFile = new JarFile(this.getFile())) { + return pluginJarFile.stream() + .map(ZipEntry::getName) + .filter(name -> name.startsWith("lang" + File.separator) && name.endsWith(".yml")) + .collect(Collectors.toSet()); } catch (IOException e) { - logger.severe("Error while getting default language file names! - " + e.getLocalizedMessage()); - e.printStackTrace(); + logger.severe("Failed getting default lang files! - "+e.getLocalizedMessage()); + return Collections.emptySet(); } - return languageFiles; } } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/config/LanguageCache.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/config/LanguageCache.java index 84c49bbd2..e1c97547d 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/config/LanguageCache.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/config/LanguageCache.java @@ -34,7 +34,7 @@ public LanguageCache(String locale) throws Exception { AnarchyExploitFixes.getLog().severe("Unable to create lang directory."); // Check if the file already exists and save the one from the plugins resources folder if it does not if (!langYML.exists()) - plugin.saveResource("lang/" + locale + ".yml", false); + plugin.saveResource("lang" + File.separator + locale + ".yml", false); // Finally load the lang file with configmaster this.lang = ConfigFile.loadConfig(langYML); @@ -124,7 +124,7 @@ public LanguageCache(String locale) throws Exception { try { lang.save(); } catch (Exception e) { - AnarchyExploitFixes.getLog().severe("Failed to save language file: "+ lang.getFile().getName() +" - " + e.getLocalizedMessage()); + AnarchyExploitFixes.getLog().severe("Failed to save language file: "+ langYML.getName() +" - " + e.getLocalizedMessage()); } }