Make scoreboard only available in one world.

Discussion in 'Plugin Development' started by The_Punkster, Jun 19, 2014.

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

    The_Punkster

    Hiya! I'm making a plugin that shows how many people are in eah world on the scoreboard.

    I want to make this only in the world "newspawn".

    I found some code, but not sure how to add it? I don't understand it's workings.

    If someone could help me implement this, it'd be nice.

    Code needed to be added:

    Code:java
    1. package me.worldstats;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.*;
    7. import org.bukkit.World;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.scheduler.BukkitScheduler;
    10. import org.bukkit.scoreboard.Objective;
    11. import org.bukkit.scoreboard.Score;
    12. import org.bukkit.scoreboard.Scoreboard;
    13. import org.bukkit.scoreboard.DisplaySlot;
    14. import org.bukkit.scoreboard.ScoreboardManager;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17.  
    18. public class Main extends JavaPlugin implements Listener {
    19.  
    20.  
    21. public int survivalPlayers;
    22. public int pvpPlayers;
    23. public int skyworldPlayers;
    24. public int hubPlayers;
    25. public int creativePlayers;
    26.  
    27.  
    28.  
    29.  
    30. @Override
    31. public void onEnable() {
    32.  
    33.  
    34.  
    35. if (player.hasPermission("worldboard.view")) {
    36. //Do something
    37.  
    38.  
    39.  
    40.  
    41.  
    42.  
    43. BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
    44. scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
    45. @Override
    46. public void run() {
    47. // Do something
    48.  
    49.  
    50.  
    51. survivalPlayers = 0;
    52. pvpPlayers = 0;
    53. skyworldPlayers = 0;
    54. hubPlayers = 0;
    55. creativePlayers = 0;
    56.  
    57. Player[] onlinePlayers = Bukkit.getOnlinePlayers();
    58.  
    59.  
    60. for (Player player : onlinePlayers){
    61.  
    62. if (player.getWorld().getName().equalsIgnoreCase("pvp")){
    63. pvpPlayers = pvpPlayers +1;
    64.  
    65. } else if (player.getWorld().getName().equalsIgnoreCase("survival")) {
    66. survivalPlayers = survivalPlayers + 1;
    67.  
    68. } else if (player.getWorld().getName().equalsIgnoreCase("skyworld")) {
    69. skyworldPlayers = skyworldPlayers + 1;
    70.  
    71. } else if (player.getWorld().getName().equalsIgnoreCase("newspawn")) {
    72. hubPlayers = hubPlayers + 1;
    73.  
    74. } else if (player.getWorld().getName().equalsIgnoreCase("creative")) {
    75. creativePlayers = creativePlayers + 1;
    76.  
    77. } else {
    78. Bukkit.getServer().broadcastMessage("ERROR"); }
    79.  
    80. }
    81.  
    82.  
    83.  
    84.  
    85.  
    86. ScoreboardManager sbManager = Bukkit.getScoreboardManager();
    87. Scoreboard sBoard = sbManager.getNewScoreboard();
    88.  
    89. Objective objHub = sBoard.registerNewObjective("hubplayers", "Hub");
    90. Objective objPvp = sBoard.registerNewObjective("pvpplayers", "PVP");
    91. Objective objSurvival = sBoard.registerNewObjective("survivalplayers", "Survival");
    92. Objective objSkyblock = sBoard.registerNewObjective("skyworldplayers", "SkyBlock");
    93. Objective objCreative = sBoard.registerNewObjective("creativeplayers", "Creative");
    94.  
    95. objHub.setDisplaySlot(DisplaySlot.SIDEBAR);
    96. objHub.setDisplayName("Dimensions");
    97.  
    98. Score hubscore = objHub.getScore(Bukkit.getOfflinePlayer("Hub: "));
    99. hubscore.setScore(hubPlayers);
    100.  
    101. Score pvpscore = objHub.getScore(Bukkit.getOfflinePlayer("PVP: "));
    102. pvpscore.setScore(pvpPlayers);
    103.  
    104. Score survivalscore = objHub.getScore(Bukkit.getOfflinePlayer("Survival: "));
    105. survivalscore.setScore(survivalPlayers);
    106.  
    107. Score skyblockscore = objHub.getScore(Bukkit.getOfflinePlayer("SkyBlock: "));
    108. skyblockscore.setScore(skyworldPlayers);
    109.  
    110. Score creativescore = objHub.getScore(Bukkit.getOfflinePlayer("Creative: "));
    111. creativescore.setScore(creativePlayers);
    112.  
    113. }
    114.  
    115.  
    116. }}, 0L, 20L);
    117.  
    118. }
    119.  
    120.  
    121.  
    122.  
    123.  
    124.  
    125.  
    126. @Override
    127. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    128. if (cmd.getName().equalsIgnoreCase("worldstats")) {
    129.  
    130.  
    131. Bukkit.getServer().broadcastMessage("PVP: " + pvpPlayers);
    132. Bukkit.getServer().broadcastMessage("SURVIVAL: " + survivalPlayers);
    133. Bukkit.getServer().broadcastMessage("SKYBLOCK : " + skyworldPlayers);
    134. Bukkit.getServer().broadcastMessage("HUB: " + hubPlayers);
    135.  
    136. return true;
    137. }
    138.  
    139.  
    140.  
    141. return false;
    142.  
    143. }}
    144.  
    145.  
    146.  




    Code needed to be added:

    Code:java
    1. for(Player inWorld : world.getPlayers()){
    2. inWorld.setScoreboard(board);
    3. }
     
  2. Why is this in your onEnable() method? Just run a timer every 2-3 seconds and get the players, then set their scoreboard depending on their world.
     
  3. Offline

    DoctorDark

    The_Punkster

    Instead of using a timer, only apply the scoreboard if they are in a specific world and then remove it when they switch to a world that isn't equal to the specific world using the WorldChangeEvent?

    Code:java
    1. World world = Bukkit.getServer().getWorld("world");
    2. for (Player players : Bukkit.getServer().getOnlinePlayers()) {
    3. if (players.getWorld().getName().equals(world.getName()))
    4. players.setScoreboard(board);
    5. }


    Also to increment an integer, use 'creativePlayers++' instead.

    Also as Incomprehendable, you should set the scoreboard when they join instead, if new players join after the onEnable was called, they won't see the scoreboard.
     
  4. I often update my scoreboard with scrolling text so I just use timers. The last bit of your message doesn't make sense if you're telling me this as I questioned why OP put his methods in the onEnable() method.
     
Thread Status:
Not open for further replies.

Share This Page