diff --git a/src/lt/govindas/skooldown/Skooldown.java b/src/lt/govindas/skooldown/Skooldown.java index bb3149b..d0ab9b2 100644 --- a/src/lt/govindas/skooldown/Skooldown.java +++ b/src/lt/govindas/skooldown/Skooldown.java @@ -2,56 +2,36 @@ package lt.govindas.skooldown; import ch.njol.skript.Skript; import ch.njol.skript.lang.ExpressionType; -import ch.njol.skript.lang.util.SimpleEvent; -import ch.njol.skript.lang.util.SimpleExpression; -import ch.njol.skript.registrations.EventValues; -import ch.njol.skript.util.Getter; import ch.njol.skript.util.Timespan; -import com.google.common.collect.ArrayListMultimap; -import com.google.common.collect.Multimap; -import com.sun.applet2.AppletParameters; import lt.govindas.skooldown.conditions.CondIsCooldownOver; import lt.govindas.skooldown.effects.EffEndCooldown; import lt.govindas.skooldown.effects.EffStartCooldown; -import lt.govindas.skooldown.events.CooldownEndEvent; -import lt.govindas.skooldown.events.EvtCooldown; import lt.govindas.skooldown.expressions.ExprCooldown; -import lt.govindas.skooldown.expressions.ExprCooldownData; import lt.govindas.skooldown.utilities.CleanupTimer; -import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; -import lt.govindas.skooldown.utilities.Timer; + import java.util.HashMap; public final class Skooldown extends JavaPlugin { public static HashMap cooldowns = new HashMap(); - public static HashMap eventCooldowns = new HashMap(); - @Override public void onEnable() { - Skript.registerEffect(EffStartCooldown.class, "(create|start) [a] cooldown %string% for %timespan%", "(create|start) [a] (1¦event) cooldown %string% [with data %-string%] for %timespan%"); + Skript.registerAddon(this); + Skript.registerEffect(EffStartCooldown.class, "(create|start) [a] cooldown %string% for %timespan%", "(create|start) [a] cooldown %string% for %timespan%"); - Skript.registerEffect(EffEndCooldown.class, "(reset|stop|delete|clear) [(1¦event)] cooldown %string% [with data %-string%]"); + Skript.registerEffect(EffEndCooldown.class, "(reset|stop|delete|clear) cooldown %string%"); - Skript.registerCondition(CondIsCooldownOver.class, "[(1¦event)] cooldown %string% [with data %-string%] (is|has) (finished|over|done)", "[the] [(1¦event)] cooldown %string% [with data %-string%] is(n't| not) unfinished)", "[the] [(1¦event)] cooldown %string% [with data %-string%] is(n't| not) (finished|over|done)", "[the] [(1¦event)] cooldown %string% [with data %-string%] is unfinished"); - Skript.registerExpression(ExprCooldown.class, Timespan.class, ExpressionType.PROPERTY, "[(1¦event)] cooldown %string% [with data %-string%]"); + Skript.registerCondition(CondIsCooldownOver.class, "cooldown %string% (is|has) (finished|over|done)", "cooldown %string% is(n't| not) unfinished)", "cooldown %string% is(n't| not) (finished|over|done)", "cooldown %string% is unfinished"); + Skript.registerExpression(ExprCooldown.class, Timespan.class, ExpressionType.PROPERTY, "cooldown %string%"); - Skript.registerEvent("Cooldown End", EvtCooldown.class, CooldownEndEvent.class, "(finish|end|complete) of cooldown %string%"); - Skript.registerExpression(ExprCooldownData.class, String.class, ExpressionType.SIMPLE, "cooldown data"); - EventValues.registerEventValue(CooldownEndEvent.class, String.class, new Getter() { - @Override - public String get(CooldownEndEvent e) { - return e.getData(); - } - }, 0); - getLogger().info("[Skooldown] Plugin enabled!"); + getLogger().info("Skript addon enabled!"); new CleanupTimer(); } @Override public void onDisable() { - getLogger().info("[Skooldown] Plugin disabled!"); + getLogger().info("Skript addon disabled!"); } } diff --git a/src/lt/govindas/skooldown/conditions/CondIsCooldownOver.java b/src/lt/govindas/skooldown/conditions/CondIsCooldownOver.java index 53a79ed..147ba78 100644 --- a/src/lt/govindas/skooldown/conditions/CondIsCooldownOver.java +++ b/src/lt/govindas/skooldown/conditions/CondIsCooldownOver.java @@ -6,53 +6,39 @@ import ch.njol.skript.lang.SkriptParser.ParseResult; import ch.njol.util.Kleenean; import lt.govindas.skooldown.Skooldown; import org.bukkit.event.Event; -import org.jetbrains.annotations.Nullable; public class CondIsCooldownOver extends Condition { private Expression name; - private Expression data; - private boolean eventCooldown = false; - private String dataInput = ""; @SuppressWarnings("unchecked") @Override - public boolean init(Expression[] expr, int matchedPattern, Kleenean paramKleenean, ParseResult paramParseResult) { + public boolean init(final Expression[] expr, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) { name = (Expression) expr[0]; - int mark = paramParseResult.mark; - if (mark == 1) eventCooldown = true; - if (expr.length > 1) data = (Expression) expr[1]; setNegated(matchedPattern == 2 || matchedPattern == 3); return true; } @Override - public String toString(@Nullable Event e, boolean debug) { - return "is cooldown over " + name; + public String toString(Event event, boolean debug) { + return "is cooldown over " + name.toString(event, debug); } @Override public boolean check(Event e) { - if (!eventCooldown) { - Long cooldown = Skooldown.cooldowns.get(name.getSingle(e)); + Long cooldown = Skooldown.cooldowns.get(name.getSingle(e)); - //if cooldown isn't created, will return that it is over - if (cooldown == null) return !isNegated(); + //if cooldown isn't created, will return that it is over + if (cooldown == null) return !isNegated(); - if (cooldown < System.currentTimeMillis()) { - Skooldown.cooldowns.remove(name.getSingle(e)); - //will return that cooldown is over - return !isNegated(); - } - //will return that cooldown is not over - - return isNegated(); - } else { - if (data != null) dataInput = data.getSingle(e); - - if (Skooldown.eventCooldowns.containsKey(name.getSingle(e) + dataInput)) return isNegated(); - else return !isNegated(); + if (cooldown < System.currentTimeMillis()) { + Skooldown.cooldowns.remove(name.getSingle(e)); + //will return that cooldown is over + return !isNegated(); } + //will return that cooldown is not over + + return isNegated(); } } \ No newline at end of file diff --git a/src/lt/govindas/skooldown/effects/EffEndCooldown.java b/src/lt/govindas/skooldown/effects/EffEndCooldown.java index 46f95af..f1e0d1d 100644 --- a/src/lt/govindas/skooldown/effects/EffEndCooldown.java +++ b/src/lt/govindas/skooldown/effects/EffEndCooldown.java @@ -5,48 +5,27 @@ import ch.njol.skript.lang.Expression; import ch.njol.skript.lang.SkriptParser.ParseResult; import ch.njol.util.Kleenean; import lt.govindas.skooldown.Skooldown; -import lt.govindas.skooldown.utilities.Timer; import org.bukkit.event.Event; -import org.jetbrains.annotations.Nullable; public class EffEndCooldown extends Effect { private Expression name; - private Expression data; - private boolean eventCooldown = false; - private String dataInput = ""; @SuppressWarnings("unchecked") @Override - public boolean init(Expression[] expr, int matchedPattern, Kleenean paramKleenean, ParseResult paramParseResult) { + public boolean init(final Expression[] expr, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) { name = (Expression) expr[0]; - int mark = paramParseResult.mark; - if (mark == 1) { - eventCooldown = true; - } - if (expr.length > 2) { - data = (Expression) expr[1]; - } return true; } @Override - public String toString(@Nullable Event e, boolean debug) { - return "end cooldown " + name.getSingle(e); + public String toString(Event event, boolean debug) { + return "end cooldown " + name.toString(event, debug); } @Override protected void execute(Event e) { - - if (!eventCooldown) { Skooldown.cooldowns.remove(name.getSingle(e)); } - else { - if (data != null) dataInput = data.getSingle(e); - Timer timer = Skooldown.eventCooldowns.get(name.getSingle(e) + dataInput); - - if (timer != null) { - timer.stop(); - Skooldown.eventCooldowns.remove(name.getSingle(e) + dataInput); - } - } + Skooldown.cooldowns.remove(name.getSingle(e)); } } + diff --git a/src/lt/govindas/skooldown/effects/EffStartCooldown.java b/src/lt/govindas/skooldown/effects/EffStartCooldown.java index acae1fc..d1552cb 100644 --- a/src/lt/govindas/skooldown/effects/EffStartCooldown.java +++ b/src/lt/govindas/skooldown/effects/EffStartCooldown.java @@ -6,66 +6,28 @@ import ch.njol.skript.lang.SkriptParser.ParseResult; import ch.njol.skript.util.Timespan; import ch.njol.util.Kleenean; import lt.govindas.skooldown.Skooldown; -import lt.govindas.skooldown.events.CooldownEndEvent; -import lt.govindas.skooldown.utilities.Timer; -import org.bukkit.Bukkit; import org.bukkit.event.Event; -import org.jetbrains.annotations.Nullable; - -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; - public class EffStartCooldown extends Effect { private Expression name; private Expression time; - private Expression data; - private boolean eventCooldown = false; - private String dataInput = ""; @SuppressWarnings("unchecked") @Override - public boolean init(Expression[] expr, int matchedPattern, Kleenean paramKleenean, ParseResult paramParseResult) { + public boolean init(final Expression[] expr, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) { name = (Expression) expr[0]; - int mark = paramParseResult.mark; - if (mark == 1) { - eventCooldown = true; - } - //TODO test if this is right, maybe expression IDs are solid - if (expr.length > 2) { - data = (Expression) expr[1]; - time = (Expression) expr[2]; - } else { - time = (Expression) expr[1]; - } + time = (Expression) expr[1]; return true; } @Override - public String toString(@Nullable Event e, boolean debug) { - return "start cooldown " + name.getSingle(e); + public String toString(Event event, boolean debug) { + return "start cooldown " + name.toString(event, debug) + " for " + time.toString(event, debug); } @Override protected void execute(Event e) { - - - - if (!eventCooldown) { - Skooldown.cooldowns.put(name.getSingle(e), System.currentTimeMillis() + time.getSingle(e).getMilliSeconds()); - } else { - if (data != null) dataInput = data.getSingle(e); - Timer timer = new Timer((int) time.getSingle(e).getMilliSeconds(), new ActionListener() { - @Override - public void actionPerformed(ActionEvent arg0) { - Skooldown.eventCooldowns.remove(name.getSingle(e) + dataInput); - Bukkit.getServer().getPluginManager().callEvent(new CooldownEndEvent(name.getSingle(e), dataInput, time.getSingle(e).getMilliSeconds())); - } - }); - timer.setRepeats(false); - timer.start(); - Skooldown.eventCooldowns.put(name.getSingle(e) + dataInput, timer); - } + Skooldown.cooldowns.put(name.getSingle(e), System.currentTimeMillis() + time.getSingle(e).getMilliSeconds()); } } diff --git a/src/lt/govindas/skooldown/events/CooldownEndEvent.java b/src/lt/govindas/skooldown/events/CooldownEndEvent.java deleted file mode 100644 index f169501..0000000 --- a/src/lt/govindas/skooldown/events/CooldownEndEvent.java +++ /dev/null @@ -1,33 +0,0 @@ -package lt.govindas.skooldown.events; - -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -public final class CooldownEndEvent extends Event { - private static final HandlerList handlers = new HandlerList(); - private String data; - private String name; - private long delay; - - public CooldownEndEvent(String name, String data, long delay) { - this.data = data; - this.name = name; - this.delay = delay; - } - - public String getData() { return data; } - public String getName() { return name;} - - public long getDelay() { return delay;} - - public HandlerList getHandlers() { - return handlers; - } - public boolean matches(String id) { - return id.equalsIgnoreCase(this.getName() + this.getData()); - } - public static HandlerList getHandlerList() { - return handlers; - } -} - diff --git a/src/lt/govindas/skooldown/events/EvtCooldown.java b/src/lt/govindas/skooldown/events/EvtCooldown.java deleted file mode 100644 index d7cc40a..0000000 --- a/src/lt/govindas/skooldown/events/EvtCooldown.java +++ /dev/null @@ -1,36 +0,0 @@ -package lt.govindas.skooldown.events; - -import ch.njol.skript.lang.Literal; -import ch.njol.skript.lang.SkriptEvent; -import ch.njol.skript.lang.SkriptParser.ParseResult; -import org.bukkit.event.Event; -import org.jetbrains.annotations.Nullable; - -import java.util.Arrays; - -public class EvtCooldown extends SkriptEvent { - - String name; - - @Override - public boolean init(Literal[] lit, int arg1, ParseResult arg2) { - name = Arrays.toString(lit); - name = name.substring(1, name.length()-1); - return true; - } - @Override - public String toString(@Nullable Event event, boolean arg1) { - return "cooldown end event "; - } - - @Override - public boolean check(Event event) { - if (event instanceof CooldownEndEvent) { - return ((CooldownEndEvent) event).getName().equalsIgnoreCase(name); - } - return false; - } - - - -} \ No newline at end of file diff --git a/src/lt/govindas/skooldown/expressions/ExprAllCooldowns.java b/src/lt/govindas/skooldown/expressions/ExprAllCooldowns.java index 10b0763..a5f8a04 100644 --- a/src/lt/govindas/skooldown/expressions/ExprAllCooldowns.java +++ b/src/lt/govindas/skooldown/expressions/ExprAllCooldowns.java @@ -1,4 +1,5 @@ package lt.govindas.skooldown.expressions; public class ExprAllCooldowns { + //TODO } diff --git a/src/lt/govindas/skooldown/expressions/ExprCooldown.java b/src/lt/govindas/skooldown/expressions/ExprCooldown.java index 09783ca..9e0acb5 100644 --- a/src/lt/govindas/skooldown/expressions/ExprCooldown.java +++ b/src/lt/govindas/skooldown/expressions/ExprCooldown.java @@ -11,16 +11,10 @@ import ch.njol.util.Kleenean; import ch.njol.util.coll.CollectionUtils; import lt.govindas.skooldown.Skooldown; import org.bukkit.event.Event; -import org.jetbrains.annotations.Nullable; -import lt.govindas.skooldown.utilities.Timer; -//TODO add event cooldown editing support, such as remove/add time public class ExprCooldown extends SimpleExpression { private Expression name; - private Expression data; - private boolean eventCooldown = false; - private String dataInput = ""; @Override public boolean isSingle() { @@ -34,50 +28,31 @@ public class ExprCooldown extends SimpleExpression { @SuppressWarnings("unchecked") @Override - public boolean init(Expression[] expr, int matchedPattern, Kleenean paramKleenean, ParseResult paramParseResult) { + public boolean init(final Expression[] expr, final int matchedPattern, final Kleenean isDelayed, final ParseResult paramParseResult) { name = (Expression) expr[0]; - int mark = paramParseResult.mark; - if (mark == 1) { eventCooldown = true; } - - if (expr.length > 2) { data = (Expression) expr[1]; } return true; } @Override - public String toString(@Nullable Event e, boolean debug) { - return "get cooldown "; - } + public String toString(Event event, boolean debug) { return "get cooldown " + name.toString(event, debug); } @Override protected Timespan[] get(Event e) { - if (data != null) dataInput = data.getSingle(e); Long cooldown; - Timer timerCooldown; - if (!eventCooldown) { - cooldown = Skooldown.cooldowns.get(name.getSingle(e) + dataInput); + cooldown = Skooldown.cooldowns.get(name.getSingle(e)); - //if cooldown isn't created, will return that it is over + //if cooldown isn't created, will return that it is over - if (cooldown == null) return new Timespan[]{new Timespan(0)}; + if (cooldown == null) return new Timespan[]{new Timespan(0)}; - //if cooldown has expired, will return 0ms + //if cooldown has expired, will return 0ms - if (cooldown < System.currentTimeMillis()) { - Skooldown.cooldowns.remove(name.getSingle(e) + dataInput); - return new Timespan[]{new Timespan(0)}; - } - //return time left - return new Timespan[]{new Timespan(cooldown - System.currentTimeMillis())}; - } else { - timerCooldown = Skooldown.eventCooldowns.get(name.getSingle(e) + dataInput); - - //if cooldown isn't created, will return that it is over - - if (timerCooldown == null) return new Timespan[]{new Timespan(0)}; - //return time left - - return new Timespan[] {new Timespan(timerCooldown.getEndDate() - System.currentTimeMillis())}; + if (cooldown < System.currentTimeMillis()) { + Skooldown.cooldowns.remove(name.getSingle(e)); + return new Timespan[]{new Timespan(0)}; } + //return time left + return new Timespan[]{new Timespan(cooldown - System.currentTimeMillis())}; } @Override @@ -89,32 +64,19 @@ public class ExprCooldown extends SimpleExpression { case REMOVE_ALL: case DELETE: case RESET: - if (data != null) dataInput = data.getSingle(e); - if (!eventCooldown) { Skooldown.cooldowns.remove(name.getSingle(e)); } - else { - Timer timer = Skooldown.eventCooldowns.get(name.getSingle(e) + dataInput); - - if (timer != null) { - timer.stop(); - Skooldown.eventCooldowns.remove(name.getSingle(e) + dataInput); - } - } + Skooldown.cooldowns.remove(name.getSingle(e)); break; case ADD: - if (!eventCooldown) { - Long cooldown = Skooldown.cooldowns.get(name.getSingle(e)); - if (cooldown == null) { - Skooldown.cooldowns.put(name.getSingle(e), System.currentTimeMillis() + ((Timespan) changer[0]).getMilliSeconds()); - break; - } - Skooldown.cooldowns.put(name.getSingle(e), cooldown + ((Timespan) changer[0]).getMilliSeconds()); - break; - } else { - //todo make this stop the timer without calling event & start it with another time fitting to time elapsed and added time - } - case REMOVE: Long cooldown = Skooldown.cooldowns.get(name.getSingle(e)); + if (cooldown == null) { + Skooldown.cooldowns.put(name.getSingle(e), System.currentTimeMillis() + ((Timespan) changer[0]).getMilliSeconds()); + break; + } + Skooldown.cooldowns.put(name.getSingle(e), cooldown + ((Timespan) changer[0]).getMilliSeconds()); + break; + case REMOVE: + cooldown = Skooldown.cooldowns.get(name.getSingle(e)); //if removing from non-existent cooldown, do nothing if (cooldown == null) return; diff --git a/src/lt/govindas/skooldown/expressions/ExprCooldownData.java b/src/lt/govindas/skooldown/expressions/ExprCooldownData.java deleted file mode 100644 index 410ca24..0000000 --- a/src/lt/govindas/skooldown/expressions/ExprCooldownData.java +++ /dev/null @@ -1,47 +0,0 @@ -package lt.govindas.skooldown.expressions; - - -import ch.njol.skript.classes.Changer; -import ch.njol.skript.lang.Expression; -import ch.njol.skript.lang.SkriptParser.ParseResult; -import ch.njol.skript.lang.util.SimpleExpression; -import ch.njol.util.Kleenean; -import lt.govindas.skooldown.events.CooldownEndEvent; -import org.bukkit.event.Event; -import org.jetbrains.annotations.Nullable; - -public class ExprCooldownData extends SimpleExpression { - @Override - public boolean isSingle() { - return true; - } - - @Override - public Class getReturnType() { - return String.class; - } - - @SuppressWarnings("unchecked") - @Override - public boolean init(Expression[] expr, int matchedPattern, Kleenean paramKleenean, ParseResult paramParseResult) { - return true; - } - - @Override - public String toString(@Nullable Event e, boolean debug) { - return "cooldown data "; - } - - @Override - protected String[] get(Event e) { - if (e instanceof CooldownEndEvent){ - return new String[]{((CooldownEndEvent) e).getData()}; - } - return null; - } - - - @Override - public Class[] acceptChange(final Changer.ChangeMode mode) { return null; } -} - diff --git a/src/lt/govindas/skooldown/utilities/CleanupTimer.java b/src/lt/govindas/skooldown/utilities/CleanupTimer.java index 0bd8eaf..c303f83 100644 --- a/src/lt/govindas/skooldown/utilities/CleanupTimer.java +++ b/src/lt/govindas/skooldown/utilities/CleanupTimer.java @@ -1,17 +1,19 @@ package lt.govindas.skooldown.utilities; import lt.govindas.skooldown.Skooldown; +import org.bukkit.Bukkit; import java.util.Iterator; import java.util.Map; import java.util.Timer; import java.util.TimerTask; +import java.util.logging.Level; public class CleanupTimer { - //Hourly Timer to prevent memory leaks + //Regular Cleanup Timer to prevent memory leaks public CleanupTimer() { Timer timer = new Timer(); - TimerTask hourlyTask = new TimerTask() { + TimerTask regularTask = new TimerTask() { int i = 0; @Override @@ -29,12 +31,13 @@ public class CleanupTimer { } } if (i > 0) { - System.out.println("[Skooldown Hourly Memory Cleanup] " + i + " finished cooldowns cleared from memory."); + Bukkit.getLogger().log(Level.INFO, "[Skooldown Regular Memory Cleanup] " + i + " finished cooldowns cleared from memory."); } } }; - //hourly schedule - timer.schedule(hourlyTask, 100, 1000 * 60 * 60); + //regular schedule + int minutes = 30; + timer.schedule(regularTask, 100, 1000 * 60 * minutes); } } diff --git a/src/lt/govindas/skooldown/utilities/Timer.java b/src/lt/govindas/skooldown/utilities/Timer.java deleted file mode 100644 index b8723ec..0000000 --- a/src/lt/govindas/skooldown/utilities/Timer.java +++ /dev/null @@ -1,27 +0,0 @@ -package lt.govindas.skooldown.utilities; - -import java.awt.event.ActionListener; - -public class Timer extends javax.swing.Timer { - long startTime = System.currentTimeMillis(); - /** - * Creates a {@code Timer} and initializes both the initial delay and - * between-event delay to {@code delay} milliseconds. If {@code delay} - * is less than or equal to zero, the timer fires as soon as it - * is started. If listener is not null, - * it's registered as an action listener on the timer. - * - * @param delay milliseconds for the initial and between-event delay - * @param listener an initial listener; can be null - * @see #addActionListener - * @see #setInitialDelay - * @see #setRepeats - */ - public Timer(int delay, ActionListener listener) { - super(delay, listener); - } - public long getEndDate() { - return startTime + this.getDelay(); - - } -} diff --git a/src/plugin.yml b/src/plugin.yml index db2de56..4f17523 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,4 +1,5 @@ name: Skooldown main: lt.govindas.skooldown.Skooldown -version: 1.0.0 -author: Govindas \ No newline at end of file +version: 1.1.0 +author: Govindas +depend: [Skript] \ No newline at end of file