-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
BlockCrackCommand.java
124 lines (103 loc) · 4.84 KB
/
BlockCrackCommand.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
package net.aufdemrand.denizen.scripts.commands.player;
import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.nms.NMSHandler;
import net.aufdemrand.denizen.nms.interfaces.PacketHelper;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
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 org.bukkit.Location;
import org.bukkit.entity.Player;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public class BlockCrackCommand extends AbstractCommand {
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (arg.matchesPrefix("players")
&& arg.matchesArgumentList(dPlayer.class)) {
scriptEntry.addObject("players", arg.asType(dList.class).filter(dPlayer.class, scriptEntry));
}
else if (arg.matchesPrefix("progress")
&& arg.matchesPrimitive(aH.PrimitiveType.Integer)) {
scriptEntry.addObject("progress", arg.asElement());
}
else if (arg.matchesArgumentType(dLocation.class)) {
scriptEntry.addObject("location", arg.asType(dLocation.class));
}
else if (arg.matches("stack")) {
scriptEntry.addObject("stack", new Element(true));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("progress")) {
throw new InvalidArgumentsException("Must specify crack animation progress!");
}
if (!scriptEntry.hasObject("location")) {
throw new InvalidArgumentsException("Must specify a valid location!");
}
scriptEntry.defaultObject("players", Collections.singletonList(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer()))
.defaultObject("stack", new Element(false));
}
private static class IntHolder {
public int theInt;
public int base;
}
private static Map<Location, Map<UUID, IntHolder>> progressTracker = new HashMap<Location, Map<UUID, IntHolder>>();
private static int lastBase;
@Override
public void execute(ScriptEntry scriptEntry) {
List<dPlayer> players = (List<dPlayer>) scriptEntry.getObject("players");
Element progress = scriptEntry.getElement("progress");
dLocation location = scriptEntry.getdObject("location");
Element stack = scriptEntry.getElement("stack");
if (scriptEntry.dbCallShouldDebug()) {
dB.report(scriptEntry, getName(), aH.debugList("players", players)
+ progress.debug() + location.debug() + stack.debug());
}
Location loc = location.getBlock().getLocation();
if (!progressTracker.containsKey(loc)) {
progressTracker.put(loc, new HashMap<UUID, IntHolder>());
lastBase += 10;
}
Map<UUID, IntHolder> uuidInt = progressTracker.get(loc);
boolean stackVal = stack.asBoolean();
PacketHelper packetHelper = NMSHandler.getInstance().getPacketHelper();
for (dPlayer player : players) {
if (!player.isOnline()) {
dB.echoError("Players must be online!");
continue;
}
Player playerEnt = player.getPlayerEntity();
UUID uuid = playerEnt.getUniqueId();
if (!uuidInt.containsKey(uuid)) {
IntHolder newIntHolder = new IntHolder();
newIntHolder.theInt = lastBase;
newIntHolder.base = lastBase;
uuidInt.put(uuid, newIntHolder);
}
IntHolder intHolder = uuidInt.get(uuid);
if (!stackVal && intHolder.theInt > intHolder.base) {
for (int i = intHolder.base; i <= intHolder.theInt; i++) {
packetHelper.showBlockCrack(playerEnt, i, loc, -1);
}
intHolder.theInt = intHolder.base;
}
else if (stackVal && intHolder.theInt - intHolder.base > 10) {
continue;
}
int id = stackVal ? intHolder.theInt++ : intHolder.theInt;
packetHelper.showBlockCrack(player.getPlayerEntity(), id, loc, progress.asInt() - 1);
}
}
}