Bukkit.getServer().getOnlinePlayers() error

Discussion in 'Plugin Development' started by Tudedude, Jul 22, 2014.

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

    Tudedude

    I'm trying to create a thirst plugin, and the person I'm developing it for wanted a wave thirst system, so it would subtract thirst from all online players at once. I attempted to use this code:
    Code:java
    1. package io.github.Tudedude100.SurvivalThirst;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.io.InputStream;
    6. import java.lang.reflect.InvocationTargetException;
    7. import java.util.Collection;
    8. import java.util.logging.Level;
    9.  
    10. import org.bukkit.Bukkit;
    11. import org.bukkit.ChatColor;
    12. import org.bukkit.GameMode;
    13. import org.bukkit.command.Command;
    14. import org.bukkit.command.CommandSender;
    15. import org.bukkit.configuration.file.FileConfiguration;
    16. import org.bukkit.configuration.file.YamlConfiguration;
    17. import org.bukkit.entity.Player;
    18. import org.bukkit.event.EventHandler;
    19. import org.bukkit.event.Listener;
    20. import org.bukkit.event.player.PlayerJoinEvent;
    21. import org.bukkit.event.player.PlayerQuitEvent;
    22. import org.bukkit.plugin.Plugin;
    23. import org.bukkit.plugin.PluginManager;
    24. import org.bukkit.plugin.java.JavaPlugin;
    25. import org.bukkit.scoreboard.DisplaySlot;
    26. import org.bukkit.scoreboard.Objective;
    27. import org.bukkit.scoreboard.Score;
    28. import org.bukkit.scoreboard.Scoreboard;
    29. import org.bukkit.scoreboard.ScoreboardManager;
    30. import org.bukkit.scoreboard.Team;
    31.  
    32. public class Main extends JavaPlugin implements Listener{
    33.  
    34. public boolean bossBarEnabled;
    35.  
    36. public int thirst;
    37.  
    38. ScoreboardManager manager = Bukkit.getScoreboardManager();
    39. Scoreboard board = manager.getNewScoreboard();
    40. Objective objective = board.registerNewObjective("Thirst", "dummy");
    41.  
    42. public void onEnable(){
    43. PluginManager pm = getServer().getPluginManager();
    44. pm.registerEvents(this, this);
    45. this.saveDefaultConfig();
    46. Plugin BarAPI = pm.getPlugin("BarAPI");
    47. if(BarAPI == null){
    48. getLogger().severe("BarAPI not found! Disabling the boss bar visual display setting!");
    49. getConfig().set("bossBarEnabled", false);
    50. getLogger().info("Boss bar visual display setting disabled.");
    51. }else{
    52. getLogger().info("BarAPI detected, enabling boss bar visual display setting.");
    53. getConfig().set("bossBarEnabled", true);
    54. }
    55.  
    56. taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    57. @Override
    58. public void run() {
    59. doThirst();
    60. }
    61. }, 18 * 20L, 18 * 20L);
    62.  
    63. objective.setDisplayName("Thirst");
    64. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    65. reloadCustomConfig();
    66. }
    67.  
    68. private FileConfiguration customConfig = null;
    69. private File customConfigFile = null;
    70.  
    71. int display = 1;
    72.  
    73. public void reloadCustomConfig() {
    74. if (customConfigFile == null) {
    75. customConfigFile = new File(getDataFolder(), "thirstPlayers.yml");
    76. }
    77. customConfig = YamlConfiguration.loadConfiguration(customConfigFile);
    78.  
    79. // Look for defaults in the jar
    80. InputStream defConfigStream = this.getResource("thirstPlayers.yml");
    81. if (defConfigStream != null) {
    82. @SuppressWarnings("deprecation")
    83. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
    84. customConfig.setDefaults(defConfig);
    85. }
    86. }
    87.  
    88. public FileConfiguration getCustomConfig() {
    89. if(customConfig == null){
    90. reloadCustomConfig();
    91. }
    92. return customConfig;
    93. }
    94.  
    95. public void saveCustomConfig(){
    96. if(customConfig == null || customConfigFile == null){
    97. return;
    98. }
    99. try{
    100. getCustomConfig().save(customConfigFile);
    101. }
    102. catch(IOException e){
    103. e.printStackTrace();
    104. }
    105. }
    106.  
    107. public void doThirst(){
    108. for(Player p : Bukkit.getOnlinePlayers()){
    109. String pNames = p.getName();
    110. int oThirst = getConfig().getInt("Thirst." + pNames + ".Thirst");
    111. if(p.getGameMode().equals(GameMode.SURVIVAL)){
    112. int thirst = (oThirst - 1);
    113. getCustomConfig().set("Thirst." + pNames + ".Thirst", thirst);
    114. saveCustomConfig();
    115. }
    116. //TODO - REMOVE DEBUG CODE
    117. Bukkit.getServer().broadcast("DEBUG - WAVE OCCURED", "DEBUG - WAVE OCCURED");
    118. }
    119. }
    120.  
    121. public void displayVisualSystems(Player p, int display){
    122. switch(display){
    123. case 1:
    124. p.setScoreboard(board);
    125. Score score = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GREEN + "Thirst"));
    126. score.setScore(getCustomConfig().getInt("Players." + p.getName() + ".Thirst"));
    127. break;
    128. case 2:
    129. //chat notifications
    130. break;
    131. case 3:
    132. //boss bar
    133. break;
    134. }
    135. }
    136.  
    137. public void switchVisualSystem(Player p, int newDisplay, int oldDisplay){
    138. //Display tracking vars:
    139. // - Scoreboard: 1
    140. // - Chat notifications: 2
    141. // - BossBar (IF ENABLED): 3
    142. int displayVar = newDisplay + (oldDisplay*10);
    143. switch(displayVar){
    144. case 11:
    145. p.sendMessage("You cannot switch to a display you're already on!");
    146. break;
    147. case 12:
    148. p.sendMessage("Switchingd");
    149. //TODO - ADD SWITCHING CODE
    150. break;
    151. case 13:
    152. p.sendMessage("Switching");
    153. //TODO - ADD SWITCHING CODE
    154. break;
    155. case 21:
    156. p.sendMessage("Switching");
    157. //TODO - ADD SWITCHING CODE
    158. break;
    159. case 22:
    160. p.sendMessage("You cannot switch to a display you're already on!");
    161. break;
    162. case 23:
    163. p.sendMessage("Switching");
    164. //TODO - ADD SWITCHING CODE
    165. break;
    166. case 31:
    167. p.sendMessage("Switching");
    168. //TODO - ADD SWITCHING CODE
    169. break;
    170. case 32:
    171. p.sendMessage("Switching");
    172. //TODO - ADD SWITCHING CODE
    173. break;
    174. case 33:
    175. p.sendMessage("You cannot switch to a display you're already on!");
    176. break;
    177. }
    178. }
    179.  
    180. @Override
    181. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    182. if(cmd.getName().equalsIgnoreCase("thirst")){
    183. Player p = (Player) sender;
    184. int cThirst = getCustomConfig().getInt("Players." + p.getName()+ ".Thirst");
    185. sender.sendMessage("Your thirst is: " + cThirst);
    186. displayVisualSystems(p, 1);
    187. return true;
    188. }
    189. return false;
    190. }
    191.  
    192. private Integer taskID;
    193.  
    194. @EventHandler
    195. public void onPlayerJoin(PlayerJoinEvent e){
    196. //TODO- SETUP TASK SCHEDULER, .YML PLACEMENT, .YML CHECKS, AND DISPLAY CODE
    197. final Player p = e.getPlayer();
    198. if(!(getCustomConfig().isSet("Players." + p.getName() + ".Thirst"))){
    199. getCustomConfig().set("Players." + p.getName() + ".Thirst", 100);
    200. saveCustomConfig();
    201. }
    202. taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    203. @Override
    204. public void run() {
    205. int pThirst = getCustomConfig().getInt("Players." + p.getName()+ ".Thirst");
    206. if(p.getGameMode().equals(GameMode.SURVIVAL) || p.getGameMode().equals(GameMode.ADVENTURE)){
    207. thirst = pThirst-1;
    208. getCustomConfig().set("Players." + p.getName() + ".Thirst", thirst);
    209. saveCustomConfig();
    210. }else{
    211. thirst = pThirst;
    212. saveCustomConfig();
    213. }
    214.  
    215. }
    216. }, 18 * 20L, 18 * 20L);
    217. getCustomConfig().set("TaskIDs." + p.getName() + ".TaskID", taskID);
    218. saveCustomConfig();
    219. }
    220.  
    221. public void onPlayerLeave(PlayerQuitEvent e){
    222. Player p = e.getPlayer();
    223. taskID = getConfig().getInt("TaskIDs."+p.getName()+".taskID");
    224. if(!(taskID == null)){
    225. Bukkit.getScheduler().cancelTask(taskID);
    226. getCustomConfig().set("TaskIDs." + p.getName() + ".taskID", "Offline");
    227. saveConfig();
    228. }
    229. }
    230.  
    231. }
    232.  

    And it came up with this error.
    Code:java
    1.  
    2. [15:39:53] [Server thread/WARN]: [SurvivalThirst] Task #29 for SurvivalThirst v0.0 [INDEV] generated an exception
    3. java.lang.NoSuchMethodError: org.bukkit.Bukkit.getOnlinePlayers()Ljava/util/Collection;
    4. at io.github.Tudedude100.SurvivalThirst.Main.doThirst(Main.java:108) ~[?:?]
    5. at io.github.Tudedude100.SurvivalThirst.Main$1.run(Main.java:59) ~[?:?]
    6. at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftTask.run(CraftTask.java:53) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    7. at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:345) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    8. at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:600) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    9. at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    10. at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    11. at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    12. at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    13.  

    It appears to think that the getOnlinePlayers() method doesn't exist, however, Eclipse and the JavaDocs seem to disagree. Any ideas of how to fix this, or possibly alternate methods?
    PS: I do realize much of this code is unfinished. It is to a point where it shouldn't conflict with anything else though, and I've mostly started working on the arbitrary configuration part and the thirst wave part.

    Quick edit: I do realize the method in the code is Bukkit.getOnlinePlayers() not Bukkit.getServer().getOnlinePlayers(), but I've tried it both ways, and it gives the same error :I
     
  2. Offline

    Gnat008

    Tudedude
    In one of the latest updates, the return type of getOnlinePlayers() was changed. This will cause that error.
     
  3. Offline

    Tudedude

    Gnat008
    I just remembered reading about that... is there any way to achieve what I'm trying to do with an alternate method, or something like that?
     
  4. Offline

    Gnat008

    Tudedude
    You could just change how your plugin gets the data to use the other return type instead. It depends on who is going to use the plugin, and what their environment is like (version of CraftBukkit using the new method or not).

    *EDIT: For full compatibility, I suppose you could check to see what the method returns, and go from there.
     
  5. Offline

    Tudedude

    Gnat008 Ty for bringing up the new method or not. I realized I was building off of the 1.7.10 dev version, instead of the 1.7.9 beta. In 1.7.10, it's the stupid Collection<? extends Player> and in 1.7.9, it's a nice, easy to use Player[] array. Thank you so much for your help, and I'll get back when I have a solution, so if the general public has a problem like this, they have a solution to help them on their way. :)
     
  6. Offline

    Gnat008

    Tudedude
    Good luck, and glad to help!
     
  7. Offline

    Goblom

    Tudedude If you want it to be compatible with all versions here is a simple work around...

    Code:java
    1. public static List<Player> getOnlinePlayers() {
    2. List<Player> list = Lists.newArrayList();
    3. for (World world : Bukkit.getWorlds()) {
    4. list.addAll(world.getPlayers());
    5. }
    6. return Collections.unmodifiableList(list);
    7. }
     
  8. Offline

    Tudedude

    Goblom Oh, ty. I've already made a workaround for the current version, but when it needs to be updated, I'll use your method. Thanks! :D

    And to the general public, my workaround was, with Bukkit 1.7.9 R0.2, getting the array of players, using a for loop to count through and select all the players, and then do stuff to them with the method. For my thirst plugin, I did the following:
    Code:java
    1. -snip-
    2. public void onEnable(){
    3. taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    4. @Override
    5. public void run() {
    6. doThirst();
    7. }
    8. }, 18 * 20L, 18 * 20L);
    9. }
    10. -snip-
    11. public void doThirst(){
    12. Player[] p = Bukkit.getOnlinePlayers();
    13. for(int i=0; i<p.length; i++){
    14. Player tp = p[I];[/I]
    15. if(!(getServer().getPlayer(tp.getUniqueId()) == null)){
    16. if(!(tp.hasPermission("survivalthirst.bypass"))){
    17. if(tp.getGameMode() == GameMode.SURVIVAL){
    18. int oThirst = getCustomConfig().getInt("Players." + tp.getName() + ".Thirst");
    19. int thirst = oThirst - 1;
    20. getCustomConfig().set("Players." + tp.getName() + ".Thirst", thirst);
    21. tp.sendMessage("DEBUG - WAVE OCCURED");
    22. saveCustomConfig();
    23. }
    24. }
    25. }
    26. }
    27. }


    Edit: For the record, my code is formatted, but for some reason it removed the formatting...
    ¯\_(ツ)_/¯

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

    RawCode

    compile vs proper cbukkit version.
    read update logs
    ???
    profit
     
    Necrodoom and xTigerRebornx like this.
  10. Offline

    stormneo7

    Wasn't it
    Bukkit.getOnlinePlayers()
    instead of
    Bukkit.getServer().getOnlinePlayers()?....
     
Thread Status:
Not open for further replies.

Share This Page