From 121fe393162754f74cc5500687c59f13e69fea8e Mon Sep 17 00:00:00 2001 From: Cjsah Date: Tue, 17 Sep 2024 18:40:39 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E7=BD=AErule=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/dev/anvilcraft/rg/api/RGRule.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/main/java/dev/anvilcraft/rg/api/RGRule.java b/src/main/java/dev/anvilcraft/rg/api/RGRule.java index 9158ee5..ee81745 100644 --- a/src/main/java/dev/anvilcraft/rg/api/RGRule.java +++ b/src/main/java/dev/anvilcraft/rg/api/RGRule.java @@ -114,4 +114,21 @@ public static void checkSerialize(@NotNull String str) { // 使用正则表达式匹配驼峰命名法中的大写字母,并在其前添加下划线,然后将整个字符串转换为小写 return str.replaceAll("([a-z])([A-Z]+)", "$1_$2").toLowerCase(); } + + /** + * 设置字段的值 + * + * @param value 要设置的字段值 + * @throws RGRuleException 当值无法被设置时抛出异常 + */ + @SuppressWarnings("unchecked") + public void setFieldValue(T value) { + try { + if (this.validator.validate((T) this.field.get(null), value)) { + this.field.set(null, value); + } + } catch (IllegalAccessException e) { + throw new RGRuleException("Illegal value: %s", value); + } + } }