mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-12 01:27:27 +00:00
Linked Item Saving & Writing
This commit is contained in:
1 parent
85165070e0
commit
5cbab2581c
7 files changed
+36
-20
No files matched your search
@@ -73,7 +73,7 @@ public final class LinkedGetPacket extends AbstractPacket {
|
||||
} else if (action == Action.ITEM) {
|
||||
int id = in.readInt();
|
||||
LinkedItem data = LinkedItemController.getInstance().get(id);
|
||||
NBTTagCompound compound = data.writeToNBT();
|
||||
NBTTagCompound compound = data.writeToNBT(false);
|
||||
GuiDataPacket.sendGuiData((EntityPlayerMP) player, compound);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ public final class LinkedItemSavePacket extends AbstractPacket {
|
||||
|
||||
String prevName = ByteBufUtils.readString(in);
|
||||
LinkedItem linkedItem = new LinkedItem();
|
||||
linkedItem.readFromNBT(ByteBufUtils.readNBT(in), false);
|
||||
linkedItem.readFromNBT(ByteBufUtils.readNBT(in));
|
||||
linkedItem.version++;
|
||||
|
||||
LinkedItemController.getInstance().saveLinkedItem(linkedItem);
|
||||
|
||||
@@ -205,7 +205,7 @@ public class GuiNPCManageLinked extends GuiNPCInterface2 implements IScrollData,
|
||||
while (data.containsKey(name))
|
||||
name += "_";
|
||||
LinkedItem linkedItem = new LinkedItem(name);
|
||||
PacketClient.sendClient(new LinkedItemSavePacket(linkedItem.writeToNBT(), ""));
|
||||
PacketClient.sendClient(new LinkedItemSavePacket(linkedItem.writeToNBT(false), ""));
|
||||
}
|
||||
}
|
||||
if(button.id == 2){
|
||||
@@ -383,7 +383,7 @@ public class GuiNPCManageLinked extends GuiNPCInterface2 implements IScrollData,
|
||||
loadedNPC = true;
|
||||
} else {
|
||||
this.linkedItem = new LinkedItem();
|
||||
this.linkedItem.readFromNBT(compound, false);
|
||||
this.linkedItem.readFromNBT(compound);
|
||||
}
|
||||
initGui();
|
||||
}
|
||||
|
||||
@@ -251,7 +251,7 @@ public class SubGuiLinkedItem extends SubGuiInterface implements ITextfieldListe
|
||||
@Override
|
||||
public void close() {
|
||||
super.close();
|
||||
PacketClient.sendClient(new LinkedItemSavePacket(linkedItem.writeToNBT(), originalName));
|
||||
PacketClient.sendClient(new LinkedItemSavePacket(linkedItem.writeToNBT(false), originalName));
|
||||
}
|
||||
|
||||
public enum LinkedItemType {
|
||||
|
||||
@@ -100,7 +100,7 @@ public class LinkedItemController {
|
||||
continue;
|
||||
try {
|
||||
LinkedItem linkedItem = new LinkedItem();
|
||||
linkedItem.readFromNBT(NBTJsonUtil.LoadFile(file), false);
|
||||
linkedItem.readFromNBT(NBTJsonUtil.LoadFile(file));
|
||||
linkedItem.name = file.getName().substring(0, file.getName().length() - 5);
|
||||
|
||||
if(linkedItem.id == -1){
|
||||
@@ -181,7 +181,7 @@ public class LinkedItemController {
|
||||
File file2 = new File(dir, linkedItem.getName() + ".json");
|
||||
|
||||
try {
|
||||
NBTJsonUtil.SaveFile(file, ((LinkedItem)linkedItem).writeToNBT());
|
||||
NBTJsonUtil.SaveFile(file, ((LinkedItem)linkedItem).writeToNBT(true));
|
||||
if(file2.exists())
|
||||
file2.delete();
|
||||
file.renameTo(file2);
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package noppes.npcs.controllers.data;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraftforge.common.util.Constants;
|
||||
import noppes.npcs.api.handler.data.ILinkedItem;
|
||||
import noppes.npcs.controllers.CustomEffectController;
|
||||
import noppes.npcs.controllers.LinkedItemController;
|
||||
|
||||
public class LinkedItem implements ILinkedItem {
|
||||
@@ -33,11 +35,11 @@ public class LinkedItem implements ILinkedItem {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public INpcScriptHandler getScriptHandler() {
|
||||
return LinkedItemController.getInstance().getScriptHandler(this.getId());
|
||||
public LinkedItemScript getScriptHandler() {
|
||||
return (LinkedItemScript) LinkedItemController.getInstance().getScriptHandler(this.getId());
|
||||
}
|
||||
|
||||
public NBTTagCompound writeToNBT() {
|
||||
public NBTTagCompound writeToNBT(boolean saveScripts) {
|
||||
NBTTagCompound compound = new NBTTagCompound();
|
||||
compound.setInteger("Id", this.id);
|
||||
compound.setInteger("Version", this.version);
|
||||
@@ -56,15 +58,19 @@ public class LinkedItem implements ILinkedItem {
|
||||
|
||||
compound.setInteger("MaxItemUseDuration", this.maxItemUseDuration);
|
||||
compound.setInteger("ItemUseAction", this.itemUseAction);
|
||||
|
||||
if (saveScripts) {
|
||||
NBTTagCompound scriptData = new NBTTagCompound();
|
||||
LinkedItemScript handler = getScriptHandler();
|
||||
if (handler != null)
|
||||
handler.writeToNBT(scriptData);
|
||||
compound.setTag("ScriptData", scriptData);
|
||||
}
|
||||
return compound;
|
||||
}
|
||||
|
||||
public void readFromNBT(NBTTagCompound compound, boolean checkID) {
|
||||
if(checkID)
|
||||
this.id = compound.hasKey("Id") ? compound.getInteger("Id") : LinkedItemController.getInstance().getUnusedId();
|
||||
else
|
||||
this.id = compound.getInteger("Id");
|
||||
|
||||
public void readFromNBT(NBTTagCompound compound) {
|
||||
this.id = compound.hasKey("Id") ? compound.getInteger("Id") : LinkedItemController.getInstance().getUnusedId();
|
||||
this.name = compound.getString("Name");
|
||||
this.version = compound.getInteger("Version");
|
||||
|
||||
@@ -81,12 +87,22 @@ public class LinkedItem implements ILinkedItem {
|
||||
|
||||
this.maxItemUseDuration = compound.getInteger("MaxItemUseDuration");
|
||||
this.itemUseAction = compound.getInteger("ItemUseAction");
|
||||
|
||||
if (compound.hasKey("ScriptData", Constants.NBT.TAG_COMPOUND)) {
|
||||
LinkedItemScript handler = new LinkedItemScript();
|
||||
handler.readFromNBT(compound.getCompoundTag("ScriptData"));
|
||||
setScriptHandler(handler);
|
||||
}
|
||||
}
|
||||
|
||||
public ILinkedItem save() {
|
||||
return LinkedItemController.getInstance().saveLinkedItem(this);
|
||||
}
|
||||
|
||||
public void setScriptHandler(LinkedItemScript handler) {
|
||||
LinkedItemController.getInstance().linkedItemsScripts.put(this.id, handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
return id;
|
||||
@@ -208,9 +224,9 @@ public class LinkedItem implements ILinkedItem {
|
||||
}
|
||||
|
||||
public LinkedItem clone() {
|
||||
NBTTagCompound nbt = this.writeToNBT();
|
||||
NBTTagCompound nbt = this.writeToNBT(true);
|
||||
LinkedItem clone = new LinkedItem(this.name);
|
||||
clone.readFromNBT(nbt, false);
|
||||
clone.readFromNBT(nbt);
|
||||
return clone;
|
||||
}
|
||||
|
||||
|
||||
@@ -197,14 +197,14 @@ public class ScriptLinkedItem extends ScriptCustomizableItem implements IItemLin
|
||||
|
||||
public NBTTagCompound getItemNBT(NBTTagCompound compound) {
|
||||
this.itemDisplay.writeToNBT(compound);
|
||||
compound.setTag(LinkedItem.LINKED_DATA_NBT_TAG, this.linkedItem.writeToNBT());
|
||||
compound.setTag(LinkedItem.LINKED_DATA_NBT_TAG, this.linkedItem.writeToNBT(false));
|
||||
compound.setInteger(LinkedItem.LINKED_VERSION_VERSION_TAG, this.linkedVersion);
|
||||
return compound;
|
||||
}
|
||||
|
||||
public void setItemNBT(NBTTagCompound compound) {
|
||||
this.itemDisplay.readFromNBT(compound);
|
||||
this.linkedItem.readFromNBT(compound.getCompoundTag(LinkedItem.LINKED_DATA_NBT_TAG), false);
|
||||
this.linkedItem.readFromNBT(compound.getCompoundTag(LinkedItem.LINKED_DATA_NBT_TAG));
|
||||
this.linkedVersion = compound.getInteger(LinkedItem.LINKED_VERSION_VERSION_TAG);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user