/
HideEntitiesHelper.java
207 lines (184 loc) · 7.44 KB
/
HideEntitiesHelper.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package com.denizenscript.denizen.utilities.entity;
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.utilities.packets.NetworkInterceptHelper;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.*;
public class HideEntitiesHelper {
public static class PlayerHideMap {
public UUID player;
public HashSet<UUID> entitiesHidden = new HashSet<>();
public HashSet<UUID> overridinglyShow = new HashSet<>();
public HashSet<String> matchersHidden = new HashSet<>();
public boolean shouldHideViaMatcher(Entity entity) {
if (entity == null) {
return false;
}
if (!matchersHidden.isEmpty()) {
if (overridinglyShow.contains(entity.getUniqueId())) {
return false;
}
EntityTag entityTag = new EntityTag(entity);
for (String matchable : matchersHidden) {
if (entityTag.tryAdvancedMatcher(matchable)) {
if (entity instanceof Player) {
Player thisPlayer = Bukkit.getPlayer(player);
if (thisPlayer != null && thisPlayer.canSee((Player) entity)) {
thisPlayer.hidePlayer(Denizen.getInstance(), (Player) entity);
}
}
return true;
}
}
}
return false;
}
public boolean shouldHide(Entity entity) {
if (entity == null) {
return false;
}
if (overridinglyShow.contains(entity.getUniqueId())) {
return false;
}
if (entitiesHidden.contains(entity.getUniqueId())) {
return true;
}
if (defaultHidden.contains(entity.getUniqueId())) {
return true;
}
return shouldHideViaMatcher(entity);
}
}
public static HashMap<UUID, PlayerHideMap> playerHides = new HashMap<>();
public static HashSet<UUID> defaultHidden = new HashSet<>();
public static boolean hasAnyHides() {
return !playerHides.isEmpty() || !defaultHidden.isEmpty();
}
public static PlayerHideMap getPlayerMapFor(UUID player) {
PlayerHideMap map = playerHides.get(player);
if (map == null) {
map = new PlayerHideMap();
map.player = player;
playerHides.put(player, map);
}
return map;
}
public static boolean playerShouldHide(UUID player, Entity ent) {
PlayerHideMap map = playerHides.get(player);
if (map == null) {
return defaultHidden.contains(ent.getUniqueId());
}
return map.shouldHide(ent);
}
public static boolean addHide(UUID player, UUID entity) {
NetworkInterceptHelper.enable();
ensurePlayerHiding();
if (player == null) {
return defaultHidden.add(entity);
}
PlayerHideMap map = getPlayerMapFor(player);
map.overridinglyShow.remove(entity);
return map.entitiesHidden.add(entity);
}
public static void hideEntity(Player player, Entity entity) {
if (addHide(player == null ? null : player.getUniqueId(), entity.getUniqueId())) {
if (player == null) {
for (Player pl : Bukkit.getOnlinePlayers()) {
NMSHandler.entityHelper.sendHidePacket(pl, entity);
}
}
else {
NMSHandler.entityHelper.sendHidePacket(player, entity);
}
}
}
public static boolean removeHide(UUID player, UUID entity) {
NetworkInterceptHelper.enable();
if (player == null) {
return defaultHidden.remove(entity);
}
PlayerHideMap map = playerHides.get(player);
if (defaultHidden.contains(entity) || (map != null && map.shouldHideViaMatcher(Bukkit.getEntity(entity)))) {
if (map == null) {
map = new PlayerHideMap();
map.player = player;
playerHides.put(player, map);
}
map.entitiesHidden.remove(entity);
return map.overridinglyShow.add(entity);
}
if (map == null) {
return false;
}
boolean result = map.entitiesHidden.remove(entity);
if (result && map.entitiesHidden.isEmpty() && map.overridinglyShow.isEmpty() && map.matchersHidden.isEmpty()) {
playerHides.remove(player);
}
return result;
}
public static void unhideEntity(Player player, Entity entity) {
if (removeHide(player == null ? null : player.getUniqueId(), entity.getUniqueId())) {
if (player == null) {
for (Player pl : Bukkit.getOnlinePlayers()) {
NMSHandler.entityHelper.sendShowPacket(pl, entity);
}
}
else {
NMSHandler.entityHelper.sendShowPacket(player, entity);
}
}
}
public static class EnforcePlayerHides implements Listener {
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
for (UUID id : defaultHidden) {
Player pTarget = Bukkit.getPlayer(id);
if (pTarget != null) {
event.getPlayer().hidePlayer(Denizen.getInstance(), pTarget);
}
}
final Player pl = event.getPlayer();
PlayerHideMap map = playerHides.get(pl.getUniqueId());
if (map == null) {
return;
}
new BukkitRunnable() {
@Override
public void run() {
if (pl.isOnline()) {
if (map.matchersHidden != null) {
for (Entity entity : pl.getWorld().getEntities()) {
if (map.shouldHide(entity)) {
NMSHandler.entityHelper.sendHidePacket(pl, entity);
}
}
}
else {
for (UUID id : map.entitiesHidden) {
Entity ent = Bukkit.getEntity(id);
if (ent != null) {
NMSHandler.entityHelper.sendHidePacket(pl, ent);
}
}
}
}
}
}.runTaskLater(Denizen.getInstance(), 5);
}
}
public static EnforcePlayerHides EPH = null;
public static void ensurePlayerHiding() {
if (EPH == null) {
EPH = new EnforcePlayerHides();
Bukkit.getPluginManager().registerEvents(EPH, Denizen.getInstance());
}
}
}