-
-
Notifications
You must be signed in to change notification settings - Fork 398
/
CopyItemCmd.java
97 lines (79 loc) · 2.67 KB
/
CopyItemCmd.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* Copyright (c) 2014-2024 Wurst-Imperium and contributors.
*
* This source code is subject to the terms of the GNU General Public
* License, version 3. If a copy of the GPL was not distributed with this
* file, You can obtain one at: https://www.gnu.org/licenses/gpl-3.0.txt
*/
package net.wurstclient.commands;
import net.minecraft.client.network.AbstractClientPlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.network.packet.c2s.play.CreativeInventoryActionC2SPacket;
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;
public final class CopyItemCmd extends Command
{
public CopyItemCmd()
{
super("copyitem",
"Allows you to copy items that other people are holding\n"
+ "or wearing. Requires creative mode.",
".copyitem <player> <slot>",
"Valid slots: hand, head, chest, legs, feet");
}
@Override
public void call(String[] args) throws CmdException
{
if(args.length != 2)
throw new CmdSyntaxError();
if(!MC.player.getAbilities().creativeMode)
throw new CmdError("Creative mode only.");
AbstractClientPlayerEntity player = getPlayer(args[0]);
ItemStack item = getItem(player, args[1]);
giveItem(item);
ChatUtils.message("Item copied.");
}
private AbstractClientPlayerEntity getPlayer(String name) throws CmdError
{
for(AbstractClientPlayerEntity player : MC.world.getPlayers())
{
if(!player.getName().getString().equalsIgnoreCase(name))
continue;
return player;
}
throw new CmdError("Player \"" + name + "\" could not be found.");
}
private ItemStack getItem(AbstractClientPlayerEntity player, String slot)
throws CmdSyntaxError
{
switch(slot.toLowerCase())
{
case "hand":
return player.getInventory().getMainHandStack();
case "head":
return player.getInventory().getArmorStack(3);
case "chest":
return player.getInventory().getArmorStack(2);
case "legs":
return player.getInventory().getArmorStack(1);
case "feet":
return player.getInventory().getArmorStack(0);
default:
throw new CmdSyntaxError();
}
}
private void giveItem(ItemStack stack) throws CmdError
{
int slot = MC.player.getInventory().getEmptySlot();
if(slot < 0)
throw new CmdError("Cannot give item. Your inventory is full.");
if(slot < 9)
slot += 36;
CreativeInventoryActionC2SPacket packet =
new CreativeInventoryActionC2SPacket(slot, stack);
MC.player.networkHandler.sendPacket(packet);
}
}