categories not an enum, so its easier to extend. Validators can modify value.
This commit is contained in:
1 parent
92f40c36b8
commit
f05c661fbf
7 files changed
+65
-54
No files matched your search
@@ -3,7 +3,6 @@ package carpet.commands;
|
|||||||
import carpet.CarpetServer;
|
import carpet.CarpetServer;
|
||||||
import carpet.CarpetSettings;
|
import carpet.CarpetSettings;
|
||||||
import carpet.settings.ParsedRule;
|
import carpet.settings.ParsedRule;
|
||||||
import carpet.settings.RuleCategory;
|
|
||||||
import carpet.settings.SettingsManager;
|
import carpet.settings.SettingsManager;
|
||||||
import carpet.utils.Messenger;
|
import carpet.utils.Messenger;
|
||||||
import com.mojang.brigadier.CommandDispatcher;
|
import com.mojang.brigadier.CommandDispatcher;
|
||||||
@@ -11,16 +10,12 @@ import com.mojang.brigadier.arguments.StringArgumentType;
|
|||||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||||
import net.minecraft.server.command.ServerCommandSource;
|
import net.minecraft.server.command.ServerCommandSource;
|
||||||
import net.minecraft.server.command.CommandManager;
|
|
||||||
import net.minecraft.entity.player.PlayerEntity;
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
import net.minecraft.text.BaseText;
|
import net.minecraft.text.BaseText;
|
||||||
import net.minecraft.text.Text;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
import static net.minecraft.server.command.CommandManager.argument;
|
import static net.minecraft.server.command.CommandManager.argument;
|
||||||
import static net.minecraft.server.command.CommandManager.literal;
|
import static net.minecraft.server.command.CommandManager.literal;
|
||||||
@@ -43,7 +38,7 @@ public class ParcetCommand
|
|||||||
"Current CarpetMod Startup Settings from carpet.conf",
|
"Current CarpetMod Startup Settings from carpet.conf",
|
||||||
CarpetServer.settingsManager.findStartupOverrides()))).
|
CarpetServer.settingsManager.findStartupOverrides()))).
|
||||||
then(argument("tag",StringArgumentType.word()).
|
then(argument("tag",StringArgumentType.word()).
|
||||||
suggests( (c, b)->suggestMatching(Arrays.stream(RuleCategory.values()).map(r -> r.lowerCase), b)).
|
suggests( (c, b)->suggestMatching(SettingsManager.getCategories(), b)).
|
||||||
executes( (c) -> listSettings(c.getSource(),
|
executes( (c) -> listSettings(c.getSource(),
|
||||||
String.format("CarpetMod Settings matching \"%s\"", StringArgumentType.getString(c, "tag")),
|
String.format("CarpetMod Settings matching \"%s\"", StringArgumentType.getString(c, "tag")),
|
||||||
CarpetServer.settingsManager.getRulesMatching(StringArgumentType.getString(c, "tag"))))));
|
CarpetServer.settingsManager.getRulesMatching(StringArgumentType.getString(c, "tag"))))));
|
||||||
@@ -94,7 +89,7 @@ public class ParcetCommand
|
|||||||
|
|
||||||
List<BaseText> tags = new ArrayList<>();
|
List<BaseText> tags = new ArrayList<>();
|
||||||
tags.add(Messenger.c("w Tags: "));
|
tags.add(Messenger.c("w Tags: "));
|
||||||
for (String t: Arrays.stream(RuleCategory.values()).sorted().map(c -> c.lowerCase).collect(Collectors.toList()))
|
for (String t: SettingsManager.getCategories())
|
||||||
{
|
{
|
||||||
tags.add(Messenger.c("c ["+t+"]", "^g list all "+t+" settings","!/carpet list "+t));
|
tags.add(Messenger.c("c ["+t+"]", "^g list all "+t+" settings","!/carpet list "+t));
|
||||||
tags.add(Messenger.c("w , "));
|
tags.add(Messenger.c("w , "));
|
||||||
@@ -191,7 +186,7 @@ public class ParcetCommand
|
|||||||
PlayerEntity player = source.getPlayer();
|
PlayerEntity player = source.getPlayer();
|
||||||
List<Object> tags = new ArrayList<>();
|
List<Object> tags = new ArrayList<>();
|
||||||
tags.add("w Browse Categories:\n");
|
tags.add("w Browse Categories:\n");
|
||||||
for (String t : Arrays.stream(RuleCategory.values()).map(r -> r.lowerCase).collect(Collectors.toList()))
|
for (String t : SettingsManager.getCategories())
|
||||||
{
|
{
|
||||||
tags.add("c [" + t+"]");
|
tags.add("c [" + t+"]");
|
||||||
tags.add("^g list all " + t + " settings");
|
tags.add("^g list all " + t + " settings");
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package carpet.settings;
|
package carpet.settings;
|
||||||
|
|
||||||
|
import carpet.CarpetServer;
|
||||||
import carpet.utils.Messenger;
|
import carpet.utils.Messenger;
|
||||||
import com.google.common.collect.ImmutableList;
|
import com.google.common.collect.ImmutableList;
|
||||||
import net.minecraft.server.command.ServerCommandSource;
|
import net.minecraft.server.command.ServerCommandSource;
|
||||||
@@ -12,13 +13,11 @@ import java.util.List;
|
|||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
public final class ParsedRule<T> implements Comparable<ParsedRule> {
|
public final class ParsedRule<T> implements Comparable<ParsedRule> {
|
||||||
public final Rule rule;
|
|
||||||
public final Field field;
|
public final Field field;
|
||||||
|
|
||||||
public final String name;
|
public final String name;
|
||||||
public final String description;
|
public final String description;
|
||||||
public final ImmutableList<String> extraInfo;
|
public final ImmutableList<String> extraInfo;
|
||||||
public final ImmutableList<RuleCategory> categories;
|
public final ImmutableList<String> categories;
|
||||||
public final ImmutableList<String> options;
|
public final ImmutableList<String> options;
|
||||||
public boolean isStrict;
|
public boolean isStrict;
|
||||||
public final Class<T> type;
|
public final Class<T> type;
|
||||||
@@ -37,7 +36,6 @@ public final class ParsedRule<T> implements Comparable<ParsedRule> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ParsedRule(Field field, Rule rule) {
|
ParsedRule(Field field, Rule rule) {
|
||||||
this.rule = rule;
|
|
||||||
this.field = field;
|
this.field = field;
|
||||||
this.name = rule.name().isEmpty() ? field.getName() : rule.name();
|
this.name = rule.name().isEmpty() ? field.getName() : rule.name();
|
||||||
this.type = (Class<T>) field.getType();
|
this.type = (Class<T>) field.getType();
|
||||||
@@ -52,16 +50,20 @@ public final class ParsedRule<T> implements Comparable<ParsedRule> {
|
|||||||
}
|
}
|
||||||
if (categories.contains(RuleCategory.COMMANDS))
|
if (categories.contains(RuleCategory.COMMANDS))
|
||||||
this.validators.add(callConstructor(Validator._COMMAND.class));
|
this.validators.add(callConstructor(Validator._COMMAND.class));
|
||||||
|
|
||||||
this.defaultValue = get();
|
this.defaultValue = get();
|
||||||
this.defaultAsString = convertToString(this.defaultValue);
|
this.defaultAsString = convertToString(this.defaultValue);
|
||||||
if (this.type == boolean.class) {
|
if (this.type == boolean.class)
|
||||||
|
{
|
||||||
this.isStrict = true; // it has no sense otherwise
|
this.isStrict = true; // it has no sense otherwise
|
||||||
this.options = ImmutableList.of("true", "false");
|
this.options = ImmutableList.of("true", "false");
|
||||||
} else if (this.type.isEnum()) {
|
}
|
||||||
|
else if (this.type.isEnum())
|
||||||
|
{
|
||||||
this.isStrict = true; // it has no sense otherwise
|
this.isStrict = true; // it has no sense otherwise
|
||||||
this.options = Arrays.stream(this.type.getEnumConstants()).map(e -> ((Enum) e).name().toLowerCase(Locale.ROOT)).collect(ImmutableList.toImmutableList());
|
this.options = Arrays.stream(this.type.getEnumConstants()).map(e -> ((Enum) e).name().toLowerCase(Locale.ROOT)).collect(ImmutableList.toImmutableList());
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
this.options = ImmutableList.copyOf(rule.options());
|
this.options = ImmutableList.copyOf(rule.options());
|
||||||
}
|
}
|
||||||
if (isStrict)
|
if (isStrict)
|
||||||
@@ -70,7 +72,8 @@ public final class ParsedRule<T> implements Comparable<ParsedRule> {
|
|||||||
|
|
||||||
|
|
||||||
public ParsedRule<T> set(ServerCommandSource source, String value) {
|
public ParsedRule<T> set(ServerCommandSource source, String value) {
|
||||||
//if locked - prevent
|
if (CarpetServer.settingsManager.locked)
|
||||||
|
return null;
|
||||||
if (type == String.class)
|
if (type == String.class)
|
||||||
{
|
{
|
||||||
return set(source, (T) value, value);
|
return set(source, (T) value, value);
|
||||||
@@ -95,12 +98,12 @@ public final class ParsedRule<T> implements Comparable<ParsedRule> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ParsedRule<T> set(ServerCommandSource source, T value, String stringValue) {
|
ParsedRule<T> set(ServerCommandSource source, T value, String stringValue) {
|
||||||
try {
|
try {
|
||||||
for (Validator<T> validator : this.validators)
|
for (Validator<T> validator : this.validators)
|
||||||
{
|
{
|
||||||
boolean allowed = validator.validate(source, this, value, stringValue);
|
value = validator.validate(source, this, value, stringValue);
|
||||||
if (!allowed)
|
if (value == null)
|
||||||
{
|
{
|
||||||
Messenger.m(source, "r Wrong value for " + name + ": " + stringValue);
|
Messenger.m(source, "r Wrong value for " + name + ": " + stringValue);
|
||||||
return null;
|
return null;
|
||||||
@@ -112,6 +115,7 @@ public final class ParsedRule<T> implements Comparable<ParsedRule> {
|
|||||||
catch (IllegalAccessException e)
|
catch (IllegalAccessException e)
|
||||||
{
|
{
|
||||||
Messenger.m(source, "r Unable to access setting for "+name);
|
Messenger.m(source, "r Unable to access setting for "+name);
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ import java.lang.annotation.Target;
|
|||||||
/**
|
/**
|
||||||
* A list of categories the rule is in
|
* A list of categories the rule is in
|
||||||
*/
|
*/
|
||||||
RuleCategory[] category();
|
String[] category();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Options to select in menu.
|
* Options to select in menu.
|
||||||
@@ -47,9 +47,9 @@ import java.lang.annotation.Target;
|
|||||||
/**
|
/**
|
||||||
* if a rule is not strict - can take any value, otherwise it needs to match
|
* if a rule is not strict - can take any value, otherwise it needs to match
|
||||||
* any of the options
|
* any of the options
|
||||||
* For enums, its always strict
|
* For enums, its always strict, same for booleans - no need to set that for them.
|
||||||
*/
|
*/
|
||||||
boolean strict() default true;
|
boolean strict() default false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The class of the validator checked when the rule is changed.
|
* The class of the validator checked when the rule is changed.
|
||||||
|
|||||||
@@ -1,13 +1,12 @@
|
|||||||
package carpet.settings;
|
package carpet.settings;
|
||||||
|
|
||||||
import java.util.Locale;
|
public class RuleCategory {
|
||||||
|
public static final String BUGFIX = "bugfix";
|
||||||
|
public static final String SURVIVAL = "survival";
|
||||||
|
public static final String CREATIVE = "creative";
|
||||||
|
public static final String EXPERIMENTAL = "experimental";
|
||||||
|
public static final String OPTIMIZATIONS = "optimizations";
|
||||||
|
public static final String FEATURE = "feature";
|
||||||
|
public static final String COMMANDS = "commands";
|
||||||
|
|
||||||
public enum RuleCategory {
|
|
||||||
BUGFIX, SURVIVAL, CREATIVE, EXPERIMENTAL, OPTIMIZATIONS, FEATURE, COMMANDS ;
|
|
||||||
|
|
||||||
public final String lowerCase;
|
|
||||||
|
|
||||||
RuleCategory() {
|
|
||||||
this.lowerCase = this.name().toLowerCase(Locale.ROOT);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
package carpet.settings;
|
package carpet.settings;
|
||||||
|
|
||||||
|
import net.minecraft.server.command.ServerCommandSource;
|
||||||
|
|
||||||
import static carpet.settings.RuleCategory.BUGFIX;
|
import static carpet.settings.RuleCategory.BUGFIX;
|
||||||
import static carpet.settings.RuleCategory.COMMANDS;
|
import static carpet.settings.RuleCategory.COMMANDS;
|
||||||
import static carpet.settings.RuleCategory.CREATIVE;
|
import static carpet.settings.RuleCategory.CREATIVE;
|
||||||
@@ -28,11 +30,20 @@ public class Settings
|
|||||||
@Rule(desc = "fill/clone/setblock and structure blocks cause block updates", category = CREATIVE)
|
@Rule(desc = "fill/clone/setblock and structure blocks cause block updates", category = CREATIVE)
|
||||||
public static boolean fillUpdates = true;
|
public static boolean fillUpdates = true;
|
||||||
|
|
||||||
|
|
||||||
|
private static class CheckFillLimitLimits extends Validator<Integer>
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
Integer validate(ServerCommandSource source, ParsedRule<Integer> currentRule, Integer newValue, String typedString)
|
||||||
|
{
|
||||||
|
return newValue < 20000000 ? newValue : null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@Rule(
|
@Rule(
|
||||||
desc = "Customizable fill/clone volume limit",
|
desc = "Customizable fill/clone volume limit",
|
||||||
options = {"32768", "250000", "1000000"},
|
options = {"32768", "250000", "1000000"},
|
||||||
validate = Validator.POSITIVE_NUMBER.class,
|
validate = {Validator.POSITIVE_NUMBER.class, CheckFillLimitLimits.class},
|
||||||
category = CREATIVE
|
category = {CREATIVE, "skyblock"}
|
||||||
)
|
)
|
||||||
public static int fillLimit = 32768;
|
public static int fillLimit = 32768;
|
||||||
|
|
||||||
|
|||||||
@@ -11,14 +11,7 @@ import org.apache.logging.log4j.Logger;
|
|||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Locale;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@@ -47,6 +40,13 @@ public class SettingsManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Iterable<String> getCategories()
|
||||||
|
{
|
||||||
|
Set<String> categories = new HashSet<>();
|
||||||
|
getRules().stream().map(r -> r.categories).forEach(categories::addAll);
|
||||||
|
return categories;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public ParsedRule getRule(String name) {
|
public ParsedRule getRule(String name) {
|
||||||
return rules.get(name);
|
return rules.get(name);
|
||||||
@@ -75,7 +75,7 @@ public class SettingsManager {
|
|||||||
return server.getLevelStorage().resolveFile(server.getLevelName(), "carpet.conf");
|
return server.getLevelStorage().resolveFile(server.getLevelName(), "carpet.conf");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void disableBooleanFromCategory(RuleCategory category) {
|
public void disableBooleanFromCategory(String category) {
|
||||||
for (ParsedRule<?> rule : rules.values()) {
|
for (ParsedRule<?> rule : rules.values()) {
|
||||||
if (rule.type != boolean.class || !rule.categories.contains(category)) continue;
|
if (rule.type != boolean.class || !rule.categories.contains(category)) continue;
|
||||||
((ParsedRule<Boolean>) rule).set(server.getCommandSource(), false, "false");
|
((ParsedRule<Boolean>) rule).set(server.getCommandSource(), false, "false");
|
||||||
@@ -223,7 +223,7 @@ public class SettingsManager {
|
|||||||
String lcSearch = search.toLowerCase(Locale.ROOT);
|
String lcSearch = search.toLowerCase(Locale.ROOT);
|
||||||
return getRulesMatching(rule -> {
|
return getRulesMatching(rule -> {
|
||||||
if (rule.name.toLowerCase(Locale.ROOT).contains(lcSearch)) return true;
|
if (rule.name.toLowerCase(Locale.ROOT).contains(lcSearch)) return true;
|
||||||
for (RuleCategory c : rule.categories) if (c.lowerCase.equals(search)) return true;
|
for (String c : rule.categories) if (c.toLowerCase(Locale.ROOT).equals(search)) return true;
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,40 +11,42 @@ public abstract class Validator<T> {
|
|||||||
* Validate the new value of a rule
|
* Validate the new value of a rule
|
||||||
* @return true if valid, false if new rule invalid.
|
* @return true if valid, false if new rule invalid.
|
||||||
*/
|
*/
|
||||||
abstract boolean validate(ServerCommandSource source, ParsedRule<T> currentRule, T newValue, String string);
|
abstract T validate(ServerCommandSource source, ParsedRule<T> currentRule, T newValue, String string);
|
||||||
|
|
||||||
|
|
||||||
public static class POSITIVE_NUMBER<T extends Number> extends Validator<T> {
|
public static class POSITIVE_NUMBER<T extends Number> extends Validator<T> {
|
||||||
@Override
|
@Override
|
||||||
public boolean validate(ServerCommandSource source, ParsedRule<T> currentRule, T newValue, String string) {
|
public T validate(ServerCommandSource source, ParsedRule<T> currentRule, T newValue, String string) {
|
||||||
return newValue.doubleValue() > 0;
|
return newValue.doubleValue() > 0 ? newValue : null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static class _COMMAND<T> extends Validator<T> {
|
public static class _COMMAND<T> extends Validator<T> {
|
||||||
@Override
|
@Override
|
||||||
public boolean validate(ServerCommandSource source, ParsedRule<T> currentRule, T newValue, String string) {
|
public T validate(ServerCommandSource source, ParsedRule<T> currentRule, T newValue, String string) {
|
||||||
if (CarpetServer.settingsManager != null)
|
if (CarpetServer.settingsManager != null)
|
||||||
CarpetServer.settingsManager.notifyPlayersCommandsChanged();
|
CarpetServer.settingsManager.notifyPlayersCommandsChanged();
|
||||||
return true;
|
return newValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class WIP<T> extends Validator<T> {
|
public static class WIP<T> extends Validator<T> {
|
||||||
@Override
|
@Override
|
||||||
public boolean validate(ServerCommandSource source, ParsedRule<T> currentRule, T newValue, String string) {
|
public T validate(ServerCommandSource source, ParsedRule<T> currentRule, T newValue, String string) {
|
||||||
if (source != null)
|
if (source != null)
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Messenger.m(source, "r Work in progress - limited or no functionality available for "+currentRule.name);
|
Messenger.m(source, "r Work in progress - limited or no functionality available for "+currentRule.name);
|
||||||
}
|
}
|
||||||
catch (NullPointerException ignored) { }
|
catch (NullPointerException ignored) { }
|
||||||
return true;
|
return newValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public static class _STRICT<T> extends Validator<T> {
|
public static class _STRICT<T> extends Validator<T> {
|
||||||
@Override
|
@Override
|
||||||
public boolean validate(ServerCommandSource source, ParsedRule<T> currentRule, T newValue, String string) {
|
public T validate(ServerCommandSource source, ParsedRule<T> currentRule, T newValue, String string) {
|
||||||
if (!currentRule.options.contains(string.toLowerCase(Locale.ROOT)))
|
if (!currentRule.options.contains(string.toLowerCase(Locale.ROOT)))
|
||||||
{
|
{
|
||||||
if (source != null)
|
if (source != null)
|
||||||
@@ -55,9 +57,9 @@ public abstract class Validator<T> {
|
|||||||
catch (NullPointerException ignored)
|
catch (NullPointerException ignored)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
return true;
|
return newValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user