mirror of
https://github.com/Minestom/Minestom.git
synced 2024-11-17 08:05:25 +01:00
Added OptionalCallback for convenience
This commit is contained in:
parent
40516d17a9
commit
3665241e94
@ -26,6 +26,7 @@ import net.minestom.server.utils.BlockPosition;
|
||||
import net.minestom.server.utils.Position;
|
||||
import net.minestom.server.utils.Vector;
|
||||
import net.minestom.server.utils.binary.BinaryWriter;
|
||||
import net.minestom.server.utils.callback.OptionalCallback;
|
||||
import net.minestom.server.utils.chunk.ChunkUtils;
|
||||
import net.minestom.server.utils.entity.EntityUtils;
|
||||
import net.minestom.server.utils.player.PlayerUtils;
|
||||
@ -226,8 +227,8 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
|
||||
refreshView(position.getYaw(), position.getPitch());
|
||||
}
|
||||
sendSynchronization();
|
||||
if (callback != null)
|
||||
callback.run();
|
||||
|
||||
OptionalCallback.execute(callback);
|
||||
};
|
||||
|
||||
if (instance.hasEnabledAutoChunkLoad()) {
|
||||
|
@ -50,6 +50,7 @@ import net.minestom.server.utils.BlockPosition;
|
||||
import net.minestom.server.utils.MathUtils;
|
||||
import net.minestom.server.utils.Position;
|
||||
import net.minestom.server.utils.binary.BinaryWriter;
|
||||
import net.minestom.server.utils.callback.OptionalCallback;
|
||||
import net.minestom.server.utils.chunk.ChunkCallback;
|
||||
import net.minestom.server.utils.chunk.ChunkUtils;
|
||||
import net.minestom.server.utils.validate.Check;
|
||||
@ -1370,8 +1371,7 @@ public class Player extends LivingEntity implements CommandSender {
|
||||
public void teleport(Position position, Runnable callback) {
|
||||
super.teleport(position, () -> {
|
||||
updatePlayerPosition();
|
||||
if (callback != null)
|
||||
callback.run();
|
||||
OptionalCallback.execute(callback);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -16,6 +16,7 @@ import net.minestom.server.utils.MathUtils;
|
||||
import net.minestom.server.utils.binary.BinaryReader;
|
||||
import net.minestom.server.utils.binary.BinaryWriter;
|
||||
import net.minestom.server.utils.block.CustomBlockUtils;
|
||||
import net.minestom.server.utils.callback.OptionalCallback;
|
||||
import net.minestom.server.utils.chunk.ChunkCallback;
|
||||
import net.minestom.server.utils.chunk.ChunkUtils;
|
||||
import net.minestom.server.utils.time.CooldownUtils;
|
||||
@ -379,9 +380,7 @@ public class DynamicChunk extends Chunk {
|
||||
}
|
||||
|
||||
// Finished reading
|
||||
if (callback != null) {
|
||||
callback.accept(this);
|
||||
}
|
||||
OptionalCallback.execute(callback, this);
|
||||
}
|
||||
}).schedule();
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ import net.minestom.server.storage.StorageLocation;
|
||||
import net.minestom.server.utils.BlockPosition;
|
||||
import net.minestom.server.utils.Position;
|
||||
import net.minestom.server.utils.block.CustomBlockUtils;
|
||||
import net.minestom.server.utils.callback.OptionalCallback;
|
||||
import net.minestom.server.utils.chunk.ChunkCallback;
|
||||
import net.minestom.server.utils.chunk.ChunkSupplier;
|
||||
import net.minestom.server.utils.chunk.ChunkUtils;
|
||||
@ -388,8 +389,7 @@ public class InstanceContainer extends Instance {
|
||||
final Chunk chunk = getChunk(chunkX, chunkZ);
|
||||
if (chunk != null) {
|
||||
// Chunk already loaded
|
||||
if (callback != null)
|
||||
callback.accept(chunk);
|
||||
OptionalCallback.execute(callback, chunk);
|
||||
} else {
|
||||
// Retrieve chunk from somewhere else (file or create a new one using the ChunkGenerator)
|
||||
retrieveChunk(chunkX, chunkZ, callback);
|
||||
@ -401,16 +401,14 @@ public class InstanceContainer extends Instance {
|
||||
final Chunk chunk = getChunk(chunkX, chunkZ);
|
||||
if (chunk != null) {
|
||||
// Chunk already loaded
|
||||
if (callback != null)
|
||||
callback.accept(chunk);
|
||||
OptionalCallback.execute(callback, chunk);
|
||||
} else {
|
||||
if (hasEnabledAutoChunkLoad()) {
|
||||
// Load chunk from StorageLocation or with ChunkGenerator
|
||||
retrieveChunk(chunkX, chunkZ, callback);
|
||||
} else {
|
||||
// Chunk not loaded, return null
|
||||
if (callback != null)
|
||||
callback.accept(null);
|
||||
OptionalCallback.execute(callback, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -478,8 +476,7 @@ public class InstanceContainer extends Instance {
|
||||
try {
|
||||
parallelSavingThreadPool.shutdown();
|
||||
parallelSavingThreadPool.awaitTermination(1L, java.util.concurrent.TimeUnit.DAYS);
|
||||
if (callback != null)
|
||||
callback.run();
|
||||
OptionalCallback.execute(callback);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -512,8 +509,7 @@ public class InstanceContainer extends Instance {
|
||||
// Execute callback and event in the instance thread
|
||||
scheduleNextTick(instance -> {
|
||||
callChunkLoadEvent(chunkX, chunkZ);
|
||||
if (callback != null)
|
||||
callback.accept(chunk);
|
||||
OptionalCallback.execute(callback, chunk);
|
||||
});
|
||||
});
|
||||
|
||||
@ -544,8 +540,7 @@ public class InstanceContainer extends Instance {
|
||||
chunkBatch.flushChunkGenerator(chunkGenerator, callback);
|
||||
} else {
|
||||
// No chunk generator, execute the callback with the empty chunk
|
||||
if (callback != null)
|
||||
callback.accept(chunk);
|
||||
OptionalCallback.execute(callback, chunk);
|
||||
}
|
||||
|
||||
UPDATE_MANAGER.signalChunkLoad(this, chunkX, chunkZ);
|
||||
|
@ -2,6 +2,7 @@ package net.minestom.server.instance;
|
||||
|
||||
import net.minestom.server.storage.StorageLocation;
|
||||
import net.minestom.server.utils.binary.BinaryReader;
|
||||
import net.minestom.server.utils.callback.OptionalCallback;
|
||||
import net.minestom.server.utils.chunk.ChunkCallback;
|
||||
import net.minestom.server.utils.chunk.ChunkSupplier;
|
||||
import org.slf4j.Logger;
|
||||
@ -52,16 +53,14 @@ public class MinestomBasicChunkLoader implements IChunkLoader {
|
||||
final byte[] data = chunk.getSerializedData();
|
||||
if (data == null) {
|
||||
// Chunk cannot be serialized (returned null), stop here
|
||||
if (callback != null)
|
||||
callback.run();
|
||||
OptionalCallback.execute(callback);
|
||||
return;
|
||||
}
|
||||
|
||||
// Save the serialized data to the storage location
|
||||
storageLocation.set(key, data);
|
||||
|
||||
if (callback != null)
|
||||
callback.run();
|
||||
OptionalCallback.execute(callback);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -0,0 +1,34 @@
|
||||
package net.minestom.server.utils.callback;
|
||||
|
||||
import net.minestom.server.instance.Chunk;
|
||||
import net.minestom.server.utils.chunk.ChunkCallback;
|
||||
|
||||
/**
|
||||
* Convenient class to execute callbacks which can be null.
|
||||
*/
|
||||
public class OptionalCallback {
|
||||
|
||||
/**
|
||||
* Executes an optional {@link Runnable}.
|
||||
*¬
|
||||
* @param callback the optional runnable, can be null
|
||||
*/
|
||||
public static void execute(Runnable callback) {
|
||||
if (callback != null) {
|
||||
callback.run();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes an optional {@link ChunkCallback}.
|
||||
*
|
||||
* @param callback the optional chunk callback, can be null
|
||||
* @param chunk the chunk to forward to the callback
|
||||
*/
|
||||
public static void execute(ChunkCallback callback, Chunk chunk) {
|
||||
if (callback != null) {
|
||||
callback.accept(chunk);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user