Double right click/accidental left click

Discussion in 'Plugin Development' started by Quint2597, Jun 15, 2020.

Thread Status:
Not open for further replies.
  1. Heya! I'm fairly new to Java and making plugins as a whole, so sorry if I accidentally use any wrong terminology or anything.

    I don't really know how to segue, so here's the issue:

    I'm making a base/prototype for a later plugin, and I currently two (three, kind of) issues with my current code. Here they are:
    1. When right clicking a block using with the proper item it will register a right click twice (this may be an issue on my end, actually. I've had issues with this using different plugins on different servers)
    2. When left clicking a block or an air block it will also register a right click.

    Some additional info: there's only one class (Main), and I'm using Eclipse IDE. I'm using Spigot 1.12.2 and am running 1.12.2 on my client. The server is hosted off of my Macbook Air (running on High Sierra) laptop and I'm connecting using ip: "localhost". There are no plugin.yml errors, and all my keys are on default. There are no syntax errors.


    (Sorry if I mess up the formatting here)

    Code:
    package me.quint.quintitem;
    
    
    
    
    
    import org.bukkit.entity.Player;
    
    import org.bukkit.event.EventHandler;
    
    import org.bukkit.event.Listener;
    
    import org.bukkit.event.player.PlayerInteractEvent;
    
    import org.bukkit.inventory.ItemStack;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    
    publicclassMainextendsJavaPluginimplementsListener{
    
    
    
        publicvoidonEnable() {
    
            getServer().getPluginManager().registerEvents(this, this);
    
        }
    
    
    
        publicvoidonDisable() {
    
        
    
        }
    
    
    
        @EventHandler
    
        publicvoidrightClick(PlayerInteractEventevent) {
    
        
    
            Player thePlayer = event.getPlayer();
    
        
    
        ItemStackheld = thePlayer.getInventory().getItemInMainHand();
    
    
    
        if (held != null && held.getItemMeta().getLore().contains("Weapon")) {
    
        
    
        
    
            thePlayer.sendMessage("Right clicked with item lored: Weapon");
    
        
    
        
    
        }
    
        
    
        
    
        
    
        }
    
    
    
    
    
    }
    [code]
     
  2. Offline

    TeaMaker

    Maybe try adding a delay, like using a Hashmap to store a player tick till next register, if the player is on that map, you return and not do anything with the event. use a runnable to continue loop through all the hashmap player and decrease by one. see if that works.
     
  3. Offline

    gochi9

    This is a little messy.
    Ok so let's start from the begging

    1. You get the player

    2. You want to check if the player is holding an item if not ignore it. Example
    Code:
    if(event.getItem() == null || event.getItem().getType() == Material.AIR) return; 
    3. Then you get the item and you can get it more simply by using the event function for that
    Code:
    ItemStack held = event.getItem();
    4.Since you want to check if item has a specific lore you first check if the item has an ItemMeta so you will ignore the default minecraft items with no specific names/lores,etc
    Code:
    if(!held.hasItemMeta()) return;
    5.Check if the item has your disered meta if not ignore it.
    Code:
    if (!held.getItemMeta().getLore().contains("Weapon")) return;
    6.Now when you right click you actually use your both hands,so you want to check if it uses only the right hand.If not then ignore

    Code:
    if(event.getHand() != EquipmentSlot.HAND) return;
    7.Now you check if the player rightCliked
    Code:
    if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) return;
    8.Send your message
    Code:
    thePlayer.sendMessage("Right clicked with item lored: Weapon");
    9.Lastly.I noticed that your layout is a bit off. Here is how the final product should look and it looks nice
    Code:
        @EventHandler
        public void rightClick(PlayerInteractEvent event) {
            Player thePlayer = event.getPlayer();
          
            //Here you check if the item is null
            if(event.getItem() == null || event.getItem().getType() == Material.AIR) return;
          
            ItemStack held = event.getItem();
          
            //Since you want to check if your item has a specific lore you have to check if the item has an item meta
            if(!held.hasItemMeta()) return;
          
            //Here you check if your item has a specific item meta
            if (!held.getItemMeta().getLore().contains("Weapon")) return;
          
            //When you right click you actually use your offHand and mainHand
            //Here you check if the player clicks with only his mainHand
            if(event.getHand() != EquipmentSlot.HAND) return;
          
            //Here you check if the player right clicks
            if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) return;
    
          
            thePlayer.sendMessage("Right clicked with item lored: Weapon");
        }
    
    Hope this helped!
     
  4. It helped a lot! Thank you so much. I just have a couple questions about the mechanism behind it, if you wouldn't mind answering (still trying to learn). If I understand correctly, the
    Code:
     if(event.getHand() != EquipmentSlot.HAND) return; 
    basically discards the trigger of the offhand? And if I understand correctly
    Code:
     return; 
    (I likely don't), it'll basically just stop the event? I'm just curious what the point is.
     
    Last edited: Jun 16, 2020
  5. Offline

    gochi9

    Yes return; will just stop your code there so here
    Code:
    if(event.getHand() != EquipmentSlot.HAND) return;
    
    So it will first click with the right hand and the code will continue and then when it clicks with the left hand the code will stop since you don't want the player to also use his left hand
     
Thread Status:
Not open for further replies.

Share This Page