This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
ScriptCraft/scriptcraft/src/main/java/com/thebrokenrail/scriptcraft/api/bridge/TagBridges.java

117 lines
4.2 KiB
Java
Raw Normal View History

2020-04-28 00:15:24 +00:00
package com.thebrokenrail.scriptcraft.api.bridge;
2020-04-26 01:17:59 +00:00
2020-04-28 00:15:24 +00:00
import com.thebrokenrail.scriptcraft.core.ScriptCraftCore;
2020-06-07 21:58:49 +00:00
import com.thebrokenrail.scriptcraft.api.util.ValueUtil;
2020-04-26 01:17:59 +00:00
import net.minecraft.nbt.AbstractListTag;
import net.minecraft.nbt.AbstractNumberTag;
import net.minecraft.nbt.ByteTag;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.DoubleTag;
2020-04-27 18:01:23 +00:00
import net.minecraft.nbt.FloatTag;
import net.minecraft.nbt.IntTag;
2020-04-26 01:25:57 +00:00
import net.minecraft.nbt.ListTag;
2020-04-27 18:01:23 +00:00
import net.minecraft.nbt.LongTag;
import net.minecraft.nbt.ShortTag;
2020-04-26 01:17:59 +00:00
import net.minecraft.nbt.StringTag;
import net.minecraft.nbt.Tag;
2020-04-27 18:01:23 +00:00
import net.minecraft.util.math.MathHelper;
import java.util.Locale;
2020-04-26 01:17:59 +00:00
@SuppressWarnings({"rawtypes", "unchecked"})
class TagBridges {
private static Object[] toOut(Tag obj) {
Object[] out = new Object[2];
out[0] = false;
2020-04-28 20:18:22 +00:00
out[1] = null;
2020-04-26 01:17:59 +00:00
if (obj instanceof StringTag) {
out[1] = obj.asString();
} else if (obj instanceof AbstractNumberTag) {
2020-04-27 18:01:23 +00:00
out[1] = ((AbstractNumberTag) obj).getDouble();
2020-04-26 01:17:59 +00:00
} else if (obj instanceof AbstractListTag) {
out[0] = true;
out[1] = obj;
} else if (obj instanceof CompoundTag) {
out[1] = obj;
}
return out;
}
2020-04-27 18:01:23 +00:00
private static Tag toTag(Object obj, String numberType) {
2020-04-26 01:17:59 +00:00
if (obj instanceof Tag) {
return (Tag) obj;
} else if (obj instanceof Boolean) {
return ByteTag.of((Boolean) obj);
} else if (obj instanceof Double) {
2020-04-27 18:01:23 +00:00
double value = (Double) obj;
numberType = numberType.toUpperCase(Locale.ROOT);
switch (numberType.toUpperCase(Locale.ROOT)) {
case "INT": {
return IntTag.of(MathHelper.floor(value));
}
case "BYTE": {
return ByteTag.of((byte) (MathHelper.floor(value) & 255));
}
case "DOUBLE": {
return DoubleTag.of(value);
}
case "LONG": {
return LongTag.of((long) Math.floor(value));
}
case "FLOAT": {
return FloatTag.of((float) value);
}
case "SHORT": {
return ShortTag.of((short) (MathHelper.floor(value) & '\uffff'));
}
default: {
throw new RuntimeException("Invalid Number Type: " + numberType);
}
}
2020-04-26 01:17:59 +00:00
} else if (obj instanceof String) {
return StringTag.of((String) obj);
} else {
return null;
}
}
static void register() {
2020-04-28 00:15:24 +00:00
ScriptCraftCore.addBridge("CompoundTag.get", args -> {
2020-04-26 01:17:59 +00:00
CompoundTag tag = (CompoundTag) args[0];
Tag obj = tag.get((String) args[1]);
return toOut(obj);
});
2020-04-28 00:15:24 +00:00
ScriptCraftCore.addBridge("CompoundTag.set", args -> {
2020-04-26 01:17:59 +00:00
CompoundTag tag = (CompoundTag) args[0];
2020-04-27 18:01:23 +00:00
tag.put((String) args[1], toTag(args[2], (String) args[3]));
2020-04-26 01:17:59 +00:00
return null;
});
2020-04-28 00:15:24 +00:00
ScriptCraftCore.addBridge("CompoundTag.keys", args -> {
2020-04-26 01:17:59 +00:00
CompoundTag tag = (CompoundTag) args[0];
return tag.getKeys().toArray(new String[0]);
});
2020-06-02 17:24:32 +00:00
ScriptCraftCore.addBridge("CompoundTag.isValid", args -> args[0] instanceof CompoundTag);
2020-04-28 00:15:24 +00:00
ScriptCraftCore.addBridge("ListTag.get", args -> {
2020-04-26 01:17:59 +00:00
AbstractListTag<?> tag = (AbstractListTag<?>) args[0];
2020-04-28 00:15:24 +00:00
Tag obj = tag.get((int) ValueUtil.toDouble(args[1], 0));
2020-04-26 01:17:59 +00:00
return toOut(obj);
});
2020-04-28 00:15:24 +00:00
ScriptCraftCore.addBridge("ListTag.set", args -> {
2020-04-26 01:17:59 +00:00
AbstractListTag tag = (AbstractListTag) args[0];
2020-04-28 00:15:24 +00:00
tag.set((int) ValueUtil.toDouble(args[1], 0), toTag(args[2], (String) args[3]));
2020-04-26 01:17:59 +00:00
return null;
});
2020-04-28 00:15:24 +00:00
ScriptCraftCore.addBridge("ListTag.size", args -> {
2020-04-26 01:17:59 +00:00
AbstractListTag<?> tag = (AbstractListTag<?>) args[0];
return tag.size();
});
2020-04-26 01:25:57 +00:00
2020-06-02 17:24:32 +00:00
ScriptCraftCore.addBridge("ListTag.isValid", args -> args[0] instanceof ListTag);
2020-04-28 00:15:24 +00:00
ScriptCraftCore.addBridge("CompoundTag.create", args -> new CompoundTag());
ScriptCraftCore.addBridge("ListTag.create", args -> new ListTag());
2020-04-26 01:17:59 +00:00
}
}