diff --git a/src/main/java/fr/themode/demo/PlayerInit.java b/src/main/java/fr/themode/demo/PlayerInit.java index f4f63ffa3..950cf8c34 100644 --- a/src/main/java/fr/themode/demo/PlayerInit.java +++ b/src/main/java/fr/themode/demo/PlayerInit.java @@ -122,6 +122,14 @@ public class PlayerInit { }); + player.addEventCallback(PlayerBlockInteractEvent.class, event -> { + if (event.getHand() != Player.Hand.MAIN) + return; + + short blockId = player.getInstance().getBlockId(event.getBlockPosition()); + player.sendMessage("block alternative id: " + Block.getBlockAlternative(blockId).getId()); + }); + player.addEventCallback(PickupItemEvent.class, event -> { event.setCancelled(!player.getInventory().addItemStack(event.getItemStack())); // Cancel event if player does not have enough inventory space }); @@ -163,7 +171,7 @@ public class PlayerInit { player.openInventory(inventory); - player.getInventory().addItemStack(new ItemStack((short) 1, (byte) 100)); + player.getInventory().addItemStack(new ItemStack(Material.STONE, (byte) 100)); player.getInventory().addItemStack(new ItemStack(Material.DIAMOND_CHESTPLATE, (byte) 1)); /*TeamManager teamManager = Main.getTeamManager(); diff --git a/src/main/java/fr/themode/demo/blocks/StoneBlock.java b/src/main/java/fr/themode/demo/blocks/StoneBlock.java index b5a2d95ef..7a500935c 100644 --- a/src/main/java/fr/themode/demo/blocks/StoneBlock.java +++ b/src/main/java/fr/themode/demo/blocks/StoneBlock.java @@ -6,7 +6,6 @@ import net.minestom.server.instance.Instance; import net.minestom.server.instance.block.Block; import net.minestom.server.instance.block.CustomBlock; import net.minestom.server.utils.BlockPosition; -import net.minestom.server.utils.time.UpdateOption; public class StoneBlock extends CustomBlock { @@ -17,6 +16,7 @@ public class StoneBlock extends CustomBlock { @Override public void onPlace(Instance instance, BlockPosition blockPosition, Data data) { System.out.println("PLACED"); + instance.refreshBlockId(blockPosition, Block.LAVA); } @Override @@ -29,11 +29,6 @@ public class StoneBlock extends CustomBlock { return false; } - @Override - public UpdateOption getUpdateOption() { - return null; - } - @Override public int getBreakDelay(Player player) { return 750; diff --git a/src/main/java/fr/themode/demo/generator/NoiseTestGenerator.java b/src/main/java/fr/themode/demo/generator/NoiseTestGenerator.java index 4531cef82..f38da1e7d 100644 --- a/src/main/java/fr/themode/demo/generator/NoiseTestGenerator.java +++ b/src/main/java/fr/themode/demo/generator/NoiseTestGenerator.java @@ -29,7 +29,7 @@ public class NoiseTestGenerator extends ChunkGenerator { if (random.nextInt(100) > 10) { batch.setCustomBlock(x, y, z, "custom_block"); } else { - batch.setBlock(x, y, z, Block.DIAMOND_ORE); + batch.setBlock(x, y, z, Block.DIAMOND_BLOCK); } } } diff --git a/src/main/java/net/minestom/server/instance/Instance.java b/src/main/java/net/minestom/server/instance/Instance.java index 56f7a72fa..8ee1592a4 100644 --- a/src/main/java/net/minestom/server/instance/Instance.java +++ b/src/main/java/net/minestom/server/instance/Instance.java @@ -47,7 +47,7 @@ public abstract class Instance implements BlockModifier, DataContainer { this.dimension = dimension; } - public abstract void refreshBlockId(int x, int y, int z, short blockId); + public abstract void refreshBlockId(BlockPosition blockPosition, short blockId); // Used to call BlockBreakEvent and sending particle packet if true public abstract void breakBlock(Player player, BlockPosition blockPosition); @@ -153,10 +153,18 @@ public abstract class Instance implements BlockModifier, DataContainer { return Collections.unmodifiableSet(getEntitiesInChunk(ChunkUtils.getChunkIndex(chunk.getChunkX(), chunk.getChunkZ()))); } + public void refreshBlockId(int x, int y, int z, short blockId) { + refreshBlockId(new BlockPosition(x, y, z), blockId); + } + public void refreshBlockId(int x, int y, int z, Block block) { refreshBlockId(x, y, z, block.getBlockId()); } + public void refreshBlockId(BlockPosition blockPosition, Block block) { + refreshBlockId(blockPosition, block.getBlockId()); + } + public void loadChunk(int chunkX, int chunkZ) { loadChunk(chunkX, chunkZ, null); } diff --git a/src/main/java/net/minestom/server/instance/InstanceContainer.java b/src/main/java/net/minestom/server/instance/InstanceContainer.java index 004d04a37..fcc5c747f 100644 --- a/src/main/java/net/minestom/server/instance/InstanceContainer.java +++ b/src/main/java/net/minestom/server/instance/InstanceContainer.java @@ -84,22 +84,22 @@ public class InstanceContainer extends Instance { // Refresh neighbors since a new block has been placed executeNeighboursBlockPlacementRule(blockPosition); + // Refresh player chunk block + sendBlockChange(chunk, blockPosition, blockId); + // Call the place listener for custom block if (isCustomBlock) callBlockPlace(chunk, index, blockPosition); - - // Refresh player chunk block - sendBlockChange(chunk, blockPosition, blockId); } } @Override - public void refreshBlockId(int x, int y, int z, short blockId) { - Chunk chunk = getChunkAt(x, z); + public void refreshBlockId(BlockPosition blockPosition, short blockId) { + Chunk chunk = getChunkAt(blockPosition.getX(), blockPosition.getZ()); synchronized (chunk) { - chunk.refreshBlockValue(x, y, z, blockId); + chunk.refreshBlockValue(blockPosition.getX(), blockPosition.getY(), + blockPosition.getZ(), blockId); - BlockPosition blockPosition = new BlockPosition(x, y, z); sendBlockChange(chunk, blockPosition, blockId); } } diff --git a/src/main/java/net/minestom/server/instance/SharedInstance.java b/src/main/java/net/minestom/server/instance/SharedInstance.java index 3f960ad4d..09c5680c3 100644 --- a/src/main/java/net/minestom/server/instance/SharedInstance.java +++ b/src/main/java/net/minestom/server/instance/SharedInstance.java @@ -25,8 +25,8 @@ public class SharedInstance extends Instance { } @Override - public void refreshBlockId(int x, int y, int z, short blockId) { - instanceContainer.refreshBlockId(x, y, z, blockId); + public void refreshBlockId(BlockPosition blockPosition, short blockId) { + instanceContainer.refreshBlockId(blockPosition, blockId); } @Override diff --git a/src/main/java/net/minestom/server/instance/block/Block.java b/src/main/java/net/minestom/server/instance/block/Block.java index b799ba432..d92387208 100644 --- a/src/main/java/net/minestom/server/instance/block/Block.java +++ b/src/main/java/net/minestom/server/instance/block/Block.java @@ -690,12 +690,24 @@ public enum Block { HONEY_BLOCK, HONEYCOMB_BLOCK; + // Used to get a Block from any block alternatives id private static Short2ObjectOpenHashMap blocksMap = new Short2ObjectOpenHashMap<>(); + // Used to retrieve the appropriate BlockAlternative from any block id + private static Short2ObjectOpenHashMap blocksAlternativesMap = new Short2ObjectOpenHashMap<>(); + public static Block fromId(short blockId) { return blocksMap.getOrDefault(blockId, AIR); } + public static BlockAlternative getBlockAlternative(short blockId) { + return blocksAlternativesMap.get(blockId); + } + + public static void registerBlockAlternative(BlockAlternative blockAlternative) { + blocksAlternativesMap.put(blockAlternative.id, blockAlternative); + } + private short blockId; private List blockAlternatives = new ArrayList<>(); @@ -703,9 +715,9 @@ public enum Block { this.blockId = blockId; } - public void addBlockAlternative(short id, String... properties) { - this.blockAlternatives.add(new BlockAlternative(id, properties)); - blocksMap.put(id, this); + public void addBlockAlternative(BlockAlternative blockAlternative) { + this.blockAlternatives.add(blockAlternative); + blocksMap.put(blockAlternative.id, this); } public short withProperties(String... properties) { @@ -1362,7 +1374,7 @@ public enum Block { private short id; private String[] properties; - protected BlockAlternative(short id, String... properties) { + public BlockAlternative(short id, String... properties) { this.id = id; this.properties = properties; } diff --git a/src/main/java/net/minestom/server/registry/RegistryMain.java b/src/main/java/net/minestom/server/registry/RegistryMain.java index 56972a03d..d89173946 100644 --- a/src/main/java/net/minestom/server/registry/RegistryMain.java +++ b/src/main/java/net/minestom/server/registry/RegistryMain.java @@ -53,7 +53,11 @@ public class RegistryMain { for (RegistryBlock.BlockState blockState : registryBlock.states) { short id = blockState.id; String[] properties = blockState.propertiesValues.toArray(new String[blockState.propertiesValues.size()]); - block.addBlockAlternative(id, properties); + Block.BlockAlternative blockAlternative = new Block.BlockAlternative(id, properties); + + block.addBlockAlternative(blockAlternative); + + Block.registerBlockAlternative(blockAlternative); } } }