From be2175661187bc8d1ce4295ac617d683a36f8b47 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Tue, 12 Dec 2017 13:49:26 +0200 Subject: [PATCH] Locale no longer requires Plan as variable (Still requires Mock, TODO) --- .../main/java/com/djrapitops/plan/Plan.java | 2 +- .../java/com/djrapitops/plan/PlanBungee.java | 2 +- .../plan/settings/locale/Locale.java | 26 +++++++++++-------- .../plan/systems/store/FileSystem.java | 4 +++ .../comparators/StringLengthComparator.java | 2 +- Plan/test/test/java/utils/TestInit.java | 6 ++--- 6 files changed, 25 insertions(+), 17 deletions(-) diff --git a/Plan/src/main/java/com/djrapitops/plan/Plan.java b/Plan/src/main/java/com/djrapitops/plan/Plan.java index 0fb8d8cf6..cd400903a 100644 --- a/Plan/src/main/java/com/djrapitops/plan/Plan.java +++ b/Plan/src/main/java/com/djrapitops/plan/Plan.java @@ -174,7 +174,7 @@ public class Plan extends BukkitPlugin implements IPlan { throw new PlanEnableException("Something went wrong saving the downloaded GeoLite2 Geolocation database", e); } - new Locale(this).loadLocale(); + new Locale().loadLocale(); theme = new Theme(); diff --git a/Plan/src/main/java/com/djrapitops/plan/PlanBungee.java b/Plan/src/main/java/com/djrapitops/plan/PlanBungee.java index 2149b8c63..f6d06c6b7 100644 --- a/Plan/src/main/java/com/djrapitops/plan/PlanBungee.java +++ b/Plan/src/main/java/com/djrapitops/plan/PlanBungee.java @@ -87,7 +87,7 @@ public class PlanBungee extends BungeePlugin implements IPlan { } variableHolder = new ServerVariableHolder(getProxy()); - new Locale(this).loadLocale(); + new Locale().loadLocale(); theme = new Theme(); diff --git a/Plan/src/main/java/com/djrapitops/plan/settings/locale/Locale.java b/Plan/src/main/java/com/djrapitops/plan/settings/locale/Locale.java index f5de5f189..03b150317 100644 --- a/Plan/src/main/java/com/djrapitops/plan/settings/locale/Locale.java +++ b/Plan/src/main/java/com/djrapitops/plan/settings/locale/Locale.java @@ -1,13 +1,16 @@ package main.java.com.djrapitops.plan.settings.locale; import com.djrapitops.plugin.api.Benchmark; +import com.djrapitops.plugin.api.config.Config; import com.djrapitops.plugin.api.utility.log.Log; import com.djrapitops.plugin.settings.ColorScheme; import com.djrapitops.plugin.settings.DefaultMessages; import com.djrapitops.plugin.utilities.Verify; -import main.java.com.djrapitops.plan.api.IPlan; import main.java.com.djrapitops.plan.settings.Permissions; import main.java.com.djrapitops.plan.settings.Settings; +import main.java.com.djrapitops.plan.systems.store.FileSystem; +import main.java.com.djrapitops.plan.systems.store.config.ConfigSystem; +import main.java.com.djrapitops.plan.utilities.MiscUtils; import main.java.com.djrapitops.plan.utilities.comparators.LocaleEntryComparator; import main.java.com.djrapitops.plan.utilities.comparators.StringLengthComparator; import main.java.com.djrapitops.plan.utilities.file.FileUtil; @@ -34,12 +37,10 @@ import java.util.stream.Collectors; */ public class Locale { - private final IPlan plugin; private final Map messages; - public Locale(IPlan plugin) { + public Locale() { LocaleHolder.setLocale(this); - this.plugin = plugin; messages = new EnumMap<>(Msg.class); } @@ -67,7 +68,7 @@ public class Locale { if (Settings.WRITE_NEW_LOCALE.isTrue()) { writeNewDefaultLocale(); } - File localeFile = new File(plugin.getDataFolder(), "locale.txt"); + File localeFile = FileSystem.getLocaleFile(); if (localeFile.exists()) { loadFromFile(localeFile); } else if (locale.equals("DEFAULT")) { @@ -83,7 +84,6 @@ public class Locale { } private void writeNewDefaultLocale() throws IOException { - Optional key = messages.keySet().stream() .map(Msg::getIdentifier) .sorted(new StringLengthComparator()) @@ -96,9 +96,11 @@ public class Locale { .sorted(new LocaleEntryComparator()) .map(entry -> getSpacedIdentifier(entry.getKey().getIdentifier(), length) + "|| " + entry.getValue().toString()) .collect(Collectors.toList()); - Files.write(new File(plugin.getDataFolder(), "locale.txt").toPath(), lines, StandardCharsets.UTF_8); - plugin.getMainConfig().set(Settings.WRITE_NEW_LOCALE.getPath(), false); - plugin.getMainConfig().save(); + Files.write(FileSystem.getLocaleFile().toPath(), lines, StandardCharsets.UTF_8); + + Config config = ConfigSystem.getInstance().getConfig(); + config.set(Settings.WRITE_NEW_LOCALE.getPath(), false); + config.save(); } private String getSpacedIdentifier(String identifier, int length) { @@ -110,13 +112,15 @@ public class Locale { } private void loadDefault() { + // TODO Move to Msg as DefaultMessages + String analysis = "Analysis | "; String prefix = "[Plan] "; String green = "§a"; String yellow = "§e"; String red = "§c"; String arrowsRight = DefaultMessages.ARROWS_RIGHT.parse(); - ColorScheme cs = plugin.getColorScheme(); + ColorScheme cs = MiscUtils.getIPlan().getColorScheme(); String mCol = cs.getMainColor(); String sCol = cs.getSecondaryColor(); String tCol = cs.getTertiaryColor(); @@ -310,7 +314,7 @@ public class Locale { private void loadFromResource(String fileName) { try { - loadFromContents(FileUtil.lines(plugin, fileName), fileName); + loadFromContents(FileSystem.readFromResource(fileName), fileName); } catch (FileNotFoundException e) { Log.error("Could not find file inside the jar: " + fileName); Log.info("Using Locale: Default (EN)"); diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/store/FileSystem.java b/Plan/src/main/java/com/djrapitops/plan/systems/store/FileSystem.java index 7c54bf9cc..a668b7adc 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/store/FileSystem.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/store/FileSystem.java @@ -45,6 +45,10 @@ public class FileSystem implements SubSystem { return getInstance().configFile; } + public static File getLocaleFile() { + return new File(getInstance().dataFolder, "locale.txt"); + } + public static List readFromResource(String fileName) throws IOException { return FileUtil.lines(MiscUtils.getIPlan(), fileName); } diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/comparators/StringLengthComparator.java b/Plan/src/main/java/com/djrapitops/plan/utilities/comparators/StringLengthComparator.java index 721176d94..0fa093bd2 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/comparators/StringLengthComparator.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/comparators/StringLengthComparator.java @@ -3,7 +3,7 @@ package main.java.com.djrapitops.plan.utilities.comparators; import java.util.Comparator; /** - * Compares Strings and sorts them by length + * Compares Strings and sorts them by length (Longest fist). * * @author Rsl1122 * @since 3.6.2 diff --git a/Plan/test/test/java/utils/TestInit.java b/Plan/test/test/java/utils/TestInit.java index 6820623e3..9b8500611 100644 --- a/Plan/test/test/java/utils/TestInit.java +++ b/Plan/test/test/java/utils/TestInit.java @@ -51,7 +51,7 @@ public class TestInit { * Does not load any messages from anywhere because that would cause exceptions. */ public static void initEmptyLocale() { - new Locale(null); + new Locale(); } /** @@ -62,7 +62,7 @@ public class TestInit { * @param plan Mocked Plan */ public static void initLocale(Plan plan) { - new Locale(plan).loadLocale(); + new Locale().loadLocale(); } public static TestInit init() throws Exception { @@ -139,7 +139,7 @@ public class TestInit { when(planMock.getServerInfoManager()).thenReturn(bukkitServerInfoManager); ColorScheme cs = new ColorScheme(ChatColor.BLACK, ChatColor.BLACK, ChatColor.BLACK, ChatColor.BLACK); when(planMock.getColorScheme()).thenReturn(cs); - initLocale(planMock); + initLocale(null); RunnableFactory.activateTestMode(); }