/
GlowCommand.java
145 lines (123 loc) · 5.49 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package com.denizenscript.denizen.scripts.commands.player;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.depends.Depends;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.entity.LivingEntity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.UUID;
public class GlowCommand extends AbstractCommand {
public GlowCommand() {
setName("glow");
setSyntax("glow [<entity>|...] (<should glow>)");
setRequiredArguments(1, 2);
isProcedural = false;
}
// <--[command]
// @Name Glow
// @Syntax glow [<entity>|...] (<should glow>)
// @Required 1
// @Maximum 2
// @Short Makes the linked player see the chosen entities as glowing.
// @Group player
//
// @Description
// Makes the linked player see the chosen entities as glowing.
// BE WARNED, THIS COMMAND IS HIGHLY EXPERIMENTAL AND MAY NOT WORK AS EXPECTED.
// This command works by globally enabling the glow effect, then whitelisting who is allowed to see it.
// This command does it's best to disable glow effect when the entity is unloaded, but does not guarantee it.
//
// @Tags
// <EntityTag.glowing>
//
// @Usage
// Use to make the player's target glow.
// - glow <player.target>
//
// @Usage
// Use to make the player's target not glow.
// - glow <player.target> false
// -->
public static HashMap<Integer, HashSet<UUID>> glowViewers = new HashMap<>();
public static void unGlow(LivingEntity e) {
if (glowViewers.containsKey(e.getEntityId())) {
glowViewers.remove(e.getEntityId());
e.setGlowing(false);
if (Depends.citizens != null && CitizensAPI.getNPCRegistry().isNPC(e)) {
CitizensAPI.getNPCRegistry().getNPC(e).data().setPersistent(NPC.GLOWING_METADATA, false);
}
}
}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (!scriptEntry.hasObject("entities")
&& arg.matchesArgumentList(EntityTag.class)) {
scriptEntry.addObject("entities", arg.asType(ListTag.class).filter(EntityTag.class, scriptEntry));
}
else if (!scriptEntry.hasObject("glowing")
&& arg.matchesBoolean()) {
scriptEntry.addObject("glowing", arg.asElement());
}
else {
arg.reportUnhandled();
}
}
scriptEntry.defaultObject("glowing", new ElementTag("true"));
if (!Utilities.entryHasPlayer(scriptEntry)) {
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) {
final ArrayList<EntityTag> entities = (ArrayList<EntityTag>) scriptEntry.getObject("entities");
ElementTag glowing = scriptEntry.getElement("glowing");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), ArgumentHelper.debugList("entities", entities) + glowing.debug());
}
boolean shouldGlow = glowing.asBoolean();
final UUID puuid = Utilities.getEntryPlayer(scriptEntry).getOfflinePlayer().getUniqueId();
if (puuid == null) {
Debug.echoError(scriptEntry.getResidingQueue(), "Invalid/non-spawned player link!");
return;
}
for (EntityTag ent : entities) {
if (Depends.citizens != null && 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<>();
glowViewers.put(ent.getLivingEntity().getEntityId(), players);
}
players.add(puuid);
}
else {
HashSet<UUID> players = glowViewers.get(ent.getLivingEntity().getEntityId());
if (players != null) {
players.remove(puuid);
shouldGlow = !players.isEmpty();
if (!shouldGlow) {
glowViewers.remove(ent.getLivingEntity().getEntityId());
}
}
}
ent.getLivingEntity().setGlowing(shouldGlow);
}
}
}