mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-26 11:07:53 +01:00
Neighbor updates
This commit is contained in:
parent
08a32c4492
commit
f38d3718c3
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package net.minestom.server.utils;
|
||||
|
||||
// TODO: pool block positions?
|
||||
public class BlockPosition {
|
||||
|
||||
private int x, y, z;
|
||||
|
Loading…
Reference in New Issue
Block a user