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 7 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
27 changes: 16 additions & 11 deletions src/main/java/ch/njol/skript/conditions/CondHasItemCooldown.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,19 @@

@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%\""})
@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%|%itemtypes% on cooldown)",
"%players% (doesn't|does not|do not|don't) have ([([an] item|a)] cooldown (on|for) %itemtypes%|%itemtypes% on cooldown)");
"%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;
Expand All @@ -55,22 +59,23 @@ public class CondHasItemCooldown extends Condition {
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);
setNegated(matchedPattern > 1);
return true;
}

@Override
public boolean check(Event e) {
return players.check(e, (p) ->
itemtypes.check(e, (it) ->
p.hasCooldown(it.getMaterial())
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 e, boolean debug) {
return PropertyCondition.toString(this, PropertyType.HAVE, e, debug, players, itemtypes.toString(e, debug) + " on cooldown");
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
}

}
56 changes: 16 additions & 40 deletions src/main/java/ch/njol/skript/expressions/ExprItemCooldown.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

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

@Name("Item Cooldown")
@Description("Set the cooldown of a specific material for a certain amount of ticks. Setting this to <code>0 ticks</code> will remove the 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"
"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> {
Expand All @@ -73,27 +73,16 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
@Override
protected Timespan[] get(Event event) {
Player[] players = this.players.getArray(event);
ItemType[] itemtypesArray = this.itemtypes.getArray(event);
if (players == null || itemtypesArray == null)
return null;

List<ItemType> itemtypes = Arrays.stream(itemtypesArray)
.filter(it -> {
try {
it.getMaterial();
return true;
} catch (IllegalArgumentException ex) {
return false;
}
}).toList();
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()];
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()));
i++;
for (ItemType itemType : itemTypes) {
timespan[i++] = Timespan.fromTicks_i(player.getCooldown(itemType.getMaterial()));
}
}
return timespan;
Expand All @@ -102,34 +91,21 @@ protected Timespan[] get(Event event) {
@Override
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
if (mode == ChangeMode.REMOVE_ALL)
return null;
return CollectionUtils.array(Timespan.class);
return mode == ChangeMode.REMOVE_ALL ? null : CollectionUtils.array(Timespan.class);
}

@Override
public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
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);
ItemType[] itemtypesArray = this.itemtypes.getArray(event);
if (players == null || itemtypesArray == null)
return;

List<ItemType> itemtypes = Arrays.stream(itemtypesArray)
.filter(it -> {
try {
it.getMaterial();
return true;
} catch (IllegalArgumentException ex) {
return false;
}
}).toList();
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) {
for (ItemType itemtype : itemTypes) {
Material material = itemtype.getMaterial();
switch (mode) {
case RESET:
Expand Down