Do not force the use of InstanceContainer

This commit is contained in:
themode 2021-01-08 15:42:30 +01:00 committed by Matt Worzala
parent 66a5829c3e
commit ba32acbd04
5 changed files with 43 additions and 32 deletions

View File

@ -4,6 +4,7 @@ import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import net.minestom.server.data.Data;
import net.minestom.server.instance.Chunk;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.InstanceContainer;
import net.minestom.server.utils.chunk.ChunkUtils;
import org.jetbrains.annotations.NotNull;
@ -59,7 +60,7 @@ public class AbsoluteBlockBatch implements Batch<Runnable> {
* @param callback The callback to be executed when the batch is applied
*/
@Override
public void apply(@NotNull InstanceContainer instance, @Nullable Runnable callback) {
public void apply(@NotNull Instance instance, @Nullable Runnable callback) {
apply(instance, callback, true);
}
@ -70,7 +71,7 @@ public class AbsoluteBlockBatch implements Batch<Runnable> {
* @param instance The instance in which the batch should be applied
* @param callback The callback to be executed when the batch is applied
*/
public void unsafeApply(@NotNull InstanceContainer instance, @Nullable Runnable callback) {
public void unsafeApply(@NotNull Instance instance, @Nullable Runnable callback) {
apply(instance, callback, false);
}
@ -81,7 +82,7 @@ public class AbsoluteBlockBatch implements Batch<Runnable> {
* @param callback The callback to be executed when the batch is applied
* @param safeCallback If true, the callback will be executed in the next instance update. Otherwise it will be executed immediately upon completion
*/
protected void apply(@NotNull InstanceContainer instance, @Nullable Runnable callback, boolean safeCallback) {
protected void apply(@NotNull Instance instance, @Nullable Runnable callback, boolean safeCallback) {
synchronized (chunkBatchesMap) {
AtomicInteger counter = new AtomicInteger();
for (Long2ObjectMap.Entry<ChunkBatch> entry : chunkBatchesMap.long2ObjectEntrySet()) {
@ -95,7 +96,12 @@ public class AbsoluteBlockBatch implements Batch<Runnable> {
// Execute the callback if this was the last chunk to process
if (isLast) {
instance.refreshLastBlockChangeTime();
if (instance instanceof InstanceContainer) {
// FIXME: put method in Instance instead
((InstanceContainer) instance).refreshLastBlockChangeTime();
}
if (callback != null) {
if (safeCallback) {
instance.scheduleNextTick(inst -> callback.run());

View File

@ -3,7 +3,7 @@ package net.minestom.server.instance.batch;
import net.minestom.server.MinecraftServer;
import net.minestom.server.data.Data;
import net.minestom.server.instance.BlockModifier;
import net.minestom.server.instance.InstanceContainer;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.block.CustomBlock;
import net.minestom.server.utils.thread.MinestomThread;
import net.minestom.server.utils.validate.Check;
@ -69,7 +69,7 @@ public interface Batch<Callback> extends BlockModifier {
* @param instance The instance in which the batch should be applied
* @param callback The callback to be executed when the batch is applied
*/
void apply(@NotNull InstanceContainer instance, @Nullable Callback callback);
void apply(@NotNull Instance instance, @Nullable Callback callback);
// @NotNull
// Batch<Callback> reversableApply(@NotNull InstanceContainer instance, @Nullable Callback callback);

View File

@ -6,6 +6,7 @@ import it.unimi.dsi.fastutil.longs.LongArrayList;
import it.unimi.dsi.fastutil.longs.LongList;
import net.minestom.server.data.Data;
import net.minestom.server.instance.Chunk;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.InstanceContainer;
import net.minestom.server.utils.block.CustomBlockUtils;
import net.minestom.server.utils.callback.OptionalCallback;
@ -81,7 +82,7 @@ public class ChunkBatch implements Batch<ChunkCallback> {
* @param callback The callback to be executed when the batch is applied
*/
@Override
public void apply(@NotNull InstanceContainer instance, @Nullable ChunkCallback callback) {
public void apply(@NotNull Instance instance, @Nullable ChunkCallback callback) {
apply(instance, 0, 0, callback);
}
@ -89,11 +90,11 @@ public class ChunkBatch implements Batch<ChunkCallback> {
* Apply this batch to the given chunk.
*
* @param instance The instance in which the batch should be applied
* @param chunkX The x chunk coordinate of the target chunk
* @param chunkZ The z chunk coordinate of the target chunk
* @param chunkX The x chunk coordinate of the target chunk
* @param chunkZ The z chunk coordinate of the target chunk
* @param callback The callback to be executed when the batch is applied.
*/
public void apply(@NotNull InstanceContainer instance, int chunkX, int chunkZ, @Nullable ChunkCallback callback) {
public void apply(@NotNull Instance instance, int chunkX, int chunkZ, @Nullable ChunkCallback callback) {
final Chunk chunk = instance.getChunk(chunkX, chunkZ);
if (chunk == null) {
LOGGER.warn("Unable to apply ChunkBatch to unloaded chunk ({}, {}) in {}.", chunkX, chunkZ, instance.getUniqueId());
@ -106,10 +107,10 @@ public class ChunkBatch implements Batch<ChunkCallback> {
* Apply this batch to the given chunk.
*
* @param instance The instance in which the batch should be applied
* @param chunk The target chunk
* @param chunk The target chunk
* @param callback The callback to be executed when the batch is applied
*/
public void apply(@NotNull InstanceContainer instance, @NotNull Chunk chunk, @Nullable ChunkCallback callback) {
public void apply(@NotNull Instance instance, @NotNull Chunk chunk, @Nullable ChunkCallback callback) {
apply(instance, chunk, callback, true);
}
@ -118,29 +119,29 @@ public class ChunkBatch implements Batch<ChunkCallback> {
* immediately when the blocks have been applied, in an unknown thread.
*
* @param instance The instance in which the batch should be applied
* @param chunk The target chunk
* @param chunk The target chunk
* @param callback The callback to be executed when the batch is applied
*/
public void unsafeApply(@NotNull InstanceContainer instance, @NotNull Chunk chunk, @Nullable ChunkCallback callback) {
public void unsafeApply(@NotNull Instance instance, @NotNull Chunk chunk, @Nullable ChunkCallback callback) {
apply(instance, chunk, callback, false);
}
/**
* Apply this batch to the given chunk, and execute the callback depending on safeCallback.
*
* @param instance The instance in which the batch should be applied
* @param chunk The target chunk
* @param callback The callback to be executed when the batch is applied
* @param instance The instance in which the batch should be applied
* @param chunk The target chunk
* @param callback The callback to be executed when the batch is applied
* @param safeCallback If true, the callback will be executed in the next instance update. Otherwise it will be executed immediately upon completion
*/
protected void apply(@NotNull InstanceContainer instance, @NotNull Chunk chunk, @Nullable ChunkCallback callback, boolean safeCallback) {
protected void apply(@NotNull Instance instance, @NotNull Chunk chunk, @Nullable ChunkCallback callback, boolean safeCallback) {
BLOCK_BATCH_POOL.execute(() -> singleThreadFlush(instance, chunk, callback, safeCallback));
}
/**
* Applies this batch in the current thread, executing the callback upon completion.
*/
private void singleThreadFlush(InstanceContainer instance, Chunk chunk, @Nullable ChunkCallback callback, boolean safeCallback) {
private void singleThreadFlush(Instance instance, Chunk chunk, @Nullable ChunkCallback callback, boolean safeCallback) {
if (blocks.isEmpty()) {
OptionalCallback.execute(callback, chunk);
return;
@ -187,14 +188,18 @@ public class ChunkBatch implements Batch<ChunkCallback> {
/**
* Updates the given chunk for all of its viewers, and executes the callback.
*/
private void updateChunk(InstanceContainer instance, Chunk chunk, @Nullable ChunkCallback callback, boolean safeCallback) {
private void updateChunk(@NotNull Instance instance, Chunk chunk, @Nullable ChunkCallback callback, boolean safeCallback) {
// Refresh chunk for viewers
// Formerly this had an option to do a Chunk#sendChunkUpdate
// however Chunk#sendChunk does the same including a light update
chunk.sendChunk();
chunk.sendChunkUpdate();
//chunk.sendChunk();
instance.refreshLastBlockChangeTime();
if (instance instanceof InstanceContainer) {
// FIXME: put method in Instance instead
((InstanceContainer) instance).refreshLastBlockChangeTime();
}
if (callback != null) {
if (safeCallback) {

View File

@ -1,10 +1,7 @@
package net.minestom.server.instance.batch;
import net.minestom.server.data.Data;
import net.minestom.server.instance.Chunk;
import net.minestom.server.instance.ChunkGenerator;
import net.minestom.server.instance.ChunkPopulator;
import net.minestom.server.instance.InstanceContainer;
import net.minestom.server.instance.*;
import net.minestom.server.utils.block.CustomBlockUtils;
import net.minestom.server.utils.chunk.ChunkCallback;
import org.jetbrains.annotations.NotNull;
@ -57,7 +54,9 @@ public class ChunkGenerationBatch extends ChunkBatch {
}
@Override
protected void apply(@NotNull InstanceContainer instance, @NotNull Chunk chunk, @Nullable ChunkCallback callback, boolean safeCallback) {
protected void apply(@NotNull Instance instance,
@NotNull Chunk chunk, @Nullable ChunkCallback callback,
boolean safeCallback) {
throw new IllegalStateException("#apply is not supported for chunk generation batch.");
}
}

View File

@ -5,6 +5,7 @@ import it.unimi.dsi.fastutil.longs.Long2IntOpenHashMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import net.minestom.server.data.Data;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.InstanceContainer;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.validate.Check;
@ -72,23 +73,23 @@ public class RelativeBlockBatch implements Batch<Runnable> {
}
@Override
public void apply(@NotNull InstanceContainer instance, @Nullable Runnable callback) {
public void apply(@NotNull Instance instance, @Nullable Runnable callback) {
apply(instance, 0, 0, 0, callback);
}
public void apply(@NotNull InstanceContainer instance, @NotNull BlockPosition position, @Nullable Runnable callback) {
public void apply(@NotNull Instance instance, @NotNull BlockPosition position, @Nullable Runnable callback) {
apply(instance, position.getX(), position.getY(), position.getZ(), callback);
}
public void apply(@NotNull InstanceContainer instance, int x, int y, int z, @Nullable Runnable callback) {
public void apply(@NotNull Instance instance, int x, int y, int z, @Nullable Runnable callback) {
apply(instance, x, y, z, callback, true);
}
public void applyUnsafe(@NotNull InstanceContainer instance, int x, int y, int z, @Nullable Runnable callback) {
public void applyUnsafe(@NotNull Instance instance, int x, int y, int z, @Nullable Runnable callback) {
apply(instance, x, y, z, callback, false);
}
protected void apply(@NotNull InstanceContainer instance, int x, int y, int z, @Nullable Runnable callback, boolean safeCallback) {
protected void apply(@NotNull Instance instance, int x, int y, int z, @Nullable Runnable callback, boolean safeCallback) {
AbsoluteBlockBatch batch = new AbsoluteBlockBatch();
synchronized (blockIdMap) {