From f38d3718c36838a187b39e5a4f785849b846c28d Mon Sep 17 00:00:00 2001 From: jglrxavpok Date: Fri, 1 May 2020 22:08:31 +0200 Subject: [PATCH] Neighbor updates --- .../java/fr/themode/demo/blocks/StoneBlock.java | 9 ++++++++- .../minestom/server/instance/InstanceContainer.java | 12 ++++++++++++ .../minestom/server/instance/block/CustomBlock.java | 13 +++++++++++++ .../net/minestom/server/utils/BlockPosition.java | 1 + 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/main/java/fr/themode/demo/blocks/StoneBlock.java b/src/main/java/fr/themode/demo/blocks/StoneBlock.java index 798ea419d..ff4dcb859 100644 --- a/src/main/java/fr/themode/demo/blocks/StoneBlock.java +++ b/src/main/java/fr/themode/demo/blocks/StoneBlock.java @@ -15,7 +15,7 @@ public class StoneBlock extends CustomBlock { @Override public void onPlace(Instance instance, BlockPosition blockPosition, Data data) { - System.out.println("PLACED"); + System.out.println("PLACED at "+blockPosition); } @Override @@ -23,6 +23,13 @@ public class StoneBlock extends CustomBlock { } + @Override + public void updateFromNeighbor(Instance instance, BlockPosition thisPosition, BlockPosition neighborPosition, boolean directNeighbor) { + if(directNeighbor) { + System.out.println("Block at "+thisPosition+" has been updated by neighbor at "+neighborPosition); + } + } + @Override public boolean onInteract(Player player, Player.Hand hand, BlockPosition blockPosition, Data data) { return false; diff --git a/src/main/java/net/minestom/server/instance/InstanceContainer.java b/src/main/java/net/minestom/server/instance/InstanceContainer.java index 5a0d446a8..b4768dab8 100644 --- a/src/main/java/net/minestom/server/instance/InstanceContainer.java +++ b/src/main/java/net/minestom/server/instance/InstanceContainer.java @@ -154,6 +154,18 @@ public class InstanceContainer extends Instance { refreshBlockId(neighborX, neighborY, neighborZ, newNeighborId); } } + + // Update neighbors + CustomBlock customBlock = getCustomBlock(neighborX, neighborY, neighborZ); + if(customBlock != null) { + boolean directNeighbor = false; // only if directly connected to neighbor (no diagonals) + if(offsetX != 0 ^ offsetZ != 0) { + directNeighbor = offsetY == 0; + } else if(offsetX == 0 && offsetZ == 0) { + directNeighbor = true; + } + customBlock.updateFromNeighbor(this, new BlockPosition(neighborX, neighborY, neighborZ), blockPosition, directNeighbor); + } } } } diff --git a/src/main/java/net/minestom/server/instance/block/CustomBlock.java b/src/main/java/net/minestom/server/instance/block/CustomBlock.java index d10d65fbe..106c60b86 100644 --- a/src/main/java/net/minestom/server/instance/block/CustomBlock.java +++ b/src/main/java/net/minestom/server/instance/block/CustomBlock.java @@ -91,4 +91,17 @@ public abstract class CustomBlock { public Data createData(Instance instance, BlockPosition blockPosition, Data data) { return data; } + + /** + * Update this block from a neighbor. By default calls 'update' if directNeighbor is true + * @param instance current instance + * @param thisPosition this block's position + * @param neighborPosition the neighboring block which triggered the update + * @param directNeighbor is the neighbor directly connected to this block? (No diagonals) + */ + public void updateFromNeighbor(Instance instance, BlockPosition thisPosition, BlockPosition neighborPosition, boolean directNeighbor) { + if(directNeighbor) { + update(instance, thisPosition, instance.getBlockData(thisPosition)); + } + } } diff --git a/src/main/java/net/minestom/server/utils/BlockPosition.java b/src/main/java/net/minestom/server/utils/BlockPosition.java index b644d9af8..316f6902c 100644 --- a/src/main/java/net/minestom/server/utils/BlockPosition.java +++ b/src/main/java/net/minestom/server/utils/BlockPosition.java @@ -1,5 +1,6 @@ package net.minestom.server.utils; +// TODO: pool block positions? public class BlockPosition { private int x, y, z;