2020-02-15 15:39:44 +00:00
|
|
|
package lt.govindas.skooldown.expressions;
|
|
|
|
|
|
|
|
|
|
|
|
import ch.njol.skript.classes.Changer;
|
|
|
|
import ch.njol.skript.classes.Changer.ChangeMode;
|
|
|
|
import ch.njol.skript.lang.Expression;
|
|
|
|
import ch.njol.skript.lang.SkriptParser.ParseResult;
|
|
|
|
import ch.njol.skript.lang.util.SimpleExpression;
|
|
|
|
import ch.njol.skript.util.Timespan;
|
|
|
|
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;
|
2020-02-24 18:13:11 +00:00
|
|
|
import lt.govindas.skooldown.utilities.Timer;
|
2020-02-15 15:39:44 +00:00
|
|
|
|
2020-02-24 18:13:11 +00:00
|
|
|
//TODO add event cooldown editing support, such as remove/add time
|
2020-02-15 15:39:44 +00:00
|
|
|
public class ExprCooldown extends SimpleExpression<Timespan> {
|
|
|
|
|
|
|
|
private Expression<String> name;
|
2020-02-24 18:13:11 +00:00
|
|
|
private Expression<String> data;
|
|
|
|
private boolean eventCooldown = false;
|
|
|
|
private String dataInput = "";
|
2020-02-15 15:39:44 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isSingle() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Class<? extends Timespan> getReturnType() {
|
|
|
|
return Timespan.class;
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
@Override
|
|
|
|
public boolean init(Expression<?>[] expr, int matchedPattern, Kleenean paramKleenean, ParseResult paramParseResult) {
|
|
|
|
name = (Expression<String>) expr[0];
|
2020-02-24 18:13:11 +00:00
|
|
|
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]; }
|
2020-02-15 15:39:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString(@Nullable Event e, boolean debug) {
|
2020-02-24 18:13:11 +00:00
|
|
|
return "get cooldown ";
|
2020-02-15 15:39:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Timespan[] get(Event e) {
|
2020-02-24 18:13:11 +00:00
|
|
|
if (data != null) dataInput = data.getSingle(e);
|
|
|
|
Long cooldown;
|
|
|
|
Timer timerCooldown;
|
|
|
|
if (!eventCooldown) {
|
|
|
|
cooldown = Skooldown.cooldowns.get(name.getSingle(e) + dataInput);
|
2020-02-15 15:39:44 +00:00
|
|
|
|
2020-02-24 18:13:11 +00:00
|
|
|
//if cooldown isn't created, will return that it is over
|
2020-02-15 15:39:44 +00:00
|
|
|
|
2020-02-24 18:13:11 +00:00
|
|
|
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())};
|
2020-02-15 15:39:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-02-16 15:13:31 +00:00
|
|
|
public void change(Event e, Object[] changer, ChangeMode mode) {
|
2020-02-15 15:39:44 +00:00
|
|
|
switch (mode) {
|
|
|
|
case SET:
|
2020-02-16 15:13:31 +00:00
|
|
|
Skooldown.cooldowns.put(name.getSingle(e), System.currentTimeMillis() + ((Timespan) changer[0]).getMilliSeconds());
|
2020-02-15 15:39:44 +00:00
|
|
|
break;
|
|
|
|
case REMOVE_ALL:
|
|
|
|
case DELETE:
|
|
|
|
case RESET:
|
2020-02-24 18:13:11 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2020-02-15 15:39:44 +00:00
|
|
|
break;
|
|
|
|
case ADD:
|
|
|
|
Long cooldown = Skooldown.cooldowns.get(name.getSingle(e));
|
|
|
|
if (cooldown == null) {
|
2020-02-16 15:13:31 +00:00
|
|
|
Skooldown.cooldowns.put(name.getSingle(e), System.currentTimeMillis() + ((Timespan) changer[0]).getMilliSeconds());
|
2020-02-15 15:39:44 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-02-16 15:13:31 +00:00
|
|
|
Skooldown.cooldowns.put(name.getSingle(e), cooldown + ((Timespan) changer[0]).getMilliSeconds());
|
2020-02-15 15:39:44 +00:00
|
|
|
break;
|
|
|
|
case REMOVE:
|
|
|
|
cooldown = Skooldown.cooldowns.get(name.getSingle(e));
|
|
|
|
//if removing from non-existent cooldown, do nothing
|
|
|
|
if (cooldown == null) return;
|
|
|
|
|
|
|
|
//remove cooldown from hashMap if it would expire with new value
|
|
|
|
|
2020-02-16 15:13:31 +00:00
|
|
|
if ((cooldown - ((Timespan) changer[0]).getMilliSeconds()) < System.currentTimeMillis()) {
|
2020-02-15 15:39:44 +00:00
|
|
|
Skooldown.cooldowns.remove(name.getSingle(e));
|
|
|
|
} else {
|
2020-02-16 15:13:31 +00:00
|
|
|
Skooldown.cooldowns.put(name.getSingle(e), cooldown - ((Timespan) changer[0]).getMilliSeconds());
|
2020-02-15 15:39:44 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new IllegalStateException("Unexpected value: " + mode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Class<?>[] acceptChange(final Changer.ChangeMode mode) { return CollectionUtils.array(Timespan.class); }
|
|
|
|
}
|
|
|
|
|