Solved Players, Lists And playerJoinEvents

Discussion in 'Plugin Development' started by Mindfulhacker, Jul 14, 2013.

Thread Status:
Not open for further replies.
  1. Hello everybody

    I'm trying to create a plugin where a player can hide themselves. Whenever the command to hide yourself is executed it runs:

    Code:java
    1.  
    2. for(Player online : Bukkit.getOnlinePlayers()){
    3. online.hidePlayer(player);
    4. }
    5.  


    However when a player relogs/logs in after the command has been executed the player is visible and is required to run the command again. I want to use a playerJoinEvent to automatically hide the players who will be put into a list when they run the command. I have been advised to use a hashset however i have no idea how to use one. Here is the code I have written for the playerJoinEvent:

    Code:java
    1. @EventHandler
    2. public void onPlayerJoin(PlayerJoinEvent event){
    3. Player joined = event.getPlayer();
    4. for(){
    5. joined.hidePlayer(arg0);
    6. }
    7. }


    However I don't have a list that the player can be added to / removed from as I dont know how. Can someone please help me in:

    A) Creating The List
    B) Looping Through The List
     
  2. Offline

    xTrollxDudex

    Mindfulhacker
    Well first off, there are multiple different lists, someone which you might have heard of before: ArrayList, HashMap, and HashSet are the most common.

    In this case, I would use a HashSet because it is extremely fast, has 1000 calculations per second if not more

    So here's how you make one:
    Code:java
    1. public Set<String> invisiblePlayers = new HashSet<String>();
    2. //create the has set
    3.  
    4. //on player join
    5. Player p = event.getPlayer()
    6. invisiblePlayers.add(p.getName());
    7. //adding
    8.  
    9. invisiblePlayers.remove(p.getName);
    10. //removing
    11.  
    12. for(Player player : invisiblePlayers){
    13. player.setHidePlayer(p);
    14. //looping

    Hope this helps!
     
    Eats_Rainbows likes this.
  3. xTrollxDudex

    How would I hide the players in invisiblePlayers from the newly joined players? Ive tried to switch the code and have

    Code:java
    1. Player p = event.getPlayer()
    2. p.add(invisiblePlayers.getName());


    However I get a error. I belive this is where I need a for loop
     
  4. Offline

    xTrollxDudex

    Mindfulhacker
    Actually, first you need to to check on player join if the player is already contained in the list.
    Code:java
    1. //on player join
    2. Player p = event.getPlayer()
    3. if(invisiblePlayers.contains(p.getName()){
    4. return;
    5. }else if(!invisiblePlayers.contains(p.getName()){
    6. invisiblePlayers.add(p.getName);
    7.  
    8. //now we can hide the newly joined player
    9. //soz the first loop was wrong since the hashset has strings, you need not Player player but String s(or other variable)
    10. for(String s : invisiblePlayers){
    11. //get the player that joined and the players already in the set
    12. Player ply = Bukkit.getPlayer(p.getName());
    13. Player online = Bukkit.getPlayer(s);
    14.  
    15. //make the joined player invisible
    16. online.hidePlayer(ply);
    17. }
    18. }
     
  5. Posted This Secconds After Your Reply

    Ok, so I added and modified the for loop so it looks like this:

    Code:java
    1.  
    2. @EventHandler
    3. public void onPlayerJoin(PlayerJoinEvent event){
    4. Player joined = event.getPlayer();
    5. for(Player player : invisiblePlayers){
    6. joined.hidePlayer(player);
    7. }
    8.  
    9. }


    Just I get an error with the Player player: invisiblePlayers

    Code:
    Type mismatch: cannot convert from element type String to Player
     
  6. Offline

    xTrollxDudex

    Mindfulhacker
    It's a bit more complex than that because your storing strings inside the hashset instead of players objects. Storing player objects is bad programming because it causes memory leaks. Try my modified code.

    Edit: I edited the second reply. Take a look at the code
     
  7. Ok im really confused. Im gonna label this and tell me if its correct or not:

    Code:java
    1.  
    2.  
    3. Player p = event.getPlayer() //When the player joins get the players data
    4. if(invisiblePlayers.contains(p.getName()){ //If player is in invisiblePlayers list
    5. return; //Continue
    6. }else if(!invisiblePlayers.contains(p.getName()){ //Otherwise if it they are not on it
    7. invisiblePlayers.add(p.getName); //add them
    8.  
    9. for(String s : invisiblePlayers){ //Get all the players's names in the hashset
    10. Player ply = Bukkit.getPlayer(p.getName()); //Get the player who just joined's name
    11. Player online = Bukkit.getPlayer(s); //Get all online players
    12.  
    13. //make the joined player invisible
    14. online.hidePlayer(ply);// Hide the player who just joined from everyone else
    15. }


    Im not sure how the code is supposed to achieve the thing. If I'm correct it just hides the player who joins?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  8. Offline

    Henzz

    Mindfulhacker
    Try this:
    Command.
    PHP:
    if (!(sender instanceof Player)) {
                
    sender.sendMessage("Please use this command in game");
                return 
    true;
            }
         
            
    Player player = (Playersender;
         
            if (
    cmd.getName().equalsIgnoreCase("command")) {
             
                if (
    args.length == 1) {
                 
                    if (
    args[0].equalsIgnoreCase("on")) {
                        if (!(
    hiddenPlayers.contains(player.getName()))) {
                            
    hiddenPlayers.add(player.getName());
                            
    player.hidePlayer(player);
                            
    player.sendMessage("Hidden");
                        } else {
                            
    player.sendMessage("You're already hidden");
                        }
                    }
                    else if (
    args[0].equalsIgnoreCase("off")) {
                        if (
    hiddenPlayers.contains(player.getName())) {
                            
    hiddenPlayers.remove(player.getName());
                            
    player.showPlayer(player);
                            
    player.sendMessage("Unidden");
                        } else {
                            
    player.sendMessage("You're visible");
                        }
                    }
                }
            }
    Event.
    PHP:
    @EventHandler
        
    public void onJoin(PlayerJoinEvent event) {
            final 
    Player player event.getPlayer();
         
            if (
    Main.hiddenPlayers.contains(player.getName())) {
                
    player.hidePlayer(player);
            }
        }
     
  9. Offline

    xTrollxDudex

    Mindfulhacker
    Oh! I thought you were trying to hide he joined player from everyone else xD

    But if I just make the online players invisible, they will see the joined player.
    So I made them both invisible, neither can see eachother
    Code:java
    1. //on player join
    2. Player p = event.getPlayer()
    3. if(invisiblePlayers.contains(p.getName()){
    4.  
    5. for(String s : invisiblePlayers){
    6. //get the player that joined and the players already in the set
    7. Player ply = Bukkit.getPlayer(p.getName());
    8. Player online = Bukkit.getPlayer(s);
    9.  
    10. //make the joined player invisible
    11. online.hidePlayer(ply);
    12. //make the online players invisible
    13. ply.hidePlayer(online);
    14. }
    15.  
    16. }else if(!invisiblePlayers.contains(p.getName()){
    17. invisiblePlayers.add(p.getName);
    18.  
    19. //now we can hide the newly joined player
    20. //soz the first loop was wrong since the hashset has strings, you need not Player player but String s(or other variable)
    21. for(String s : invisiblePlayers){
    22. //get the player that joined and the players already in the set
    23. Player ply = Bukkit.getPlayer(p.getName());
    24. Player online = Bukkit.getPlayer(s);
    25.  
    26. //make the joined player invisible
    27. online.hidePlayer(ply);
    28. //and make the online players invisible
    29. ply.hidePlayer(online);
    30. }
    31. }
     
  10. @xTrollxDudex
    I think your getting the wrong end of the stick again lol.

    Basically User 1 wants to hide from other users

    User 1 types the command - Is added to hashset and hidden from all players.

    User 2 Joins game and can see User 1

    User 3 was allready logged on and cannot see User 1


    I want to make it so Whenever a user joins it automatically hides users in the hashset so

    User 1 types the command and is added to hashest and hidden from all players
    User 2 Joins game and User 1 (In the hashmat) user 1 is automaticaly hidden from user 2 and 3
    User 3 was allready logged on and cannot see user 1
     
  11. Offline

    xTrollxDudex

    Mindfulhacker
    Jeez :p
    Code:java
    1. //on player join
    2. Player p = event.getPlayer()
    3. if(invisiblePlayers.contains(p.getName()){
    4.  
    5. for(String s : invisiblePlayers){
    6. Player ply = Bukkit.getPlayer(p.getName());
    7. Player online = Bukkit.getPlayer(s);
    8.  
    9. //make the joined player invisible to online players
    10. online.hidePlayer(ply);
    11. }
    12.  
    13. }else if(!invisiblePlayers.contains(p.getName()){
    14.  
    15. for(String s : invisiblePlayers){
    16. Player ply = Bukkit.getPlayer(p.getName());
    17. Player online = Bukkit.getPlayer(s)
    18.  
    19. ply.hidePlayer(online);
    20. }
    21. }

    Now for the command. In the listener class you should have a hashset right? Now you need a way of accessing it, adding, contains, and removing. And to make them invisible.
     
  12. xTrollxDudex
    Thanks it works, with a few modifications. Is there any way I could get all of the names from
     
  13. Offline

    xTrollxDudex

    Mindfulhacker
    Id assume the hashset? You loop through it like I did above except this time instead of hiding them, s would be the player name.
    To get the player object, use Bukkit.getPlayer(s), making sure that it isn't null(it's an offline player)
     
  14. xTrollxDudex
    I've tried that allready, but it dosnt work :(
    I get the error
    Code:
    The method sendMessage(String) is undefined for the type String
    When i try to do

    Code:
    s.sendMessage("Just Testing!");
     
  15. Offline

    xTrollxDudex

    Mindfulhacker
    Like i said, you need to get the player.
    PHP:
    for(String s invisblePlayers){
    if(
    Bukkit.getPlayer(s) != null){
    Player p Bukkit.getPlayer()
    //do stuff with p
    }
    }
     
  16. Offline

    xTrollxDudex

    Mindfulhacker likes this.
Thread Status:
Not open for further replies.

Share This Page