Implemented scripted timers for players and NPCs.

This commit is contained in:
luisc committed 2021-10-20 23:04:52 -05:00
1 parent a993c8feff
commit 4de969f6c9
15 files changed
+327 -16

No files matched your search

@@ -87,6 +87,7 @@ import noppes.npcs.controllers.PlayerDataController;
import noppes.npcs.controllers.PlayerQuestData;
import noppes.npcs.controllers.QuestData;
import noppes.npcs.controllers.TransformData;
import noppes.npcs.entity.data.DataTimers;
import noppes.npcs.roles.JobBard;
import noppes.npcs.roles.JobFollower;
import noppes.npcs.roles.JobInterface;
@@ -115,6 +116,7 @@ public abstract class EntityNPCInterface extends EntityCreature implements IEnti
public DataInventory inventory;
public DataScript script;
public TransformData transform;
public DataTimers timers;
public String linkedName = "";
public long linkedLast = 0;
@@ -190,7 +192,7 @@ public abstract class EntityNPCInterface extends EntityCreature implements IEnti
inventory = new DataInventory(this);
transform = new TransformData(this);
script = new DataScript(this);
timers = new DataTimers(this);
this.getAttributeMap().registerAttribute(SharedMonsterAttributes.attackDamage);
this.getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue(stats.maxHealth);
@@ -226,6 +228,7 @@ public abstract class EntityNPCInterface extends EntityCreature implements IEnti
super.onUpdate();
if(this.ticksExisted % 10 == 0)
script.callScript(EnumScriptType.TICK);
this.timers.update();
}
public void setWorld(World world){
@@ -920,7 +923,7 @@ public abstract class EntityNPCInterface extends EntityCreature implements IEnti
stats.readToNBT(compound);
ai.readToNBT(compound);
script.readFromNBT(compound);
timers.readFromNBT(compound);
advanced.readToNBT(compound);
if (advanced.role != EnumRoleType.None && roleInterface != null)
roleInterface.readFromNBT(compound);
@@ -951,7 +954,7 @@ public abstract class EntityNPCInterface extends EntityCreature implements IEnti
stats.writeToNBT(compound);
ai.writeToNBT(compound);
script.writeToNBT(compound);
timers.writeToNBT(compound);
advanced.writeToNBT(compound);
if (advanced.role != EnumRoleType.None && roleInterface != null)
roleInterface.writeToNBT(compound);
@@ -12,6 +12,7 @@ import java.util.Map;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import noppes.npcs.EventHooks;
import noppes.npcs.entity.EntityNPCInterface;
import noppes.npcs.scripted.interfaces.ITimers;
import noppes.npcs.controllers.PlayerData;
import noppes.npcs.scripted.CustomNPCsException;
@@ -24,16 +25,29 @@ public class DataTimers implements ITimers {
this.parent = parent;
}
private void saveNbt() {
if(parent instanceof PlayerData) {
PlayerData playerData = (PlayerData) parent;
playerData.saveNBTData(null);
}
else if(parent instanceof PlayerData) {
PlayerData playerData = (PlayerData) parent;
playerData.saveNBTData(null);
}
}
public void start(int id, int ticks, boolean repeat) {
if(this.timers.containsKey(Integer.valueOf(id))) {
throw new CustomNPCsException("There is already a timer with id: " + id, new Object[0]);
} else {
this.timers.put(Integer.valueOf(id), new DataTimers.Timer(id, ticks, repeat));
saveNbt();
}
}
public void forceStart(int id, int ticks, boolean repeat) {
this.timers.put(Integer.valueOf(id), new DataTimers.Timer(id, ticks, repeat));
saveNbt();
}
public boolean has(int id) {
@@ -41,7 +55,9 @@ public class DataTimers implements ITimers {
}
public boolean stop(int id) {
return this.timers.remove(Integer.valueOf(id)) != null;
boolean remove = this.timers.remove(Integer.valueOf(id)) != null;
saveNbt();
return remove;
}
public void reset(int id) {
@@ -50,6 +66,7 @@ public class DataTimers implements ITimers {
throw new CustomNPCsException("There is no timer with id: " + id, new Object[0]);
} else {
timer.ticks = 0;
saveNbt();
}
}
@@ -71,14 +88,14 @@ public class DataTimers implements ITimers {
}
public void readFromNBT(NBTTagCompound compound) {
HashMap timers = new HashMap();
Map<Integer, DataTimers.Timer> timers = new HashMap();
NBTTagList list = compound.getTagList("NpcsTimers", 10);
for(int i = 0; i < list.tagCount(); ++i) {
NBTTagCompound c = list.getCompoundTagAt(i);
DataTimers.Timer t = new DataTimers.Timer(c.getInteger("ID"), c.getInteger("TimerTicks"), c.getBoolean("Repeat"));
t.ticks = c.getInteger("Ticks");
timers.put(Integer.valueOf(t.id), t);
timers.put(t.id, t);
}
this.timers = timers;
@@ -91,11 +108,12 @@ public class DataTimers implements ITimers {
DataTimers.Timer timer = (DataTimers.Timer)var1.next();
timer.update();
}
saveNbt();
}
public void clear() {
this.timers = new HashMap();
saveNbt();
}
class Timer {
@@ -120,7 +138,10 @@ public class DataTimers implements ITimers {
}
Object ob = DataTimers.this.parent;
if(ob instanceof PlayerData) {
if (ob instanceof EntityNPCInterface) {
EventHooks.onNPCTimer((EntityNPCInterface)ob, this.id);
}
else if(ob instanceof PlayerData) {
EventHooks.onPlayerTimer((PlayerData)ob, this.id);
}
}