Merge remote-tracking branch 'origin/master'

This commit is contained in:
Felix Cravic 2020-12-07 23:58:34 +01:00
commit 0de69ae2ad
2 changed files with 54 additions and 1 deletions

View File

@ -0,0 +1,42 @@
package net.minestom.server.event.instance;
import javax.annotation.Nullable;
import org.jetbrains.annotations.NotNull;
import net.minestom.server.event.Event;
import net.minestom.server.instance.InstanceContainer;
/**
* Called when an instance processes a tick
*/
public class InstanceTickEvent extends Event {
private final int duration;
private final InstanceContainer instance;
public InstanceTickEvent(@NotNull long time, @Nullable long lastTickAge, InstanceContainer someInstance) {
this.duration = (int) (time - lastTickAge);
this.instance = someInstance;
}
/**
* Gets the duration of the tick in ms
*
* @return the duration
*/
@NotNull
public int getDuration() {
return duration;
}
/**
* Gets the instance of the event
*
* @return the instance
*/
@NotNull
public InstanceContainer getInstance() {
return instance;
}
}

View File

@ -9,6 +9,7 @@ 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;
@ -64,6 +65,9 @@ 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;
@ -788,11 +792,18 @@ 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;
}
/**