forked from Tofaa2/EntityLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewerEngine.java
More file actions
176 lines (148 loc) · 5.49 KB
/
ViewerEngine.java
File metadata and controls
176 lines (148 loc) · 5.49 KB
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
package me.tofaa.entitylib.ve;
import com.github.retrooper.packetevents.protocol.player.User;
import me.tofaa.entitylib.EntityLib;
import me.tofaa.entitylib.wrapper.WrapperEntity;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnmodifiableView;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
/**
* A basic viewer manipulation engine.
* Allows for a set of rules to be applied as to show and hide an entity from viewers.
*/
@ApiStatus.Experimental
public class ViewerEngine {
private final List<ViewerRule> globalRules;
private final Set<WrapperEntity> tracked;
private final ViewerEngineListener listener;
private Executor executor;
private boolean enabled = false;
/**
* Creates an instance of ViewerEngine
* It is recommended to specify explicitly the Executor that should be used.
*/
public ViewerEngine() {
this(Executors.newSingleThreadExecutor());
}
/**
* Creates an instance of ViewerEngine with a specific executor. Depending on your rules one thread might not be enough
* @param executor The executor that is used to process entity tracking.
*/
public ViewerEngine(Executor executor) {
this.globalRules = new CopyOnWriteArrayList<>();
this.tracked = Collections.newSetFromMap(new WeakHashMap<>());
this.executor = executor;
this.listener = new ViewerEngineListener(this);
}
/**
* Enables this viewer engine.
* Registers a viewer engine listener to handle tracking
*/
public void enable() {
if (enabled) {
return;
}
enabled = true;
EntityLib.getApi().getPacketEvents().getEventManager().registerListener(listener);
}
/**
* Disables this viewer engine.
* Unregisters the viewer engine listener that handles tracking.
*/
public void disable() {
if (!enabled) {
return;
}
enabled = false;
EntityLib.getApi().getPacketEvents().getEventManager().unregisterListener(listener);
}
/**
* Refreshes and updates every tracked by this viewer engine entities viewers to see if they follow the spawning rules.
* If they do not they will no longer see the entity;
*/
public void refresh() {
getTracked0().forEach(entity -> {
for (UUID viewer : entity.getViewers()) {
if (!canSpawnFor(viewer, entity)) {
entity.removeViewer(viewer);
}
}
});
}
public Executor getExecutor() {
return executor;
}
public void setExecutor(Executor executor) {
this.executor = executor;
}
/**
* Tells this ViewerEngine to begin tracking a specific {@link WrapperEntity}
* @param entity the entity to begin tracking.
*/
public void track(@NotNull WrapperEntity entity) {
tracked.add(entity);
}
/**
* Tells this ViewerEngine to stop tracking a specific {@link WrapperEntity}
* @param entity the entity to stop tracking.
*/
public void untrack(@NotNull WrapperEntity entity) {
tracked.remove(entity);
}
public void clearTracked() {
tracked.clear();
}
/**
* Checks if a viewer/user validates every viewer rule handled by this viewer engine or not.
* @param user The user to check
* @param entity The entity that is getting its own viewer rules checked as well as the global defined one with {@link ViewerEngine#addViewerRule(ViewerRule)}
* @return true if the user passed and did not fail any rules, false otherwise
*/
public boolean canSpawnFor(User user, WrapperEntity entity) {
if (!entity.getViewerRules().isEmpty() && entity.getViewerRules().stream().allMatch(rule -> rule.shouldSee(user))) return true;
return globalRules.stream().allMatch(rule -> rule.shouldSee(user));
}
/**
* Same as {@link ViewerEngine#canSpawnFor(User, WrapperEntity)} but with UUID instead of User
*/
public boolean canSpawnFor(UUID userId, WrapperEntity entity) {
User user = EntityLib.getApi().getPacketEvents().getProtocolManager().getUser(
EntityLib.getApi().getPacketEvents().getProtocolManager().getChannel(userId)
);
if (user == null) return false;
return canSpawnFor(user, entity);
}
/**
* @return An unmodifiable view of the entities that are being tracked.
*/
public @UnmodifiableView Collection<WrapperEntity> getTracked() {
return Collections.unmodifiableCollection(tracked);
}
Set<WrapperEntity> getTracked0() {
return tracked;
}
public void addViewerRule(@NotNull ViewerRule rule) {
this.globalRules.add(rule);
}
public void removeViewerRule(@NotNull ViewerRule rule) {
this.globalRules.remove(rule);
}
public void removeViewerRule(int index) {
this.globalRules.remove(index);
}
public void clearViewerRules() {
this.globalRules.clear();
}
public @UnmodifiableView Collection<ViewerRule> getViewerRules() {
return Collections.unmodifiableCollection(globalRules);
}
public @Nullable ViewerRule getViewerRule(int index) {
if (this.globalRules.size() >= index - 1) return null;
if (index < 0) return null;
return globalRules.get(index);
}
}