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 23 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
7 changes: 7 additions & 0 deletions src/main/java/ch/njol/skript/aliases/ItemType.java
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,13 @@ public boolean hasBlock() {
}
return false;
}

/**
* @return Whether this ItemType has at least one ItemData that represents it whether it's a block or an item
*/
public boolean hasType() {
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
return types.get(0) != null;
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Sets the given block to this ItemType
Expand Down
81 changes: 81 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,81 @@
/**
* 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.Player;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

@Name("Has Item Cooldown")
@Description("Check whether a cooldown is active on the specified material for a specific player.")
@Examples({
"if player has player's tool on cooldown:",
"\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% (has|have) %itemtypes% on cooldown",
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%",
"%players% (doesn't|does not|do not|don't) have %itemtypes% on cooldown");
}

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

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
players = (Expression<Player>) exprs[0];
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 event) {
return players.check(event, (player) ->
itemtypes.check(event, (itemType) ->
itemType.hasType() && player.hasCooldown(itemType.getMaterial())
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
)
);
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return PropertyCondition.toString(this, PropertyType.HAVE, event, debug, players,
itemtypes.toString(event, debug) + " on cooldown");
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
}

}
142 changes: 142 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,142 @@
/**
* 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;

import java.util.List;
import java.util.stream.Collectors;

@Name("Item Cooldown")
@Description("Change the cooldown of a specific material to a certain amount of <a href='./classes.html#timespan'>Timespan</a>.")
@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
}

@SuppressWarnings("NotNullFieldNotInitialized")
private Expression<Player> players;
@SuppressWarnings("NotNullFieldNotInitialized")
private Expression<ItemType> itemtypes;

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
players = (Expression<Player>) exprs[matchedPattern ^ 1];
itemtypes = (Expression<ItemType>) exprs[matchedPattern];
return true;
}

@Override
protected Timespan[] get(Event event) {
Player[] players = this.players.getArray(event);

List<ItemType> itemTypes = this.itemtypes.stream(event)
.filter(ItemType::hasType).collect(Collectors.toList());
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved

Timespan[] timespan = new Timespan[players.length * itemTypes.size()];

int i = 0;
for (Player player : players) {
for (ItemType itemType : itemTypes) {
timespan[i++] = Timespan.fromTicks_i(player.getCooldown(itemType.getMaterial()));
}
}
return timespan;
}

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

@Override
public void change(Event event, Object @Nullable [] delta, ChangeMode mode) {
if (mode != ChangeMode.RESET && mode != ChangeMode.DELETE && delta == null)
return;

int ticks = delta != null ? (int) ((Timespan) delta[0]).getTicks_i() : 0; // 0 for DELETE/RESET
Player[] players = this.players.getArray(event);
List<ItemType> itemTypes = this.itemtypes.stream(event)
.filter(ItemType::hasType).collect(Collectors.toList());
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved

for (Player player : players) {
for (ItemType itemtype : itemTypes) {
Material material = itemtype.getMaterial();
switch (mode) {
case RESET:
case DELETE:
case SET:
player.setCooldown(material, ticks);
break;
case REMOVE:
player.setCooldown(material, Math.max(player.getCooldown(material) - ticks, 0));
break;
case ADD:
player.setCooldown(material, player.getCooldown(material) + ticks);
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 event, boolean debug) {
return "the cooldown of " + itemtypes.toString(event, debug) + " for " + players.toString(event, debug);
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
}

}