From 4ba0460a161b61ac76eed893cf8f94b8e078c320 Mon Sep 17 00:00:00 2001 From: luisc Date: Wed, 15 Sep 2021 13:24:46 -0500 Subject: [PATCH] Added NBT tag support to ScriptEntity, ScriptItemStack, and ScriptWorld --- .../npcs/scripted/CustomNPCsException.java | 11 + .../noppes/npcs/scripted/ScriptEntity.java | 10 + .../noppes/npcs/scripted/ScriptItemStack.java | 20 ++ .../java/noppes/npcs/scripted/ScriptNBT.java | 200 ++++++++++++++++++ .../noppes/npcs/scripted/ScriptWorld.java | 18 ++ 5 files changed, 259 insertions(+) create mode 100644 src/main/java/noppes/npcs/scripted/CustomNPCsException.java create mode 100644 src/main/java/noppes/npcs/scripted/ScriptNBT.java diff --git a/src/main/java/noppes/npcs/scripted/CustomNPCsException.java b/src/main/java/noppes/npcs/scripted/CustomNPCsException.java new file mode 100644 index 000000000..deeb5994e --- /dev/null +++ b/src/main/java/noppes/npcs/scripted/CustomNPCsException.java @@ -0,0 +1,11 @@ +package noppes.npcs.scripted; + +public class CustomNPCsException extends RuntimeException { + public CustomNPCsException(String message, Object... obs) { + super(String.format(message, obs)); + } + + public CustomNPCsException(Exception ex, String message, Object... obs) { + super(String.format(message, obs), ex); + } +} diff --git a/src/main/java/noppes/npcs/scripted/ScriptEntity.java b/src/main/java/noppes/npcs/scripted/ScriptEntity.java index 0d24236d9..2deeb9d8e 100644 --- a/src/main/java/noppes/npcs/scripted/ScriptEntity.java +++ b/src/main/java/noppes/npcs/scripted/ScriptEntity.java @@ -486,4 +486,14 @@ public class ScriptEntity { public Entity getMCEntity(){ return entity; } + + public ScriptNBT getNBT(){ + NBTTagCompound compound = new NBTTagCompound(); + entity.writeToNBT(compound); + return new ScriptNBT(compound); + } + + public ScriptNBT getEntityNBT(){return new ScriptNBT(this.entity.getEntityData());} + + public void setEntityNBT(ScriptNBT nbt) {this.entity.readFromNBT(nbt.getMCNBT());} } diff --git a/src/main/java/noppes/npcs/scripted/ScriptItemStack.java b/src/main/java/noppes/npcs/scripted/ScriptItemStack.java index 5be275499..69762aba9 100644 --- a/src/main/java/noppes/npcs/scripted/ScriptItemStack.java +++ b/src/main/java/noppes/npcs/scripted/ScriptItemStack.java @@ -206,4 +206,24 @@ public class ScriptItemStack { public ItemStack getMCItemStack() { return item; } + + public ScriptNBT getNBT() { + NBTTagCompound compound = this.item.getTagCompound(); + if(compound == null) { + this.item.readFromNBT(compound = new NBTTagCompound()); + } + + return new ScriptNBT(compound); + } + + public boolean hasNBT() { + NBTTagCompound compound = this.item.getTagCompound(); + return compound != null && !compound.hasNoTags(); + } + + public ScriptNBT getItemNBT() { + NBTTagCompound compound = new NBTTagCompound(); + this.item.writeToNBT(compound); + return new ScriptNBT(compound); + } } diff --git a/src/main/java/noppes/npcs/scripted/ScriptNBT.java b/src/main/java/noppes/npcs/scripted/ScriptNBT.java new file mode 100644 index 000000000..b2abe4d2b --- /dev/null +++ b/src/main/java/noppes/npcs/scripted/ScriptNBT.java @@ -0,0 +1,200 @@ +package noppes.npcs.scripted; + +import net.minecraft.nbt.*; +import noppes.npcs.util.NBTJsonUtil; + +import java.util.Iterator; + +public class ScriptNBT { + private NBTTagCompound compound; + + public ScriptNBT(NBTTagCompound compound) { + this.compound = compound; + } + + public void remove(String key) { + this.compound.removeTag(key); + } + + public boolean has(String key) { + return this.compound.hasKey(key); + } + + public boolean getBoolean(String key) { + return this.compound.getBoolean(key); + } + + public void setBoolean(String key, boolean value) { + this.compound.setBoolean(key, value); + } + + public short getShort(String key) { + return this.compound.getShort(key); + } + + public void setShort(String key, short value) { + this.compound.setShort(key, value); + } + + public int getInteger(String key) { + return this.compound.getInteger(key); + } + + public void setInteger(String key, int value) { + this.compound.setInteger(key, value); + } + + public byte getByte(String key) { + return this.compound.getByte(key); + } + + public void setByte(String key, byte value) { + this.compound.setByte(key, value); + } + + public long getLong(String key) { + return this.compound.getLong(key); + } + + public void setLong(String key, long value) { + this.compound.setLong(key, value); + } + + public double getDouble(String key) { + return this.compound.getDouble(key); + } + + public void setDouble(String key, double value) { + this.compound.setDouble(key, value); + } + + public float getFloat(String key) { + return this.compound.getFloat(key); + } + + public void setFloat(String key, float value) { + this.compound.setFloat(key, value); + } + + public String getString(String key) { + return this.compound.getString(key); + } + + public void setString(String key, String value) { + this.compound.setString(key, value); + } + + public byte[] getByteArray(String key) { + return this.compound.getByteArray(key); + } + + public void setByteArray(String key, byte[] value) { + this.compound.setByteArray(key, value); + } + + public int[] getIntegerArray(String key) { + return this.compound.getIntArray(key); + } + + public void setIntegerArray(String key, int[] value) { + this.compound.setIntArray(key, value); + } + + public Object[] getList(String key, int type) { + NBTTagList list = this.compound.getTagList(key, type); + Object[] nbts = new Object[list.func_150303_d()]; + + for(int i = 0; i < list.func_150303_d(); ++i) { + if(list.func_150303_d() == 10) { + nbts[i] = new ScriptNBT(list.getCompoundTagAt(i)); + } else if(list.func_150303_d() == 8) { + nbts[i] = list.getStringTagAt(i); + } else if(list.func_150303_d() == 6) { + nbts[i] = Double.valueOf(list.func_150309_d(i)); + } else if(list.func_150303_d() == 5) { + nbts[i] = Float.valueOf(list.func_150308_e(i)); + } else if(list.func_150303_d() == 3) { + nbts[i] = Integer.valueOf(list.getStringTagAt(i)); + } else if(list.func_150303_d() == 11) { + nbts[i] = list.func_150306_c(i); + } + } + + return nbts; + } + + public int getListType(String key) { + NBTBase b = this.compound.getTag(key); + if(b == null) { + return 0; + } else if(b.getId() != 9) { + throw new CustomNPCsException("NBT tag " + key + " isn\'t a list", new Object[0]); + } else { + return ((NBTTagList)b).func_150303_d(); + } + } + + public void setList(String key, Object[] value) { + NBTTagList list = new NBTTagList(); + Object[] var4 = value; + int var5 = value.length; + + for(int var6 = 0; var6 < var5; ++var6) { + Object nbt = var4[var6]; + if(nbt instanceof ScriptNBT) { + list.appendTag(((ScriptNBT)nbt).getMCNBT()); + } else if(nbt instanceof String) { + list.appendTag(new NBTTagString((String) nbt)); + } else if(nbt instanceof Double) { + list.appendTag(new NBTTagDouble(((Double) nbt).doubleValue())); + } else if(nbt instanceof Float) { + list.appendTag(new NBTTagFloat(((Float) nbt).floatValue())); + } else if(nbt instanceof Integer) { + list.appendTag(new NBTTagInt(((Integer) nbt).intValue())); + } else if(nbt instanceof int[]) { + list.appendTag(new NBTTagIntArray((int[]) ((int[]) nbt))); + } + } + + this.compound.setTag(key, list); + } + + public ScriptNBT getCompound(String key) { + return new ScriptNBT(this.compound.getCompoundTag(key)); + } + + public void setCompound(String key, ScriptNBT value) { + if(value == null) + throw new CustomNPCsException("Value cant be null", new Object[0]); + else this.compound.setTag(key, value.getMCNBT()); + } + + public String[] getKeys() { + return (String[])this.compound.func_150296_c().toArray(new String[this.compound.func_150296_c().size()]); + } + + public int getType(String key) { + return this.compound.func_150299_b(key); + } + + public NBTTagCompound getMCNBT() { + return this.compound; + } + + public String toJsonString() { + return NBTJsonUtil.Convert(this.compound); + } + + public boolean isEqual(ScriptNBT nbt) { + return nbt == null?false:this.compound.equals(nbt.getMCNBT()); + } + + public void clear() { + Iterator var1 = this.compound.func_150296_c().iterator(); + + while(var1.hasNext()) { + String name = (String)var1.next(); + this.compound.removeTag(name); + } + } +} diff --git a/src/main/java/noppes/npcs/scripted/ScriptWorld.java b/src/main/java/noppes/npcs/scripted/ScriptWorld.java index db047e28b..17df0f740 100644 --- a/src/main/java/noppes/npcs/scripted/ScriptWorld.java +++ b/src/main/java/noppes/npcs/scripted/ScriptWorld.java @@ -6,6 +6,7 @@ import java.util.Map; import net.minecraft.block.Block; import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityList; import net.minecraft.entity.effect.EntityLightningBolt; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.Blocks; @@ -350,4 +351,21 @@ public class ScriptWorld{ public World getMCWorld(){ return world; } + + public ScriptEntity createEntityFromNBT(ScriptNBT nbt) { + Entity entity = EntityList.createEntityFromNBT(nbt.getMCNBT(), this.world); + if(entity == null) { + throw new CustomNPCsException("Failed to create an entity from given NBT", new Object[0]); + } + return new ScriptEntity(entity); + } + + public ScriptItemStack createItemFromNBT(ScriptNBT nbt) { + ItemStack item = ItemStack.loadItemStackFromNBT(nbt.getMCNBT()); + if(item.stackSize < 1) { + throw new CustomNPCsException("Failed to create an item from given NBT", new Object[0]); + } else { + return new ScriptItemStack(item); + } + } }