Execute InstanceTickEvent in Instance instead of InstanceContainer

This commit is contained in:
Felix Cravic 2020-12-08 00:16:46 +01:00
parent c836bbc051
commit eca6a657c3
3 changed files with 27 additions and 22 deletions

View File

@ -2,22 +2,23 @@ package net.minestom.server.event.instance;
import net.minestom.server.event.Event; import net.minestom.server.event.Event;
import net.minestom.server.instance.Instance; import net.minestom.server.instance.Instance;
import org.jetbrains.annotations.NotNull;
/** /**
* Called when an instance processes a tick * Called when an instance processes a tick.
*/ */
public class InstanceTickEvent extends Event { public class InstanceTickEvent extends Event {
private final int duration; private final int duration;
private final Instance instance; private final Instance instance;
public InstanceTickEvent(long time, long lastTickAge, Instance someInstance) { public InstanceTickEvent(@NotNull Instance instance, long time, long lastTickAge) {
this.duration = (int) (time - lastTickAge); this.duration = (int) (time - lastTickAge);
this.instance = someInstance; this.instance = instance;
} }
/** /**
* Gets the duration of the tick in ms * Gets the duration of the tick in ms.
* *
* @return the duration * @return the duration
*/ */
@ -26,10 +27,11 @@ public class InstanceTickEvent extends Event {
} }
/** /**
* Gets the instance of the event * Gets the instance of the event.
* *
* @return the instance * @return the instance
*/ */
@NotNull
public Instance getInstance() { public Instance getInstance() {
return instance; return instance;
} }

View File

@ -13,6 +13,7 @@ import net.minestom.server.event.Event;
import net.minestom.server.event.EventCallback; import net.minestom.server.event.EventCallback;
import net.minestom.server.event.handler.EventHandler; import net.minestom.server.event.handler.EventHandler;
import net.minestom.server.event.instance.AddEntityToInstanceEvent; import net.minestom.server.event.instance.AddEntityToInstanceEvent;
import net.minestom.server.event.instance.InstanceTickEvent;
import net.minestom.server.event.instance.RemoveEntityFromInstanceEvent; import net.minestom.server.event.instance.RemoveEntityFromInstanceEvent;
import net.minestom.server.instance.batch.BlockBatch; import net.minestom.server.instance.batch.BlockBatch;
import net.minestom.server.instance.batch.ChunkBatch; import net.minestom.server.instance.batch.ChunkBatch;
@ -73,6 +74,9 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
private UpdateOption timeUpdate = new UpdateOption(1, TimeUnit.SECOND); private UpdateOption timeUpdate = new UpdateOption(1, TimeUnit.SECOND);
private long lastTimeUpdate; private long lastTimeUpdate;
// Field for tick events
private long lastTickAge = System.currentTimeMillis();
private final Map<Class<? extends Event>, Collection<EventCallback>> eventCallbacks = new ConcurrentHashMap<>(); private final Map<Class<? extends Event>, Collection<EventCallback>> eventCallbacks = new ConcurrentHashMap<>();
// Entities present in this instance // Entities present in this instance
@ -998,7 +1002,7 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
* @param time the tick time in milliseconds * @param time the tick time in milliseconds
*/ */
public void tick(long time) { public void tick(long time) {
// scheduled tasks // Scheduled tasks
if (!nextTick.isEmpty()) { if (!nextTick.isEmpty()) {
Consumer<Instance> callback; Consumer<Instance> callback;
while ((callback = nextTick.poll()) != null) { while ((callback = nextTick.poll()) != null) {
@ -1006,8 +1010,8 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
} }
} }
// Time
{ {
// time
this.worldAge++; this.worldAge++;
this.time += timeRate; this.time += timeRate;
@ -1020,6 +1024,16 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
} }
// Tick event
{
// Process tick events
InstanceTickEvent chunkTickEvent = new InstanceTickEvent(this, time, lastTickAge);
callEvent(InstanceTickEvent.class, chunkTickEvent);
// Set last tick age
lastTickAge = time;
}
this.worldBorder.update(); this.worldBorder.update();
} }

View File

@ -9,7 +9,6 @@ import net.minestom.server.data.SerializableData;
import net.minestom.server.entity.Player; import net.minestom.server.entity.Player;
import net.minestom.server.event.instance.InstanceChunkLoadEvent; import net.minestom.server.event.instance.InstanceChunkLoadEvent;
import net.minestom.server.event.instance.InstanceChunkUnloadEvent; import net.minestom.server.event.instance.InstanceChunkUnloadEvent;
import net.minestom.server.event.instance.InstanceTickEvent;
import net.minestom.server.event.player.PlayerBlockBreakEvent; import net.minestom.server.event.player.PlayerBlockBreakEvent;
import net.minestom.server.instance.batch.BlockBatch; import net.minestom.server.instance.batch.BlockBatch;
import net.minestom.server.instance.batch.ChunkBatch; import net.minestom.server.instance.batch.ChunkBatch;
@ -66,9 +65,6 @@ public class InstanceContainer extends Instance {
private final ReadWriteLock changingBlockLock = new ReentrantReadWriteLock(); private final ReadWriteLock changingBlockLock = new ReentrantReadWriteLock();
private final Map<BlockPosition, Block> currentlyChangingBlocks = new HashMap<>(); private final Map<BlockPosition, Block> currentlyChangingBlocks = new HashMap<>();
// Fields for tick events
private long lastTickAge = System.currentTimeMillis();
// the chunk loader, used when trying to load/save a chunk from another source // the chunk loader, used when trying to load/save a chunk from another source
private IChunkLoader chunkLoader; private IChunkLoader chunkLoader;
@ -793,17 +789,10 @@ public class InstanceContainer extends Instance {
// Time/world border // Time/world border
super.tick(time); super.tick(time);
// Process tick events
InstanceTickEvent chunkTickEvent = new InstanceTickEvent(time, lastTickAge, this);
callEvent(InstanceTickEvent.class, chunkTickEvent);
Lock wrlock = changingBlockLock.writeLock(); Lock wrlock = changingBlockLock.writeLock();
wrlock.lock(); wrlock.lock();
currentlyChangingBlocks.clear(); currentlyChangingBlocks.clear();
wrlock.unlock(); wrlock.unlock();
// Set last tick age
lastTickAge = time;
} }
/** /**