diff --git a/api/src/main/java/me/tofaa/entitylib/ve/ViewerEngine.java b/api/src/main/java/me/tofaa/entitylib/ve/ViewerEngine.java index b1036c7..17147dd 100644 --- a/api/src/main/java/me/tofaa/entitylib/ve/ViewerEngine.java +++ b/api/src/main/java/me/tofaa/entitylib/ve/ViewerEngine.java @@ -25,21 +25,56 @@ public class ViewerEngine { private final Set 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 = Executors.newSingleThreadExecutor(); + 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()) { @@ -58,19 +93,40 @@ public class ViewerEngine { 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().stream().anyMatch(rule -> rule.shouldSee(user))) return true; return globalRules.stream().anyMatch(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) @@ -79,6 +135,9 @@ public class ViewerEngine { return canSpawnFor(user, entity); } + /** + * @return An unmodifiable view of the entities that are being tracked. + */ public @UnmodifiableView Collection getTracked() { return Collections.unmodifiableCollection(tracked); }