From 948fc4f497e9dae86b3b90e97d4e06cc345a6ebf Mon Sep 17 00:00:00 2001 From: "Luis C.M" Date: Mon, 4 Jul 2022 13:05:43 -0500 Subject: [PATCH] Some timer functions now throw an exception when the timer they're trying to access does not exist. --- .../noppes/npcs/entity/data/DataTimers.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/main/java/noppes/npcs/entity/data/DataTimers.java b/src/main/java/noppes/npcs/entity/data/DataTimers.java index 85c62217c..ab55cb573 100644 --- a/src/main/java/noppes/npcs/entity/data/DataTimers.java +++ b/src/main/java/noppes/npcs/entity/data/DataTimers.java @@ -42,11 +42,12 @@ public class DataTimers implements ITimers { } public boolean stop(int id) { - boolean remove = this.timers.remove(Integer.valueOf(id)) != null; - return remove; + return this.timers.remove(Integer.valueOf(id)) != null; } public void reset(int id) { + timerException(id); + DataTimers.Timer timer = (DataTimers.Timer)this.timers.get(Integer.valueOf(id)); if(timer == null) { throw new CustomNPCsException("There is no timer with id: " + id, new Object[0]); @@ -56,10 +57,13 @@ public class DataTimers implements ITimers { } public int ticks(int id){ + timerException(id); return this.timers.get(Integer.valueOf(id)).ticks; } public void setTicks(int id, int ticks){ + timerException(id); + DataTimers.Timer timer = (DataTimers.Timer)this.timers.get(Integer.valueOf(id)); if(ticks < 0) ticks = 0; @@ -70,10 +74,13 @@ public class DataTimers implements ITimers { } public int maxTicks(int id){ + timerException(id); return this.timers.get(Integer.valueOf(id)).timerTicks; } public void setMaxTicks(int id, int maxTicks){ + timerException(id); + DataTimers.Timer timer = (DataTimers.Timer)this.timers.get(Integer.valueOf(id)); if(maxTicks < 0) maxTicks = 0; @@ -84,10 +91,12 @@ public class DataTimers implements ITimers { } public boolean repeats(int id){ + timerException(id); return this.timers.get(Integer.valueOf(id)).repeat; } public void setRepeats(int id, boolean repeat){ + timerException(id); this.timers.get(Integer.valueOf(id)).repeat = repeat; } @@ -108,6 +117,12 @@ public class DataTimers implements ITimers { compound.setTag("NpcsTimers", list); } + private void timerException(int id) { + if (!this.timers.containsKey(id)) { + throw new CustomNPCsException("Timer with id "+id+" does not exist."); + } + } + public void readFromNBT(NBTTagCompound compound) { Map timers = new HashMap(); NBTTagList list = compound.getTagList("NpcsTimers", 10);