diff --git a/leaves-server/minecraft-patches/features/0107-Old-BlockEntity-behaviour.patch b/leaves-server/minecraft-patches/features/0107-Old-BlockEntity-behaviour.patch index 8e6855fe..cc93e8e8 100644 --- a/leaves-server/minecraft-patches/features/0107-Old-BlockEntity-behaviour.patch +++ b/leaves-server/minecraft-patches/features/0107-Old-BlockEntity-behaviour.patch @@ -5,7 +5,7 @@ Subject: [PATCH] Old BlockEntity behaviour diff --git a/net/minecraft/world/level/block/ChiseledBookShelfBlock.java b/net/minecraft/world/level/block/ChiseledBookShelfBlock.java -index 1411c6b0471281827b9d21958c0c7d962809898f..14da3105d094e7c701e20e57e49bdb4b1791d3fd 100644 +index 1411c6b0471281827b9d21958c0c7d962809898f..2aa08b7da25f159640e0640482d40c69b060bb9f 100644 --- a/net/minecraft/world/level/block/ChiseledBookShelfBlock.java +++ b/net/minecraft/world/level/block/ChiseledBookShelfBlock.java @@ -192,7 +192,14 @@ public class ChiseledBookShelfBlock extends BaseEntityBlock { @@ -14,7 +14,7 @@ index 1411c6b0471281827b9d21958c0c7d962809898f..14da3105d094e7c701e20e57e49bdb4b chiseledBookShelfBlockEntity.clearContent(); - flag = true; + // Leaves start - behaviour 1.21.1- -+ if (org.leavesmc.leaves.LeavesConfig.modify.oldMC.oldBlockEntityBehaviour) { ++ if (org.leavesmc.leaves.LeavesConfig.modify.oldMC.updater.oldBlockEntityBehaviour) { + flag = false; + level.updateNeighbourForOutputSignal(pos, this); + } else { @@ -25,7 +25,7 @@ index 1411c6b0471281827b9d21958c0c7d962809898f..14da3105d094e7c701e20e57e49bdb4b flag = false; } diff --git a/net/minecraft/world/level/block/LecternBlock.java b/net/minecraft/world/level/block/LecternBlock.java -index 82fcae1f8b4149f13adf5118287718812518f8bf..8d5f555421c5856ef8af36136b275c03e4b0a25f 100644 +index 82fcae1f8b4149f13adf5118287718812518f8bf..d6e4e2f9d762d512d22a4da3736f997d2cd9624d 100644 --- a/net/minecraft/world/level/block/LecternBlock.java +++ b/net/minecraft/world/level/block/LecternBlock.java @@ -237,10 +237,11 @@ public class LecternBlock extends BaseEntityBlock { @@ -33,16 +33,16 @@ index 82fcae1f8b4149f13adf5118287718812518f8bf..8d5f555421c5856ef8af36136b275c03 } - super.onRemove(state, level, pos, newState, isMoving); -+ if (!org.leavesmc.leaves.LeavesConfig.modify.oldMC.oldBlockEntityBehaviour) super.onRemove(state, level, pos, newState, isMoving); // Leaves - behaviour 1.21.1- ++ if (!org.leavesmc.leaves.LeavesConfig.modify.oldMC.updater.oldBlockEntityBehaviour) super.onRemove(state, level, pos, newState, isMoving); // Leaves - behaviour 1.21.1- if (state.getValue(POWERED)) { updateBelow(level, pos, state); } -+ if (org.leavesmc.leaves.LeavesConfig.modify.oldMC.oldBlockEntityBehaviour) super.onRemove(state, level, pos, newState, isMoving); // Leaves - behaviour 1.21.1- ++ if (org.leavesmc.leaves.LeavesConfig.modify.oldMC.updater.oldBlockEntityBehaviour) super.onRemove(state, level, pos, newState, isMoving); // Leaves - behaviour 1.21.1- } } diff --git a/net/minecraft/world/level/block/SculkSensorBlock.java b/net/minecraft/world/level/block/SculkSensorBlock.java -index 2fd1bae800b7dca0a58b572095e2c58012412481..bec8053c0f0cb5dad19b1d1e3a03dff7fd76f5eb 100644 +index 2fd1bae800b7dca0a58b572095e2c58012412481..237e5a48dc345cae90aa9921251fe48d1c2470e1 100644 --- a/net/minecraft/world/level/block/SculkSensorBlock.java +++ b/net/minecraft/world/level/block/SculkSensorBlock.java @@ -138,10 +138,11 @@ public class SculkSensorBlock extends BaseEntityBlock implements SimpleWaterlogg @@ -50,16 +50,16 @@ index 2fd1bae800b7dca0a58b572095e2c58012412481..bec8053c0f0cb5dad19b1d1e3a03dff7 protected void onRemove(BlockState state, Level level, BlockPos pos, BlockState newState, boolean movedByPiston) { if (!state.is(newState.getBlock())) { - super.onRemove(state, level, pos, newState, movedByPiston); -+ if (!org.leavesmc.leaves.LeavesConfig.modify.oldMC.oldBlockEntityBehaviour) super.onRemove(state, level, pos, newState, movedByPiston); // Leaves - behaviour 1.21.1- ++ if (!org.leavesmc.leaves.LeavesConfig.modify.oldMC.updater.oldBlockEntityBehaviour) super.onRemove(state, level, pos, newState, movedByPiston); // Leaves - behaviour 1.21.1- if (getPhase(state) == SculkSensorPhase.ACTIVE) { updateNeighbours(level, pos, state); } -+ if (org.leavesmc.leaves.LeavesConfig.modify.oldMC.oldBlockEntityBehaviour) super.onRemove(state, level, pos, newState, movedByPiston); // Leaves - behaviour 1.21.1- ++ if (org.leavesmc.leaves.LeavesConfig.modify.oldMC.updater.oldBlockEntityBehaviour) super.onRemove(state, level, pos, newState, movedByPiston); // Leaves - behaviour 1.21.1- } } diff --git a/net/minecraft/world/level/block/ShulkerBoxBlock.java b/net/minecraft/world/level/block/ShulkerBoxBlock.java -index fca50f68625050daabcae3a3b615cf88cce41111..64a93d68418fbc2228e4de09e2b288e0db503e5c 100644 +index fca50f68625050daabcae3a3b615cf88cce41111..a5786b13f21781c2e85a6a7910ce42005f8951c8 100644 --- a/net/minecraft/world/level/block/ShulkerBoxBlock.java +++ b/net/minecraft/world/level/block/ShulkerBoxBlock.java @@ -178,10 +178,11 @@ public class ShulkerBoxBlock extends BaseEntityBlock { @@ -67,16 +67,16 @@ index fca50f68625050daabcae3a3b615cf88cce41111..64a93d68418fbc2228e4de09e2b288e0 if (!state.is(newState.getBlock())) { BlockEntity blockEntity = level.getBlockEntity(pos); - super.onRemove(state, level, pos, newState, isMoving); -+ if (!org.leavesmc.leaves.LeavesConfig.modify.oldMC.oldBlockEntityBehaviour) super.onRemove(state, level, pos, newState, isMoving); // Leaves - behaviour 1.21.1- ++ if (!org.leavesmc.leaves.LeavesConfig.modify.oldMC.updater.oldBlockEntityBehaviour) super.onRemove(state, level, pos, newState, isMoving); // Leaves - behaviour 1.21.1- if (blockEntity instanceof ShulkerBoxBlockEntity) { level.updateNeighbourForOutputSignal(pos, state.getBlock()); } -+ if (org.leavesmc.leaves.LeavesConfig.modify.oldMC.oldBlockEntityBehaviour) super.onRemove(state, level, pos, newState, isMoving); // Leaves - behaviour 1.21.1- ++ if (org.leavesmc.leaves.LeavesConfig.modify.oldMC.updater.oldBlockEntityBehaviour) super.onRemove(state, level, pos, newState, isMoving); // Leaves - behaviour 1.21.1- } } diff --git a/net/minecraft/world/level/block/entity/BlockEntity.java b/net/minecraft/world/level/block/entity/BlockEntity.java -index 77618757c0e678532dbab814aceed83f7f1cd892..6c6fad160cf206bca2b37a7bbe2a69b508ecac7d 100644 +index 77618757c0e678532dbab814aceed83f7f1cd892..fa78fc79b6000832d8623e632d845cf942496428 100644 --- a/net/minecraft/world/level/block/entity/BlockEntity.java +++ b/net/minecraft/world/level/block/entity/BlockEntity.java @@ -55,7 +55,7 @@ public abstract class BlockEntity { @@ -84,7 +84,7 @@ index 77618757c0e678532dbab814aceed83f7f1cd892..6c6fad160cf206bca2b37a7bbe2a69b5 public boolean isValidBlockState(BlockState state) { - return this.type.isValid(state); -+ return org.leavesmc.leaves.LeavesConfig.modify.oldMC.oldBlockEntityBehaviour || this.type.isValid(state); // Leaves - behaviour 1.21.1- ++ return org.leavesmc.leaves.LeavesConfig.modify.oldMC.updater.oldBlockEntityBehaviour || this.type.isValid(state); // Leaves - behaviour 1.21.1- } public static BlockPos getPosFromTag(CompoundTag tag) { diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/LeavesConfig.java b/leaves-server/src/main/java/org/leavesmc/leaves/LeavesConfig.java index 4880e1ef..f8c9c3c2 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/LeavesConfig.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/LeavesConfig.java @@ -121,17 +121,17 @@ public final class LeavesConfig { } } - @RemovedConfig(name = "unable-fakeplayer-names", category = "fakeplayer", convert = ListConfigValidator.STRING.class, transform = true) - @GlobalConfig(value = "unable-fakeplayer-names", validator = ListConfigValidator.STRING.class) + @RemovedConfig(name = "unable-fakeplayer-names", category = "fakeplayer", transform = true) + @GlobalConfig(value = "unable-fakeplayer-names") public List unableNames = List.of("player-name"); - @GlobalConfig(value = "limit", validator = IntConfigValidator.class) + @GlobalConfig(value = "limit") public int limit = 10; - @GlobalConfig(value = "prefix", validator = StringConfigValidator.class) + @GlobalConfig(value = "prefix") public String prefix = ""; - @GlobalConfig(value = "suffix", validator = StringConfigValidator.class) + @GlobalConfig(value = "suffix") public String suffix = ""; @GlobalConfig(value = "regen-amount", validator = RegenAmountValidator.class) @@ -196,6 +196,10 @@ public final class LeavesConfig { @RemovedConfig(name = "redstone-wire-dont-connect-if-on-trapdoor", category = {"modify", "minecraft-old"}, transform = true) @GlobalConfig("redstone-wire-dont-connect-if-on-trapdoor") public boolean redstoneDontCantOnTrapDoor = false; + + @RemovedConfig(name = "old-block-entity-behaviour", category = {"modify", "minecraft-old"}, transform = true) + @GlobalConfig("old-block-entity-behaviour") + public boolean oldBlockEntityBehaviour = false; } @RemovedConfig(name = "shears-in-dispenser-can-zero-amount", category = {}, transform = true) @@ -252,9 +256,6 @@ public final class LeavesConfig { @GlobalConfig("fix-fortress-mob-spawn") public boolean fixFortressMobSpawn = false; - @GlobalConfig("old-block-entity-behaviour") - public boolean oldBlockEntityBehaviour = false; - @GlobalConfig("old-hopper-suck-in-behavior") public boolean oldHopperSuckInBehavior = false; @@ -283,19 +284,19 @@ public final class LeavesConfig { @GlobalConfig("no-chunk-load") public boolean noChunk = false; - @GlobalConfig(value = "no-chunk-height", validator = DoubleConfigValidator.class) + @GlobalConfig(value = "no-chunk-height") public double noChunkHeight = 500.0D; - @GlobalConfig(value = "no-chunk-speed", validator = DoubleConfigValidator.class) + @GlobalConfig(value = "no-chunk-speed") public double noChunkSpeed = -1.0D; @GlobalConfig("message") public boolean noChunkMes = true; - @GlobalConfig(value = "message-start", validator = StringConfigValidator.class) + @GlobalConfig(value = "message-start") public String noChunkStartMes = "Flight enter cruise mode"; - @GlobalConfig(value = "message-end", validator = StringConfigValidator.class) + @GlobalConfig(value = "message-end") public String noChunkEndMes = "Flight exit cruise mode"; } @@ -692,7 +693,7 @@ public final class LeavesConfig { @GlobalConfig("quota") public boolean useQuota = false; - @GlobalConfig(value = "quota-limit", validator = IntConfigValidator.class) + @GlobalConfig(value = "quota-limit") public int quotaLimit = 40000000; } @@ -713,16 +714,13 @@ public final class LeavesConfig { } } - @RemovedConfig(name = "pca-sync-player-entity", category = "protocol", convert = PcaPlayerEntityValidator.class, transform = true) - @GlobalConfig(value = "pca-sync-player-entity", validator = PcaPlayerEntityValidator.class) + @RemovedConfig(name = "pca-sync-player-entity", category = "protocol", transform = true) + @GlobalConfig(value = "pca-sync-player-entity") public PcaPlayerEntityType syncPlayerEntity = PcaPlayerEntityType.OPS; public enum PcaPlayerEntityType { NOBODY, BOT, OPS, OPS_AND_SELF, EVERYONE } - - private static class PcaPlayerEntityValidator extends EnumConfigValidator { - } } public AppleSkinConfig appleskin = new AppleSkinConfig(); @@ -780,7 +778,7 @@ public final class LeavesConfig { @GlobalConfig("xaero-map-protocol") public boolean xaeroMapProtocol = false; - @GlobalConfig(value = "xaero-map-server-id", validator = IntConfigValidator.class) + @GlobalConfig(value = "xaero-map-server-id") public int xaeroMapServerID = new Random().nextInt(); @GlobalConfig("leaves-carpet-support") @@ -832,7 +830,7 @@ public final class LeavesConfig { @GlobalConfig("allow-experimental") public Boolean allowExperimental = false; - @GlobalConfig(value = "time", lock = true, validator = ListConfigValidator.STRING.class) + @GlobalConfig(value = "time", lock = true) public List updateTime = List.of("14:00", "2:00"); } @@ -896,7 +894,7 @@ public final class LeavesConfig { } } - @GlobalConfig(value = "server-mod-name", validator = StringConfigValidator.class) + @GlobalConfig(value = "server-mod-name") public String serverModName = "Leaves"; @GlobalConfig("bstats-privacy-mode") @@ -929,16 +927,13 @@ public final class LeavesConfig { @GlobalConfigCategory("linear") public static class LinearConfig { - @GlobalConfig(value = "version", lock = true, validator = LinearVersionValidator.class) + @GlobalConfig(value = "version", lock = true) public org.leavesmc.leaves.region.linear.LinearVersion version = org.leavesmc.leaves.region.linear.LinearVersion.V2; - private static class LinearVersionValidator extends EnumConfigValidator { - } - @GlobalConfig(value = "auto-convert-anvil-to-linear", lock = true) public boolean autoConvertAnvilToLinear = false; - @GlobalConfig(value = "flush-max-threads", lock = true, validator = IntConfigValidator.class) + @GlobalConfig(value = "flush-max-threads", lock = true) public int flushThreads = 6; public int getLinearFlushThreads() { @@ -949,7 +944,7 @@ public final class LeavesConfig { } } - @GlobalConfig(value = "flush-delay-ms", lock = true, validator = IntConfigValidator.class) + @GlobalConfig(value = "flush-delay-ms", lock = true) public int flushDelayMs = 100; @GlobalConfig(value = "use-virtual-thread", lock = true) diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/config/AutoConfigTransformer.java b/leaves-server/src/main/java/org/leavesmc/leaves/config/AutoConfigTransformer.java new file mode 100644 index 00000000..03e9d973 --- /dev/null +++ b/leaves-server/src/main/java/org/leavesmc/leaves/config/AutoConfigTransformer.java @@ -0,0 +1,65 @@ +package org.leavesmc.leaves.config; + +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; + +import java.lang.reflect.Field; + +public class AutoConfigTransformer implements ConfigTransformer { + + @NotNull + @Contract("_ -> new") + public static ConfigTransformer createValidator(@NotNull Field field) { + return new SimpleConfigTransformer(AutoConfigValidator.createValidator(field)); + } + + @Override + public Object transform(Object from) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public Object stringConvert(String value) throws IllegalArgumentException { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public Class getFieldClass() { + throw new UnsupportedOperationException("Not supported yet."); + } + + @SuppressWarnings({"unchecked", "rawtypes"}) + public static class SimpleConfigTransformer implements ConfigTransformer { + + private final ConfigValidator validator; + + public SimpleConfigTransformer(ConfigValidator validator) { + this.validator = validator; + } + + @Override + public Object transform(Object o) { + return o; + } + + @Override + public Object stringConvert(String value) throws IllegalArgumentException { + return validator.stringConvert(value); + } + + @Override + public Object loadConvert(Object value) throws IllegalArgumentException { + return validator.loadConvert(value); + } + + @Override + public Object saveConvert(Object value) { + return validator.saveConvert(value); + } + + @Override + public Class getFieldClass() { + return validator.getFieldClass(); + } + } +} diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/config/AutoConfigValidator.java b/leaves-server/src/main/java/org/leavesmc/leaves/config/AutoConfigValidator.java new file mode 100644 index 00000000..679ec09d --- /dev/null +++ b/leaves-server/src/main/java/org/leavesmc/leaves/config/AutoConfigValidator.java @@ -0,0 +1,68 @@ +package org.leavesmc.leaves.config; + +import org.jetbrains.annotations.NotNull; + +import java.lang.reflect.Field; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Supplier; + +public final class AutoConfigValidator implements ConfigValidator { + + private static final Map, Supplier>> BASE_VALIDATOR = new HashMap<>(); + + static { + BASE_VALIDATOR.put(Boolean.class, ConfigValidatorImpl.BooleanConfigValidator::new); + BASE_VALIDATOR.put(boolean.class, ConfigValidatorImpl.BooleanConfigValidator::new); + + BASE_VALIDATOR.put(Integer.class, ConfigValidatorImpl.IntConfigValidator::new); + BASE_VALIDATOR.put(int.class, ConfigValidatorImpl.IntConfigValidator::new); + + BASE_VALIDATOR.put(Double.class, ConfigValidatorImpl.DoubleConfigValidator::new); + BASE_VALIDATOR.put(double.class, ConfigValidatorImpl.DoubleConfigValidator::new); + + BASE_VALIDATOR.put(String.class, ConfigValidatorImpl.StringConfigValidator::new); + } + + @SuppressWarnings({"unchecked", "rawtypes"}) + public static ConfigValidator createValidator(@NotNull Field field) { + Class fieldType = field.getType(); + + if (BASE_VALIDATOR.containsKey(fieldType)) { + return BASE_VALIDATOR.get(fieldType).get(); + } + + if (List.class.isAssignableFrom(fieldType)) { + Type genericType = field.getGenericType(); + if (genericType instanceof ParameterizedType parameterizedType) { + Type[] typeArgs = parameterizedType.getActualTypeArguments(); + if (typeArgs.length > 0 && typeArgs[0] instanceof Class genericClass) { + if (genericClass.equals(String.class)) { + return new ConfigValidatorImpl.ListConfigValidator.STRING(); + } + throw new IllegalArgumentException("List type " + genericClass.getTypeName() + " is not supported."); + } + } + throw new IllegalArgumentException("List type " + genericType.getTypeName() + " is not supported."); + } + + if (fieldType.isEnum()) { + return new ConfigValidatorImpl.EnumConfigValidator<>((Class) fieldType); + } + + throw new IllegalArgumentException("Can't find validator for type " + fieldType.getName()); + } + + @Override + public Object stringConvert(String value) throws IllegalArgumentException { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public Class getFieldClass() { + throw new UnsupportedOperationException("Not supported yet."); + } +} diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/config/ConfigConverter.java b/leaves-server/src/main/java/org/leavesmc/leaves/config/ConfigConverter.java index 2f8192d4..b4aba30c 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/config/ConfigConverter.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/config/ConfigConverter.java @@ -1,7 +1,8 @@ package org.leavesmc.leaves.config; public interface ConfigConverter { - E convert(String value) throws IllegalArgumentException; + + E stringConvert(String value) throws IllegalArgumentException; @SuppressWarnings("unchecked") default E loadConvert(Object value) throws IllegalArgumentException { diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/config/ConfigTransformer.java b/leaves-server/src/main/java/org/leavesmc/leaves/config/ConfigTransformer.java new file mode 100644 index 00000000..9dcc1340 --- /dev/null +++ b/leaves-server/src/main/java/org/leavesmc/leaves/config/ConfigTransformer.java @@ -0,0 +1,5 @@ +package org.leavesmc.leaves.config; + +public interface ConfigTransformer extends ConfigConverter { + TO transform(FROM from); +} diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/config/ConfigValidatorImpl.java b/leaves-server/src/main/java/org/leavesmc/leaves/config/ConfigValidatorImpl.java index 0e9cdc0f..10dc070b 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/config/ConfigValidatorImpl.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/config/ConfigValidatorImpl.java @@ -10,7 +10,7 @@ import java.util.Locale; public abstract class ConfigValidatorImpl implements ConfigValidator { - private Class fieldClass; + protected Class fieldClass; @SuppressWarnings("unchecked") public ConfigValidatorImpl() { @@ -31,7 +31,7 @@ public abstract class ConfigValidatorImpl implements ConfigValidator { public static class BooleanConfigValidator extends ConfigValidatorImpl { @Override - public Boolean convert(String value) throws IllegalArgumentException { + public Boolean stringConvert(String value) throws IllegalArgumentException { return Boolean.parseBoolean(value); } @@ -43,21 +43,21 @@ public abstract class ConfigValidatorImpl implements ConfigValidator { public static class IntConfigValidator extends ConfigValidatorImpl { @Override - public Integer convert(String value) throws IllegalArgumentException { + public Integer stringConvert(String value) throws IllegalArgumentException { return Integer.parseInt(value); } } public static class StringConfigValidator extends ConfigValidatorImpl { @Override - public String convert(String value) throws IllegalArgumentException { + public String stringConvert(String value) throws IllegalArgumentException { return value; } } public static class DoubleConfigValidator extends ConfigValidatorImpl { @Override - public Double convert(String value) throws IllegalArgumentException { + public Double stringConvert(String value) throws IllegalArgumentException { return Double.parseDouble(value); } } @@ -67,32 +67,40 @@ public abstract class ConfigValidatorImpl implements ConfigValidator { } @Override - public List convert(String value) throws IllegalArgumentException { + public List stringConvert(String value) throws IllegalArgumentException { throw new IllegalArgumentException("not support"); // TODO } } - public abstract static class EnumConfigValidator> extends ConfigValidatorImpl { + public static class EnumConfigValidator> extends ConfigValidatorImpl { private final List enumValues; - public EnumConfigValidator() { - super(); + public EnumConfigValidator(@NotNull Class enumClass) { + this.fieldClass = enumClass; this.enumValues = new ArrayList<>() {{ - for (E e : getFieldClass().getEnumConstants()) { + for (E e : enumClass.getEnumConstants()) { + add(e.name().toLowerCase(Locale.ROOT)); + } + }}; + } + + public EnumConfigValidator() { + this.enumValues = new ArrayList<>() {{ + for (E e : fieldClass.getEnumConstants()) { add(e.name().toLowerCase(Locale.ROOT)); } }}; } @Override - public E convert(@NotNull String value) throws IllegalArgumentException { + public E stringConvert(@NotNull String value) throws IllegalArgumentException { return Enum.valueOf(getFieldClass(), value.toUpperCase(Locale.ROOT)); } @Override public E loadConvert(@NotNull Object value) throws IllegalArgumentException { - return this.convert(value.toString()); + return this.stringConvert(value.toString()); } @Override diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/config/GlobalConfig.java b/leaves-server/src/main/java/org/leavesmc/leaves/config/GlobalConfig.java index 7323d446..ce752fe8 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/config/GlobalConfig.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/config/GlobalConfig.java @@ -12,5 +12,5 @@ public @interface GlobalConfig { boolean lock() default false; - Class> validator() default ConfigValidatorImpl.BooleanConfigValidator.class; + Class> validator() default AutoConfigValidator.class; } diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/config/GlobalConfigManager.java b/leaves-server/src/main/java/org/leavesmc/leaves/config/GlobalConfigManager.java index adc023d5..4c9aab7d 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/config/GlobalConfigManager.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/config/GlobalConfigManager.java @@ -6,7 +6,6 @@ import org.jetbrains.annotations.Nullable; import org.leavesmc.leaves.LeavesConfig; import org.leavesmc.leaves.LeavesLogger; -import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.HashMap; @@ -113,7 +112,7 @@ public class GlobalConfigManager { return verifiedConfigs.keySet(); } - public record RemovedVerifiedConfig(ConfigConverter convert, boolean transform, Field field, Object upstreamField, String path) { + public record RemovedVerifiedConfig(ConfigTransformer transformer, boolean transform, Field field, Object upstreamField, String path) { public void run() { if (transform) { @@ -121,9 +120,10 @@ public class GlobalConfigManager { Object savedValue = LeavesConfig.config.get(path); if (savedValue != null) { try { - if (savedValue.getClass() != convert.getFieldClass()) { - savedValue = convert.loadConvert(savedValue); + if (savedValue.getClass() != transformer.getFieldClass()) { + savedValue = transformer.loadConvert(savedValue); } + savedValue = transformer.transform(savedValue); field.set(upstreamField, savedValue); } catch (IllegalAccessException | IllegalArgumentException e) { LeavesLogger.LOGGER.warning("Failure to load leaves config" + path, e); @@ -144,16 +144,14 @@ public class GlobalConfigManager { } path.append(config.name()); - ConfigConverter converter = null; + ConfigTransformer transformer = null; try { - Constructor> constructor = config.convert().getDeclaredConstructor(); - constructor.setAccessible(true); - converter = constructor.newInstance(); + transformer = createTransformer(config.transformer(), field); } catch (Exception e) { LeavesLogger.LOGGER.warning("Failure to load leaves config" + path, e); } - return new RemovedVerifiedConfig(converter, config.transform(), field, upstreamField, path.toString()); + return new RemovedVerifiedConfig(transformer, config.transform(), field, upstreamField, path.toString()); } } @@ -162,7 +160,7 @@ public class GlobalConfigManager { public void set(String stringValue) throws IllegalArgumentException { Object value; try { - value = validator.convert(stringValue); + value = validator.stringConvert(stringValue); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("value parse error: " + e.getMessage()); } @@ -210,7 +208,6 @@ public class GlobalConfigManager { return value.toString(); } - @SuppressWarnings("unchecked") @NotNull @Contract("_, _, _, _ -> new") public static VerifiedConfig build(@NotNull GlobalConfig config, @NotNull Field field, @Nullable Object upstreamField, @NotNull String upstreamPath) { @@ -218,9 +215,7 @@ public class GlobalConfigManager { ConfigValidator validator; try { - Constructor> constructor = config.validator().getDeclaredConstructor(); - constructor.setAccessible(true); - validator = (ConfigValidator) constructor.newInstance(); + validator = createValidator(config.validator(), field); } catch (Exception e) { LeavesLogger.LOGGER.severe("Failure to load leaves config" + path, e); throw new RuntimeException(); @@ -229,4 +224,26 @@ public class GlobalConfigManager { return new VerifiedConfig(validator, config.lock(), field, upstreamField, path); } } + + @SuppressWarnings("unchecked") + private static ConfigValidator createValidator(@NotNull Class> clazz, Field field) throws Exception { + if (clazz.equals(AutoConfigValidator.class)) { + return (ConfigValidator) AutoConfigValidator.createValidator(field); + } else { + var constructor = clazz.getDeclaredConstructor(); + constructor.setAccessible(true); + return (ConfigValidator) constructor.newInstance(); + } + } + + @SuppressWarnings("unchecked") + private static ConfigTransformer createTransformer(@NotNull Class> clazz, Field field) throws Exception { + if (clazz.equals(AutoConfigTransformer.class)) { + return (ConfigTransformer) AutoConfigTransformer.createValidator(field); + } else { + var constructor = clazz.getDeclaredConstructor(); + constructor.setAccessible(true); + return (ConfigTransformer) constructor.newInstance(); + } + } } diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/config/RemovedConfig.java b/leaves-server/src/main/java/org/leavesmc/leaves/config/RemovedConfig.java index 3b3c02fc..aeca5c34 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/config/RemovedConfig.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/config/RemovedConfig.java @@ -16,7 +16,7 @@ public @interface RemovedConfig { boolean transform() default false; - Class> convert() default ConfigValidatorImpl.BooleanConfigValidator.class; + Class> transformer() default AutoConfigTransformer.class; @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME)