Use newer Java features in places where it makes sense and more (#1009)

This commit is contained in:
altrisi authored and GitHub committed 2021-08-03 07:32:40 -03:00
1 parent 28cb136c6b
commit 7fb5e47da5
34 files changed
+338 -399

No files matched your search

+11 -11
View File
@@ -2,7 +2,6 @@ package carpet.settings;
import carpet.utils.Translations;
import carpet.utils.Messenger;
import com.google.common.collect.ImmutableList;
import net.minecraft.server.command.ServerCommandSource;
import java.lang.reflect.Constructor;
@@ -11,6 +10,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
import org.apache.commons.lang3.ClassUtils;
@@ -29,9 +29,9 @@ public final class ParsedRule<T> implements Comparable<ParsedRule> {
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;
public final List<String> extraInfo;
public final List<String> categories;
public final List<String> options;
public boolean isStrict;
public boolean isClient;
public final Class<T> type;
@@ -47,8 +47,8 @@ public final class ParsedRule<T> implements Comparable<ParsedRule> {
this.type = (Class<T>) field.getType();
this.description = rule.desc();
this.isStrict = rule.strict();
this.extraInfo = ImmutableList.copyOf(rule.extra());
this.categories = ImmutableList.copyOf(rule.category());
this.extraInfo = List.of(rule.extra());
this.categories = List.of(rule.category());
this.scarpetApp = rule.appSource();
this.settingsManager = settingsManager;
this.validators = new ArrayList<>();
@@ -76,22 +76,22 @@ public final class ParsedRule<T> implements Comparable<ParsedRule> {
this.defaultAsString = convertToString(this.defaultValue);
if (rule.options().length > 0)
{
this.options = ImmutableList.copyOf(rule.options());
this.options = List.of(rule.options());
}
else if (this.type == boolean.class){
this.options = ImmutableList.of("true","false");
this.options = List.of("true","false");
}
else if(this.type == String.class && categories.contains(RuleCategory.COMMAND))
{
this.options = ImmutableList.of("true", "false", "ops");
this.options = List.of("true", "false", "ops");
}
else if (this.type.isEnum())
{
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(Collectors.toUnmodifiableList());
}
else
{
this.options = ImmutableList.of();
this.options = List.of();
}
if (isStrict && !this.options.isEmpty())
{
@@ -5,7 +5,6 @@ import carpet.CarpetSettings;
import carpet.network.ServerNetworkHandler;
import carpet.utils.Translations;
import carpet.utils.Messenger;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
@@ -72,6 +71,7 @@ public class SettingsManager
private MinecraftServer server;
private List<TriConsumer<ServerCommandSource, ParsedRule<?>, String>> observers = new ArrayList<>();
private static List<TriConsumer<ServerCommandSource, ParsedRule<?>, String>> staticObservers = new ArrayList<>();
static record ConfigReadResult(Map<String, String> ruleMap, boolean locked) {}
/**
* Creates a new {@link SettingsManager} without a fancy name.
@@ -264,7 +264,7 @@ public class SettingsManager
*/
public Collection<ParsedRule<?>> findStartupOverrides()
{
Set<String> defaults = readSettingsFromConf(getFile()).getLeft().keySet();
Set<String> defaults = readSettingsFromConf(getFile()).ruleMap().keySet();
return rules.values().stream().filter(r -> defaults.contains(r.name)).
sorted().collect(Collectors.toList());
}
@@ -396,30 +396,30 @@ public class SettingsManager
private void loadConfigurationFromConf()
{
for (ParsedRule<?> rule : rules.values()) rule.resetToDefault(server.getCommandSource());
Pair<Map<String, String>, Boolean> conf = readSettingsFromConf(getFile());
ConfigReadResult conf = readSettingsFromConf(getFile());
locked = false;
if (conf.getRight())
if (conf.locked())
{
CarpetSettings.LOG.info("[CM]: "+fancyName+" features are locked by the administrator");
disableBooleanCommands();
}
for (String key: conf.getLeft().keySet())
for (String key: conf.ruleMap().keySet())
{
try
{
if (rules.get(key).set(server.getCommandSource(), conf.getLeft().get(key)) != null)
CarpetSettings.LOG.info("[CM]: loaded setting " + key + " as " + conf.getLeft().get(key) + " from " + identifier + ".conf");
if (rules.get(key).set(server.getCommandSource(), conf.ruleMap().get(key)) != null)
CarpetSettings.LOG.info("[CM]: loaded setting " + key + " as " + conf.ruleMap().get(key) + " from " + identifier + ".conf");
}
catch (Exception exc)
{
CarpetSettings.LOG.error("[CM Error]: Failed to load setting: "+key+", "+exc);
}
}
locked = conf.getRight();
locked = conf.locked();
}
private Pair<Map<String, String>, Boolean> readSettingsFromConf(Path path)
private ConfigReadResult readSettingsFromConf(Path path)
{
try (BufferedReader reader = Files.newBufferedReader(path))
{
@@ -452,9 +452,9 @@ public class SettingsManager
}
}
return Pair.of(result, confLocked);
return new ConfigReadResult(result, confLocked);
}
catch(NoSuchFileException e)
catch (NoSuchFileException e)
{
if (path.equals(getFile()) && FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT)
{
@@ -467,15 +467,15 @@ public class SettingsManager
}
return readSettingsFromConf(defaultsPath);
} catch (IOException e2) {
CarpetSettings.LOG.error("Exception when loading fallback default config: ", e);
CarpetSettings.LOG.error("Exception when loading fallback default config: ", e2);
}
}
return Pair.of(new HashMap<>(), false);
return new ConfigReadResult(new HashMap<>(), false);
}
catch (IOException e)
{
e.printStackTrace();
return Pair.of(new HashMap<>(), false);
return new ConfigReadResult(new HashMap<>(), false);
}
}
@@ -486,7 +486,7 @@ public class SettingsManager
if (rule.name.toLowerCase(Locale.ROOT).contains(lcSearch)) return true; // substring match, case insensitive
for (String c : rule.categories) if (c.equals(search)) return true; // category exactly, case sensitive
return Sets.newHashSet(rule.description.toLowerCase(Locale.ROOT).split("\\W+")).contains(lcSearch); // contains full term in description, but case insensitive
}).sorted().collect(ImmutableList.toImmutableList());
}).sorted().collect(Collectors.toUnmodifiableList());
}
/**
@@ -647,9 +647,9 @@ public class SettingsManager
{
if (locked) return 0;
if (!rules.containsKey(rule.name)) return 0;
Pair<Map<String, String>,Boolean> conf = readSettingsFromConf(getFile());
conf.getLeft().put(rule.name, stringValue);
writeSettingsToConf(conf.getLeft()); // this may feels weird, but if conf
ConfigReadResult conf = readSettingsFromConf(getFile());
conf.ruleMap().put(rule.name, stringValue);
writeSettingsToConf(conf.ruleMap()); // this may feels weird, but if conf
// is locked, it will never reach this point.
rule.set(source,stringValue);
Messenger.m(source ,"gi "+String.format(tr("ui.rule_%(rule)s_will_now_default_to_%(value)s","Rule %s will now default to %s"), rule.translatedName(), stringValue));
@@ -660,9 +660,9 @@ public class SettingsManager
{
if (locked) return 0;
if (!rules.containsKey(rule.name)) return 0;
Pair<Map<String, String>,Boolean> conf = readSettingsFromConf(getFile());
conf.getLeft().remove(rule.name);
writeSettingsToConf(conf.getLeft());
ConfigReadResult conf = readSettingsFromConf(getFile());
conf.ruleMap().remove(rule.name);
writeSettingsToConf(conf.ruleMap());
rules.get(rule.name).resetToDefault(source);
Messenger.m(source ,"gi "+String.format(tr("ui.rule_%(rule)s_not_set_restart","Rule %s will now not be set on restart"), rule.translatedName()));
return 1;
+4 -4
View File
@@ -2,9 +2,9 @@ package carpet.settings;
import carpet.CarpetServer;
import carpet.utils.Messenger;
import com.google.common.collect.ImmutableList;
import net.minecraft.server.command.ServerCommandSource;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
@@ -48,7 +48,7 @@ public abstract class Validator<T>
}
public static class _COMMAND_LEVEL_VALIDATOR extends Validator<String> {
private static ImmutableList<String> OPTIONS = ImmutableList.of("true", "false", "ops", "0", "1", "2", "3", "4");
private static List<String> OPTIONS = List.of("true", "false", "ops", "0", "1", "2", "3", "4");
@Override public String validate(ServerCommandSource source, ParsedRule<String> currentRule, String newValue, String userString) {
if (!OPTIONS.contains(userString.toLowerCase(Locale.ROOT)))
{
@@ -59,7 +59,7 @@ public abstract class Validator<T>
}
return userString.toLowerCase(Locale.ROOT);
}
public String description() { return "Can be limited to 'ops' only, or a custom permission level";}
@Override public String description() { return "Can be limited to 'ops' only, or a custom permission level";}
}
public static class _SCARPET<T> extends Validator<T> {
@@ -68,7 +68,7 @@ public abstract class Validator<T>
{
return newValue;
}
public String description() {
@Override public String description() {
return "It controls an accompanying Scarpet App";
}
}