Skip to content

Commit

Permalink
Refactor item name parsing in .give and .itemlist
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander01998 committed Nov 26, 2024
1 parent f7e2aaa commit 4d1bc07
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 36 deletions.
25 changes: 2 additions & 23 deletions src/main/java/net/wurstclient/commands/GiveCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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++)
Expand Down
13 changes: 2 additions & 11 deletions src/main/java/net/wurstclient/commands/ItemListCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/net/wurstclient/util/CmdUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
6 changes: 4 additions & 2 deletions src/main/java/net/wurstclient/util/ItemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);

Expand Down

0 comments on commit 4d1bc07

Please sign in to comment.