Player can join for a time (in area)

Discussion in 'Plugin Development' started by PlayerNerd, Jan 10, 2015.

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

    PlayerNerd

    I´m creating a plugin for the player join in a specific area for a time and back in the hub, i tried to make a scheduler but it doesn´t work, cause i need make 1 for each player and the server gets lag. How can i make it?
     
  2. Offline

    Skionz

    @PlayerNerd When they join save them in a Map with the current time. Then create a repeating task and compare the time.
     
  3. Offline

    PlayerNerd

    @Skionz I need make for the players join in different time, not for a group of players, when i make it with scheduler the first player that joined stay getting messages and teleport and the others don´t go.
     
  4. Offline

    Skionz

    What? Please use google translate it is hard for me to understand you.
     
  5. Offline

    PlayerNerd

    @Skionz I'm doing a safari plugin for pixelmon server, and I need the players to join at any time of day. When i use shceduler to teleport the player that ends the he is getting a message infinite and infinitely teleporting and the other players do not.
     
  6. Offline

    Skionz

    @PlayerNerd What do you mean join at any time of the day? Post your code so far.
     
  7. Offline

    PlayerNerd

    @Skionz
    Code:
    @EventHandler
        public void onRightSign(PlayerInteractEvent e)
        {
            final Player p = e.getPlayer();
           
            Block b = e.getClickedBlock();
           
            if(e.getAction().equals(Action.RIGHT_CLICK_BLOCK) && (b.getType() == Material.SIGN_POST || b.getType() == Material.WALL_SIGN))
            {
                Sign s = (Sign) b.getState();
               
                Location safari = new Location(Bukkit.getWorld(plugin.getConfig().getString("safari.safari.world")), plugin.getConfig().getDouble("safari.safari.x"), plugin.getConfig().getDouble("safari.safari.y"), plugin.getConfig().getDouble("safari.safari.z"));
       
                if(s.getLine(0).equalsIgnoreCase(ChatColor.DARK_GREEN + "[" + ChatColor.GREEN + "Safari" + ChatColor.DARK_GREEN + "]") && s.getLine(1).equalsIgnoreCase(ChatColor.DARK_GREEN + "10 Min") &&
                        s.getLine(2).equalsIgnoreCase(ChatColor.DARK_GREEN + "$750"))
                {
                   
                        if(Main.econ.getBalance(p.getName()) >= 750)
                        {
                           
                            if(p.hasPermission("playernerd.safari.entrar.10"))
                            {
                               
                            Main.econ.withdrawPlayer(p.getName(), 750);
                            p.teleport(safari);
                            p.sendMessage(Main.nserv + "Você tem " + ChatColor.DARK_GREEN + "$" + Main.econ.getBalance(p.getName()) + ChatColor.GREEN + " agora.");
                            p.sendMessage(Main.nserv + "Você está no safari por 10 Minutos.");
                            Main.nosafari10.add(p);
    //I stop here for make the loop
                           
                           
                            }else {
                                p.sendMessage(Main.nserv + ChatColor.RED + "Nop!");
                            }
                           
                        } else {
                            p.sendMessage(Main.nserv + ChatColor.RED + "Ei! " + ChatColor.GREEN + p.getName() + ChatColor.RED +
                                    " para entrar no Safari você deve ter no minimo " + ChatColor.DARK_GREEN + "$750" + ChatColor.RED + "");
                               }
                       
                       
                }
               
                if(s.getLine(0).equalsIgnoreCase(ChatColor.DARK_GREEN + "[" + ChatColor.GREEN + "Safari" + ChatColor.DARK_GREEN + "]") && s.getLine(1).equalsIgnoreCase(ChatColor.DARK_GREEN + "30 Min") &&
                        s.getLine(2).equalsIgnoreCase(ChatColor.DARK_GREEN + "$1200"))
                {
                   
                }
            }
        }
     
  8. Man use a Map:
    Code:java
    1.  
    2. Map<UUID, Integer> players = new HashMap<UUID, Integer>();
    3.  
    4. //Ad the player to the map
    5. Player player = event.getPlayer();
    6. UUID uuid = player.getUniqueId();
    7. players.put(uuid, time); //Replace time for how much seconds you wanna keep the player inside the arena
    8.  
    9. //Runnable at 20 ticks
    10. private BukkitTask task;
    11. public void runnable()
    12. {
    13. this.task = new BukkitRunnable()
    14. {
    15. @Override
    16. public void run() {
    17. //Loop to get all online players
    18. for(Player online : Bukkit.getOnlinePlayers()) {
    19. //Get the uuid from the player
    20. UUID uuid = online.getUniqueId();
    21. //Check if the players is inside the map
    22. if(!players.containsKey(uuid)) { return; }
    23. //Get the seconds
    24. int time = online.get(uuid);
    25. //If seconds >= 1
    26. if(time >= 1) {
    27. //Remove 1 second
    28. players.put(uuid, time-1);
    29. //If seconds = 0
    30. } else {
    31. map.remove(uuid);
    32. //Tp player
    33. }
    34. }
    35. }
    36. }.runTaskTimer(plugin, 10, 10);
    37. }
    38.  
    39.  
    40. //After on the void "onEnable" do this
    41. @Overrde
    42. public void onEnable() {
    43. //Initialize the runnable
    44. runnable();
    45. //Or
    46. classInstance().runnable();
    47. }
    48.  
     
    Last edited: Jan 10, 2015
  9. @MaTaMoR_
    1. Spoonfeeding is not good, people usually copy and paste without understanding it.
    2. Why are you getting all players then not using them?
    3. You are putting the player in the list then you are checking they are in and reputting them in -1 from time? Why?
     
    nverdier likes this.
  10. 1) I did put some explation messages
    2) That's a mistake i already changed player for online
    3) I'm checking if the player is in the inside the map to don't get a null pointer exception
    4) Yes i did put a player inside the hashmap but im checking all players, im reputting it in with -1 what i mean time-1 (60 , 59 , 58 , 57 every second) a count down
     
    Last edited: Jan 10, 2015
  11. @MaTaMoR_ People will still copy and paste without reading. I am not talking about the check I just don't really get the point of your code.

    Just make a timer to run every second, no need to add players every second...
     
  12. If the player isn't in the map and ill do int time = players.get(uuid) what will happen ?
     
  13. @MaTaMoR_ I have said, I am not talking about the check. You just don't need to add a player to a map every second, just make an int and take one every second.
     
  14. That will return the same time every second .
     
  15. Offline

    PlayerNerd

  16. Post your code ...
     
  17. Offline

    nverdier

    @PlayerNerd Of course it doesn't work. His code is inefficient, useless, redundant (in a bad way), and in all ways bad.
     
  18. No it wouldnt...

    So if I had the number 20 every second I took one of so I would say 19, 18 ect. It is the same in code, you take one off every second and it takes one off every second.

    @PlayerNerd Make an integer then in the timer do variable--;
     
  19. Yeah but how you make that for every player ?
     
  20. Offline

    nverdier

    @MaTaMoR_ Why are you trying to make it for every player? That included players not in the area.
     
  21. For every player in the arena ...
     
  22. @MaTaMoR_ But you are getting ALL online players, not players in an arena.
     
  23. Yeah but i'm checking if the players is inside the map(arena), you can also just set the keys and check if the player is online or just remove him on quit.
     
  24. @MaTaMoR_ You have no map there with Arena?
     
  25. I don't know if is this what you mean .
    Code:java
    1.  
    2. Map<UUID, Integer> players = new HashMap<UUID, Integer>();
    3.  
     
  26. @MaTaMoR_ Where is the arena there? You are getting the player UUID and an integer for time.
     
  27. There should be the players from the arena...
     
  28. Offline

    PlayerNerd

    Code:
        @EventHandler
        public void onRightSign(final PlayerInteractEvent e)
        {
            final Player p = e.getPlayer();
           
            Block b = e.getClickedBlock();
           
            if(e.getAction().equals(Action.RIGHT_CLICK_BLOCK) && (b.getType() == Material.SIGN_POST || b.getType() == Material.WALL_SIGN))
            {
                Sign s = (Sign) b.getState();
               
                Location safari = new Location(Bukkit.getWorld(plugin.getConfig().getString("safari.safari.world")), plugin.getConfig().getDouble("safari.safari.x"), plugin.getConfig().getDouble("safari.safari.y"), plugin.getConfig().getDouble("safari.safari.z"));
       
                if(s.getLine(0).equalsIgnoreCase(ChatColor.DARK_GREEN + "[" + ChatColor.GREEN + "Safari" + ChatColor.DARK_GREEN + "]") && s.getLine(1).equalsIgnoreCase(ChatColor.DARK_GREEN + "10 Min") &&
                        s.getLine(2).equalsIgnoreCase(ChatColor.DARK_GREEN + "$750"))
                {
                   
                        if(Main.econ.getBalance(p.getName()) >= 750)
                        {
                           
                            if(p.hasPermission("playernerd.safari.entrar.10"))
                            {
                               
                            Main.econ.withdrawPlayer(p.getName(), 750);
                            p.teleport(safari);
                            p.sendMessage(Main.nserv + "Você tem " + ChatColor.DARK_GREEN + "$" + Main.econ.getBalance(p.getName()) + ChatColor.GREEN + " agora.");
                            p.sendMessage(Main.nserv + "Você está no safari por 10 Minutos.");
                            Main.nosafari10.add(p);
                           
                            Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable ()
                            {
                               
                                int number = 30;
                               
                                public void run()
                                {
                                   
                                    Player player = e.getPlayer();
                                    UUID uuid = p.getUniqueId();
                                    players.put(uuid, number); //Replace time for how much seconds you wanna keep the player inside the arena
                                    
                                    for(@SuppressWarnings("unused") Player online : Bukkit.getOnlinePlayers()) {
                                    if(!players.containsKey(uuid)) { return; }
                                       int time = players.get(uuid);
                                           if(time >= 1) {
                                                      time--;
                                                      players.put(uuid, time);
                                                      player.sendMessage("a");
                                             } else {
                                              player.sendMessage(Main.nserv);
                                              players.put(uuid, 0);
                                    }
                                    }
                                     
                                }
                                                                    }, 0L, 1*20L);
                            }else {
                                p.sendMessage(Main.nserv + ChatColor.RED + "Nop!");
                            }
                           
                        } else {
                            p.sendMessage(Main.nserv + ChatColor.RED + "Ei! " + ChatColor.GREEN + p.getName() + ChatColor.RED +
                                    " para entrar no Safari você deve ter no minimo " + ChatColor.DARK_GREEN + "$750" + ChatColor.RED + "");
                               }
                       
                       
                }
               
                if(s.getLine(0).equalsIgnoreCase(ChatColor.DARK_GREEN + "[" + ChatColor.GREEN + "Safari" + ChatColor.DARK_GREEN + "]") && s.getLine(1).equalsIgnoreCase(ChatColor.DARK_GREEN + "30 Min") &&
                        s.getLine(2).equalsIgnoreCase(ChatColor.DARK_GREEN + "$1200"))
                {
                   
                }
            }
        }
    @MaTaMoR_
     
    Last edited by a moderator: Jan 10, 2015
  29. I really don't know why my post get removed but ok.
    You have to put the runnable out of the event .
    Just put this inside the event
    Code:java
    1.  
    2. Player player = event.getPlayer();
    3. UUID uuid = player.getUniqueId();
    4. players.put(uuid, time);
    5.  
     
  30. Offline

    PlayerNerd

Thread Status:
Not open for further replies.

Share This Page