Reputation: 1
I wanted to save to two variables the locations of the blocks the player clicked on.
I've tried to start by triggering an event after using the item but the event only generates when I click in the air.
if (p.getItemInHand().getType() == Material.BLAZE_ROD) { System.out.println("TEST"); }
I also tried this design but the code still does not work properly:
if ((p.getItemInHand().getType() == Material.BLAZE_ROD) && (event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) { System.out.println("TEST"); }
In summary, I want to write the locations of two indicated blocks to variables. One when right clicked with a given item and the other when left clicked with the same item.
I haven't searched yet but I'll ask right away, how can I check if a block at the given coordinates exists (is it empty, is it air) and how can I set or replace selected block at the given coordinates with another one?
Upvotes: 0
Views: 892
Reputation: 1243
You can achieve this via PlayerInteractEvent
, Action
, Material
and Location
. An example would be the following:
import org.bukkit.Location;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.block.Action;
import org.bukkit.block.Block;
import org.bukkit.Material;
public class YourListener {
private Location firstLocation;
private Location secondLocation;
@EventHandler
public void onPlayerInteractEvent(PlayerInteractEvent event) {
// Only process when the player has a wooden axe in the hand.
if (event.getMaterial() == Material.WOODEN_AXE) {
Action action = event.getAction();
Block clickedBlock = event.getClickedBlock();
if (clickedBlock == null) return;
if (action == Action.LEFT_CLICK_AIR || action == Action.LEFT_CLICK_BLOCK) {
// Do one thing (left click)
Material type = clickedBlock.getType(); // Block material (Check if it's Material.AIR or another type)
firstLocation = clickedBlock.getLocation(); // Save the location
} else if (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK){
// Do another thing (right click)
Material type = clickedBlock.getType(); // Block material (Check if it's Material.AIR or another type)
secondLocation = clickedBlock.getLocation(); // Save location
// Let's say you now want to replace the block material to a diamond block:
clickedBlock.setType(Material.DIAMOND_BLOCK);
}
}
}
}
The PlayerInteractEvent
has the method getMaterial()
which returns:
Returns the material of the item represented by this event
(The material of the item in the player's hand)
Then the getAction()
method returns one of the following enum entries
Action.LEFT_CLICK_AIR
: Left click on airAction.LEFT_CLICK_BLOCK
: Left click on blockAction.RIGHT_CLICK_AIR
: Right click on airAction.RIGHT_CLICK_BLOCK
: Right click on blockThe getClickedBlock()
method returns the block that the player has clicked to. Then you can use the methods getType()
and setType(Material)
to get and set the material of that block.
Finally the getLocation()
method from Block
will return the location of that block.
Make sure to read all documentation about this classes, enums and interfaces:
Upvotes: 1