Remove Weird Global Item Givers / Fixes Item Givers

This commit is contained in:
Kam committed 2024-03-05 13:51:03 -05:00
1 parent 2dfb383245
commit 6f6d4c041b
6 files changed
+68 -109

No files matched your search

+16 -32
View File
@@ -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);
@@ -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<Integer, JobItemGiver> 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;
}
}
@@ -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<IJobItemGiver> 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]);
}
}
@@ -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);
@@ -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<String> lines = new ArrayList<String>();
private int ticks = 10;
private List<EntityPlayer> recentlyChecked = new ArrayList<EntityPlayer>();
private List<EntityPlayer> 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<ItemStack> items = new Vector<ItemStack>();
Vector<ItemStack> toGive = new Vector<ItemStack>();
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
}
}