diff --git a/src/api b/src/api index 4e763f7d8..08979f139 160000 --- a/src/api +++ b/src/api @@ -1 +1 @@ -Subproject commit 4e763f7d8deb56ada24287e8366c65904c9aad3f +Subproject commit 08979f13927dcb43fba933933b9957ee333a4a69 diff --git a/src/main/java/noppes/npcs/DataAdvanced.java b/src/main/java/noppes/npcs/DataAdvanced.java index 406889b8d..e90867dcb 100644 --- a/src/main/java/noppes/npcs/DataAdvanced.java +++ b/src/main/java/noppes/npcs/DataAdvanced.java @@ -12,6 +12,7 @@ import noppes.npcs.controllers.data.Line; import noppes.npcs.controllers.data.Lines; import noppes.npcs.entity.EntityNPCInterface; import noppes.npcs.roles.*; +import org.lwjgl.Sys; import java.util.HashMap; import java.util.HashSet; @@ -24,7 +25,7 @@ public class DataAdvanced { public Lines attackLines = new Lines(); public Lines killedLines = new Lines(); public Lines killLines = new Lines(); - + public boolean orderedLines = false; public String idleSound = ""; @@ -43,7 +44,7 @@ public class DataAdvanced { public boolean attackOtherFactions = false; public boolean defendFaction = false; public boolean disablePitch = false; - + public String soulStonePlayerName = ""; public boolean refuseSoulStone = false; public boolean soulStoneInit = false; @@ -78,7 +79,7 @@ public class DataAdvanced { compound.setTag("FactionPoints", factions.writeToNBT(new NBTTagCompound())); compound.setTag("NPCDialogOptions", nbtDialogs(npc.dialogs)); - + compound.setBoolean("RefuseSoulStone", refuseSoulStone); compound.setString("SoulStonePlayerName", soulStonePlayerName); compound.setInteger("MinFactionPointsToSoulStone", minFactionPointsToSoulStone); @@ -90,7 +91,7 @@ public class DataAdvanced { } compound.setTag("TagUUIDs", nbtTagList); } - + return compound; } @@ -120,8 +121,8 @@ public class DataAdvanced { factions.readFromNBT(compound.getCompoundTag("FactionPoints")); - npc.dialogs = getDialogs(compound.getTagList("NPCDialogOptions", 10)); - + npc.dialogs = getDialogs(compound.getTagList("NPCDialogOptions", 10)); + refuseSoulStone = compound.getBoolean("RefuseSoulStone"); soulStonePlayerName = compound.getString("SoulStonePlayerName"); minFactionPointsToSoulStone = compound.getInteger("MinFactionPointsToSoulStone"); @@ -204,40 +205,23 @@ public class DataAdvanced { public void setJob(int i) { if(npc.jobInterface != null && !npc.worldObj.isRemote) npc.jobInterface.reset(); - + job = EnumJobType.values()[i % EnumJobType.values().length]; if (job == EnumJobType.None) npc.jobInterface = null; - else if (job == EnumJobType.Bard && !(npc.jobInterface instanceof JobBard)) + else if (job == EnumJobType.Bard && !(npc.jobInterface instanceof JobBard)) npc.jobInterface = new JobBard(npc); - else if (job == EnumJobType.Healer && !(npc.jobInterface instanceof JobHealer)) + else if (job == EnumJobType.Healer && !(npc.jobInterface instanceof JobHealer)) npc.jobInterface = new JobHealer(npc); - else if (job == EnumJobType.Guard && !(npc.jobInterface instanceof JobGuard)) + else if (job == EnumJobType.Guard && !(npc.jobInterface instanceof JobGuard)) npc.jobInterface = new JobGuard(npc); - else if (job == EnumJobType.ItemGiver && !(npc.jobInterface instanceof JobItemGiver)) { - if (!npc.isRemote()) { - if (npc.itemGiverId > 0 && GlobalDataController.Instance.itemGivers.containsKey(npc.itemGiverId)) { - GlobalDataController.Instance.itemGivers.get(npc.itemGiverId).npc = npc; - npc.jobInterface = GlobalDataController.Instance.itemGivers.get(npc.itemGiverId); - } else { - npc.jobInterface = new JobItemGiver(npc); - if (npc.itemGiverId == -1) { - ((JobItemGiver) npc.jobInterface).itemGiverId = GlobalDataController.Instance.incrementItemGiverId(); - } else { - ((JobItemGiver) npc.jobInterface).itemGiverId = npc.itemGiverId; - } - GlobalDataController.Instance.itemGivers.put(((JobItemGiver) npc.jobInterface).itemGiverId, (JobItemGiver) npc.jobInterface); - npc.itemGiverId = ((JobItemGiver) npc.jobInterface).itemGiverId; - } - } else { - npc.jobInterface = new JobItemGiver(npc); - } - } - else if (job == EnumJobType.Follower && !(npc.jobInterface instanceof JobFollower)) + else if (job == EnumJobType.ItemGiver && !(npc.jobInterface instanceof JobItemGiver)) + npc.jobInterface = new JobItemGiver(npc); + else if (job == EnumJobType.Follower && !(npc.jobInterface instanceof JobFollower)) npc.jobInterface = new JobFollower(npc); - else if (job == EnumJobType.Spawner && !(npc.jobInterface instanceof JobSpawner)) + else if (job == EnumJobType.Spawner && !(npc.jobInterface instanceof JobSpawner)) npc.jobInterface = new JobSpawner(npc); - else if (job == EnumJobType.Conversation && !(npc.jobInterface instanceof JobConversation)) + else if (job == EnumJobType.Conversation && !(npc.jobInterface instanceof JobConversation)) npc.jobInterface = new JobConversation(npc); else if (job == EnumJobType.ChunkLoader && !(npc.jobInterface instanceof JobChunkLoader)) npc.jobInterface = new JobChunkLoader(npc); diff --git a/src/main/java/noppes/npcs/controllers/GlobalDataController.java b/src/main/java/noppes/npcs/controllers/GlobalDataController.java index ff2e306c1..7cc1b5b9a 100644 --- a/src/main/java/noppes/npcs/controllers/GlobalDataController.java +++ b/src/main/java/noppes/npcs/controllers/GlobalDataController.java @@ -4,24 +4,26 @@ import net.minecraft.nbt.CompressedStreamTools; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import noppes.npcs.CustomNpcs; +import noppes.npcs.config.ConfigMain; import noppes.npcs.roles.JobItemGiver; +import noppes.npcs.util.NBTJsonUtil; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.HashMap; +import static noppes.npcs.util.CustomNPCsThreader.customNPCThread; + public class GlobalDataController { public static GlobalDataController Instance; - private int itemGiverId = 0; - public HashMap itemGivers = new HashMap<>(); - + public GlobalDataController(){ Instance = this; load(); } - + private void load(){ File saveDir = CustomNpcs.getWorldSaveDirectory(); try { @@ -35,7 +37,7 @@ public class GlobalDataController { if(file.exists()){ loadData(file); } - + } catch (Exception ee) { ee.printStackTrace(); } @@ -44,54 +46,41 @@ public class GlobalDataController { private void loadData(File file) throws Exception { NBTTagCompound nbttagcompound1 = CompressedStreamTools.readCompressed(new FileInputStream(file)); itemGiverId = nbttagcompound1.getInteger("itemGiverId"); - - NBTTagList jobList = nbttagcompound1.getTagList("ItemGivers", 10); - for (int i = 0; i < jobList.tagCount(); i++) { - NBTTagCompound compound = jobList.getCompoundTagAt(i); - JobItemGiver jobItemGiver = new JobItemGiver(); - jobItemGiver.readFromNBT(compound); - itemGivers.put(jobItemGiver.itemGiverId, jobItemGiver); - } } public void saveData(){ - try { - File saveDir = CustomNpcs.getWorldSaveDirectory(); + customNPCThread.execute(() -> { + try { + File saveDir = CustomNpcs.getWorldSaveDirectory(); - NBTTagCompound nbttagcompound = new NBTTagCompound(); - nbttagcompound.setInteger("itemGiverId", itemGiverId); + NBTTagCompound nbttagcompound = new NBTTagCompound(); + nbttagcompound.setInteger("itemGiverId", itemGiverId); - NBTTagList jobList = new NBTTagList(); - for (JobItemGiver jobItemGiver : itemGivers.values()) { - NBTTagCompound compound = new NBTTagCompound(); - jobItemGiver.writeToNBT(compound); - jobList.appendTag(compound); - } - nbttagcompound.setTag("ItemGivers", jobList); - - File file = new File(saveDir, "global.dat_new"); - File file1 = new File(saveDir, "global.dat_old"); - File file2 = new File(saveDir, "global.dat"); - CompressedStreamTools.writeCompressed(nbttagcompound, new FileOutputStream(file)); - if(file1.exists()) - { - file1.delete(); + File file = new File(saveDir, "global.dat_new"); + File file1 = new File(saveDir, "global.dat_old"); + File file2 = new File(saveDir, "global.dat"); + CompressedStreamTools.writeCompressed(nbttagcompound, 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) { + e.printStackTrace(); } - file2.renameTo(file1); - if(file2.exists()) - { - file2.delete(); - } - file.renameTo(file2); - if(file.exists()) - { - file.delete(); - } - } catch (Exception e) { - e.printStackTrace(); - } + }); } public int incrementItemGiverId(){ itemGiverId++; + saveData(); return itemGiverId; } } diff --git a/src/main/java/noppes/npcs/controllers/data/PlayerItemGiverData.java b/src/main/java/noppes/npcs/controllers/data/PlayerItemGiverData.java index 73aafd84c..22777cb22 100644 --- a/src/main/java/noppes/npcs/controllers/data/PlayerItemGiverData.java +++ b/src/main/java/noppes/npcs/controllers/data/PlayerItemGiverData.java @@ -32,7 +32,7 @@ public class PlayerItemGiverData implements IPlayerItemGiverData { public boolean hasInteractedBefore(JobItemGiver jobItemGiver) { return itemgivers.containsKey(jobItemGiver.itemGiverId); } - + public long getTime(JobItemGiver jobItemGiver){ return itemgivers.get(jobItemGiver.itemGiverId); } @@ -59,17 +59,4 @@ public class PlayerItemGiverData implements IPlayerItemGiverData { public boolean hasInteractedBefore(IJobItemGiver jobItemGiver) { return itemgivers.containsKey(((JobItemGiver) jobItemGiver).itemGiverId); } - - public IJobItemGiver[] getItemGivers() { - ArrayList list = new ArrayList<>(); - for (JobItemGiver jobItemGiver : GlobalDataController.Instance.itemGivers.values()) { - if (jobItemGiver.npc != null) { - list.add(new ScriptJobItemGiver(jobItemGiver.npc)); - } else { - list.add(new ScriptJobItemGiver(jobItemGiver)); - } - } - - return list.toArray(new IJobItemGiver[0]); - } } diff --git a/src/main/java/noppes/npcs/entity/EntityNPCInterface.java b/src/main/java/noppes/npcs/entity/EntityNPCInterface.java index d2ade0c41..13b465433 100644 --- a/src/main/java/noppes/npcs/entity/EntityNPCInterface.java +++ b/src/main/java/noppes/npcs/entity/EntityNPCInterface.java @@ -99,7 +99,6 @@ public abstract class EntityNPCInterface extends EntityCreature implements IEnti public long totalTicksAlive = 0; private int taskCount = 1; public int lastInteract = 0; - public int itemGiverId = 0; public boolean isDrawn = false; public Faction faction; //should only be used server side @@ -1020,11 +1019,6 @@ public abstract class EntityNPCInterface extends EntityCreature implements IEnti script.readFromNBT(compound); script.readEventsFromNBT(compound); timers.readFromNBT(compound); - if (compound.hasKey("ItemGiverId")) { - itemGiverId = compound.getInteger("ItemGiverId"); - } else { - itemGiverId = -1; - } advanced.readToNBT(compound); if (advanced.role != EnumRoleType.None && roleInterface != null) roleInterface.readFromNBT(compound); diff --git a/src/main/java/noppes/npcs/roles/JobItemGiver.java b/src/main/java/noppes/npcs/roles/JobItemGiver.java index 550cfd1bc..ea69aa07e 100644 --- a/src/main/java/noppes/npcs/roles/JobItemGiver.java +++ b/src/main/java/noppes/npcs/roles/JobItemGiver.java @@ -7,6 +7,7 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import noppes.npcs.NBTTags; import noppes.npcs.NpcMiscInventory; +import noppes.npcs.controllers.GlobalDataController; import noppes.npcs.controllers.PlayerDataController; import noppes.npcs.controllers.data.Availability; import noppes.npcs.controllers.data.Line; @@ -25,11 +26,11 @@ public class JobItemGiver extends JobInterface{ public int cooldown = 10; public NpcMiscInventory inventory; public int itemGiverId = 0; - + public List lines = new ArrayList(); - + private int ticks = 10; - + private List recentlyChecked = new ArrayList(); private List toCheck; public Availability availability = new Availability(); @@ -65,6 +66,10 @@ public class JobItemGiver extends JobInterface{ cooldown = nbttagcompound.getInteger("igCooldown"); lines = NBTTags.getStringList(nbttagcompound.getTagList("igLines", 10)); inventory.setFromNBT(nbttagcompound.getCompoundTag("igJobInventory")); + + if(itemGiverId == 0 && GlobalDataController.Instance != null) + itemGiverId = GlobalDataController.Instance.incrementItemGiverId(); + availability.readFromNBT(nbttagcompound.getCompoundTag("igAvailability")); } @@ -91,16 +96,16 @@ public class JobItemGiver extends JobInterface{ } return map; } - + public boolean giveItems(EntityPlayer player){ PlayerItemGiverData data = PlayerDataController.Instance.getPlayerData(player).itemgiverData; if(!canPlayerInteract(data)){ return false; } - + Vector items = new Vector(); Vector toGive = new Vector(); - + for(ItemStack is : inventory.items.values()) if(is != null) items.add(is.copy()); @@ -234,7 +239,7 @@ public class JobItemGiver extends JobInterface{ private boolean isDaily(){ return cooldownType == 2; } - + public boolean aiShouldExecute() { if(npc.isAttacking()) return false; @@ -242,7 +247,7 @@ public class JobItemGiver extends JobInterface{ if(ticks > 0) return false; ticks = 10; - + toCheck = npc.worldObj.getEntitiesWithinAABB(EntityPlayer.class, npc.boundingBox.expand(3, 3, 3)); toCheck.removeAll(recentlyChecked); @@ -260,11 +265,11 @@ public class JobItemGiver extends JobInterface{ } } } - + @Override public void killed() { // TODO Auto-generated method stub - + } private boolean interact(EntityPlayer player) { @@ -276,6 +281,6 @@ public class JobItemGiver extends JobInterface{ @Override public void delete() { // TODO Auto-generated method stub - + } }