Solved Getting doubled message

Discussion in 'Plugin Development' started by ExtremeOrphan, Aug 24, 2019.

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

    ExtremeOrphan

    I am experiencing a bug where the message "Player has been bagged" and "You have been black bagged" are being send x2 to the recieving players (located @ bottom of 2nd code, EventListener) Looking for any ideas as to why this is happening, relatively new to this.

    Main.java
    Code:
    package com.jeremyrpeters.vcblackbags;
    
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public final class Main extends JavaPlugin {
     
        @Override
        public void onEnable() {
            // Enable
            this.getServer().getPluginManager().registerEvents(new EventListener(), this);
    
        }
     
        @Override
        public void onDisable() {
            // Disable
        }
    }
    
    EventListener.java
    Code:
    package com.jeremyrpeters.vcblackbags;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEntityEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class EventListener implements Listener {
        @EventHandler
        public void onPlayerQuit(PlayerQuitEvent event) {
           
            // Your code here...
        }
       
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onPlayerInteract(PlayerInteractEntityEvent event) {
           
            Player p = event.getPlayer();
            ItemStack item = p.getItemInHand();
            Entity entity = event.getRightClicked();
           
            if(entity instanceof Player) {
           
                if(item.getType() == Material.WOOL) {
           
                    ItemMeta meta = item.getItemMeta();
               
                    if(item.hasItemMeta()) {
                   
                        String name = meta.getDisplayName();
                   
                        if(name.equalsIgnoreCase(ChatColor.DARK_GRAY + "Black Bag")) {
                       
                            p.sendMessage("Player has been bagged!");
                            entity.sendMessage("You have been blackbagged");
                           
                        }
                    }
                }
            }
        }
    }
    
     
    Last edited: Aug 25, 2019
  2. Offline

    wand555

    It appears that player and entity are the same. Try add a (!p.getName.equalsIgnoreCase(entity.getName). Depending on what the right clicked entity should be, you could check and cast the entity directly to your wanted entitytype
     
  3. Offline

    KarimAKL

    @ExtremeOrphan I'm not sure, but maybe it could have something to do with the fact that you have two hands in 1.9+.
     
  4. Offline

    robertlit

    @ExtremeOrphan Maybe the event registers twice, try adding a cooldown.

    EDIT: Tested the event on 1.8.8 (Didn't check if the item in hand is a wool with a specific display name, to save some time, also I tested it on a chicken because I have not alt) and it worked fine.
     
    Last edited: Aug 25, 2019
  5. Offline

    ExtremeOrphan

    @robertlit Not exactly sure what you mean by adding a cool down, if you could provide some sample code I'd really appreciate it, and when you say you tested in 1.8.8 was it to test the dual hands problem suggested by @KarimAKL or your cool down?

    @wand555 the entity and the player are different in the event. The player is the one executing the right click action, and the entity is the well "entity" being right clicked, thus why I'm checking if its a player.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 25, 2019
  6. Offline

    wand555

    I get it now. I thought you we're complaining about the two messages being sent, because you only wanted one.
    If you add a debugger message right at the beginning of the event, is it also being sent twice?
     
  7. Offline

    ExtremeOrphan

    @wand555 decided to put some debug messages into the code to decipher where the duplication originates from and determined it to be "if(entity instanceof player) {" causing everything to be triggered twice.

    Here is code (which has been updated slightly since post), and example of what I receive in chat now.

    Code:
    package com.jeremyrpeters.vcblackbags;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEntityEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class EventListener implements Listener {
     
        Main plugin;
      
        public EventListener (Main instance) {
         
        plugin = instance;
     
        }
     
        @EventHandler
        public void onPlayerQuit(PlayerQuitEvent event) {
         
            // Your code here...
        }
     
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onPlayerInteract(PlayerInteractEntityEvent event) {
         
            Player p = event.getPlayer();
            ItemStack item = p.getItemInHand();
            Entity entity = event.getRightClicked();
         
            if(entity instanceof Player) {
             
                p.sendMessage("IS A PLAYER");
         
                if(item.getType() == Material.WOOL) {
         
                    p.sendMessage("IS USING WOOL");
                    ItemMeta meta = item.getItemMeta();
             
                    if(item.hasItemMeta()) {
                     
                        p.sendMessage("ITEM HAS A META");
                        String name = meta.getDisplayName();
                 
                        if(name.equalsIgnoreCase(ChatColor.DARK_GRAY + "Black Bag")) {
                     
                            p.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Messages.BaggedPlayer")));
                            entity.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Messages.GetBagged")));
                         
                        }
                    }
                }
            }
        }
    }
    
    [​IMG]
     
  8. Offline

    wand555

    Your event is registered twice somewhere I think.
    I don't believe "instanceof" can cause something to be executed twice.
     
  9. Offline

    ExtremeOrphan

    UPDATE: Had to check, and single out which hand I wanted being used in the event as, after 1.9 when you right click it utilises both hands, thus providing 2 messages.
    Code:
    if(event.getHand() == EquipmentSlot.HAND) {
    SOLVED
     
Thread Status:
Not open for further replies.

Share This Page