Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/releases/1.21' into releases/1.21
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/java/dev/anvilcraft/rg/api/RGRule.java
#	src/main/java/dev/anvilcraft/rg/api/RGRuleException.java
  • Loading branch information
Cjsah committed Sep 17, 2024
2 parents c1b689a + 5b92eb2 commit 73843a2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 15 deletions.
43 changes: 28 additions & 15 deletions src/main/java/dev/anvilcraft/rg/api/RGRule.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package dev.anvilcraft.rg.api;

import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

public class RGRule<T> {
final String namespace;
final Class<T> type;
final RGEnvironment environment;
final String serialize;
Expand All @@ -17,6 +17,7 @@ public class RGRule<T> {
final Field field;

public RGRule(String namespace, Class<T> type, RGEnvironment environment, String serialize, String[] preset, RGValidator<T> validator, T defaultValue, Field field) {
this.namespace = namespace;
this.type = type;
this.environment = environment;
this.serialize = serialize;
Expand All @@ -28,20 +29,12 @@ public RGRule(String namespace, Class<T> type, RGEnvironment environment, String

@SuppressWarnings("unchecked")
public static <T> @NotNull RGRule<T> of(String namespace, @NotNull Field field) {
if (!Modifier.isStatic(field.getModifiers())) {
throw new RuntimeException("Field %s is not static".formatted(field.getName()));
}
if (!Modifier.isPublic(field.getModifiers())) {
throw new RuntimeException("Field %s is not public".formatted(field.getName()));
}
if (!Modifier.isFinal(field.getModifiers())) {
throw new RuntimeException("Field %s can't be final".formatted(field.getName()));
}
if (!Modifier.isStatic(field.getModifiers())) throw RGRuleException.notStatic(field.getName());
if (!Modifier.isPublic(field.getModifiers())) throw RGRuleException.notPublic(field.getName());
if (Modifier.isFinal(field.getModifiers())) throw RGRuleException.beFinal(field.getName());
Class<?> type = RGRule.checkType(field);
Rule rule = field.getAnnotation(Rule.class);
if (rule == null) {
throw new RuntimeException("Field %s is not annotated with @Rule".formatted(field.getName()));
}
if (rule == null) throw RGRuleException.notAnnotated(field.getName());
String serialize = rule.serialize().isEmpty() ? RGRule.caseToSnake(field.getName()) : rule.serialize();
RGRule.checkSerialize(serialize);
try {
Expand All @@ -56,7 +49,7 @@ public RGRule(String namespace, Class<T> type, RGEnvironment environment, String
field
);
} catch (Exception e) {
throw new RuntimeException("Failed to create rule for field %s".formatted(field.getName()), e);
throw RGRuleException.createRuleFailed(field.getName());
}
}

Expand All @@ -82,7 +75,7 @@ public static Class<?> checkType(@NotNull Field field) {
case "float", "java.lang.Float" -> Float.class;
case "double", "java.lang.Double" -> Double.class;
case "java.lang.String" -> String.class;
default -> throw new RGRuleException("Field %s has unsupported type %s", field.getName(), field.getType().getTypeName());
default -> throw RGRuleException.unsupportedType(field.getName(), field.getType());
};
}

Expand Down Expand Up @@ -156,4 +149,24 @@ public void setFieldValue(JsonElement primitive) {
};
this.setFieldValue((T) value);
}

/**
* 获取规则的名称翻译键
*
* @return 返回格式化的名称翻译键字符串,包括命名空间和序列化字段
*/
public @NotNull String getNameTranslationKey() {
// 使用formatted方法格式化名称翻译键
return "%s.rolling_gate.rule.%s".formatted(this.namespace, this.serialize);
}

/**
* 获取规则的描述翻译键
*
* @return 返回格式化的描述翻译键字符串,包括命名空间和序列化字段
*/
public @NotNull String getDescriptionTranslationKey() {
// 使用String.format方法构建描述翻译键,包含命名空间和序列化值
return "%s.rolling_gate.rule.%s.desc".formatted(this.namespace, this.serialize);
}
}
26 changes: 26 additions & 0 deletions src/main/java/dev/anvilcraft/rg/api/RGRuleException.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.anvilcraft.rg.api;

import org.jetbrains.annotations.NotNull;

public class RGRuleException extends RuntimeException {
public RGRuleException(String msg, Object... args) {
super(String.format(msg, args));
Expand All @@ -8,4 +10,28 @@ public RGRuleException(String msg, Object... args) {
public RGRuleException(String msg, Throwable e) {
super(msg, e);
}

public static @NotNull RGRuleException notStatic(@NotNull String name) {
return new RGRuleException("Field %s is not static", name);
}

public static @NotNull RGRuleException notPublic(@NotNull String name) {
return new RGRuleException("Field %s is not public", name);
}

public static @NotNull RGRuleException beFinal(@NotNull String name) {
return new RGRuleException("Field %s can't be final", name);
}

public static @NotNull RGRuleException notAnnotated(@NotNull String name) {
return new RGRuleException("Field %s is not annotated with @Rule", name);
}

public static @NotNull RGRuleException createRuleFailed(@NotNull String name) {
return new RGRuleException("Failed to create rule for field %s", name);
}

public static @NotNull RGRuleException unsupportedType(@NotNull String name, @NotNull Class<?> type) {
return new RGRuleException("Field %s has unsupported type, this type can only be boolean, byte, int, long, float, double, String, but got %s", name, type.getTypeName());
}
}

0 comments on commit 73843a2

Please sign in to comment.