|
| 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 | +} |
0 commit comments