mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-19 15:09:15 +00:00
拓展tag以实现指定数字类型
This commit is contained in:
@@ -4,25 +4,44 @@ import net.momirealms.craftengine.core.plugin.locale.TranslationManager;
|
|||||||
import net.momirealms.craftengine.core.util.VersionHelper;
|
import net.momirealms.craftengine.core.util.VersionHelper;
|
||||||
import org.yaml.snakeyaml.LoaderOptions;
|
import org.yaml.snakeyaml.LoaderOptions;
|
||||||
import org.yaml.snakeyaml.constructor.SafeConstructor;
|
import org.yaml.snakeyaml.constructor.SafeConstructor;
|
||||||
import org.yaml.snakeyaml.nodes.MappingNode;
|
import org.yaml.snakeyaml.nodes.*;
|
||||||
import org.yaml.snakeyaml.nodes.Node;
|
|
||||||
import org.yaml.snakeyaml.nodes.NodeTuple;
|
|
||||||
import org.yaml.snakeyaml.nodes.ScalarNode;
|
|
||||||
|
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static org.yaml.snakeyaml.nodes.Tag.PREFIX;
|
||||||
|
import static org.yaml.snakeyaml.nodes.Tag.standardTags;
|
||||||
|
|
||||||
public class StringKeyConstructor extends SafeConstructor {
|
public class StringKeyConstructor extends SafeConstructor {
|
||||||
private final Path path;
|
private final Path path;
|
||||||
private static final String VERSION_PREFIX = "$$";
|
private static final String VERSION_PREFIX = "$$";
|
||||||
private static final String DEEP_KEY_SEPARATOR = "::";
|
private static final String DEEP_KEY_SEPARATOR = "::";
|
||||||
|
public static final Tag BYTE = new Tag(PREFIX + "byte");
|
||||||
|
public static final Tag SHORT = new Tag(PREFIX + "short");
|
||||||
|
public static final Tag LONG = new Tag(PREFIX + "long");
|
||||||
|
public static final Tag FLOAT_SINGLE = new Tag(PREFIX + "float_single"); // 原本有个!!float解析出来的是双精度浮点数使用这个避免命名冲突
|
||||||
|
public static final Tag FLOAT_DOUBLE = new Tag(PREFIX + "double");
|
||||||
|
|
||||||
|
static {
|
||||||
|
standardTags.add(BYTE);
|
||||||
|
standardTags.add(SHORT);
|
||||||
|
standardTags.add(LONG);
|
||||||
|
standardTags.add(FLOAT_SINGLE);
|
||||||
|
standardTags.add(FLOAT_DOUBLE);
|
||||||
|
}
|
||||||
|
|
||||||
public StringKeyConstructor(Path path, LoaderOptions loaderOptions) {
|
public StringKeyConstructor(Path path, LoaderOptions loaderOptions) {
|
||||||
super(loaderOptions);
|
super(loaderOptions);
|
||||||
|
this.yamlConstructors.put(BYTE, new ConstructYamlByte());
|
||||||
|
this.yamlConstructors.put(SHORT, new ConstructYamlShort());
|
||||||
|
this.yamlConstructors.put(LONG, new ConstructYamlLong());
|
||||||
|
this.yamlConstructors.put(FLOAT_SINGLE, new ConstructYamlFloatSingle());
|
||||||
|
this.yamlConstructors.put(FLOAT_DOUBLE, new ConstructYamlDouble());
|
||||||
this.path = path;
|
this.path = path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private boolean isVersionMatch(String versionSpec) {
|
private boolean isVersionMatch(String versionSpec) {
|
||||||
int index = versionSpec.indexOf('~');
|
int index = versionSpec.indexOf('~');
|
||||||
// 没有范围值
|
// 没有范围值
|
||||||
@@ -235,4 +254,64 @@ public class StringKeyConstructor extends SafeConstructor {
|
|||||||
configKey,
|
configKey,
|
||||||
String.valueOf(node.getStartMark().getLine() + 1));
|
String.valueOf(node.getStartMark().getLine() + 1));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
public class ConstructYamlByte extends ConstructYamlInt {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object construct(Node node) {
|
||||||
|
Object value = super.construct(node);
|
||||||
|
if (value instanceof Number number) {
|
||||||
|
return number.byteValue();
|
||||||
|
}
|
||||||
|
throw new RuntimeException("Unexpected type: " + value.getClass().getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ConstructYamlShort extends ConstructYamlInt {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object construct(Node node) {
|
||||||
|
Object value = super.construct(node);
|
||||||
|
if (value instanceof Number number) {
|
||||||
|
return number.shortValue();
|
||||||
|
}
|
||||||
|
throw new RuntimeException("Unexpected type: " + value.getClass().getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ConstructYamlLong extends ConstructYamlInt {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object construct(Node node) {
|
||||||
|
Object value = super.construct(node);
|
||||||
|
if (value instanceof Number number) {
|
||||||
|
return number.longValue();
|
||||||
|
}
|
||||||
|
throw new RuntimeException("Unexpected type: " + value.getClass().getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ConstructYamlFloatSingle extends ConstructYamlFloat {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object construct(Node node) {
|
||||||
|
Object value = super.construct(node);
|
||||||
|
if (value instanceof Number number) {
|
||||||
|
return number.floatValue();
|
||||||
|
}
|
||||||
|
throw new RuntimeException("Unexpected type: " + value.getClass().getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ConstructYamlDouble extends ConstructYamlFloat {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object construct(Node node) {
|
||||||
|
Object value = super.construct(node);
|
||||||
|
if (value instanceof Number number) {
|
||||||
|
return number.doubleValue();
|
||||||
|
}
|
||||||
|
throw new RuntimeException("Unexpected type: " + value.getClass().getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user