add observers
This commit is contained in:
1 parent
9bbaaccb3e
commit
e8fbf3aadc
2 files changed
+48
-22
No files matched your search
@@ -25,20 +25,6 @@ public final class ParsedRule<T> implements Comparable<ParsedRule> {
|
|||||||
public final T defaultValue;
|
public final T defaultValue;
|
||||||
public final String defaultAsString;
|
public final String defaultAsString;
|
||||||
|
|
||||||
public static <T> T callConstructor(Class<T> cls)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Constructor<T> constr = cls.getDeclaredConstructor();
|
|
||||||
constr.setAccessible(true);
|
|
||||||
return constr.newInstance();
|
|
||||||
}
|
|
||||||
catch (ReflectiveOperationException e)
|
|
||||||
{
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ParsedRule(Field field, Rule rule)
|
ParsedRule(Field field, Rule rule)
|
||||||
{
|
{
|
||||||
this.field = field;
|
this.field = field;
|
||||||
@@ -73,6 +59,19 @@ public final class ParsedRule<T> implements Comparable<ParsedRule> {
|
|||||||
this.validators.add(callConstructor(Validator._STRICT.class));
|
this.validators.add(callConstructor(Validator._STRICT.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private <T> T callConstructor(Class<T> cls)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Constructor<T> constr = cls.getDeclaredConstructor();
|
||||||
|
constr.setAccessible(true);
|
||||||
|
return constr.newInstance();
|
||||||
|
}
|
||||||
|
catch (ReflectiveOperationException e)
|
||||||
|
{
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public ParsedRule<T> set(ServerCommandSource source, String value)
|
public ParsedRule<T> set(ServerCommandSource source, String value)
|
||||||
{
|
{
|
||||||
@@ -115,8 +114,11 @@ public final class ParsedRule<T> implements Comparable<ParsedRule> {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.field.set(null, value);
|
if (!value.equals(get()))
|
||||||
|
{
|
||||||
|
this.field.set(null, value);
|
||||||
|
SettingsManager.notifyRuleChanged(this, stringValue);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (IllegalAccessException e)
|
catch (IllegalAccessException e)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -8,9 +8,24 @@ import org.apache.commons.lang3.tuple.Pair;
|
|||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.BufferedReader;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintStream;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.*;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.TreeMap;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class SettingsManager
|
public class SettingsManager
|
||||||
@@ -18,7 +33,8 @@ public class SettingsManager
|
|||||||
private static final Logger LOG = LogManager.getLogger();
|
private static final Logger LOG = LogManager.getLogger();
|
||||||
private static Map<String, ParsedRule<?>> rules = new HashMap<>();
|
private static Map<String, ParsedRule<?>> rules = new HashMap<>();
|
||||||
public boolean locked;
|
public boolean locked;
|
||||||
MinecraftServer server;
|
private MinecraftServer server;
|
||||||
|
private static List<BiConsumer<ParsedRule<?>, String>> observers = new ArrayList<>();
|
||||||
|
|
||||||
public SettingsManager(MinecraftServer server)
|
public SettingsManager(MinecraftServer server)
|
||||||
{
|
{
|
||||||
@@ -26,7 +42,6 @@ public class SettingsManager
|
|||||||
loadConfigurationFromConf();
|
loadConfigurationFromConf();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void parseSettingsClass(Class settingsClass)
|
public static void parseSettingsClass(Class settingsClass)
|
||||||
{
|
{
|
||||||
for (Field f : settingsClass.getDeclaredFields())
|
for (Field f : settingsClass.getDeclaredFields())
|
||||||
@@ -38,6 +53,16 @@ public class SettingsManager
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void addRuleObserver(BiConsumer<ParsedRule<?>, String> observer)
|
||||||
|
{
|
||||||
|
observers.add(observer);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void notifyRuleChanged(ParsedRule<?> rule, String userTypedValue)
|
||||||
|
{
|
||||||
|
observers.forEach(observer -> observer.accept(rule, userTypedValue));
|
||||||
|
}
|
||||||
|
|
||||||
public static Iterable<String> getCategories()
|
public static Iterable<String> getCategories()
|
||||||
{
|
{
|
||||||
Set<String> categories = new HashSet<>();
|
Set<String> categories = new HashSet<>();
|
||||||
@@ -88,7 +113,6 @@ public class SettingsManager
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private void writeSettingsToConf(Map<String, String> values)
|
private void writeSettingsToConf(Map<String, String> values)
|
||||||
{
|
{
|
||||||
if (locked)
|
if (locked)
|
||||||
@@ -141,7 +165,7 @@ public class SettingsManager
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Pair<Map<String, String>,Boolean> readSettingsFromConf()
|
private Pair<Map<String, String>,Boolean> readSettingsFromConf()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in new issue
Block a user