Add Tag Bindings
All checks were successful
ScriptCraft/pipeline/head This commit looks good

This commit is contained in:
TheBrokenRail 2020-04-25 21:17:59 -04:00
parent a8e9aa99f1
commit d2ba0b6cf2
11 changed files with 250 additions and 6 deletions

View File

@ -37,5 +37,6 @@ public class Bridges {
ItemSettingsBridges.register();
EntityBridges.register();
DamageSourceBridges.register();
TagBridges.register();
}
}

View File

@ -3,6 +3,7 @@ package com.thebrokenrail.scriptcraft.bridge;
import com.thebrokenrail.scriptcraft.util.Util;
import net.minecraft.entity.Entity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.registry.Registry;
@ -45,5 +46,11 @@ class EntityBridges {
((Entity) args[0]).updatePosition(Util.toDouble(args[1], 0), Util.toDouble(args[2], 0), Util.toDouble(args[3], 0));
return null;
});
Bridges.addBridge("Entity.toTag", args -> ((Entity) args[0]).toTag(new CompoundTag()));
Bridges.addBridge("Entity.fromTag", args -> {
((Entity) args[0]).fromTag((CompoundTag) args[1]);
return null;
});
}
}

View File

@ -1,13 +1,21 @@
package com.thebrokenrail.scriptcraft.bridge;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
class ItemStackBridges {
static void register() {
Bridges.addBridge("ItemStack.create", args -> new ItemStack(Registry.ITEM.get(new Identifier((String) args[0])), ((Double) args[1]).intValue()));
Bridges.addBridge("ItemStack.getItem", args -> Registry.ITEM.getId(((ItemStack) args[0]).getItem()).toString());
Bridges.addBridge("ItemStack.getCount", args -> (double) ((ItemStack) args[0]).getCount());
Bridges.addBridge("ItemStack.getTag", args -> ((ItemStack) args[0]).getTag());
Bridges.addBridge("ItemStack.setTag", args -> {
((ItemStack) args[0]).setTag((CompoundTag) args[1]);
return null;
});
}
}

View File

@ -0,0 +1,75 @@
package com.thebrokenrail.scriptcraft.bridge;
import com.thebrokenrail.scriptcraft.util.Util;
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;
import net.minecraft.nbt.StringTag;
import net.minecraft.nbt.Tag;
@SuppressWarnings({"rawtypes", "unchecked"})
class TagBridges {
private static Object[] toOut(Tag obj) {
Object[] out = new Object[2];
out[0] = false;
if (obj instanceof StringTag) {
out[1] = obj.asString();
} else if (obj instanceof AbstractNumberTag) {
out[1] = ((AbstractNumberTag) obj).getNumber().doubleValue();
} else if (obj instanceof AbstractListTag) {
out[0] = true;
out[1] = obj;
} else if (obj instanceof CompoundTag) {
out[1] = obj;
}
return out;
}
private static Tag toTag(Object obj) {
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);
} else if (obj instanceof String) {
return StringTag.of((String) obj);
} else {
return null;
}
}
static void register() {
Bridges.addBridge("CompoundTag.get", args -> {
CompoundTag tag = (CompoundTag) args[0];
Tag obj = tag.get((String) args[1]);
return toOut(obj);
});
Bridges.addBridge("CompoundTag.set", args -> {
CompoundTag tag = (CompoundTag) args[0];
tag.put((String) args[1], toTag(args[2]));
return null;
});
Bridges.addBridge("CompoundTag.keys", args -> {
CompoundTag tag = (CompoundTag) args[0];
return tag.getKeys().toArray(new String[0]);
});
Bridges.addBridge("ListTag.get", args -> {
AbstractListTag<?> tag = (AbstractListTag<?>) args[0];
Tag obj = tag.get((int) Util.toDouble(args[1], 0));
return toOut(obj);
});
Bridges.addBridge("ListTag.set", args -> {
AbstractListTag tag = (AbstractListTag) args[0];
tag.set((int) Util.toDouble(args[1], 0), toTag(args[2]));
return null;
});
Bridges.addBridge("ListTag.size", args -> {
AbstractListTag<?> tag = (AbstractListTag<?>) args[0];
return tag.size();
});
}
}

View File

@ -97,7 +97,7 @@ export class BlockState {
/**
* @internal
*/
javaObject: JavaObject;
readonly javaObject: JavaObject;
constructor(javaObject: JavaObject) {
this.javaObject = javaObject;
@ -154,5 +154,6 @@ export class BlockRegistry implements SingleRegistry<CustomBlock> {
}
addBridge('CustomBlock.onUse', (id: string, world: JavaObject, state: JavaObject, x: number, y: number, z: number, side: keyof typeof Direction, player: JavaObject, hand: keyof typeof Hand): string => {
console.log(typeof world);
return BlockRegistry.INSTANCE.get(new Identifier(id)).onUse(new World(world), new BlockState(state), new Pos(x, y, z), Direction[side], new PlayerEntity(player), Hand[hand]);
});

View File

@ -1,6 +1,7 @@
import { ItemStack } from './item';
import { useBridge, Hand, Identifier, Pos } from './core';
import { World } from './world';
import { CompoundTag } from './tag';
/**
* Damage Source
@ -9,7 +10,7 @@ export class DamageSource {
/**
* @internal
*/
javaObject: JavaObject;
readonly javaObject: JavaObject;
/**
* @internal
@ -65,7 +66,7 @@ export class Entity {
/**
* @internal
*/
javaObject: JavaObject;
readonly javaObject: JavaObject;
/**
* @internal
@ -197,6 +198,27 @@ export class Entity {
setPosition(pos: Pos) {
useBridge('Entity.setPosition', this.javaObject, pos.getX(), pos.getY(), pos.getZ());
}
/**
* Save Data To {@link CompoundTag}
* @returns Tag
*/
toTag(): CompoundTag {
const obj = useBridge('Entity.toTag', this.javaObject) as JavaObject;
if (obj) {
return new CompoundTag(obj);
} else {
return null;
}
}
/**
* Load Data From {@link CompoundTag}
* @param tag Tag
*/
fromTag(tag: CompoundTag) {
useBridge('Entity.fromTag', this.javaObject, tag.javaObject);
}
}
/**

View File

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

View File

@ -1,6 +1,7 @@
import { useBridge, Identifier, Hand, ActionResult, addBridge, Pos, Direction, SingleRegistry } from './core';
import { World } from './world';
import { PlayerEntity, LivingEntity } from './entity';
import { CompoundTag } from './tag';
/**
* Item Stack
@ -9,7 +10,7 @@ export class ItemStack {
/**
* @internal
*/
javaObject: JavaObject;
readonly javaObject: JavaObject;
/**
* @internal
@ -52,6 +53,27 @@ export class ItemStack {
getCount(): number {
return useBridge('ItemStack.getCount', this.javaObject) as number;
}
/**
* Get Item Tag
* @returns Item Tag
*/
getTag(): CompoundTag {
const obj = useBridge('ItemStack.getTag', this.javaObject) as JavaObject;
if (obj) {
return new CompoundTag(obj);
} else {
return null;
}
}
/**
* Set Item Tag
* @param tag New Item Tag
*/
setTag(tag: CompoundTag) {
useBridge('ItemStack.setTag', this.javaObject, tag.javaObject);
}
}
/**

View File

@ -0,0 +1,106 @@
import { useBridge } from "./core";
type TagType = number | boolean | string | CompoundTag | ListTag;
/**
* @internal
*/
function getTagValue(obj: [boolean, BridgeValueType]): TagType {
if (typeof obj[1] === 'object') {
if (obj[0]) {
return new ListTag(obj[1] as JavaObject);
} else {
return new CompoundTag(obj[1] as JavaObject);
}
} else {
return obj[1];
}
}
/**
* Compound Tag
*/
export class CompoundTag {
/**
* @internal
*/
readonly javaObject: JavaObject;
/**
* @internal
*/
constructor(javaObject: JavaObject) {
this.javaObject = javaObject;
}
/**
* Get Value In Tag
* @param key Key
* @returns Value
*/
get(key: string): TagType {
const obj = useBridge('CompoundTag.get', this.javaObject, key) as [boolean, BridgeValueType];
return getTagValue(obj);
}
/**
* Set Value In Tag
* @param key Key
* @param value Value
*/
set(key: string, value: TagType) {
useBridge('CompoundTag.set', this.javaObject, key, (value instanceof CompoundTag || value instanceof ListTag) ? value.javaObject : value);
}
/**
* Get All Keys
* @returns List Of Keys
*/
keys(): string[] {
return useBridge('CompoundTag.keys', this.javaObject) as string[];
}
}
/**
* List Tag
*/
export class ListTag {
/**
* @internal
*/
readonly javaObject: JavaObject;
/**
* @internal
*/
constructor(javaObject: JavaObject) {
this.javaObject = javaObject;
}
/**
* Get Value In Tag
* @param key Key
* @returns Value
*/
get(key: number): TagType {
const obj = useBridge('ListTag.get', this.javaObject, key) as [boolean, BridgeValueType];
return getTagValue(obj);
}
/**
* Set Value In Tag
* @param key Key
* @param value Value
*/
set(key: number, value: TagType) {
useBridge('ListTag.set', this.javaObject, key, (value instanceof CompoundTag || value instanceof ListTag) ? value.javaObject : value);
}
/**
* Get Size Of Tag
* @returns Size
*/
size(): number {
return useBridge('ListTag.size', this.javaObject) as number;
}
}

View File

@ -9,7 +9,7 @@ export class World {
/**
* @internal
*/
javaObject: JavaObject;
readonly javaObject: JavaObject;
/**
* @internal

View File

@ -33,7 +33,8 @@ class MyItem extends CustomItem {
}
onUseOnEntity(player: PlayerEntity, target: LivingEntity, hand: Hand): ActionResult {
console.log('Item Use: Entity ' + target.getPosition());
console.log('Item Use: Entity ' + target.toTag().keys());
console.log('Health: ' + target.toTag().get('Health').toString());
return ActionResult.SUCCESS;
}
}