diff --git a/src/main/java/net/wurstclient/commands/GiveCmd.java b/src/main/java/net/wurstclient/commands/GiveCmd.java index 603feb58f5..f7f890fb73 100644 --- a/src/main/java/net/wurstclient/commands/GiveCmd.java +++ b/src/main/java/net/wurstclient/commands/GiveCmd.java @@ -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; @@ -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; - } } diff --git a/src/main/java/net/wurstclient/util/CmdUtils.java b/src/main/java/net/wurstclient/util/CmdUtils.java index 183f3b4808..00ac9305f8 100644 --- a/src/main/java/net/wurstclient/util/CmdUtils.java +++ b/src/main/java/net/wurstclient/util/CmdUtils.java @@ -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; @@ -20,6 +24,8 @@ public enum CmdUtils { ; + private static final MinecraftClient MC = WurstClient.MC; + public static Feature findFeature(String name) throws CmdError { Stream stream = @@ -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); + } }