Solved Right Click Specific Block with Specific Item

Discussion in 'Plugin Development' started by EpicCraft21, Jan 11, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    EpicCraft21

    Hi guys, what is the code where if I click (for example) a diamond block with a diamond, it would give you an emerald or something like that.

    My code so far:

    Code:
    package me.epiccraft21.core;
    
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin{
     
    @EventHandler
    public void onBlockClicked(Block block, Location loc, PlayerInteractEvent e){
        Player p = e.getPlayer();
        if(block.getType() == Material.DIAMOND_BLOCK){
            if(p.getItemInHand().getType() == Material.DIAMOND){
                p.getInventory().addItem(new ItemStack(Material.EMERALD, 1));
                p.getInventory().removeItem(new ItemStack(Material.DIAMOND));
            }
        }
    
     
    Last edited: Jan 11, 2015
  2. Offline

    ahamling27

    Here's a tip! Using Pastebin or the 'Insert Code' tool above helps format your code for easier reading! :)

    Edit: Thanks for updating your thread! :D
     
    Last edited: Jan 11, 2015
  3. Offline

    teej107

    @EpicCraft21
    Yeah. I'm not even trying to read the code when it's like that. Make things easier to read so people will be more likely to help you.
     
    ahamling27 likes this.
  4. Offline

    EpicCraft21

    Ok, sorry. I didn't know about that.
     
    ahamling27 likes this.
  5. Offline

    16davidjm6

    public class Main extends JavaPlugin implements Listener

    Adding the "implements Listener" part allows you to listen to events in this class.

    You also need to register the listener so bukkit knows it exists. Add this method:

    Code:
    public void onEnable(){
      this.getServer().getPluginManager().registerEvents(this, this);
    }
     
    EpicCraft21 likes this.
  6. Offline

    Krizeh

    Make sure your class implements Listener and that the event is registered.

    If you're having problems with your EventHandler try using this:

    Code:
      @EventHandler
      public void onClickBlock(PlayerInteractEvent e) {
          Player p = e.getPlayer();
          if (p.getItemInHand().getType().equals(Material.DIAMOND)) {
          if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
              Block b = e.getClickedBlock();
              if (b.getType().equals(Material.DIAMOND_BLOCK)) {
                  if (p.getItemInHand().getAmount() > 1) {
                      p.getItemInHand().setAmount(p.getItemInHand().getAmount() -1);
                      p.getInventory().addItem(new ItemStack(Material.EMERALD));
                  } else {
                      p.getInventory().remove(p.getItemInHand());
                      p.getInventory().addItem(new ItemStack(Material.EMERALD));
                  }
              }
          }
      }
      }
     
    EpicCraft21 likes this.
  7. Offline

    teej107

  8. Offline

    EpicCraft21

    Thanks, this worked perfectly!
     
Thread Status:
Not open for further replies.

Share This Page