From 4d1bc0783dad041af980613daf96fbcbf7a0c19b Mon Sep 17 00:00:00 2001 From: Alexander01998 Date: Tue, 26 Nov 2024 13:01:49 +0100 Subject: [PATCH] Refactor item name parsing in .give and .itemlist --- .../net/wurstclient/commands/GiveCmd.java | 25 ++----------------- .../net/wurstclient/commands/ItemListCmd.java | 13 ++-------- .../java/net/wurstclient/util/CmdUtils.java | 13 ++++++++++ .../java/net/wurstclient/util/ItemUtils.java | 6 +++-- 4 files changed, 21 insertions(+), 36 deletions(-) diff --git a/src/main/java/net/wurstclient/commands/GiveCmd.java b/src/main/java/net/wurstclient/commands/GiveCmd.java index aa805a14ea..603feb58f5 100644 --- a/src/main/java/net/wurstclient/commands/GiveCmd.java +++ b/src/main/java/net/wurstclient/commands/GiveCmd.java @@ -15,18 +15,15 @@ import net.minecraft.component.type.NbtComponent; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; -import net.minecraft.item.Items; import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.StringNbtReader; import net.minecraft.network.packet.c2s.play.CreativeInventoryActionC2SPacket; -import net.minecraft.registry.Registries; -import net.minecraft.util.Identifier; -import net.minecraft.util.InvalidIdentifierException; import net.wurstclient.command.CmdError; import net.wurstclient.command.CmdException; import net.wurstclient.command.CmdSyntaxError; import net.wurstclient.command.Command; import net.wurstclient.util.ChatUtils; +import net.wurstclient.util.CmdUtils; import net.wurstclient.util.MathUtils; public final class GiveCmd extends Command @@ -50,13 +47,7 @@ public void call(String[] args) throws CmdException throw new CmdError("Creative mode only."); // id/name - Item item = getItem(args[0]); - - if(item == Items.AIR && MathUtils.isInteger(args[0])) - item = Item.byRawId(Integer.parseInt(args[0])); - - if(item == Items.AIR) - throw new CmdError("Item \"" + args[0] + "\" could not be found."); + Item item = CmdUtils.parseItem(args[0]); // amount int amount = 1; @@ -99,18 +90,6 @@ public void call(String[] args) throws CmdException ChatUtils.message("Item" + (amount > 1 ? "s" : "") + " created."); } - private Item getItem(String id) throws CmdSyntaxError - { - try - { - return Registries.ITEM.get(Identifier.of(id)); - - }catch(InvalidIdentifierException e) - { - throw new CmdSyntaxError("Invalid item: " + id); - } - } - private boolean placeStackInHotbar(ItemStack stack) { for(int i = 0; i < 9; i++) diff --git a/src/main/java/net/wurstclient/commands/ItemListCmd.java b/src/main/java/net/wurstclient/commands/ItemListCmd.java index 4a9fa08a5a..cc1f138510 100644 --- a/src/main/java/net/wurstclient/commands/ItemListCmd.java +++ b/src/main/java/net/wurstclient/commands/ItemListCmd.java @@ -22,7 +22,6 @@ import net.wurstclient.settings.Setting; import net.wurstclient.util.ChatUtils; import net.wurstclient.util.CmdUtils; -import net.wurstclient.util.ItemUtils; import net.wurstclient.util.MathUtils; @DontBlock @@ -80,11 +79,7 @@ private void add(Feature feature, ItemListSetting setting, String[] args) if(args.length != 4) throw new CmdSyntaxError(); - String inputItemName = args[3]; - Item item = ItemUtils.getItemFromNameOrID(inputItemName); - if(item == null) - throw new CmdSyntaxError( - "\"" + inputItemName + "\" is not a valid item."); + Item item = CmdUtils.parseItem(args[3]); String itemName = Registries.ITEM.getId(item).toString(); int index = Collections.binarySearch(setting.getItemNames(), itemName); @@ -101,11 +96,7 @@ private void remove(Feature feature, ItemListSetting setting, String[] args) if(args.length != 4) throw new CmdSyntaxError(); - String inputItemName = args[3]; - Item item = ItemUtils.getItemFromNameOrID(inputItemName); - if(item == null) - throw new CmdSyntaxError( - "\"" + inputItemName + "\" is not a valid item."); + Item item = CmdUtils.parseItem(args[3]); String itemName = Registries.ITEM.getId(item).toString(); int index = Collections.binarySearch(setting.getItemNames(), itemName); diff --git a/src/main/java/net/wurstclient/util/CmdUtils.java b/src/main/java/net/wurstclient/util/CmdUtils.java index 816d58a19d..183f3b4808 100644 --- a/src/main/java/net/wurstclient/util/CmdUtils.java +++ b/src/main/java/net/wurstclient/util/CmdUtils.java @@ -9,9 +9,11 @@ import java.util.stream.Stream; +import net.minecraft.item.Item; import net.wurstclient.Feature; import net.wurstclient.WurstClient; import net.wurstclient.command.CmdError; +import net.wurstclient.command.CmdSyntaxError; import net.wurstclient.settings.Setting; public enum CmdUtils @@ -44,4 +46,15 @@ public static Setting findSetting(Feature feature, String name) return setting; } + + public static Item parseItem(String nameOrId) throws CmdSyntaxError + { + Item item = ItemUtils.getItemFromNameOrID(nameOrId); + + if(item == null) + throw new CmdSyntaxError( + "\"" + nameOrId + "\" is not a valid item."); + + return item; + } } diff --git a/src/main/java/net/wurstclient/util/ItemUtils.java b/src/main/java/net/wurstclient/util/ItemUtils.java index 4cc9a9a978..11c82a6b26 100644 --- a/src/main/java/net/wurstclient/util/ItemUtils.java +++ b/src/main/java/net/wurstclient/util/ItemUtils.java @@ -43,8 +43,8 @@ public static Item getItemFromNameOrID(String nameOrId) { if(MathUtils.isInteger(nameOrId)) { - // There is no getOrEmpty() for raw IDs, so this detects when the - // Registry defaults and returns null instead + // There is no getOptionalValue() for raw IDs, so this detects when + // the registry defaults and returns null instead int id = Integer.parseInt(nameOrId); Item item = Registries.ITEM.get(id); if(id != 0 && Registries.ITEM.getRawId(item) == 0) @@ -55,6 +55,8 @@ public static Item getItemFromNameOrID(String nameOrId) try { + // getOptionalValue() returns null instead of Items.AIR if the + // requested item doesn't exist return Registries.ITEM.getOptionalValue(Identifier.of(nameOrId)) .orElse(null);