Merge pull request #166 from Minestom/tickable

Tickable interface
This commit is contained in:
TheMode 2021-04-14 20:12:28 +02:00 committed by GitHub
commit 90211ddf50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 60 additions and 26 deletions

View File

@ -0,0 +1,14 @@
package net.minestom.server;
/**
* Represents an element which is ticked at a regular interval.
*/
public interface Tickable {
/**
* Ticks this element.
*
* @param time the time of the tick in milliseconds
*/
void tick(long time);
}

View File

@ -6,6 +6,7 @@ import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.event.HoverEvent.ShowEntity;
import net.kyori.adventure.text.event.HoverEventSource;
import net.minestom.server.MinecraftServer;
import net.minestom.server.Tickable;
import net.minestom.server.Viewable;
import net.minestom.server.chat.JsonMessage;
import net.minestom.server.collision.BoundingBox;
@ -57,7 +58,7 @@ import java.util.function.UnaryOperator;
* <p>
* To create your own entity you probably want to extends {@link LivingEntity} or {@link EntityCreature} instead.
*/
public class Entity implements Viewable, EventHandler, DataContainer, PermissionHandler, HoverEventSource<ShowEntity> {
public class Entity implements Viewable, Tickable, EventHandler, DataContainer, PermissionHandler, HoverEventSource<ShowEntity> {
private static final Map<Integer, Entity> ENTITY_BY_ID = new ConcurrentHashMap<>();
private static final Map<UUID, Entity> ENTITY_BY_UUID = new ConcurrentHashMap<>();
@ -443,6 +444,7 @@ public class Entity implements Viewable, EventHandler, DataContainer, Permission
*
* @param time the update time in milliseconds
*/
@Override
public void tick(long time) {
if (instance == null)
return;

View File

@ -1,6 +1,7 @@
package net.minestom.server.instance;
import net.minestom.server.MinecraftServer;
import net.minestom.server.Tickable;
import net.minestom.server.Viewable;
import net.minestom.server.data.Data;
import net.minestom.server.data.DataContainer;
@ -47,7 +48,7 @@ import java.util.concurrent.ConcurrentHashMap;
* You generally want to avoid storing references of this object as this could lead to a huge memory leak,
* you should store the chunk coordinates instead.
*/
public abstract class Chunk implements Viewable, DataContainer {
public abstract class Chunk implements Viewable, Tickable, DataContainer {
protected static final BlockManager BLOCK_MANAGER = MinecraftServer.getBlockManager();
protected static final BiomeManager BIOME_MANAGER = MinecraftServer.getBiomeManager();
@ -63,6 +64,7 @@ public abstract class Chunk implements Viewable, DataContainer {
private final UUID identifier;
protected Instance instance;
@NotNull
protected final Biome[] biomes;
protected final int chunkX, chunkZ;
@ -81,8 +83,9 @@ public abstract class Chunk implements Viewable, DataContainer {
// Data
protected Data data;
public Chunk(@Nullable Biome[] biomes, int chunkX, int chunkZ, boolean shouldGenerate) {
public Chunk(@NotNull Instance instance, @Nullable Biome[] biomes, int chunkX, int chunkZ, boolean shouldGenerate) {
this.identifier = UUID.randomUUID();
this.instance = instance;
this.chunkX = chunkX;
this.chunkZ = chunkZ;
this.shouldGenerate = shouldGenerate;
@ -122,10 +125,10 @@ public abstract class Chunk implements Viewable, DataContainer {
* <p>
* WARNING: this method doesn't necessary have to be thread-safe, proceed with caution.
*
* @param time the time of the update in milliseconds
* @param instance the {@link Instance} linked to this chunk
* @param time the time of the update in milliseconds
*/
public abstract void tick(long time, @NotNull Instance instance);
@Override
public abstract void tick(long time);
/**
* Gets the block state id at a position.
@ -241,12 +244,13 @@ public abstract class Chunk implements Viewable, DataContainer {
* <p>
* The chunk position (X/Z) can be modified using the given arguments.
*
* @param chunkX the chunk X of the copy
* @param chunkZ the chunk Z of the copy
* @param instance the chunk owner
* @param chunkX the chunk X of the copy
* @param chunkZ the chunk Z of the copy
* @return a copy of this chunk with a potentially new instance and position
*/
@NotNull
public abstract Chunk copy(int chunkX, int chunkZ);
public abstract Chunk copy(@NotNull Instance instance, int chunkX, int chunkZ);
/**
* Resets the chunk, this means clearing all the data making it empty.
@ -293,6 +297,16 @@ public abstract class Chunk implements Viewable, DataContainer {
return identifier;
}
/**
* Gets the instance where this chunk is stored
*
* @return the linked instance
*/
@NotNull
public Instance getInstance() {
return instance;
}
public Biome[] getBiomes() {
return biomes;
}

View File

@ -62,15 +62,15 @@ public class DynamicChunk extends Chunk {
private ChunkDataPacket cachedPacket;
private long cachedPacketTime;
public DynamicChunk(@Nullable Biome[] biomes, int chunkX, int chunkZ,
public DynamicChunk(@NotNull Instance instance, @Nullable Biome[] biomes, int chunkX, int chunkZ,
@NotNull PaletteStorage blockPalette, @NotNull PaletteStorage customBlockPalette) {
super(biomes, chunkX, chunkZ, true);
super(instance, biomes, chunkX, chunkZ, true);
this.blockPalette = blockPalette;
this.customBlockPalette = customBlockPalette;
}
public DynamicChunk(@Nullable Biome[] biomes, int chunkX, int chunkZ) {
this(biomes, chunkX, chunkZ,
public DynamicChunk(@NotNull Instance instance, @Nullable Biome[] biomes, int chunkX, int chunkZ) {
this(instance, biomes, chunkX, chunkZ,
new PaletteStorage(8, 2),
new PaletteStorage(8, 2));
}
@ -131,7 +131,7 @@ public class DynamicChunk extends Chunk {
}
@Override
public void tick(long time, @NotNull Instance instance) {
public void tick(long time) {
if (updatableBlocks.isEmpty())
return;
@ -406,8 +406,8 @@ public class DynamicChunk extends Chunk {
@NotNull
@Override
public Chunk copy(int chunkX, int chunkZ) {
DynamicChunk dynamicChunk = new DynamicChunk(biomes.clone(), chunkX, chunkZ);
public Chunk copy(@NotNull Instance instance, int chunkX, int chunkZ) {
DynamicChunk dynamicChunk = new DynamicChunk(instance, biomes.clone(), chunkX, chunkZ);
dynamicChunk.blockPalette = blockPalette.clone();
dynamicChunk.customBlockPalette = customBlockPalette.clone();
dynamicChunk.blocksData.putAll(blocksData);

View File

@ -2,6 +2,7 @@ package net.minestom.server.instance;
import com.google.common.collect.Queues;
import net.minestom.server.MinecraftServer;
import net.minestom.server.Tickable;
import net.minestom.server.UpdateManager;
import net.minestom.server.adventure.audience.PacketGroupingAudience;
import net.minestom.server.data.Data;
@ -55,7 +56,7 @@ import java.util.function.Consumer;
* you need to be sure to signal the {@link UpdateManager} of the changes using
* {@link UpdateManager#signalChunkLoad(Instance, Chunk)} and {@link UpdateManager#signalChunkUnload(Instance, Chunk)}.
*/
public abstract class Instance implements BlockModifier, EventHandler, DataContainer, PacketGroupingAudience {
public abstract class Instance implements BlockModifier, Tickable, EventHandler, DataContainer, PacketGroupingAudience {
protected static final BlockManager BLOCK_MANAGER = MinecraftServer.getBlockManager();
protected static final UpdateManager UPDATE_MANAGER = MinecraftServer.getUpdateManager();
@ -1009,6 +1010,7 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
*
* @param time the tick time in milliseconds
*/
@Override
public void tick(long time) {
// Scheduled tasks
if (!nextTick.isEmpty()) {

View File

@ -529,7 +529,7 @@ public class InstanceContainer extends Instance {
chunkGenerator.fillBiomes(biomes, chunkX, chunkZ);
}
final Chunk chunk = chunkSupplier.createChunk(biomes, chunkX, chunkZ);
final Chunk chunk = chunkSupplier.createChunk(this, biomes, chunkX, chunkZ);
Check.notNull(chunk, "Chunks supplied by a ChunkSupplier cannot be null.");
cacheChunk(chunk);
@ -623,7 +623,7 @@ public class InstanceContainer extends Instance {
/**
* Copies all the chunks of this instance and create a new instance container with all of them.
* <p>
* Chunks are copied with {@link Chunk#copy(int, int)},
* Chunks are copied with {@link Chunk#copy(Instance, int, int)},
* {@link UUID} is randomized, {@link DimensionType} is passed over and the {@link StorageLocation} is null.
*
* @return an {@link InstanceContainer} with the exact same chunks as 'this'
@ -638,7 +638,7 @@ public class InstanceContainer extends Instance {
final int chunkX = chunk.getChunkX();
final int chunkZ = chunk.getChunkZ();
final Chunk copiedChunk = chunk.copy(chunkX, chunkZ);
final Chunk copiedChunk = chunk.copy(copiedInstance, chunkX, chunkZ);
copiedInstance.cacheChunk(copiedChunk);
UPDATE_MANAGER.signalChunkLoad(copiedInstance, copiedChunk);

View File

@ -77,7 +77,7 @@ public class MinestomBasicChunkLoader implements IChunkLoader {
// Found, load from result bytes
BinaryReader reader = new BinaryReader(bytes);
// Create the chunk object using the instance's ChunkSupplier to support multiple implementations
Chunk chunk = instanceContainer.getChunkSupplier().createChunk(null, chunkX, chunkZ);
Chunk chunk = instanceContainer.getChunkSupplier().createChunk(instance, null, chunkX, chunkZ);
// Execute the callback once all blocks are placed (allow for multithreaded implementations)
chunk.readChunk(reader, callback);
return true;

View File

@ -144,7 +144,7 @@ public abstract class ThreadProvider {
* @param time the current time in ms
*/
protected void updateChunk(@NotNull Instance instance, @NotNull Chunk chunk, long time) {
chunk.tick(time, instance);
chunk.tick(time);
}
// ENTITY UPDATE

View File

@ -1,6 +1,7 @@
package net.minestom.server.utils.chunk;
import net.minestom.server.instance.Chunk;
import net.minestom.server.instance.Instance;
import net.minestom.server.world.biomes.Biome;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -14,11 +15,12 @@ public interface ChunkSupplier {
/**
* Creates a {@link Chunk} object.
*
* @param biomes the biomes of the chunk, can be null
* @param chunkX the chunk X
* @param chunkZ the chunk Z
* @param instance the linked instance
* @param biomes the biomes of the chunk, can be null
* @param chunkX the chunk X
* @param chunkZ the chunk Z
* @return a newly {@link Chunk} object, cannot be null
*/
@NotNull
Chunk createChunk(@Nullable Biome[] biomes, int chunkX, int chunkZ);
Chunk createChunk(@NotNull Instance instance, @Nullable Biome[] biomes, int chunkX, int chunkZ);
}