* 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
215 lines
9.2 KiB
Java
215 lines
9.2 KiB
Java
package carpet.settings;
|
|
|
|
import carpet.CarpetServer;
|
|
import carpet.CarpetSettings;
|
|
import carpet.api.settings.CarpetRule;
|
|
import carpet.utils.CommandHelper;
|
|
import com.mojang.brigadier.CommandDispatcher;
|
|
import net.minecraft.commands.CommandBuildContext;
|
|
import net.minecraft.commands.CommandSourceStack;
|
|
import net.minecraft.core.RegistryAccess;
|
|
import net.minecraft.server.MinecraftServer;
|
|
import org.apache.logging.log4j.util.TriConsumer;
|
|
import java.util.Collection;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* Manages and parses Carpet rules with their own command.
|
|
* @deprecated Use {@link carpet.api.settings.SettingsManager} instead
|
|
*/
|
|
@Deprecated(forRemoval = true)
|
|
public class SettingsManager extends carpet.api.settings.SettingsManager
|
|
{
|
|
/**
|
|
* @deprecated Use {@link #locked()} instead
|
|
*/
|
|
@Deprecated(forRemoval = true) //to private (or protected?)
|
|
public boolean locked;
|
|
|
|
/**
|
|
* Creates a new {@link SettingsManager} without a fancy name.
|
|
* @see #SettingsManager(String, String, String)
|
|
*
|
|
* @param version A {@link String} with the mod's version
|
|
* @param identifier A {@link String} with the mod's id, will be the command name
|
|
* @deprecated This type is deprecated, use {@link carpet.api.settings.SettingsManager#SettingsManager(String, String, String)} instead
|
|
*/
|
|
@Deprecated(forRemoval = true)
|
|
public SettingsManager(String version, String identifier)
|
|
{
|
|
this(version, identifier, identifier);
|
|
}
|
|
|
|
/**
|
|
* Creates a new {@link SettingsManager} with a fancy name
|
|
*
|
|
* @param version A {@link String} with the mod's version
|
|
* @param identifier A {@link String} with the mod's id, will be the command name
|
|
* @param fancyName A {@link String} being the mod's fancy name.
|
|
*
|
|
* @deprecated This type is deprecated, use {@link carpet.api.settings.SettingsManager#SettingsManager(String, String, String)} instead
|
|
*/
|
|
@Deprecated(forRemoval = true)
|
|
public SettingsManager(String version, String identifier, String fancyName)
|
|
{
|
|
super(version, identifier, fancyName);
|
|
}
|
|
|
|
/**
|
|
* <p>Adds a custom rule observer to changes in rules from
|
|
* <b>this specific</b> {@link SettingsManager} instance.</p>
|
|
*
|
|
* <p>This <b>outdated</b> method may not be able to listen to all {@link CarpetRule} implementations</p>
|
|
*
|
|
* @see SettingsManager#registerGlobalRuleObserver(RuleObserver)
|
|
*
|
|
* @param observer A {@link TriConsumer} that will be called with
|
|
* the used {@link CommandSourceStack}, the changed
|
|
* {@link ParsedRule} and a {@link String} being the
|
|
* value that the user typed.
|
|
* @deprecated Use {@link SettingsManager#registerRuleObserver(RuleObserver)} instead.
|
|
*/
|
|
@Deprecated(forRemoval = true) //to remove
|
|
public void addRuleObserver(TriConsumer<CommandSourceStack, ParsedRule<?>, String> observer)
|
|
{
|
|
registerRuleObserver((source, rule, stringValue) -> {
|
|
if (rule instanceof ParsedRule<?> pr)
|
|
observer.accept(source, pr, stringValue);
|
|
else
|
|
CarpetSettings.LOG.warn("Failed to notify observer '" + observer.getClass().getName() + "' about rule change");
|
|
});
|
|
CarpetSettings.LOG.warn("""
|
|
Extension added outdated rule observer, this is deprecated and will crash in later carpet versions \
|
|
(way before the rest of the old settings api because of relying on log4j)!
|
|
The observer class name is '%s'""".formatted(observer.getClass().getName()));
|
|
}
|
|
|
|
/**
|
|
* Adds a custom rule observer to changes in rules from
|
|
* <b>any</b> registered {@link SettingsManager} instance.
|
|
* @see SettingsManager#addRuleObserver(TriConsumer)
|
|
*
|
|
* @param observer A {@link TriConsumer} that will be called with
|
|
* the used {@link CommandSourceStack}, the changed
|
|
* {@link ParsedRule} and a {@link String} being the
|
|
* value that the user typed.
|
|
* @deprecated Use {@link SettingsManager#registerRuleObserver(RuleObserver)} instead, given this one can't deal with {@link CarpetRule}
|
|
*/
|
|
@Deprecated(forRemoval = true) // to remove. This isn't really used anywhere
|
|
public static void addGlobalRuleObserver(TriConsumer<CommandSourceStack, ParsedRule<?>, String> observer)
|
|
{
|
|
registerGlobalRuleObserver((source, rule, stringValue) -> {
|
|
if (rule instanceof ParsedRule<?> pr)
|
|
observer.accept(source, pr, stringValue);
|
|
else
|
|
CarpetSettings.LOG.warn("Failed to notify observer '" + observer.getClass().getName() + "' about rule change");
|
|
});
|
|
CarpetSettings.LOG.warn("""
|
|
Extension added outdated rule observer, this is deprecated and will crash in later carpet versions \
|
|
(way before the rest of the old settings api because of relying on log4j)!
|
|
The observer class name is '%s'""".formatted(observer.getClass().getName()));
|
|
}
|
|
|
|
/**
|
|
* @deprecated Use {@link #identifier()} instead
|
|
*/
|
|
@Deprecated(forRemoval = true)
|
|
public String getIdentifier() {
|
|
return identifier();
|
|
}
|
|
|
|
/**
|
|
* <p>Gets a registered {@link ParsedRule} in this {@link SettingsManager} by its name.</p>
|
|
* @param name The rule name
|
|
* @return The {@link ParsedRule} with that name
|
|
* @deprecated Use {@link #getCarpetRule(String)} instead. This method is not able to return rules not implemented by {@link ParsedRule}
|
|
*/
|
|
@Deprecated(forRemoval = true)
|
|
public ParsedRule<?> getRule(String name)
|
|
{
|
|
return getCarpetRule(name) instanceof ParsedRule<?> pr ? pr : null;
|
|
}
|
|
|
|
/**
|
|
* @return A {@link Collection} of the registered {@link ParsedRule}s in this {@link SettingsManager}.
|
|
* @deprecated Use {@link #getCarpetRules()} instead. This method won't be able to return rules not implemented by {@link ParsedRule}
|
|
*/
|
|
@Deprecated(forRemoval = true)
|
|
public Collection<ParsedRule<?>> getRules()
|
|
{
|
|
return List.of(getCarpetRules().stream().filter(ParsedRule.class::isInstance).map(ParsedRule.class::cast).toArray(ParsedRule[]::new));
|
|
}
|
|
|
|
/**
|
|
* @deprecated Use {@link #dumpAllRulesToStream(java.io.PrintStream, String)} instead
|
|
*/
|
|
@Deprecated(forRemoval = true)
|
|
public int printAllRulesToLog(String category) {
|
|
return dumpAllRulesToStream(System.out, category);
|
|
}
|
|
|
|
/**
|
|
* Notifies all players that the commands changed by resending the command tree.
|
|
* @deprecated While there's not an API replacement for this (at least yet),
|
|
* you can use {@link CommandHelper#notifyPlayersCommandsChanged(MinecraftServer)} instead
|
|
*/
|
|
@Deprecated(forRemoval = true)
|
|
public void notifyPlayersCommandsChanged()
|
|
{
|
|
CommandHelper.notifyPlayersCommandsChanged(CarpetServer.minecraft_server);
|
|
}
|
|
|
|
/**
|
|
* Returns whether the {@link CommandSourceStack} can execute
|
|
* a command given the required permission level, according to
|
|
* Carpet's standard for permissions.
|
|
* @param source The origin {@link CommandSourceStack}
|
|
* @param commandLevel The permission level
|
|
* @return Whether or not the {@link CommandSourceStack} meets the required level
|
|
*
|
|
* @deprecated While there's not an API replacement for this (at least yet),
|
|
* you can use {@link CommandHelper#canUseCommand(CommandSourceStack, Object)} instead
|
|
*/
|
|
@Deprecated(forRemoval = true)
|
|
public static boolean canUseCommand(CommandSourceStack source, Object commandLevel)
|
|
{
|
|
return CommandHelper.canUseCommand(source, commandLevel);
|
|
}
|
|
|
|
/**
|
|
* @param commandLevel A {@link String} being a permission level according to
|
|
* Carpet's standard for permissions (either 0-4, a {@link boolean},
|
|
* or "ops".
|
|
* @return An {@link int} with the translated Vanilla permission level
|
|
*
|
|
* @deprecated While there's not an API replacement for this (at least yet),
|
|
* you can use the similar {@link CommandHelper#canUseCommand(CommandSourceStack, Object)} instead
|
|
* @apiNote Note that this method returns {@code 2} for {@code false} and {@code 0} for {@code true}.
|
|
* You probably want to use {@link #canUseCommand(CommandSourceStack, Object)}e
|
|
*/
|
|
@Deprecated(forRemoval = true)
|
|
public static int getCommandLevel(String commandLevel)
|
|
{
|
|
switch (commandLevel)
|
|
{
|
|
case "true": return 2;
|
|
case "false": return 0;
|
|
case "ops": return 2; // typical for other cheaty commands
|
|
case "0":
|
|
case "1":
|
|
case "2":
|
|
case "3":
|
|
case "4":
|
|
return Integer.parseInt(commandLevel);
|
|
}
|
|
return 0;
|
|
}
|
|
@Deprecated(forRemoval = true)
|
|
public void registerCommand(CommandDispatcher<CommandSourceStack> dispatcher)
|
|
{
|
|
final CommandBuildContext context = new CommandBuildContext(RegistryAccess.BUILTIN.get());
|
|
context.missingTagAccessPolicy(CommandBuildContext.MissingTagAccessPolicy.RETURN_EMPTY);
|
|
registerCommand(dispatcher, context);
|
|
}
|
|
}
|