Improve Manipulating Tag Number Types
All checks were successful
ScriptCraft/pipeline/head This commit looks good

This commit is contained in:
TheBrokenRail 2020-04-27 14:01:23 -04:00
parent 9e9710653f
commit 80559457f5
4 changed files with 61 additions and 12 deletions

View File

@ -6,9 +6,16 @@ import net.minecraft.nbt.AbstractNumberTag;
import net.minecraft.nbt.ByteTag; import net.minecraft.nbt.ByteTag;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.DoubleTag; import net.minecraft.nbt.DoubleTag;
import net.minecraft.nbt.FloatTag;
import net.minecraft.nbt.IntTag;
import net.minecraft.nbt.ListTag; import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.LongTag;
import net.minecraft.nbt.ShortTag;
import net.minecraft.nbt.StringTag; import net.minecraft.nbt.StringTag;
import net.minecraft.nbt.Tag; import net.minecraft.nbt.Tag;
import net.minecraft.util.math.MathHelper;
import java.util.Locale;
@SuppressWarnings({"rawtypes", "unchecked"}) @SuppressWarnings({"rawtypes", "unchecked"})
class TagBridges { class TagBridges {
@ -18,7 +25,7 @@ class TagBridges {
if (obj instanceof StringTag) { if (obj instanceof StringTag) {
out[1] = obj.asString(); out[1] = obj.asString();
} else if (obj instanceof AbstractNumberTag) { } else if (obj instanceof AbstractNumberTag) {
out[1] = ((AbstractNumberTag) obj).getNumber().doubleValue(); out[1] = ((AbstractNumberTag) obj).getDouble();
} else if (obj instanceof AbstractListTag) { } else if (obj instanceof AbstractListTag) {
out[0] = true; out[0] = true;
out[1] = obj; out[1] = obj;
@ -28,13 +35,37 @@ class TagBridges {
return out; return out;
} }
private static Tag toTag(Object obj) { private static Tag toTag(Object obj, String numberType) {
if (obj instanceof Tag) { if (obj instanceof Tag) {
return (Tag) obj; return (Tag) obj;
} else if (obj instanceof Boolean) { } else if (obj instanceof Boolean) {
return ByteTag.of((Boolean) obj); return ByteTag.of((Boolean) obj);
} else if (obj instanceof Double) { } 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) { } else if (obj instanceof String) {
return StringTag.of((String) obj); return StringTag.of((String) obj);
} else { } else {
@ -50,7 +81,7 @@ class TagBridges {
}); });
Bridges.addBridge("CompoundTag.set", args -> { Bridges.addBridge("CompoundTag.set", args -> {
CompoundTag tag = (CompoundTag) args[0]; 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; return null;
}); });
Bridges.addBridge("CompoundTag.keys", args -> { Bridges.addBridge("CompoundTag.keys", args -> {
@ -65,7 +96,7 @@ class TagBridges {
}); });
Bridges.addBridge("ListTag.set", args -> { Bridges.addBridge("ListTag.set", args -> {
AbstractListTag tag = (AbstractListTag) args[0]; 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; return null;
}); });
Bridges.addBridge("ListTag.size", args -> { Bridges.addBridge("ListTag.size", args -> {

View File

@ -3,5 +3,5 @@ export { CustomBlock, CustomBlockEntity, CustomBlockWithEntity, BlockSettings, B
export { ItemStack, ItemSettings, CustomItem, BlockItem } from './item'; export { ItemStack, ItemSettings, CustomItem, BlockItem } from './item';
export { World } from './world'; export { World } from './world';
export { LivingEntity, PlayerEntity } from './entity'; export { LivingEntity, PlayerEntity } from './entity';
export { CompoundTag, ListTag } from './tag'; export { CompoundTag, ListTag, NumberType } from './tag';
export { Registry } from './registry'; export { Registry } from './registry';

View File

@ -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 * Compound Tag
*/ */
@ -49,9 +61,12 @@ export class CompoundTag {
* Set Value In Tag * Set Value In Tag
* @param key Key * @param key Key
* @param value Value * @param value Value
* @param numberType Number Type
*/ */
set(key: string, value: TagType) { set(key: string, value: number, numberType: NumberType): void;
useBridge('CompoundTag.set', this.javaObject, key, (value instanceof CompoundTag || value instanceof ListTag) ? value.javaObject : value); set(key: string, value: Exclude<TagType, number>): 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 * Set Value In Tag
* @param key Key * @param key Key
* @param value Value * @param value Value
* @param numberType Number Type
*/ */
set(key: number, value: TagType) { set(key: number, value: number, numberType: NumberType): void;
useBridge('ListTag.set', this.javaObject, key, (value instanceof CompoundTag || value instanceof ListTag) ? value.javaObject : value); set(key: number, value: Exclude<TagType, number>): void;
set(key: number, value: TagType, numberType?: NumberType) {
useBridge('ListTag.set', this.javaObject, key, (value instanceof CompoundTag || value instanceof ListTag) ? value.javaObject : value, numberType);
} }
/** /**

View File

@ -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'); console.log('hello');
@ -6,7 +6,7 @@ class MyBlockEntity extends CustomBlockEntity {
ticks: number; ticks: number;
toTag(tag: CompoundTag): CompoundTag { toTag(tag: CompoundTag): CompoundTag {
tag.set('MyTicks', this.ticks); tag.set('MyTicks', this.ticks, NumberType.INT);
return tag; return tag;
} }