Finish event cooldowns! woo

This commit is contained in:
Govindas 2020-02-24 20:13:11 +02:00
parent c119e8216a
commit c8ad91bd49
10 changed files with 253 additions and 28 deletions

View File

@ -2,28 +2,50 @@ package lt.govindas.skooldown;
import ch.njol.skript.Skript; import ch.njol.skript.Skript;
import ch.njol.skript.lang.ExpressionType; 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 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.conditions.CondIsCooldownOver;
import lt.govindas.skooldown.effects.EffEndCooldown;
import lt.govindas.skooldown.effects.EffStartCooldown; 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.ExprCooldown;
import lt.govindas.skooldown.expressions.ExprCooldownData;
import lt.govindas.skooldown.utilities.CleanupTimer; import lt.govindas.skooldown.utilities.CleanupTimer;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import lt.govindas.skooldown.utilities.Timer;
import javax.swing.*;
import java.util.HashMap; import java.util.HashMap;
public final class Skooldown extends JavaPlugin { public final class Skooldown extends JavaPlugin {
public static HashMap<String, Long> cooldowns = new HashMap<String, Long>(); public static HashMap<String, Long> cooldowns = new HashMap<String, Long>();
public static HashMap<String, Timer> eventCooldowns = new HashMap<String, javax.swing.Timer>(); public static HashMap<String, Timer> eventCooldowns = new HashMap<String, Timer>();
@Override @Override
public void onEnable() { public void onEnable() {
Skript.registerEffect(EffStartCooldown.class, "(create|start) [a] [(1¦event] cooldown %string% [with data %-string%] for %timespan%"); Skript.registerEffect(EffStartCooldown.class, "(create|start) [a] cooldown %string% for %timespan%", "(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(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<String, CooldownEndEvent>() {
@Override
public String get(CooldownEndEvent e) {
return e.getData();
}
}, 0);
getLogger().info("[Skooldown] Plugin enabled!"); getLogger().info("[Skooldown] Plugin enabled!");
new CleanupTimer(); new CleanupTimer();
} }

View File

@ -13,6 +13,7 @@ public class CondIsCooldownOver extends Condition {
private Expression<String> name; private Expression<String> name;
private Expression<String> data; private Expression<String> data;
private boolean eventCooldown = false; private boolean eventCooldown = false;
private String dataInput = "";
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
@ -48,9 +49,7 @@ public class CondIsCooldownOver extends Condition {
return isNegated(); return isNegated();
} else { } else {
String dataInput; if (data != null) dataInput = data.getSingle(e);
if (data == null) dataInput = "";
else dataInput = data.getSingle(e);
if (Skooldown.eventCooldowns.containsKey(name.getSingle(e) + dataInput)) return isNegated(); if (Skooldown.eventCooldowns.containsKey(name.getSingle(e) + dataInput)) return isNegated();
else return !isNegated(); else return !isNegated();

View File

@ -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<String> name;
private Expression<String> data;
private boolean eventCooldown = false;
private String dataInput = "";
@SuppressWarnings("unchecked")
@Override
public boolean init(Expression<?>[] expr, int matchedPattern, Kleenean paramKleenean, ParseResult paramParseResult) {
name = (Expression<String>) expr[0];
int mark = paramParseResult.mark;
if (mark == 1) {
eventCooldown = true;
}
if (expr.length > 2) {
data = (Expression<String>) 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);
}
}
}
}

View File

@ -7,12 +7,12 @@ import ch.njol.skript.util.Timespan;
import ch.njol.util.Kleenean; import ch.njol.util.Kleenean;
import lt.govindas.skooldown.Skooldown; import lt.govindas.skooldown.Skooldown;
import lt.govindas.skooldown.events.CooldownEndEvent; import lt.govindas.skooldown.events.CooldownEndEvent;
import lt.govindas.skooldown.utilities.Timer;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
@ -22,6 +22,7 @@ public class EffStartCooldown extends Effect {
private Expression<Timespan> time; private Expression<Timespan> time;
private Expression<String> data; private Expression<String> data;
private boolean eventCooldown = false; private boolean eventCooldown = false;
private String dataInput = "";
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
@ -49,14 +50,12 @@ public class EffStartCooldown extends Effect {
@Override @Override
protected void execute(Event e) { protected void execute(Event e) {
String dataInput;
if (data == null) dataInput = "";
else dataInput = data.getSingle(e);
if (!eventCooldown) { if (!eventCooldown) {
Skooldown.cooldowns.put(name.getSingle(e), System.currentTimeMillis() + time.getSingle(e).getMilliSeconds()); Skooldown.cooldowns.put(name.getSingle(e), System.currentTimeMillis() + time.getSingle(e).getMilliSeconds());
} else { } else {
if (data != null) dataInput = data.getSingle(e);
Timer timer = new Timer((int) time.getSingle(e).getMilliSeconds(), new ActionListener() { Timer timer = new Timer((int) time.getSingle(e).getMilliSeconds(), new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {

View File

@ -17,12 +17,15 @@ public final class CooldownEndEvent extends Event {
public String getData() { return data; } public String getData() { return data; }
public String getName() { return name;} public String getName() { return name;}
public long getDelay() { return delay;} public long getDelay() { return delay;}
public HandlerList getHandlers() { public HandlerList getHandlers() {
return handlers; return handlers;
} }
public boolean matches(String id) {
return id.equalsIgnoreCase(this.getName() + this.getData());
}
public static HandlerList getHandlerList() { public static HandlerList getHandlerList() {
return handlers; return handlers;
} }

View File

@ -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;
}
}

View File

@ -12,11 +12,15 @@ import ch.njol.util.coll.CollectionUtils;
import lt.govindas.skooldown.Skooldown; import lt.govindas.skooldown.Skooldown;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable; 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<Timespan> { public class ExprCooldown extends SimpleExpression<Timespan> {
private Expression<String> name; private Expression<String> name;
private Expression<String> data;
private boolean eventCooldown = false;
private String dataInput = "";
@Override @Override
public boolean isSingle() { public boolean isSingle() {
@ -32,26 +36,48 @@ public class ExprCooldown extends SimpleExpression<Timespan> {
@Override @Override
public boolean init(Expression<?>[] expr, int matchedPattern, Kleenean paramKleenean, ParseResult paramParseResult) { public boolean init(Expression<?>[] expr, int matchedPattern, Kleenean paramKleenean, ParseResult paramParseResult) {
name = (Expression<String>) expr[0]; name = (Expression<String>) 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<String>) expr[1]; }
return true; return true;
} }
@Override @Override
public String toString(@Nullable Event e, boolean debug) { public String toString(@Nullable Event e, boolean debug) {
return "get cooldown " + name.getSingle(e); return "get cooldown ";
} }
@Override @Override
protected Timespan[] get(Event e) { 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 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 < System.currentTimeMillis()) { if (cooldown < System.currentTimeMillis()) {
Skooldown.cooldowns.remove(name.getSingle(e)); Skooldown.cooldowns.remove(name.getSingle(e) + dataInput);
return new Timespan[]{new Timespan(0)}; return new Timespan[]{new Timespan(0)};
} }
return new Timespan[]{new Timespan(Skooldown.cooldowns.get(name.getSingle(e)) - System.currentTimeMillis())}; //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())};
}
} }
@Override @Override
@ -63,7 +89,17 @@ public class ExprCooldown extends SimpleExpression<Timespan> {
case REMOVE_ALL: case REMOVE_ALL:
case DELETE: case DELETE:
case RESET: 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; break;
case ADD: case ADD:
Long cooldown = Skooldown.cooldowns.get(name.getSingle(e)); Long cooldown = Skooldown.cooldowns.get(name.getSingle(e));

View File

@ -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<String> {
@Override
public boolean isSingle() {
return true;
}
@Override
public Class<? extends String> 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; }
}

View File

@ -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 <code>listener</code> is not <code>null</code>,
* 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 <code>null</code>
* @see #addActionListener
* @see #setInitialDelay
* @see #setRepeats
*/
public Timer(int delay, ActionListener listener) {
super(delay, listener);
}
public long getEndDate() {
return startTime + this.getDelay();
}
}

View File

@ -1,4 +1,4 @@
name: Skooldown name: Skooldown
main: lt.govindas.skooldown.Skooldown main: lt.govindas.skooldown.Skooldown
version: 0.0.1 version: 1.0.0
authors: [Govindas, Nashoba24] author: Govindas