/
FakeItemCommand.java
135 lines (111 loc) · 5.8 KB
/
FakeItemCommand.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package net.aufdemrand.denizen.scripts.commands.item;
import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.objects.dItem;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.DenizenCore;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.Duration;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.objects.dList;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.commands.AbstractCommand;
import net.aufdemrand.denizencore.utilities.scheduling.OneTimeSchedulable;
import net.minecraft.server.v1_8_R3.ItemStack;
import net.minecraft.server.v1_8_R3.PacketPlayOutSetSlot;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftInventory;
import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import java.util.Arrays;
import java.util.List;
public class FakeItemCommand extends AbstractCommand {
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
/* Match arguments to expected variables */
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("slot")
&& arg.matchesPrefix("slot")
&& arg.matchesPrimitive(aH.PrimitiveType.Integer)) {
scriptEntry.addObject("slot", arg.asElement());
}
else if (!scriptEntry.hasObject("duration")
&& arg.matchesPrefix("duration")
&& arg.matchesArgumentType(Duration.class)) {
scriptEntry.addObject("duration", arg.asType(Duration.class));
}
else if (!scriptEntry.hasObject("item")
&& arg.matchesArgumentType(dItem.class)) {
scriptEntry.addObject("item", arg.asType(dItem.class));
}
else if (!scriptEntry.hasObject("players")
&& arg.matchesArgumentList(dPlayer.class)
&& arg.matchesPrefix("players")) {
scriptEntry.addObject("players", arg.asType(dList.class).filter(dPlayer.class));
}
else if (!scriptEntry.hasObject("player_only")
&& arg.matches("player_only")) {
scriptEntry.addObject("player_only", new Element(true));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("item")) {
throw new InvalidArgumentsException("Must specify a valid item to fake!");
}
if (!scriptEntry.hasObject("slot")) {
throw new InvalidArgumentsException("Must specify a valid slot!");
}
scriptEntry.defaultObject("duration", Duration.ZERO).defaultObject("player_only", new Element(false))
.defaultObject("players", Arrays.asList(((BukkitScriptEntryData)scriptEntry.entryData).getPlayer()));
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
dItem item = scriptEntry.getdObject("item");
final Element slot = scriptEntry.getElement("slot");
Duration duration = scriptEntry.getdObject("duration");
final List<dPlayer> players = (List<dPlayer>) scriptEntry.getObject("players");
final Element player_only = scriptEntry.getElement("player_only");
dB.report(scriptEntry, getName(), item.debug() + slot.debug() + duration.debug()
+ aH.debugList("players", players) + player_only.debug());
net.minecraft.server.v1_8_R3.ItemStack itemStack = CraftItemStack.asNMSCopy(item.getItemStack());
for (dPlayer player : players) {
setSlot((CraftPlayer) player.getPlayerEntity(), slot.asInt() - 1, itemStack, player_only.asBoolean());
}
if (duration.getSeconds() > 0) {
DenizenCore.schedule(new OneTimeSchedulable(new Runnable() {
@Override
public void run() {
for (dPlayer player : players) {
CraftPlayer craftPlayer = (CraftPlayer) player.getPlayerEntity();
ItemStack original = CraftItemStack.asNMSCopy(craftPlayer.getOpenInventory()
.getItem(translateSlot(craftPlayer, slot.asInt() - 1, player_only.asBoolean())));
setSlot(craftPlayer, slot.asInt() - 1, original, player_only.asBoolean());
}
}
}, (float) duration.getSeconds()));
}
}
static void setSlot(CraftPlayer craftPlayer, int slot, ItemStack itemStack, boolean player_only) {
PacketPlayOutSetSlot setSlotPacket = new PacketPlayOutSetSlot(
player_only ? 0 : craftPlayer.getHandle().activeContainer.windowId,
translateSlot(craftPlayer, slot, player_only),
itemStack
);
craftPlayer.getHandle().playerConnection.sendPacket(setSlotPacket);
}
static int translateSlot(CraftPlayer craftPlayer, int slot, boolean player_only) {
int total = player_only ? 41 : craftPlayer.getOpenInventory().countSlots();
if (total == 41) {
total += 4;
if (slot > 35) {
slot = 44 + 36 - slot;
}
}
return (int) (slot + (total - 9) - (9 * (2 * Math.floor(slot / 9))));
}
}