Skip to content

Commit 95791c1

Browse files
committed
Add first empty slot expression
1 parent 77173b2 commit 95791c1

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package ch.njol.skript.expressions;
2+
3+
import ch.njol.skript.Skript;
4+
import ch.njol.skript.expressions.base.SimplePropertyExpression;
5+
import ch.njol.skript.lang.ExpressionType;
6+
import ch.njol.skript.util.slot.InventorySlot;
7+
import ch.njol.skript.util.slot.Slot;
8+
import org.bukkit.inventory.Inventory;
9+
import org.jetbrains.annotations.Nullable;
10+
11+
import java.util.ArrayList;
12+
import java.util.Arrays;
13+
import java.util.List;
14+
15+
public class ExprFirstEmptySlot extends SimplePropertyExpression<Inventory, Slot> {
16+
17+
static {
18+
// support `first empty slot in inventory` as well as typical property syntax
19+
List<String> patterns = new ArrayList<>(Arrays.asList(getPatterns("first empty slot", "inventory")));
20+
patterns.add("[the] first empty slot in %inventory%");
21+
Skript.registerExpression(ExprFirstEmptySlot.class, Slot.class, ExpressionType.PROPERTY, patterns.toArray(new String[0]));
22+
}
23+
24+
@Override
25+
public @Nullable Slot convert(Inventory from) {
26+
int slotIndex = from.firstEmpty();
27+
if (slotIndex == -1)
28+
return null; // No empty slot found
29+
return new InventorySlot(from, slotIndex);
30+
}
31+
32+
@Override
33+
public Class<? extends Slot> getReturnType() {
34+
return Slot.class;
35+
}
36+
37+
@Override
38+
protected String getPropertyName() {
39+
return "first empty slot";
40+
}
41+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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"

0 commit comments

Comments
 (0)