Replace JAVA_ONLY_SETTINGS array with @JavaOnly annotation

This commit is contained in:
Brady
2023-06-04 12:31:34 -05:00
parent 5dd10b6e81
commit 80ec9023ce
5 changed files with 37 additions and 15 deletions

View File

@@ -29,6 +29,10 @@ import net.minecraft.util.math.Vec3i;
import net.minecraft.util.text.ITextComponent;
import java.awt.*;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
@@ -1154,6 +1158,7 @@ public final class Settings {
* via {@link Consumer#andThen(Consumer)} or it can completely be overriden via setting
* {@link Setting#value};
*/
@JavaOnly
public final Setting<Consumer<ITextComponent>> logger = new Setting<>(msg -> Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(msg));
/**
@@ -1161,6 +1166,7 @@ public final class Settings {
* via {@link Consumer#andThen(Consumer)} or it can completely be overriden via setting
* {@link Setting#value};
*/
@JavaOnly
public final Setting<BiConsumer<String, Boolean>> notifier = new Setting<>(NotificationHelper::notify);
/**
@@ -1168,6 +1174,7 @@ public final class Settings {
* via {@link Consumer#andThen(Consumer)} or it can completely be overriden via setting
* {@link Setting#value};
*/
@JavaOnly
public final Setting<BiConsumer<ITextComponent, ITextComponent>> toaster = new Setting<>(BaritoneToast::addOrUpdate);
/**
@@ -1312,6 +1319,7 @@ public final class Settings {
public T value;
public final T defaultValue;
private String name;
private boolean javaOnly;
@SuppressWarnings("unchecked")
private Setting(T value) {
@@ -1320,6 +1328,7 @@ public final class Settings {
}
this.value = value;
this.defaultValue = value;
this.javaOnly = false;
}
/**
@@ -1356,8 +1365,25 @@ public final class Settings {
public final Type getType() {
return settingTypes.get(this);
}
/**
* This should always be the same as whether the setting can be parsed from or serialized to a string; in other
* words, the only way to modify it is by writing to {@link #value} programatically.
*
* @return {@code true} if the setting can not be set or read by the user
*/
public boolean isJavaOnly() {
return javaOnly;
}
}
/**
* Marks a {@link Setting} field as being {@link Setting#isJavaOnly() Java-only}
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
private @interface JavaOnly {}
// here be dragons
Settings() {
@@ -1373,6 +1399,7 @@ public final class Settings {
Setting<?> setting = (Setting<?>) field.get(this);
String name = field.getName();
setting.name = name;
setting.javaOnly = field.isAnnotationPresent(JavaOnly.class);
name = name.toLowerCase();
if (tmpByName.containsKey(name)) {
throw new IllegalStateException("Duplicate setting name");

View File

@@ -253,7 +253,7 @@ public class TabCompleteHelper {
public TabCompleteHelper addSettings() {
return append(
BaritoneAPI.getSettings().allSettings.stream()
.filter(s -> !SettingsUtil.javaOnlySetting(s))
.filter(s -> !s.isJavaOnly())
.map(Settings.Setting::getName)
.sorted(String.CASE_INSENSITIVE_ORDER)
);

View File

@@ -50,7 +50,6 @@ public class SettingsUtil {
public static final String SETTINGS_DEFAULT_NAME = "settings.txt";
private static final Pattern SETTING_PATTERN = Pattern.compile("^(?<setting>[^ ]+) +(?<value>.+)"); // key and value split by the first space
private static final String[] JAVA_ONLY_SETTINGS = {"logger", "notifier", "toaster"};
private static boolean isComment(String line) {
return line.startsWith("#") || line.startsWith("//");
@@ -116,7 +115,7 @@ public class SettingsUtil {
System.out.println("NULL SETTING?" + setting.getName());
continue;
}
if (javaOnlySetting(setting)) {
if (setting.isJavaOnly()) {
continue; // NO
}
if (setting.value == setting.defaultValue) {
@@ -170,7 +169,7 @@ public class SettingsUtil {
}
public static String settingToString(Settings.Setting setting) throws IllegalStateException {
if (javaOnlySetting(setting)) {
if (setting.isJavaOnly()) {
return setting.getName();
}
@@ -178,18 +177,14 @@ public class SettingsUtil {
}
/**
* This should always be the same as whether the setting can be parsed from or serialized to a string
* Deprecated. Use {@link Settings.Setting#isJavaOnly()} instead.
*
* @param setting The Setting
* @return true if the setting can not be set or read by the user
*/
@Deprecated
public static boolean javaOnlySetting(Settings.Setting setting) {
for (String name : JAVA_ONLY_SETTINGS) { // no JAVA_ONLY_SETTINGS.contains(...) because that would be case sensitive
if (setting.getName().equalsIgnoreCase(name)) {
return true;
}
}
return false;
return setting.isJavaOnly();
}
public static void parseAndApply(Settings settings, String settingName, String settingValue) throws IllegalStateException, NumberFormatException {

View File

@@ -124,7 +124,7 @@ public class ExampleBaritoneControl implements Helper, AbstractGameEventListener
}
} else if (argc.hasExactlyOne()) {
for (Settings.Setting setting : settings.allSettings) {
if (SettingsUtil.javaOnlySetting(setting)) {
if (setting.isJavaOnly()) {
continue;
}
if (setting.getName().equalsIgnoreCase(pair.getFirst())) {
@@ -177,7 +177,7 @@ public class ExampleBaritoneControl implements Helper, AbstractGameEventListener
.stream();
}
Settings.Setting setting = settings.byLowerName.get(argc.getString().toLowerCase(Locale.US));
if (setting != null && !SettingsUtil.javaOnlySetting(setting)) {
if (setting != null && !setting.isJavaOnly()) {
if (setting.getValueClass() == Boolean.class) {
TabCompleteHelper helper = new TabCompleteHelper();
if ((Boolean) setting.value) {

View File

@@ -77,7 +77,7 @@ public class SetCommand extends Command {
args.requireMax(1);
List<? extends Settings.Setting> toPaginate =
(viewModified ? SettingsUtil.modifiedSettings(Baritone.settings()) : Baritone.settings().allSettings).stream()
.filter(s -> !javaOnlySetting(s))
.filter(s -> !s.isJavaOnly())
.filter(s -> s.getName().toLowerCase(Locale.US).contains(search.toLowerCase(Locale.US)))
.sorted((s1, s2) -> String.CASE_INSENSITIVE_ORDER.compare(s1.getName(), s2.getName()))
.collect(Collectors.toList());
@@ -141,7 +141,7 @@ public class SetCommand extends Command {
if (setting == null) {
throw new CommandInvalidTypeException(args.consumed(), "a valid setting");
}
if (javaOnlySetting(setting)) {
if (setting.isJavaOnly()) {
// ideally it would act as if the setting didn't exist
// but users will see it in Settings.java or its javadoc
// so at some point we have to tell them or they will see it as a bug