More clarification with CustomBlock updates

This commit is contained in:
themode 2020-09-12 18:44:52 +02:00
parent 5d733fa6e9
commit daaa126a1f
7 changed files with 29 additions and 31 deletions

View File

@ -14,7 +14,6 @@ import net.minestom.server.instance.batch.ChunkBatch;
import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockManager;
import net.minestom.server.instance.block.CustomBlock;
import net.minestom.server.instance.block.UpdateConsumer;
import net.minestom.server.network.PacketWriterUtils;
import net.minestom.server.network.packet.server.play.ChunkDataPacket;
import net.minestom.server.network.packet.server.play.UpdateLightPacket;
@ -84,16 +83,19 @@ public abstract class Chunk implements Viewable {
* <p>
* WARNING: this method is not thread-safe (in order to bring performance improvement with {@link ChunkBatch} & {@link BlockBatch})
* The thread-safe version is {@link InstanceContainer#setSeparateBlocks(int, int, int, short, short, Data)} (or any similar instance methods)
* Otherwise, you can simply do not forget to have this chunk synchronized when this is called
*
* @param x the block X
* @param y the block Y
* @param z the block Z
* @param blockStateId the block state id
* @param customId the custom block id
* @param data the data of the block, can be null
* @param updateConsumer the update method of the block, can be null
* @param x the block X
* @param y the block Y
* @param z the block Z
* @param blockStateId the block state id
* @param customBlockId the custom block id
* @param data the data of the block, can be null
* @param updatable true if the block has an update method
* Warning: {@param customBlockId} cannot be 0 and needs to be valid since the update delay and method
* will be retrieved from the associated {@link CustomBlock} object
*/
public abstract void setBlock(int x, int y, int z, short blockStateId, short customId, Data data, UpdateConsumer updateConsumer);
public abstract void setBlock(int x, int y, int z, short blockStateId, short customBlockId, Data data, boolean updatable);
/**
* Set the {@link Data} at a position

View File

@ -7,7 +7,6 @@ import it.unimi.dsi.fastutil.objects.Object2ShortOpenHashMap;
import net.minestom.server.data.Data;
import net.minestom.server.data.SerializableData;
import net.minestom.server.entity.pathfinding.PFBlockDescription;
import net.minestom.server.instance.block.UpdateConsumer;
import net.minestom.server.network.packet.server.play.ChunkDataPacket;
import net.minestom.server.reader.ChunkReader;
import net.minestom.server.utils.MathUtils;
@ -30,7 +29,7 @@ public class DynamicChunk extends Chunk {
}
@Override
public void setBlock(int x, int y, int z, short blockStateId, short customId, Data data, UpdateConsumer updateConsumer) {
public void setBlock(int x, int y, int z, short blockStateId, short customBlockId, Data data, boolean updatable) {
{
// Update pathfinder
@ -43,10 +42,10 @@ public class DynamicChunk extends Chunk {
final int index = getBlockIndex(x, y, z);
// True if the block is not complete air without any custom block capabilities
final boolean hasBlock = blockStateId != 0 || customId != 0;
final boolean hasBlock = blockStateId != 0 || customBlockId != 0;
if (hasBlock) {
this.blocksStateId[index] = blockStateId;
this.customBlocksId[index] = customId;
this.customBlocksId[index] = customBlockId;
} else {
// Block has been deleted, clear cache and return
@ -72,7 +71,7 @@ public class DynamicChunk extends Chunk {
}
// Set update consumer
if (updateConsumer != null) {
if (updatable) {
this.updatableBlocks.add(index);
this.updatableBlocksLastUpdate.put(index, System.currentTimeMillis());
} else {

View File

@ -13,7 +13,6 @@ import net.minestom.server.instance.batch.ChunkBatch;
import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockProvider;
import net.minestom.server.instance.block.CustomBlock;
import net.minestom.server.instance.block.UpdateConsumer;
import net.minestom.server.instance.block.rule.BlockPlacementRule;
import net.minestom.server.network.packet.server.play.BlockChangePacket;
import net.minestom.server.network.packet.server.play.ParticlePacket;
@ -139,15 +138,15 @@ public class InstanceContainer extends Instance {
// Retrieve custom block values
short customBlockId = 0;
UpdateConsumer updateConsumer = null;
boolean hasUpdate = false;
if (isCustomBlock) {
customBlockId = customBlock.getCustomBlockId();
data = customBlock.createData(this, blockPosition, data);
updateConsumer = CustomBlockUtils.getCustomBlockUpdate(customBlock);
hasUpdate = CustomBlockUtils.hasUpdate(customBlock);
}
// Set the block
chunk.setBlock(x, y, z, blockStateId, customBlockId, data, updateConsumer);
chunk.setBlock(x, y, z, blockStateId, customBlockId, data, hasUpdate);
// Refresh neighbors since a new block has been placed
executeNeighboursBlockPlacementRule(blockPosition);

View File

@ -3,7 +3,6 @@ package net.minestom.server.instance;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import net.minestom.server.data.Data;
import net.minestom.server.instance.block.BlockProvider;
import net.minestom.server.instance.block.UpdateConsumer;
import net.minestom.server.network.packet.server.play.ChunkDataPacket;
import net.minestom.server.utils.chunk.ChunkUtils;
import net.minestom.server.world.biomes.Biome;
@ -20,7 +19,7 @@ public class StaticChunk extends Chunk {
}
@Override
public void setBlock(int x, int y, int z, short blockStateId, short customId, Data data, UpdateConsumer updateConsumer) {
public void setBlock(int x, int y, int z, short blockStateId, short customBlockId, Data data, boolean updatable) {
//noop
}

View File

@ -98,7 +98,7 @@ public class BlockBatch implements InstanceBatch {
private Data data;
public void apply(Chunk chunk) {
chunk.setBlock(x, y, z, blockStateId, customBlockId, data, CustomBlockUtils.getCustomBlockUpdate(customBlockId));
chunk.setBlock(x, y, z, blockStateId, customBlockId, data, CustomBlockUtils.hasUpdate(customBlockId));
}
}

View File

@ -118,7 +118,7 @@ public class ChunkBatch implements InstanceBatch {
private Data data;
public void apply(Chunk chunk) {
chunk.setBlock(x, y, z, blockStateId, customBlockId, data, CustomBlockUtils.getCustomBlockUpdate(customBlockId));
chunk.setBlock(x, y, z, blockStateId, customBlockId, data, CustomBlockUtils.hasUpdate(customBlockId));
}
}

View File

@ -3,31 +3,30 @@ package net.minestom.server.utils.block;
import net.minestom.server.MinecraftServer;
import net.minestom.server.instance.block.BlockManager;
import net.minestom.server.instance.block.CustomBlock;
import net.minestom.server.instance.block.UpdateConsumer;
public class CustomBlockUtils {
private static final BlockManager BLOCK_MANAGER = MinecraftServer.getBlockManager();
/**
* Get the {@link UpdateConsumer} of a custom block id
* Get if a custom block id has an update method
*
* @param customBlockId the custom block id
* @return the {@link UpdateConsumer} of the custom block
* @return true if {@param customBlockId} has an update method
*/
public static UpdateConsumer getCustomBlockUpdate(short customBlockId) {
public static boolean hasUpdate(short customBlockId) {
final CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(customBlockId);
return getCustomBlockUpdate(customBlock);
return hasUpdate(customBlock);
}
/**
* Get the {@link UpdateConsumer} of a {@link CustomBlock}
* Get if a {@link CustomBlock} has an update method
*
* @param customBlock the {@link CustomBlock}
* @return the {@link UpdateConsumer} of the {@link CustomBlock}
* @return true if {@param customBlock} has an update method
*/
public static UpdateConsumer getCustomBlockUpdate(CustomBlock customBlock) {
return customBlock != null && customBlock.hasUpdate() ? customBlock::update : null;
public static boolean hasUpdate(CustomBlock customBlock) {
return customBlock != null && customBlock.hasUpdate();
}
}