1 parent
5c76723091
commit
b9eee6e58b
12 files changed
+166
-26
No files matched your search
@@ -4,6 +4,7 @@ import carpet.CarpetServer;
|
||||
import carpet.utils.Translations;
|
||||
import carpet.utils.Messenger;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Sets;
|
||||
import net.minecraft.server.command.ServerCommandSource;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
@@ -12,6 +13,7 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
|
||||
import static carpet.utils.Translations.tr;
|
||||
|
||||
@@ -19,6 +21,7 @@ public final class ParsedRule<T> implements Comparable<ParsedRule> {
|
||||
public final Field field;
|
||||
public final String name;
|
||||
public final String description;
|
||||
public final String scarpetApp;
|
||||
public final ImmutableList<String> extraInfo;
|
||||
public final ImmutableList<String> categories;
|
||||
public final ImmutableList<String> options;
|
||||
@@ -28,8 +31,10 @@ public final class ParsedRule<T> implements Comparable<ParsedRule> {
|
||||
public final List<Validator<T>> validators;
|
||||
public final T defaultValue;
|
||||
public final String defaultAsString;
|
||||
public final SettingsManager settingsManager;
|
||||
private static final Set<Class<?>> NUMBER_CLASSES = Sets.newHashSet(byte.class, short.class, int.class, long.class, float.class, double.class);
|
||||
|
||||
ParsedRule(Field field, Rule rule)
|
||||
ParsedRule(Field field, Rule rule, SettingsManager settingsManager)
|
||||
{
|
||||
this.field = field;
|
||||
this.name = rule.name().isEmpty() ? field.getName() : rule.name();
|
||||
@@ -38,6 +43,8 @@ public final class ParsedRule<T> implements Comparable<ParsedRule> {
|
||||
this.isStrict = rule.strict();
|
||||
this.extraInfo = ImmutableList.copyOf(rule.extra());
|
||||
this.categories = ImmutableList.copyOf(rule.category());
|
||||
this.scarpetApp = rule.scarpetApp();
|
||||
this.settingsManager = settingsManager;
|
||||
this.validators = new ArrayList<>();
|
||||
for (Class v : rule.validate())
|
||||
this.validators.add((Validator<T>) callConstructor(v));
|
||||
@@ -50,6 +57,10 @@ public final class ParsedRule<T> implements Comparable<ParsedRule> {
|
||||
this.validators.add((Validator<T>) callConstructor(Validator._COMMAND_LEVEL_VALIDATOR.class));
|
||||
}
|
||||
}
|
||||
if (!scarpetApp.isEmpty())
|
||||
{
|
||||
this.validators.add((Validator<T>) callConstructor(Validator._SCARPET.class));
|
||||
}
|
||||
this.isClient = categories.contains(RuleCategory.CLIENT);
|
||||
if (this.isClient)
|
||||
{
|
||||
@@ -105,7 +116,7 @@ public final class ParsedRule<T> implements Comparable<ParsedRule> {
|
||||
|
||||
public ParsedRule<T> set(ServerCommandSource source, String value)
|
||||
{
|
||||
if (CarpetServer.settingsManager != null && CarpetServer.settingsManager.locked)
|
||||
if (settingsManager != null && settingsManager.locked)
|
||||
return null;
|
||||
if (type == String.class)
|
||||
{
|
||||
@@ -156,7 +167,7 @@ public final class ParsedRule<T> implements Comparable<ParsedRule> {
|
||||
if (!value.equals(get()) || source == null)
|
||||
{
|
||||
this.field.set(null, value);
|
||||
if (source != null) CarpetServer.settingsManager.notifyRuleChanged(source, this, stringValue);
|
||||
if (source != null) settingsManager.notifyRuleChanged(source, this, stringValue);
|
||||
}
|
||||
}
|
||||
catch (IllegalAccessException e)
|
||||
@@ -187,7 +198,7 @@ public final class ParsedRule<T> implements Comparable<ParsedRule> {
|
||||
public boolean getBoolValue()
|
||||
{
|
||||
if (type == boolean.class) return (Boolean) get();
|
||||
if (type.isAssignableFrom(Number.class)) return ((Number) get()).doubleValue() > 0;
|
||||
if (NUMBER_CLASSES.contains(type)) return ((Number) get()).doubleValue() > 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -51,6 +51,14 @@ public @interface Rule
|
||||
* For enums, its always strict, same for booleans - no need to set that for them.
|
||||
*/
|
||||
boolean strict() default true;
|
||||
|
||||
/**
|
||||
* If specified, the rule will automatically enable or disable
|
||||
* a builtin Scarpet Rule App with this name.
|
||||
* Consider telling the rule name so users can edit globals
|
||||
* (in case there are relevant globals to edit ofc)
|
||||
*/
|
||||
String scarpetApp() default "";
|
||||
|
||||
/**
|
||||
* The class of the validator checked when the rule is changed.
|
||||
|
||||
@@ -43,6 +43,7 @@ import java.util.TreeMap;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static carpet.utils.Translations.tr;
|
||||
import static carpet.script.CarpetEventServer.Event.CARPET_RULE_CHANGES;
|
||||
import static net.minecraft.command.CommandSource.suggestMatching;
|
||||
import static net.minecraft.server.command.CommandManager.argument;
|
||||
import static net.minecraft.server.command.CommandManager.literal;
|
||||
@@ -71,7 +72,10 @@ public class SettingsManager
|
||||
this.fancyName = fancyName;
|
||||
}
|
||||
|
||||
|
||||
public String getIdentifier() {
|
||||
return identifier;
|
||||
}
|
||||
|
||||
public void attachServer(MinecraftServer server)
|
||||
{
|
||||
this.server = server;
|
||||
@@ -90,7 +94,7 @@ public class SettingsManager
|
||||
{
|
||||
Rule rule = f.getAnnotation(Rule.class);
|
||||
if (rule == null) continue;
|
||||
ParsedRule parsed = new ParsedRule(f, rule);
|
||||
ParsedRule parsed = new ParsedRule(f, rule, this);
|
||||
rules.put(parsed.name, parsed);
|
||||
}
|
||||
}
|
||||
@@ -104,6 +108,30 @@ public class SettingsManager
|
||||
{
|
||||
observers.forEach(observer -> observer.accept(source, rule, userTypedValue));
|
||||
ServerNetworkHandler.updateRuleWithConnectedClients(rule);
|
||||
switchScarpetRule(source, rule);
|
||||
CARPET_RULE_CHANGES.onCarpetRuleChanges(rule, source);
|
||||
}
|
||||
|
||||
void switchScarpetRule(ServerCommandSource source, ParsedRule<?> rule)
|
||||
{
|
||||
if (!rule.scarpetApp.isEmpty())
|
||||
{
|
||||
if (rule.getBoolValue() || (rule.type == String.class && !rule.get().equals("false")))
|
||||
{
|
||||
CarpetServer.scriptServer.addScriptHost(source, rule.scarpetApp, false, false, true);
|
||||
} else {
|
||||
CarpetServer.scriptServer.removeScriptHost(source, rule.scarpetApp, false, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void initializeScarpetRules() {
|
||||
for (ParsedRule<?> rule : rules.values())
|
||||
{
|
||||
if (!rule.scarpetApp.isEmpty()) {
|
||||
switchScarpetRule(server.getCommandSource(), rule);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Iterable<String> getCategories()
|
||||
|
||||
@@ -57,6 +57,17 @@ public abstract class Validator<T>
|
||||
}
|
||||
public String description() { return "Can be limited to 'ops' only, or a custom permission level";}
|
||||
}
|
||||
|
||||
public static class _SCARPET<T> extends Validator<T> {
|
||||
@Override
|
||||
public T validate(ServerCommandSource source, ParsedRule<T> currentRule, T newValue, String string)
|
||||
{
|
||||
return newValue;
|
||||
}
|
||||
public String description() {
|
||||
return "It controls an accompanying Scarpet App";
|
||||
}
|
||||
}
|
||||
|
||||
public static class WIP<T> extends Validator<T>
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user