Skip to content

Commit

Permalink
Add CmdUtils.giveItem() helper and update .give to use it
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander01998 committed Nov 26, 2024
1 parent 4d1bc07 commit e12d356
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
20 changes: 1 addition & 19 deletions src/main/java/net/wurstclient/commands/GiveCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.StringNbtReader;
import net.minecraft.network.packet.c2s.play.CreativeInventoryActionC2SPacket;
import net.wurstclient.command.CmdError;
import net.wurstclient.command.CmdException;
import net.wurstclient.command.CmdSyntaxError;
Expand Down Expand Up @@ -85,24 +84,7 @@ public void call(String[] args) throws CmdException
}

// give item
if(!placeStackInHotbar(stack))
throw new CmdError("Please clear a slot in your hotbar.");
CmdUtils.giveItem(stack);
ChatUtils.message("Item" + (amount > 1 ? "s" : "") + " created.");
}

private boolean placeStackInHotbar(ItemStack stack)
{
for(int i = 0; i < 9; i++)
{
if(!MC.player.getInventory().getStack(i).isEmpty())
continue;

MC.player.getInventory().main.set(i, stack);
MC.player.networkHandler.sendPacket(
new CreativeInventoryActionC2SPacket(36 + i, stack));
return true;
}

return false;
}
}
20 changes: 20 additions & 0 deletions src/main/java/net/wurstclient/util/CmdUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@

import java.util.stream.Stream;

import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.network.packet.c2s.play.CreativeInventoryActionC2SPacket;
import net.wurstclient.Feature;
import net.wurstclient.WurstClient;
import net.wurstclient.command.CmdError;
Expand All @@ -20,6 +24,8 @@ public enum CmdUtils
{
;

private static final MinecraftClient MC = WurstClient.MC;

public static Feature findFeature(String name) throws CmdError
{
Stream<Feature> stream =
Expand Down Expand Up @@ -57,4 +63,18 @@ public static Item parseItem(String nameOrId) throws CmdSyntaxError

return item;
}

public static void giveItem(ItemStack stack) throws CmdError
{
PlayerInventory inventory = MC.player.getInventory();
int slot = inventory.getEmptySlot();
if(slot < 0)
throw new CmdError("Cannot give item. Your inventory is full.");

inventory.setStack(slot, stack);
CreativeInventoryActionC2SPacket packet =
new CreativeInventoryActionC2SPacket(
InventoryUtils.toNetworkSlot(slot), stack);
MC.player.networkHandler.sendPacket(packet);
}
}

0 comments on commit e12d356

Please sign in to comment.