Refresh Plugin every x ms

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

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

    The_Punkster

    Hey guys, I'm super new to development and want to make a plugin that check how many players are in each world and lists it, ideally in the end it'll show it on a scoreboard.

    Currently, my code aways shows 0 players on, now I can't tell if it's because I don't have anyhing to update the plugin every (e.g.) 100ms, or so, OR if it's simplt wrong code.

    Anyone care to help a newbie?

    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.event.Listener;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10.  
    11. public class Main extends JavaPlugin implements Listener {
    12.  
    13.  
    14. public static int survivalPlayers;
    15. public static int pvpPlayers;
    16. public static int skyworldPlayers;
    17. public static int hubPlayers;
    18.  
    19.  
    20.  
    21. @Override
    22. public void onEnable() {
    23.  
    24.  
    25.  
    26. survivalPlayers = 0;
    27. pvpPlayers = 0;
    28. skyworldPlayers = 0;
    29. hubPlayers = 0;
    30.  
    31. Player[] onlinePlayers = Bukkit.getOnlinePlayers();
    32.  
    33.  
    34. for (Player player : onlinePlayers){
    35.  
    36. if (player.getWorld().getName() == "pvp"){
    37. pvpPlayers = pvpPlayers +1;
    38.  
    39. } else if (player.getWorld().getName() == "survival") {
    40. survivalPlayers = survivalPlayers + 1;
    41.  
    42. } else if (player.getWorld().getName() == "skyworld") {
    43. skyworldPlayers = skyworldPlayers + 1;
    44.  
    45. } else if (player.getWorld().getName() == "newspawn") {
    46. hubPlayers = hubPlayers + 1;
    47.  
    48. } else {
    49. Bukkit.getServer().broadcastMessage("ERROR"); } }
    50. }
    51.  
    52.  
    53.  
    54.  
    55.  
    56. @Override
    57. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    58. if (cmd.getName().equalsIgnoreCase("worldstats")) {
    59.  
    60.  
    61. Bukkit.getServer().broadcastMessage("PVP: " + pvpPlayers);
    62. Bukkit.getServer().broadcastMessage("SURVIVAL: " + survivalPlayers);
    63. Bukkit.getServer().broadcastMessage("SKYBLOCK : " + skyworldPlayers);
    64. Bukkit.getServer().broadcastMessage("HUB: " + hubPlayers);
    65.  
    66. return true;
    67. }
    68.  
    69.  
    70.  
    71. return false;
    72.  
    73. }}
    74.  
    75.  
    76.  
     
  2. Offline

    drachenauge1000

    You could add your code into a repeating task. Something like that would help you out :

    Code:java
    1. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    2. @Override
    3. public void run() {
    4. // Your Code here
    5. }
    6. }, 0L, 20L);


    This will run your code every 20 Ticks (1 sec = 20 Ticks)

    See http://wiki.bukkit.org/Scheduler_Programming for more informations about scheduler programming.
     
  3. Offline

    Blockhaus2000

    Hi The_Punkster,

    Yeah, I see you're new in both Java and Bukkit plugin development. But I still want to answeryour question.

    But at first, two basic rules for Java programming:
    1.: Do not declare variables as public if you don't need them to be public.
    2.: Do not declare variables as static if you don't need thema to be static .

    And now the answer for your question:
    If you really want to refresh the list every XX seconds, the easiest way is to use the Bukkit scheduler (http://jd.bukkit.org/rb/apidocs/org/bukkit/scheduler/BukkitScheduler.html) with a timer task. In the anonymous class that implements Runnable, you'll call your method to update your counters. So you have to move all your code of the onEnable() method out into a new method like updatePlayerCounters() or something like that. Then you have to call that method in the onEnable() method (and in the timer, of course).
    Also, your plugin does nothing right now. Thats because you can not compare Strings with the equals-equals operator. You have to use equals(String str) or equalsIgnoreCase(Strign str) to compare them. Anyway, you have to assign values to your class variables. For now, only the local variables in your onEnable() method will be assigned with values. You have to assign the values of the local variables to the class variables. But that's inefficient. It will be more efficient if you use the post (or pre) increment expression.

    Also, I'll recommend you only to update the counters, if a players joins, quits or something like that (kick, etc.). But that's your choice.

    Greetings, Blockhaus2000

    PS: I recommend you to learn Java before you're programming Bukkit plugins. That's because the Bukkit API isn't as easy as it seems to be. Indeed, it's easy for use only, but if you want to programm efficient plugins (or plugins that are very powerful), you also have to understand some implementations of the Bukkit API.
     
  4. Offline

    The_Punkster

    I'd like to thank both of you for your help. Blockhaus2000 I was confused about declaring them publicly. At first they weren't, but if I didn't declare them as public, they wouldn't be recognized in the area where I declare the command.
     
  5. Offline

    AoH_Ruthless

    The_Punkster
    That's why you make getters for your variables, and use getters in other classes.
     
    ZodiacTheories likes this.
  6. Offline

    ZodiacTheories

    The_Punkster

    For future help, you could have also used a switch() and case statement :)
     
    AoH_Ruthless likes this.
  7. Offline

    The_Punkster

    ZodiacTheories

    Ah, remind me about VB again XD.

    Thanks, that would make it neater.
     
  8. Offline

    MineStein

    It appears you are refreshing the statistics you put in, while your topic says "Refresh Plugin every x ms".
    I am therefore going to assume you want the FileConfiguration reloaded in which case, it is extremely simple.

    Code:java
    1. if (cmd.getName().equalsIgnoreCase("xreload")) {
    2. if (!sender.hasPermission("xessentials.xreload")) {
    3. sender.sendMessage(ChatColor.RED + "You don't have permission!");
    4. return true;
    5. }
    6. settings.reloadConfig();
    7. sender.sendMessage(ChatColor.GREEN
    8. + "Reloaded config successfully!");
    9. return true;
    10. }


    This uses a SettingsManager with the method "reloadConfig"

    Code:java
    1. import java.io.File;
    2.  
    3. import java.io.IOException;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.configuration.file.FileConfiguration;
    8. import org.bukkit.configuration.file.YamlConfiguration;
    9. import org.bukkit.plugin.Plugin;
    10. import org.bukkit.plugin.PluginDescriptionFile;
    11.  
    12. public class SettingsManager {
    13.  
    14. private SettingsManager() {
    15.  
    16. }
    17.  
    18. static SettingsManager instance = new SettingsManager();
    19.  
    20. public static SettingsManager getInstance() {
    21. return instance;
    22. }
    23.  
    24. Plugin p;
    25. FileConfiguration config;
    26. File cFile;
    27.  
    28. public void setup(Plugin p) {
    29. config = p.getConfig();
    30. config.options().copyDefaults(true);
    31. cFile = new File(p.getDataFolder(), "config.yml");
    32. saveConfig();
    33. }
    34.  
    35. public FileConfiguration getConfig() {
    36. return config;
    37. }
    38.  
    39. public void saveConfig() {
    40. try {
    41. config.save(cFile);
    42. }
    43. catch(IOException e) {
    44. Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not save config.yml");
    45. }
    46. }
    47.  
    48. public void reloadConfig() {
    49. config = YamlConfiguration.loadConfiguration(cFile);
    50. }
    51.  
    52. public PluginDescriptionFile getDesc() {
    53. return p.getDescription();
    54. }
    55.  
    56. }
     
  9. Offline

    The_Punkster



    Thanks, actually it was my mistake in the title, it was the statistics refresh I needed.

    Btw guys, I got it working now with a scoreboard that shows how many people are on in each worlds :D
     
Thread Status:
Not open for further replies.

Share This Page