From 80559457f5560efa94586b2c4e5d545bef4760e1 Mon Sep 17 00:00:00 2001 From: TheBrokenRail Date: Mon, 27 Apr 2020 14:01:23 -0400 Subject: [PATCH] Improve Manipulating Tag Number Types --- .../scriptcraft/bridge/TagBridges.java | 41 ++++++++++++++++--- .../resources/scriptcraft/minecraft/index.ts | 2 +- .../resources/scriptcraft/minecraft/tag.ts | 26 ++++++++++-- src/main/resources/scriptcraft/test/index.ts | 4 +- 4 files changed, 61 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/thebrokenrail/scriptcraft/bridge/TagBridges.java b/src/main/java/com/thebrokenrail/scriptcraft/bridge/TagBridges.java index 5a8ee29..e4689c7 100644 --- a/src/main/java/com/thebrokenrail/scriptcraft/bridge/TagBridges.java +++ b/src/main/java/com/thebrokenrail/scriptcraft/bridge/TagBridges.java @@ -6,9 +6,16 @@ import net.minecraft.nbt.AbstractNumberTag; import net.minecraft.nbt.ByteTag; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.DoubleTag; +import net.minecraft.nbt.FloatTag; +import net.minecraft.nbt.IntTag; import net.minecraft.nbt.ListTag; +import net.minecraft.nbt.LongTag; +import net.minecraft.nbt.ShortTag; import net.minecraft.nbt.StringTag; import net.minecraft.nbt.Tag; +import net.minecraft.util.math.MathHelper; + +import java.util.Locale; @SuppressWarnings({"rawtypes", "unchecked"}) class TagBridges { @@ -18,7 +25,7 @@ class TagBridges { if (obj instanceof StringTag) { out[1] = obj.asString(); } else if (obj instanceof AbstractNumberTag) { - out[1] = ((AbstractNumberTag) obj).getNumber().doubleValue(); + out[1] = ((AbstractNumberTag) obj).getDouble(); } else if (obj instanceof AbstractListTag) { out[0] = true; out[1] = obj; @@ -28,13 +35,37 @@ class TagBridges { return out; } - private static Tag toTag(Object obj) { + private static Tag toTag(Object obj, String numberType) { if (obj instanceof Tag) { return (Tag) obj; } else if (obj instanceof Boolean) { return ByteTag.of((Boolean) obj); } else if (obj instanceof Double) { - return DoubleTag.of((Double) obj); + 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); + } + } } else if (obj instanceof String) { return StringTag.of((String) obj); } else { @@ -50,7 +81,7 @@ class TagBridges { }); Bridges.addBridge("CompoundTag.set", args -> { CompoundTag tag = (CompoundTag) args[0]; - tag.put((String) args[1], toTag(args[2])); + tag.put((String) args[1], toTag(args[2], (String) args[3])); return null; }); Bridges.addBridge("CompoundTag.keys", args -> { @@ -65,7 +96,7 @@ class TagBridges { }); Bridges.addBridge("ListTag.set", args -> { AbstractListTag tag = (AbstractListTag) args[0]; - tag.set((int) Util.toDouble(args[1], 0), toTag(args[2])); + tag.set((int) Util.toDouble(args[1], 0), toTag(args[2], (String) args[3])); return null; }); Bridges.addBridge("ListTag.size", args -> { diff --git a/src/main/resources/scriptcraft/minecraft/index.ts b/src/main/resources/scriptcraft/minecraft/index.ts index 446cfd8..d57192f 100644 --- a/src/main/resources/scriptcraft/minecraft/index.ts +++ b/src/main/resources/scriptcraft/minecraft/index.ts @@ -3,5 +3,5 @@ export { CustomBlock, CustomBlockEntity, CustomBlockWithEntity, BlockSettings, B export { ItemStack, ItemSettings, CustomItem, BlockItem } from './item'; export { World } from './world'; export { LivingEntity, PlayerEntity } from './entity'; -export { CompoundTag, ListTag } from './tag'; +export { CompoundTag, ListTag, NumberType } from './tag'; export { Registry } from './registry'; \ No newline at end of file diff --git a/src/main/resources/scriptcraft/minecraft/tag.ts b/src/main/resources/scriptcraft/minecraft/tag.ts index a445f73..05c3563 100644 --- a/src/main/resources/scriptcraft/minecraft/tag.ts +++ b/src/main/resources/scriptcraft/minecraft/tag.ts @@ -19,6 +19,18 @@ function getTagValue(obj: [boolean, BridgeValueType]): TagType { } } +/** + * Number Types + */ +export enum NumberType { + INT = 'INT', + BYTE = 'BYTE', + DOUBLE = 'DOUBLE', + LONG = 'LONG', + FLOAT = 'FLOAT', + SHORT = 'SHORT' +} + /** * Compound Tag */ @@ -49,9 +61,12 @@ export class CompoundTag { * Set Value In Tag * @param key Key * @param value Value + * @param numberType Number Type */ - set(key: string, value: TagType) { - useBridge('CompoundTag.set', this.javaObject, key, (value instanceof CompoundTag || value instanceof ListTag) ? value.javaObject : value); + set(key: string, value: number, numberType: NumberType): void; + set(key: string, value: Exclude): void; + set(key: string, value: TagType, numberType?: NumberType) { + useBridge('CompoundTag.set', this.javaObject, key, (value instanceof CompoundTag || value instanceof ListTag) ? value.javaObject : value, numberType); } /** @@ -106,9 +121,12 @@ export class ListTag { * Set Value In Tag * @param key Key * @param value Value + * @param numberType Number Type */ - set(key: number, value: TagType) { - useBridge('ListTag.set', this.javaObject, key, (value instanceof CompoundTag || value instanceof ListTag) ? value.javaObject : value); + set(key: number, value: number, numberType: NumberType): void; + set(key: number, value: Exclude): void; + set(key: number, value: TagType, numberType?: NumberType) { + useBridge('ListTag.set', this.javaObject, key, (value instanceof CompoundTag || value instanceof ListTag) ? value.javaObject : value, numberType); } /** diff --git a/src/main/resources/scriptcraft/test/index.ts b/src/main/resources/scriptcraft/test/index.ts index 83f425d..3bf6cb8 100644 --- a/src/main/resources/scriptcraft/test/index.ts +++ b/src/main/resources/scriptcraft/test/index.ts @@ -1,4 +1,4 @@ -import { BlockSettings, Identifier, Registry, BlockState, ActionResult, World, Pos, Hand, PlayerEntity, BlockItem, ItemSettings, CustomItem, Direction, LivingEntity, CustomBlockWithEntity, CustomBlockEntity, CompoundTag } from 'minecraft'; +import { BlockSettings, Identifier, Registry, BlockState, ActionResult, World, Pos, Hand, PlayerEntity, BlockItem, ItemSettings, CustomItem, Direction, LivingEntity, CustomBlockWithEntity, CustomBlockEntity, CompoundTag, NumberType } from 'minecraft'; console.log('hello'); @@ -6,7 +6,7 @@ class MyBlockEntity extends CustomBlockEntity { ticks: number; toTag(tag: CompoundTag): CompoundTag { - tag.set('MyTicks', this.ticks); + tag.set('MyTicks', this.ticks, NumberType.INT); return tag; }