Odd issue with PlayerInteractEvent.

Discussion in 'Plugin Development' started by domhoe_error_reporter, Apr 19, 2016.

Thread Status:
Not open for further replies.
  1. Okay, I'm having this problem trying to make a staff mode for my server, and I can't seem to get the PlayerInteractEvent to work so that whenever a player right clicks the nether star OR the record, nothing happens.
    Here's my PlayerInteractEvent:
    Code (open)
    Code:
    @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event){
            Player player = event.getPlayer();
            Action action = event.getAction();
            if(!methodutil.staffmode.contains(player.getName())){
                return;
            }
            if(action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK){
                if(player.getItemInHand().getType() == Material.NETHER_STAR){
                    Bukkit.dispatchCommand(player, "vanish");
                }else if(player.getItemInHand().getType() == Material.RECORD_3){
                    if(Bukkit.getOnlinePlayers().length == 1){
                        player.sendMessage(methodutil.cc("&9&lRandom Teleport&8 »&7 There are not enough players online to use this!"));
                    }
                    if(Bukkit.getOnlinePlayers().length > 1){
                        Player target = Bukkit.getOnlinePlayers()[new Random().nextInt(Bukkit.getOnlinePlayers().length)];
                        if(player != target){
                            player.teleport(target);
                            player.sendMessage(methodutil.cc("&9&lRandom Teleport&8 »&7 You were teleported randomly to " + target.getName()));
                        } else if(player == target){
                            player.sendMessage(methodutil.cc("&9&lRandom Teleport&8 »&7 Try again!"));
                        }
                    }
                }
            }
        }

    Is there any way I can fix this?
     
  2. Offline

    Lordloss

    Yes there is, how about debugging?
     
  3. What do you mean? I know my code works because I've pre debugged before posting.
     
  4. Offline

    Lordloss

    Did you wrote debug messages in front of every if? Where does it get stuck at?
     
  5. Offline

    Konato_K

    @domhoe_error_reporter Did you register your events?

    Are you sure methodutils#staffmode actually contains the player name?
     
  6. Offline

    Zombie_Striker

    Code:
        if(!methodutil.staffmode.contains(player.getName())){
    ------------------------------
    if(player.getItemInHand().getType() == Material.NETHER_STAR){         
    -------------------
          if(Bukkit.getOnlinePlayers().length == 1){
                        player.sendMessage(methodutil.cc("&9&lRandom Teleport&8 »&7 There are not enough players online to use this!"));
                    }
                    if(Bukkit.getOnlinePlayers().length > 1){
    1. Are you sure that collection contains the player's name?
    2. Are you sure the Item in the players hasn't is not null? Are you sure the item in the player's hand is a record/nether star?
    3. You don't need to double check, replace the second if statement with an "else" statement
     
  7. 1) My event listener is registered.
    2) The collection DOES contain the player's name.
    3) I double check to be safe.
    4) If I remove the second if, that means that when anything is right clicked, it will perform the Random Teleport code.
     
  8. Offline

    WolfMage1

    he said replace the second if with an else,
     
  9. Like I said in number 4, the random teleport code will execute.
     
Thread Status:
Not open for further replies.

Share This Page