Skip to content

Commit

Permalink
添加规则“viewDistance”,用以设置服务器的默认视距
Browse files Browse the repository at this point in the history
  • Loading branch information
Gu-ZT committed Nov 8, 2024
1 parent 2e54874 commit f82c99c
Show file tree
Hide file tree
Showing 11 changed files with 193 additions and 180 deletions.
1 change: 1 addition & 0 deletions src/main/java/dev/anvilcraft/rg/RollingGateCategories.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

public class RollingGateCategories {
public static final String BASE = "base";
public static final String CREATIVE = "creative";
public static final String DISABLED = "disabled";
}
20 changes: 19 additions & 1 deletion src/main/java/dev/anvilcraft/rg/RollingGateServerRules.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
package dev.anvilcraft.rg;

import dev.anvilcraft.rg.api.RGValidator;
import dev.anvilcraft.rg.api.Rule;
import org.jetbrains.annotations.NotNull;

import java.util.Map;

public class RollingGateServerRules {
@Rule(allowed = {"zh_cn", "en_us"}, categories = {RollingGateCategories.BASE})
@Rule(allowed = {"zh_cn", "en_us"}, categories = RollingGateCategories.BASE)
public static String language = "zh_cn";

public static class ViewDistanceValidator extends RGValidator.IntegerValidator {
@Override
public @NotNull Map.Entry<Integer, Integer> getRange() {
return Map.entry(0, 32);
}
}

@Rule(
allowed = {"0", "12", "16", "32"},
categories = RollingGateCategories.CREATIVE,
validator = ViewDistanceValidator.class
)
public static int viewDistance = 0;
}
13 changes: 12 additions & 1 deletion src/main/java/dev/anvilcraft/rg/api/RGRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.google.gson.JsonElement;
import dev.anvilcraft.rg.RollingGate;
import dev.anvilcraft.rg.api.event.RGRuleChangeEvent;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.server.ServerLifecycleHooks;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Field;
Expand Down Expand Up @@ -153,7 +156,15 @@ public void setFieldValue(String value) {
throw new RGRuleException("Illegal value: %s, reason: %s", value, validator.reason());
}
}
this.field.set(null, this.codec.decode(value));
RGRuleChangeEvent<T> event;
if (this.environment().isServer()) {
event = new RGRuleChangeEvent.Server<>(this, this.getValue(), this.codec.decode(value), ServerLifecycleHooks.getCurrentServer());
} else {
event = new RGRuleChangeEvent.Client<>(this, this.getValue(), this.codec.decode(value));
}
NeoForge.EVENT_BUS.post(event);
if (event.isCanceled()) return;
this.field.set(null, event.getNewValue());
} catch (IllegalAccessException e) {
throw new RGRuleException("Illegal value: %s", value);
}
Expand Down
207 changes: 38 additions & 169 deletions src/main/java/dev/anvilcraft/rg/api/RGValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@SuppressWarnings("unused")
public interface RGValidator<T> {
Expand Down Expand Up @@ -39,188 +36,60 @@ public String reason() {
}
}

abstract class ValidatorFactory<T> {
public abstract Class<? extends RGValidator<T>> get();
abstract class NumberValidator<T extends Number> implements RGValidator<Integer> {
public abstract @NotNull Map.Entry<T, T> getRange();


private static class ByteValidatorFactory extends ValidatorFactory<Byte> {
private final byte min;
private final byte max;

private ByteValidatorFactory(byte min, byte max) {
this.min = min;
this.max = max;
}

public Class<? extends RGValidator<Byte>> get() {
return RangeValidator.class;
}

public class RangeValidator implements RGValidator<Byte> {
@Override
public boolean validate(@NotNull Byte oldValue, @NotNull String newValue) {
try {
byte value = Byte.parseByte(newValue);
return value >= min && value <= max;
} catch (NumberFormatException e) {
return false;
}
}

@Override
public String reason() {
return "The input value must be between " + min + " and " + max + "!";
}
}
}


private static class ShortValidatorFactory extends ValidatorFactory<Short> {
private final short min;
private final short max;

private ShortValidatorFactory(short min, short max) {
this.min = min;
this.max = max;
}

public Class<? extends RGValidator<Short>> get() {
return RangeValidator.class;
}

public class RangeValidator implements RGValidator<Short> {
@Override
public boolean validate(@NotNull Short oldValue, @NotNull String newValue) {
try {
short value = Short.parseShort(newValue);
return value >= min && value <= max;
} catch (NumberFormatException e) {
return false;
}
}

@Override
public String reason() {
return "The input value must be between " + min + " and " + max + "!";
}
}
}

private static class IntValidatorFactory extends ValidatorFactory<Integer> {
private final int min;
private final int max;

private IntValidatorFactory(int min, int max) {
this.min = min;
this.max = max;
}

public Class<? extends RGValidator<Integer>> get() {
return RangeValidator.class;
}

public class RangeValidator implements RGValidator<Integer> {
@Override
public boolean validate(@NotNull Integer oldValue, @NotNull String newValue) {
try {
int value = Integer.parseInt(newValue);
return value >= min && value <= max;
} catch (NumberFormatException e) {
return false;
}
}

@Override
public String reason() {
return "The input value must be between " + min + " and " + max + "!";
}
@Override
public boolean validate(@NotNull Integer oldValue, @NotNull String newValue) {
try {
T value = parse(newValue);
return value.doubleValue() >= getRange().getKey().doubleValue() && value.doubleValue() <= getRange().getValue().doubleValue();
} catch (NumberFormatException e) {
return false;
}
}


private static class LongValidatorFactory extends ValidatorFactory<Long> {
private final long min;
private final long max;

private LongValidatorFactory(long min, long max) {
this.min = min;
this.max = max;
}

public Class<? extends RGValidator<Long>> get() {
return RangeValidator.class;
}

public class RangeValidator implements RGValidator<Long> {
@Override
public boolean validate(@NotNull Long oldValue, @NotNull String newValue) {
try {
long value = Long.parseLong(newValue);
return value >= min && value <= max;
} catch (NumberFormatException e) {
return false;
}
}

@Override
public String reason() {
return "The input value must be between " + min + " and " + max + "!";
}
}
@Override
public String reason() {
return "The input value must be between " + getRange().getKey().toString() + " and " + getRange().getValue().toString() + "!";
}

protected abstract T parse(@NotNull String newValue);
}

private static class StringValidatorFactory extends ValidatorFactory<String> {
private final List<String> accept;

private StringValidatorFactory(String... accept) {
this.accept = Arrays.asList(accept);
}

private StringValidatorFactory(Collection<String> accept) {
this.accept = new ArrayList<>(accept);
}

public Class<? extends RGValidator<String>> get() {
return RangeValidator.class;
}

public class RangeValidator implements RGValidator<String> {
@Override
public boolean validate(@NotNull String oldValue, @NotNull String newValue) {
if (newValue.isEmpty()) return false;
return accept.contains(newValue);
}

@Override
public String reason() {
return "The input value must be one of %s!".formatted(accept);
}
}
}

public static @NotNull Class<? extends RGValidator<Byte>> rangeByte(byte min, byte max) {
return new ByteValidatorFactory(min, max).get();
abstract class ByteValidator extends NumberValidator<Byte> {
protected Byte parse(@NotNull String newValue) {
return Byte.parseByte(newValue);
}
}

public static @NotNull Class<? extends RGValidator<Short>> rangeShort(short min, short max) {
return new ShortValidatorFactory(min, max).get();
abstract class ShortValidator extends NumberValidator<Short> {
protected Short parse(@NotNull String newValue) {
return Short.parseShort(newValue);
}
}

public static @NotNull Class<? extends RGValidator<Integer>> rangeInt(int min, int max) {
return new IntValidatorFactory(min, max).get();
abstract class IntegerValidator extends NumberValidator<Integer> {
protected Integer parse(@NotNull String newValue) {
return Integer.parseInt(newValue);
}
}

public static @NotNull Class<? extends RGValidator<Long>> rangeLong(long min, long max) {
return new LongValidatorFactory(min, max).get();
abstract class LongValidator extends NumberValidator<Long> {
protected Long parse(@NotNull String newValue) {
return Long.parseLong(newValue);
}
}

public static @NotNull Class<? extends RGValidator<String>> rangeString(String... accept) {
return new StringValidatorFactory(accept).get();
abstract class FloatValidator extends NumberValidator<Float> {
protected Float parse(@NotNull String newValue) {
return Float.parseFloat(newValue);
}
}

public static @NotNull Class<? extends RGValidator<String>> rangeString(Collection<String> accept) {
return new StringValidatorFactory(accept).get();
abstract class DoubleValidator extends NumberValidator<Double> {
protected Double parse(@NotNull String newValue) {
return Double.parseDouble(newValue);
}
}
}
53 changes: 53 additions & 0 deletions src/main/java/dev/anvilcraft/rg/api/event/RGRuleChangeEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package dev.anvilcraft.rg.api.event;

import dev.anvilcraft.rg.api.RGRule;
import net.minecraft.server.MinecraftServer;
import net.neoforged.bus.api.Event;
import net.neoforged.bus.api.ICancellableEvent;

public class RGRuleChangeEvent<T> extends Event implements ICancellableEvent {
private final RGRule<T> rule;
private final T oldValue;
private T newValue;

public RGRuleChangeEvent(RGRule<T> rule, T oldValue, T newValue) {
this.rule = rule;
this.oldValue = oldValue;
this.newValue = newValue;
}

public RGRule<T> getRule() {
return rule;
}

public T getNewValue() {
return newValue;
}

public T getOldValue() {
return oldValue;
}

public void setNewValue(T newValue) {
this.newValue = newValue;
}

public static class Server<T> extends RGRuleChangeEvent<T> {
private final MinecraftServer server;

public Server(RGRule<T> rule, T oldValue, T newValue, MinecraftServer server) {
super(rule, oldValue, newValue);
this.server = server;
}

public MinecraftServer getServer() {
return server;
}
}

public static class Client<T> extends RGRuleChangeEvent<T> {
public Client(RGRule<T> rule, T oldValue, T newValue) {
super(rule, oldValue, newValue);
}
}
}
Loading

0 comments on commit f82c99c

Please sign in to comment.