diff --git a/src/main/java/com/volmit/iris/core/IrisSettings.java b/src/main/java/com/volmit/iris/core/IrisSettings.java index 905b4db91..b6f2b508f 100644 --- a/src/main/java/com/volmit/iris/core/IrisSettings.java +++ b/src/main/java/com/volmit/iris/core/IrisSettings.java @@ -34,101 +34,18 @@ import java.io.IOException; public class IrisSettings { public static transient IrisSettings settings; public int configurationVersion = 3; - private IrisSettingsCache cache = new IrisSettingsCache(); private IrisSettingsConcurrency concurrency = new IrisSettingsConcurrency(); private IrisSettingsGeneral general = new IrisSettingsGeneral(); private IrisSettingsGUI gui = new IrisSettingsGUI(); private IrisSettingsGenerator generator = new IrisSettingsGenerator(); private IrisSettingsStudio studio = new IrisSettingsStudio(); - public static int getPriority(int c) { - return Math.max(Math.min(c, Thread.MAX_PRIORITY), Thread.MIN_PRIORITY); - } - public static int getThreadCount(int c) { - if (c < 2 && c >= 0) { - return 2; - } - - return Math.max(2, c < 0 ? Runtime.getRuntime().availableProcessors() / -c : c); - } - - public static IrisSettings get() { - if (settings != null) { - return settings; - } - - IrisSettings defaults = new IrisSettings(); - JSONObject def = new JSONObject(new Gson().toJson(defaults)); - if (settings == null) { - settings = new IrisSettings(); - - File s = Iris.instance.getDataFile("settings.json"); - - if (!s.exists()) { - try { - IO.writeAll(s, new JSONObject(new Gson().toJson(settings)).toString(4)); - } catch (JSONException | IOException e) { - e.printStackTrace(); - Iris.reportError(e); - } - } else { - try { - String ss = IO.readAll(s); - settings = new Gson().fromJson(ss, IrisSettings.class); - try { - IO.writeAll(s, new JSONObject(new Gson().toJson(settings)).toString(4)); - } catch (IOException e) { - e.printStackTrace(); - } - } catch (Throwable ee) { - Iris.reportError(ee); - Iris.error("Configuration Error in settings.json! " + ee.getClass().getSimpleName() + ": " + ee.getMessage()); - } - } - - if (!s.exists()) { - try { - IO.writeAll(s, new JSONObject(new Gson().toJson(settings)).toString(4)); - } catch (JSONException | IOException e) { - Iris.reportError(e); - e.printStackTrace(); - } - } - } - - return settings; - } - - public static void invalidate() { - synchronized (settings) { - settings = null; - } - } - - public boolean isStudio() { - return getStudio().isStudio(); - } - - @SuppressWarnings("BooleanMethodIsAlwaysInverted") - public boolean isUseServerLaunchedGuis() { - return getGui().isUseServerLaunchedGuis(); - } - - public void forceSave() { - File s = Iris.instance.getDataFile("settings.json"); - - try { - IO.writeAll(s, new JSONObject(new Gson().toJson(settings)).toString(4)); - } catch (JSONException | IOException e) { - e.printStackTrace(); - Iris.reportError(e); - } - } - - @Data - public static class IrisSettingsCache { - public int complexCacheSize = 131072; + return switch (c) { + case -1, -2, -4 -> Runtime.getRuntime().availableProcessors() / -c; + case 0, 1, 2 -> 1; + default -> Math.max(c, 2); + }; } @Data @@ -151,6 +68,7 @@ public class IrisSettings { public int spins = 7; public int spinb = 8; + @SuppressWarnings("BooleanMethodIsAlwaysInverted") public boolean canUseCustomColors(VolmitSender volmitSender) { return volmitSender.isPlayer() ? useCustomColorsIngame : useConsoleCustomColors; } @@ -177,4 +95,61 @@ public class IrisSettings { public boolean disableTimeAndWeather = true; public boolean autoStartDefaultStudio = false; } + + public static IrisSettings get() { + if (settings != null) { + return settings; + } + + settings = new IrisSettings(); + + File s = Iris.instance.getDataFile("settings.json"); + + if (!s.exists()) { + try { + IO.writeAll(s, new JSONObject(new Gson().toJson(settings)).toString(4)); + } catch (JSONException | IOException e) { + e.printStackTrace(); + Iris.reportError(e); + } + } else { + try { + String ss = IO.readAll(s); + settings = new Gson().fromJson(ss, IrisSettings.class); + try { + IO.writeAll(s, new JSONObject(new Gson().toJson(settings)).toString(4)); + } catch (IOException e) { + e.printStackTrace(); + } + } catch (Throwable ee) { + Iris.reportError(ee); + Iris.error("Configuration Error in settings.json! " + ee.getClass().getSimpleName() + ": " + ee.getMessage()); + } + } + + if (settings.getConfigurationVersion() != new IrisSettings().getConfigurationVersion()) { + Iris.error("Existing configuration's version does not match current configuration version! Resetting..."); + settings = new IrisSettings(); + settings.forceSave(); + } + + return settings; + } + + public static void invalidate() { + synchronized (settings) { + settings = null; + } + } + + public void forceSave() { + File s = Iris.instance.getDataFile("settings.json"); + + try { + IO.writeAll(s, new JSONObject(new Gson().toJson(settings)).toString(4)); + } catch (JSONException | IOException e) { + e.printStackTrace(); + Iris.reportError(e); + } + } } diff --git a/src/main/java/com/volmit/iris/core/commands/CommandStudio.java b/src/main/java/com/volmit/iris/core/commands/CommandStudio.java index e6682f17b..46ceb342c 100644 --- a/src/main/java/com/volmit/iris/core/commands/CommandStudio.java +++ b/src/main/java/com/volmit/iris/core/commands/CommandStudio.java @@ -909,7 +909,7 @@ public class CommandStudio implements DecreeExecutor { * @return true if server GUIs are not enabled */ private boolean noGUI() { - if (!IrisSettings.get().isUseServerLaunchedGuis()) { + if (!IrisSettings.get().getGui().isUseServerLaunchedGuis()) { sender().sendMessage(C.RED + "You must have server launched GUIs enabled in the settings!"); return true; } diff --git a/src/main/java/com/volmit/iris/util/decree/virtual/VirtualDecreeCommand.java b/src/main/java/com/volmit/iris/util/decree/virtual/VirtualDecreeCommand.java index 402f6143e..bc83c792b 100644 --- a/src/main/java/com/volmit/iris/util/decree/virtual/VirtualDecreeCommand.java +++ b/src/main/java/com/volmit/iris/util/decree/virtual/VirtualDecreeCommand.java @@ -189,7 +189,7 @@ public class VirtualDecreeCommand { } private boolean invokeTabComplete(KList args, KList skip, KList tabs, String raw) { - if (isStudio() && !IrisSettings.get().isStudio()) { + if (isStudio() && !IrisSettings.get().getStudio().isStudio()) { return false; } @@ -388,7 +388,7 @@ public class VirtualDecreeCommand { } public boolean invoke(VolmitSender sender, KList args, KList skip) { - if (isStudio() && !IrisSettings.get().isStudio()) { + if (isStudio() && !IrisSettings.get().getStudio().isStudio()) { sender.sendMessage(C.RED + "To use Iris Studio Commands, please enable studio in Iris/settings.json (settings auto-reload)"); return false; }