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

🚀 Item cooldown expression + condition #4198

Merged
merged 31 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
f9bde06
Item cooldown expression + condition
AyhamAl-Ali Jul 17, 2021
bb27df5
Changes and Improvements
AyhamAl-Ali Jul 18, 2021
52d70f2
Merge branch 'master' into feature/item-cooldown
AyhamAl-Ali Oct 30, 2021
0804701
Merge branch 'master' into feature/item-cooldown
AyhamAl-Ali Jan 17, 2022
840e256
Update src/main/java/ch/njol/skript/conditions/CondHasItemCooldown.java
AyhamAl-Ali Jul 15, 2022
09c19ad
Update src/main/java/ch/njol/skript/expressions/ExprItemCooldown.java
AyhamAl-Ali Jul 15, 2022
597fbba
Update src/main/java/ch/njol/skript/expressions/ExprItemCooldown.java
AyhamAl-Ali Jul 15, 2022
589dfe5
Improvements & Fix committed changes
AyhamAl-Ali Jul 15, 2022
70a287c
Improvements
AyhamAl-Ali Jul 15, 2022
8c8677a
Update src/main/java/ch/njol/skript/expressions/ExprItemCooldown.java
AyhamAl-Ali Jul 15, 2022
168cb67
Update src/main/java/ch/njol/skript/conditions/CondHasItemCooldown.java
AyhamAl-Ali Jul 15, 2022
5fb0234
Update src/main/java/ch/njol/skript/expressions/ExprItemCooldown.java
AyhamAl-Ali Jul 17, 2022
7b0d6a6
Syntax Improvements & Overall Improvements
AyhamAl-Ali Jul 17, 2022
0b99e12
Remove unnecessary warning suppressions
AyhamAl-Ali Jul 17, 2022
a445cc7
Merge branch 'master' into feature/item-cooldown
AyhamAl-Ali Jul 21, 2022
57b4403
Code Improvements
AyhamAl-Ali Aug 26, 2022
a38169d
Update src/main/java/ch/njol/skript/expressions/ExprItemCooldown.java
AyhamAl-Ali Jan 2, 2023
687a0a4
Improvements and exception fixes
AyhamAl-Ali Jan 2, 2023
7b90f2e
Merge branch 'master' into feature/item-cooldown
AyhamAl-Ali Jan 2, 2023
4ba9647
Update src/main/java/ch/njol/skript/expressions/ExprItemCooldown.java
AyhamAl-Ali Jan 2, 2023
3f8a90f
Update src/main/java/ch/njol/skript/expressions/ExprItemCooldown.java
AyhamAl-Ali Jan 2, 2023
e6bcb87
Merge branch 'master' into feature/item-cooldown
AyhamAl-Ali Feb 13, 2023
84ed9e0
Description Improvements
AyhamAl-Ali Feb 13, 2023
9fc88bd
Apply suggestions from code review
AyhamAl-Ali Feb 18, 2023
fb9a0bc
Requested Changes
AyhamAl-Ali Apr 8, 2023
015e460
Merge branch 'master' into feature/item-cooldown
AyhamAl-Ali Apr 10, 2023
d7f9013
Merge branch 'master' into feature/item-cooldown
AyhamAl-Ali Apr 16, 2023
309af39
Merge branch 'master' into feature/item-cooldown
TheLimeGlass Jun 6, 2023
0c1ed05
Apply suggestions from code review
AyhamAl-Ali Jun 6, 2023
3a0df80
Merge branch 'master' into feature/item-cooldown
TheLimeGlass Jul 31, 2023
5603e64
Update src/main/java/ch/njol/skript/conditions/CondHasItemCooldown.java
TheLimeGlass Jul 31, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions src/main/java/ch/njol/skript/conditions/CondHasItemCooldown.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* 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.conditions;

import ch.njol.skript.Skript;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.conditions.base.PropertyCondition;
import ch.njol.skript.conditions.base.PropertyCondition.PropertyType;
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.lang.Condition;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;
import org.bukkit.entity.HumanEntity;
import org.bukkit.event.Event;

@Name("Has Item Cooldown")
@Description("Check whether a cooldown is active on the specified material for a specific player.")
@Examples({"if player has item cooldown on player's tool:",
"\tsend \"You can't use this item right now. Wait %item cooldown of player's tool for player%\""})
@Since("INSERT VERSION")
public class CondHasItemCooldown extends Condition {

static {
Skript.registerCondition(CondHasItemCooldown.class,
"%players% (has|have) [([an] item|a)] cooldown (on|for) %itemtypes%",
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
"%players% (doesn't|does not|do not|don't) have [([an] item|a)] cooldown (on|for) %itemtypes%");
}

private Expression<HumanEntity> players;
private Expression<ItemType> itemtypes;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
players = (Expression<HumanEntity>) exprs[0];
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
itemtypes = (Expression<ItemType>) exprs[1];
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
setNegated(matchedPattern == 1);
return true;
}

@Override
public boolean check(Event e) {
return players.check(e, (p) ->
itemtypes.check(e, (it) ->
p.hasCooldown(it.getMaterial())
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
)
);
}

@Override
public String toString(Event e, boolean debug) {
return PropertyCondition.toString(this, PropertyType.HAVE, e, debug, players, "item cooldown on " + itemtypes.toString(e, debug));
}

}
150 changes: 150 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprItemCooldown.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/**
* 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.aliases.ItemType;
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.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
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 org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

@Name("Item Cooldown")
@Description("Set the cooldown of a specific material for a certain amount of ticks. Setting this to <code>0 tick</code> will remove the cooldown.")
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
@Examples({
"on right click using stick:" +
"\tset item cooldown of player's tool for player to 1 minute",
"\tset item cooldown of stone and grass for all players to 20 seconds",
"\treset item cooldown of cobblestone and dirt for all players"
})
@Since("INSERT VERSION")
public class ExprItemCooldown extends SimpleExpression<Timespan> {

static {
Skript.registerExpression(ExprItemCooldown.class, Timespan.class, ExpressionType.COMBINED,
"[the] [item] cooldown of %itemtypes% for %players%",
"%players%'[s] [item] cooldown for %itemtypes%");
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
}

private Expression<Player> players;
private Expression<ItemType> itemtypes;

@Override
@SuppressWarnings({"null", "unchecked"})
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
players = (Expression<Player>) exprs[1];
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
itemtypes = (Expression<ItemType>) exprs[0];
return true;
}

@Override
@SuppressWarnings("null")
protected Timespan[] get(Event e) {
Player[] players = this.players.getArray(e);
ItemType[] itemtypes = this.itemtypes.getArray(e);
if (players.length == 0 || itemtypes.length == 0)
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
return new Timespan[0];
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved

Timespan[] timespan = new Timespan[players.length * itemtypes.length];

int i = 0;
for (Player p : players) {
for (ItemType it : itemtypes) {
timespan[i] = Timespan.fromTicks_i(p.getCooldown(it.getMaterial()));
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
i++;
}
}
return timespan;
}

@Override
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
switch (mode) {
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
case SET:
case RESET:
case REMOVE:
case DELETE:
case ADD:
return CollectionUtils.array(Timespan.class);
default:
return null;
}
}

@Override
public void change(Event e, Object[] delta, ChangeMode mode) {
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
if (mode != ChangeMode.RESET && mode != ChangeMode.DELETE && (delta == null || !(delta[0] instanceof Timespan)))
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
return;

int timespan = delta != null ? (int) ((Timespan) delta[0]).getTicks_i() : 0; // 0 for DELETE/RESET
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
Player[] players = this.players.getArray(e);
ItemType[] itemtypes = this.itemtypes.getArray(e);

if (players.length == 0 || itemtypes.length == 0)
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
return;

for (Player p : players) {
for (ItemType it : itemtypes) {
Material mat = it.getMaterial();
switch (mode) {
case RESET:
case DELETE:
case SET:
p.setCooldown(mat, timespan);
break;
case REMOVE:
p.setCooldown(mat, Math.max(p.getCooldown(mat) - timespan, 0));
break;
case ADD:
p.setCooldown(mat, p.getCooldown(mat) + timespan);
break;
}
}
}
}

@Override
public boolean isSingle() {
return players.isSingle() && itemtypes.isSingle();
}

@Override
public Class<? extends Timespan> getReturnType() {
return Timespan.class;
}

@Override
public String toString(@Nullable Event e, boolean debug) {
return "the cooldown of " + itemtypes.toString(e, debug) + " for " + players.toString(e, debug);
}

}