Solved PlayerJoinEvent not working??

Discussion in 'Spigot Plugin Development' started by imfeared, Jun 7, 2017.

  1. So, I want the players that are not in the mode and do not have access to see the player in the mode...
    I want them to not see the player in the mode whatsoever unless the player exits the mode..


    Code (Text):

    @EventHandler
    public void onJoin(PlayerJoinEvent e) {
        Player p = e.getPlayer();

        if (mode.contains(p.getUniqueId().toString())) {

            for (Player players : Bukkit.getOnlinePlayers()) {

                if (players != null) {

                    players.hidePlayer(p);

                }
                if(mode.contains(players.getUniqueId().toString())) {

                    p.hidePlayer(players);

                }

            }

        }

    }
     
     
  2. Is it currently doing anything right now?
     
  3. It should be
    because I want the player that is not a staff member/op or doesn't have the permission to not be able to see the person in the mode
     
  4. Im kind of newer to Java, but what is mode.contains supposed to do? To me, and I can't say if this is right, is that it may not be working because you haven't defined that?
     
    • Like Like x 1
  5. Code (Text):

    public void onStaffEnable(Player p) {

        ArrayList<ItemStack> items = new ArrayList<>();
        for(ItemStack item : p.getInventory().getContents()){
            items.add(item);
        }
        for(ItemStack item : p.getInventory().getArmorContents()){
            items.add(item);
        }
        for(ItemStack item : p.getInventory().getStorageContents()){
            items.add(item);
        }
        inventories.put(p, items);
        p.getInventory().clear();
        p.sendMessage(ChatColor.GREEN + "STAFF MODE ENABLED!");
        i.addPlayerInv(p, 0, i.makeFreeze());
        i.addPlayerInv(p, 1, i.makeRandomTp());
        i.addPlayerInv(p, 2, i.makeCheckInv());
        i.addPlayerInv(p, 3, i.makeWE());
        i.addPlayerInv(p, 8, i.makeDisMode());
        p.updateInventory();
        p.setGameMode(GameMode.CREATIVE);
        p.setHealth(20);
        p.setFoodLevel(20);
        if (p.canSee(p)) {
            for (Player player : Bukkit.getOnlinePlayers()) {
                if (player.hasPermission("staffmode.see")) {

                    player.showPlayer(p);

                } else {

                    player.hidePlayer(p);

                }

            }
        }


    }
     
    That is what mode does if it contains them.
     
  6. TomTheDeveloper

    Supporter

    You've registered your event?
     
  7. Code (Text):

    private static Plugin plugin;
    public static ItemstackClass itemstackClass;
    public static ListenerClass listenerClass;

    @Override
    public void onEnable() {

        itemstackClass = new ItemstackClass();
        listenerClass = new ListenerClass();
        registerEvents(this, listenerClass);
        plugin = this;
        getCommand("staff").setExecutor(listenerClass);
        getCommand("ss").setExecutor(listenerClass);

    }
     
     
  8. TomTheDeveloper

    Supporter

    Try to debug with some debug messages. This way you'll see how far you get into your event and if your event is even triggered at all.
     
  9. Code (Text):

    if (mode.contains(p.getUniqueId().toString())) {

        Bukkit.getServer().broadcastMessage("YUP"); // not triggering

        for (Player players : Bukkit.getOnlinePlayers()) {

            if (players != null) {

                players.hidePlayer(p);
                Bukkit.getServer().broadcastMessage("NO WORK");  // not triggering

            }
            if(mode.contains(players.getUniqueId().toString())) {

                p.hidePlayer(players);

            }

            Bukkit.getServer().broadcastMessage("YUP"); // not triggering
        }

    }
     
    seems like the whole thing does not trigger but if I put it outside of the for loop and the if statement it triggers??
    Is it a logical error?
     
  10. TomTheDeveloper

    Supporter

    You mean the event is triggered but it never goes past the if statement? If this is the case, then you know where your mistake is. Most likely you made somewhere a stupid logical error like we all do a lot of the times. So hunt that mistake down and your issue will be fixed.
     
  11. Ok, so I figured out the problem and the only thing that I need is how would I check if atleast one of the staff members are in the mode array?
     
  12. TomTheDeveloper

    Supporter

    Check if one of the players has a specific staff permission? Or if you are using Vault, you can check if a player has a staff rank or above.
     
    • Like Like x 1
  13. Your code isn't doing what you are trying to do. Just rethink your outer if-statement and the contents of your for-loop.

    And "players" will never be null, as collections may not contain null references. See Java Collection Framework.

    Edit: A start would be to remove the outer if-statement. Its misplaced.
     
    • Like Like x 1
  14. Ok, I have fixed it due to your help and I thank you so much for helping me..

    Pearson1919, don't worry about being new to java, I am basically still new to java myself...
     
    • Like Like x 1
  15. Can you show what was fixed? And thanks, Im just trying to keep my plugin alive for the time being :p. I'm making a Web Interface for that since that's what I learned first was HTML / CSS / some PHP.
     
  16. TomTheDeveloper

    Supporter

    I also recommend removing the static keyword from your listeners. Making them static is bad practice.
     
  17. Code (Text):

    for (Player players : Bukkit.getServer().getOnlinePlayers()) {

        if (mode.contains(p.getUniqueId().toString())) {

            if (players != null) {

                players.hidePlayer(p);
                Bukkit.getServer().broadcastMessage("asdasdasd");

            }

        }

        if (!mode.contains(players.getUniqueId().toString())) {

            players.hidePlayer(p);
            Bukkit.getServer().broadcastMessage("asdasdasd");

        }

    }
     
    It was checking if the player that joined was in the array and I believe that messed it all up.
    Well, I also removed the player from the array if it contained them so that contributed to the problem.


    Got it!
     
  18. TomTheDeveloper

    Supporter

    Btw. All I did was just ask some questions back. Ultimately you solved your problem yourself. As a programmer you have to question everything you've written down even that part where you from think it's 100% right. But if you keep programming, you'll learn this most likely the hard way like every programmer does :p.

    Now have fun and keep challenging yourself! :)
     
    • Agree Agree x 1
    • Friendly Friendly x 1