Skip to content

Commit 6fba8ea

Browse files
committed
fix: ViewerEngine#untrack impl
1 parent 244145e commit 6fba8ea

File tree

1 file changed

+60
-1
lines changed

1 file changed

+60
-1
lines changed

api/src/main/java/me/tofaa/entitylib/ve/ViewerEngine.java

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,56 @@ public class ViewerEngine {
2525
private final Set<WrapperEntity> tracked;
2626
private final ViewerEngineListener listener;
2727
private Executor executor;
28+
private boolean enabled = false;
2829

30+
/**
31+
* Creates an instance of ViewerEngine
32+
* It is recommended to specify explicitly the Executor that should be used.
33+
*/
2934
public ViewerEngine() {
35+
this(Executors.newSingleThreadExecutor());
36+
}
37+
38+
/**
39+
* Creates an instance of ViewerEngine with a specific executor. Depending on your rules one thread might not be enough
40+
* @param executor The executor that is used to process entity tracking.
41+
*/
42+
public ViewerEngine(Executor executor) {
3043
this.globalRules = new CopyOnWriteArrayList<>();
3144
this.tracked = Collections.newSetFromMap(new WeakHashMap<>());
32-
this.executor = Executors.newSingleThreadExecutor();
45+
this.executor = executor;
3346
this.listener = new ViewerEngineListener(this);
3447
}
3548

49+
/**
50+
* Enables this viewer engine.
51+
* Registers a viewer engine listener to handle tracking
52+
*/
3653
public void enable() {
54+
if (enabled) {
55+
return;
56+
}
57+
enabled = true;
3758
EntityLib.getApi().getPacketEvents().getEventManager().registerListener(listener);
3859
}
60+
61+
62+
/**
63+
* Disables this viewer engine.
64+
* Unregisters the viewer engine listener that handles tracking.
65+
*/
3966
public void disable() {
67+
if (!enabled) {
68+
return;
69+
}
70+
enabled = false;
4071
EntityLib.getApi().getPacketEvents().getEventManager().unregisterListener(listener);
4172
}
4273

74+
/**
75+
* Refreshes and updates every tracked by this viewer engine entities viewers to see if they follow the spawning rules.
76+
* If they do not they will no longer see the entity;
77+
*/
4378
public void refresh() {
4479
getTracked0().forEach(entity -> {
4580
for (UUID viewer : entity.getViewers()) {
@@ -58,19 +93,40 @@ public void setExecutor(Executor executor) {
5893
this.executor = executor;
5994
}
6095

96+
/**
97+
* Tells this ViewerEngine to begin tracking a specific {@link WrapperEntity}
98+
* @param entity the entity to begin tracking.
99+
*/
61100
public void track(@NotNull WrapperEntity entity) {
62101
tracked.add(entity);
63102
}
64103

104+
/**
105+
* Tells this ViewerEngine to stop tracking a specific {@link WrapperEntity}
106+
* @param entity the entity to stop tracking.
107+
*/
108+
public void untrack(@NotNull WrapperEntity entity) {
109+
tracked.remove(entity);
110+
}
111+
65112
public void clearTracked() {
66113
tracked.clear();
67114
}
68115

116+
/**
117+
* Checks if a viewer/user validates every viewer rule handled by this viewer engine or not.
118+
* @param user The user to check
119+
* @param entity The entity that is getting its own viewer rules checked as well as the global defined one with {@link ViewerEngine#addViewerRule(ViewerRule)}
120+
* @return true if the user passed and did not fail any rules, false otherwise
121+
*/
69122
public boolean canSpawnFor(User user, WrapperEntity entity) {
70123
if (entity.getViewerRules().stream().anyMatch(rule -> rule.shouldSee(user))) return true;
71124
return globalRules.stream().anyMatch(rule -> rule.shouldSee(user));
72125
}
73126

127+
/**
128+
* Same as {@link ViewerEngine#canSpawnFor(User, WrapperEntity)} but with UUID instead of User
129+
*/
74130
public boolean canSpawnFor(UUID userId, WrapperEntity entity) {
75131
User user = EntityLib.getApi().getPacketEvents().getProtocolManager().getUser(
76132
EntityLib.getApi().getPacketEvents().getProtocolManager().getChannel(userId)
@@ -79,6 +135,9 @@ public boolean canSpawnFor(UUID userId, WrapperEntity entity) {
79135
return canSpawnFor(user, entity);
80136
}
81137

138+
/**
139+
* @return An unmodifiable view of the entities that are being tracked.
140+
*/
82141
public @UnmodifiableView Collection<WrapperEntity> getTracked() {
83142
return Collections.unmodifiableCollection(tracked);
84143
}

0 commit comments

Comments
 (0)