From d510fe8a00808589b42a9e0497ced8ae865077ac Mon Sep 17 00:00:00 2001 From: luisc Date: Thu, 13 Feb 2025 13:48:59 -0600 Subject: [PATCH 01/13] Created abstract ScriptCustomizableItem class for scripted items --- .../npcs/scripted/item/ScriptCustomItem.java | 257 +++--------------- .../scripted/item/ScriptCustomizableItem.java | 249 +++++++++++++++++ 2 files changed, 285 insertions(+), 221 deletions(-) create mode 100644 src/main/java/noppes/npcs/scripted/item/ScriptCustomizableItem.java diff --git a/src/main/java/noppes/npcs/scripted/item/ScriptCustomItem.java b/src/main/java/noppes/npcs/scripted/item/ScriptCustomItem.java index 4559dd758..437114c90 100644 --- a/src/main/java/noppes/npcs/scripted/item/ScriptCustomItem.java +++ b/src/main/java/noppes/npcs/scripted/item/ScriptCustomItem.java @@ -14,7 +14,7 @@ import noppes.npcs.scripted.CustomNPCsException; import java.util.*; -public class ScriptCustomItem extends ScriptItemStack implements IItemCustom, INpcScriptHandler { +public class ScriptCustomItem extends ScriptCustomizableItem implements IItemCustom, INpcScriptHandler { public List scripts = new ArrayList(); public List errored = new ArrayList(); public String scriptLanguage = "ECMAScript"; @@ -33,8 +33,6 @@ public class ScriptCustomItem extends ScriptItemStack implements IItemCustom, IN public int armorType = -2; //-2: Fits in no armor slot, -1: Fits in all slots, 0 - 4: Fits in Head -> Boots slot respectively public int enchantability; - public Display itemDisplay = new Display(); - public long lastInited = -1; public ScriptCustomItem(ItemStack item) { @@ -140,17 +138,6 @@ public class ScriptCustomItem extends ScriptItemStack implements IItemCustom, IN public void clearConsole() { } - public String getTexture() { - return this.itemDisplay.texture == null ? "" : this.itemDisplay.texture; - } - - public void setTexture(String texture){ - if(texture == null) - texture = ""; - this.itemDisplay.texture = texture; - saveItemData(); - } - public int getMaxStackSize() { return this.stackSize; } @@ -210,33 +197,6 @@ public class ScriptCustomItem extends ScriptItemStack implements IItemCustom, IN saveItemData(); } - public boolean getDurabilityShow() { - return this.itemDisplay.durabilityShow; - } - - public void setDurabilityShow(boolean bo) { - this.itemDisplay.durabilityShow = bo; - saveItemData(); - } - - public int getDurabilityColor() { - return this.itemDisplay.durabilityColor; - } - - public void setDurabilityColor(int color) { - this.itemDisplay.durabilityColor = color; - saveItemData(); - } - - public int getColor() { - return this.itemDisplay.itemColor; - } - - public void setColor(int color) { - this.itemDisplay.itemColor = color; - saveItemData(); - } - public int getMaxItemUseDuration(){ return this.maxItemUseDuration; } @@ -264,165 +224,6 @@ public class ScriptCustomItem extends ScriptItemStack implements IItemCustom, IN saveItemData(); } - public void setRotation(float rotationX, float rotationY, float rotationZ){ - this.itemDisplay.rotationX = rotationX; - this.itemDisplay.rotationY = rotationY; - this.itemDisplay.rotationZ = rotationZ; - saveItemData(); - } - - public void setRotationRate(float rotationXRate, float rotationYRate, float rotationZRate){ - this.itemDisplay.rotationXRate = rotationXRate; - this.itemDisplay.rotationYRate = rotationYRate; - this.itemDisplay.rotationZRate = rotationZRate; - saveItemData(); - } - - public void setScale(float scaleX, float scaleY, float scaleZ){ - this.itemDisplay.scaleX = scaleX; - this.itemDisplay.scaleY = scaleY; - this.itemDisplay.scaleZ = scaleZ; - saveItemData(); - } - - public void setTranslate(float translateX, float translateY, float translateZ){ - this.itemDisplay.translateX = translateX; - this.itemDisplay.translateY = translateY; - this.itemDisplay.translateZ = translateZ; - saveItemData(); - } - - public float getRotationX() { - return this.itemDisplay.rotationX; - } - - public float getRotationY() { - return this.itemDisplay.rotationY; - } - - public float getRotationZ() { - return this.itemDisplay.rotationZ; - } - - public float getRotationXRate() { - return this.itemDisplay.rotationXRate; - } - - public float getRotationYRate() { - return this.itemDisplay.rotationYRate; - } - - public float getRotationZRate() { - return this.itemDisplay.rotationZRate; - } - - public float getScaleX() { - return this.itemDisplay.scaleX; - } - - public float getScaleY() { - return this.itemDisplay.scaleY; - } - - public float getScaleZ() { - return this.itemDisplay.scaleZ; - } - - public float getTranslateX() { - return this.itemDisplay.translateX; - } - - public float getTranslateY() { - return this.itemDisplay.translateY; - } - - public float getTranslateZ() { - return this.itemDisplay.translateZ; - } - - public NBTTagCompound getMCNbt() { - NBTTagCompound compound = super.getMCNbt(); - compound.setTag("ItemData", this.getItemNBT(new NBTTagCompound())); - compound.setTag("ScriptedData", this.getScriptNBT(new NBTTagCompound())); - return compound; - } - - public void setMCNbt(NBTTagCompound compound) { - setScriptNBT(compound.getCompoundTag("ScriptedData")); - setItemNBT(compound.getCompoundTag("ItemData")); - } - - public NBTTagCompound getItemNBT(NBTTagCompound compound) { - compound.setBoolean("DurabilityShow",this.itemDisplay.durabilityShow); - compound.setDouble("DurabilityValue",this.durabilityValue); - compound.setInteger("DurabilityColor",this.itemDisplay.durabilityColor ); - compound.setInteger("ItemColor",this.itemDisplay.itemColor); - compound.setInteger("MaxStackSize",this.stackSize); - compound.setString("ItemTexture",this.itemDisplay.texture); - - compound.setFloat("RotationX",this.itemDisplay.rotationX); - compound.setFloat("RotationY",this.itemDisplay.rotationY); - compound.setFloat("RotationZ",this.itemDisplay.rotationZ); - - compound.setFloat("RotationXRate",this.itemDisplay.rotationXRate); - compound.setFloat("RotationYRate",this.itemDisplay.rotationYRate); - compound.setFloat("RotationZRate",this.itemDisplay.rotationZRate); - - compound.setFloat("ScaleX",this.itemDisplay.scaleX); - compound.setFloat("ScaleY",this.itemDisplay.scaleY); - compound.setFloat("ScaleZ",this.itemDisplay.scaleZ); - - compound.setFloat("TranslateX",this.itemDisplay.translateX); - compound.setFloat("TranslateY",this.itemDisplay.translateY); - compound.setFloat("TranslateZ",this.itemDisplay.translateZ); - - compound.setBoolean("IsTool", this.isTool); - compound.setBoolean("IsNormalItem", this.isNormalItem); - compound.setInteger("DigSpeed", this.digSpeed); - compound.setInteger("ArmorType", this.armorType); - compound.setInteger("Enchantability", this.enchantability); - - compound.setInteger("MaxItemUseDuration", this.maxItemUseDuration); - compound.setInteger("ItemUseAction", this.itemUseAction); - return compound; - } - - public void setItemNBT(NBTTagCompound compound) { - this.itemDisplay.durabilityShow = compound.getBoolean("DurabilityShow"); - this.durabilityValue = compound.getDouble("DurabilityValue"); - if (compound.hasKey("DurabilityColor")) { - this.itemDisplay.durabilityColor = compound.getInteger("DurabilityColor"); - } - this.itemDisplay.itemColor = compound.getInteger("ItemColor"); - this.stackSize = compound.getInteger("MaxStackSize"); - this.itemDisplay.texture = compound.getString("ItemTexture"); - - this.itemDisplay.rotationX = compound.getFloat("RotationX"); - this.itemDisplay.rotationY = compound.getFloat("RotationY"); - this.itemDisplay.rotationZ = compound.getFloat("RotationZ"); - - this.itemDisplay.rotationXRate = compound.getFloat("RotationXRate"); - this.itemDisplay.rotationYRate = compound.getFloat("RotationYRate"); - this.itemDisplay.rotationZRate = compound.getFloat("RotationZRate"); - - this.itemDisplay.scaleX = compound.getFloat("ScaleX"); - this.itemDisplay.scaleY = compound.getFloat("ScaleY"); - this.itemDisplay.scaleZ = compound.getFloat("ScaleZ"); - - this.itemDisplay.translateX = compound.getFloat("TranslateX"); - this.itemDisplay.translateY = compound.getFloat("TranslateY"); - this.itemDisplay.translateZ = compound.getFloat("TranslateZ"); - - this.isTool = compound.getBoolean("IsTool"); - this.isNormalItem = compound.getBoolean("IsNormalItem"); - this.digSpeed = compound.getInteger("DigSpeed"); - this.armorType = compound.getInteger("ArmorType"); - this.enchantability = compound.getInteger("Enchantability"); - - this.maxItemUseDuration = compound.getInteger("MaxItemUseDuration"); - this.itemUseAction = compound.getInteger("ItemUseAction"); - } - public void saveScriptData() { NBTTagCompound c = this.item.getTagCompound(); if (c == null) { @@ -439,31 +240,45 @@ public class ScriptCustomItem extends ScriptItemStack implements IItemCustom, IN } } - public void saveItemData() { - NBTTagCompound c = this.item.getTagCompound(); - if (c == null) { - this.item.setTagCompound(c = new NBTTagCompound()); - } - - c.setTag("ItemData", this.getItemNBT(new NBTTagCompound())); + public NBTTagCompound getMCNbt() { + NBTTagCompound compound = super.getMCNbt(); + compound.setTag("ScriptedData", this.getScriptNBT(new NBTTagCompound())); + return compound; } - public void loadItemData() { - NBTTagCompound c = this.item.getTagCompound(); - if (c != null && !c.getCompoundTag("ItemData").hasNoTags()){ - this.setItemNBT(c.getCompoundTag("ItemData")); - } + public void setMCNbt(NBTTagCompound compound) { + super.setMCNbt(compound); + setScriptNBT(compound.getCompoundTag("ScriptedData")); } + public NBTTagCompound getItemNBT(NBTTagCompound compound) { + super.getItemNBT(compound); + compound.setDouble("DurabilityValue",this.durabilityValue); + compound.setInteger("MaxStackSize",this.stackSize); - public static class Display { - public String texture = "minecraft:textures/items/iron_pickaxe.png"; - public float translateX, translateY, translateZ; - public int itemColor = 0x8B4513; - public float scaleX = 1.0F, scaleY = 1.0F, scaleZ = 1.0F; - public float rotationX, rotationY, rotationZ; - public float rotationXRate, rotationYRate, rotationZRate; - public boolean durabilityShow = false; - public int durabilityColor = -1; + compound.setBoolean("IsTool", this.isTool); + compound.setBoolean("IsNormalItem", this.isNormalItem); + compound.setInteger("DigSpeed", this.digSpeed); + compound.setInteger("ArmorType", this.armorType); + compound.setInteger("Enchantability", this.enchantability); + + compound.setInteger("MaxItemUseDuration", this.maxItemUseDuration); + compound.setInteger("ItemUseAction", this.itemUseAction); + return compound; + } + + public void setItemNBT(NBTTagCompound compound) { + super.setItemNBT(compound); + this.durabilityValue = compound.getDouble("DurabilityValue"); + this.stackSize = compound.getInteger("MaxStackSize"); + + this.isTool = compound.getBoolean("IsTool"); + this.isNormalItem = compound.getBoolean("IsNormalItem"); + this.digSpeed = compound.getInteger("DigSpeed"); + this.armorType = compound.getInteger("ArmorType"); + this.enchantability = compound.getInteger("Enchantability"); + + this.maxItemUseDuration = compound.getInteger("MaxItemUseDuration"); + this.itemUseAction = compound.getInteger("ItemUseAction"); } } diff --git a/src/main/java/noppes/npcs/scripted/item/ScriptCustomizableItem.java b/src/main/java/noppes/npcs/scripted/item/ScriptCustomizableItem.java new file mode 100644 index 000000000..ddbbe67d0 --- /dev/null +++ b/src/main/java/noppes/npcs/scripted/item/ScriptCustomizableItem.java @@ -0,0 +1,249 @@ +package noppes.npcs.scripted.item; + +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; + +public abstract class ScriptCustomizableItem extends ScriptItemStack { + public final Display itemDisplay = new Display(); + + public ScriptCustomizableItem(ItemStack item) { + super(item); + } + + public abstract int getMaxStackSize(); + +// public abstract void setArmorType(int armorType); + + public abstract int getArmorType(); + +// public abstract void setIsTool(boolean isTool); + + public abstract boolean isTool(); + +// public abstract void setIsNormalItem(boolean normalItem); + + public abstract boolean isNormalItem(); + +// public abstract void setDigSpeed(int digSpeed); + + public abstract int getDigSpeed(); + +// public abstract void setMaxStackSize(int size); + + public abstract double getDurabilityValue(); + +// public abstract void setDurabilityValue(float value); + + public abstract int getMaxItemUseDuration(); + +// public abstract void setMaxItemUseDuration(int duration); + + public abstract int getItemUseAction(); + +// public abstract void setItemUseAction(int action); + + public abstract int getEnchantability(); + +// public abstract void setEnchantability(int enchantability); + + public String getTexture() { + return this.itemDisplay.texture == null ? "" : this.itemDisplay.texture; + } + + public void setTexture(String texture){ + if(texture == null) + texture = ""; + this.itemDisplay.texture = texture; + saveItemData(); + } + + public boolean getDurabilityShow() { + return this.itemDisplay.durabilityShow; + } + + public void setDurabilityShow(boolean bo) { + this.itemDisplay.durabilityShow = bo; + saveItemData(); + } + + public int getDurabilityColor() { + return this.itemDisplay.durabilityColor; + } + + public void setDurabilityColor(int color) { + this.itemDisplay.durabilityColor = color; + saveItemData(); + } + + public int getColor() { + return this.itemDisplay.itemColor; + } + + public void setColor(int color) { + this.itemDisplay.itemColor = color; + saveItemData(); + } + + + public void setRotation(float rotationX, float rotationY, float rotationZ){ + this.itemDisplay.rotationX = rotationX; + this.itemDisplay.rotationY = rotationY; + this.itemDisplay.rotationZ = rotationZ; + saveItemData(); + } + + public void setRotationRate(float rotationXRate, float rotationYRate, float rotationZRate){ + this.itemDisplay.rotationXRate = rotationXRate; + this.itemDisplay.rotationYRate = rotationYRate; + this.itemDisplay.rotationZRate = rotationZRate; + saveItemData(); + } + + public void setScale(float scaleX, float scaleY, float scaleZ){ + this.itemDisplay.scaleX = scaleX; + this.itemDisplay.scaleY = scaleY; + this.itemDisplay.scaleZ = scaleZ; + saveItemData(); + } + + public void setTranslate(float translateX, float translateY, float translateZ){ + this.itemDisplay.translateX = translateX; + this.itemDisplay.translateY = translateY; + this.itemDisplay.translateZ = translateZ; + saveItemData(); + } + + public float getRotationX() { + return this.itemDisplay.rotationX; + } + + public float getRotationY() { + return this.itemDisplay.rotationY; + } + + public float getRotationZ() { + return this.itemDisplay.rotationZ; + } + + public float getRotationXRate() { + return this.itemDisplay.rotationXRate; + } + + public float getRotationYRate() { + return this.itemDisplay.rotationYRate; + } + + public float getRotationZRate() { + return this.itemDisplay.rotationZRate; + } + + public float getScaleX() { + return this.itemDisplay.scaleX; + } + + public float getScaleY() { + return this.itemDisplay.scaleY; + } + + public float getScaleZ() { + return this.itemDisplay.scaleZ; + } + + public float getTranslateX() { + return this.itemDisplay.translateX; + } + + public float getTranslateY() { + return this.itemDisplay.translateY; + } + + public float getTranslateZ() { + return this.itemDisplay.translateZ; + } + + public NBTTagCompound getMCNbt() { + NBTTagCompound compound = super.getMCNbt(); + compound.setTag("ItemData", this.getItemNBT(new NBTTagCompound())); + return compound; + } + + public void setMCNbt(NBTTagCompound compound) { + super.setMCNbt(compound); + setItemNBT(compound.getCompoundTag("ItemData")); + } + + public void saveItemData() { + NBTTagCompound c = this.item.getTagCompound(); + if (c == null) { + this.item.setTagCompound(c = new NBTTagCompound()); + } + c.setTag("ItemData", this.getItemNBT(new NBTTagCompound())); + } + + public void loadItemData() { + NBTTagCompound c = this.item.getTagCompound(); + if (c != null && !c.getCompoundTag("ItemData").hasNoTags()){ + this.setItemNBT(c.getCompoundTag("ItemData")); + } + } + + public NBTTagCompound getItemNBT(NBTTagCompound compound) { + compound.setBoolean("DurabilityShow",this.itemDisplay.durabilityShow); + compound.setInteger("DurabilityColor",this.itemDisplay.durabilityColor ); + compound.setInteger("ItemColor",this.itemDisplay.itemColor); + compound.setString("ItemTexture",this.itemDisplay.texture); + + compound.setFloat("RotationX",this.itemDisplay.rotationX); + compound.setFloat("RotationY",this.itemDisplay.rotationY); + compound.setFloat("RotationZ",this.itemDisplay.rotationZ); + + compound.setFloat("RotationXRate",this.itemDisplay.rotationXRate); + compound.setFloat("RotationYRate",this.itemDisplay.rotationYRate); + compound.setFloat("RotationZRate",this.itemDisplay.rotationZRate); + + compound.setFloat("ScaleX",this.itemDisplay.scaleX); + compound.setFloat("ScaleY",this.itemDisplay.scaleY); + compound.setFloat("ScaleZ",this.itemDisplay.scaleZ); + + compound.setFloat("TranslateX",this.itemDisplay.translateX); + compound.setFloat("TranslateY",this.itemDisplay.translateY); + compound.setFloat("TranslateZ",this.itemDisplay.translateZ); + return compound; + } + + public void setItemNBT(NBTTagCompound compound) { + this.itemDisplay.durabilityShow = compound.getBoolean("DurabilityShow"); + if (compound.hasKey("DurabilityColor")) { + this.itemDisplay.durabilityColor = compound.getInteger("DurabilityColor"); + } + this.itemDisplay.itemColor = compound.getInteger("ItemColor"); + this.itemDisplay.texture = compound.getString("ItemTexture"); + + this.itemDisplay.rotationX = compound.getFloat("RotationX"); + this.itemDisplay.rotationY = compound.getFloat("RotationY"); + this.itemDisplay.rotationZ = compound.getFloat("RotationZ"); + + this.itemDisplay.rotationXRate = compound.getFloat("RotationXRate"); + this.itemDisplay.rotationYRate = compound.getFloat("RotationYRate"); + this.itemDisplay.rotationZRate = compound.getFloat("RotationZRate"); + + this.itemDisplay.scaleX = compound.getFloat("ScaleX"); + this.itemDisplay.scaleY = compound.getFloat("ScaleY"); + this.itemDisplay.scaleZ = compound.getFloat("ScaleZ"); + + this.itemDisplay.translateX = compound.getFloat("TranslateX"); + this.itemDisplay.translateY = compound.getFloat("TranslateY"); + this.itemDisplay.translateZ = compound.getFloat("TranslateZ"); + } + + public static class Display { + public String texture = "minecraft:textures/items/iron_pickaxe.png"; + public float translateX, translateY, translateZ; + public int itemColor = 0x8B4513; + public float scaleX = 1.0F, scaleY = 1.0F, scaleZ = 1.0F; + public float rotationX, rotationY, rotationZ; + public float rotationXRate, rotationYRate, rotationZRate; + public boolean durabilityShow = false; + public int durabilityColor = -1; + } +} From 82dfbc8635219c1c986a28d780af6e67e41df152 Mon Sep 17 00:00:00 2001 From: luisc Date: Thu, 13 Feb 2025 14:12:27 -0600 Subject: [PATCH 02/13] Made display fields nullable --- .../npcs/scripted/item/ScriptCustomizableItem.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/noppes/npcs/scripted/item/ScriptCustomizableItem.java b/src/main/java/noppes/npcs/scripted/item/ScriptCustomizableItem.java index ddbbe67d0..cff5da481 100644 --- a/src/main/java/noppes/npcs/scripted/item/ScriptCustomizableItem.java +++ b/src/main/java/noppes/npcs/scripted/item/ScriptCustomizableItem.java @@ -238,12 +238,12 @@ public abstract class ScriptCustomizableItem extends ScriptItemStack { public static class Display { public String texture = "minecraft:textures/items/iron_pickaxe.png"; - public float translateX, translateY, translateZ; - public int itemColor = 0x8B4513; - public float scaleX = 1.0F, scaleY = 1.0F, scaleZ = 1.0F; - public float rotationX, rotationY, rotationZ; - public float rotationXRate, rotationYRate, rotationZRate; - public boolean durabilityShow = false; - public int durabilityColor = -1; + public Float translateX = 0F, translateY = 0F, translateZ = 0F; + public Integer itemColor = 0x8B4513; + public Float scaleX = 1.0F, scaleY = 1.0F, scaleZ = 1.0F; + public Float rotationX = 0F, rotationY = 0F, rotationZ = 0F; + public Float rotationXRate = 0F, rotationYRate = 0F, rotationZRate = 0F; + public Boolean durabilityShow = false; + public Integer durabilityColor = -1; } } From fb75f5280675ea6cb80aa9e0e5a50f712817d8e9 Mon Sep 17 00:00:00 2001 From: luisc Date: Thu, 13 Feb 2025 14:48:21 -0600 Subject: [PATCH 03/13] NBT changes to accommodate nullable fields --- .../scripted/item/ScriptCustomizableItem.java | 186 +++++++++++------- 1 file changed, 118 insertions(+), 68 deletions(-) diff --git a/src/main/java/noppes/npcs/scripted/item/ScriptCustomizableItem.java b/src/main/java/noppes/npcs/scripted/item/ScriptCustomizableItem.java index cff5da481..e788e0e26 100644 --- a/src/main/java/noppes/npcs/scripted/item/ScriptCustomizableItem.java +++ b/src/main/java/noppes/npcs/scripted/item/ScriptCustomizableItem.java @@ -57,108 +57,108 @@ public abstract class ScriptCustomizableItem extends ScriptItemStack { saveItemData(); } - public boolean getDurabilityShow() { - return this.itemDisplay.durabilityShow; + public Boolean getDurabilityShow() { + return this.itemDisplay.durabilityShow != null ? this.itemDisplay.durabilityShow : false; } - public void setDurabilityShow(boolean bo) { + public void setDurabilityShow(Boolean bo) { this.itemDisplay.durabilityShow = bo; saveItemData(); } - public int getDurabilityColor() { - return this.itemDisplay.durabilityColor; + public Integer getDurabilityColor() { + return this.itemDisplay.durabilityColor != null ? this.itemDisplay.durabilityColor : -1; } - public void setDurabilityColor(int color) { + public void setDurabilityColor(Integer color) { this.itemDisplay.durabilityColor = color; saveItemData(); } - public int getColor() { - return this.itemDisplay.itemColor; + public Integer getColor() { + return this.itemDisplay.itemColor != null ? this.itemDisplay.itemColor : 0x8B4513; } - public void setColor(int color) { + public void setColor(Integer color) { this.itemDisplay.itemColor = color; saveItemData(); } - public void setRotation(float rotationX, float rotationY, float rotationZ){ + public void setRotation(Float rotationX, Float rotationY, Float rotationZ){ this.itemDisplay.rotationX = rotationX; this.itemDisplay.rotationY = rotationY; this.itemDisplay.rotationZ = rotationZ; saveItemData(); } - public void setRotationRate(float rotationXRate, float rotationYRate, float rotationZRate){ + public void setRotationRate(Float rotationXRate, Float rotationYRate, Float rotationZRate){ this.itemDisplay.rotationXRate = rotationXRate; this.itemDisplay.rotationYRate = rotationYRate; this.itemDisplay.rotationZRate = rotationZRate; saveItemData(); } - public void setScale(float scaleX, float scaleY, float scaleZ){ + public void setScale(Float scaleX, Float scaleY, Float scaleZ){ this.itemDisplay.scaleX = scaleX; this.itemDisplay.scaleY = scaleY; this.itemDisplay.scaleZ = scaleZ; saveItemData(); } - public void setTranslate(float translateX, float translateY, float translateZ){ + public void setTranslate(Float translateX, Float translateY, Float translateZ){ this.itemDisplay.translateX = translateX; this.itemDisplay.translateY = translateY; this.itemDisplay.translateZ = translateZ; saveItemData(); } - public float getRotationX() { - return this.itemDisplay.rotationX; + public Float getRotationX() { + return this.itemDisplay.rotationX != null ? this.itemDisplay.rotationX : 0; } - public float getRotationY() { - return this.itemDisplay.rotationY; + public Float getRotationY() { + return this.itemDisplay.rotationY != null ? this.itemDisplay.rotationY : 0; } - public float getRotationZ() { - return this.itemDisplay.rotationZ; + public Float getRotationZ() { + return this.itemDisplay.rotationZ != null ? this.itemDisplay.rotationZ : 0; } - public float getRotationXRate() { - return this.itemDisplay.rotationXRate; + public Float getRotationXRate() { + return this.itemDisplay.rotationXRate != null ? this.itemDisplay.rotationXRate : 0; } - public float getRotationYRate() { - return this.itemDisplay.rotationYRate; + public Float getRotationYRate() { + return this.itemDisplay.rotationYRate != null ? this.itemDisplay.rotationYRate : 0; } - public float getRotationZRate() { - return this.itemDisplay.rotationZRate; + public Float getRotationZRate() { + return this.itemDisplay.rotationZRate != null ? this.itemDisplay.rotationZRate : 0; } - public float getScaleX() { - return this.itemDisplay.scaleX; + public Float getScaleX() { + return this.itemDisplay.scaleX != null ? this.itemDisplay.scaleX : 0; } - public float getScaleY() { - return this.itemDisplay.scaleY; + public Float getScaleY() { + return this.itemDisplay.scaleY != null ? this.itemDisplay.scaleY : 0; } - public float getScaleZ() { - return this.itemDisplay.scaleZ; + public Float getScaleZ() { + return this.itemDisplay.scaleZ != null ? this.itemDisplay.scaleZ : 0; } - public float getTranslateX() { - return this.itemDisplay.translateX; + public Float getTranslateX() { + return this.itemDisplay.translateX != null ? this.itemDisplay.translateX : 0; } - public float getTranslateY() { - return this.itemDisplay.translateY; + public Float getTranslateY() { + return this.itemDisplay.translateY != null ? this.itemDisplay.translateY : 0; } - public float getTranslateZ() { - return this.itemDisplay.translateZ; + public Float getTranslateZ() { + return this.itemDisplay.translateZ != null ? this.itemDisplay.translateZ : 0; } public NBTTagCompound getMCNbt() { @@ -188,52 +188,102 @@ public abstract class ScriptCustomizableItem extends ScriptItemStack { } public NBTTagCompound getItemNBT(NBTTagCompound compound) { - compound.setBoolean("DurabilityShow",this.itemDisplay.durabilityShow); - compound.setInteger("DurabilityColor",this.itemDisplay.durabilityColor ); - compound.setInteger("ItemColor",this.itemDisplay.itemColor); - compound.setString("ItemTexture",this.itemDisplay.texture); + this.setBoolean(compound, "DurabilityShow",this.itemDisplay.durabilityShow); + this.setInteger(compound, "DurabilityColor",this.itemDisplay.durabilityColor); + this.setInteger(compound, "ItemColor",this.itemDisplay.itemColor); + if (this.itemDisplay.texture != null) { + compound.setString("ItemTexture", this.itemDisplay.texture); + } - compound.setFloat("RotationX",this.itemDisplay.rotationX); - compound.setFloat("RotationY",this.itemDisplay.rotationY); - compound.setFloat("RotationZ",this.itemDisplay.rotationZ); + this.setFloat(compound, "RotationX",this.itemDisplay.rotationX); + this.setFloat(compound, "RotationY",this.itemDisplay.rotationY); + this.setFloat(compound, "RotationZ",this.itemDisplay.rotationZ); - compound.setFloat("RotationXRate",this.itemDisplay.rotationXRate); - compound.setFloat("RotationYRate",this.itemDisplay.rotationYRate); - compound.setFloat("RotationZRate",this.itemDisplay.rotationZRate); + this.setFloat(compound, "RotationXRate",this.itemDisplay.rotationXRate); + this.setFloat(compound, "RotationYRate",this.itemDisplay.rotationYRate); + this.setFloat(compound, "RotationZRate",this.itemDisplay.rotationZRate); - compound.setFloat("ScaleX",this.itemDisplay.scaleX); - compound.setFloat("ScaleY",this.itemDisplay.scaleY); - compound.setFloat("ScaleZ",this.itemDisplay.scaleZ); + this.setFloat(compound, "ScaleX",this.itemDisplay.scaleX); + this.setFloat(compound, "ScaleY",this.itemDisplay.scaleY); + this.setFloat(compound, "ScaleZ",this.itemDisplay.scaleZ); - compound.setFloat("TranslateX",this.itemDisplay.translateX); - compound.setFloat("TranslateY",this.itemDisplay.translateY); - compound.setFloat("TranslateZ",this.itemDisplay.translateZ); + this.setFloat(compound, "TranslateX",this.itemDisplay.translateX); + this.setFloat(compound, "TranslateY",this.itemDisplay.translateY); + this.setFloat(compound, "TranslateZ",this.itemDisplay.translateZ); return compound; } + private void setBoolean(NBTTagCompound compound, String key, Boolean value) { + if (value != null) { + compound.setBoolean(key, value); + } + } + + private void setInteger(NBTTagCompound compound, String key, Integer value) { + if (value != null) { + compound.setInteger(key, value); + } + } + + private void setFloat(NBTTagCompound compound, String key, Float value) { + if (value != null) { + compound.setFloat(key, value); + } + } + public void setItemNBT(NBTTagCompound compound) { - this.itemDisplay.durabilityShow = compound.getBoolean("DurabilityShow"); + if (compound.hasKey("DurabilityShow")) { + this.itemDisplay.durabilityShow = compound.getBoolean("DurabilityShow"); + } if (compound.hasKey("DurabilityColor")) { this.itemDisplay.durabilityColor = compound.getInteger("DurabilityColor"); } - this.itemDisplay.itemColor = compound.getInteger("ItemColor"); - this.itemDisplay.texture = compound.getString("ItemTexture"); + if (compound.hasKey("ItemColor")) { + this.itemDisplay.itemColor = compound.getInteger("ItemColor"); + } + if (compound.hasKey("ItemTexture")) { + this.itemDisplay.texture = compound.getString("ItemTexture"); + } - this.itemDisplay.rotationX = compound.getFloat("RotationX"); - this.itemDisplay.rotationY = compound.getFloat("RotationY"); - this.itemDisplay.rotationZ = compound.getFloat("RotationZ"); + if (compound.hasKey("RotationX")) { + this.itemDisplay.rotationX = compound.getFloat("RotationX"); + } + if (compound.hasKey("RotationY")) { + this.itemDisplay.rotationY = compound.getFloat("RotationY"); + } + if (compound.hasKey("RotationZ")) { + this.itemDisplay.rotationZ = compound.getFloat("RotationZ"); + } - this.itemDisplay.rotationXRate = compound.getFloat("RotationXRate"); - this.itemDisplay.rotationYRate = compound.getFloat("RotationYRate"); - this.itemDisplay.rotationZRate = compound.getFloat("RotationZRate"); + if (compound.hasKey("RotationXRate")) { + this.itemDisplay.rotationXRate = compound.getFloat("RotationXRate"); + } + if (compound.hasKey("RotationYRate")) { + this.itemDisplay.rotationYRate = compound.getFloat("RotationYRate"); + } + if (compound.hasKey("RotationZRate")) { + this.itemDisplay.rotationZRate = compound.getFloat("RotationZRate"); + } - this.itemDisplay.scaleX = compound.getFloat("ScaleX"); - this.itemDisplay.scaleY = compound.getFloat("ScaleY"); - this.itemDisplay.scaleZ = compound.getFloat("ScaleZ"); + if (compound.hasKey("ScaleX")) { + this.itemDisplay.scaleX = compound.getFloat("ScaleX"); + } + if (compound.hasKey("ScaleY")) { + this.itemDisplay.scaleY = compound.getFloat("ScaleY"); + } + if (compound.hasKey("ScaleZ")) { + this.itemDisplay.scaleZ = compound.getFloat("ScaleZ"); + } - this.itemDisplay.translateX = compound.getFloat("TranslateX"); - this.itemDisplay.translateY = compound.getFloat("TranslateY"); - this.itemDisplay.translateZ = compound.getFloat("TranslateZ"); + if (compound.hasKey("TranslateX")) { + this.itemDisplay.translateX = compound.getFloat("TranslateX"); + } + if (compound.hasKey("TranslateY")) { + this.itemDisplay.translateY = compound.getFloat("TranslateY"); + } + if (compound.hasKey("TranslateZ")) { + this.itemDisplay.translateZ = compound.getFloat("TranslateZ"); + } } public static class Display { From 42de9a0568193c7efb32392b75ede44efd0fc8d7 Mon Sep 17 00:00:00 2001 From: luisc Date: Thu, 13 Feb 2025 14:48:36 -0600 Subject: [PATCH 04/13] NBT changes to accommodate nullable fields --- src/api | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api b/src/api index 168670878..5026e3043 160000 --- a/src/api +++ b/src/api @@ -1 +1 @@ -Subproject commit 16867087899cdabbaf55506727ce19130b994dee +Subproject commit 5026e30437fb0c4868a56cea747d616ce5392646 From fb87e2a72c1765e2ea5d8d4404624b5f0202e79c Mon Sep 17 00:00:00 2001 From: luisc Date: Thu, 13 Feb 2025 14:55:25 -0600 Subject: [PATCH 05/13] removed unused ItemLinked class --- .../java/noppes/npcs/items/ItemLinked.java | 158 ------------------ 1 file changed, 158 deletions(-) delete mode 100644 src/main/java/noppes/npcs/items/ItemLinked.java diff --git a/src/main/java/noppes/npcs/items/ItemLinked.java b/src/main/java/noppes/npcs/items/ItemLinked.java deleted file mode 100644 index c20d97424..000000000 --- a/src/main/java/noppes/npcs/items/ItemLinked.java +++ /dev/null @@ -1,158 +0,0 @@ -package noppes.npcs.items; - -import cpw.mods.fml.common.registry.GameRegistry; -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; -import net.minecraft.block.Block; -import net.minecraft.client.renderer.texture.IIconRegister; -import net.minecraft.entity.Entity; -import net.minecraft.entity.EntityLivingBase; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.init.Items; -import net.minecraft.item.EnumAction; -import net.minecraft.item.Item; -import net.minecraft.item.ItemStack; -import net.minecraft.util.ChatComponentTranslation; -import net.minecraft.world.World; -import noppes.npcs.CustomItems; -import noppes.npcs.CustomNpcs; -import noppes.npcs.CustomNpcsPermissions; -import noppes.npcs.EventHooks; -import noppes.npcs.api.item.IItemStack; -import noppes.npcs.config.ConfigScript; -import noppes.npcs.constants.EnumGuiType; -import noppes.npcs.scripted.NpcAPI; -import noppes.npcs.scripted.event.ItemEvent; -import noppes.npcs.scripted.item.ScriptCustomItem; -import org.lwjgl.opengl.GL11; - -public class ItemLinked extends Item implements ItemRenderInterface { - - public ItemLinked() { - maxStackSize = 1; - CustomNpcs.proxy.registerItem(this); - setHasSubtypes(true); - } - - @SideOnly(Side.CLIENT) - public void registerIcons(IIconRegister par1IconRegister){ - this.itemIcon = Items.iron_pickaxe.getIconFromDamage(0); - } - - @Override - public int getColorFromItemStack(ItemStack itemStack, int par2){ - return 0x8B4513; - } - - @Override - public boolean requiresMultipleRenderPasses(){ - return true; - } - - @Override - public Item setUnlocalizedName(String name){ - GameRegistry.registerItem(this, name); - return super.setUnlocalizedName(name); - } - - public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) - { - player.setItemInUse(stack, this.getMaxItemUseDuration(stack)); - return stack; - } - - @Override - public boolean onEntitySwing(EntityLivingBase entityLivingBase, ItemStack stack){ - if (entityLivingBase.worldObj.isRemote) { - return false; - } - - IItemStack istack = NpcAPI.Instance().getIItemStack(stack); - ItemEvent.AttackEvent eve = new ItemEvent.AttackEvent( (ScriptCustomItem) istack, NpcAPI.Instance().getIEntity(entityLivingBase), 2, null); - return EventHooks.onScriptItemAttack((ScriptCustomItem) istack, eve); - } - - public int getMaxItemUseDuration(ItemStack stack){ - return (new ScriptCustomItem(stack)).getMaxItemUseDuration(); - } - - public static ScriptCustomItem GetWrapper(ItemStack stack) { - return new ScriptCustomItem(stack); - } - - public boolean showDurabilityBar(ItemStack stack) { - IItemStack istack = NpcAPI.Instance().getIItemStack(stack); - return istack instanceof ScriptCustomItem && (new ScriptCustomItem(stack)).itemDisplay.durabilityShow; - } - - public double getDurabilityForDisplay(ItemStack stack) { - IItemStack istack = NpcAPI.Instance().getIItemStack(stack); - return istack instanceof ScriptCustomItem ? 1.0D - (new ScriptCustomItem(stack)).durabilityValue : 1.0D; - } - - public int getItemStackLimit(ItemStack stack) { - IItemStack istack = NpcAPI.Instance().getIItemStack(stack); - return istack instanceof ScriptCustomItem ? (new ScriptCustomItem(stack)).getMaxStackSize() : super.getItemStackLimit(stack); - } - - public boolean isItemTool(ItemStack stack) - { - IItemStack istack = NpcAPI.Instance().getIItemStack(stack); - return istack instanceof ScriptCustomItem ? (new ScriptCustomItem(stack)).isTool() : super.isItemTool(stack); - } - - public float getDigSpeed(ItemStack stack, Block block, int metadata) - { - IItemStack istack = NpcAPI.Instance().getIItemStack(stack); - return istack instanceof ScriptCustomItem ? (new ScriptCustomItem(stack)).getDigSpeed() : super.getDigSpeed(stack, block, metadata); - } - - public boolean isValidArmor(ItemStack stack, int armorType, Entity entity) - { - IItemStack istack = NpcAPI.Instance().getIItemStack(stack); - - if((new ScriptCustomItem(stack)).getArmorType() == -1) - return true; - - return istack instanceof ScriptCustomItem ? armorType == (new ScriptCustomItem(stack)).getArmorType() : super.isValidArmor(stack, armorType, entity); - } - - public int getItemEnchantability(ItemStack stack) - { - IItemStack istack = NpcAPI.Instance().getIItemStack(stack); - return istack instanceof ScriptCustomItem ? (new ScriptCustomItem(stack)).getEnchantability() : super.getItemEnchantability(stack); - } - - public boolean hitEntity(ItemStack stack, EntityLivingBase target, EntityLivingBase attacker) { - IItemStack istack = NpcAPI.Instance().getIItemStack(stack); - ItemEvent.AttackEvent eve = new ItemEvent.AttackEvent( (ScriptCustomItem) istack, NpcAPI.Instance().getIEntity(attacker), 1, NpcAPI.Instance().getIEntity(target)); - return EventHooks.onScriptItemAttack((ScriptCustomItem) istack, eve); - } - - public EnumAction getItemUseAction(ItemStack stack) - { - IItemStack istack = NpcAPI.Instance().getIItemStack(stack); - if (istack instanceof ScriptCustomItem) { - switch (((ScriptCustomItem) istack).getItemUseAction()) { - case 0: - return EnumAction.none; - case 1: - return EnumAction.block; - case 2: - return EnumAction.bow; - case 3: - return EnumAction.eat; - case 4: - return EnumAction.drink; - } - } - return super.getItemUseAction(stack); - } - - @Override - public void renderSpecial() {} - - public void renderOffset(ScriptCustomItem scriptCustomItem) { - GL11.glTranslatef(0.135F * scriptCustomItem.itemDisplay.scaleX, 0.2F * scriptCustomItem.itemDisplay.scaleY, 0.07F * scriptCustomItem.itemDisplay.scaleZ); - } -} From 44e5c8b4c5a0a873263f72ba1a8a2733849f89ff Mon Sep 17 00:00:00 2001 From: luisc Date: Thu, 13 Feb 2025 18:11:58 -0600 Subject: [PATCH 06/13] progress on linked items --- src/main/java/noppes/npcs/CustomItems.java | 3 + .../controllers/LinkedItemController.java | 34 ++++ .../controllers/data/ItemDisplayData.java | 120 +++++++++++++ .../npcs/controllers/data/LinkedItem.java | 73 ++++++++ .../java/noppes/npcs/items/ItemLinked.java | 160 ++++++++++++++++++ .../java/noppes/npcs/scripted/NpcAPI.java | 13 +- .../scripted/item/ScriptCustomizableItem.java | 107 +----------- .../npcs/scripted/item/ScriptLinkedItem.java | 145 ++++++++++++++++ 8 files changed, 546 insertions(+), 109 deletions(-) create mode 100644 src/main/java/noppes/npcs/controllers/LinkedItemController.java create mode 100644 src/main/java/noppes/npcs/controllers/data/ItemDisplayData.java create mode 100644 src/main/java/noppes/npcs/controllers/data/LinkedItem.java create mode 100644 src/main/java/noppes/npcs/items/ItemLinked.java create mode 100644 src/main/java/noppes/npcs/scripted/item/ScriptLinkedItem.java diff --git a/src/main/java/noppes/npcs/CustomItems.java b/src/main/java/noppes/npcs/CustomItems.java index 8763f7b93..d4289300e 100644 --- a/src/main/java/noppes/npcs/CustomItems.java +++ b/src/main/java/noppes/npcs/CustomItems.java @@ -28,7 +28,9 @@ public class CustomItems { public static Item mount; public static Item teleporter; public static Item tool; + public static Item scripted_item; + public static Item linked_item; public static Item soulstoneEmpty; public static Item soulstoneFull; @@ -245,6 +247,7 @@ public class CustomItems { mount = new ItemMounter().setUnlocalizedName("npcMounter").setFull3D(); teleporter = new ItemTeleporter().setUnlocalizedName("npcTeleporter").setFull3D(); scripted_item = new ItemScripted().setUnlocalizedName("scripted_item").setFull3D(); + linked_item = new ItemLinked().setUnlocalizedName("linked_item").setFull3D(); tool = new ItemNpcTool().setUnlocalizedName("npcTool").setFull3D(); redstoneBlock = new BlockNpcRedstone().setHardness(50.0F).setResistance(2000).setBlockName("npcRedstoneBlock").setBlockTextureName("customnpcs:npcRedstoneBlock").setCreativeTab(tab); diff --git a/src/main/java/noppes/npcs/controllers/LinkedItemController.java b/src/main/java/noppes/npcs/controllers/LinkedItemController.java new file mode 100644 index 000000000..4c721ce7e --- /dev/null +++ b/src/main/java/noppes/npcs/controllers/LinkedItemController.java @@ -0,0 +1,34 @@ +package noppes.npcs.controllers; + +import noppes.npcs.controllers.data.LinkedItem; + +import java.util.HashMap; + +//TODO: load this data from somewhere, use in GUIs, create script wrapper +public class LinkedItemController { + private static LinkedItemController Instance; + private final HashMap linkedItems = new HashMap<>(); + + private LinkedItemController() { + } + + public static LinkedItemController Instance() { + if (Instance == null) { + Instance = new LinkedItemController(); + } + return Instance; + } + + public void add(LinkedItem linkedItem) { + if (linkedItem != null) { + String name = linkedItem.getName(); + if (name != null && !name.isEmpty()) { + this.linkedItems.put(name.toLowerCase(), linkedItem); + } + } + } + + public LinkedItem get(String name) { + return name != null && !name.isEmpty() ? this.linkedItems.get(name.toLowerCase()) : null; + } +} diff --git a/src/main/java/noppes/npcs/controllers/data/ItemDisplayData.java b/src/main/java/noppes/npcs/controllers/data/ItemDisplayData.java new file mode 100644 index 000000000..0fa448b96 --- /dev/null +++ b/src/main/java/noppes/npcs/controllers/data/ItemDisplayData.java @@ -0,0 +1,120 @@ +package noppes.npcs.controllers.data; + +import net.minecraft.nbt.NBTTagCompound; + +public class ItemDisplayData { + public String texture = "minecraft:textures/items/iron_pickaxe.png"; + public Float translateX = 0F, translateY = 0F, translateZ = 0F; + public Integer itemColor = 0x8B4513; + public Float scaleX = 1.0F, scaleY = 1.0F, scaleZ = 1.0F; + public Float rotationX = 0F, rotationY = 0F, rotationZ = 0F; + public Float rotationXRate = 0F, rotationYRate = 0F, rotationZRate = 0F; + public Boolean durabilityShow = false; + public Integer durabilityColor = -1; + + public NBTTagCompound writeToNBT() { + NBTTagCompound compound = new NBTTagCompound(); + this.writeToNBT(compound); + return compound; + } + + public NBTTagCompound writeToNBT(NBTTagCompound compound) { + this.setBoolean(compound, "DurabilityShow", this.durabilityShow); + this.setInteger(compound, "DurabilityColor", this.durabilityColor); + this.setInteger(compound, "ItemColor", this.itemColor); + if (this.texture != null) { + compound.setString("ItemTexture", this.texture); + } + + this.setFloat(compound, "RotationX", this.rotationX); + this.setFloat(compound, "RotationY", this.rotationY); + this.setFloat(compound, "RotationZ", this.rotationZ); + + this.setFloat(compound, "RotationXRate", this.rotationXRate); + this.setFloat(compound, "RotationYRate", this.rotationYRate); + this.setFloat(compound, "RotationZRate", this.rotationZRate); + + this.setFloat(compound, "ScaleX", this.scaleX); + this.setFloat(compound, "ScaleY", this.scaleY); + this.setFloat(compound, "ScaleZ", this.scaleZ); + + this.setFloat(compound, "TranslateX", this.translateX); + this.setFloat(compound, "TranslateY", this.translateY); + this.setFloat(compound, "TranslateZ", this.translateZ); + + return compound; + } + + public void readFromNBT(NBTTagCompound compound) { + if (compound.hasKey("DurabilityShow")) { + this.durabilityShow = compound.getBoolean("DurabilityShow"); + } + if (compound.hasKey("DurabilityColor")) { + this.durabilityColor = compound.getInteger("DurabilityColor"); + } + if (compound.hasKey("ItemColor")) { + this.itemColor = compound.getInteger("ItemColor"); + } + if (compound.hasKey("ItemTexture")) { + this.texture = compound.getString("ItemTexture"); + } + + if (compound.hasKey("RotationX")) { + this.rotationX = compound.getFloat("RotationX"); + } + if (compound.hasKey("RotationY")) { + this.rotationY = compound.getFloat("RotationY"); + } + if (compound.hasKey("RotationZ")) { + this.rotationZ = compound.getFloat("RotationZ"); + } + + if (compound.hasKey("RotationXRate")) { + this.rotationXRate = compound.getFloat("RotationXRate"); + } + if (compound.hasKey("RotationYRate")) { + this.rotationYRate = compound.getFloat("RotationYRate"); + } + if (compound.hasKey("RotationZRate")) { + this.rotationZRate = compound.getFloat("RotationZRate"); + } + + if (compound.hasKey("ScaleX")) { + this.scaleX = compound.getFloat("ScaleX"); + } + if (compound.hasKey("ScaleY")) { + this.scaleY = compound.getFloat("ScaleY"); + } + if (compound.hasKey("ScaleZ")) { + this.scaleZ = compound.getFloat("ScaleZ"); + } + + if (compound.hasKey("TranslateX")) { + this.translateX = compound.getFloat("TranslateX"); + } + if (compound.hasKey("TranslateY")) { + this.translateY = compound.getFloat("TranslateY"); + } + if (compound.hasKey("TranslateZ")) { + this.translateZ = compound.getFloat("TranslateZ"); + } + } + + private void setBoolean(NBTTagCompound compound, String key, Boolean value) { + if (value != null) { + compound.setBoolean(key, value); + } + } + + private void setInteger(NBTTagCompound compound, String key, Integer value) { + if (value != null) { + compound.setInteger(key, value); + } + } + + private void setFloat(NBTTagCompound compound, String key, Float value) { + if (value != null) { + compound.setFloat(key, value); + } + } +} diff --git a/src/main/java/noppes/npcs/controllers/data/LinkedItem.java b/src/main/java/noppes/npcs/controllers/data/LinkedItem.java new file mode 100644 index 000000000..8eb28ec70 --- /dev/null +++ b/src/main/java/noppes/npcs/controllers/data/LinkedItem.java @@ -0,0 +1,73 @@ +package noppes.npcs.controllers.data; + +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagString; +import noppes.npcs.CustomItems; + +public class LinkedItem { + public static final String LINKED_NBT_TAG = "LinkedName"; + + private final String name; + public final ItemDisplayData display = new ItemDisplayData(); + + public double durabilityValue = 1.0D; + public int stackSize = 64; + + public int maxItemUseDuration = 20; + public int itemUseAction = 0; //0: none, 1: block, 2: bow, 3: eat, 4: drink + + public boolean isNormalItem = false; + public boolean isTool = false; + public int digSpeed = 1; + public int armorType = -2; //-2: Fits in no armor slot, -1: Fits in all slots, 0 - 4: Fits in Head -> Boots slot respectively + public int enchantability; + + public LinkedItem(String name) { + this.name = name; + } + + public String getName() { + return this.name; + } + + public ItemStack createStack() { + ItemStack linkedStack = new ItemStack(CustomItems.linked_item); + linkedStack.setTagInfo(LinkedItem.LINKED_NBT_TAG, new NBTTagString(this.getName())); + return linkedStack; + } + + public NBTTagCompound writeToNBT() { + NBTTagCompound compound = new NBTTagCompound(); + compound.setTag("Display", this.display.writeToNBT()); + + compound.setDouble("DurabilityValue",this.durabilityValue); + compound.setInteger("MaxStackSize",this.stackSize); + + compound.setBoolean("IsTool", this.isTool); + compound.setBoolean("IsNormalItem", this.isNormalItem); + compound.setInteger("DigSpeed", this.digSpeed); + compound.setInteger("ArmorType", this.armorType); + compound.setInteger("Enchantability", this.enchantability); + + compound.setInteger("MaxItemUseDuration", this.maxItemUseDuration); + compound.setInteger("ItemUseAction", this.itemUseAction); + return compound; + } + + public void readFromNBT(NBTTagCompound compound) { + this.display.readFromNBT(compound.getCompoundTag("Display")); + + this.durabilityValue = compound.getDouble("DurabilityValue"); + this.stackSize = compound.getInteger("MaxStackSize"); + + this.isTool = compound.getBoolean("IsTool"); + this.isNormalItem = compound.getBoolean("IsNormalItem"); + this.digSpeed = compound.getInteger("DigSpeed"); + this.armorType = compound.getInteger("ArmorType"); + this.enchantability = compound.getInteger("Enchantability"); + + this.maxItemUseDuration = compound.getInteger("MaxItemUseDuration"); + this.itemUseAction = compound.getInteger("ItemUseAction"); + } +} diff --git a/src/main/java/noppes/npcs/items/ItemLinked.java b/src/main/java/noppes/npcs/items/ItemLinked.java new file mode 100644 index 000000000..8ab7bf546 --- /dev/null +++ b/src/main/java/noppes/npcs/items/ItemLinked.java @@ -0,0 +1,160 @@ +package noppes.npcs.items; + +import cpw.mods.fml.common.registry.GameRegistry; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import net.minecraft.block.Block; +import net.minecraft.client.renderer.texture.IIconRegister; +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.init.Items; +import net.minecraft.item.EnumAction; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.world.World; +import noppes.npcs.CustomNpcs; +import noppes.npcs.EventHooks; +import noppes.npcs.api.item.IItemStack; +import noppes.npcs.scripted.NpcAPI; +import noppes.npcs.scripted.event.ItemEvent; +import noppes.npcs.scripted.item.ScriptCustomItem; +import noppes.npcs.scripted.item.ScriptLinkedItem; +import org.lwjgl.opengl.GL11; + +public class ItemLinked extends Item implements ItemRenderInterface { + + public ItemLinked() { + maxStackSize = 1; + CustomNpcs.proxy.registerItem(this); + setHasSubtypes(true); + } + + @SideOnly(Side.CLIENT) + public void registerIcons(IIconRegister par1IconRegister){ + this.itemIcon = Items.iron_pickaxe.getIconFromDamage(0); + } + + @Override + public int getColorFromItemStack(ItemStack itemStack, int par2){ + return 0x8B4513; + } + + @Override + public boolean requiresMultipleRenderPasses(){ + return true; + } + + @Override + public Item setUnlocalizedName(String name){ + GameRegistry.registerItem(this, name); + return super.setUnlocalizedName(name); + } + + public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) + { + player.setItemInUse(stack, this.getMaxItemUseDuration(stack)); + return stack; + } + + @Override + public boolean onEntitySwing(EntityLivingBase entityLivingBase, ItemStack stack){ + if (entityLivingBase.worldObj.isRemote) { + return false; + } + + //TODO: re-implement item attack swing hook +// IItemStack istack = NpcAPI.Instance().getIItemStack(stack); +// ItemEvent.AttackEvent eve = new ItemEvent.AttackEvent((ScriptLinkedItem) istack, NpcAPI.Instance().getIEntity(entityLivingBase), 2, null); +// return EventHooks.onScriptItemAttack((ScriptLinkedItem) istack, eve); + return true; + } + + public int getMaxItemUseDuration(ItemStack stack){ + return (new ScriptLinkedItem(stack)).getMaxItemUseDuration(); + } + + //TODO: Use this in existing item scripted rendering. Refactor the renderer to use the new abstract ScriptCustomizableItem class + public static ScriptLinkedItem GetWrapper(ItemStack stack) { + return new ScriptLinkedItem(stack); + } + + public boolean showDurabilityBar(ItemStack stack) { + IItemStack istack = NpcAPI.Instance().getIItemStack(stack); + return istack instanceof ScriptLinkedItem && (new ScriptLinkedItem(stack)).itemDisplay.durabilityShow; + } + + public double getDurabilityForDisplay(ItemStack stack) { + IItemStack istack = NpcAPI.Instance().getIItemStack(stack); + return istack instanceof ScriptLinkedItem ? 1.0D - (new ScriptLinkedItem(stack)).getDurabilityValue() : 1.0D; + } + + public int getItemStackLimit(ItemStack stack) { + IItemStack istack = NpcAPI.Instance().getIItemStack(stack); + return istack instanceof ScriptLinkedItem ? (new ScriptLinkedItem(stack)).getMaxStackSize() : super.getItemStackLimit(stack); + } + + public boolean isItemTool(ItemStack stack) + { + IItemStack istack = NpcAPI.Instance().getIItemStack(stack); + return istack instanceof ScriptLinkedItem ? (new ScriptLinkedItem(stack)).isTool() : super.isItemTool(stack); + } + + public float getDigSpeed(ItemStack stack, Block block, int metadata) + { + IItemStack istack = NpcAPI.Instance().getIItemStack(stack); + return istack instanceof ScriptLinkedItem ? (new ScriptLinkedItem(stack)).getDigSpeed() : super.getDigSpeed(stack, block, metadata); + } + + public boolean isValidArmor(ItemStack stack, int armorType, Entity entity) + { + IItemStack istack = NpcAPI.Instance().getIItemStack(stack); + + if((new ScriptLinkedItem(stack)).getArmorType() == -1) + return true; + + return istack instanceof ScriptLinkedItem ? armorType == (new ScriptLinkedItem(stack)).getArmorType() : super.isValidArmor(stack, armorType, entity); + } + + public int getItemEnchantability(ItemStack stack) + { + IItemStack istack = NpcAPI.Instance().getIItemStack(stack); + return istack instanceof ScriptLinkedItem ? (new ScriptLinkedItem(stack)).getEnchantability() : super.getItemEnchantability(stack); + } + + public boolean hitEntity(ItemStack stack, EntityLivingBase target, EntityLivingBase attacker) { + IItemStack istack = NpcAPI.Instance().getIItemStack(stack); + + //TODO: re-implement item attack hook +// ItemEvent.AttackEvent eve = new ItemEvent.AttackEvent( (ScriptLinkedItem) istack, NpcAPI.Instance().getIEntity(attacker), 1, NpcAPI.Instance().getIEntity(target)); +// return EventHooks.onScriptItemAttack((ScriptLinkedItem) istack, eve); + return true; + } + + public EnumAction getItemUseAction(ItemStack stack) + { + IItemStack istack = NpcAPI.Instance().getIItemStack(stack); + if (istack instanceof ScriptLinkedItem) { + switch (((ScriptLinkedItem) istack).getItemUseAction()) { + case 0: + return EnumAction.none; + case 1: + return EnumAction.block; + case 2: + return EnumAction.bow; + case 3: + return EnumAction.eat; + case 4: + return EnumAction.drink; + } + } + return super.getItemUseAction(stack); + } + + @Override + public void renderSpecial() {} + + public void renderOffset(ScriptLinkedItem scriptCustomItem) { + GL11.glTranslatef(0.135F * scriptCustomItem.itemDisplay.scaleX, 0.2F * scriptCustomItem.itemDisplay.scaleY, 0.07F * scriptCustomItem.itemDisplay.scaleZ); + } +} diff --git a/src/main/java/noppes/npcs/scripted/NpcAPI.java b/src/main/java/noppes/npcs/scripted/NpcAPI.java index 0b5f7d64d..1b47fb30d 100644 --- a/src/main/java/noppes/npcs/scripted/NpcAPI.java +++ b/src/main/java/noppes/npcs/scripted/NpcAPI.java @@ -48,13 +48,11 @@ import noppes.npcs.config.ConfigScript; import noppes.npcs.constants.EnumAnimationPart; import noppes.npcs.containers.ContainerNpcInterface; import noppes.npcs.controllers.*; -import noppes.npcs.controllers.data.Animation; -import noppes.npcs.controllers.data.Frame; -import noppes.npcs.controllers.data.FramePart; -import noppes.npcs.controllers.data.SkinOverlay; +import noppes.npcs.controllers.data.*; import noppes.npcs.entity.EntityCustomNpc; import noppes.npcs.entity.EntityNPCInterface; import noppes.npcs.entity.EntityProjectile; +import noppes.npcs.items.ItemLinked; import noppes.npcs.items.ItemScripted; import noppes.npcs.scripted.entity.*; import noppes.npcs.scripted.gui.ScriptGui; @@ -64,7 +62,6 @@ import noppes.npcs.util.*; import java.io.*; import java.net.HttpURLConnection; -import java.net.MalformedURLException; import java.net.URL; import java.util.*; import java.util.concurrent.ExecutorService; @@ -473,7 +470,11 @@ public class NpcAPI extends AbstractNpcAPI { if (scriptItemCache.containsKey(itemstack)) { scriptStack = scriptItemCache.get(itemstack).getObject(); } else { - if (itemstack.getItem() instanceof ItemScripted) { + String linkedItemName = itemstack.getTagCompound().getString(LinkedItem.LINKED_NBT_TAG); + LinkedItem linkedItem = LinkedItemController.Instance().get(linkedItemName); + if (linkedItem != null && itemstack.getItem() instanceof ItemLinked) { + scriptStack = new ScriptLinkedItem(itemstack); + } else if (itemstack.getItem() instanceof ItemScripted) { scriptStack = new ScriptCustomItem(itemstack); } else if (itemstack.getItem() instanceof ItemArmor) { scriptStack = new ScriptItemArmor(itemstack); diff --git a/src/main/java/noppes/npcs/scripted/item/ScriptCustomizableItem.java b/src/main/java/noppes/npcs/scripted/item/ScriptCustomizableItem.java index e788e0e26..fc837d14e 100644 --- a/src/main/java/noppes/npcs/scripted/item/ScriptCustomizableItem.java +++ b/src/main/java/noppes/npcs/scripted/item/ScriptCustomizableItem.java @@ -2,9 +2,10 @@ package noppes.npcs.scripted.item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; +import noppes.npcs.controllers.data.ItemDisplayData; public abstract class ScriptCustomizableItem extends ScriptItemStack { - public final Display itemDisplay = new Display(); + public final ItemDisplayData itemDisplay = new ItemDisplayData(); public ScriptCustomizableItem(ItemStack item) { super(item); @@ -188,112 +189,12 @@ public abstract class ScriptCustomizableItem extends ScriptItemStack { } public NBTTagCompound getItemNBT(NBTTagCompound compound) { - this.setBoolean(compound, "DurabilityShow",this.itemDisplay.durabilityShow); - this.setInteger(compound, "DurabilityColor",this.itemDisplay.durabilityColor); - this.setInteger(compound, "ItemColor",this.itemDisplay.itemColor); - if (this.itemDisplay.texture != null) { - compound.setString("ItemTexture", this.itemDisplay.texture); - } - - this.setFloat(compound, "RotationX",this.itemDisplay.rotationX); - this.setFloat(compound, "RotationY",this.itemDisplay.rotationY); - this.setFloat(compound, "RotationZ",this.itemDisplay.rotationZ); - - this.setFloat(compound, "RotationXRate",this.itemDisplay.rotationXRate); - this.setFloat(compound, "RotationYRate",this.itemDisplay.rotationYRate); - this.setFloat(compound, "RotationZRate",this.itemDisplay.rotationZRate); - - this.setFloat(compound, "ScaleX",this.itemDisplay.scaleX); - this.setFloat(compound, "ScaleY",this.itemDisplay.scaleY); - this.setFloat(compound, "ScaleZ",this.itemDisplay.scaleZ); - - this.setFloat(compound, "TranslateX",this.itemDisplay.translateX); - this.setFloat(compound, "TranslateY",this.itemDisplay.translateY); - this.setFloat(compound, "TranslateZ",this.itemDisplay.translateZ); + this.itemDisplay.writeToNBT(compound); return compound; } - private void setBoolean(NBTTagCompound compound, String key, Boolean value) { - if (value != null) { - compound.setBoolean(key, value); - } - } - - private void setInteger(NBTTagCompound compound, String key, Integer value) { - if (value != null) { - compound.setInteger(key, value); - } - } - - private void setFloat(NBTTagCompound compound, String key, Float value) { - if (value != null) { - compound.setFloat(key, value); - } - } - public void setItemNBT(NBTTagCompound compound) { - if (compound.hasKey("DurabilityShow")) { - this.itemDisplay.durabilityShow = compound.getBoolean("DurabilityShow"); - } - if (compound.hasKey("DurabilityColor")) { - this.itemDisplay.durabilityColor = compound.getInteger("DurabilityColor"); - } - if (compound.hasKey("ItemColor")) { - this.itemDisplay.itemColor = compound.getInteger("ItemColor"); - } - if (compound.hasKey("ItemTexture")) { - this.itemDisplay.texture = compound.getString("ItemTexture"); - } - - if (compound.hasKey("RotationX")) { - this.itemDisplay.rotationX = compound.getFloat("RotationX"); - } - if (compound.hasKey("RotationY")) { - this.itemDisplay.rotationY = compound.getFloat("RotationY"); - } - if (compound.hasKey("RotationZ")) { - this.itemDisplay.rotationZ = compound.getFloat("RotationZ"); - } - - if (compound.hasKey("RotationXRate")) { - this.itemDisplay.rotationXRate = compound.getFloat("RotationXRate"); - } - if (compound.hasKey("RotationYRate")) { - this.itemDisplay.rotationYRate = compound.getFloat("RotationYRate"); - } - if (compound.hasKey("RotationZRate")) { - this.itemDisplay.rotationZRate = compound.getFloat("RotationZRate"); - } - - if (compound.hasKey("ScaleX")) { - this.itemDisplay.scaleX = compound.getFloat("ScaleX"); - } - if (compound.hasKey("ScaleY")) { - this.itemDisplay.scaleY = compound.getFloat("ScaleY"); - } - if (compound.hasKey("ScaleZ")) { - this.itemDisplay.scaleZ = compound.getFloat("ScaleZ"); - } - - if (compound.hasKey("TranslateX")) { - this.itemDisplay.translateX = compound.getFloat("TranslateX"); - } - if (compound.hasKey("TranslateY")) { - this.itemDisplay.translateY = compound.getFloat("TranslateY"); - } - if (compound.hasKey("TranslateZ")) { - this.itemDisplay.translateZ = compound.getFloat("TranslateZ"); - } + this.itemDisplay.readFromNBT(compound); } - public static class Display { - public String texture = "minecraft:textures/items/iron_pickaxe.png"; - public Float translateX = 0F, translateY = 0F, translateZ = 0F; - public Integer itemColor = 0x8B4513; - public Float scaleX = 1.0F, scaleY = 1.0F, scaleZ = 1.0F; - public Float rotationX = 0F, rotationY = 0F, rotationZ = 0F; - public Float rotationXRate = 0F, rotationYRate = 0F, rotationZRate = 0F; - public Boolean durabilityShow = false; - public Integer durabilityColor = -1; - } } diff --git a/src/main/java/noppes/npcs/scripted/item/ScriptLinkedItem.java b/src/main/java/noppes/npcs/scripted/item/ScriptLinkedItem.java new file mode 100644 index 000000000..1fb1833ba --- /dev/null +++ b/src/main/java/noppes/npcs/scripted/item/ScriptLinkedItem.java @@ -0,0 +1,145 @@ +package noppes.npcs.scripted.item; + +import net.minecraft.item.ItemStack; +import noppes.npcs.controllers.LinkedItemController; +import noppes.npcs.controllers.data.LinkedItem; + +public class ScriptLinkedItem extends ScriptCustomizableItem { + private final LinkedItem linkedItem; + + public ScriptLinkedItem(ItemStack item) { + super(item); + String linkedItemName = item.getTagCompound().getString(LinkedItem.LINKED_NBT_TAG); + this.linkedItem = LinkedItemController.Instance().get(linkedItemName); + + this.itemDisplay.texture = null; + this.itemDisplay.translateX = null; + this.itemDisplay.translateY = null; + this.itemDisplay.translateZ = null; + this.itemDisplay.itemColor = null; + this.itemDisplay.scaleX = null; + this.itemDisplay.scaleY = null; + this.itemDisplay.scaleZ = null; + this.itemDisplay.rotationX = null; + this.itemDisplay.rotationY = null; + this.itemDisplay.rotationZ = null; + this.itemDisplay.rotationXRate = null; + this.itemDisplay.rotationYRate = null; + this.itemDisplay.rotationZRate = null; + this.itemDisplay.durabilityShow = null; + this.itemDisplay.durabilityColor = null; + } + + public LinkedItem getLinkedItem() { + return this.linkedItem; + } + + @Override + public int getMaxStackSize() { + return this.linkedItem.stackSize; + } + + @Override + public int getArmorType() { + return this.linkedItem.armorType; + } + + @Override + public boolean isTool() { + return this.linkedItem.isTool; + } + + @Override + public boolean isNormalItem() { + return this.linkedItem.isNormalItem; + } + + @Override + public int getDigSpeed() { + return this.linkedItem.digSpeed; + } + + @Override + public double getDurabilityValue() { + return this.linkedItem.durabilityValue; + } + + @Override + public int getMaxItemUseDuration() { + return this.linkedItem.maxItemUseDuration; + } + + @Override + public int getItemUseAction() { + return this.linkedItem.itemUseAction; + } + + @Override + public int getEnchantability() { + return this.linkedItem.enchantability; + } + + public String getTexture() { + return this.itemDisplay.texture == null ? this.linkedItem.display.texture : this.itemDisplay.texture; + } + + public Boolean getDurabilityShow() { + return this.itemDisplay.durabilityShow != null ? this.itemDisplay.durabilityShow : this.linkedItem.display.durabilityShow; + } + + public Integer getDurabilityColor() { + return this.itemDisplay.durabilityColor != null ? this.itemDisplay.durabilityColor : this.linkedItem.display.durabilityColor; + } + + public Integer getColor() { + return this.itemDisplay.itemColor != null ? this.itemDisplay.itemColor : this.linkedItem.display.itemColor; + } + + public Float getRotationX() { + return this.itemDisplay.rotationX != null ? this.itemDisplay.rotationX : this.linkedItem.display.rotationX; + } + + public Float getRotationY() { + return this.itemDisplay.rotationY != null ? this.itemDisplay.rotationY : this.linkedItem.display.rotationY; + } + + public Float getRotationZ() { + return this.itemDisplay.rotationZ != null ? this.itemDisplay.rotationZ : this.linkedItem.display.rotationZ; + } + + public Float getRotationXRate() { + return this.itemDisplay.rotationXRate != null ? this.itemDisplay.rotationXRate : this.linkedItem.display.rotationXRate; + } + + public Float getRotationYRate() { + return this.itemDisplay.rotationYRate != null ? this.itemDisplay.rotationYRate : this.linkedItem.display.rotationYRate; + } + + public Float getRotationZRate() { + return this.itemDisplay.rotationZRate != null ? this.itemDisplay.rotationZRate : this.linkedItem.display.rotationZRate; + } + + public Float getScaleX() { + return this.itemDisplay.scaleX != null ? this.itemDisplay.scaleX : this.linkedItem.display.scaleX; + } + + public Float getScaleY() { + return this.itemDisplay.scaleY != null ? this.itemDisplay.scaleY : this.linkedItem.display.scaleY; + } + + public Float getScaleZ() { + return this.itemDisplay.scaleZ != null ? this.itemDisplay.scaleZ : this.linkedItem.display.scaleZ; + } + + public Float getTranslateX() { + return this.itemDisplay.translateX != null ? this.itemDisplay.translateX : this.linkedItem.display.translateX; + } + + public Float getTranslateY() { + return this.itemDisplay.translateY != null ? this.itemDisplay.translateY : this.linkedItem.display.translateY; + } + + public Float getTranslateZ() { + return this.itemDisplay.translateZ != null ? this.itemDisplay.translateZ : this.linkedItem.display.translateZ; + } +} From d9e6372acb8a545645e6e3ec34e41d0b074b1419 Mon Sep 17 00:00:00 2001 From: luisc Date: Thu, 13 Feb 2025 18:16:53 -0600 Subject: [PATCH 07/13] Added ItemCustomizable abstract class which both scripted and linked items extend from. Common behavior between the classes should move there. --- src/main/java/noppes/npcs/items/ItemCustomizable.java | 9 +++++++++ src/main/java/noppes/npcs/items/ItemLinked.java | 5 +---- src/main/java/noppes/npcs/items/ItemScripted.java | 5 +---- 3 files changed, 11 insertions(+), 8 deletions(-) create mode 100644 src/main/java/noppes/npcs/items/ItemCustomizable.java diff --git a/src/main/java/noppes/npcs/items/ItemCustomizable.java b/src/main/java/noppes/npcs/items/ItemCustomizable.java new file mode 100644 index 000000000..f028149ca --- /dev/null +++ b/src/main/java/noppes/npcs/items/ItemCustomizable.java @@ -0,0 +1,9 @@ +package noppes.npcs.items; + +import net.minecraft.item.Item; + +public abstract class ItemCustomizable extends Item implements ItemRenderInterface { + + @Override + public void renderSpecial() {} +} diff --git a/src/main/java/noppes/npcs/items/ItemLinked.java b/src/main/java/noppes/npcs/items/ItemLinked.java index 8ab7bf546..e8e791f7f 100644 --- a/src/main/java/noppes/npcs/items/ItemLinked.java +++ b/src/main/java/noppes/npcs/items/ItemLinked.java @@ -22,7 +22,7 @@ import noppes.npcs.scripted.item.ScriptCustomItem; import noppes.npcs.scripted.item.ScriptLinkedItem; import org.lwjgl.opengl.GL11; -public class ItemLinked extends Item implements ItemRenderInterface { +public class ItemLinked extends ItemCustomizable { public ItemLinked() { maxStackSize = 1; @@ -151,9 +151,6 @@ public class ItemLinked extends Item implements ItemRenderInterface { return super.getItemUseAction(stack); } - @Override - public void renderSpecial() {} - public void renderOffset(ScriptLinkedItem scriptCustomItem) { GL11.glTranslatef(0.135F * scriptCustomItem.itemDisplay.scaleX, 0.2F * scriptCustomItem.itemDisplay.scaleY, 0.07F * scriptCustomItem.itemDisplay.scaleZ); } diff --git a/src/main/java/noppes/npcs/items/ItemScripted.java b/src/main/java/noppes/npcs/items/ItemScripted.java index 5fdcf5751..cdd01a119 100644 --- a/src/main/java/noppes/npcs/items/ItemScripted.java +++ b/src/main/java/noppes/npcs/items/ItemScripted.java @@ -26,7 +26,7 @@ import noppes.npcs.scripted.event.ItemEvent; import noppes.npcs.scripted.item.ScriptCustomItem; import org.lwjgl.opengl.GL11; -public class ItemScripted extends Item implements ItemRenderInterface { +public class ItemScripted extends ItemCustomizable { public ItemScripted() { maxStackSize = 1; setCreativeTab(CustomItems.tab); @@ -156,9 +156,6 @@ public class ItemScripted extends Item implements ItemRenderInterface { return super.getItemUseAction(stack); } - @Override - public void renderSpecial() {} - public void renderOffset(ScriptCustomItem scriptCustomItem) { GL11.glTranslatef(0.135F * scriptCustomItem.itemDisplay.scaleX, 0.2F * scriptCustomItem.itemDisplay.scaleY, 0.07F * scriptCustomItem.itemDisplay.scaleZ); } From cb5b8898bbe615eb3b576cc40a98d1b975c5e463 Mon Sep 17 00:00:00 2001 From: luisc Date: Thu, 13 Feb 2025 18:20:01 -0600 Subject: [PATCH 08/13] Moved common stuff into ItemCustomizable --- .../noppes/npcs/items/ItemCustomizable.java | 60 +++++++++++++++++++ .../java/noppes/npcs/items/ItemLinked.java | 27 --------- .../java/noppes/npcs/items/ItemScripted.java | 46 +------------- 3 files changed, 63 insertions(+), 70 deletions(-) diff --git a/src/main/java/noppes/npcs/items/ItemCustomizable.java b/src/main/java/noppes/npcs/items/ItemCustomizable.java index f028149ca..68467cd90 100644 --- a/src/main/java/noppes/npcs/items/ItemCustomizable.java +++ b/src/main/java/noppes/npcs/items/ItemCustomizable.java @@ -1,9 +1,69 @@ package noppes.npcs.items; +import cpw.mods.fml.common.registry.GameRegistry; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import net.minecraft.client.renderer.texture.IIconRegister; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.init.Items; +import net.minecraft.item.EnumAction; import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.world.World; +import noppes.npcs.api.item.IItemStack; +import noppes.npcs.scripted.NpcAPI; +import noppes.npcs.scripted.item.ScriptCustomizableItem; public abstract class ItemCustomizable extends Item implements ItemRenderInterface { + @SideOnly(Side.CLIENT) + public void registerIcons(IIconRegister par1IconRegister){ + this.itemIcon = Items.iron_pickaxe.getIconFromDamage(0); + } + + @Override + public int getColorFromItemStack(ItemStack itemStack, int par2){ + return 0x8B4513; + } + + @Override + public boolean requiresMultipleRenderPasses(){ + return true; + } + + @Override + public Item setUnlocalizedName(String name){ + GameRegistry.registerItem(this, name); + return super.setUnlocalizedName(name); + } + + public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) + { + player.setItemInUse(stack, this.getMaxItemUseDuration(stack)); + return stack; + } + + + public EnumAction getItemUseAction(ItemStack stack) + { + IItemStack istack = NpcAPI.Instance().getIItemStack(stack); + if (istack instanceof ScriptCustomizableItem) { + switch (((ScriptCustomizableItem) istack).getItemUseAction()) { + case 0: + return EnumAction.none; + case 1: + return EnumAction.block; + case 2: + return EnumAction.bow; + case 3: + return EnumAction.eat; + case 4: + return EnumAction.drink; + } + } + return super.getItemUseAction(stack); + } + @Override public void renderSpecial() {} } diff --git a/src/main/java/noppes/npcs/items/ItemLinked.java b/src/main/java/noppes/npcs/items/ItemLinked.java index e8e791f7f..f104b2647 100644 --- a/src/main/java/noppes/npcs/items/ItemLinked.java +++ b/src/main/java/noppes/npcs/items/ItemLinked.java @@ -30,33 +30,6 @@ public class ItemLinked extends ItemCustomizable { setHasSubtypes(true); } - @SideOnly(Side.CLIENT) - public void registerIcons(IIconRegister par1IconRegister){ - this.itemIcon = Items.iron_pickaxe.getIconFromDamage(0); - } - - @Override - public int getColorFromItemStack(ItemStack itemStack, int par2){ - return 0x8B4513; - } - - @Override - public boolean requiresMultipleRenderPasses(){ - return true; - } - - @Override - public Item setUnlocalizedName(String name){ - GameRegistry.registerItem(this, name); - return super.setUnlocalizedName(name); - } - - public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) - { - player.setItemInUse(stack, this.getMaxItemUseDuration(stack)); - return stack; - } - @Override public boolean onEntitySwing(EntityLivingBase entityLivingBase, ItemStack stack){ if (entityLivingBase.worldObj.isRemote) { diff --git a/src/main/java/noppes/npcs/items/ItemScripted.java b/src/main/java/noppes/npcs/items/ItemScripted.java index cdd01a119..68db37050 100644 --- a/src/main/java/noppes/npcs/items/ItemScripted.java +++ b/src/main/java/noppes/npcs/items/ItemScripted.java @@ -24,9 +24,11 @@ import noppes.npcs.constants.EnumGuiType; import noppes.npcs.scripted.NpcAPI; import noppes.npcs.scripted.event.ItemEvent; import noppes.npcs.scripted.item.ScriptCustomItem; +import noppes.npcs.scripted.item.ScriptCustomizableItem; import org.lwjgl.opengl.GL11; public class ItemScripted extends ItemCustomizable { + public ItemScripted() { maxStackSize = 1; setCreativeTab(CustomItems.tab); @@ -34,27 +36,6 @@ public class ItemScripted extends ItemCustomizable { setHasSubtypes(true); } - @SideOnly(Side.CLIENT) - public void registerIcons(IIconRegister par1IconRegister){ - this.itemIcon = Items.iron_pickaxe.getIconFromDamage(0); - } - - @Override - public int getColorFromItemStack(ItemStack itemStack, int par2){ - return 0x8B4513; - } - - @Override - public boolean requiresMultipleRenderPasses(){ - return true; - } - - @Override - public Item setUnlocalizedName(String name){ - GameRegistry.registerItem(this, name); - return super.setUnlocalizedName(name); - } - public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) { if(player.isSneaking() && player.capabilities.isCreativeMode) { @@ -64,8 +45,7 @@ public class ItemScripted extends ItemCustomizable { CustomNpcs.proxy.openGui(0, 0, 0, EnumGuiType.ScriptItem, player); } } - player.setItemInUse(stack, this.getMaxItemUseDuration(stack)); - return stack; + return super.onItemRightClick(stack, world, player); } @Override @@ -136,26 +116,6 @@ public class ItemScripted extends ItemCustomizable { return EventHooks.onScriptItemAttack((ScriptCustomItem) istack, eve); } - public EnumAction getItemUseAction(ItemStack stack) - { - IItemStack istack = NpcAPI.Instance().getIItemStack(stack); - if (istack instanceof ScriptCustomItem) { - switch (((ScriptCustomItem) istack).getItemUseAction()) { - case 0: - return EnumAction.none; - case 1: - return EnumAction.block; - case 2: - return EnumAction.bow; - case 3: - return EnumAction.eat; - case 4: - return EnumAction.drink; - } - } - return super.getItemUseAction(stack); - } - public void renderOffset(ScriptCustomItem scriptCustomItem) { GL11.glTranslatef(0.135F * scriptCustomItem.itemDisplay.scaleX, 0.2F * scriptCustomItem.itemDisplay.scaleY, 0.07F * scriptCustomItem.itemDisplay.scaleZ); } From 6d80acb5f0d399d3f05f4e174838b63bf33bcfdd Mon Sep 17 00:00:00 2001 From: luisc Date: Thu, 13 Feb 2025 18:44:11 -0600 Subject: [PATCH 09/13] Created IItemCustomizable interface --- .../java/noppes/npcs/scripted/item/ScriptCustomizableItem.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/noppes/npcs/scripted/item/ScriptCustomizableItem.java b/src/main/java/noppes/npcs/scripted/item/ScriptCustomizableItem.java index fc837d14e..fcea1e25e 100644 --- a/src/main/java/noppes/npcs/scripted/item/ScriptCustomizableItem.java +++ b/src/main/java/noppes/npcs/scripted/item/ScriptCustomizableItem.java @@ -2,9 +2,10 @@ package noppes.npcs.scripted.item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; +import noppes.npcs.api.item.IItemCustomizable; import noppes.npcs.controllers.data.ItemDisplayData; -public abstract class ScriptCustomizableItem extends ScriptItemStack { +public abstract class ScriptCustomizableItem extends ScriptItemStack implements IItemCustomizable { public final ItemDisplayData itemDisplay = new ItemDisplayData(); public ScriptCustomizableItem(ItemStack item) { From d394174108c0ac530578635d3967a3f1b0fced0b Mon Sep 17 00:00:00 2001 From: luisc Date: Thu, 13 Feb 2025 19:00:21 -0600 Subject: [PATCH 10/13] Made item events use IItemCustomizable interface. --- src/api | 2 +- .../java/noppes/npcs/items/ItemLinked.java | 31 ------------------- .../java/noppes/npcs/items/ItemScripted.java | 8 ----- .../noppes/npcs/scripted/event/ItemEvent.java | 31 ++++++++++--------- 4 files changed, 17 insertions(+), 55 deletions(-) diff --git a/src/api b/src/api index 5026e3043..d093c0049 160000 --- a/src/api +++ b/src/api @@ -1 +1 @@ -Subproject commit 5026e30437fb0c4868a56cea747d616ce5392646 +Subproject commit d093c00493d4dfd82f2f250008432ca6536c4529 diff --git a/src/main/java/noppes/npcs/items/ItemLinked.java b/src/main/java/noppes/npcs/items/ItemLinked.java index f104b2647..fff812e50 100644 --- a/src/main/java/noppes/npcs/items/ItemLinked.java +++ b/src/main/java/noppes/npcs/items/ItemLinked.java @@ -1,24 +1,13 @@ package noppes.npcs.items; -import cpw.mods.fml.common.registry.GameRegistry; -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; import net.minecraft.block.Block; -import net.minecraft.client.renderer.texture.IIconRegister; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityLivingBase; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.init.Items; import net.minecraft.item.EnumAction; -import net.minecraft.item.Item; import net.minecraft.item.ItemStack; -import net.minecraft.world.World; import noppes.npcs.CustomNpcs; -import noppes.npcs.EventHooks; import noppes.npcs.api.item.IItemStack; import noppes.npcs.scripted.NpcAPI; -import noppes.npcs.scripted.event.ItemEvent; -import noppes.npcs.scripted.item.ScriptCustomItem; import noppes.npcs.scripted.item.ScriptLinkedItem; import org.lwjgl.opengl.GL11; @@ -104,26 +93,6 @@ public class ItemLinked extends ItemCustomizable { return true; } - public EnumAction getItemUseAction(ItemStack stack) - { - IItemStack istack = NpcAPI.Instance().getIItemStack(stack); - if (istack instanceof ScriptLinkedItem) { - switch (((ScriptLinkedItem) istack).getItemUseAction()) { - case 0: - return EnumAction.none; - case 1: - return EnumAction.block; - case 2: - return EnumAction.bow; - case 3: - return EnumAction.eat; - case 4: - return EnumAction.drink; - } - } - return super.getItemUseAction(stack); - } - public void renderOffset(ScriptLinkedItem scriptCustomItem) { GL11.glTranslatef(0.135F * scriptCustomItem.itemDisplay.scaleX, 0.2F * scriptCustomItem.itemDisplay.scaleY, 0.07F * scriptCustomItem.itemDisplay.scaleZ); } diff --git a/src/main/java/noppes/npcs/items/ItemScripted.java b/src/main/java/noppes/npcs/items/ItemScripted.java index 68db37050..d2e1edad3 100644 --- a/src/main/java/noppes/npcs/items/ItemScripted.java +++ b/src/main/java/noppes/npcs/items/ItemScripted.java @@ -1,16 +1,9 @@ package noppes.npcs.items; -import cpw.mods.fml.common.registry.GameRegistry; -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; import net.minecraft.block.Block; -import net.minecraft.client.renderer.texture.IIconRegister; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.init.Items; -import net.minecraft.item.EnumAction; -import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.util.ChatComponentTranslation; import net.minecraft.world.World; @@ -24,7 +17,6 @@ import noppes.npcs.constants.EnumGuiType; import noppes.npcs.scripted.NpcAPI; import noppes.npcs.scripted.event.ItemEvent; import noppes.npcs.scripted.item.ScriptCustomItem; -import noppes.npcs.scripted.item.ScriptCustomizableItem; import org.lwjgl.opengl.GL11; public class ItemScripted extends ItemCustomizable { diff --git a/src/main/java/noppes/npcs/scripted/event/ItemEvent.java b/src/main/java/noppes/npcs/scripted/event/ItemEvent.java index d94c0448b..212ca154f 100644 --- a/src/main/java/noppes/npcs/scripted/event/ItemEvent.java +++ b/src/main/java/noppes/npcs/scripted/event/ItemEvent.java @@ -5,16 +5,17 @@ import noppes.npcs.api.entity.IEntity; import noppes.npcs.api.entity.IPlayer; import noppes.npcs.api.event.IItemEvent; import noppes.npcs.api.item.IItemCustom; +import noppes.npcs.api.item.IItemCustomizable; import noppes.npcs.constants.EnumScriptType; public class ItemEvent extends CustomNPCsEvent implements IItemEvent { - public final IItemCustom item; + public final IItemCustomizable item; - public ItemEvent(IItemCustom item) { + public ItemEvent(IItemCustomizable item) { this.item = item; } - public IItemCustom getItem() { + public IItemCustomizable getItem() { return item; } @@ -23,7 +24,7 @@ public class ItemEvent extends CustomNPCsEvent implements IItemEvent { } public static class InitEvent extends ItemEvent implements IItemEvent.InitEvent { - public InitEvent(IItemCustom item) { + public InitEvent(IItemCustomizable item) { super(item); } @@ -35,7 +36,7 @@ public class ItemEvent extends CustomNPCsEvent implements IItemEvent { public static class UpdateEvent extends ItemEvent implements IItemEvent.UpdateEvent { public final IEntity entity; - public UpdateEvent(IItemCustom item, IEntity entity) { + public UpdateEvent(IItemCustomizable item, IEntity entity) { super(item); this.entity = entity; } @@ -54,7 +55,7 @@ public class ItemEvent extends CustomNPCsEvent implements IItemEvent { public final IEntity entity; public final IPlayer player; - public TossedEvent(IItemCustom item, IPlayer player, IEntity entity) { + public TossedEvent(IItemCustomizable item, IPlayer player, IEntity entity) { super(item); this.entity = entity; this.player = player; @@ -76,7 +77,7 @@ public class ItemEvent extends CustomNPCsEvent implements IItemEvent { public static class PickedUpEvent extends ItemEvent implements IItemEvent.PickedUpEvent { public final IPlayer player; - public PickedUpEvent(IItemCustom item, IPlayer player) { + public PickedUpEvent(IItemCustomizable item, IPlayer player) { super(item); this.player = player; } @@ -94,7 +95,7 @@ public class ItemEvent extends CustomNPCsEvent implements IItemEvent { public static class SpawnEvent extends ItemEvent implements IItemEvent.SpawnEvent { public final IEntity entity; - public SpawnEvent(IItemCustom item, IEntity entity){ + public SpawnEvent(IItemCustomizable item, IEntity entity){ super(item); this.entity = entity; } @@ -114,7 +115,7 @@ public class ItemEvent extends CustomNPCsEvent implements IItemEvent { public final IEntity target; public final IPlayer player; - public InteractEvent(IItemCustom item, IPlayer player, int type, IEntity target) { + public InteractEvent(IItemCustomizable item, IPlayer player, int type, IEntity target) { super(item); this.type = type; this.target = target; @@ -148,7 +149,7 @@ public class ItemEvent extends CustomNPCsEvent implements IItemEvent { public final Object target; public final IPlayer player; - public RightClickEvent(IItemCustom item, IPlayer player, int type, Object target) { + public RightClickEvent(IItemCustomizable item, IPlayer player, int type, Object target) { super(item); this.type = type; this.target = target; @@ -178,7 +179,7 @@ public class ItemEvent extends CustomNPCsEvent implements IItemEvent { public final IEntity target; public final IEntity swingingEntity; - public AttackEvent(IItemCustom item, IEntity swingingEntity, int type, IEntity target) { + public AttackEvent(IItemCustomizable item, IEntity swingingEntity, int type, IEntity target) { super(item); this.type = type; this.target = target; @@ -207,7 +208,7 @@ public class ItemEvent extends CustomNPCsEvent implements IItemEvent { public final IPlayer player; public final int duration; - public StartUsingItem(IItemCustom item, IPlayer player, int duration){ + public StartUsingItem(IItemCustomizable item, IPlayer player, int duration){ super(item); this.player = player; this.duration = duration; @@ -231,7 +232,7 @@ public class ItemEvent extends CustomNPCsEvent implements IItemEvent { public final IPlayer player; public final int duration; - public UsingItem(IItemCustom item, IPlayer player, int duration){ + public UsingItem(IItemCustomizable item, IPlayer player, int duration){ super(item); this.player = player; this.duration = duration; @@ -255,7 +256,7 @@ public class ItemEvent extends CustomNPCsEvent implements IItemEvent { public final IPlayer player; public final int duration; - public StopUsingItem(IItemCustom item, IPlayer player, int duration){ + public StopUsingItem(IItemCustomizable item, IPlayer player, int duration){ super(item); this.player = player; this.duration = duration; @@ -279,7 +280,7 @@ public class ItemEvent extends CustomNPCsEvent implements IItemEvent { public final IPlayer player; public final int duration; - public FinishUsingItem(IItemCustom item, IPlayer player, int duration){ + public FinishUsingItem(IItemCustomizable item, IPlayer player, int duration){ super(item); this.player = player; this.duration = duration; From 4a1a092757c0262c25ed98409490b9ebb3e2bd17 Mon Sep 17 00:00:00 2001 From: luisc Date: Fri, 14 Feb 2025 12:28:17 -0600 Subject: [PATCH 11/13] More linked item changes --- src/api | 2 +- src/main/java/noppes/npcs/EventHooks.java | 104 +++++++---- .../java/noppes/npcs/client/ClientProxy.java | 3 +- .../renderer/items/CustomItemRenderer.java | 95 +++++----- .../controllers/LinkedItemController.java | 67 ++++++- .../npcs/controllers/data/EffectScript.java | 10 +- .../npcs/controllers/data/LinkedItem.java | 39 ++++- .../controllers/data/LinkedItemScript.java | 165 ------------------ .../noppes/npcs/items/ItemCustomizable.java | 83 ++++++++- .../java/noppes/npcs/items/ItemLinked.java | 87 --------- .../java/noppes/npcs/items/ItemScripted.java | 81 --------- .../java/noppes/npcs/scripted/NpcAPI.java | 10 +- .../npcs/scripted/item/ScriptCustomItem.java | 5 + .../npcs/scripted/item/ScriptItemStack.java | 13 ++ .../npcs/scripted/item/ScriptLinkedItem.java | 10 +- 15 files changed, 338 insertions(+), 436 deletions(-) delete mode 100644 src/main/java/noppes/npcs/controllers/data/LinkedItemScript.java diff --git a/src/api b/src/api index d093c0049..232da0325 160000 --- a/src/api +++ b/src/api @@ -1 +1 @@ -Subproject commit d093c00493d4dfd82f2f250008432ca6536c4529 +Subproject commit 232da0325b4c8b10c4ebc3e5f726e3cfd2df86fb diff --git a/src/main/java/noppes/npcs/EventHooks.java b/src/main/java/noppes/npcs/EventHooks.java index 43e6d570f..a5875a6cd 100644 --- a/src/main/java/noppes/npcs/EventHooks.java +++ b/src/main/java/noppes/npcs/EventHooks.java @@ -20,7 +20,7 @@ import noppes.npcs.api.entity.IPlayer; import noppes.npcs.api.entity.IProjectile; import noppes.npcs.api.gui.ICustomGui; import noppes.npcs.api.gui.IItemSlot; -import noppes.npcs.api.item.IItemCustom; +import noppes.npcs.api.item.IItemCustomizable; import noppes.npcs.api.item.IItemStack; import noppes.npcs.constants.EnumScriptType; import noppes.npcs.controllers.CustomGuiController; @@ -41,76 +41,108 @@ public class EventHooks { public EventHooks() { } - public static void onScriptItemInit(IItemCustom handler) { - if (!((ScriptCustomItem) handler).isClient()) { - ItemEvent.InitEvent event = new ItemEvent.InitEvent(handler); - ((ScriptCustomItem) handler).callScript(EnumScriptType.INIT, event); + public static void onScriptItemInit(IItemCustomizable item) { + INpcScriptHandler handler = item.getScriptHandler(); + if (handler != null && !handler.isClient()) { + ItemEvent.InitEvent event = new ItemEvent.InitEvent(item); + handler.callScript(EnumScriptType.INIT, event); NpcAPI.EVENT_BUS.post(event); } } - public static void onScriptItemUpdate(IItemCustom handler, EntityLivingBase player) { - if (!((ScriptCustomItem) handler).isClient()) { - ItemEvent.UpdateEvent event = new ItemEvent.UpdateEvent(handler, NpcAPI.Instance().getIEntity(player)); - ((ScriptCustomItem) handler).callScript(EnumScriptType.TICK, event); + public static void onScriptItemUpdate(IItemCustomizable item, EntityLivingBase player) { + INpcScriptHandler handler = item.getScriptHandler(); + if (handler != null && !handler.isClient()) { + ItemEvent.UpdateEvent event = new ItemEvent.UpdateEvent(item, NpcAPI.Instance().getIEntity(player)); + handler.callScript(EnumScriptType.TICK, event); NpcAPI.EVENT_BUS.post(event); } } - public static boolean onScriptItemTossed(IItemCustom handler, EntityPlayer player, EntityItem entity) { - ItemEvent.TossedEvent event = new ItemEvent.TossedEvent(handler, (IPlayer)NpcAPI.Instance().getIEntity(player), NpcAPI.Instance().getIEntity(entity)); - ((ScriptCustomItem) handler).callScript(EnumScriptType.TOSSED, event); + public static boolean onScriptItemTossed(IItemCustomizable item, EntityPlayer player, EntityItem entity) { + INpcScriptHandler handler = item.getScriptHandler(); + ItemEvent.TossedEvent event = new ItemEvent.TossedEvent(item, (IPlayer)NpcAPI.Instance().getIEntity(player), NpcAPI.Instance().getIEntity(entity)); + if (handler != null) { + handler.callScript(EnumScriptType.TOSSED, event); + } return NpcAPI.EVENT_BUS.post(event); } - public static boolean onScriptItemPickedUp(IItemCustom handler, EntityPlayer player) { - ItemEvent.PickedUpEvent event = new ItemEvent.PickedUpEvent(handler, (IPlayer)NpcAPI.Instance().getIEntity(player)); - ((ScriptCustomItem) handler).callScript(EnumScriptType.PICKEDUP, event); + public static boolean onScriptItemPickedUp(IItemCustomizable item, EntityPlayer player) { + INpcScriptHandler handler = item.getScriptHandler(); + ItemEvent.PickedUpEvent event = new ItemEvent.PickedUpEvent(item, (IPlayer)NpcAPI.Instance().getIEntity(player)); + if (handler != null) { + handler.callScript(EnumScriptType.PICKEDUP, event); + } return NpcAPI.EVENT_BUS.post(event); } - public static boolean onScriptItemSpawn(IItemCustom handler, EntityItem entity) { - ItemEvent.SpawnEvent event = new ItemEvent.SpawnEvent(handler, NpcAPI.Instance().getIEntity(entity)); - ((ScriptCustomItem) handler).callScript(EnumScriptType.SPAWN, event); + public static boolean onScriptItemSpawn(IItemCustomizable item, EntityItem entity) { + INpcScriptHandler handler = item.getScriptHandler(); + ItemEvent.SpawnEvent event = new ItemEvent.SpawnEvent(item, NpcAPI.Instance().getIEntity(entity)); + if (handler != null) { + handler.callScript(EnumScriptType.SPAWN, event); + } return NpcAPI.EVENT_BUS.post(event); } - public static boolean onScriptItemInteract(IItemCustom handler, noppes.npcs.scripted.event.ItemEvent.InteractEvent event) { - ((ScriptCustomItem) handler).callScript(EnumScriptType.INTERACT, event); + public static boolean onScriptItemInteract(IItemCustomizable item, noppes.npcs.scripted.event.ItemEvent.InteractEvent event) { + INpcScriptHandler handler = item.getScriptHandler(); + if (handler != null) { + handler.callScript(EnumScriptType.INTERACT, event); + } return NpcAPI.EVENT_BUS.post(event); } - public static boolean onScriptItemRightClick(IItemCustom handler, noppes.npcs.scripted.event.ItemEvent.RightClickEvent event) { - ((ScriptCustomItem) handler).callScript(EnumScriptType.RIGHT_CLICK, event); + public static boolean onScriptItemRightClick(IItemCustomizable item, noppes.npcs.scripted.event.ItemEvent.RightClickEvent event) { + INpcScriptHandler handler = item.getScriptHandler(); + if (handler != null) { + handler.callScript(EnumScriptType.RIGHT_CLICK, event); + } return NpcAPI.EVENT_BUS.post(event); } - public static boolean onScriptItemAttack(IItemCustom handler, noppes.npcs.scripted.event.ItemEvent.AttackEvent event) { - ((ScriptCustomItem) handler).callScript(EnumScriptType.ATTACK, event); + public static boolean onScriptItemAttack(IItemCustomizable item, noppes.npcs.scripted.event.ItemEvent.AttackEvent event) { + INpcScriptHandler handler = item.getScriptHandler(); + if (handler != null) { + handler.callScript(EnumScriptType.ATTACK, event); + } return NpcAPI.EVENT_BUS.post(event); } - public static boolean onStartUsingCustomItem(IItemCustom handler, IPlayer player, int duration) { - ItemEvent.StartUsingItem event = new ItemEvent.StartUsingItem(handler, player, duration); - ((ScriptCustomItem) handler).callScript(EnumScriptType.START_USING_ITEM, event); + public static boolean onStartUsingCustomItem(IItemCustomizable item, IPlayer player, int duration) { + INpcScriptHandler handler = item.getScriptHandler(); + ItemEvent.StartUsingItem event = new ItemEvent.StartUsingItem(item, player, duration); + if (handler != null) { + handler.callScript(EnumScriptType.START_USING_ITEM, event); + } return NpcAPI.EVENT_BUS.post(event); } - public static boolean onUsingCustomItem(IItemCustom handler, IPlayer player, int duration) { - ItemEvent.UsingItem event = new ItemEvent.UsingItem(handler, player, duration); - ((ScriptCustomItem) handler).callScript(EnumScriptType.USING_ITEM, event); + public static boolean onUsingCustomItem(IItemCustomizable item, IPlayer player, int duration) { + INpcScriptHandler handler = item.getScriptHandler(); + ItemEvent.UsingItem event = new ItemEvent.UsingItem(item, player, duration); + if (handler != null) { + handler.callScript(EnumScriptType.USING_ITEM, event); + } return NpcAPI.EVENT_BUS.post(event); } - public static boolean onStopUsingCustomItem(IItemCustom handler, IPlayer player, int duration) { - ItemEvent.StopUsingItem event = new ItemEvent.StopUsingItem(handler, player, duration); - ((ScriptCustomItem) handler).callScript(EnumScriptType.STOP_USING_ITEM, event); + public static boolean onStopUsingCustomItem(IItemCustomizable item, IPlayer player, int duration) { + INpcScriptHandler handler = item.getScriptHandler(); + ItemEvent.StopUsingItem event = new ItemEvent.StopUsingItem(item, player, duration); + if (handler != null) { + handler.callScript(EnumScriptType.STOP_USING_ITEM, event); + } return NpcAPI.EVENT_BUS.post(event); } - public static boolean onFinishUsingCustomItem(IItemCustom handler, IPlayer player, int duration) { - ItemEvent.FinishUsingItem event = new ItemEvent.FinishUsingItem(handler, player, duration); - ((ScriptCustomItem) handler).callScript(EnumScriptType.FINISH_USING_ITEM, event); + public static boolean onFinishUsingCustomItem(IItemCustomizable item, IPlayer player, int duration) { + INpcScriptHandler handler = item.getScriptHandler(); + ItemEvent.FinishUsingItem event = new ItemEvent.FinishUsingItem(item, player, duration); + if (handler != null) { + handler.callScript(EnumScriptType.FINISH_USING_ITEM, event); + } return NpcAPI.EVENT_BUS.post(event); } diff --git a/src/main/java/noppes/npcs/client/ClientProxy.java b/src/main/java/noppes/npcs/client/ClientProxy.java index 1e27b7cec..ebf8fc0d3 100644 --- a/src/main/java/noppes/npcs/client/ClientProxy.java +++ b/src/main/java/noppes/npcs/client/ClientProxy.java @@ -63,6 +63,7 @@ import noppes.npcs.controllers.data.PlayerData; import noppes.npcs.entity.*; import noppes.npcs.entity.data.ModelData; import noppes.npcs.entity.data.ModelPartData; +import noppes.npcs.items.ItemLinked; import noppes.npcs.items.ItemScripted; import org.lwjgl.input.Keyboard; import tconstruct.client.tabs.InventoryTabCustomNpc; @@ -507,7 +508,7 @@ public class ClientProxy extends CommonProxy { @Override public void registerItem(Item item) { - if (item instanceof ItemScripted) { + if (item instanceof ItemScripted || item instanceof ItemLinked) { MinecraftForgeClient.registerItemRenderer(item, new CustomItemRenderer()); } else { MinecraftForgeClient.registerItemRenderer(item, new NpcItemRenderer()); diff --git a/src/main/java/noppes/npcs/client/renderer/items/CustomItemRenderer.java b/src/main/java/noppes/npcs/client/renderer/items/CustomItemRenderer.java index 7b871267a..bf7263356 100644 --- a/src/main/java/noppes/npcs/client/renderer/items/CustomItemRenderer.java +++ b/src/main/java/noppes/npcs/client/renderer/items/CustomItemRenderer.java @@ -14,10 +14,12 @@ import net.minecraft.util.IIcon; import net.minecraft.util.MathHelper; import net.minecraft.util.ResourceLocation; import net.minecraftforge.client.IItemRenderer; +import noppes.npcs.api.item.IItemCustomizable; +import noppes.npcs.api.item.IItemStack; import noppes.npcs.client.ClientCacheHandler; import noppes.npcs.client.renderer.ImageData; import noppes.npcs.items.ItemScripted; -import noppes.npcs.scripted.item.ScriptCustomItem; +import noppes.npcs.scripted.NpcAPI; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL12; @@ -44,9 +46,13 @@ public class CustomItemRenderer implements IItemRenderer { @Override public void renderItem(ItemRenderType type, ItemStack itemStack, Object... data) { - ScriptCustomItem scriptCustomItem = ItemScripted.GetWrapper(itemStack); + IItemStack iItemStack = NpcAPI.Instance().getIItemStack(itemStack); + if (!(iItemStack instanceof IItemCustomizable)) { + return; + } - ImageData imageData = ClientCacheHandler.getImageData(scriptCustomItem.itemDisplay.texture); + IItemCustomizable scriptCustomItem = (IItemCustomizable) iItemStack; + ImageData imageData = ClientCacheHandler.getImageData(scriptCustomItem.getTexture()); if (!imageData.imageLoaded()) { return; } @@ -64,34 +70,34 @@ public class CustomItemRenderer implements IItemRenderer { if (!entityItem.ignoreFrustumCheck) { entityItem.ignoreFrustumCheck = true; - float entityXZSize = (float) Math.sqrt(Math.pow(scriptCustomItem.itemDisplay.scaleX,2) + Math.pow(scriptCustomItem.itemDisplay.scaleZ,2)); - this.setEntitySize(entityItem, entityXZSize * 0.25F, scriptCustomItem.itemDisplay.scaleY * 0.25F); + float entityXZSize = (float) Math.sqrt(Math.pow(scriptCustomItem.getScaleX(),2) + Math.pow(scriptCustomItem.getScaleZ(),2)); + this.setEntitySize(entityItem, entityXZSize * 0.25F, scriptCustomItem.getScaleY() * 0.25F); double XSize = entityItem.boundingBox.maxX - entityItem.boundingBox.minX; double YSize = entityItem.boundingBox.maxY - entityItem.boundingBox.minY; double ZSize = entityItem.boundingBox.maxZ - entityItem.boundingBox.minZ; - entityItem.boundingBox.maxX = entityItem.boundingBox.minX + XSize * scriptCustomItem.itemDisplay.scaleX; - entityItem.boundingBox.maxY = entityItem.boundingBox.minY + YSize * scriptCustomItem.itemDisplay.scaleY; - entityItem.boundingBox.maxZ = entityItem.boundingBox.minZ + ZSize * scriptCustomItem.itemDisplay.scaleZ; + entityItem.boundingBox.maxX = entityItem.boundingBox.minX + XSize * scriptCustomItem.getScaleX(); + entityItem.boundingBox.maxY = entityItem.boundingBox.minY + YSize * scriptCustomItem.getScaleY(); + entityItem.boundingBox.maxZ = entityItem.boundingBox.minZ + ZSize * scriptCustomItem.getScaleZ(); } float entityRenderTicks = Minecraft.getMinecraft().timer.renderPartialTicks; float bobbing = MathHelper.sin(((float)entityItem.age + entityRenderTicks) / 10.0F + entityItem.hoverStart) * 0.1F + 0.1F; - if(!scriptCustomItem.isNormalItem){ - GL11.glRotatef(scriptCustomItem.itemDisplay.rotationX, 1, 0, 0); - GL11.glRotatef(scriptCustomItem.itemDisplay.rotationY, 0, 1, 0); - GL11.glRotatef(scriptCustomItem.itemDisplay.rotationZ, 0, 0, 1); + if(!scriptCustomItem.isNormalItem()){ + GL11.glRotatef(scriptCustomItem.getRotationX(), 1, 0, 0); + GL11.glRotatef(scriptCustomItem.getRotationY(), 0, 1, 0); + GL11.glRotatef(scriptCustomItem.getRotationZ(), 0, 0, 1); } - GL11.glRotatef(scriptCustomItem.itemDisplay.rotationXRate * entityRenderTicks %360, 1, 0, 0); - GL11.glRotatef(scriptCustomItem.itemDisplay.rotationYRate * entityRenderTicks %360, 0, 1, 0); - GL11.glRotatef(scriptCustomItem.itemDisplay.rotationZRate * entityRenderTicks %360, 0, 0, 1); + GL11.glRotatef(scriptCustomItem.getRotationXRate() * entityRenderTicks %360, 1, 0, 0); + GL11.glRotatef(scriptCustomItem.getRotationYRate() * entityRenderTicks %360, 0, 1, 0); + GL11.glRotatef(scriptCustomItem.getRotationZRate() * entityRenderTicks %360, 0, 0, 1); if (!renderInFrame) { - if(!scriptCustomItem.isNormalItem){ - GL11.glScalef(scriptCustomItem.itemDisplay.scaleX, scriptCustomItem.itemDisplay.scaleY, scriptCustomItem.itemDisplay.scaleZ); - GL11.glTranslatef(0.0F, (Math.max(scriptCustomItem.itemDisplay.scaleY, 1) - 1) * (1.0F / 4), 0.0F); + if(!scriptCustomItem.isNormalItem()){ + GL11.glScalef(scriptCustomItem.getScaleX(), scriptCustomItem.getScaleY(), scriptCustomItem.getScaleZ()); + GL11.glTranslatef(0.0F, (Math.max(scriptCustomItem.getScaleY(), 1) - 1) * (1.0F / 4), 0.0F); } GL11.glTranslatef(0.0F, -bobbing, 0.0F); } @@ -108,8 +114,7 @@ public class CustomItemRenderer implements IItemRenderer { return; } - if (type == ItemRenderType.EQUIPPED_FIRST_PERSON && scriptCustomItem.isNormalItem) { - + if (type == ItemRenderType.EQUIPPED_FIRST_PERSON && scriptCustomItem.isNormalItem()) { GL11.glPushMatrix(); GL11.glTranslatef(0.9375F, 0.0625F, 0.0F); GL11.glRotatef(-315.0F, 0.0F, 0.0F, 1.0F); @@ -138,29 +143,29 @@ public class CustomItemRenderer implements IItemRenderer { GL11.glRotatef(-315.0F, 0.0F, 0.0F, 1.0F); ((ItemScripted) itemStack.getItem()).renderOffset(scriptCustomItem); - if(scriptCustomItem.isNormalItem){ + if(scriptCustomItem.isNormalItem()){ GL11.glTranslatef(-0.05F, 0.3F, 0.3F); GL11.glRotatef(50, 1, 0, 0); GL11.glRotatef(-80, 0, 1, 0); GL11.glRotatef(80, 0, 0, 1); - GL11.glRotatef(scriptCustomItem.itemDisplay.rotationXRate * item3dRenderTicks %360, 1, 0, 0); - GL11.glRotatef(scriptCustomItem.itemDisplay.rotationYRate * item3dRenderTicks %360, 0, 1, 0); - GL11.glRotatef(scriptCustomItem.itemDisplay.rotationZRate * item3dRenderTicks %360, 0, 0, 1); + GL11.glRotatef(scriptCustomItem.getRotationXRate() * item3dRenderTicks %360, 1, 0, 0); + GL11.glRotatef(scriptCustomItem.getRotationYRate() * item3dRenderTicks %360, 0, 1, 0); + GL11.glRotatef(scriptCustomItem.getRotationZRate() * item3dRenderTicks %360, 0, 0, 1); GL11.glScalef(0.6F, 0.6F,0.6F); } else { - GL11.glTranslatef(scriptCustomItem.itemDisplay.translateX, scriptCustomItem.itemDisplay.translateY, scriptCustomItem.itemDisplay.translateZ); - GL11.glRotatef(scriptCustomItem.itemDisplay.rotationX, 1, 0, 0); - GL11.glRotatef(scriptCustomItem.itemDisplay.rotationY, 0, 1, 0); - GL11.glRotatef(scriptCustomItem.itemDisplay.rotationZ, 0, 0, 1); + GL11.glTranslatef(scriptCustomItem.getTranslateX(), scriptCustomItem.getTranslateY(), scriptCustomItem.getTranslateZ()); + GL11.glRotatef(scriptCustomItem.getRotationX(), 1, 0, 0); + GL11.glRotatef(scriptCustomItem.getRotationY(), 0, 1, 0); + GL11.glRotatef(scriptCustomItem.getRotationZ(), 0, 0, 1); - GL11.glRotatef(scriptCustomItem.itemDisplay.rotationXRate * item3dRenderTicks %360, 1, 0, 0); - GL11.glRotatef(scriptCustomItem.itemDisplay.rotationYRate * item3dRenderTicks %360, 0, 1, 0); - GL11.glRotatef(scriptCustomItem.itemDisplay.rotationZRate * item3dRenderTicks %360, 0, 0, 1); + GL11.glRotatef(scriptCustomItem.getRotationXRate() * item3dRenderTicks %360, 1, 0, 0); + GL11.glRotatef(scriptCustomItem.getRotationYRate() * item3dRenderTicks %360, 0, 1, 0); + GL11.glRotatef(scriptCustomItem.getRotationZRate() * item3dRenderTicks %360, 0, 0, 1); - GL11.glScalef(scriptCustomItem.itemDisplay.scaleX, scriptCustomItem.itemDisplay.scaleY, scriptCustomItem.itemDisplay.scaleZ); + GL11.glScalef(scriptCustomItem.getScaleX(), scriptCustomItem.getScaleY(), scriptCustomItem.getScaleZ()); } int color = scriptCustomItem.getColor(); @@ -226,11 +231,11 @@ public class CustomItemRenderer implements IItemRenderer { } } - public void renderEntityCustomItem(ScriptCustomItem scriptCustomItem, ItemStack itemStack, EntityItem entityItem) { + public void renderEntityCustomItem(IItemCustomizable scriptCustomItem, ItemStack itemStack, EntityItem entityItem) { int pass = 0; GL11.glPushMatrix(); - ClientCacheHandler.getImageData(scriptCustomItem.itemDisplay.texture).bindTexture(); + ClientCacheHandler.getImageData(scriptCustomItem.getTexture()).bindTexture(); if (renderInFrame) { @@ -276,8 +281,8 @@ public class CustomItemRenderer implements IItemRenderer { b0 = 4; } - if(!scriptCustomItem.isNormalItem) - GL11.glScalef(scriptCustomItem.itemDisplay.scaleX, scriptCustomItem.itemDisplay.scaleY, scriptCustomItem.itemDisplay.scaleZ); + if(!scriptCustomItem.isNormalItem()) + GL11.glScalef(scriptCustomItem.getScaleX(), scriptCustomItem.getScaleY(), scriptCustomItem.getScaleZ()); GL11.glTranslatef(-f7, -f8, -((f9 + f10) * (float)b0 / 2.0F)); for (int k = 0; k < b0; ++k) @@ -289,7 +294,7 @@ public class CustomItemRenderer implements IItemRenderer { GL11.glRotatef(-90.0F, 0.0F, 0.0F, 1.0F); }*/ - ImageData imageData = ClientCacheHandler.getImageData(scriptCustomItem.itemDisplay.texture); + ImageData imageData = ClientCacheHandler.getImageData(scriptCustomItem.getTexture()); ItemRenderer.renderItemIn2D(tessellator, f15, f4, f14, f5, imageData.getTotalWidth(), imageData.getTotalHeight(), f9); if (itemStack.hasEffect(pass)) @@ -378,7 +383,7 @@ public class CustomItemRenderer implements IItemRenderer { GL11.glPopMatrix(); } - public void renderInventoryCustomItem(ScriptCustomItem scriptCustomItem) { + public void renderInventoryCustomItem(IItemCustomizable scriptCustomItem) { GL11.glPushMatrix(); int color = scriptCustomItem.getColor(); float itemRed = (color >> 16 & 255) / 255f; @@ -389,13 +394,13 @@ public class CustomItemRenderer implements IItemRenderer { GL11.glDisable(GL11.GL_LIGHTING); //Forge: Make sure that render states are reset, a renderEffect can derp them up. GL11.glEnable(GL11.GL_ALPHA_TEST); - ClientCacheHandler.getImageData(scriptCustomItem.itemDisplay.texture).bindTexture(); + ClientCacheHandler.getImageData(scriptCustomItem.getTexture()).bindTexture(); renderCustomItemSlot(0,0,16,16, itemRed, itemGreen, itemBlue); GL11.glEnable(GL11.GL_LIGHTING); GL11.glDisable(GL11.GL_ALPHA_TEST); - if (scriptCustomItem.item.hasEffect(0)) + if (scriptCustomItem.getMCItemStack().hasEffect(0)) { renderEffect(Minecraft.getMinecraft().getTextureManager(), 0, 0); } @@ -457,14 +462,14 @@ public class CustomItemRenderer implements IItemRenderer { tessellator.draw(); } - public void renderItem3d(ScriptCustomItem scriptCustomItem, EntityLivingBase entityLivingBase, ItemStack itemStack) { + public void renderItem3d(IItemCustomizable scriptCustomItem, EntityLivingBase entityLivingBase, ItemStack itemStack) { item3dRenderTicks++; Minecraft mc = Minecraft.getMinecraft(); TextureManager texturemanager = mc.getTextureManager(); int par3 = 0; - ClientCacheHandler.getImageData(scriptCustomItem.itemDisplay.texture).bindTexture(); + ClientCacheHandler.getImageData(scriptCustomItem.getTexture()).bindTexture(); Tessellator tessellator = Tessellator.instance; IIcon icon = entityLivingBase.getItemIcon(itemStack, par3); @@ -482,7 +487,7 @@ public class CustomItemRenderer implements IItemRenderer { GL11.glRotatef(335.0F, 0.0F, 0.0F, 1.0F); GL11.glTranslatef(-0.9375F, -0.0625F, 0.0F); - renderCustomItemIn2D(itemStack, tessellator, 1.0F, 0.0F, 0.0F, 1.0F, 0.0625F); + renderCustomItemIn2D(scriptCustomItem, tessellator, 1.0F, 0.0F, 0.0F, 1.0F, 0.0625F); if (itemStack.hasEffect(par3)) { GL11.glDepthFunc(GL11.GL_EQUAL); @@ -517,9 +522,9 @@ public class CustomItemRenderer implements IItemRenderer { GL11.glDisable(GL12.GL_RESCALE_NORMAL); } - public static void renderCustomItemIn2D(ItemStack itemStack, Tessellator p_78439_0_, float p_78439_1_, float p_78439_2_, float p_78439_3_, float p_78439_4_, float p_78439_7_) + public static void renderCustomItemIn2D(IItemCustomizable wrapper, Tessellator p_78439_0_, float p_78439_1_, float p_78439_2_, float p_78439_3_, float p_78439_4_, float p_78439_7_) { - ImageData imageData = ClientCacheHandler.getImageData(ItemScripted.GetWrapper(itemStack).itemDisplay.texture); + ImageData imageData = ClientCacheHandler.getImageData(wrapper.getTexture()); int width = imageData.getTotalWidth(); int height = imageData.getTotalHeight(); diff --git a/src/main/java/noppes/npcs/controllers/LinkedItemController.java b/src/main/java/noppes/npcs/controllers/LinkedItemController.java index 4c721ce7e..da82ba343 100644 --- a/src/main/java/noppes/npcs/controllers/LinkedItemController.java +++ b/src/main/java/noppes/npcs/controllers/LinkedItemController.java @@ -1,13 +1,22 @@ package noppes.npcs.controllers; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import noppes.npcs.CustomNpcs; +import noppes.npcs.controllers.data.EffectScript; +import noppes.npcs.controllers.data.INpcScriptHandler; import noppes.npcs.controllers.data.LinkedItem; +import java.io.File; import java.util.HashMap; -//TODO: load this data from somewhere, use in GUIs, create script wrapper +//TODO: load this data from somewhere, use in GUIs public class LinkedItemController { private static LinkedItemController Instance; - private final HashMap linkedItems = new HashMap<>(); + private int lastUsedID = 0; + + private final HashMap linkedItems = new HashMap<>(); + private final HashMap linkedItemsScripts = new HashMap<>(); private LinkedItemController() { } @@ -19,16 +28,64 @@ public class LinkedItemController { return Instance; } + public int getUnusedId() { + for (int id : this.linkedItems.keySet()) { + if (id > this.lastUsedID) + this.lastUsedID = id; + } + this.lastUsedID++; + return this.lastUsedID; + } + + private File getDir() { + return new File(CustomNpcs.getWorldSaveDirectory(), "linkeditems"); + } + + public LinkedItem createItem(String name) { + return new LinkedItem(name); + } + public void add(LinkedItem linkedItem) { if (linkedItem != null) { String name = linkedItem.getName(); if (name != null && !name.isEmpty()) { - this.linkedItems.put(name.toLowerCase(), linkedItem); + int linkedItemId = linkedItem.getId(); + int id = this.linkedItems.containsKey(linkedItemId) ? linkedItemId : this.getUnusedId(); + this.linkedItems.put(id, linkedItem); + linkedItem.setId(id); + this.addScript(id); } } } - public LinkedItem get(String name) { - return name != null && !name.isEmpty() ? this.linkedItems.get(name.toLowerCase()) : null; + @SideOnly(Side.SERVER) + private void addScript(int id) { + this.linkedItemsScripts.put(id, new EffectScript()); + } + + public void remove(int id) { + this.linkedItems.remove(id); + this.removeScript(id); + } + + @SideOnly(Side.SERVER) + private void removeScript(int id) { + this.linkedItemsScripts.remove(id); + } + + public LinkedItem get(int id) { + return this.linkedItems.get(id); + } + + public boolean contains(int id) { + return this.linkedItems.containsKey(id); + } + + public boolean contains(LinkedItem linkedItem) { + return this.linkedItems.containsValue(linkedItem); + } + + public INpcScriptHandler getScriptHandler(int id) { + return this.linkedItemsScripts.get(id); } } diff --git a/src/main/java/noppes/npcs/controllers/data/EffectScript.java b/src/main/java/noppes/npcs/controllers/data/EffectScript.java index 45dda2e9b..ec58bbfe8 100644 --- a/src/main/java/noppes/npcs/controllers/data/EffectScript.java +++ b/src/main/java/noppes/npcs/controllers/data/EffectScript.java @@ -7,6 +7,7 @@ import kamkeel.npcs.util.ByteBufUtils; import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.common.util.Constants; import noppes.npcs.config.ConfigScript; +import noppes.npcs.constants.EnumScriptType; import noppes.npcs.controllers.ScriptContainer; import noppes.npcs.controllers.ScriptController; import noppes.npcs.scripted.NpcAPI; @@ -16,7 +17,7 @@ import javax.script.ScriptEngine; import java.io.IOException; import java.util.*; -public class EffectScript implements IScriptHandler { +public class EffectScript implements INpcScriptHandler { public ScriptContainer container; public String scriptLanguage = "ECMAScript"; public boolean enabled = false; @@ -50,7 +51,12 @@ public class EffectScript implements IScriptHandler { return this.enabled && ScriptController.HasStart && container != null && ConfigScript.ScriptingEnabled; } - @Override + @Override + public void callScript(EnumScriptType type, Event event) { + this.callScript(type.function, event); + } + + @Override public void callScript(String s, Event event) { if (!this.isEnabled()) { return; diff --git a/src/main/java/noppes/npcs/controllers/data/LinkedItem.java b/src/main/java/noppes/npcs/controllers/data/LinkedItem.java index 8eb28ec70..2776298b3 100644 --- a/src/main/java/noppes/npcs/controllers/data/LinkedItem.java +++ b/src/main/java/noppes/npcs/controllers/data/LinkedItem.java @@ -2,13 +2,15 @@ package noppes.npcs.controllers.data; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.nbt.NBTTagString; +import net.minecraft.nbt.NBTTagInt; import noppes.npcs.CustomItems; +import noppes.npcs.controllers.LinkedItemController; public class LinkedItem { - public static final String LINKED_NBT_TAG = "LinkedName"; + public static final String LINKED_NBT_TAG = "LinkedId"; - private final String name; + private int id = -1; + private String name; public final ItemDisplayData display = new ItemDisplayData(); public double durabilityValue = 1.0D; @@ -23,22 +25,44 @@ public class LinkedItem { public int armorType = -2; //-2: Fits in no armor slot, -1: Fits in all slots, 0 - 4: Fits in Head -> Boots slot respectively public int enchantability; + public LinkedItem() { + this("New"); + } + public LinkedItem(String name) { this.name = name; } + public int getId() { + return this.id; + } + + public void setId(int id) { + this.id = id; + } + public String getName() { return this.name; } public ItemStack createStack() { - ItemStack linkedStack = new ItemStack(CustomItems.linked_item); - linkedStack.setTagInfo(LinkedItem.LINKED_NBT_TAG, new NBTTagString(this.getName())); - return linkedStack; + if (LinkedItemController.Instance().contains(this)) { + ItemStack linkedStack = new ItemStack(CustomItems.linked_item); + linkedStack.setTagInfo(LinkedItem.LINKED_NBT_TAG, new NBTTagInt(this.getId())); + return linkedStack; + } + return null; + } + + public INpcScriptHandler getScriptHandler() { + return LinkedItemController.Instance().getScriptHandler(this.getId()); } public NBTTagCompound writeToNBT() { NBTTagCompound compound = new NBTTagCompound(); + compound.setInteger("Id", this.id); + compound.setString("Name", this.name); + compound.setTag("Display", this.display.writeToNBT()); compound.setDouble("DurabilityValue",this.durabilityValue); @@ -56,6 +80,9 @@ public class LinkedItem { } public void readFromNBT(NBTTagCompound compound) { + this.id = compound.hasKey("Id") ? compound.getInteger("Id") : LinkedItemController.Instance().getUnusedId(); + this.name = compound.getString("Name"); + this.display.readFromNBT(compound.getCompoundTag("Display")); this.durabilityValue = compound.getDouble("DurabilityValue"); diff --git a/src/main/java/noppes/npcs/controllers/data/LinkedItemScript.java b/src/main/java/noppes/npcs/controllers/data/LinkedItemScript.java deleted file mode 100644 index 4fd57dba3..000000000 --- a/src/main/java/noppes/npcs/controllers/data/LinkedItemScript.java +++ /dev/null @@ -1,165 +0,0 @@ -package noppes.npcs.controllers.data; - -import cpw.mods.fml.common.FMLCommonHandler; -import cpw.mods.fml.common.eventhandler.Event; -import io.netty.buffer.ByteBuf; -import kamkeel.npcs.util.ByteBufUtils; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraftforge.common.util.Constants; -import noppes.npcs.config.ConfigScript; -import noppes.npcs.controllers.ScriptContainer; -import noppes.npcs.controllers.ScriptController; -import noppes.npcs.scripted.NpcAPI; - -import javax.script.ScriptEngine; -import java.io.IOException; -import java.util.*; - -public class LinkedItemScript implements IScriptHandler { - public ScriptContainer container; - public String scriptLanguage = "ECMAScript"; - public boolean enabled = false; - - - public NBTTagCompound writeToNBT(NBTTagCompound compound) { - compound.setString("ScriptLanguage", scriptLanguage); - compound.setBoolean("ScriptEnabled", enabled); - - if (container != null) - compound.setTag("ScriptContent", container.writeToNBT(new NBTTagCompound())); - return compound; - } - - public LinkedItemScript readFromNBT(NBTTagCompound compound) { - scriptLanguage = compound.getString("ScriptLanguage"); - enabled = compound.getBoolean("ScriptEnabled"); - - if (compound.hasKey("ScriptContent", Constants.NBT.TAG_COMPOUND)) { - container = new ScriptContainer(this); - container.readFromNBT(compound.getCompoundTag("ScriptContent")); - } - return this; - } - - public boolean isEnabled() { - return this.enabled && ScriptController.HasStart && container != null && ConfigScript.ScriptingEnabled; - } - - @Override - public void callScript(String s, Event event) { - if (!this.isEnabled()) { - return; - } - - ScriptEngine engine = container.engine; - engine.put("API", NpcAPI.Instance()); - - for (Map.Entry engineObjects : NpcAPI.engineObjects.entrySet()) { - engine.put(engineObjects.getKey(), engineObjects.getValue()); - } - - container.run(s, event); - } - - @Override - public boolean isClient() { - return FMLCommonHandler.instance().getEffectiveSide().isClient(); - } - - @Override - public boolean getEnabled() { - return this.enabled; - } - - @Override - public void setEnabled(boolean b) { - this.enabled = b; - } - - @Override - public String getLanguage() { - return this.scriptLanguage; - } - - @Override - public void setLanguage(String s) { - this.scriptLanguage = s; - } - - @Override - public void setScripts(List list) { - if (list == null || list.isEmpty()) { - container = null; - return; - } - container = list.get(0); - } - - @Override - public List getScripts() { - if (container == null) - return new ArrayList<>(); - return Collections.singletonList(container); - } - - @Override - public String noticeString() { - return ""; - } - - @Override - public Map getConsoleText() { - TreeMap map = new TreeMap(); - int tab = 0; - Iterator var3 = this.getScripts().iterator(); - - while(var3.hasNext()) { - ScriptContainer script = (ScriptContainer)var3.next(); - ++tab; - Iterator var5 = script.console.entrySet().iterator(); - - while(var5.hasNext()) { - Map.Entry longStringEntry = (Map.Entry)var5.next(); - map.put(longStringEntry.getKey(), " tab " + tab + ":\n" + (String)longStringEntry.getValue()); - } - } - - return map; - } - - @Override - public void clearConsole() { - Iterator var1 = this.getScripts().iterator(); - - while(var1.hasNext()) { - ScriptContainer script = (ScriptContainer)var1.next(); - script.console.clear(); - } - } - - public void saveScript(ByteBuf buffer) throws IOException { - int tab = buffer.readInt(); - int totalScripts = buffer.readInt(); - if (totalScripts == 0) { - this.container = null; - } - - if (tab == 0) { - NBTTagCompound tabCompound = ByteBufUtils.readNBT(buffer); - ScriptContainer script = new ScriptContainer(this); - script.readFromNBT(tabCompound); - this.container = script; - } else { - NBTTagCompound compound = ByteBufUtils.readNBT(buffer); - this.setLanguage(compound.getString("ScriptLanguage")); - if (!ScriptController.Instance.languages.containsKey(this.getLanguage())) { - if (!ScriptController.Instance.languages.isEmpty()) { - this.setLanguage((String) ScriptController.Instance.languages.keySet().toArray()[0]); - } else { - this.setLanguage("ECMAScript"); - } - } - this.setEnabled(compound.getBoolean("ScriptEnabled")); - } - } -} diff --git a/src/main/java/noppes/npcs/items/ItemCustomizable.java b/src/main/java/noppes/npcs/items/ItemCustomizable.java index 68467cd90..921af8afa 100644 --- a/src/main/java/noppes/npcs/items/ItemCustomizable.java +++ b/src/main/java/noppes/npcs/items/ItemCustomizable.java @@ -3,16 +3,22 @@ package noppes.npcs.items; import cpw.mods.fml.common.registry.GameRegistry; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; +import net.minecraft.block.Block; import net.minecraft.client.renderer.texture.IIconRegister; +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.Items; import net.minecraft.item.EnumAction; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.world.World; +import noppes.npcs.EventHooks; +import noppes.npcs.api.item.IItemCustomizable; import noppes.npcs.api.item.IItemStack; import noppes.npcs.scripted.NpcAPI; -import noppes.npcs.scripted.item.ScriptCustomizableItem; +import noppes.npcs.scripted.event.ItemEvent; +import org.lwjgl.opengl.GL11; public abstract class ItemCustomizable extends Item implements ItemRenderInterface { @@ -26,6 +32,73 @@ public abstract class ItemCustomizable extends Item implements ItemRenderInterfa return 0x8B4513; } + @Override + public boolean onEntitySwing(EntityLivingBase entityLivingBase, ItemStack stack){ + if (entityLivingBase.worldObj.isRemote) { + return false; + } + + IItemStack istack = NpcAPI.Instance().getIItemStack(stack); + ItemEvent.AttackEvent eve = new ItemEvent.AttackEvent((IItemCustomizable) istack, NpcAPI.Instance().getIEntity(entityLivingBase), 2, null); + return EventHooks.onScriptItemAttack((IItemCustomizable) istack, eve); + } + + public int getMaxItemUseDuration(ItemStack stack){ + IItemCustomizable customizable = (IItemCustomizable) NpcAPI.Instance().getIItemStack(stack); + return customizable.getMaxItemUseDuration(); + } + + public boolean showDurabilityBar(ItemStack stack) { + IItemStack istack = NpcAPI.Instance().getIItemStack(stack); + return istack instanceof IItemCustomizable && ((IItemCustomizable) istack).getDurabilityShow(); + } + + public double getDurabilityForDisplay(ItemStack stack) { + + IItemStack istack = NpcAPI.Instance().getIItemStack(stack); + return istack instanceof IItemCustomizable ? 1.0D - ((IItemCustomizable) istack).getDurabilityValue() : 1.0D; + } + + public int getItemStackLimit(ItemStack stack) { + IItemStack istack = NpcAPI.Instance().getIItemStack(stack); + return istack instanceof IItemCustomizable ? istack.getMaxStackSize() : super.getItemStackLimit(stack); + } + + public boolean isItemTool(ItemStack stack) + { + IItemStack istack = NpcAPI.Instance().getIItemStack(stack); + return istack instanceof IItemCustomizable ? ((IItemCustomizable) istack).isTool() : super.isItemTool(stack); + } + + public float getDigSpeed(ItemStack stack, Block block, int metadata) + { + IItemStack istack = NpcAPI.Instance().getIItemStack(stack); + return istack instanceof IItemCustomizable ? ((IItemCustomizable) istack).getDigSpeed() : super.getDigSpeed(stack, block, metadata); + } + + public boolean isValidArmor(ItemStack stack, int armorType, Entity entity) + { + IItemStack istack = NpcAPI.Instance().getIItemStack(stack); + if (istack instanceof IItemCustomizable) { + if(((IItemCustomizable) istack).getArmorType() == -1) + return true; + return armorType == ((IItemCustomizable) istack).getArmorType(); + } + return super.isValidArmor(stack, armorType, entity); + } + + public int getItemEnchantability(ItemStack stack) + { + IItemStack istack = NpcAPI.Instance().getIItemStack(stack); + return istack instanceof IItemCustomizable ? ((IItemCustomizable) istack).getEnchantability() : super.getItemEnchantability(stack); + } + + public boolean hitEntity(ItemStack stack, EntityLivingBase target, EntityLivingBase attacker) { + IItemStack istack = NpcAPI.Instance().getIItemStack(stack); + ItemEvent.AttackEvent eve = new ItemEvent.AttackEvent((IItemCustomizable) istack, NpcAPI.Instance().getIEntity(attacker), 1, NpcAPI.Instance().getIEntity(target)); + return EventHooks.onScriptItemAttack((IItemCustomizable) istack, eve); + } + @Override public boolean requiresMultipleRenderPasses(){ return true; @@ -47,8 +120,8 @@ public abstract class ItemCustomizable extends Item implements ItemRenderInterfa public EnumAction getItemUseAction(ItemStack stack) { IItemStack istack = NpcAPI.Instance().getIItemStack(stack); - if (istack instanceof ScriptCustomizableItem) { - switch (((ScriptCustomizableItem) istack).getItemUseAction()) { + if (istack instanceof IItemCustomizable) { + switch (((IItemCustomizable) istack).getItemUseAction()) { case 0: return EnumAction.none; case 1: @@ -66,4 +139,8 @@ public abstract class ItemCustomizable extends Item implements ItemRenderInterfa @Override public void renderSpecial() {} + + public void renderOffset(IItemCustomizable scriptCustomItem) { + GL11.glTranslatef(0.135F * scriptCustomItem.getScaleX(), 0.2F * scriptCustomItem.getScaleY(), 0.07F * scriptCustomItem.getScaleZ()); + } } diff --git a/src/main/java/noppes/npcs/items/ItemLinked.java b/src/main/java/noppes/npcs/items/ItemLinked.java index fff812e50..cc7b31b35 100644 --- a/src/main/java/noppes/npcs/items/ItemLinked.java +++ b/src/main/java/noppes/npcs/items/ItemLinked.java @@ -1,15 +1,6 @@ package noppes.npcs.items; -import net.minecraft.block.Block; -import net.minecraft.entity.Entity; -import net.minecraft.entity.EntityLivingBase; -import net.minecraft.item.EnumAction; -import net.minecraft.item.ItemStack; import noppes.npcs.CustomNpcs; -import noppes.npcs.api.item.IItemStack; -import noppes.npcs.scripted.NpcAPI; -import noppes.npcs.scripted.item.ScriptLinkedItem; -import org.lwjgl.opengl.GL11; public class ItemLinked extends ItemCustomizable { @@ -18,82 +9,4 @@ public class ItemLinked extends ItemCustomizable { CustomNpcs.proxy.registerItem(this); setHasSubtypes(true); } - - @Override - public boolean onEntitySwing(EntityLivingBase entityLivingBase, ItemStack stack){ - if (entityLivingBase.worldObj.isRemote) { - return false; - } - - //TODO: re-implement item attack swing hook -// IItemStack istack = NpcAPI.Instance().getIItemStack(stack); -// ItemEvent.AttackEvent eve = new ItemEvent.AttackEvent((ScriptLinkedItem) istack, NpcAPI.Instance().getIEntity(entityLivingBase), 2, null); -// return EventHooks.onScriptItemAttack((ScriptLinkedItem) istack, eve); - return true; - } - - public int getMaxItemUseDuration(ItemStack stack){ - return (new ScriptLinkedItem(stack)).getMaxItemUseDuration(); - } - - //TODO: Use this in existing item scripted rendering. Refactor the renderer to use the new abstract ScriptCustomizableItem class - public static ScriptLinkedItem GetWrapper(ItemStack stack) { - return new ScriptLinkedItem(stack); - } - - public boolean showDurabilityBar(ItemStack stack) { - IItemStack istack = NpcAPI.Instance().getIItemStack(stack); - return istack instanceof ScriptLinkedItem && (new ScriptLinkedItem(stack)).itemDisplay.durabilityShow; - } - - public double getDurabilityForDisplay(ItemStack stack) { - IItemStack istack = NpcAPI.Instance().getIItemStack(stack); - return istack instanceof ScriptLinkedItem ? 1.0D - (new ScriptLinkedItem(stack)).getDurabilityValue() : 1.0D; - } - - public int getItemStackLimit(ItemStack stack) { - IItemStack istack = NpcAPI.Instance().getIItemStack(stack); - return istack instanceof ScriptLinkedItem ? (new ScriptLinkedItem(stack)).getMaxStackSize() : super.getItemStackLimit(stack); - } - - public boolean isItemTool(ItemStack stack) - { - IItemStack istack = NpcAPI.Instance().getIItemStack(stack); - return istack instanceof ScriptLinkedItem ? (new ScriptLinkedItem(stack)).isTool() : super.isItemTool(stack); - } - - public float getDigSpeed(ItemStack stack, Block block, int metadata) - { - IItemStack istack = NpcAPI.Instance().getIItemStack(stack); - return istack instanceof ScriptLinkedItem ? (new ScriptLinkedItem(stack)).getDigSpeed() : super.getDigSpeed(stack, block, metadata); - } - - public boolean isValidArmor(ItemStack stack, int armorType, Entity entity) - { - IItemStack istack = NpcAPI.Instance().getIItemStack(stack); - - if((new ScriptLinkedItem(stack)).getArmorType() == -1) - return true; - - return istack instanceof ScriptLinkedItem ? armorType == (new ScriptLinkedItem(stack)).getArmorType() : super.isValidArmor(stack, armorType, entity); - } - - public int getItemEnchantability(ItemStack stack) - { - IItemStack istack = NpcAPI.Instance().getIItemStack(stack); - return istack instanceof ScriptLinkedItem ? (new ScriptLinkedItem(stack)).getEnchantability() : super.getItemEnchantability(stack); - } - - public boolean hitEntity(ItemStack stack, EntityLivingBase target, EntityLivingBase attacker) { - IItemStack istack = NpcAPI.Instance().getIItemStack(stack); - - //TODO: re-implement item attack hook -// ItemEvent.AttackEvent eve = new ItemEvent.AttackEvent( (ScriptLinkedItem) istack, NpcAPI.Instance().getIEntity(attacker), 1, NpcAPI.Instance().getIEntity(target)); -// return EventHooks.onScriptItemAttack((ScriptLinkedItem) istack, eve); - return true; - } - - public void renderOffset(ScriptLinkedItem scriptCustomItem) { - GL11.glTranslatef(0.135F * scriptCustomItem.itemDisplay.scaleX, 0.2F * scriptCustomItem.itemDisplay.scaleY, 0.07F * scriptCustomItem.itemDisplay.scaleZ); - } } diff --git a/src/main/java/noppes/npcs/items/ItemScripted.java b/src/main/java/noppes/npcs/items/ItemScripted.java index d2e1edad3..d22dd2827 100644 --- a/src/main/java/noppes/npcs/items/ItemScripted.java +++ b/src/main/java/noppes/npcs/items/ItemScripted.java @@ -1,8 +1,5 @@ package noppes.npcs.items; -import net.minecraft.block.Block; -import net.minecraft.entity.Entity; -import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.util.ChatComponentTranslation; @@ -10,14 +7,8 @@ import net.minecraft.world.World; import noppes.npcs.CustomItems; import noppes.npcs.CustomNpcs; import noppes.npcs.CustomNpcsPermissions; -import noppes.npcs.EventHooks; -import noppes.npcs.api.item.IItemStack; import noppes.npcs.config.ConfigScript; import noppes.npcs.constants.EnumGuiType; -import noppes.npcs.scripted.NpcAPI; -import noppes.npcs.scripted.event.ItemEvent; -import noppes.npcs.scripted.item.ScriptCustomItem; -import org.lwjgl.opengl.GL11; public class ItemScripted extends ItemCustomizable { @@ -39,76 +30,4 @@ public class ItemScripted extends ItemCustomizable { } return super.onItemRightClick(stack, world, player); } - - @Override - public boolean onEntitySwing(EntityLivingBase entityLivingBase, ItemStack stack){ - if (entityLivingBase.worldObj.isRemote) { - return false; - } - - IItemStack istack = NpcAPI.Instance().getIItemStack(stack); - ItemEvent.AttackEvent eve = new ItemEvent.AttackEvent( (ScriptCustomItem) istack, NpcAPI.Instance().getIEntity(entityLivingBase), 2, null); - return EventHooks.onScriptItemAttack((ScriptCustomItem) istack, eve); - } - - public int getMaxItemUseDuration(ItemStack stack){ - return (new ScriptCustomItem(stack)).getMaxItemUseDuration(); - } - - public static ScriptCustomItem GetWrapper(ItemStack stack) { - return new ScriptCustomItem(stack); - } - - public boolean showDurabilityBar(ItemStack stack) { - IItemStack istack = NpcAPI.Instance().getIItemStack(stack); - return istack instanceof ScriptCustomItem && (new ScriptCustomItem(stack)).itemDisplay.durabilityShow; - } - - public double getDurabilityForDisplay(ItemStack stack) { - IItemStack istack = NpcAPI.Instance().getIItemStack(stack); - return istack instanceof ScriptCustomItem ? 1.0D - (new ScriptCustomItem(stack)).durabilityValue : 1.0D; - } - - public int getItemStackLimit(ItemStack stack) { - IItemStack istack = NpcAPI.Instance().getIItemStack(stack); - return istack instanceof ScriptCustomItem ? (new ScriptCustomItem(stack)).getMaxStackSize() : super.getItemStackLimit(stack); - } - - public boolean isItemTool(ItemStack stack) - { - IItemStack istack = NpcAPI.Instance().getIItemStack(stack); - return istack instanceof ScriptCustomItem ? (new ScriptCustomItem(stack)).isTool() : super.isItemTool(stack); - } - - public float getDigSpeed(ItemStack stack, Block block, int metadata) - { - IItemStack istack = NpcAPI.Instance().getIItemStack(stack); - return istack instanceof ScriptCustomItem ? (new ScriptCustomItem(stack)).getDigSpeed() : super.getDigSpeed(stack, block, metadata); - } - - public boolean isValidArmor(ItemStack stack, int armorType, Entity entity) - { - IItemStack istack = NpcAPI.Instance().getIItemStack(stack); - - if((new ScriptCustomItem(stack)).getArmorType() == -1) - return true; - - return istack instanceof ScriptCustomItem ? armorType == (new ScriptCustomItem(stack)).getArmorType() : super.isValidArmor(stack, armorType, entity); - } - - public int getItemEnchantability(ItemStack stack) - { - IItemStack istack = NpcAPI.Instance().getIItemStack(stack); - return istack instanceof ScriptCustomItem ? (new ScriptCustomItem(stack)).getEnchantability() : super.getItemEnchantability(stack); - } - - public boolean hitEntity(ItemStack stack, EntityLivingBase target, EntityLivingBase attacker) { - IItemStack istack = NpcAPI.Instance().getIItemStack(stack); - ItemEvent.AttackEvent eve = new ItemEvent.AttackEvent( (ScriptCustomItem) istack, NpcAPI.Instance().getIEntity(attacker), 1, NpcAPI.Instance().getIEntity(target)); - return EventHooks.onScriptItemAttack((ScriptCustomItem) istack, eve); - } - - public void renderOffset(ScriptCustomItem scriptCustomItem) { - GL11.glTranslatef(0.135F * scriptCustomItem.itemDisplay.scaleX, 0.2F * scriptCustomItem.itemDisplay.scaleY, 0.07F * scriptCustomItem.itemDisplay.scaleZ); - } } diff --git a/src/main/java/noppes/npcs/scripted/NpcAPI.java b/src/main/java/noppes/npcs/scripted/NpcAPI.java index 1b47fb30d..80faea624 100644 --- a/src/main/java/noppes/npcs/scripted/NpcAPI.java +++ b/src/main/java/noppes/npcs/scripted/NpcAPI.java @@ -470,8 +470,14 @@ public class NpcAPI extends AbstractNpcAPI { if (scriptItemCache.containsKey(itemstack)) { scriptStack = scriptItemCache.get(itemstack).getObject(); } else { - String linkedItemName = itemstack.getTagCompound().getString(LinkedItem.LINKED_NBT_TAG); - LinkedItem linkedItem = LinkedItemController.Instance().get(linkedItemName); + LinkedItem linkedItem; + if (itemstack.hasTagCompound()) { + int linkedItemId = itemstack.getTagCompound().getInteger(LinkedItem.LINKED_NBT_TAG); + linkedItem = LinkedItemController.Instance().get(linkedItemId); + } else { + linkedItem = null; + } + if (linkedItem != null && itemstack.getItem() instanceof ItemLinked) { scriptStack = new ScriptLinkedItem(itemstack); } else if (itemstack.getItem() instanceof ItemScripted) { diff --git a/src/main/java/noppes/npcs/scripted/item/ScriptCustomItem.java b/src/main/java/noppes/npcs/scripted/item/ScriptCustomItem.java index 437114c90..1457302d8 100644 --- a/src/main/java/noppes/npcs/scripted/item/ScriptCustomItem.java +++ b/src/main/java/noppes/npcs/scripted/item/ScriptCustomItem.java @@ -63,6 +63,11 @@ public class ScriptCustomItem extends ScriptCustomizableItem implements IItemCus return this.enabled && ScriptController.HasStart; } + @Override + public INpcScriptHandler getScriptHandler() { + return this; + } + public void callScript(EnumScriptType type, Event event) { this.callScript(type.function, event); } diff --git a/src/main/java/noppes/npcs/scripted/item/ScriptItemStack.java b/src/main/java/noppes/npcs/scripted/item/ScriptItemStack.java index 59242a021..963ce0d84 100644 --- a/src/main/java/noppes/npcs/scripted/item/ScriptItemStack.java +++ b/src/main/java/noppes/npcs/scripted/item/ScriptItemStack.java @@ -16,6 +16,9 @@ import net.minecraftforge.common.IPlantable; import noppes.npcs.NoppesUtilPlayer; import noppes.npcs.api.INbt; import noppes.npcs.api.item.IItemStack; +import noppes.npcs.controllers.LinkedItemController; +import noppes.npcs.controllers.data.LinkedItem; +import noppes.npcs.items.ItemLinked; import noppes.npcs.items.ItemScripted; import noppes.npcs.scripted.CustomNPCsException; import noppes.npcs.scripted.NpcAPI; @@ -313,6 +316,16 @@ public class ScriptItemStack implements IItemStack { if(item == null) return null; + if (item.getItem() instanceof ItemLinked) { + LinkedItem linkedItem; + if (item.hasTagCompound()) { + int linkedItemId = item.getTagCompound().getInteger(LinkedItem.LINKED_NBT_TAG); + linkedItem = LinkedItemController.Instance().get(linkedItemId); + if (linkedItem != null) { + return new ScriptLinkedItem(item); + } + } + } if(item.getItem() instanceof ItemScripted) { return new ScriptCustomItem(item); } diff --git a/src/main/java/noppes/npcs/scripted/item/ScriptLinkedItem.java b/src/main/java/noppes/npcs/scripted/item/ScriptLinkedItem.java index 1fb1833ba..676b151e6 100644 --- a/src/main/java/noppes/npcs/scripted/item/ScriptLinkedItem.java +++ b/src/main/java/noppes/npcs/scripted/item/ScriptLinkedItem.java @@ -2,6 +2,7 @@ package noppes.npcs.scripted.item; import net.minecraft.item.ItemStack; import noppes.npcs.controllers.LinkedItemController; +import noppes.npcs.controllers.data.INpcScriptHandler; import noppes.npcs.controllers.data.LinkedItem; public class ScriptLinkedItem extends ScriptCustomizableItem { @@ -9,8 +10,8 @@ public class ScriptLinkedItem extends ScriptCustomizableItem { public ScriptLinkedItem(ItemStack item) { super(item); - String linkedItemName = item.getTagCompound().getString(LinkedItem.LINKED_NBT_TAG); - this.linkedItem = LinkedItemController.Instance().get(linkedItemName); + int linkedItemId = item.getTagCompound().getInteger(LinkedItem.LINKED_NBT_TAG); + this.linkedItem = LinkedItemController.Instance().get(linkedItemId); this.itemDisplay.texture = null; this.itemDisplay.translateX = null; @@ -34,6 +35,11 @@ public class ScriptLinkedItem extends ScriptCustomizableItem { return this.linkedItem; } + @Override + public INpcScriptHandler getScriptHandler() { + return this.getLinkedItem().getScriptHandler(); + } + @Override public int getMaxStackSize() { return this.linkedItem.stackSize; From 8909124fff09fdde1f97016015548ef648e5be94 Mon Sep 17 00:00:00 2001 From: luisc Date: Fri, 14 Feb 2025 12:29:53 -0600 Subject: [PATCH 12/13] Made ScriptLinkedItem implement IItemLinked api interface --- src/main/java/noppes/npcs/scripted/item/ScriptLinkedItem.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/noppes/npcs/scripted/item/ScriptLinkedItem.java b/src/main/java/noppes/npcs/scripted/item/ScriptLinkedItem.java index 676b151e6..2611f85b6 100644 --- a/src/main/java/noppes/npcs/scripted/item/ScriptLinkedItem.java +++ b/src/main/java/noppes/npcs/scripted/item/ScriptLinkedItem.java @@ -1,11 +1,12 @@ package noppes.npcs.scripted.item; import net.minecraft.item.ItemStack; +import noppes.npcs.api.item.IItemLinked; import noppes.npcs.controllers.LinkedItemController; import noppes.npcs.controllers.data.INpcScriptHandler; import noppes.npcs.controllers.data.LinkedItem; -public class ScriptLinkedItem extends ScriptCustomizableItem { +public class ScriptLinkedItem extends ScriptCustomizableItem implements IItemLinked { private final LinkedItem linkedItem; public ScriptLinkedItem(ItemStack item) { From 30aa37ba6f61798ff51a4bccc24afd7ebbb12dad Mon Sep 17 00:00:00 2001 From: luisc Date: Fri, 14 Feb 2025 12:30:07 -0600 Subject: [PATCH 13/13] Made ScriptLinkedItem implement IItemLinked api interface --- src/api | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api b/src/api index 232da0325..299136c9d 160000 --- a/src/api +++ b/src/api @@ -1 +1 @@ -Subproject commit 232da0325b4c8b10c4ebc3e5f726e3cfd2df86fb +Subproject commit 299136c9db54f283f83de15db0382898c3090dc4