Make Instance implements EventHandler

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2022-04-17 06:19:14 +02:00
parent 0f767da5f0
commit 8b187894f7
2 changed files with 28 additions and 7 deletions

View File

@ -82,7 +82,8 @@ import java.util.function.UnaryOperator;
* <p>
* To create your own entity you probably want to extends {@link LivingEntity} or {@link EntityCreature} instead.
*/
public class Entity implements Viewable, Tickable, Schedulable, Snapshotable, EventHandler<EntityEvent>, Taggable, PermissionHandler, HoverEventSource<ShowEntity>, Sound.Emitter {
public class Entity implements Viewable, Tickable, Schedulable, Snapshotable, EventHandler<EntityEvent>, Taggable,
PermissionHandler, HoverEventSource<ShowEntity>, Sound.Emitter {
private static final Int2ObjectSyncMap<Entity> ENTITY_BY_ID = Int2ObjectSyncMap.hashmap();
private static final Map<UUID, Entity> ENTITY_BY_UUID = new ConcurrentHashMap<>();
@ -1618,7 +1619,7 @@ public class Entity implements Viewable, Tickable, Schedulable, Snapshotable, Ev
/**
* Raycasts current entity's eye position to target eye position.
*
* @param entity the entity to be checked.
* @param entity the entity to be checked.
* @param exactView if set to TRUE, checks whether target is IN the line of sight of the current one;
* otherwise checks if the current entity can rotate so that target will be in its line of sight.
* @return true if the ray reaches the target bounding box before hitting a block.
@ -1639,9 +1640,9 @@ public class Entity implements Viewable, Tickable, Schedulable, Snapshotable, Ev
}
/**
* @see Entity#hasLineOfSight(Entity, boolean)
* @param entity the entity to be checked.
* @return if the current entity has line of sight to the given one.
* @see Entity#hasLineOfSight(Entity, boolean)
*/
public boolean hasLineOfSight(Entity entity) {
return hasLineOfSight(entity, false);
@ -1650,7 +1651,7 @@ public class Entity implements Viewable, Tickable, Schedulable, Snapshotable, Ev
/**
* Gets first entity on the line of sight of the current one that matches the given predicate.
*
* @param range max length of the line of sight of the current entity to be checked.
* @param range max length of the line of sight of the current entity to be checked.
* @param predicate optional predicate
* @return resulting entity whether there're any, null otherwise.
*/
@ -1666,7 +1667,7 @@ public class Entity implements Viewable, Tickable, Schedulable, Snapshotable, Ev
&& e.boundingBox.boundingBoxRayIntersectionCheck(startAsVec, position.direction(), e.getPosition())
&& predicate.test(e)
&& CollisionUtils.isLineOfSightReachingShape(instance, currentChunk, start,
e.position.withY(e.position.y() + e.getEyeHeight()), e.boundingBox);
e.position.withY(e.position.y() + e.getEyeHeight()), e.boundingBox);
Optional<Entity> nearby = instance.getNearbyEntities(position, range).stream()
.filter(finalPredicate)

View File

@ -4,6 +4,7 @@ import it.unimi.dsi.fastutil.objects.ObjectArraySet;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.pointer.Pointers;
import net.minestom.server.MinecraftServer;
import net.minestom.server.ServerProcess;
import net.minestom.server.Tickable;
import net.minestom.server.adventure.audience.PacketGroupingAudience;
import net.minestom.server.coordinate.Point;
@ -13,7 +14,11 @@ import net.minestom.server.entity.ExperienceOrb;
import net.minestom.server.entity.Player;
import net.minestom.server.entity.pathfinding.PFInstanceSpace;
import net.minestom.server.event.EventDispatcher;
import net.minestom.server.event.EventFilter;
import net.minestom.server.event.EventHandler;
import net.minestom.server.event.EventNode;
import net.minestom.server.event.instance.InstanceTickEvent;
import net.minestom.server.event.trait.InstanceEvent;
import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockHandler;
import net.minestom.server.instance.generator.Generator;
@ -58,7 +63,8 @@ import java.util.stream.Collectors;
* with {@link InstanceManager#registerInstance(Instance)}, and
* you need to be sure to signal the {@link ThreadDispatcher} of every partition/element changes.
*/
public abstract class Instance implements Block.Getter, Block.Setter, Tickable, Schedulable, Snapshotable, Taggable, PacketGroupingAudience {
public abstract class Instance implements Block.Getter, Block.Setter,
Tickable, Schedulable, Snapshotable, EventHandler<InstanceEvent>, Taggable, PacketGroupingAudience {
private boolean registered;
@ -87,8 +93,8 @@ public abstract class Instance implements Block.Getter, Block.Setter, Tickable,
// instance custom data
private final TagHandler tagHandler = TagHandler.newHandler();
private final Scheduler scheduler = Scheduler.newScheduler();
private final EventNode<InstanceEvent> eventNode;
// the explosion supplier
private ExplosionSupplier explosionSupplier;
@ -116,6 +122,14 @@ public abstract class Instance implements Block.Getter, Block.Setter, Tickable,
this.pointers = Pointers.builder()
.withDynamic(Identity.UUID, this::getUniqueId)
.build();
final ServerProcess process = MinecraftServer.process();
if (process != null) {
this.eventNode = process.eventHandler().map(this, EventFilter.INSTANCE);
} else {
// Local nodes require a server process
this.eventNode = null;
}
}
/**
@ -617,6 +631,12 @@ public abstract class Instance implements Block.Getter, Block.Setter, Tickable,
return scheduler;
}
@Override
@ApiStatus.Experimental
public @NotNull EventNode<InstanceEvent> eventNode() {
return eventNode;
}
@Override
public @NotNull InstanceSnapshot updateSnapshot(@NotNull SnapshotUpdater updater) {
final Map<Long, AtomicReference<ChunkSnapshot>> chunksMap = updater.referencesMapLong(getChunks(), ChunkUtils::getChunkIndex);