Skip to content

Commit 133952d

Browse files
authored
Add first empty slot in inventory expression (#7914)
* Add first empty slot expression * plural support * can't forget docs * Update src/main/java/ch/njol/skript/expressions/ExprFirstEmptySlot.java
1 parent 748ce2e commit 133952d

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package ch.njol.skript.expressions;
2+
3+
import ch.njol.skript.Skript;
4+
import ch.njol.skript.doc.*;
5+
import ch.njol.skript.expressions.base.SimplePropertyExpression;
6+
import ch.njol.skript.lang.ExpressionType;
7+
import ch.njol.skript.util.slot.InventorySlot;
8+
import ch.njol.skript.util.slot.Slot;
9+
import org.bukkit.inventory.Inventory;
10+
import org.jetbrains.annotations.Nullable;
11+
12+
import java.util.ArrayList;
13+
import java.util.Arrays;
14+
import java.util.List;
15+
16+
@Name("First Empty Slot in Inventory")
17+
@Description("Returns the first empty slot in an inventory. If no empty slot is found, it returns nothing.")
18+
@Example("set the first empty slot in player's inventory to 5 diamonds")
19+
@Example("""
20+
if the first empty slot in player's inventory is not set:
21+
message "No empty slot available in your inventory!" to player
22+
""")
23+
@Since("INSERT VERSION")
24+
@Keywords({"full", "inventory", "empty", "air", "slot"})
25+
public class ExprFirstEmptySlot extends SimplePropertyExpression<Inventory, Slot> {
26+
27+
static {
28+
// support `first empty slot in inventory` as well as typical property syntax
29+
List<String> patterns = new ArrayList<>(Arrays.asList(getPatterns("first empty slot[s]", "inventories")));
30+
patterns.add("[the] first empty slot[s] in %inventories%");
31+
Skript.registerExpression(ExprFirstEmptySlot.class, Slot.class, ExpressionType.PROPERTY, patterns.toArray(new String[0]));
32+
}
33+
34+
@Override
35+
public @Nullable Slot convert(Inventory from) {
36+
int slotIndex = from.firstEmpty();
37+
if (slotIndex == -1)
38+
return null; // No empty slot found
39+
return new InventorySlot(from, slotIndex);
40+
}
41+
42+
@Override
43+
public Class<? extends Slot> getReturnType() {
44+
return Slot.class;
45+
}
46+
47+
@Override
48+
protected String getPropertyName() {
49+
return "first empty slot";
50+
}
51+
52+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
test "first empty slot":
2+
set {_inv} to a chest inventory with 3 rows
3+
assert the index of the first empty slot in {_inv} is 0 with "First empty slot in empty inventory should be 0"
4+
set slot 0 of {_inv} to a stone
5+
assert the index of the first empty slot in {_inv} is 1 with "First empty slot in inventory should be 1"
6+
set slot 3 of {_inv} to a stone
7+
assert the index of the first empty slot in {_inv} is 1 with "First empty slot in inventory should be 1"
8+
set slot 1 of {_inv} to a stone
9+
assert the index of the first empty slot in {_inv} is 2 with "First empty slot in inventory should be 2"
10+
11+
set first empty slot of {_inv} to diamond sword
12+
assert slot 2 of {_inv} is a diamond sword with "First empty slot should be set to diamond sword"
13+
assert the index of the first empty slot in {_inv} is 4 with "First empty slot in inventory should be 4"
14+
15+
set {_inv2} to a hopper inventory named "Test Hopper"
16+
set the first empty slots in {_inv2} and {_inv} to diamond sword
17+
assert slot 0 of {_inv2} is a diamond sword with "First empty slot in hopper inventory should be set to diamond sword"
18+
assert slot 4 of {_inv} is a diamond sword with "First empty slot in chest inventory should be set to diamond sword"

0 commit comments

Comments
 (0)