mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-08 17:37:42 +01:00
Merge branches 'experimental' and 'master' of https://github.com/Minestom/Minestom
This commit is contained in:
commit
eb9aa8b6b6
@ -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.entity.Entity;
|
||||||
import net.minestom.server.event.CancellableEvent;
|
import net.minestom.server.event.CancellableEvent;
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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.entity.Entity;
|
||||||
import net.minestom.server.event.CancellableEvent;
|
import net.minestom.server.event.CancellableEvent;
|
@ -7,9 +7,9 @@ import net.minestom.server.data.DataContainer;
|
|||||||
import net.minestom.server.entity.*;
|
import net.minestom.server.entity.*;
|
||||||
import net.minestom.server.event.Event;
|
import net.minestom.server.event.Event;
|
||||||
import net.minestom.server.event.EventCallback;
|
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.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.BlockBatch;
|
||||||
import net.minestom.server.instance.batch.ChunkBatch;
|
import net.minestom.server.instance.batch.ChunkBatch;
|
||||||
import net.minestom.server.instance.block.Block;
|
import net.minestom.server.instance.block.Block;
|
||||||
|
@ -5,6 +5,8 @@ import net.minestom.server.MinecraftServer;
|
|||||||
import net.minestom.server.data.Data;
|
import net.minestom.server.data.Data;
|
||||||
import net.minestom.server.data.SerializableData;
|
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.InstanceChunkUnloadEvent;
|
||||||
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;
|
||||||
@ -277,9 +279,11 @@ public class InstanceContainer extends Instance {
|
|||||||
public void loadChunk(int chunkX, int chunkZ, Consumer<Chunk> callback) {
|
public void loadChunk(int chunkX, int chunkZ, Consumer<Chunk> callback) {
|
||||||
Chunk chunk = getChunk(chunkX, chunkZ);
|
Chunk chunk = getChunk(chunkX, chunkZ);
|
||||||
if (chunk != null) {
|
if (chunk != null) {
|
||||||
|
// Chunk already loaded
|
||||||
if (callback != null)
|
if (callback != null)
|
||||||
callback.accept(chunk);
|
callback.accept(chunk);
|
||||||
} else {
|
} else {
|
||||||
|
// Retrieve chunk from somewhere else (file or create a new use using the ChunkGenerator)
|
||||||
retrieveChunk(chunkX, chunkZ, callback);
|
retrieveChunk(chunkX, chunkZ, callback);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -319,6 +323,8 @@ public class InstanceContainer extends Instance {
|
|||||||
chunk.removeViewer(viewer);
|
chunk.removeViewer(viewer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
callChunkUnloadEvent(chunkX, chunkZ);
|
||||||
|
|
||||||
// Remove all entities in chunk
|
// Remove all entities in chunk
|
||||||
getChunkEntities(chunk).forEach(entity -> {
|
getChunkEntities(chunk).forEach(entity -> {
|
||||||
if (!(entity instanceof Player))
|
if (!(entity instanceof Player))
|
||||||
@ -391,11 +397,12 @@ public class InstanceContainer extends Instance {
|
|||||||
protected void retrieveChunk(int chunkX, int chunkZ, Consumer<Chunk> callback) {
|
protected void retrieveChunk(int chunkX, int chunkZ, Consumer<Chunk> callback) {
|
||||||
boolean loaded = chunkLoader.loadChunk(this, chunkX, chunkZ, chunk -> {
|
boolean loaded = chunkLoader.loadChunk(this, chunkX, chunkZ, chunk -> {
|
||||||
cacheChunk(chunk);
|
cacheChunk(chunk);
|
||||||
|
callChunkLoadEvent(chunkX, chunkZ);
|
||||||
if (callback != null)
|
if (callback != null)
|
||||||
callback.accept(chunk);
|
callback.accept(chunk);
|
||||||
});
|
});
|
||||||
|
|
||||||
if(!loaded) {
|
if (!loaded) {
|
||||||
// Not found, create a new chunk
|
// Not found, create a new chunk
|
||||||
createChunk(chunkX, chunkZ, callback);
|
createChunk(chunkX, chunkZ, callback);
|
||||||
}
|
}
|
||||||
@ -417,6 +424,8 @@ public class InstanceContainer extends Instance {
|
|||||||
|
|
||||||
chunkBatch.flushChunkGenerator(chunkGenerator, callback);
|
chunkBatch.flushChunkGenerator(chunkGenerator, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
callChunkLoadEvent(chunkX, chunkZ);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendChunkUpdate(Chunk chunk) {
|
public void sendChunkUpdate(Chunk chunk) {
|
||||||
@ -567,4 +576,14 @@ public class InstanceContainer extends Instance {
|
|||||||
currentlyChangingBlocks.clear();
|
currentlyChangingBlocks.clear();
|
||||||
wrlock.unlock();
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user