Settings rework and encapsulation (#1330)

* Settings rework and encapsulation

* Settings overhaul 3

Move everything in the new system to a new API package

Require rules using the new system to specify descriptions in their translation files

Implement temporary system on top of current translations to allow that to work properly

* Fix getRules fallback

* Final touches to settings refactor

* Mention that set shouldn't throw and remove comment

* Change translation keys to unify with translation PR

* Deprecate printAllRulesToLog to allow using any PrintStream

* Remove restriction of CarpetRules being Comparable

And fix overreaction in a deprecation message

* Bring back user input to rule observers

* Make Validator deprecation warning not spam on use

* Remove unneeded abstract methods in CarpetRule already inherited from CarpetRule

They literally do nothing, don't even force them to be implemented, and may be confusing as to why they're there or as if they are needed to be implemented

* Fix legacy SettingsManager field not being assigned correctly

* Finish fixing conflicts from future merge

* Actually fix remaining conflicts from future merge (missing imports)

* Import the newer rule in SettingsManager

* Refactor Validators

* Don't spam deprecation warnings on custom managers

* Get out the var

* Fix missing warning in old rule observers

* Update src/main/java/carpet/api/settings/Rule.java

* Remove `@ApiStatus.Experimental` annotations

* Fix incorrect `@deprecated` javadoc tags

By removing them given the class is deprecated anyway and else there's too much noise

* Add missing static import

* Remove TypedField and use a regular field

I liked that class tbh, but it's true that it wasn't really needed

* Move `field` assignment out of for removal

* Fix not following contract of `type()` returning boxed value

Previously handled by TypedField

* Fix few incorrect translation keys

* Remove resaving of comments when saving config files

Comments ignoring is still a thing when reading, so `locked` configs for example will keep comments

* Condense rule load messages in one

* Only print loaded settings message when at least one setting was changed

* Fix legacy validator detector

* Tweak and add more context to response to invalid values

* Fix strict validator not actually erroring on invalid input
This commit is contained in:
altrisi authored and GitHub committed 2022-06-27 13:12:15 +02:00
1 parent f5a92ebb8e
commit 0a972d6c56
44 files changed
+2687 -1616

No files matched your search

+20 -5
View File
@@ -1,8 +1,10 @@
package carpet;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import carpet.commands.CounterCommand;
import carpet.commands.DistanceCommand;
@@ -22,7 +24,7 @@ import carpet.helpers.HopperCounter;
import carpet.helpers.TickSpeed;
import carpet.logging.LoggerRegistry;
import carpet.script.CarpetScriptServer;
import carpet.settings.SettingsManager;
import carpet.api.settings.SettingsManager;
import carpet.logging.HUDController;
import carpet.utils.FabricAPIHooks;
import carpet.utils.MobAI;
@@ -43,7 +45,7 @@ public class CarpetServer // static for now - easier to handle all around the co
public static MinecraftServer minecraft_server;
private static CommandDispatcher<CommandSourceStack> currentCommandDispatcher;
public static CarpetScriptServer scriptServer;
public static SettingsManager settingsManager;
public static carpet.settings.SettingsManager settingsManager; // to change type to api type, can't change right now because of binary and source compat
public static final List<CarpetExtension> extensions = new ArrayList<>();
// Separate from onServerLoaded, because a server can be loaded multiple times in singleplayer
@@ -77,7 +79,7 @@ public class CarpetServer // static for now - easier to handle all around the co
// to register before this call in a ModInitializer (declared in fabric.mod.json)
public static void onGameStarted()
{
settingsManager = new SettingsManager(CarpetSettings.carpetVersion, "carpet", "Carpet Mod");
settingsManager = new carpet.settings.SettingsManager(CarpetSettings.carpetVersion, "carpet", "Carpet Mod");
settingsManager.parseSettingsClass(CarpetSettings.class);
extensions.forEach(CarpetExtension::onGameStarted);
FabricAPIHooks.initialize();
@@ -92,7 +94,7 @@ public class CarpetServer // static for now - easier to handle all around the co
settingsManager.attachServer(server);
extensions.forEach(e -> {
SettingsManager sm = e.customSettingsManager();
SettingsManager sm = e.extensionSettingsManager();
if (sm != null) sm.attachServer(server);
e.onServerLoaded(server);
});
@@ -129,7 +131,7 @@ public class CarpetServer // static for now - easier to handle all around the co
}
settingsManager.registerCommand(dispatcher, commandBuildContext);
extensions.forEach(e -> {
SettingsManager sm = e.customSettingsManager();
SettingsManager sm = e.extensionSettingsManager();
if (sm != null) sm.registerCommand(dispatcher, commandBuildContext);
});
TickCommand.register(dispatcher, commandBuildContext);
@@ -204,6 +206,10 @@ public class CarpetServer // static for now - easier to handle all around the co
public static void onServerDoneClosing(MinecraftServer server)
{
settingsManager.detachServer();
extensions.forEach(e -> {
SettingsManager manager = e.extensionSettingsManager();
if (manager != null) manager.detachServer();
});
}
public static void registerExtensionLoggers()
@@ -216,5 +222,14 @@ public class CarpetServer // static for now - easier to handle all around the co
scriptServer.reload(server);
extensions.forEach(e -> e.onReload(server));
}
private static final Set<CarpetExtension> warnedOutdatedManagerProviders = new HashSet<>();
static void warnOutdatedManager(CarpetExtension ext)
{
if (!warnedOutdatedManagerProviders.contains(ext))
CarpetSettings.LOG.warn("""
%s is providing a SettingsManager from an outdated method in CarpetExtension!
This behaviour will not work in later Carpet versions and the manager won't be registered!""".formatted(ext.getClass().getName()));
}
}