Refactor the addon, remove features that I've decided to be unneeded.

This commit is contained in:
Govindass 2020-11-02 10:37:42 +02:00
parent c18a94ac38
commit 2c4390998f
12 changed files with 63 additions and 332 deletions

View File

@ -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<String, Long> cooldowns = new HashMap<String, Long>();
public static HashMap<String, Timer> eventCooldowns = new HashMap<String, Timer>();
@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<String, CooldownEndEvent>() {
@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!");
}
}

View File

@ -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<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) {
public boolean init(final Expression<?>[] expr, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
name = (Expression<String>) expr[0];
int mark = paramParseResult.mark;
if (mark == 1) eventCooldown = true;
if (expr.length > 1) data = (Expression<String>) 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();
}
}

View File

@ -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<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) {
public boolean init(final Expression<?>[] expr, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
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);
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));
}
}

View File

@ -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<String> name;
private Expression<Timespan> time;
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) {
public boolean init(final Expression<?>[] expr, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
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];
time = (Expression<Timespan>) expr[2];
} else {
time = (Expression<Timespan>) expr[1];
}
time = (Expression<Timespan>) 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());
}
}

View File

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

View File

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

View File

@ -1,4 +1,5 @@
package lt.govindas.skooldown.expressions;
public class ExprAllCooldowns {
//TODO
}

View File

@ -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<Timespan> {
private Expression<String> name;
private Expression<String> data;
private boolean eventCooldown = false;
private String dataInput = "";
@Override
public boolean isSingle() {
@ -34,50 +28,31 @@ public class ExprCooldown extends SimpleExpression<Timespan> {
@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<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 "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<Timespan> {
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;

View File

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

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

View File

@ -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 <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,5 @@
name: Skooldown
main: lt.govindas.skooldown.Skooldown
version: 1.0.0
author: Govindas
version: 1.1.0
author: Govindas
depend: [Skript]