From c8ad91bd498b86d5bd1e30a26fb999b9a0802014 Mon Sep 17 00:00:00 2001 From: Govindas Date: Mon, 24 Feb 2020 20:13:11 +0200 Subject: [PATCH] Finish event cooldowns! woo --- src/lt/govindas/skooldown/Skooldown.java | 38 ++++++++++--- .../conditions/CondIsCooldownOver.java | 5 +- .../skooldown/effects/EffEndCooldown.java | 52 +++++++++++++++++ .../skooldown/effects/EffStartCooldown.java | 7 +-- .../skooldown/events/CooldownEndEvent.java | 5 +- .../skooldown/events/EvtCooldown.java | 39 +++++++++++++ .../skooldown/expressions/ExprCooldown.java | 56 +++++++++++++++---- .../expressions/ExprCooldownData.java | 48 ++++++++++++++++ .../govindas/skooldown/utilities/Timer.java | 27 +++++++++ src/plugin.yml | 4 +- 10 files changed, 253 insertions(+), 28 deletions(-) create mode 100644 src/lt/govindas/skooldown/effects/EffEndCooldown.java create mode 100644 src/lt/govindas/skooldown/events/EvtCooldown.java create mode 100644 src/lt/govindas/skooldown/expressions/ExprCooldownData.java create mode 100644 src/lt/govindas/skooldown/utilities/Timer.java diff --git a/src/lt/govindas/skooldown/Skooldown.java b/src/lt/govindas/skooldown/Skooldown.java index 396d406..aefffe7 100644 --- a/src/lt/govindas/skooldown/Skooldown.java +++ b/src/lt/govindas/skooldown/Skooldown.java @@ -2,28 +2,50 @@ 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.effects.EffStartEventCooldown; +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 javax.swing.*; +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(); - + public static HashMap eventCooldowns = new HashMap(); @Override public void onEnable() { - Skript.registerEffect(EffStartCooldown.class, "(create|start) [a] [(1¦event] cooldown %string% [with data %-string%] for %timespan%"); - Skript.registerCondition(CondIsCooldownOver.class, "[the] [(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, "cooldown %string%"); + Skript.registerEffect(EffStartCooldown.class, "(create|start) [a] cooldown %string% for %timespan%", "(create|start) [a] (1¦event) cooldown %string% [with data %-string%] for %timespan%"); + + + Skript.registerEffect(EffEndCooldown.class, "(reset|stop|delete|clear) [(1¦event)] cooldown %string% [with data %-string%]"); + + Skript.registerCondition(CondIsCooldownOver.class, "[the] [(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%]"); + //TODO make the event, but I don't know how right now + 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!"); new CleanupTimer(); } diff --git a/src/lt/govindas/skooldown/conditions/CondIsCooldownOver.java b/src/lt/govindas/skooldown/conditions/CondIsCooldownOver.java index 45a1efd..53a79ed 100644 --- a/src/lt/govindas/skooldown/conditions/CondIsCooldownOver.java +++ b/src/lt/govindas/skooldown/conditions/CondIsCooldownOver.java @@ -13,6 +13,7 @@ public class CondIsCooldownOver extends Condition { private Expression name; private Expression data; private boolean eventCooldown = false; + private String dataInput = ""; @SuppressWarnings("unchecked") @Override @@ -48,9 +49,7 @@ public class CondIsCooldownOver extends Condition { return isNegated(); } else { - String dataInput; - if (data == null) dataInput = ""; - else dataInput = data.getSingle(e); + if (data != null) dataInput = data.getSingle(e); if (Skooldown.eventCooldowns.containsKey(name.getSingle(e) + dataInput)) return isNegated(); else return !isNegated(); diff --git a/src/lt/govindas/skooldown/effects/EffEndCooldown.java b/src/lt/govindas/skooldown/effects/EffEndCooldown.java new file mode 100644 index 0000000..46f95af --- /dev/null +++ b/src/lt/govindas/skooldown/effects/EffEndCooldown.java @@ -0,0 +1,52 @@ +package lt.govindas.skooldown.effects; + +import ch.njol.skript.lang.Effect; +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) { + 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); + } + + @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); + } + } + } +} diff --git a/src/lt/govindas/skooldown/effects/EffStartCooldown.java b/src/lt/govindas/skooldown/effects/EffStartCooldown.java index 67d4456..acae1fc 100644 --- a/src/lt/govindas/skooldown/effects/EffStartCooldown.java +++ b/src/lt/govindas/skooldown/effects/EffStartCooldown.java @@ -7,12 +7,12 @@ 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 javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @@ -22,6 +22,7 @@ public class EffStartCooldown extends Effect { private Expression time; private Expression data; private boolean eventCooldown = false; + private String dataInput = ""; @SuppressWarnings("unchecked") @Override @@ -49,14 +50,12 @@ public class EffStartCooldown extends Effect { @Override protected void execute(Event e) { - String dataInput; - if (data == null) dataInput = ""; - else dataInput = data.getSingle(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) { diff --git a/src/lt/govindas/skooldown/events/CooldownEndEvent.java b/src/lt/govindas/skooldown/events/CooldownEndEvent.java index 545d41f..f169501 100644 --- a/src/lt/govindas/skooldown/events/CooldownEndEvent.java +++ b/src/lt/govindas/skooldown/events/CooldownEndEvent.java @@ -17,12 +17,15 @@ public final class CooldownEndEvent extends Event { 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 new file mode 100644 index 0000000..6319e17 --- /dev/null +++ b/src/lt/govindas/skooldown/events/EvtCooldown.java @@ -0,0 +1,39 @@ +package lt.govindas.skooldown.events; + +import ch.njol.skript.lang.Expression; +import lt.govindas.skooldown.Skooldown; +import org.bukkit.event.Event; + +import ch.njol.skript.lang.Literal; +import ch.njol.skript.lang.SkriptEvent; +import ch.njol.skript.lang.SkriptParser.ParseResult; +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/ExprCooldown.java b/src/lt/govindas/skooldown/expressions/ExprCooldown.java index 94df1e9..c89fe88 100644 --- a/src/lt/govindas/skooldown/expressions/ExprCooldown.java +++ b/src/lt/govindas/skooldown/expressions/ExprCooldown.java @@ -12,11 +12,15 @@ 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 support +//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() { @@ -32,26 +36,48 @@ public class ExprCooldown extends SimpleExpression { @Override public boolean init(Expression[] expr, int matchedPattern, Kleenean paramKleenean, ParseResult paramParseResult) { 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]; } return true; } @Override public String toString(@Nullable Event e, boolean debug) { - return "get cooldown " + name.getSingle(e); + return "get cooldown "; } @Override protected Timespan[] get(Event e) { - Long cooldown = Skooldown.cooldowns.get(name.getSingle(e)); + if (data != null) dataInput = data.getSingle(e); + Long cooldown; + Timer timerCooldown; + if (!eventCooldown) { + cooldown = Skooldown.cooldowns.get(name.getSingle(e) + dataInput); - //if cooldown isn't created, will return that it is over - if (cooldown == null) return new Timespan[]{new Timespan(0)}; + //if cooldown isn't created, will return that it is over - if (cooldown < System.currentTimeMillis()) { - Skooldown.cooldowns.remove(name.getSingle(e)); - return new Timespan[]{new Timespan(0)}; + if (cooldown == null) return new Timespan[]{new Timespan(0)}; + + //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())}; } - return new Timespan[]{new Timespan(Skooldown.cooldowns.get(name.getSingle(e)) - System.currentTimeMillis())}; } @Override @@ -63,7 +89,17 @@ public class ExprCooldown extends SimpleExpression { case REMOVE_ALL: case DELETE: case RESET: - Skooldown.cooldowns.remove(name.getSingle(e)); + 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); + } + } break; case ADD: Long cooldown = Skooldown.cooldowns.get(name.getSingle(e)); diff --git a/src/lt/govindas/skooldown/expressions/ExprCooldownData.java b/src/lt/govindas/skooldown/expressions/ExprCooldownData.java new file mode 100644 index 0000000..0484c78 --- /dev/null +++ b/src/lt/govindas/skooldown/expressions/ExprCooldownData.java @@ -0,0 +1,48 @@ +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; + +//TODO add event cooldown editing support, such as remove/add time +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/Timer.java b/src/lt/govindas/skooldown/utilities/Timer.java new file mode 100644 index 0000000..b8723ec --- /dev/null +++ b/src/lt/govindas/skooldown/utilities/Timer.java @@ -0,0 +1,27 @@ +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 b498966..db2de56 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,4 +1,4 @@ name: Skooldown main: lt.govindas.skooldown.Skooldown -version: 0.0.1 -authors: [Govindas, Nashoba24] \ No newline at end of file +version: 1.0.0 +author: Govindas \ No newline at end of file