Solved Amount Of Online Players On Sign Need Help (bug)

Discussion in 'Plugin Development' started by Johny9020, Apr 16, 2014.

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

    Johny9020

    Hello, so i wanted to make a simple plugin for my test server. The plugins function is that is records the amount of players online and put them onto a sign and here is where im stuck: so it working fine it records players online and puts them on the sign but when there are only 2 players online and one leave the sign still shows 2 players out of max and i dont know how to fix it here is my code.


    Code:java
    1. package me.johny9020.playerSign;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.Location;
    7. import org.bukkit.World;
    8. import org.bukkit.block.Sign;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.block.SignChangeEvent;
    15. import org.bukkit.event.player.PlayerJoinEvent;
    16. import org.bukkit.event.player.PlayerQuitEvent;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18.  
    19. public class Main extends JavaPlugin implements Listener{
    20.  
    21. public final Logger logger = Logger.getLogger("Minecraft");
    22.  
    23. @Override
    24. public void onEnable() {
    25. saveDefaultConfig();
    26. this.logger.info("Was Enabled!");
    27. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    28. }
    29.  
    30. @Override
    31. public void onDisable() {
    32.  
    33. }
    34.  
    35. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    36. if(cmd.getName().equalsIgnoreCase("oplayers")) {
    37. sender.sendMessage("Total People Online: " + Bukkit.getServer().getOnlinePlayers().length + "/" + Bukkit.getServer().getMaxPlayers());
    38. }
    39. return true;
    40. }
    41.  
    42. @EventHandler
    43. public void onPlayerJoin(PlayerJoinEvent e) {
    44.  
    45. Player p = e.getPlayer();
    46.  
    47. World w = Bukkit.getServer().getWorld(this.getConfig().getString("OPlayers.World"));
    48. double x = this.getConfig().getDouble("OPlayers.X");
    49. double y = this.getConfig().getDouble("OPlayers.Y");
    50. double z = this.getConfig().getDouble("OPlayers.Z");
    51. Location OPlayers = new Location(w, x, y, z);
    52.  
    53. if(p.getWorld().getBlockAt(OPlayers) != null) {
    54. Sign sign = (Sign) p.getWorld().getBlockAt(OPlayers).getState();
    55. sign.setLine(0, Bukkit.getServer().getOnlinePlayers().length + "/" + Bukkit.getServer().getMaxPlayers());
    56. sign.update(true);
    57. sign.update();
    58. }
    59.  
    60. }
    61.  
    62. @EventHandler
    63. public void onPlayerLeave(PlayerQuitEvent e) {
    64.  
    65. Player p = e.getPlayer();
    66.  
    67. World w = Bukkit.getServer().getWorld(this.getConfig().getString("OPlayers.World"));
    68. double x = this.getConfig().getDouble("OPlayers.X");
    69. double y = this.getConfig().getDouble("OPlayers.Y");
    70. double z = this.getConfig().getDouble("OPlayers.Z");
    71. Location OPlayers = new Location(w, x, y, z);
    72.  
    73. if(p.getWorld().getBlockAt(OPlayers) != null) {
    74. Sign sign = (Sign) p.getWorld().getBlockAt(OPlayers).getState();
    75. sign.setLine(0, Bukkit.getServer().getOnlinePlayers().length + "/" + Bukkit.getServer().getMaxPlayers());
    76. sign.update();
    77.  
    78. }
    79.  
    80. }
    81.  
    82. @EventHandler
    83. public void onSignChange(SignChangeEvent e) {
    84. if(e.getLine(0).equalsIgnoreCase("[oplayers]")) {
    85. this.getConfig().set("OPlayers.X", e.getBlock().getLocation().getX());
    86. this.getConfig().set("OPlayers.Y", e.getBlock().getLocation().getY());
    87. this.getConfig().set("OPlayers.Z", e.getBlock().getLocation().getZ());
    88. this.getConfig().set("OPlayers.World", e.getBlock().getLocation().getWorld().getName());
    89. this.saveConfig();
    90. e.setLine(0, Bukkit.getServer().getOnlinePlayers().length + "/" + Bukkit.getServer().getMaxPlayers());
    91. }
    92. }
    93.  
    94. }
    95.  


    please tell me what im doing wrong thanks
     
  2. Offline

    2MBKindiegames

    Hi,

    I think when executing the player leave method, the leaving player is still counted in the Bukkit.getOnlinePlayers(). If that's the case, just do:

     
  3. Offline

    Johny9020

    Thanks Ill Try It Out

    Thank You, you fixed my problem! :D

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

    2MBKindiegames

    Johny9020 You're very welcome!

    -If you ever need my help again, just PM ME or create a new topic!
     
Thread Status:
Not open for further replies.

Share This Page