Left Click Block/Right Click Block

Discussion in 'Plugin Development' started by SamTMH, Mar 21, 2018.

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

    SamTMH

    So my problem is that the left click block action is working properly but my right click action is not. The left click action runs once like it should but the right click action runs twice. As you can see in the first image my health goes from 2 to 3 when I left click but when I right click you see in the second image my heath goes from 3 to 5 and the chat has the message sent twice. All I want is for the right click action to run once.

    Screenshot (1).png Screenshot (2).png

    Code:
    package me.samtmh.Test3.Events;
    
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    
    import net.md_5.bungee.api.ChatColor;
    
    public class EventsClass implements Listener {
    
        @EventHandler
        public void onInteract(PlayerInteractEvent event) {
            Action action = event.getAction();
            Player player = event.getPlayer();
            Block block = event.getClickedBlock();
    
            if (action.equals(Action.LEFT_CLICK_BLOCK)) {
                if (block.getType().equals(Material.EMERALD_BLOCK)) {
                    if (player.getHealth() != 20) {
                        player.setHealth(player.getHealth() + 1);
                        player.sendMessage(ChatColor.GREEN + "You have been healed");
                    } else {
                        player.sendMessage(ChatColor.GOLD + "You have full health");
                    }
                } else {
                    player.sendMessage(
                            ChatColor.RED + "You clicked: " + ChatColor.RED + block.getType().toString().toUpperCase());
                }
            }
            if (action.equals(Action.RIGHT_CLICK_BLOCK)) {
                if (block.getType().equals(Material.EMERALD_BLOCK)) {
                    if (player.getHealth() != 20) {
                        player.setHealth(player.getHealth() + 1);
                        player.sendMessage(ChatColor.GREEN + "You have been healed");
                    } else {
                        player.sendMessage(ChatColor.GOLD + "You have full health");
                    }
                } else {
                    player.sendMessage(
                            ChatColor.RED + "You clicked: " + ChatColor.RED + block.getType().toString().toUpperCase());
                }
            }
        }
    }
    
     
  2. Offline

    timtower Administrator Administrator Moderator

    @SamTMH This is probably due to the main hand and offhand.
     
  3. Offline

    SamTMH

    Could you elaborate more on that?

     
  4. Offline

    timtower Administrator Administrator Moderator

    @SamTMH You have a main hand and an offhand.
    You need to check which hand is being used with getHand()
     
  5. Offline

    SamTMH

    Oh okay. So when I right click it may be triggering twice because of the right click action from my main hand and my off hand? How should I implement that in the code, I've never actually worked with main hand/offhand actions. The last time I made a plugin was in 1.8 (I know it's been so long). I'm rusty at this now.

     
  6. Offline

    timtower Administrator Administrator Moderator

    @SamTMH if(event.getHand()==mainhand){
    //do whatever you need to do
    }
     
  7. Offline

    SamTMH

    Thank you for this information, I'm happy I know about this main hand/offhand stuff now. This will make plugins a lot easier, this isn't the first time I've ran into this problem this week. I'll update the plugin in the afternoon and say if it worked or not. For the rest of the code does it look good or is there a better way to approach what I'm doing?

     
  8. Offline

    timtower Administrator Administrator Moderator

    @SamTMH I would use the max health instead of the hardcoded 20
     
  9. Offline

    SamTMH

    Okay so change the
    Code:
    if (player.getHealth() != 20) {
    to
    Code:
    if (player.getMaxHealth() {
    ?

     
  10. Offline

    timtower Administrator Administrator Moderator

    @SamTMH You replace the 20 by player.getMaxHealth()
     
  11. Offline

    SamTMH

    Okay good to know. I will edit the plugin when I wake up and hopefully everything works out the way I want it to. Thank you for this information.

    I am having trouble adding the if statement for the mainhand. What type of variable should I be using for this to work?

    EDIT: I figured it out, I was writing the code wrong. Instead of writing
    Code:
    if (event.getHand() == EquipmentSlot.HAND) {
    I was writing
    Code:
    if (event.getHand() == equipmentslot.HAND) {
    Thank you for all of your help, I appreciate it. Should I make any other changes to this code or do you think it's good now.

    Code:
    package me.samtmh.Test3.Events;
    
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.EquipmentSlot;
    
    import net.md_5.bungee.api.ChatColor;
    
    public class EventsClass implements Listener {
        @SuppressWarnings("deprecation")
    
        @EventHandler
        public void onInteract(PlayerInteractEvent event) {
            Action action = event.getAction();
            Player player = event.getPlayer();
            Block block = event.getClickedBlock();
    
            if (event.getHand() == EquipmentSlot.HAND) {
                if (action.equals(Action.LEFT_CLICK_BLOCK)) {
                    if (block.getType().equals(Material.EMERALD_BLOCK)) {
                        if (player.getHealth() != player.getMaxHealth()) {
                            player.setHealth(player.getHealth() + 1);
                            player.sendMessage(ChatColor.GREEN + "You have been healed");
                        } else {
                            player.sendMessage(ChatColor.GOLD + "You have full health");
                        }
                    } else {
                        player.sendMessage(
                                ChatColor.RED + "You clicked: " + ChatColor.RED + block.getType().toString().toUpperCase());
                    }
                }
                if (event.getHand() == EquipmentSlot.HAND) {
                    if (action.equals(Action.RIGHT_CLICK_BLOCK)) {
                        if (block.getType().equals(Material.EMERALD_BLOCK)) {
                            if (player.getHealth() != player.getMaxHealth()) {
                                player.setHealth(player.getHealth() + 1);
                                player.sendMessage(ChatColor.GREEN + "You have been healed");
                            } else {
                                player.sendMessage(ChatColor.GOLD + "You have full health");
                            }
                        } else {
                            player.sendMessage(ChatColor.RED + "You clicked: " + ChatColor.RED
                                    + block.getType().toString().toUpperCase());
                        }
                    }
                }
            }
        }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Mar 21, 2018
    timtower likes this.
Thread Status:
Not open for further replies.

Share This Page