2021-11-01 18:04:00 +01:00
|
|
|
package net.minestom.server.utils;
|
|
|
|
|
2021-12-22 04:47:48 +01:00
|
|
|
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
|
|
|
|
import it.unimi.dsi.fastutil.ints.IntSet;
|
2022-01-26 18:37:21 +01:00
|
|
|
import net.minestom.server.MinecraftServer;
|
2021-11-01 18:04:00 +01:00
|
|
|
import net.minestom.server.coordinate.Point;
|
|
|
|
import net.minestom.server.entity.Entity;
|
|
|
|
import net.minestom.server.entity.Player;
|
|
|
|
import net.minestom.server.instance.EntityTracker;
|
2022-02-08 05:28:19 +01:00
|
|
|
import net.minestom.server.instance.Instance;
|
|
|
|
import net.minestom.server.instance.InstanceContainer;
|
2022-02-13 07:51:47 +01:00
|
|
|
import net.minestom.server.instance.SharedInstance;
|
2021-11-01 18:04:00 +01:00
|
|
|
import org.jetbrains.annotations.ApiStatus;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
|
2021-12-09 17:26:27 +01:00
|
|
|
import java.util.*;
|
2021-11-01 18:04:00 +01:00
|
|
|
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
import java.util.function.Predicate;
|
2022-02-08 05:28:19 +01:00
|
|
|
import java.util.stream.Stream;
|
2021-11-01 18:04:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines which players are able to see this element.
|
|
|
|
*/
|
|
|
|
@ApiStatus.Internal
|
|
|
|
public final class ViewEngine {
|
|
|
|
private final Entity entity;
|
2022-02-08 05:28:19 +01:00
|
|
|
private final int range;
|
|
|
|
private final Set<Player> manualViewers = new HashSet<>();
|
2021-11-01 18:04:00 +01:00
|
|
|
|
2022-02-08 05:28:19 +01:00
|
|
|
private Instance instance;
|
|
|
|
private Point lastPoint;
|
2021-11-01 18:04:00 +01:00
|
|
|
|
|
|
|
// Decide if this entity should be viewable to X players
|
|
|
|
public final Option<Player> viewableOption;
|
|
|
|
// Decide if this entity should view X entities
|
|
|
|
public final Option<Entity> viewerOption;
|
|
|
|
|
2022-02-13 17:34:09 +01:00
|
|
|
private final Set<Player> set;
|
2021-11-01 18:04:00 +01:00
|
|
|
private final Object mutex = this;
|
|
|
|
|
|
|
|
public ViewEngine(@Nullable Entity entity,
|
|
|
|
Consumer<Player> autoViewableAddition, Consumer<Player> autoViewableRemoval,
|
|
|
|
Consumer<Entity> autoViewerAddition, Consumer<Entity> autoViewerRemoval) {
|
|
|
|
this.entity = entity;
|
2022-02-13 17:34:09 +01:00
|
|
|
if (entity != null) {
|
|
|
|
this.range = MinecraftServer.getEntityViewDistance();
|
|
|
|
this.set = new EntitySet();
|
|
|
|
} else {
|
|
|
|
this.range = MinecraftServer.getChunkViewDistance();
|
|
|
|
this.set = new ChunkSet();
|
|
|
|
}
|
2022-02-08 05:28:19 +01:00
|
|
|
this.viewableOption = new Option<>(EntityTracker.Target.PLAYERS, Entity::autoViewEntities, autoViewableAddition, autoViewableRemoval);
|
|
|
|
this.viewerOption = new Option<>(EntityTracker.Target.ENTITIES, Entity::isAutoViewable, autoViewerAddition, autoViewerRemoval);
|
2021-11-01 18:04:00 +01:00
|
|
|
}
|
|
|
|
|
2022-01-12 10:47:38 +01:00
|
|
|
public ViewEngine(@Nullable Entity entity) {
|
|
|
|
this(entity, null, null, null, null);
|
|
|
|
}
|
|
|
|
|
2021-11-01 18:04:00 +01:00
|
|
|
public ViewEngine() {
|
2022-01-12 10:47:38 +01:00
|
|
|
this(null);
|
2021-11-01 18:04:00 +01:00
|
|
|
}
|
|
|
|
|
2022-02-13 07:51:47 +01:00
|
|
|
public void updateTracker(@Nullable Instance instance, @NotNull Point point) {
|
2021-11-01 18:04:00 +01:00
|
|
|
synchronized (mutex) {
|
2022-02-08 05:28:19 +01:00
|
|
|
this.instance = instance;
|
|
|
|
this.lastPoint = point;
|
2021-11-01 18:04:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean manualAdd(@NotNull Player player) {
|
|
|
|
if (player == this.entity) return false;
|
|
|
|
synchronized (mutex) {
|
2022-02-13 17:34:09 +01:00
|
|
|
if (manualViewers.add(player)) {
|
|
|
|
viewableOption.bitSet.add(player.getEntityId());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2021-11-01 18:04:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean manualRemove(@NotNull Player player) {
|
|
|
|
if (player == this.entity) return false;
|
|
|
|
synchronized (mutex) {
|
2022-02-13 17:34:09 +01:00
|
|
|
if (manualViewers.remove(player)) {
|
|
|
|
viewableOption.bitSet.remove(player.getEntityId());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2021-11-01 18:04:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-25 21:30:58 +01:00
|
|
|
public void forManuals(@NotNull Consumer<Player> consumer) {
|
|
|
|
synchronized (mutex) {
|
|
|
|
this.manualViewers.forEach(consumer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-01 18:04:00 +01:00
|
|
|
public boolean hasPredictableViewers() {
|
|
|
|
// Verify if this entity's viewers can be predicted from surrounding entities
|
|
|
|
synchronized (mutex) {
|
|
|
|
return viewableOption.isAuto() && manualViewers.isEmpty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void handleAutoViewAddition(Entity entity) {
|
|
|
|
handleAutoView(entity, viewerOption.addition, viewableOption.addition);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void handleAutoViewRemoval(Entity entity) {
|
|
|
|
handleAutoView(entity, viewerOption.removal, viewableOption.removal);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleAutoView(Entity entity, Consumer<Entity> viewer, Consumer<Player> viewable) {
|
|
|
|
if (entity.getVehicle() != null)
|
|
|
|
return; // Passengers are handled by the vehicle, inheriting its viewing settings
|
|
|
|
if (this.entity instanceof Player && viewerOption.isAuto() && entity.isAutoViewable()) {
|
2022-01-12 10:47:38 +01:00
|
|
|
if (viewer != null) viewer.accept(entity); // Send packet to this player
|
2021-11-01 18:04:00 +01:00
|
|
|
}
|
|
|
|
if (entity instanceof Player player && player.autoViewEntities() && viewableOption.isAuto()) {
|
2022-01-12 10:47:38 +01:00
|
|
|
if (viewable != null) viewable.accept(player); // Send packet to the range-visible player
|
2021-11-01 18:04:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-23 23:44:22 +01:00
|
|
|
public Object mutex() {
|
|
|
|
return mutex;
|
|
|
|
}
|
|
|
|
|
2021-11-01 18:04:00 +01:00
|
|
|
public Set<Player> asSet() {
|
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
|
|
|
public final class Option<T extends Entity> {
|
|
|
|
@SuppressWarnings("rawtypes")
|
|
|
|
private static final AtomicIntegerFieldUpdater<Option> UPDATER = AtomicIntegerFieldUpdater.newUpdater(Option.class, "auto");
|
2022-02-08 05:28:19 +01:00
|
|
|
// Entities that should be tracked from this option
|
|
|
|
private final EntityTracker.Target<T> target;
|
2021-11-01 18:04:00 +01:00
|
|
|
// The condition that must be met for this option to be considered auto.
|
|
|
|
private final Predicate<T> loopPredicate;
|
|
|
|
// The consumers to be called when an entity is added/removed.
|
|
|
|
public final Consumer<T> addition, removal;
|
2021-12-16 14:47:20 +01:00
|
|
|
// Contains all the auto-entity ids that are viewable by this option.
|
2021-12-22 04:47:48 +01:00
|
|
|
public final IntSet bitSet = new IntOpenHashSet();
|
2021-11-01 18:04:00 +01:00
|
|
|
// 1 if auto, 0 if manual
|
|
|
|
private volatile int auto = 1;
|
|
|
|
// The custom rule used to determine if an entity is viewable.
|
|
|
|
private Predicate<T> predicate = entity -> true;
|
|
|
|
|
2022-02-08 05:28:19 +01:00
|
|
|
public Option(EntityTracker.Target<T> target, Predicate<T> loopPredicate,
|
2021-11-01 18:04:00 +01:00
|
|
|
Consumer<T> addition, Consumer<T> removal) {
|
2022-02-08 05:28:19 +01:00
|
|
|
this.target = target;
|
2021-11-01 18:04:00 +01:00
|
|
|
this.loopPredicate = loopPredicate;
|
|
|
|
this.addition = addition;
|
|
|
|
this.removal = removal;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isAuto() {
|
|
|
|
return auto == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean predicate(T entity) {
|
|
|
|
return predicate.test(entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isRegistered(T entity) {
|
2021-12-13 12:54:27 +01:00
|
|
|
return bitSet.contains(entity.getEntityId());
|
2021-11-01 18:04:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void register(T entity) {
|
2021-12-13 12:54:27 +01:00
|
|
|
this.bitSet.add(entity.getEntityId());
|
2021-11-01 18:04:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void unregister(T entity) {
|
2021-12-13 12:54:27 +01:00
|
|
|
this.bitSet.remove(entity.getEntityId());
|
2021-11-01 18:04:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void updateAuto(boolean autoViewable) {
|
|
|
|
final boolean previous = UPDATER.getAndSet(this, autoViewable ? 1 : 0) == 1;
|
|
|
|
if (previous != autoViewable) {
|
|
|
|
synchronized (mutex) {
|
2022-02-08 05:28:19 +01:00
|
|
|
if (autoViewable) update(loopPredicate, addition);
|
|
|
|
else update(this::isRegistered, removal);
|
2021-11-01 18:04:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateRule(Predicate<T> predicate) {
|
|
|
|
synchronized (mutex) {
|
|
|
|
this.predicate = predicate;
|
|
|
|
updateRule();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateRule() {
|
|
|
|
synchronized (mutex) {
|
2022-02-08 05:28:19 +01:00
|
|
|
update(loopPredicate, entity -> {
|
2021-11-01 18:04:00 +01:00
|
|
|
final boolean result = predicate.test(entity);
|
|
|
|
if (result != isRegistered(entity)) {
|
|
|
|
if (result) addition.accept(entity);
|
|
|
|
else removal.accept(entity);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-08 05:28:19 +01:00
|
|
|
private void update(Predicate<T> visibilityPredicate,
|
2021-11-01 18:04:00 +01:00
|
|
|
Consumer<T> action) {
|
2022-02-08 05:28:19 +01:00
|
|
|
references().forEach(entity -> {
|
|
|
|
if (entity == ViewEngine.this.entity || !visibilityPredicate.test(entity)) return;
|
|
|
|
if (entity instanceof Player player && manualViewers.contains(player)) return;
|
|
|
|
if (entity.getVehicle() != null) return;
|
|
|
|
action.accept(entity);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private Stream<T> references() {
|
2022-02-13 07:51:47 +01:00
|
|
|
final Instance instance = ViewEngine.this.instance;
|
|
|
|
if (instance == null) return Stream.empty();
|
|
|
|
var references = instance.getEntityTracker().references(lastPoint, range, target);
|
|
|
|
Stream<T> result = references.stream().flatMap(Collection::stream);
|
|
|
|
if (instance instanceof InstanceContainer container) {
|
2022-02-08 05:28:19 +01:00
|
|
|
// References from shared instances must be added to the result.
|
2022-02-13 07:51:47 +01:00
|
|
|
final List<SharedInstance> shared = container.getSharedInstances();
|
2022-02-08 05:28:19 +01:00
|
|
|
if (!shared.isEmpty()) {
|
2022-02-13 07:51:47 +01:00
|
|
|
Stream<T> sharedInstanceStream = shared.stream().<List<T>>mapMulti((inst, consumer) -> {
|
2022-02-08 05:28:19 +01:00
|
|
|
var ref = inst.getEntityTracker().references(lastPoint, range, target);
|
|
|
|
ref.forEach(consumer);
|
2022-02-13 20:35:02 +01:00
|
|
|
}).flatMap(Collection::stream);
|
2022-02-13 07:51:47 +01:00
|
|
|
result = Stream.concat(result, sharedInstanceStream);
|
2021-11-01 18:04:00 +01:00
|
|
|
}
|
2021-12-23 23:44:22 +01:00
|
|
|
}
|
2022-02-13 20:02:06 +01:00
|
|
|
return result;
|
2021-11-01 18:04:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-13 17:34:09 +01:00
|
|
|
final class ChunkSet extends AbstractSet<Player> {
|
|
|
|
@Override
|
|
|
|
public @NotNull Iterator<Player> iterator() {
|
|
|
|
synchronized (mutex) {
|
|
|
|
return viewableOption.references().toList().iterator();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int size() {
|
|
|
|
synchronized (mutex) {
|
|
|
|
return (int) viewableOption.references().count();
|
|
|
|
}
|
|
|
|
}
|
2021-12-09 17:26:27 +01:00
|
|
|
|
2022-02-13 17:34:09 +01:00
|
|
|
@Override
|
|
|
|
public void forEach(Consumer<? super Player> action) {
|
|
|
|
synchronized (mutex) {
|
|
|
|
viewableOption.references().forEach(action);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final class EntitySet extends AbstractSet<Player> {
|
2021-11-01 18:04:00 +01:00
|
|
|
@Override
|
|
|
|
public @NotNull Iterator<Player> iterator() {
|
|
|
|
synchronized (mutex) {
|
2022-02-13 17:34:09 +01:00
|
|
|
return viewableOption.bitSet.intStream()
|
|
|
|
.mapToObj(operand -> (Player) Entity.getEntity(operand))
|
|
|
|
.toList().iterator();
|
2021-11-01 18:04:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int size() {
|
|
|
|
synchronized (mutex) {
|
2022-02-13 17:34:09 +01:00
|
|
|
return viewableOption.bitSet.size();
|
2021-11-01 18:04:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isEmpty() {
|
|
|
|
synchronized (mutex) {
|
2022-02-13 17:34:09 +01:00
|
|
|
return viewableOption.bitSet.isEmpty();
|
2021-11-01 18:04:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean contains(Object o) {
|
|
|
|
if (!(o instanceof Player player)) return false;
|
|
|
|
synchronized (mutex) {
|
2022-02-13 17:34:09 +01:00
|
|
|
return viewableOption.isRegistered(player);
|
2021-11-01 18:04:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void forEach(Consumer<? super Player> action) {
|
|
|
|
synchronized (mutex) {
|
2022-02-13 17:34:09 +01:00
|
|
|
viewableOption.bitSet.forEach((int id) -> action.accept((Player) Entity.getEntity(id)));
|
2021-11-01 18:04:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|