Fix first tick call

This commit is contained in:
KrystilizeNevaDies 2020-12-08 07:52:46 +10:00
parent 6cc8946963
commit e8e30b1328
2 changed files with 20 additions and 7 deletions

View File

@ -5,25 +5,38 @@ 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 int duration;
private final InstanceContainer instance;
public InstanceTickEvent(@NotNull long time, @Nullable long lastTickAge) {
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 instance
* @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

@ -65,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;
@ -78,9 +81,6 @@ public class InstanceContainer extends Instance {
// Fields for instance copy
protected InstanceContainer srcInstance; // only present if this instance has been created using a copy
private long lastBlockChangeTime; // Time at which the last block change happened (#setBlock)
// Fields for tick events
private long lastTickAge;
/**
* Creates an {@link InstanceContainer}.
@ -794,7 +794,7 @@ public class InstanceContainer extends Instance {
super.tick(time);
// Process tick events
InstanceTickEvent chunkTickEvent = new InstanceTickEvent(time, lastTickAge);
InstanceTickEvent chunkTickEvent = new InstanceTickEvent(time, lastTickAge, this);
callEvent(InstanceTickEvent.class, chunkTickEvent);
Lock wrlock = changingBlockLock.writeLock();