diff --git a/src/api b/src/api index 8da3e91ed..c56ad93a1 160000 --- a/src/api +++ b/src/api @@ -1 +1 @@ -Subproject commit 8da3e91ed47235828863736461c148e29fdcf362 +Subproject commit c56ad93a1b64c2541b0f4d319e03edf0fc5fc4b9 diff --git a/src/main/java/noppes/npcs/CustomNpcs.java b/src/main/java/noppes/npcs/CustomNpcs.java index 736bd9ec8..f0e218de7 100644 --- a/src/main/java/noppes/npcs/CustomNpcs.java +++ b/src/main/java/noppes/npcs/CustomNpcs.java @@ -210,6 +210,8 @@ public class CustomNpcs { new LinkedNpcController(); new AnimationController(); + LinkedItemController.Instance().load(); + // Custom Effects CustomEffectController.getInstance().load(); diff --git a/src/main/java/noppes/npcs/controllers/LinkedItemController.java b/src/main/java/noppes/npcs/controllers/LinkedItemController.java index da82ba343..ddd30e6cf 100644 --- a/src/main/java/noppes/npcs/controllers/LinkedItemController.java +++ b/src/main/java/noppes/npcs/controllers/LinkedItemController.java @@ -2,24 +2,29 @@ package noppes.npcs.controllers; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; +import net.minecraft.nbt.CompressedStreamTools; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; import noppes.npcs.CustomNpcs; -import noppes.npcs.controllers.data.EffectScript; -import noppes.npcs.controllers.data.INpcScriptHandler; -import noppes.npcs.controllers.data.LinkedItem; +import noppes.npcs.LogWriter; +import noppes.npcs.api.handler.data.ILinkedItem; +import noppes.npcs.controllers.data.*; +import noppes.npcs.util.NBTJsonUtil; -import java.io.File; +import java.io.*; import java.util.HashMap; +import java.util.zip.GZIPInputStream; -//TODO: load this data from somewhere, use in GUIs public class LinkedItemController { private static LinkedItemController Instance; private int lastUsedID = 0; - private final HashMap linkedItems = new HashMap<>(); - private final HashMap linkedItemsScripts = new HashMap<>(); + private HashMap linkedItems = new HashMap<>(); + private HashMap linkedItemsScripts = new HashMap<>(); - private LinkedItemController() { - } + private HashMap bootOrder; + + private LinkedItemController() {} public static LinkedItemController Instance() { if (Instance == null) { @@ -27,20 +32,6 @@ 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); } @@ -60,7 +51,7 @@ public class LinkedItemController { @SideOnly(Side.SERVER) private void addScript(int id) { - this.linkedItemsScripts.put(id, new EffectScript()); + this.linkedItemsScripts.put(id, new LinkedItemScript()); } public void remove(int id) { @@ -88,4 +79,223 @@ public class LinkedItemController { public INpcScriptHandler getScriptHandler(int id) { return this.linkedItemsScripts.get(id); } + + //////////////////////////////////////////////////////// + //////////////////////////////////////////////////////// + + public void load(){ + bootOrder = new HashMap<>(); + linkedItems = new HashMap<>(); + LogWriter.info("Loading linked items..."); + readLinkedMap(); + loadLinkedItems(); + LogWriter.info("Done loading linked items."); + } + + private void loadLinkedItems(){ + linkedItems.clear(); + + File dir = getDir(); + if (!dir.exists()) { + dir.mkdir(); + } else { + for (File file : dir.listFiles()) { + if (!file.isFile() || !file.getName().endsWith(".json")) + continue; + try { + LinkedItem linkedItem = new LinkedItem(); + linkedItem.readFromNBT(NBTJsonUtil.LoadFile(file), false); + linkedItem.name = file.getName().substring(0, file.getName().length() - 5); + + if(linkedItem.id == -1){ + linkedItem.id = getUnusedId(); + } + + int originalID = linkedItem.id; + int setID = linkedItem.id; + while (bootOrder.containsKey(setID) || linkedItems.containsKey(setID)){ + if(bootOrder.containsKey(setID)) + if(bootOrder.get(setID).equals(linkedItem.name)) + break; + + setID++; + } + + linkedItem.id = setID; + if(originalID != setID){ + LogWriter.info("Found Linked Item ID Mismatch: " + linkedItem.name + ", New ID: " + setID); + linkedItem.save(); + } + + linkedItems.put(linkedItem.id, linkedItem); + } catch(Exception e) { + LogWriter.error("Error loading: " + file.getAbsolutePath(), e); + } + } + } + + saveLinkedItemsMap(); + } + + private File getDir() { + return new File(CustomNpcs.getWorldSaveDirectory(), "linkeditems"); + } + + public int getUnusedId(){ + if(lastUsedID == 0){ + for(int catid : linkedItems.keySet()){ + if(catid > lastUsedID) + lastUsedID = catid; + } + + } + lastUsedID++; + return lastUsedID; + } + + public boolean hasName(String newName) { + if(newName.trim().isEmpty()) + return true; + for(LinkedItem linkedItem : linkedItems.values()) + if(linkedItem.name.equals(newName)) + return true; + return false; + } + + public ILinkedItem saveLinkedItem(ILinkedItem linkedItem){ + if(linkedItem.getId() < 0){ + linkedItem.setId(getUnusedId()); + while(hasName(linkedItem.getName())) + linkedItem.setName(linkedItem.getName() + "_"); + } + else{ + LinkedItem existing = linkedItems.get(linkedItem.getId()); + if(existing != null && !existing.name.equals(linkedItem.getName())) + while(hasName(linkedItem.getName())) + linkedItem.setName(linkedItem.getName() + "_"); + } + + linkedItems.remove(linkedItem.getId()); + linkedItems.put(linkedItem.getId(), (LinkedItem) linkedItem); + + saveLinkedItemsMap(); + + // Save Linked Item File + File dir = this.getDir(); + if(!dir.exists()) + dir.mkdirs(); + + File file = new File(dir, linkedItem.getName() + ".json_new"); + File file2 = new File(dir, linkedItem.getName() + ".json"); + + try { + NBTJsonUtil.SaveFile(file, ((LinkedItem)linkedItem).writeToNBT()); + if(file2.exists()) + file2.delete(); + file.renameTo(file2); + } catch (Exception e) { + LogWriter.except(e); + } + return linkedItems.get(linkedItem.getId()); + } + + + //////////////////////////////////////////////////////// + //////////////////////////////////////////////////////// + // LINKED ITEMS MAP + + public File getMapDir(){ + File dir = CustomNpcs.getWorldSaveDirectory(); + if(!dir.exists()) + dir.mkdir(); + return dir; + } + + public void readLinkedMap(){ + bootOrder.clear(); + + try { + File file = new File(getMapDir(), "linkeditems.dat"); + if(file.exists()){ + loadLinkedMap(file); + } + } catch (Exception e) { + try { + File file = new File(getMapDir(), "linkeditems.dat_old"); + if(file.exists()){ + loadLinkedMap(file); + } + } catch (Exception ignored) {} + } + } + + public NBTTagCompound writeMapNBT(){ + NBTTagCompound nbt = new NBTTagCompound(); + NBTTagList linkedList = new NBTTagList(); + for(Integer key: linkedItems.keySet()){ + LinkedItem linkedItem = linkedItems.get(key); + if(!linkedItem.getName().isEmpty()){ + NBTTagCompound linkedCompound = new NBTTagCompound(); + linkedCompound.setString("Name", linkedItem.getName()); + linkedCompound.setInteger("ID", key); + + linkedList.appendTag(linkedCompound); + } + } + nbt.setTag("LinkedItems", linkedList); + return nbt; + } + + public void readMapNBT(NBTTagCompound compound){ + NBTTagList list = compound.getTagList("LinkedItems", 10); + if(list != null){ + for(int i = 0; i < list.tagCount(); i++) + { + NBTTagCompound nbttagcompound = list.getCompoundTagAt(i); + String linkedName = nbttagcompound.getString("Name"); + Integer key = nbttagcompound.getInteger("ID"); + bootOrder.put(key, linkedName); + } + } + } + + private void loadLinkedMap(File file) throws IOException { + DataInputStream var1 = new DataInputStream(new BufferedInputStream(new GZIPInputStream(new FileInputStream(file)))); + readLinkedMap(var1); + var1.close(); + } + + public void readLinkedMap(DataInputStream stream) throws IOException{ + NBTTagCompound nbtCompound = CompressedStreamTools.read(stream); + this.readMapNBT(nbtCompound); + } + + public void saveLinkedItemsMap(){ + try { + File saveDir = getMapDir(); + File file = new File(saveDir, "linkeditems.dat_new"); + File file1 = new File(saveDir, "linkeditems.dat_old"); + File file2 = new File(saveDir, "linkeditems.dat"); + CompressedStreamTools.writeCompressed(this.writeMapNBT(), new FileOutputStream(file)); + if(file1.exists()) + { + file1.delete(); + } + file2.renameTo(file1); + if(file2.exists()) + { + file2.delete(); + } + file.renameTo(file2); + if(file.exists()) + { + file.delete(); + } + } catch (Exception e) { + LogWriter.except(e); + } + } + + //////////////////////////////////////////////////////// + //////////////////////////////////////////////////////// } diff --git a/src/main/java/noppes/npcs/controllers/LinkedScriptedItemController.java b/src/main/java/noppes/npcs/controllers/LinkedScriptedItemController.java deleted file mode 100644 index eaba4baaa..000000000 --- a/src/main/java/noppes/npcs/controllers/LinkedScriptedItemController.java +++ /dev/null @@ -1,114 +0,0 @@ -package noppes.npcs.controllers; - - -import net.minecraft.nbt.NBTTagCompound; -import noppes.npcs.CustomNpcs; -import noppes.npcs.LogWriter; -import noppes.npcs.util.JsonException; -import noppes.npcs.util.NBTJsonUtil; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -public class LinkedScriptedItemController { - public static LinkedScriptedItemController Instance; - public List list = new ArrayList<>(); - - public LinkedScriptedItemController() { - Instance = this; - load(); - } - - public File getDir() { - File dir = new File(CustomNpcs.getWorldSaveDirectory(), "linkeditems"); - if (!dir.exists()) { - dir.mkdirs(); - } - return dir; - } - - public void load() { - File dir = getDir(); - File[] files = dir.listFiles((d, name) -> name.endsWith(".json")); - if(files == null) return; - for (File file : files) { - try { - NBTTagCompound compound = NBTJsonUtil.LoadFile(file); - LinkedScriptedItemData data = new LinkedScriptedItemData(); - data.loadFromNBT(compound); - list.add(data); - } catch (Exception e) { - LogWriter.error("Error loading linked items: " + file.getAbsolutePath(), e); - } - } - } - - public void save() { - File dir = getDir(); - for (LinkedScriptedItemData data : list) { - try { - File file = new File(dir, data.name + ".json_new"); - NBTJsonUtil.SaveFile(file, data.saveToNBT()); - } catch (IOException e) { - LogWriter.except(e); - } catch (JsonException e) { - throw new RuntimeException(e); - } - } - } - - public LinkedScriptedItemData getData(String name) { - for (LinkedScriptedItemData data : list) { - if (data.name.equalsIgnoreCase(name)) { - return data; - } - } - return null; - } - - public void addData(String name, NBTTagCompound scriptData) { - if (name.isEmpty() || getData(name) != null) - return; - LinkedScriptedItemData data = new LinkedScriptedItemData(); - data.name = name; - data.scriptData = scriptData; - data.time = System.currentTimeMillis(); - list.add(data); - save(); - } - - public void updateData(String name, NBTTagCompound scriptData) { - LinkedScriptedItemData data = getData(name); - if (data != null) { - data.scriptData = scriptData; - data.time = System.currentTimeMillis(); - save(); - } - } - - public static class LinkedScriptedItemData { - public String name = "LinkedScriptedItem"; - public long time; - public NBTTagCompound scriptData = new NBTTagCompound(); - - public LinkedScriptedItemData() { - time = System.currentTimeMillis(); - } - - public void loadFromNBT(NBTTagCompound compound) { - name = compound.getString("LinkedScriptName"); - scriptData = compound.getCompoundTag("ScriptData"); - time = compound.getLong("Time"); - } - - public NBTTagCompound saveToNBT() { - NBTTagCompound compound = new NBTTagCompound(); - compound.setString("LinkedScriptName", name); - compound.setTag("ScriptData", scriptData); - compound.setLong("Time", time); - return compound; - } - } -} diff --git a/src/main/java/noppes/npcs/controllers/data/LinkedItem.java b/src/main/java/noppes/npcs/controllers/data/LinkedItem.java index 2776298b3..aef3ef980 100644 --- a/src/main/java/noppes/npcs/controllers/data/LinkedItem.java +++ b/src/main/java/noppes/npcs/controllers/data/LinkedItem.java @@ -1,16 +1,16 @@ package noppes.npcs.controllers.data; -import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.nbt.NBTTagInt; -import noppes.npcs.CustomItems; +import noppes.npcs.api.handler.data.ILinkedItem; import noppes.npcs.controllers.LinkedItemController; -public class LinkedItem { - public static final String LINKED_NBT_TAG = "LinkedId"; +public class LinkedItem implements ILinkedItem { + public static final String LINKED_VERSION_VERSION_TAG = "LinkedVersion"; + public static final String LINKED_DATA_NBT_TAG = "LinkedData"; - private int id = -1; - private String name; + public int id = -1; + public int version = 1; + public String name; public final ItemDisplayData display = new ItemDisplayData(); public double durabilityValue = 1.0D; @@ -33,27 +33,6 @@ public class LinkedItem { 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() { - 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()); } @@ -61,6 +40,7 @@ public class LinkedItem { public NBTTagCompound writeToNBT() { NBTTagCompound compound = new NBTTagCompound(); compound.setInteger("Id", this.id); + compound.setInteger("Version", this.version); compound.setString("Name", this.name); compound.setTag("Display", this.display.writeToNBT()); @@ -79,9 +59,14 @@ public class LinkedItem { return compound; } - public void readFromNBT(NBTTagCompound compound) { - this.id = compound.hasKey("Id") ? compound.getInteger("Id") : LinkedItemController.Instance().getUnusedId(); + public void readFromNBT(NBTTagCompound compound, boolean checkID) { + if(checkID) + this.id = compound.hasKey("Id") ? compound.getInteger("Id") : LinkedItemController.Instance().getUnusedId(); + else + this.id = compound.getInteger("Id"); + this.name = compound.getString("Name"); + this.version = compound.getInteger("Version"); this.display.readFromNBT(compound.getCompoundTag("Display")); @@ -97,4 +82,131 @@ public class LinkedItem { this.maxItemUseDuration = compound.getInteger("MaxItemUseDuration"); this.itemUseAction = compound.getInteger("ItemUseAction"); } + + public ILinkedItem save() { + return LinkedItemController.Instance().saveLinkedItem(this); + } + + @Override + public int getId() { + return id; + } + + @Override + public void setId(int id) { + this.id = id; + } + + @Override + public int getVersion() { + return version; + } + + @Override + public void setVersion(int version) { + this.version = version; + } + + @Override + public String getName() { + return name; + } + + @Override + public void setName(String name) { + this.name = name; + } + + @Override + public double getDurabilityValue() { + return durabilityValue; + } + + @Override + public void setDurabilityValue(double durabilityValue) { + this.durabilityValue = durabilityValue; + } + + @Override + public int getStackSize() { + return stackSize; + } + + @Override + public void setStackSize(int stackSize) { + this.stackSize = stackSize; + } + + @Override + public int getMaxItemUseDuration() { + return maxItemUseDuration; + } + + @Override + public void setMaxItemUseDuration(int maxItemUseDuration) { + this.maxItemUseDuration = maxItemUseDuration; + } + + @Override + public int getItemUseAction() { + return itemUseAction; + } + + @Override + public void setItemUseAction(int itemUseAction) { + this.itemUseAction = itemUseAction; + } + + @Override + public boolean isNormalItem() { + return isNormalItem; + } + + @Override + public void setNormalItem(boolean normalItem) { + isNormalItem = normalItem; + } + + @Override + public boolean isTool() { + return isTool; + } + + @Override + public void setTool(boolean tool) { + isTool = tool; + } + + @Override + public int getDigSpeed() { + return digSpeed; + } + + @Override + public void setDigSpeed(int digSpeed) { + this.digSpeed = digSpeed; + } + + @Override + public int getArmorType() { + return armorType; + } + + @Override + public void setArmorType(int armorType) { + this.armorType = armorType; + } + + @Override + public int getEnchantability() { + return enchantability; + } + + @Override + public void setEnchantability(int enchantability) { + this.enchantability = enchantability; + } + + // ADD / GET SETS FOR LINKED ITEM DISPLAY THINGS + } diff --git a/src/main/java/noppes/npcs/controllers/data/LinkedItemScript.java b/src/main/java/noppes/npcs/controllers/data/LinkedItemScript.java new file mode 100644 index 000000000..9541d2ee6 --- /dev/null +++ b/src/main/java/noppes/npcs/controllers/data/LinkedItemScript.java @@ -0,0 +1,187 @@ +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.constants.EnumScriptType; +import noppes.npcs.controllers.ScriptContainer; +import noppes.npcs.controllers.ScriptController; +import noppes.npcs.scripted.NpcAPI; +import noppes.npcs.scripted.event.PlayerEvent; + +import javax.script.ScriptEngine; +import java.io.IOException; +import java.util.*; + +public class LinkedItemScript implements INpcScriptHandler { + 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 void callScript(ScriptType type, PlayerEvent.EffectEvent event) { + callScript(type.function, event); + } + + public boolean isEnabled() { + return this.enabled && ScriptController.HasStart && container != null && ConfigScript.ScriptingEnabled; + } + + @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; + } + + 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")); + } + } + + public enum ScriptType { + OnAdd("onAdded"), + OnTick("onTick"), + OnRemove("onRemove"); + + public final String function; + ScriptType(String functionName) { + this.function = functionName; + } + } +} diff --git a/src/main/java/noppes/npcs/scripted/NpcAPI.java b/src/main/java/noppes/npcs/scripted/NpcAPI.java index 80faea624..57aecc287 100644 --- a/src/main/java/noppes/npcs/scripted/NpcAPI.java +++ b/src/main/java/noppes/npcs/scripted/NpcAPI.java @@ -470,15 +470,7 @@ public class NpcAPI extends AbstractNpcAPI { if (scriptItemCache.containsKey(itemstack)) { scriptStack = scriptItemCache.get(itemstack).getObject(); } else { - 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) { + if (itemstack.getItem() instanceof ItemLinked) { scriptStack = new ScriptLinkedItem(itemstack); } else if (itemstack.getItem() instanceof ItemScripted) { scriptStack = new ScriptCustomItem(itemstack); diff --git a/src/main/java/noppes/npcs/scripted/item/ScriptCustomizableItem.java b/src/main/java/noppes/npcs/scripted/item/ScriptCustomizableItem.java index fcea1e25e..c440d7b0a 100644 --- a/src/main/java/noppes/npcs/scripted/item/ScriptCustomizableItem.java +++ b/src/main/java/noppes/npcs/scripted/item/ScriptCustomizableItem.java @@ -14,39 +14,22 @@ public abstract class ScriptCustomizableItem extends ScriptItemStack implements 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; diff --git a/src/main/java/noppes/npcs/scripted/item/ScriptItemStack.java b/src/main/java/noppes/npcs/scripted/item/ScriptItemStack.java index 963ce0d84..5b15da052 100644 --- a/src/main/java/noppes/npcs/scripted/item/ScriptItemStack.java +++ b/src/main/java/noppes/npcs/scripted/item/ScriptItemStack.java @@ -317,14 +317,7 @@ public class ScriptItemStack implements IItemStack { 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); - } - } + 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 2611f85b6..dc9c4f03a 100644 --- a/src/main/java/noppes/npcs/scripted/item/ScriptLinkedItem.java +++ b/src/main/java/noppes/npcs/scripted/item/ScriptLinkedItem.java @@ -1,39 +1,23 @@ package noppes.npcs.scripted.item; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; 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 implements IItemLinked { - private final LinkedItem linkedItem; - + public LinkedItem linkedItem; + public double durabilityValue = 1.0D; + public int linkedVersion = 1; public ScriptLinkedItem(ItemStack item) { super(item); - int linkedItemId = item.getTagCompound().getInteger(LinkedItem.LINKED_NBT_TAG); - this.linkedItem = LinkedItemController.Instance().get(linkedItemId); - - 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; + loadItemData(); } public LinkedItem getLinkedItem() { - return this.linkedItem; + return LinkedItemController.Instance().get(this.linkedItem.getId()); } @Override @@ -68,7 +52,7 @@ public class ScriptLinkedItem extends ScriptCustomizableItem implements IItemLin @Override public double getDurabilityValue() { - return this.linkedItem.durabilityValue; + return this.durabilityValue; } @Override @@ -86,67 +70,112 @@ public class ScriptLinkedItem extends ScriptCustomizableItem implements IItemLin return this.linkedItem.enchantability; } + @Override public String getTexture() { return this.itemDisplay.texture == null ? this.linkedItem.display.texture : this.itemDisplay.texture; } + @Override public Boolean getDurabilityShow() { return this.itemDisplay.durabilityShow != null ? this.itemDisplay.durabilityShow : this.linkedItem.display.durabilityShow; } + @Override public Integer getDurabilityColor() { return this.itemDisplay.durabilityColor != null ? this.itemDisplay.durabilityColor : this.linkedItem.display.durabilityColor; } + @Override public Integer getColor() { return this.itemDisplay.itemColor != null ? this.itemDisplay.itemColor : this.linkedItem.display.itemColor; } + @Override public Float getRotationX() { return this.itemDisplay.rotationX != null ? this.itemDisplay.rotationX : this.linkedItem.display.rotationX; } + @Override public Float getRotationY() { return this.itemDisplay.rotationY != null ? this.itemDisplay.rotationY : this.linkedItem.display.rotationY; } + @Override public Float getRotationZ() { return this.itemDisplay.rotationZ != null ? this.itemDisplay.rotationZ : this.linkedItem.display.rotationZ; } + @Override public Float getRotationXRate() { return this.itemDisplay.rotationXRate != null ? this.itemDisplay.rotationXRate : this.linkedItem.display.rotationXRate; } + @Override public Float getRotationYRate() { return this.itemDisplay.rotationYRate != null ? this.itemDisplay.rotationYRate : this.linkedItem.display.rotationYRate; } + @Override public Float getRotationZRate() { return this.itemDisplay.rotationZRate != null ? this.itemDisplay.rotationZRate : this.linkedItem.display.rotationZRate; } + @Override public Float getScaleX() { return this.itemDisplay.scaleX != null ? this.itemDisplay.scaleX : this.linkedItem.display.scaleX; } + @Override public Float getScaleY() { return this.itemDisplay.scaleY != null ? this.itemDisplay.scaleY : this.linkedItem.display.scaleY; } + @Override public Float getScaleZ() { return this.itemDisplay.scaleZ != null ? this.itemDisplay.scaleZ : this.linkedItem.display.scaleZ; } + @Override 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 NBTTagCompound getMCNbt() { + NBTTagCompound compound = super.getMCNbt(); + compound.setTag("ItemData", this.getItemNBT(new NBTTagCompound())); + return compound; } - public Float getTranslateZ() { - return this.itemDisplay.translateZ != null ? this.itemDisplay.translateZ : this.linkedItem.display.translateZ; + 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) { + this.itemDisplay.writeToNBT(compound); + compound.setTag(LinkedItem.LINKED_DATA_NBT_TAG, this.linkedItem.writeToNBT()); + compound.setInteger(LinkedItem.LINKED_VERSION_VERSION_TAG, this.linkedVersion); + return compound; + } + + public void setItemNBT(NBTTagCompound compound) { + this.itemDisplay.readFromNBT(compound); + this.linkedItem.readFromNBT(compound, false); + this.linkedVersion = compound.getInteger(LinkedItem.LINKED_VERSION_VERSION_TAG); } }