Chest refill every X minutes within area

Discussion in 'Plugin Development' started by KarimAKL, May 23, 2018.

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

    KarimAKL

    The title pretty much says it all, i want to make a plugin that refills all chests within a certain area(my arena) every X minutes. The reason i made this thread is because i don't know where i should start and also i have tried searching about this for the last 1-2 hours and couldn't find anything that helped me get started with this. I tried searching how to make a runnable but all the answers was this:
    Code:
    Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    @Override
    public void run() {
    // Do something
    }
    }, 0L, 20L);
    
    But whenever i tried this, the "Bukkit." came with this error:
    "Multiple markers at this line
    - Syntax error, insert ";" to complete MethodDeclaration
    - Syntax error, insert "Identifier (" to complete
    MethodHeaderName
    - Syntax error, insert ")" to complete MethodDeclaration
    - Syntax error, insert "}" to complete ClassBody"
    And i don't know why i can't use "Bukkit.", i tried "this.", "plugin." and "plugin.getServer()." aswell but they all came with errors, i just want to create a class with the runnable like this:
    Code:
    public class ClassName {
      
        private Main plugin;
      
        public ClassName(Main plugin) {
            this.plugin = plugin;
        }
      
        Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
            @Override
            public void run() {
                // Do something
            }
        }, 0L, 20L);
    }
    
    The error is probably pretty obvious but i can't find it or anything about it. :/
    Anyway, how would i make all chests within a certain area refill every X minutes?
     
  2. Online

    timtower Ninja on the waves Moderator

  3. Offline

    KarimAKL

    @timtower But what method should i put it in? Like an event or something like that? But if i do that then it'll only be whenever that event is executed and not every X minutes. :/
    EDIT: Or do you mean that i should put it in a public void method and call that method when needed?
     
  4. Online

    timtower Ninja on the waves Moderator

    In the constructor of the class you posted is probably a good place.
     
  5. Offline

    KarimAKL

    @timtower Like this?:
    Code:
    public class ClassName {
       
        private Main plugin;
       
        public ClassName(Main plugin) {
            this.plugin = plugin;
            Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
                @Override
                public void run() {
                    // Do something
                }
            }, 0L, 20L);
        }
    }
    
     
  6. Online

    timtower Ninja on the waves Moderator

  7. Offline

    KarimAKL

    @timtower Then how would i call it every X minutes?
    EDIT: Just by doing "new ClassName(this);" in the onEnable?
     
  8. Online

    timtower Ninja on the waves Moderator

    @KarimAKL Depends on how you use it. If it calls 1 method that is in a different class then I would put the call in the onEnable.

    And it takes an amount of ticks. 1 second is 20 ticks. You can do the math to make it minutes or hours.
     
  9. Offline

    KarimAKL

    @timtower Yeah, i haven't done anything with the runnable yet but this is how i plan to use the Main and Runnable class:
    Main
    Code:Java
    1.  
    2. public class Main extends JavaPlugin {
    3.  
    4. public void onEnable() {
    5. new ClassName(this);
    6. }
    7. }
    8.  

    Runnable class:
    Code:Java
    1.  
    2. public class ClassName {
    3.  
    4. private Main plugin;
    5.  
    6. public ClassName(Main plugin) {
    7. this.plugin = plugin;
    8. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    9. @Override
    10. public void run() {
    11. // Do something
    12. }
    13. }, 0L, 20L);
    14. }
    15. }
    16.  
     
  10. Online

    timtower Ninja on the waves Moderator

    @KarimAKL That is an option, but it is better to have an instance of the thing so that you can stop it as well.
     
  11. Offline

    KarimAKL

    @timtower Would it be possible to make it so that it only runs when a minimum of 1 player is online? (Then it won't run when no-one is online)
     
  12. Online

    timtower Ninja on the waves Moderator

    @KarimAKL You can add a check to the runnable in the first line.
     
  13. Offline

    KarimAKL

    @timtower Like this?:
    Code:Java
    1.  
    2. public class ClassName {
    3.  
    4. private Main plugin;
    5.  
    6. public ClassName(Main plugin) {
    7. this.plugin = plugin;
    8. if (any players are online) {
    9. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    10. @Override
    11. public void run() {
    12. // Do something
    13. }
    14. }, 0L, 20L);
    15. }
    16. }
    17. }
    18.  

    I don't know how to check if any players are online. :p Could you let me know?
     
  14. Online

    timtower Ninja on the waves Moderator

    @KarimAKL No, the check should be INSIDE the run. Because it gets started once.
    And check if the collection of onlineplayers has anything in it.
     
  15. Offline

    KarimAKL

    @timtower Oh, like this then?:
    Code:Java
    1.  
    2. public class ClassName {
    3.  
    4. private Main plugin;
    5.  
    6. public ClassName(Main plugin) {
    7. this.plugin = plugin;
    8. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    9. @Override
    10. public void run() {
    11. if (any players are online) {
    12. // Do something
    13. }
    14. }
    15. }, 0L, 20L);
    16. }
    17. }
    18.  

    Still don't know how to check if any players are online. :/ Did a quick search but didn't find anything.
    EDIT: My guess is i need to loop through all players and check if any of them are online or something like that.
     
  16. Online

    timtower Ninja on the waves Moderator

    @KarimAKL Like that.
    And how about .empty() or .size() ?
     
  17. Offline

    KarimAKL

    @timtower Okay, thanks. :) And would .empty() check if the server is empty of players or what does it do?
     
  18. Online

    timtower Ninja on the waves Moderator

    You run it on a collection, empty collection means empty server.
     
  19. Offline

    KarimAKL

    @timtower I don't know what a collection is or how i use it. :/
     
  20. Online

    timtower Ninja on the waves Moderator

    Look into getOnlinePlayers()
     
  21. Offline

    KarimAKL

    @timtower Would this work?
    Code:Java
    1.  
    2. public class ClassName {
    3.  
    4. private Main plugin;
    5.  
    6. public ClassName(Main plugin) {
    7. this.plugin = plugin;
    8. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    9. @Override
    10. public void run() {
    11. if (!plugin.getServer().getOnlinePlayers().isEmpty()) {
    12. // Do something
    13. }
    14. }
    15. }, 0L, 20L);
    16. }
    17. }
    18.  
     
  22. Online

    timtower Ninja on the waves Moderator

    @KarimAKL Runs every second but that can be changed.
    And change Runnable into BukkitRunnable please.
     
  23. Offline

    KarimAKL

    @timtower The "20L" at the bottom is the time in ticks, right? Also i tried changing the "new Runnable" to "new BukkitRunnable" then import and it came with alot of errors and deprecated and stuff, how would i change it to BukkitRunnable?
     
  24. Online

    timtower Ninja on the waves Moderator

    @KarimAKL That is in ticks yes.
    And use runTaskTimer then instead of scheduleSyncRepeatingTask
     
  25. Offline

    KarimAKL

    @timtower runTaskTimer would still repeat doing this every X time i put at the "20L", right?
     
  26. Online

    timtower Ninja on the waves Moderator

    Did you check the javadocs? Did you check what is available?
     
  27. Offline

    KarimAKL

    @timtower No i haven't, but doesn't it show what is available when i hover over the text? Anyway i tried this but it still came with errors:
    Code:Java
    1.  
    2. public class ClassName {
    3.  
    4. private Main plugin;
    5.  
    6. public ClassName(Main plugin) {
    7. this.plugin = plugin;
    8. Bukkit.getServer().getScheduler().runTaskTimer(plugin, new BukkitRunnable(), 0, 20) {
    9. @Override
    10. public void run() {
    11. if (!plugin.getServer().getOnlinePlayers().isEmpty()) {
    12. // Do something
    13. }
    14. }
    15. }
    16. }
    17. }
    18.  

    I don't know how i should use this "runTaskTimer" method. :/
    EDIT: New code, but still doesn't work:
    Code:Java
    1.  
    2. public class ClassName {
    3.  
    4. private Main plugin;
    5.  
    6. public ClassName(Main plugin) {
    7. this.plugin = plugin;
    8. Bukkit.getScheduler().runTaskTimer(plugin, new BukkitRunnable() {
    9. @Override
    10. public void run() {
    11. if (!plugin.getServer().getOnlinePlayers().isEmpty()) {
    12. // Do something
    13. }
    14. }
    15. }, 0, 20);
    16. }
    17. }
    18.  

    EDIT: Looks like i found something that doesn't come with any errors, is this correct?:
    Code:Java
    1.  
    2. public class ClassName {
    3.  
    4. private Main plugin;
    5.  
    6. public ClassName(Main plugin) {
    7. this.plugin = plugin;
    8. new BukkitRunnable() {
    9. @Override
    10. public void run() {
    11. if (!plugin.getServer().getOnlinePlayers().isEmpty()) {
    12. // Do something
    13. }
    14. }
    15. }.runTaskTimer(plugin, 0, 20);
    16. }
    17. }
    18.  
     
    Last edited: May 23, 2018
  28. Online

    timtower Ninja on the waves Moderator

  29. Offline

    KarimAKL

    @timtower Just tried testing it by doing this:
    Code:Java
    1.  
    2. public class ClassName {
    3.  
    4. @SuppressWarnings("unused")
    5. private Main plugin;
    6.  
    7. public ClassName(Main plugin) {
    8. this.plugin = plugin;
    9. new BukkitRunnable() {
    10. @Override
    11. public void run() {
    12. if (!plugin.getServer().getOnlinePlayers().isEmpty()) {
    13. Bukkit.getConsoleSender().sendMessage("It works!");
    14. }
    15. }
    16. }.runTaskTimer(plugin, 0, 200);
    17. }
    18. }
    19.  

    And the Main class is just like this:
    Code:Java
    1.  
    2. public class Main extends JavaPlugin {
    3.  
    4. public void onEnable() {
    5. new ClassName(this);
    6. }
    7. }
    8.  

    And it sends the message "It works!" to the console every 10 seconds if i'm on the server, if not it doesn't do anything so i guess it works. :p On to the next thing, how would i refill all chests within a certain area?
     
  30. Online

    timtower Ninja on the waves Moderator

    @KarimAKL Depends on what you call refill.
    And do you have a list of those chests? Or do you need to search for them every time?
     
Thread Status:
Not open for further replies.

Share This Page