Skip to content

Commit

Permalink
improve loading
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Dec 30, 2023
1 parent 908412d commit 1ca4cea
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@
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 {

private static AnarchyExploitFixes instance;
private static HashMap<String, LanguageCache> languageCacheMap;
private static Config config;
private static Logger logger;
private static boolean isServerFolia;
private static boolean isServerFolia, foundProtocolLib;

public TPSCache tpsCache;
public final HashSet<UUID> NEW_CHUNK_PLAYERS = new HashSet<>();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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));
}
}
}
Expand All @@ -163,17 +168,14 @@ private void reloadLang() {
}

private Set<String> getDefaultLanguageFiles() {
Set<String> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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<UUID> CONNECTION_MSG_TOGGLE = new HashSet<>();
Expand Down Expand Up @@ -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("##########################################################");
Expand Down Expand Up @@ -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() {
Expand All @@ -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));
}
}
}
Expand All @@ -173,18 +172,14 @@ private void reloadLang() {
}

private Set<String> getDefaultLanguageFiles() {
Set<String> languageFiles = new HashSet<>();
try (JarFile jarFile = new JarFile(this.getFile())) {
Enumeration<JarEntry> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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());
}
}

Expand Down

0 comments on commit 1ca4cea

Please sign in to comment.