From 4ba0460a161b61ac76eed893cf8f94b8e078c320 Mon Sep 17 00:00:00 2001 From: luisc Date: Wed, 15 Sep 2021 13:24:46 -0500 Subject: [PATCH 1/3] 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); + } + } } From a5f9ec103f7d9d0add74535cda15c4cd4a655862 Mon Sep 17 00:00:00 2001 From: luisc Date: Wed, 15 Sep 2021 14:06:20 -0500 Subject: [PATCH 2/3] GiveItem and RemoveItem methods now update the player's server-side inventory to client changes --- .../noppes/npcs/scripted/ScriptPlayer.java | 84 +++++++++++++------ 1 file changed, 60 insertions(+), 24 deletions(-) diff --git a/src/main/java/noppes/npcs/scripted/ScriptPlayer.java b/src/main/java/noppes/npcs/scripted/ScriptPlayer.java index 2b98f0c7d..62e0fd70a 100644 --- a/src/main/java/noppes/npcs/scripted/ScriptPlayer.java +++ b/src/main/java/noppes/npcs/scripted/ScriptPlayer.java @@ -1,5 +1,6 @@ package noppes.npcs.scripted; +import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; @@ -8,6 +9,7 @@ import net.minecraft.stats.StatBase; import net.minecraft.stats.StatList; import net.minecraft.util.ChatComponentTranslation; import net.minecraft.util.ChunkCoordinates; +import net.minecraft.util.ResourceLocation; import net.minecraft.world.WorldSettings; import noppes.npcs.CustomNpcsPermissions; import noppes.npcs.NoppesStringUtils; @@ -180,6 +182,15 @@ public class ScriptPlayer extends ScriptLivingBase{ } return i; } + + public int inventoryItemCount(String id, int damage) { + Item item = (Item)Item.itemRegistry.getObject(id); + if(item == null) { + throw new CustomNPCsException("Unknown item id: " + id, new Object[0]); + } else { + return this.inventoryItemCount(new ScriptItemStack(new ItemStack(item, 1, damage))); + } + } /** * @since 1.7.10d @@ -202,26 +213,32 @@ public class ScriptPlayer extends ScriptLivingBase{ */ public boolean removeItem(ScriptItemStack item, int amount){ int count = inventoryItemCount(item); - if(amount > count) + if(amount > count) { return false; - else if(count == amount) - removeAllItems(item); - else{ - for(int i = 0; i < player.inventory.mainInventory.length; i++){ - ItemStack is = player.inventory.mainInventory[i]; - if (is != null && is.isItemEqual(item.item)){ - if(amount > is.stackSize){ - player.inventory.mainInventory[i] = null; - amount -= is.stackSize; - } - else{ - is.splitStack(amount); - break; - } - } + } else { + if (count == amount) { + this.removeAllItems(item); + } else { + for(int i = 0; i < ((EntityPlayerMP)this.entity).inventory.getSizeInventory(); ++i) { + ItemStack is = ((EntityPlayerMP)this.entity).inventory.getStackInSlot(i); + if(is != null && this.isItemEqual(item.getMCItemStack(), is)) { + if(amount < is.stackSize) { + is.splitStack(amount); + break; + } + + player.inventory.setInventorySlotContents(i, null); + amount -= is.stackSize; + } + } } + this.updatePlayerInventory(); + return true; } - return true; + } + + private boolean isItemEqual(ItemStack stack, ItemStack other) { + return other.stackSize<1?false:(stack.getItem() != other.getItem()?false:(stack.getItemDamageForDisplay() < 0?true:stack.getItemDamageForDisplay() == other.getItemDamageForDisplay())); } /** @@ -233,8 +250,9 @@ public class ScriptPlayer extends ScriptLivingBase{ */ public boolean removeItem(String id, int damage, int amount){ Item item = (Item)Item.itemRegistry.getObject(id); - if(item == null) - return false; + if(item == null) { + throw new CustomNPCsException("Unknown item id: " + id, new Object[0]); + } return removeItem(new ScriptItemStack(new ItemStack(item, 1, damage)), amount); } @@ -245,9 +263,14 @@ public class ScriptPlayer extends ScriptLivingBase{ * @return Returns whether or not it gave the item succesfully */ public boolean giveItem(ScriptItemStack item, int amount){ - if(item != null && item.getMCItemStack() != null) { + if(item != null && item.getMCItemStack() != null && amount > 0) { item.setStackSize(amount); - return this.player.inventory.addItemStackToInventory(item.getMCItemStack()); + ItemStack mcItemStack = item.getMCItemStack(); + boolean bool = player.inventory.addItemStackToInventory(mcItemStack); + if(bool) { + updatePlayerInventory(); + } + return bool; } else { return false; } @@ -262,9 +285,17 @@ public class ScriptPlayer extends ScriptLivingBase{ */ public boolean giveItem(String id, int damage, int amount){ Item item = (Item)Item.itemRegistry.getObject(id); - if(item == null) - return false; - return player.inventory.addItemStackToInventory(new ItemStack(item, amount, damage)); + if(item == null) { + throw new CustomNPCsException("Unknown item id: " + id, new Object[0]); + } + + ItemStack itemStack = new ItemStack(item, amount, damage); + boolean bool = player.inventory.addItemStackToInventory(itemStack); + if(bool) { + updatePlayerInventory(); + } + + return bool; } /** @@ -293,6 +324,7 @@ public class ScriptPlayer extends ScriptLivingBase{ if (is != null && is.isItemEqual(item.item)) player.inventory.mainInventory[i] = null; } + updatePlayerInventory(); } @@ -342,4 +374,8 @@ public class ScriptPlayer extends ScriptLivingBase{ return null; return new ScriptPixelmonPlayerData(player); } + + public void updatePlayerInventory() { + ((EntityPlayerMP)this.entity).inventoryContainer.detectAndSendChanges(); + } } From 58a1ccc3e79901eb989cf176c2aaeb66fd45dc9b Mon Sep 17 00:00:00 2001 From: luisc Date: Thu, 23 Sep 2021 03:02:58 -0500 Subject: [PATCH 3/3] Added new knockback function with x y and z knockback power parameters. --- .../java/noppes/npcs/scripted/ScriptEntity.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main/java/noppes/npcs/scripted/ScriptEntity.java b/src/main/java/noppes/npcs/scripted/ScriptEntity.java index 2deeb9d8e..ccb7203cc 100644 --- a/src/main/java/noppes/npcs/scripted/ScriptEntity.java +++ b/src/main/java/noppes/npcs/scripted/ScriptEntity.java @@ -461,6 +461,20 @@ public class ScriptEntity { entity.motionZ *= 0.6D; entity.attackEntityFrom(DamageSource.outOfWorld, 0.0001F); } + + /** + * @param xpower How strong the knockback is on the x axis + * @param ypower How strong the knockback is on the y axis + * @param zpower How strong the knockback is on the z axis + * @param direction The direction in which he flies back (0-360). Usually based on getRotation() + */ + public void knockback(int xpower, int ypower, int zpower, float direction){ + float v = direction * (float)Math.PI / 180.0F; + entity.addVelocity(-MathHelper.sin(v) * (float)xpower, ypower, MathHelper.cos(v) * (float)zpower); + entity.motionX *= 0.6D; + entity.motionZ *= 0.6D; + entity.attackEntityFrom(DamageSource.outOfWorld, 0.0001F); + } /** * @since 1.7.10c