'Bukkit - Change block on click (client side)
What I'm trying to do is to replace a block into a diamond block on client side when a player right clicks it
public class setBlock implements Listener {
@EventHandler
public void onBlockClick(PlayerInteractEvent event) {
if (event.getHand()== EquipmentSlot.HAND) {
Player player = event.getPlayer();
BlockData block = Material.DIAMOND_BLOCK.createBlockData();
Location loc = event.getClickedBlock().getLocation();
player.sendBlockChange(loc, block);
}
}
}
Can I get some help please?
Solution 1:[1]
I would recommend to check if you clicked right and if the location of the clicked block is even not null.
public class setBlock implements Listener {
@EventHandler
public void onBlockClick(PlayerInteractEvent event) {
if (event.getClickedBlock() == null) return;
if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
Player player = event.getPlayer();
BlockData block = Material.DIAMOND_BLOCK.createBlockData();
Location loc = event.getClickedBlock().getLocation();
player.sendBlockChange(loc, block);
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Levi Heßmann |