From 26debd0d4b35b1b9c3883784dcd7b2e2b71e9f0e Mon Sep 17 00:00:00 2001 From: Felix Cravic Date: Sun, 26 Apr 2020 20:41:58 +0200 Subject: [PATCH] Optimization + fixed custom block placement --- src/main/java/fr/themode/demo/PlayerInit.java | 4 + .../demo/generator/ChunkGeneratorDemo.java | 3 +- .../demo/generator/NoiseTestGenerator.java | 6 +- .../server/instance/BlockModifier.java | 22 +++--- .../net/minestom/server/instance/Chunk.java | 18 ++--- .../server/instance/InstanceContainer.java | 79 +++++++++---------- .../server/instance/SharedInstance.java | 6 +- .../server/instance/block/BlockManager.java | 16 ++-- .../network/netty/channel/ClientChannel.java | 1 - 9 files changed, 77 insertions(+), 78 deletions(-) diff --git a/src/main/java/fr/themode/demo/PlayerInit.java b/src/main/java/fr/themode/demo/PlayerInit.java index d372f9232..85ad99661 100644 --- a/src/main/java/fr/themode/demo/PlayerInit.java +++ b/src/main/java/fr/themode/demo/PlayerInit.java @@ -141,6 +141,10 @@ public class PlayerInit { itemEntity.setVelocity(velocity, 500); }); + player.setEventCallback(PlayerDisconnectEvent.class, event -> { + System.out.println("DISCONNECTION " + player.getUsername()); + }); + player.setEventCallback(PlayerLoginEvent.class, event -> { event.setSpawningInstance(instanceContainer); }); diff --git a/src/main/java/fr/themode/demo/generator/ChunkGeneratorDemo.java b/src/main/java/fr/themode/demo/generator/ChunkGeneratorDemo.java index 996d99b83..799810adc 100644 --- a/src/main/java/fr/themode/demo/generator/ChunkGeneratorDemo.java +++ b/src/main/java/fr/themode/demo/generator/ChunkGeneratorDemo.java @@ -4,6 +4,7 @@ import net.minestom.server.instance.Biome; import net.minestom.server.instance.Chunk; import net.minestom.server.instance.ChunkGenerator; import net.minestom.server.instance.batch.ChunkBatch; +import net.minestom.server.instance.block.Block; import java.util.Random; @@ -19,7 +20,7 @@ public class ChunkGeneratorDemo extends ChunkGenerator { if (random.nextInt(100) > 10) { batch.setCustomBlock(x, y, z, "custom_block"); } else { - batch.setBlock(x, y, z, (short) 10); + batch.setBlock(x, y, z, Block.COMMAND_BLOCK); } } } diff --git a/src/main/java/fr/themode/demo/generator/NoiseTestGenerator.java b/src/main/java/fr/themode/demo/generator/NoiseTestGenerator.java index 8a178b155..4531cef82 100644 --- a/src/main/java/fr/themode/demo/generator/NoiseTestGenerator.java +++ b/src/main/java/fr/themode/demo/generator/NoiseTestGenerator.java @@ -4,6 +4,7 @@ import net.minestom.server.instance.Biome; import net.minestom.server.instance.Chunk; import net.minestom.server.instance.ChunkGenerator; import net.minestom.server.instance.batch.ChunkBatch; +import net.minestom.server.instance.block.Block; import net.minestom.server.utils.noise.FastNoise; import java.util.Random; @@ -20,7 +21,7 @@ public class NoiseTestGenerator extends ChunkGenerator { @Override public void generateChunkData(ChunkBatch batch, int chunkX, int chunkZ) { - for (byte x = 0; x < Chunk.CHUNK_SIZE_X; x++) + for (byte x = 0; x < Chunk.CHUNK_SIZE_X; x++) { for (byte z = 0; z < Chunk.CHUNK_SIZE_Z; z++) { float height = fastNoise.GetSimplex(x + Chunk.CHUNK_SIZE_X * chunkX, z + Chunk.CHUNK_SIZE_Z * chunkZ) * 135; height = Math.max(height, 70); @@ -28,10 +29,11 @@ public class NoiseTestGenerator extends ChunkGenerator { if (random.nextInt(100) > 10) { batch.setCustomBlock(x, y, z, "custom_block"); } else { - batch.setBlock(x, y, z, (short) 10); + batch.setBlock(x, y, z, Block.DIAMOND_ORE); } } } + } } @Override diff --git a/src/main/java/net/minestom/server/instance/BlockModifier.java b/src/main/java/net/minestom/server/instance/BlockModifier.java index 1210e9723..8cb555aef 100644 --- a/src/main/java/net/minestom/server/instance/BlockModifier.java +++ b/src/main/java/net/minestom/server/instance/BlockModifier.java @@ -14,7 +14,7 @@ public interface BlockModifier { void setBlock(int x, int y, int z, short blockId, Data data); - void setCustomBlock(int x, int y, int z, short blockId, Data data); + void setCustomBlock(int x, int y, int z, short customBlockId, Data data); default void setBlock(int x, int y, int z, short blockId) { setBlock(x, y, z, blockId, null); @@ -24,8 +24,8 @@ public interface BlockModifier { setBlock(x, y, z, block.getBlockId(), null); } - default void setCustomBlock(int x, int y, int z, short blockId) { - setCustomBlock(x, y, z, blockId, null); + default void setCustomBlock(int x, int y, int z, short customBlockId) { + setCustomBlock(x, y, z, customBlockId, null); } default void setBlock(BlockPosition blockPosition, Block block) { @@ -45,21 +45,21 @@ public interface BlockModifier { setBlock(position.toBlockPosition(), blockId); } - default void setCustomBlock(int x, int y, int z, String blockId, Data data) { - CustomBlock customBlock = BLOCK_MANAGER.getBlock(blockId); + default void setCustomBlock(int x, int y, int z, String customBlockId, Data data) { + CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(customBlockId); setCustomBlock(x, y, z, customBlock.getId(), data); } - default void setCustomBlock(int x, int y, int z, String blockId) { - setCustomBlock(x, y, z, blockId, null); + default void setCustomBlock(int x, int y, int z, String customBlockId) { + setCustomBlock(x, y, z, customBlockId, null); } - default void setCustomBlock(BlockPosition blockPosition, String blockId) { - setCustomBlock(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), blockId); + default void setCustomBlock(BlockPosition blockPosition, String customBlockId) { + setCustomBlock(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), customBlockId); } - default void setCustomBlock(Position position, String blockId) { - setCustomBlock(position.toBlockPosition(), blockId); + default void setCustomBlock(Position position, String customBlockId) { + setCustomBlock(position.toBlockPosition(), customBlockId); } } diff --git a/src/main/java/net/minestom/server/instance/Chunk.java b/src/main/java/net/minestom/server/instance/Chunk.java index 63bb0beb5..24114c8a6 100644 --- a/src/main/java/net/minestom/server/instance/Chunk.java +++ b/src/main/java/net/minestom/server/instance/Chunk.java @@ -70,22 +70,14 @@ public class Chunk implements Viewable { setBlock(x, y, z, blockId, (short) 0, data, null); } - public void UNSAFE_setBlock(int x, int y, int z, short blockId) { - UNSAFE_setBlock(x, y, z, blockId, null); - } - public void UNSAFE_setCustomBlock(int x, int y, int z, short customBlockId, Data data) { - CustomBlock customBlock = BLOCK_MANAGER.getBlock(customBlockId); + CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(customBlockId); if (customBlock == null) throw new IllegalArgumentException("The custom block " + customBlockId + " does not exist or isn't registered"); setCustomBlock(x, y, z, customBlock, data); } - public void UNSAFE_setCustomBlock(int x, int y, int z, short customBlockId) { - UNSAFE_setCustomBlock(x, y, z, customBlockId, null); - } - private void setCustomBlock(int x, int y, int z, CustomBlock customBlock, Data data) { UpdateConsumer updateConsumer = customBlock.hasUpdate() ? customBlock::update : null; setBlock(x, y, z, customBlock.getBlockId(), customBlock.getId(), data, updateConsumer); @@ -159,7 +151,7 @@ public class Chunk implements Viewable { public CustomBlock getCustomBlock(int x, int y, int z) { short id = customBlocksId[getBlockIndex(x, y, z)]; - return id != 0 ? BLOCK_MANAGER.getBlock(id) : null; + return id != 0 ? BLOCK_MANAGER.getCustomBlock(id) : null; } protected CustomBlock getCustomBlock(int index) { @@ -168,8 +160,10 @@ public class Chunk implements Viewable { } protected void refreshBlockValue(int x, int y, int z, short blockId, short customId) { - this.blocksId[getBlockIndex(x, y, z)] = blockId; - this.customBlocksId[getBlockIndex(x, y, z)] = customId; + int blockIndex = getBlockIndex(x, y, z); + + this.blocksId[blockIndex] = blockId; + this.customBlocksId[blockIndex] = customId; } protected void refreshBlockValue(int x, int y, int z, short blockId) { diff --git a/src/main/java/net/minestom/server/instance/InstanceContainer.java b/src/main/java/net/minestom/server/instance/InstanceContainer.java index 052ac2096..646f1fb79 100644 --- a/src/main/java/net/minestom/server/instance/InstanceContainer.java +++ b/src/main/java/net/minestom/server/instance/InstanceContainer.java @@ -44,47 +44,48 @@ public class InstanceContainer extends Instance { } @Override - public synchronized void setBlock(int x, int y, int z, short blockId, Data data) { - Chunk chunk = getChunkAt(x, z); - synchronized (chunk) { - - int index = SerializerUtils.coordToChunkIndex(x, y, z); - - callBlockDestroy(chunk, index, x, y, z); - - BlockPosition blockPosition = new BlockPosition(x, y, z); - - blockId = executeBlockPlacementRule(blockId, blockPosition); - - chunk.UNSAFE_setBlock(x, y, z, blockId, data); - - executeNeighboursBlockPlacementRule(blockPosition); - - sendBlockChange(chunk, x, y, z, blockId); - } + public void setBlock(int x, int y, int z, short blockId, Data data) { + setBlock(x, y, z, blockId, (short) 0, data); } @Override - public synchronized void setCustomBlock(int x, int y, int z, short blockId, Data data) { + public void setCustomBlock(int x, int y, int z, short customBlockId, Data data) { + short blockId = BLOCK_MANAGER.getCustomBlock(customBlockId).getBlockId(); + setBlock(x, y, z, blockId, customBlockId, data); + } + + private synchronized void setBlock(int x, int y, int z, short blockId, short customBlockId, Data data) { Chunk chunk = getChunkAt(x, z); synchronized (chunk) { - int index = SerializerUtils.coordToChunkIndex(x, y, z); + boolean isCustomBlock = customBlockId != 0; - callBlockDestroy(chunk, index, x, y, z); + int index = SerializerUtils.coordToChunkIndex(x, y, z); BlockPosition blockPosition = new BlockPosition(x, y, z); + // Call the destroy listener if previous block was a custom block + callBlockDestroy(chunk, index, blockPosition); + + // Change id based on neighbors blockId = executeBlockPlacementRule(blockId, blockPosition); - chunk.UNSAFE_setCustomBlock(x, y, z, blockId, data); + // Set the block + if (isCustomBlock) { + chunk.UNSAFE_setCustomBlock(x, y, z, customBlockId, data); + } else { + chunk.UNSAFE_setBlock(x, y, z, blockId, data); + } + // Refresh neighbors since a new block has been placed executeNeighboursBlockPlacementRule(blockPosition); - callBlockPlace(chunk, index, x, y, z); + // Call the place listener for custom block + if (isCustomBlock) + callBlockPlace(chunk, index, blockPosition); - short id = BLOCK_MANAGER.getBlock(blockId).getBlockId(); - sendBlockChange(chunk, x, y, z, id); + // Refresh player chunk block + sendBlockChange(chunk, blockPosition, blockId); } } @@ -94,22 +95,23 @@ public class InstanceContainer extends Instance { synchronized (chunk) { chunk.refreshBlockValue(x, y, z, blockId); - sendBlockChange(chunk, x, y, z, blockId); + BlockPosition blockPosition = new BlockPosition(x, y, z); + sendBlockChange(chunk, blockPosition, blockId); } } - private void callBlockDestroy(Chunk chunk, int index, int x, int y, int z) { + private void callBlockDestroy(Chunk chunk, int index, BlockPosition blockPosition) { CustomBlock previousBlock = chunk.getCustomBlock(index); if (previousBlock != null) { Data previousData = chunk.getData(index); - previousBlock.onDestroy(this, new BlockPosition(x, y, z), previousData); + previousBlock.onDestroy(this, blockPosition, previousData); } } - private void callBlockPlace(Chunk chunk, int index, int x, int y, int z) { + private void callBlockPlace(Chunk chunk, int index, BlockPosition blockPosition) { CustomBlock actualBlock = chunk.getCustomBlock(index); Data previousData = chunk.getData(index); - actualBlock.onPlace(this, new BlockPosition(x, y, z), previousData); + actualBlock.onPlace(this, blockPosition, previousData); } private short executeBlockPlacementRule(short blockId, BlockPosition blockPosition) { @@ -148,22 +150,19 @@ public class InstanceContainer extends Instance { public void breakBlock(Player player, BlockPosition blockPosition) { Chunk chunk = getChunkAt(blockPosition); - int blockX = blockPosition.getX(); - int blockY = blockPosition.getY(); - int blockZ = blockPosition.getZ(); + int x = blockPosition.getX(); + int y = blockPosition.getY(); + int z = blockPosition.getZ(); - short blockId = chunk.getBlockId(blockX, blockY, blockZ); + short blockId = chunk.getBlockId(x, y, z); if (blockId == 0) { - sendChunkSectionUpdate(chunk, ChunkUtils.getSectionAt(blockPosition.getY()), player); + sendChunkSectionUpdate(chunk, ChunkUtils.getSectionAt(y), player); return; } PlayerBlockBreakEvent blockBreakEvent = new PlayerBlockBreakEvent(blockPosition); player.callEvent(PlayerBlockBreakEvent.class, blockBreakEvent); if (!blockBreakEvent.isCancelled()) { - int x = blockPosition.getX(); - int y = blockPosition.getY(); - int z = blockPosition.getZ(); // Break or change the broken block based on event result short resultBlockId = blockBreakEvent.getResultBlock(); @@ -355,9 +354,9 @@ public class InstanceContainer extends Instance { this.folder = folder; } - private void sendBlockChange(Chunk chunk, int x, int y, int z, short blockId) { + private void sendBlockChange(Chunk chunk, BlockPosition blockPosition, short blockId) { BlockChangePacket blockChangePacket = new BlockChangePacket(); - blockChangePacket.blockPosition = new BlockPosition(x, y, z); + blockChangePacket.blockPosition = blockPosition; blockChangePacket.blockId = blockId; chunk.sendPacketToViewers(blockChangePacket); } diff --git a/src/main/java/net/minestom/server/instance/SharedInstance.java b/src/main/java/net/minestom/server/instance/SharedInstance.java index 3aea2d4f1..1b3eb4acf 100644 --- a/src/main/java/net/minestom/server/instance/SharedInstance.java +++ b/src/main/java/net/minestom/server/instance/SharedInstance.java @@ -139,11 +139,11 @@ public class SharedInstance extends Instance { } @Override - public void setCustomBlock(int x, int y, int z, short blockId, Data data) { - instanceContainer.setBlock(x, y, z, blockId, data); + public void setCustomBlock(int x, int y, int z, short customBlockId, Data data) { + instanceContainer.setBlock(x, y, z, customBlockId, data); } - public InstanceContainer getContainer() { + public InstanceContainer getInstanceContainer() { return instanceContainer; } } diff --git a/src/main/java/net/minestom/server/instance/block/BlockManager.java b/src/main/java/net/minestom/server/instance/block/BlockManager.java index c50176d84..389af51df 100644 --- a/src/main/java/net/minestom/server/instance/block/BlockManager.java +++ b/src/main/java/net/minestom/server/instance/block/BlockManager.java @@ -9,16 +9,16 @@ import java.util.Map; public class BlockManager { - private Short2ObjectMap blocksInternalId = new Short2ObjectOpenHashMap<>(); - private Map blocksId = new HashMap<>(); + private Short2ObjectMap customBlocksInternalId = new Short2ObjectOpenHashMap<>(); + private Map customBlocksId = new HashMap<>(); private Short2ObjectOpenHashMap placementRules = new Short2ObjectOpenHashMap<>(); public void registerCustomBlock(CustomBlock customBlock) { String identifier = customBlock.getIdentifier(); short id = customBlock.getId(); - this.blocksInternalId.put(id, customBlock); - this.blocksId.put(identifier, customBlock); + this.customBlocksInternalId.put(id, customBlock); + this.customBlocksId.put(identifier, customBlock); } public void registerBlockPlacementRule(BlockPlacementRule blockPlacementRule) { @@ -35,12 +35,12 @@ public class BlockManager { return getBlockPlacementRule(block.getBlockId()); } - public CustomBlock getBlock(String identifier) { - return blocksId.get(identifier); + public CustomBlock getCustomBlock(String identifier) { + return customBlocksId.get(identifier); } - public CustomBlock getBlock(short id) { - return blocksInternalId.get(id); + public CustomBlock getCustomBlock(short id) { + return customBlocksInternalId.get(id); } } diff --git a/src/main/java/net/minestom/server/network/netty/channel/ClientChannel.java b/src/main/java/net/minestom/server/network/netty/channel/ClientChannel.java index a919c40a8..8e21c7296 100644 --- a/src/main/java/net/minestom/server/network/netty/channel/ClientChannel.java +++ b/src/main/java/net/minestom/server/network/netty/channel/ClientChannel.java @@ -56,7 +56,6 @@ public class ClientChannel extends ChannelInboundHandlerAdapter { @Override public void channelInactive(ChannelHandlerContext ctx) { - System.out.println("DISCONNECTION"); PlayerConnection playerConnection = packetProcessor.getPlayerConnection(ctx); if (playerConnection != null) { playerConnection.refreshOnline(false);