Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add playerexpcooldownchange event #5822

Closed
wants to merge 12 commits into from
Closed
9 changes: 9 additions & 0 deletions src/main/java/ch/njol/skript/events/SimpleEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import org.bukkit.event.player.PlayerBucketFillEvent;
import org.bukkit.event.player.PlayerChangedWorldEvent;
import org.bukkit.event.player.PlayerEggThrowEvent;
import org.bukkit.event.player.PlayerExpCooldownChangeEvent;
import org.bukkit.event.player.PlayerFishEvent;
import org.bukkit.event.player.PlayerItemBreakEvent;
import org.bukkit.event.player.PlayerItemDamageEvent;
Expand Down Expand Up @@ -731,6 +732,14 @@ public class SimpleEvents {
)
.since("INSERT VERSION");

Skript.registerEvent("Player Exp Cooldown Change", SimpleEvent.class, PlayerExpCooldownChangeEvent.class, "[player] (exp cooldown change)")
TheLimeGlass marked this conversation as resolved.
Show resolved Hide resolved
.description("Called when a player's exp cooldown changes")
.examples(
"On player exp cooldown change:",
"\tsend \"Your exp cooldown has changed!\" to player"
)
.since("INSERT VERSION");

}

}
105 changes: 105 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprExpCooldown.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.expressions;


import ch.njol.skript.Skript;
import ch.njol.skript.classes.Changer.ChangeMode;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.expressions.base.SimplePropertyExpression;
import ch.njol.skript.util.Timespan;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;

import javax.annotation.Nullable;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Jetbrains or Eclipse Nullable, we don't typically use javax


@Name("Exp Cooldown")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be better to not use the abbreviated name here.

@Description("The exp cooldown of a player")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate more? What's an exp cooldown? Is it a cooldown for gaining experience, is it a cooldown for picking up experience, for dropping experience, etc.

@Examples({
"player's exp cooldown is less than 3 ticks:",
"\tsend \"increasing your exp cooldown!\" to player",
"\t\tset player's exp cooldown to 40 ticks"
nylhus marked this conversation as resolved.
Show resolved Hide resolved
})
@Since("2.7")
public class ExprExpCooldown extends SimplePropertyExpression<Player, Timespan> {

static {
if (Skript.methodExists(Player.class, "getNewCooldown"))
nylhus marked this conversation as resolved.
Show resolved Hide resolved
register(ExprExpCooldown.class, Timespan.class, "cooldown change", "players");
nylhus marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
@Nullable
public Timespan convert(Player player) { return Timespan.fromTicks_i(player.getExpCooldown()); }
nylhus marked this conversation as resolved.
Show resolved Hide resolved

@Override
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
return (mode != ChangeMode.REMOVE_ALL) ? CollectionUtils.array(Timespan.class) : null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May want to default to null if given an unexpected new ChangeMode, rather than accepting it. A switch is often recommended, I believe.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Skript doesn't do one liners, changes need similar to the convert

}

@Override
public void change(Event e, @Nullable Object[] delta, ChangeMode mode) {
int time = delta == null ? 0 : (int) ((Timespan) delta[0]).getTicks_i();
int newTime;
switch (mode) {
case ADD:
for (Player player : getExpr().getArray(e)) {
newTime = player.getExpCooldown() + time;
setExpCooldown(player, newTime);
}
break;
case REMOVE:
for (Player player : getExpr().getArray(e)) {
newTime = player.getExpCooldown() - time;
setExpCooldown(player, newTime);
}
break;
case SET:
for (Player player : getExpr().getArray(e)) {
setExpCooldown(player, time);
}
break;
case DELETE:
case RESET:
for (Player player : getExpr().getArray(e)) {
setExpCooldown(player, 0);
}
break;
default:
assert false;
}
}

@Override
public Class<? extends Timespan> getReturnType() { return Timespan.class; }
nylhus marked this conversation as resolved.
Show resolved Hide resolved

@Override
protected String getPropertyName() { return "exp cooldown"; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The property should probably be experience cooldown.

nylhus marked this conversation as resolved.
Show resolved Hide resolved

private void setExpCooldown(Player player, int ticks) {
if (ticks < 0)
ticks = 0;
player.setExpCooldown(ticks);
}
nylhus marked this conversation as resolved.
Show resolved Hide resolved
}