/
GlowCommand.java
113 lines (93 loc) · 4.5 KB
/
GlowCommand.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
package net.aufdemrand.denizen.scripts.commands.player;
import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.packets.StatusEffectHelper;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
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 net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.UUID;
public class GlowCommand extends AbstractCommand {
public static HashMap<Integer, HashSet<UUID>> glowViewers = new HashMap<Integer, HashSet<UUID>>();
public static void unGlow(LivingEntity e) {
if (glowViewers.containsKey(e.getEntityId())) {
glowViewers.remove(e.getEntityId());
e.setGlowing(false);
if (CitizensAPI.getNPCRegistry().isNPC(e)) {
CitizensAPI.getNPCRegistry().getNPC(e).data().setPersistent(NPC.GLOWING_METADATA, false);
}
}
}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("entities")
&& arg.matchesArgumentList(dEntity.class)) {
scriptEntry.addObject("entities", arg.asType(dList.class).filter(dEntity.class));
}
else if (!scriptEntry.hasObject("glowing")
&& arg.matchesPrimitive(aH.PrimitiveType.Boolean)) {
scriptEntry.addObject("glowing", arg.asElement());
}
else {
arg.reportUnhandled();
}
}
scriptEntry.defaultObject("glowing", new Element("true"));
if (!((BukkitScriptEntryData) scriptEntry.entryData).hasPlayer()) {
throw new InvalidArgumentsException("Must have a valid player link!");
}
if (!scriptEntry.hasObject("entities")) {
throw new InvalidArgumentsException("Must specify entities to make glow!");
}
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
final ArrayList<dEntity> entities = (ArrayList<dEntity>)scriptEntry.getObject("entities");
Element glowing = scriptEntry.getElement("glowing");
dB.report(scriptEntry, getName(), aH.debugList("entities", entities) + glowing.debug());
boolean shouldGlow = glowing.asBoolean();
final Player player = ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getPlayerEntity();
if (player == null) {
dB.echoError(scriptEntry.getResidingQueue(), "Invalid/non-spawned player link!");
return;
}
for (dEntity ent: entities) {
if (CitizensAPI.getNPCRegistry().isNPC(ent.getLivingEntity())) {
CitizensAPI.getNPCRegistry().getNPC(ent.getLivingEntity()).data().setPersistent(NPC.GLOWING_METADATA, shouldGlow);
}
if (shouldGlow) {
HashSet<UUID> players = glowViewers.get(ent.getLivingEntity().getEntityId());
if (players == null) {
players = new HashSet<UUID>();
glowViewers.put(ent.getLivingEntity().getEntityId(), players);
}
players.add(player.getUniqueId());
}
else {
HashSet<UUID> players = glowViewers.get(ent.getLivingEntity().getEntityId());
if (players != null) {
players.remove(player.getUniqueId());
shouldGlow = !players.isEmpty();
if (!shouldGlow) {
glowViewers.remove(ent.getLivingEntity().getEntityId());
}
}
}
ent.getLivingEntity().setGlowing(shouldGlow);
}
}
}