From 4e921aee94c2bf4c8b78c77badb3b55ffd8611da Mon Sep 17 00:00:00 2001 From: Fusezion Date: Sun, 16 Mar 2025 05:41:26 -0400 Subject: [PATCH 01/12] ExprTool.java - add mainhand and offhand --- .../ch/njol/skript/expressions/ExprTool.java | 58 ++++++++++--------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprTool.java b/src/main/java/ch/njol/skript/expressions/ExprTool.java index 39b1eecee24..6b27596f3c6 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprTool.java +++ b/src/main/java/ch/njol/skript/expressions/ExprTool.java @@ -1,10 +1,7 @@ package ch.njol.skript.expressions; import ch.njol.skript.Skript; -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.doc.*; import ch.njol.skript.effects.Delay; import ch.njol.skript.expressions.base.PropertyExpression; import ch.njol.skript.lang.Expression; @@ -17,30 +14,31 @@ import ch.njol.util.Kleenean; import org.bukkit.entity.LivingEntity; import org.bukkit.event.Event; -import org.bukkit.event.player.PlayerBucketEmptyEvent; import org.bukkit.event.player.PlayerBucketEvent; -import org.bukkit.event.player.PlayerBucketFillEvent; import org.bukkit.event.player.PlayerItemHeldEvent; import org.bukkit.inventory.EntityEquipment; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; import org.jetbrains.annotations.Nullable; -/** - * @author Peter Güttinger - */ @Name("Tool") @Description("The item an entity is holding in their main or off hand.") -@Examples({"player's tool is a pickaxe", - "player's off hand tool is a shield", - "set tool of all players to a diamond sword", - "set offhand tool of target entity to a bow"}) +@Example(""" + player's tool is a pickaxe + player's off hand tool is a shield + set tool of all players to a diamond sword + set offhand tool of target entity to a bow +""") @Since("1.0") public class ExprTool extends PropertyExpression { static { - Skript.registerExpression(ExprTool.class, Slot.class, ExpressionType.PROPERTY, - "[the] ((tool|held item|weapon)|1¦(off[ ]hand (tool|item))) [of %livingentities%]", - "%livingentities%'[s] ((tool|held item|weapon)|1¦(off[ ]hand (tool|item)))"); + String[] patterns = new String[]{ + "[the] (tool|held item|weapon|main[ ]hand) [of %livingentities%]", + "%livingentities%'[s] (tool|held item|weapon|main[ ]hand)", + "[the] off[ ]hand [tool|item] [of %livingentities%]", + "%livingentities%'[s] off[ ]hand [tool|item]" + }; + Skript.registerExpression(ExprTool.class, Slot.class, ExpressionType.PROPERTY, patterns);; } private boolean offHand; @@ -49,25 +47,29 @@ public class ExprTool extends PropertyExpression { @Override public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parser) { setExpr((Expression) exprs[0]); - offHand = parser.mark == 1; + offHand = matchedPattern >= 2; return true; } @Override protected Slot[] get(Event event, LivingEntity[] source) { - final boolean delayed = Delay.isDelayed(event); + boolean delayed = Delay.isDelayed(event); return get(source, entity -> { if (!delayed) { if (!offHand && event instanceof PlayerItemHeldEvent playerItemHeldEvent && playerItemHeldEvent.getPlayer() == entity) { - final PlayerInventory i = playerItemHeldEvent.getPlayer().getInventory(); - return new InventorySlot(i, getTime() >= 0 ? playerItemHeldEvent.getNewSlot() : playerItemHeldEvent.getPreviousSlot()); + + PlayerInventory inventory = playerItemHeldEvent.getPlayer().getInventory(); + return new InventorySlot(inventory, getTime() >= 0 ? playerItemHeldEvent.getNewSlot() : playerItemHeldEvent.getPreviousSlot()); + } else if (event instanceof PlayerBucketEvent playerBucketEvent && playerBucketEvent.getPlayer() == entity) { - final PlayerInventory i = playerBucketEvent.getPlayer().getInventory(); - boolean isOffHand = playerBucketEvent.getHand() == org.bukkit.inventory.EquipmentSlot.OFF_HAND || offHand; - return new InventorySlot(i, isOffHand ? EquipmentSlot.EquipSlot.OFF_HAND.slotNumber - : playerBucketEvent.getPlayer().getInventory().getHeldItemSlot()) { + + PlayerInventory inventory = playerBucketEvent.getPlayer().getInventory(); + boolean isOffHand = offHand || playerBucketEvent.getHand() == org.bukkit.inventory.EquipmentSlot.OFF_HAND; + int inventorySlot = isOffHand ? EquipmentSlot.EquipSlot.OFF_HAND.slotNumber : inventory.getHeldItemSlot(); + + return new InventorySlot(inventory, inventorySlot) { @Override public ItemStack getItem() { return getTime() <= 0 ? super.getItem() : playerBucketEvent.getItemStack(); @@ -82,6 +84,7 @@ public void setItem(final @Nullable ItemStack item) { } } }; + } } @@ -106,15 +109,14 @@ public Class getReturnType() { } @Override - public String toString(final @Nullable Event e, final boolean debug) { + public String toString(@Nullable Event event, boolean debug) { String hand = offHand ? "off hand" : ""; - return String.format("%s tool of %s", hand, getExpr().toString(e, debug)); + return String.format("%s tool of %s", hand, getExpr().toString(event, debug)); } - @SuppressWarnings("unchecked") @Override public boolean setTime(final int time) { - return super.setTime(time, getExpr(), PlayerItemHeldEvent.class, PlayerBucketFillEvent.class, PlayerBucketEmptyEvent.class); + return super.setTime(time, getExpr(), PlayerItemHeldEvent.class, PlayerBucketEvent.class); } } From 52670e9d4ead0c56725adee562e6222f06a44e81 Mon Sep 17 00:00:00 2001 From: Fusezion Date: Sun, 16 Mar 2025 06:25:23 -0400 Subject: [PATCH 02/12] ExprTool.sk - Add basic testing --- .../tests/syntaxes/expressions/ExprTool.sk | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/test/skript/tests/syntaxes/expressions/ExprTool.sk diff --git a/src/test/skript/tests/syntaxes/expressions/ExprTool.sk b/src/test/skript/tests/syntaxes/expressions/ExprTool.sk new file mode 100644 index 00000000000..8a700bac69c --- /dev/null +++ b/src/test/skript/tests/syntaxes/expressions/ExprTool.sk @@ -0,0 +1,41 @@ +test "ExprTool": + spawn a skeleton at test location: + set helmet of entity to diamond helmet # preventing death from sun + set {_skeleton} to entity + + # Main Hand + + set main hand of {_skeleton} to a diamond + assert main hand of {_skeleton} is a diamond with "The held item of the skeleton wasn't set to a diamond" + + add 3 diamonds to the tool of {_skeleton} + assert weapon of {_skeleton} is 4 diamonds with "The held item of the skeleton didn't become 4 diamonds" + + remove stone from the held item of {_skeleton} + assert held item of {_skeleton} is 4 diamonds with "The held item of the skeleton was no longer 4 diamonds after removing a different item" + + remove 2 diamonds from the main hand of {_skeleton} + assert main hand of {_skeleton} is 2 diamonds with "The held item of the skeleton did not become 2 diamonds after removing 2 diamonds from 4" + + clear main hand of {_skeleton} + assert main hand of {_skeleton} is air with "The held item of the skeleton did not become air after being cleared" + + # Off Hand + + set offhand of {_skeleton} to a diamond + assert offhand of {_skeleton} is a diamond with "The offhand item of the skeleton wasn't set to a diamond" + + add 3 diamonds to the offhand tool of {_skeleton} + assert offhand tool of {_skeleton} is 4 diamonds with "The offhand item of the skeleton didn't become 4 diamonds" + + remove stone from the offhand item of {_skeleton} + assert offhand of {_skeleton} is 4 diamonds with "The offhand item of the skeleton was no longer 4 diamonds after removing an incorrect item" + + remove 2 diamonds from the offhand of {_skeleton} + assert offhand of {_skeleton} is 2 diamonds with "The offhand item of the skeleton did not become 2 diamonds after removing 2 diamonds from 4" + + clear offhand of {_skeleton} + assert offhand of {_skeleton} is air with "The offhand item of the skeleton did not become air after being cleared" + + # Cleanup + delete entity within {_skeleton} \ No newline at end of file From 46de19c50bc3f5d292f2fe0461906bda12a7aac4 Mon Sep 17 00:00:00 2001 From: Fusezion Date: Sun, 16 Mar 2025 07:13:44 -0400 Subject: [PATCH 03/12] ExprTool.sk - new line --- src/test/skript/tests/syntaxes/expressions/ExprTool.sk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/skript/tests/syntaxes/expressions/ExprTool.sk b/src/test/skript/tests/syntaxes/expressions/ExprTool.sk index 8a700bac69c..bb7c14bce61 100644 --- a/src/test/skript/tests/syntaxes/expressions/ExprTool.sk +++ b/src/test/skript/tests/syntaxes/expressions/ExprTool.sk @@ -38,4 +38,4 @@ test "ExprTool": assert offhand of {_skeleton} is air with "The offhand item of the skeleton did not become air after being cleared" # Cleanup - delete entity within {_skeleton} \ No newline at end of file + delete entity within {_skeleton} From c3667079dcf17b6eb59a861aa9323399b701c4d4 Mon Sep 17 00:00:00 2001 From: Fusezion Date: Sun, 16 Mar 2025 08:44:39 -0400 Subject: [PATCH 04/12] Apply suggestions from code review Add initial suggestions Co-authored-by: Efnilite <35348263+Efnilite@users.noreply.github.com> --- src/main/java/ch/njol/skript/expressions/ExprTool.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprTool.java b/src/main/java/ch/njol/skript/expressions/ExprTool.java index 6b27596f3c6..9feaf5a9132 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprTool.java +++ b/src/main/java/ch/njol/skript/expressions/ExprTool.java @@ -31,6 +31,7 @@ """) @Since("1.0") public class ExprTool extends PropertyExpression { + static { String[] patterns = new String[]{ "[the] (tool|held item|weapon|main[ ]hand) [of %livingentities%]", @@ -115,7 +116,7 @@ public String toString(@Nullable Event event, boolean debug) { } @Override - public boolean setTime(final int time) { + public boolean setTime(int time) { return super.setTime(time, getExpr(), PlayerItemHeldEvent.class, PlayerBucketEvent.class); } From 2540dce152967c2534e3e456b28cbc4178acb51b Mon Sep 17 00:00:00 2001 From: Fusezion Date: Sun, 16 Mar 2025 08:51:30 -0400 Subject: [PATCH 05/12] ExprTool.java - Change toString to SSB --- src/main/java/ch/njol/skript/expressions/ExprTool.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprTool.java b/src/main/java/ch/njol/skript/expressions/ExprTool.java index 9feaf5a9132..fcf4e296494 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprTool.java +++ b/src/main/java/ch/njol/skript/expressions/ExprTool.java @@ -7,6 +7,7 @@ import ch.njol.skript.lang.Expression; import ch.njol.skript.lang.ExpressionType; import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.lang.SyntaxStringBuilder; import ch.njol.skript.registrations.Classes; import ch.njol.skript.util.slot.EquipmentSlot; import ch.njol.skript.util.slot.InventorySlot; @@ -111,8 +112,11 @@ public Class getReturnType() { @Override public String toString(@Nullable Event event, boolean debug) { - String hand = offHand ? "off hand" : ""; - return String.format("%s tool of %s", hand, getExpr().toString(event, debug)); + SyntaxStringBuilder syntaxBuilder = new SyntaxStringBuilder(event, debug); + if (offHand) + syntaxBuilder.append("off hand"); + syntaxBuilder.append("tool of", getExpr()); + return syntaxBuilder.toString(); } @Override From 2d30c9b94374a130b7baee9c668059fddadb9cdd Mon Sep 17 00:00:00 2001 From: Fusezion Date: Sun, 23 Mar 2025 02:37:14 -0400 Subject: [PATCH 06/12] Remove support for only "offhand" and "mainhand" --- .../ch/njol/skript/expressions/ExprTool.java | 24 ++++++++++------- .../tests/syntaxes/expressions/ExprTool.sk | 26 +++++++++---------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprTool.java b/src/main/java/ch/njol/skript/expressions/ExprTool.java index 6e93944eafd..d8306680bfa 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprTool.java +++ b/src/main/java/ch/njol/skript/expressions/ExprTool.java @@ -9,6 +9,7 @@ import ch.njol.skript.lang.SkriptParser.ParseResult; import ch.njol.skript.lang.SyntaxStringBuilder; import ch.njol.skript.registrations.Classes; +import ch.njol.skript.registrations.EventValues; import ch.njol.skript.util.slot.EquipmentSlot; import ch.njol.skript.util.slot.InventorySlot; import ch.njol.skript.util.slot.Slot; @@ -35,19 +36,19 @@ public class ExprTool extends PropertyExpression { static { String[] patterns = new String[]{ - "[the] (tool|held item|weapon|main[ ]hand) [of %livingentities%]", - "%livingentities%'[s] (tool|held item|weapon|main[ ]hand)", - "[the] off[ ]hand [tool|item] [of %livingentities%]", - "%livingentities%'[s] off[ ]hand [tool|item]" + "[the] (tool|held item|weapon) [of %livingentities%]", + "%livingentities%'[s] (tool|held item|weapon)", + "[the] off[ ]hand (tool|item) [of %livingentities%]", + "%livingentities%'[s] off[ ]hand (tool|item)" }; Skript.registerExpression(ExprTool.class, Slot.class, ExpressionType.PROPERTY, patterns);; } private boolean offHand; - @SuppressWarnings({"unchecked", "null"}) @Override public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parser) { + //noinspection unchecked setExpr((Expression) exprs[0]); offHand = matchedPattern >= 2; return true; @@ -96,10 +97,15 @@ public void setItem(final @Nullable ItemStack item) { return new EquipmentSlot(equipment, offHand ? org.bukkit.inventory.EquipmentSlot.OFF_HAND : org.bukkit.inventory.EquipmentSlot.HAND) { @Override public String toString(@Nullable Event event, boolean debug) { - String time = getTime() == 1 ? "future " : getTime() == -1 ? "former " : ""; - String hand = offHand ? "off hand" : ""; - String item = Classes.toString(getItem()); - return String.format("%s %s tool of %s", time, hand, item); + SyntaxStringBuilder syntaxBuilder = new SyntaxStringBuilder(event, debug); + switch (getTime()) { + case EventValues.TIME_FUTURE -> syntaxBuilder.append("future"); + case EventValues.TIME_PAST -> syntaxBuilder.append("former"); + } + if (offHand) + syntaxBuilder.append("off hand"); + syntaxBuilder.append("tool of", Classes.toString(getItem())); + return syntaxBuilder.toString(); } }; }); diff --git a/src/test/skript/tests/syntaxes/expressions/ExprTool.sk b/src/test/skript/tests/syntaxes/expressions/ExprTool.sk index bb7c14bce61..bc5b8e73e99 100644 --- a/src/test/skript/tests/syntaxes/expressions/ExprTool.sk +++ b/src/test/skript/tests/syntaxes/expressions/ExprTool.sk @@ -5,8 +5,8 @@ test "ExprTool": # Main Hand - set main hand of {_skeleton} to a diamond - assert main hand of {_skeleton} is a diamond with "The held item of the skeleton wasn't set to a diamond" + set tool of {_skeleton} to a diamond + assert tool of {_skeleton} is a diamond with "The held item of the skeleton wasn't set to a diamond" add 3 diamonds to the tool of {_skeleton} assert weapon of {_skeleton} is 4 diamonds with "The held item of the skeleton didn't become 4 diamonds" @@ -14,28 +14,28 @@ test "ExprTool": remove stone from the held item of {_skeleton} assert held item of {_skeleton} is 4 diamonds with "The held item of the skeleton was no longer 4 diamonds after removing a different item" - remove 2 diamonds from the main hand of {_skeleton} - assert main hand of {_skeleton} is 2 diamonds with "The held item of the skeleton did not become 2 diamonds after removing 2 diamonds from 4" + remove 2 diamonds from the tool of {_skeleton} + assert tool of {_skeleton} is 2 diamonds with "The held item of the skeleton did not become 2 diamonds after removing 2 diamonds from 4" - clear main hand of {_skeleton} - assert main hand of {_skeleton} is air with "The held item of the skeleton did not become air after being cleared" + clear tool of {_skeleton} + assert tool of {_skeleton} is air with "The held item of the skeleton did not become air after being cleared" # Off Hand - set offhand of {_skeleton} to a diamond - assert offhand of {_skeleton} is a diamond with "The offhand item of the skeleton wasn't set to a diamond" + set offhand tool of {_skeleton} to a diamond + assert offhand tool of {_skeleton} is a diamond with "The offhand item of the skeleton wasn't set to a diamond" add 3 diamonds to the offhand tool of {_skeleton} assert offhand tool of {_skeleton} is 4 diamonds with "The offhand item of the skeleton didn't become 4 diamonds" remove stone from the offhand item of {_skeleton} - assert offhand of {_skeleton} is 4 diamonds with "The offhand item of the skeleton was no longer 4 diamonds after removing an incorrect item" + assert offhand tool of {_skeleton} is 4 diamonds with "The offhand item of the skeleton was no longer 4 diamonds after removing an incorrect item" - remove 2 diamonds from the offhand of {_skeleton} - assert offhand of {_skeleton} is 2 diamonds with "The offhand item of the skeleton did not become 2 diamonds after removing 2 diamonds from 4" + remove 2 diamonds from the offhand tool of {_skeleton} + assert offhand tool of {_skeleton} is 2 diamonds with "The offhand item of the skeleton did not become 2 diamonds after removing 2 diamonds from 4" - clear offhand of {_skeleton} - assert offhand of {_skeleton} is air with "The offhand item of the skeleton did not become air after being cleared" + clear offhand item of {_skeleton} + assert offhand tool of {_skeleton} is air with "The offhand item of the skeleton did not become air after being cleared" # Cleanup delete entity within {_skeleton} From 5a9c639c0aba06d8e9c829b15fe9909dbdc0602e Mon Sep 17 00:00:00 2001 From: Fusezion Date: Sun, 23 Mar 2025 03:08:40 -0400 Subject: [PATCH 07/12] Move patterns back into the method --- src/main/java/ch/njol/skript/expressions/ExprTool.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprTool.java b/src/main/java/ch/njol/skript/expressions/ExprTool.java index d8306680bfa..7bfd48688d9 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprTool.java +++ b/src/main/java/ch/njol/skript/expressions/ExprTool.java @@ -35,13 +35,12 @@ public class ExprTool extends PropertyExpression { static { - String[] patterns = new String[]{ + Skript.registerExpression(ExprTool.class, Slot.class, ExpressionType.PROPERTY, "[the] (tool|held item|weapon) [of %livingentities%]", "%livingentities%'[s] (tool|held item|weapon)", "[the] off[ ]hand (tool|item) [of %livingentities%]", "%livingentities%'[s] off[ ]hand (tool|item)" - }; - Skript.registerExpression(ExprTool.class, Slot.class, ExpressionType.PROPERTY, patterns);; + ); } private boolean offHand; From 1072b7f4ebe5401c045a43eef3a48b168300bbab Mon Sep 17 00:00:00 2001 From: Fusezion Date: Sun, 23 Mar 2025 12:02:09 -0400 Subject: [PATCH 08/12] Update src/main/java/ch/njol/skript/expressions/ExprTool.java Co-authored-by: sovdee <10354869+sovdeeth@users.noreply.github.com> --- src/main/java/ch/njol/skript/expressions/ExprTool.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprTool.java b/src/main/java/ch/njol/skript/expressions/ExprTool.java index 7bfd48688d9..994bf3b6028 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprTool.java +++ b/src/main/java/ch/njol/skript/expressions/ExprTool.java @@ -86,7 +86,6 @@ public void setItem(final @Nullable ItemStack item) { } } }; - } } From edd184c1db6c58b7daa2bf5686d01fff654c48df Mon Sep 17 00:00:00 2001 From: Fusezion Date: Sun, 23 Mar 2025 12:46:09 -0400 Subject: [PATCH 09/12] Move equipment slot map to BukkitUtils --- .../njol/skript/bukkitutil/BukkitUtils.java | 39 +++++++++++++++++++ .../njol/skript/util/slot/EquipmentSlot.java | 37 +++++------------- 2 files changed, 48 insertions(+), 28 deletions(-) diff --git a/src/main/java/ch/njol/skript/bukkitutil/BukkitUtils.java b/src/main/java/ch/njol/skript/bukkitutil/BukkitUtils.java index 53c43f02a7f..30102739d36 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/BukkitUtils.java +++ b/src/main/java/ch/njol/skript/bukkitutil/BukkitUtils.java @@ -2,14 +2,35 @@ import ch.njol.skript.Skript; import org.bukkit.Registry; +import org.bukkit.inventory.EquipmentSlot; import org.bukkit.potion.PotionEffectType; import org.jetbrains.annotations.Nullable; +import java.util.HashMap; +import java.util.Map; + /** * Utility class with methods pertaining to Bukkit API */ public class BukkitUtils { + private static final Map BUKKIT_EQUIPMENT_INDICES = new HashMap<>(); + private static final Map BUKKIT_EQUIPMENT_INDICES_REVERSED = new HashMap<>(); + + static { + BUKKIT_EQUIPMENT_INDICES.put(EquipmentSlot.FEET, 36); + BUKKIT_EQUIPMENT_INDICES.put(EquipmentSlot.LEGS, 37); + BUKKIT_EQUIPMENT_INDICES.put(EquipmentSlot.CHEST, 38); + BUKKIT_EQUIPMENT_INDICES.put(EquipmentSlot.HEAD, 39); + BUKKIT_EQUIPMENT_INDICES.put(EquipmentSlot.OFF_HAND, 40); + + BUKKIT_EQUIPMENT_INDICES_REVERSED.put(36, EquipmentSlot.FEET); + BUKKIT_EQUIPMENT_INDICES_REVERSED.put(37, EquipmentSlot.LEGS); + BUKKIT_EQUIPMENT_INDICES_REVERSED.put(38, EquipmentSlot.CHEST); + BUKKIT_EQUIPMENT_INDICES_REVERSED.put(39, EquipmentSlot.HEAD); + BUKKIT_EQUIPMENT_INDICES_REVERSED.put(40, EquipmentSlot.OFF_HAND); + } + /** * Check if a registry exists * @@ -36,4 +57,22 @@ public static boolean registryExists(String registry) { return null; } + /** + * Get the inventory slot index of the {@link EquipmentSlot} + * @param equipmentSlot The equipment slot to get the index of + * @return The equipment slot index of the provided slot, otherwise null if invalid + */ + public static Integer getEquipmentSlotIndex(EquipmentSlot equipmentSlot) { + return BUKKIT_EQUIPMENT_INDICES.get(equipmentSlot); + } + + /** + * Get the {@link EquipmentSlot} represented by the inventory slot index + * @param slotIndex The index of the equipment slot + * @return The equipment slot the provided slot index, otherwise null if invalid + */ + public static EquipmentSlot getEquipmentSlotFromIndex(Integer slotIndex) { + return BUKKIT_EQUIPMENT_INDICES_REVERSED.get(slotIndex); + } + } diff --git a/src/main/java/ch/njol/skript/util/slot/EquipmentSlot.java b/src/main/java/ch/njol/skript/util/slot/EquipmentSlot.java index 43560ef408f..b9ac3d24abe 100644 --- a/src/main/java/ch/njol/skript/util/slot/EquipmentSlot.java +++ b/src/main/java/ch/njol/skript/util/slot/EquipmentSlot.java @@ -1,5 +1,6 @@ package ch.njol.skript.util.slot; +import ch.njol.skript.bukkitutil.BukkitUtils; import ch.njol.skript.bukkitutil.PlayerUtils; import ch.njol.skript.registrations.Classes; import com.google.common.base.Preconditions; @@ -12,9 +13,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.HashMap; import java.util.Locale; -import java.util.Map; /** * Represents equipment slot of an entity. @@ -130,18 +129,6 @@ public void set(EntityEquipment equipment, @Nullable ItemStack item) { } - private static final org.bukkit.inventory.EquipmentSlot[] BUKKIT_VALUES = org.bukkit.inventory.EquipmentSlot.values(); - - private static final Map BUKKIT_SLOT_INDICES = new HashMap<>(); - - static { - BUKKIT_SLOT_INDICES.put(org.bukkit.inventory.EquipmentSlot.FEET, 36); - BUKKIT_SLOT_INDICES.put(org.bukkit.inventory.EquipmentSlot.LEGS, 37); - BUKKIT_SLOT_INDICES.put(org.bukkit.inventory.EquipmentSlot.CHEST, 38); - BUKKIT_SLOT_INDICES.put(org.bukkit.inventory.EquipmentSlot.HEAD, 39); - BUKKIT_SLOT_INDICES.put(org.bukkit.inventory.EquipmentSlot.OFF_HAND, 40); - } - private final EntityEquipment entityEquipment; private EquipSlot skriptSlot; private final int slotIndex; @@ -195,13 +182,7 @@ public EquipmentSlot(@NotNull EntityEquipment equipment, @NotNull org.bukkit.inv } public EquipmentSlot(@NotNull HumanEntity holder, int index) { - /* - * slot: 6 entries in EquipSlot, indices descending - * So this math trick gets us the EquipSlot from inventory slot index - * slotToString: Referring to numeric slot id, right? - */ - //noinspection DataFlowIssue - this(holder.getEquipment(), BUKKIT_VALUES[41 - index], true); + this(holder.getEquipment(), BukkitUtils.getEquipmentSlotFromIndex(index), true); } @Override @@ -210,7 +191,7 @@ public EquipmentSlot(@NotNull HumanEntity holder, int index) { return skriptSlot.get(entityEquipment); return entityEquipment.getItem(bukkitSlot); } - + @Override public void setItem(@Nullable ItemStack item) { if (skriptSlot != null) { @@ -221,13 +202,13 @@ public void setItem(@Nullable ItemStack item) { if (entityEquipment.getHolder() instanceof Player player) PlayerUtils.updateInventory(player); } - + @Override public int getAmount() { ItemStack item = getItem(); return item != null ? item.getAmount() : 0; } - + @Override public void setAmount(int amount) { ItemStack item = getItem(); @@ -235,7 +216,7 @@ public void setAmount(int amount) { item.setAmount(amount); setItem(item); } - + /** * @deprecated Use {@link EquipmentSlot#EquipmentSlot(EntityEquipment, org.bukkit.inventory.EquipmentSlot)} and {@link #getEquipmentSlot()} */ @@ -259,8 +240,8 @@ public int getIndex() { return slotIndex; } else if (skriptSlot != null) { return skriptSlot.slotNumber; - } else if (BUKKIT_SLOT_INDICES.containsKey(bukkitSlot)) { - return BUKKIT_SLOT_INDICES.get(bukkitSlot); + } else if (BukkitUtils.getEquipmentSlotIndex(bukkitSlot) != null) { + return BukkitUtils.getEquipmentSlotIndex(bukkitSlot); } return -1; } @@ -279,5 +260,5 @@ public String toString(@Nullable Event event, boolean debug) { } return Classes.toString(getItem()); } - + } From 9528df5ecd9648cda73ec0eb74cd7c62f4d62154 Mon Sep 17 00:00:00 2001 From: Fusezion Date: Sun, 23 Mar 2025 12:46:36 -0400 Subject: [PATCH 10/12] Requested changes --- .../java/ch/njol/skript/expressions/ExprTool.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprTool.java b/src/main/java/ch/njol/skript/expressions/ExprTool.java index 994bf3b6028..52f4eefac94 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprTool.java +++ b/src/main/java/ch/njol/skript/expressions/ExprTool.java @@ -1,6 +1,7 @@ package ch.njol.skript.expressions; import ch.njol.skript.Skript; +import ch.njol.skript.bukkitutil.BukkitUtils; import ch.njol.skript.doc.*; import ch.njol.skript.effects.Delay; import ch.njol.skript.expressions.base.PropertyExpression; @@ -25,12 +26,10 @@ @Name("Tool") @Description("The item an entity is holding in their main or off hand.") -@Example(""" - player's tool is a pickaxe - player's off hand tool is a shield - set tool of all players to a diamond sword - set offhand tool of target entity to a bow -""") +@Example("player's tool is a pickaxe") +@Example("player's off hand tool is a shield") +@Example("set tool of all players to a diamond sword") +@Example("set offhand tool of target entity to a bow") @Since("1.0") public class ExprTool extends PropertyExpression { @@ -69,8 +68,8 @@ protected Slot[] get(Event event, LivingEntity[] source) { PlayerInventory inventory = playerBucketEvent.getPlayer().getInventory(); boolean isOffHand = offHand || playerBucketEvent.getHand() == org.bukkit.inventory.EquipmentSlot.OFF_HAND; - int inventorySlot = isOffHand ? EquipmentSlot.EquipSlot.OFF_HAND.slotNumber : inventory.getHeldItemSlot(); - + int inventorySlot = isOffHand ? BukkitUtils.getEquipmentSlotIndex(org.bukkit.inventory.EquipmentSlot.OFF_HAND) + : inventory.getHeldItemSlot(); return new InventorySlot(inventory, inventorySlot) { @Override public ItemStack getItem() { From 1fafd408918b374191e1600f78daae38bdd464e1 Mon Sep 17 00:00:00 2001 From: Fusezion Date: Sun, 23 Mar 2025 13:32:46 -0400 Subject: [PATCH 11/12] Update to a BiMap for equipmentslot --- .../njol/skript/bukkitutil/BukkitUtils.java | 28 +++++++------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/src/main/java/ch/njol/skript/bukkitutil/BukkitUtils.java b/src/main/java/ch/njol/skript/bukkitutil/BukkitUtils.java index 30102739d36..1697ae5d354 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/BukkitUtils.java +++ b/src/main/java/ch/njol/skript/bukkitutil/BukkitUtils.java @@ -1,34 +1,26 @@ package ch.njol.skript.bukkitutil; import ch.njol.skript.Skript; +import com.google.common.collect.BiMap; +import com.google.common.collect.HashBiMap; import org.bukkit.Registry; import org.bukkit.inventory.EquipmentSlot; import org.bukkit.potion.PotionEffectType; import org.jetbrains.annotations.Nullable; -import java.util.HashMap; -import java.util.Map; - /** * Utility class with methods pertaining to Bukkit API */ public class BukkitUtils { - private static final Map BUKKIT_EQUIPMENT_INDICES = new HashMap<>(); - private static final Map BUKKIT_EQUIPMENT_INDICES_REVERSED = new HashMap<>(); + private static final BiMap BUKKIT_EQUIPMENT_SLOT_INDICES = HashBiMap.create(); static { - BUKKIT_EQUIPMENT_INDICES.put(EquipmentSlot.FEET, 36); - BUKKIT_EQUIPMENT_INDICES.put(EquipmentSlot.LEGS, 37); - BUKKIT_EQUIPMENT_INDICES.put(EquipmentSlot.CHEST, 38); - BUKKIT_EQUIPMENT_INDICES.put(EquipmentSlot.HEAD, 39); - BUKKIT_EQUIPMENT_INDICES.put(EquipmentSlot.OFF_HAND, 40); - - BUKKIT_EQUIPMENT_INDICES_REVERSED.put(36, EquipmentSlot.FEET); - BUKKIT_EQUIPMENT_INDICES_REVERSED.put(37, EquipmentSlot.LEGS); - BUKKIT_EQUIPMENT_INDICES_REVERSED.put(38, EquipmentSlot.CHEST); - BUKKIT_EQUIPMENT_INDICES_REVERSED.put(39, EquipmentSlot.HEAD); - BUKKIT_EQUIPMENT_INDICES_REVERSED.put(40, EquipmentSlot.OFF_HAND); + BUKKIT_EQUIPMENT_SLOT_INDICES.put(EquipmentSlot.FEET, 36); + BUKKIT_EQUIPMENT_SLOT_INDICES.put(EquipmentSlot.LEGS, 37); + BUKKIT_EQUIPMENT_SLOT_INDICES.put(EquipmentSlot.CHEST, 38); + BUKKIT_EQUIPMENT_SLOT_INDICES.put(EquipmentSlot.HEAD, 39); + BUKKIT_EQUIPMENT_SLOT_INDICES.put(EquipmentSlot.OFF_HAND, 40); } /** @@ -63,7 +55,7 @@ public static boolean registryExists(String registry) { * @return The equipment slot index of the provided slot, otherwise null if invalid */ public static Integer getEquipmentSlotIndex(EquipmentSlot equipmentSlot) { - return BUKKIT_EQUIPMENT_INDICES.get(equipmentSlot); + return BUKKIT_EQUIPMENT_SLOT_INDICES.get(equipmentSlot); } /** @@ -72,7 +64,7 @@ public static Integer getEquipmentSlotIndex(EquipmentSlot equipmentSlot) { * @return The equipment slot the provided slot index, otherwise null if invalid */ public static EquipmentSlot getEquipmentSlotFromIndex(Integer slotIndex) { - return BUKKIT_EQUIPMENT_INDICES_REVERSED.get(slotIndex); + return BUKKIT_EQUIPMENT_SLOT_INDICES.inverse().get(slotIndex); } } From 39b2d6681ddaeaa8baf33cc731bc9bacbb345fe2 Mon Sep 17 00:00:00 2001 From: Fusezion Date: Mon, 24 Mar 2025 14:30:14 -0400 Subject: [PATCH 12/12] Update src/main/java/ch/njol/skript/bukkitutil/BukkitUtils.java Co-authored-by: Efnilite <35348263+Efnilite@users.noreply.github.com> --- src/main/java/ch/njol/skript/bukkitutil/BukkitUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/bukkitutil/BukkitUtils.java b/src/main/java/ch/njol/skript/bukkitutil/BukkitUtils.java index 1697ae5d354..bcc1659fc2d 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/BukkitUtils.java +++ b/src/main/java/ch/njol/skript/bukkitutil/BukkitUtils.java @@ -63,7 +63,7 @@ public static Integer getEquipmentSlotIndex(EquipmentSlot equipmentSlot) { * @param slotIndex The index of the equipment slot * @return The equipment slot the provided slot index, otherwise null if invalid */ - public static EquipmentSlot getEquipmentSlotFromIndex(Integer slotIndex) { + public static EquipmentSlot getEquipmentSlotFromIndex(int slotIndex) { return BUKKIT_EQUIPMENT_SLOT_INDICES.inverse().get(slotIndex); }