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,34 +2,36 @@ package net.minestom.server.event.instance;
import net.minestom.server.event.Event;
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 {
private final int duration;
private final Instance instance;
private final int duration;
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.instance = someInstance;
this.instance = instance;
}
/**
* Gets the duration of the tick in ms
* Gets the duration of the tick in ms.
*
* @return the duration
*/
public int getDuration() {
return duration;
}
/**
* Gets the instance of the event
* Gets the instance of the event.
*
* @return the instance
*/
@NotNull
public Instance getInstance() {
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.handler.EventHandler;
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.instance.batch.BlockBatch;
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 long lastTimeUpdate;
// Field for tick events
private long lastTickAge = System.currentTimeMillis();
private final Map<Class<? extends Event>, Collection<EventCallback>> eventCallbacks = new ConcurrentHashMap<>();
// Entities present in this instance
@ -998,7 +1002,7 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
* @param time the tick time in milliseconds
*/
public void tick(long time) {
// scheduled tasks
// Scheduled tasks
if (!nextTick.isEmpty()) {
Consumer<Instance> callback;
while ((callback = nextTick.poll()) != null) {
@ -1006,8 +1010,8 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
}
}
// Time
{
// time
this.worldAge++;
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();
}

View File

@ -9,7 +9,6 @@ import net.minestom.server.data.SerializableData;
import net.minestom.server.entity.Player;
import net.minestom.server.event.instance.InstanceChunkLoadEvent;
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.instance.batch.BlockBatch;
import net.minestom.server.instance.batch.ChunkBatch;
@ -65,9 +64,6 @@ public class InstanceContainer extends Instance {
private final ReadWriteLock changingBlockLock = new ReentrantReadWriteLock();
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
private IChunkLoader chunkLoader;
@ -792,18 +788,11 @@ public class InstanceContainer extends Instance {
// Time/world border
super.tick(time);
// Process tick events
InstanceTickEvent chunkTickEvent = new InstanceTickEvent(time, lastTickAge, this);
callEvent(InstanceTickEvent.class, chunkTickEvent);
Lock wrlock = changingBlockLock.writeLock();
wrlock.lock();
currentlyChangingBlocks.clear();
wrlock.unlock();
// Set last tick age
lastTickAge = time;
}
/**