Custom blocks can block item interaction, prevents blocks being placed when opening containers

This commit is contained in:
jglrxavpok 2020-04-28 18:12:06 +02:00
parent 6d9ac0b35d
commit 85da9203b1
5 changed files with 36 additions and 6 deletions

View File

@ -25,8 +25,8 @@ public class StoneBlock extends CustomBlock {
}
@Override
public void onInteract(Player player, Player.Hand hand, BlockPosition blockPosition, Data data) {
public boolean onInteract(Player player, Player.Hand hand, BlockPosition blockPosition, Data data) {
return false;
}
@Override

View File

@ -33,8 +33,8 @@ public class UpdatableBlockDemo extends CustomBlock {
}
@Override
public void onInteract(Player player, Player.Hand hand, BlockPosition blockPosition, Data data) {
public boolean onInteract(Player player, Player.Hand hand, BlockPosition blockPosition, Data data) {
return false;
}
@Override

View File

@ -8,11 +8,25 @@ public class PlayerBlockInteractEvent extends CancellableEvent {
private BlockPosition blockPosition;
private Player.Hand hand;
/**
* Does this interaction block the normal item use?
* True for containers which open an inventory instead of letting blocks be placed
*/
private boolean blocksItemUse;
public PlayerBlockInteractEvent(BlockPosition blockPosition, Player.Hand hand) {
this.blockPosition = blockPosition;
this.hand = hand;
}
public boolean isBlockingItemUse() {
return blocksItemUse;
}
public void setBlockingItemUse(boolean blocks) {
this.blocksItemUse = blocks;
}
public BlockPosition getBlockPosition() {
return blockPosition;
}

View File

@ -33,7 +33,16 @@ public abstract class CustomBlock {
public abstract void onDestroy(Instance instance, BlockPosition blockPosition, Data data);
public abstract void onInteract(Player player, Player.Hand hand, BlockPosition blockPosition, Data data);
/**
* Handles interactions with this block. Can also block normal item use (containers should block when opening the
* menu, this prevents the player from placing a block when opening it for instance)
* @param player the player interacting
* @param hand the hand used to interact
* @param blockPosition the position of this block
* @param data the data at this position
* @return true if this block blocks normal item use, false otherwise
*/
public abstract boolean onInteract(Player player, Player.Hand hand, BlockPosition blockPosition, Data data);
public abstract UpdateOption getUpdateOption();

View File

@ -42,10 +42,17 @@ public class BlockPlacementListener {
CustomBlock customBlock = instance.getCustomBlock(blockPosition);
if (customBlock != null) {
Data data = instance.getBlockData(blockPosition);
customBlock.onInteract(player, hand, blockPosition, data);
boolean blocksItem = customBlock.onInteract(player, hand, blockPosition, data);
if(blocksItem) {
playerBlockInteractEvent.setBlockingItemUse(true);
}
}
});
if(playerBlockInteractEvent.isBlockingItemUse()) {
return;
}
// Check if item at hand is a block
ItemStack usedItem = hand == Player.Hand.MAIN ? playerInventory.getItemInMainHand() : playerInventory.getItemInOffHand();
Material material = Material.fromId(usedItem.getMaterialId());