From ecef1cb5f97f6aaead510c913b4bb003f028a478 Mon Sep 17 00:00:00 2001 From: Govindass Date: Sat, 15 Feb 2020 17:39:44 +0200 Subject: [PATCH] Initial commit --- .gitignore | 3 + src/lt/govindas/skooldown/Skooldown.java | 30 ++++++ .../conditions/CondCooldownOver.java | 46 +++++++++ .../skooldown/effects/EffStartCooldown.java | 36 +++++++ .../expressions/ExprAllCooldowns.java | 4 + .../skooldown/expressions/ExprCooldown.java | 98 +++++++++++++++++++ src/plugin.yml | 4 + 7 files changed, 221 insertions(+) create mode 100644 .gitignore create mode 100644 src/lt/govindas/skooldown/Skooldown.java create mode 100644 src/lt/govindas/skooldown/conditions/CondCooldownOver.java create mode 100644 src/lt/govindas/skooldown/effects/EffStartCooldown.java create mode 100644 src/lt/govindas/skooldown/expressions/ExprAllCooldowns.java create mode 100644 src/lt/govindas/skooldown/expressions/ExprCooldown.java create mode 100644 src/plugin.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..efe7734 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +out +Skooldown.iml \ No newline at end of file diff --git a/src/lt/govindas/skooldown/Skooldown.java b/src/lt/govindas/skooldown/Skooldown.java new file mode 100644 index 0000000..0a5a410 --- /dev/null +++ b/src/lt/govindas/skooldown/Skooldown.java @@ -0,0 +1,30 @@ +package lt.govindas.skooldown; + +import ch.njol.skript.Skript; +import ch.njol.skript.lang.ExpressionType; +import ch.njol.skript.util.Timespan; +import lt.govindas.skooldown.conditions.CondCooldownOver; +import lt.govindas.skooldown.effects.EffStartCooldown; +import lt.govindas.skooldown.expressions.ExprCooldown; +import org.bukkit.plugin.java.JavaPlugin; + +import java.util.HashMap; + +public final class Skooldown extends JavaPlugin { + public static HashMap cooldowns = new HashMap(); + + + + @Override + public void onEnable() { + Skript.registerEffect(EffStartCooldown.class, "(create|start) [a] cooldown %string% for %timespan%"); + Skript.registerCondition(CondCooldownOver.class, "[the] cooldown %string% (is|has) (finished|over|done)", "[the] cooldown %string% is(n't| not) unfinished)", "[the] cooldown %string% is(n't| not) (finished|over|done)", "[the] cooldown %string% is unfinished"); + Skript.registerExpression(ExprCooldown.class, Timespan.class, ExpressionType.PROPERTY, "cooldown %string%"); + getLogger().info("[Skooldown] Plugin enabled!"); + } + + @Override + public void onDisable() { + getLogger().info("[Skooldown] Plugin disabled!"); + } +} diff --git a/src/lt/govindas/skooldown/conditions/CondCooldownOver.java b/src/lt/govindas/skooldown/conditions/CondCooldownOver.java new file mode 100644 index 0000000..3090bda --- /dev/null +++ b/src/lt/govindas/skooldown/conditions/CondCooldownOver.java @@ -0,0 +1,46 @@ +package lt.govindas.skooldown.conditions; + +import ch.njol.skript.lang.Condition; +import ch.njol.skript.lang.Expression; +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 CondCooldownOver extends Condition { + + private Expression name; + + @SuppressWarnings("unchecked") + @Override + + public boolean init(Expression[] expr, int matchedPattern, Kleenean paramKleenean, ParseResult paramParseResult) { + name = (Expression) expr[0]; + System.out.println(matchedPattern); + setNegated(matchedPattern == 2 || matchedPattern == 3); + return true; + } + + @Override + public String toString(@Nullable Event e, boolean debug) { + return "is cooldown over" + name; + } + + @Override + public boolean check(Event 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 < 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/EffStartCooldown.java b/src/lt/govindas/skooldown/effects/EffStartCooldown.java new file mode 100644 index 0000000..0169fab --- /dev/null +++ b/src/lt/govindas/skooldown/effects/EffStartCooldown.java @@ -0,0 +1,36 @@ +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.skript.util.Timespan; +import ch.njol.util.Kleenean; +import lt.govindas.skooldown.Skooldown; +import org.bukkit.event.Event; + +import org.jetbrains.annotations.Nullable; + +public class EffStartCooldown extends Effect { + + private Expression name; + private Expression time; + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int matchedPattern, Kleenean paramKleenean, ParseResult paramParseResult) { + name = (Expression) expr[0]; + time = (Expression) expr[1]; + return true; + } + + @Override + public String toString(@Nullable Event e, boolean debug) { + return "start cooldown " + name.getSingle(e); + } + + @Override + protected void execute(Event e) { + Skooldown.cooldowns.put(name.getSingle(e), System.currentTimeMillis() + time.getSingle(e).getMilliSeconds()); + + } +} diff --git a/src/lt/govindas/skooldown/expressions/ExprAllCooldowns.java b/src/lt/govindas/skooldown/expressions/ExprAllCooldowns.java new file mode 100644 index 0000000..10b0763 --- /dev/null +++ b/src/lt/govindas/skooldown/expressions/ExprAllCooldowns.java @@ -0,0 +1,4 @@ +package lt.govindas.skooldown.expressions; + +public class ExprAllCooldowns { +} diff --git a/src/lt/govindas/skooldown/expressions/ExprCooldown.java b/src/lt/govindas/skooldown/expressions/ExprCooldown.java new file mode 100644 index 0000000..612a0f6 --- /dev/null +++ b/src/lt/govindas/skooldown/expressions/ExprCooldown.java @@ -0,0 +1,98 @@ +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; + +//TODO rename "delta" into something else, since I don't know what delta is +public class ExprCooldown extends SimpleExpression { + + private Expression name; + + @Override + public boolean isSingle() { + return true; + } + + @Override + public Class getReturnType() { + return Timespan.class; + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int matchedPattern, Kleenean paramKleenean, ParseResult paramParseResult) { + name = (Expression) expr[0]; + return true; + } + + @Override + public String toString(@Nullable Event e, boolean debug) { + return "get cooldown " + name.getSingle(e); + } + + @Override + protected Timespan[] get(Event e) { + Long cooldown = Skooldown.cooldowns.get(name.getSingle(e)); + + //if cooldown isn't created, will return that it is over + if (cooldown == null) return new Timespan[]{new Timespan(0)}; + + if (cooldown < System.currentTimeMillis()) { + Skooldown.cooldowns.remove(name.getSingle(e)); + return new Timespan[]{new Timespan(0)}; + } + return new Timespan[]{new Timespan(Skooldown.cooldowns.get(name.getSingle(e)) - System.currentTimeMillis())}; + } + + @Override + public void change(Event e, Object[] delta, ChangeMode mode) { + switch (mode) { + case SET: + Skooldown.cooldowns.put(name.getSingle(e), System.currentTimeMillis() + ((Timespan) delta[0]).getMilliSeconds()); + break; + case REMOVE_ALL: + case DELETE: + case RESET: + Skooldown.cooldowns.remove(name.getSingle(e)); + break; + case ADD: + Long cooldown = Skooldown.cooldowns.get(name.getSingle(e)); + if (cooldown == null) { + Skooldown.cooldowns.put(name.getSingle(e), System.currentTimeMillis() + ((Timespan) delta[0]).getMilliSeconds()); + break; + } + Skooldown.cooldowns.put(name.getSingle(e), cooldown + ((Timespan) delta[0]).getMilliSeconds()); + 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 + + if ((cooldown - ((Timespan) delta[0]).getMilliSeconds()) < System.currentTimeMillis()) { + Skooldown.cooldowns.remove(name.getSingle(e)); + } else { + Skooldown.cooldowns.put(name.getSingle(e), cooldown - ((Timespan) delta[0]).getMilliSeconds()); + } + break; + default: + throw new IllegalStateException("Unexpected value: " + mode); + } + } + + + @Override + public Class[] acceptChange(final Changer.ChangeMode mode) { return CollectionUtils.array(Timespan.class); } +} + diff --git a/src/plugin.yml b/src/plugin.yml new file mode 100644 index 0000000..b498966 --- /dev/null +++ b/src/plugin.yml @@ -0,0 +1,4 @@ +name: Skooldown +main: lt.govindas.skooldown.Skooldown +version: 0.0.1 +authors: [Govindas, Nashoba24] \ No newline at end of file