Neighbor updates

This commit is contained in:
jglrxavpok 2020-05-01 22:08:31 +02:00
parent 08a32c4492
commit f38d3718c3
4 changed files with 34 additions and 1 deletions

View File

@ -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;

View File

@ -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);
}
}
}
}

View File

@ -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));
}
}
}

View File

@ -1,5 +1,6 @@
package net.minestom.server.utils;
// TODO: pool block positions?
public class BlockPosition {
private int x, y, z;