Merge branches 'experimental' and 'master' of https://github.com/Minestom/Minestom

This commit is contained in:
Eoghanmc22 2020-07-01 10:55:44 -04:00
commit eb9aa8b6b6
6 changed files with 117 additions and 5 deletions

View File

@ -1,4 +1,4 @@
package net.minestom.server.event.entity;
package net.minestom.server.event.instance;
import net.minestom.server.entity.Entity;
import net.minestom.server.event.CancellableEvent;

View File

@ -0,0 +1,46 @@
package net.minestom.server.event.instance;
import net.minestom.server.event.Event;
import net.minestom.server.instance.Instance;
/**
* Called when a chunk in an instance is loaded
*/
public class InstanceChunkLoadEvent extends Event {
private Instance instance;
private int chunkX, chunkZ;
public InstanceChunkLoadEvent(Instance instance, int chunkX, int chunkZ) {
this.instance = instance;
this.chunkX = chunkX;
this.chunkZ = chunkZ;
}
/**
* Get the instance where the chunk has been loaded
*
* @return the instance
*/
public Instance getInstance() {
return instance;
}
/**
* Get the chunk X
*
* @return the chunk X
*/
public int getChunkX() {
return chunkX;
}
/**
* Get the chunk Z
*
* @return the chunk Z
*/
public int getChunkZ() {
return chunkZ;
}
}

View File

@ -0,0 +1,47 @@
package net.minestom.server.event.instance;
import net.minestom.server.event.Event;
import net.minestom.server.instance.Instance;
/**
* Called when a chunk in an instance is unloaded
*/
public class InstanceChunkUnloadEvent extends Event {
private Instance instance;
private int chunkX, chunkZ;
public InstanceChunkUnloadEvent(Instance instance, int chunkX, int chunkZ) {
this.instance = instance;
this.chunkX = chunkX;
this.chunkZ = chunkZ;
}
/**
* Get the instance where the chunk has been unloaded
*
* @return the instance
*/
public Instance getInstance() {
return instance;
}
/**
* Get the chunk X
*
* @return the chunk X
*/
public int getChunkX() {
return chunkX;
}
/**
* Get the chunk Z
*
* @return the chunk Z
*/
public int getChunkZ() {
return chunkZ;
}
}

View File

@ -1,4 +1,4 @@
package net.minestom.server.event.entity;
package net.minestom.server.event.instance;
import net.minestom.server.entity.Entity;
import net.minestom.server.event.CancellableEvent;

View File

@ -7,9 +7,9 @@ import net.minestom.server.data.DataContainer;
import net.minestom.server.entity.*;
import net.minestom.server.event.Event;
import net.minestom.server.event.EventCallback;
import net.minestom.server.event.entity.AddEntityToInstanceEvent;
import net.minestom.server.event.entity.RemoveEntityFromInstanceEvent;
import net.minestom.server.event.handler.EventHandler;
import net.minestom.server.event.instance.AddEntityToInstanceEvent;
import net.minestom.server.event.instance.RemoveEntityFromInstanceEvent;
import net.minestom.server.instance.batch.BlockBatch;
import net.minestom.server.instance.batch.ChunkBatch;
import net.minestom.server.instance.block.Block;

View File

@ -5,6 +5,8 @@ import net.minestom.server.MinecraftServer;
import net.minestom.server.data.Data;
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.player.PlayerBlockBreakEvent;
import net.minestom.server.instance.batch.BlockBatch;
import net.minestom.server.instance.batch.ChunkBatch;
@ -277,9 +279,11 @@ public class InstanceContainer extends Instance {
public void loadChunk(int chunkX, int chunkZ, Consumer<Chunk> callback) {
Chunk chunk = getChunk(chunkX, chunkZ);
if (chunk != null) {
// Chunk already loaded
if (callback != null)
callback.accept(chunk);
} else {
// Retrieve chunk from somewhere else (file or create a new use using the ChunkGenerator)
retrieveChunk(chunkX, chunkZ, callback);
}
}
@ -319,6 +323,8 @@ public class InstanceContainer extends Instance {
chunk.removeViewer(viewer);
}
callChunkUnloadEvent(chunkX, chunkZ);
// Remove all entities in chunk
getChunkEntities(chunk).forEach(entity -> {
if (!(entity instanceof Player))
@ -391,11 +397,12 @@ public class InstanceContainer extends Instance {
protected void retrieveChunk(int chunkX, int chunkZ, Consumer<Chunk> callback) {
boolean loaded = chunkLoader.loadChunk(this, chunkX, chunkZ, chunk -> {
cacheChunk(chunk);
callChunkLoadEvent(chunkX, chunkZ);
if (callback != null)
callback.accept(chunk);
});
if(!loaded) {
if (!loaded) {
// Not found, create a new chunk
createChunk(chunkX, chunkZ, callback);
}
@ -417,6 +424,8 @@ public class InstanceContainer extends Instance {
chunkBatch.flushChunkGenerator(chunkGenerator, callback);
}
callChunkLoadEvent(chunkX, chunkZ);
}
public void sendChunkUpdate(Chunk chunk) {
@ -567,4 +576,14 @@ public class InstanceContainer extends Instance {
currentlyChangingBlocks.clear();
wrlock.unlock();
}
private void callChunkLoadEvent(int chunkX, int chunkZ) {
InstanceChunkLoadEvent chunkLoadEvent = new InstanceChunkLoadEvent(this, chunkX, chunkZ);
callEvent(InstanceChunkLoadEvent.class, chunkLoadEvent);
}
private void callChunkUnloadEvent(int chunkX, int chunkZ) {
InstanceChunkUnloadEvent chunkUnloadEvent = new InstanceChunkUnloadEvent(this, chunkX, chunkZ);
callEvent(InstanceChunkUnloadEvent.class, chunkUnloadEvent);
}
}