SyncRepeatingTask in it's own Class

Discussion in 'Plugin Development' started by BurnerDiamond, Feb 8, 2015.

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

    BurnerDiamond

    Title says everything.

    Can I make a seperate class for the repeating task??

    Code:
    this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                                @Override
                                public void run() {
                                    if (timer != 0) {
                                        timer--;
                                        for (Player p : Bukkit.getOnlinePlayers()) {
                                            p.setLevel(timer);
                                        }
    
    
                                        if (timer == 0) {
                                            check = true;
                                        }
                                    }
                                }
                            }, 0L, 20L);
                        }
                    }
                }
     
  2. Offline

    Konato_K

  3. Offline

    BurnerDiamond

  4. Offline

    Konato_K

  5. Offline

    teej107

    And pass the right objects through the parameters.
     
  6. Offline

    BurnerDiamond

    @Konato_K

    I'm a little confused on what you mean. And doesn't a class extend runnable rather then implement?
     
  7. Offline

    Konato_K

    @BurnerDiamond Runnable is an interface, you can't extend it unless you're doing it in another interface, but you can't instantiate an interface
     
  8. Offline

    BurnerDiamond

    If I'm correct I should pass an instance of my main class with the onEnable(); and then do the repeating task.

    @Konato_K
     
  9. Offline

    Konato_K

    @BurnerDiamond The example you show does not need any arguments (at least not the runnable) so I don't know if you really need the instance of the task or not, but just define your runnable in another class and put your stuff there
     
  10. Offline

    BurnerDiamond

    @Konato_K

    One of my arguememts is if

    Code:
    If(gameStarted == true) {
    
    
    So I would have to pass an instance.

    I still don't understand by what you mean by define a runnable in another class.
     
  11. Offline

    Konato_K

    @BurnerDiamond In that case you only need to pass "gameStarted" not the whole instance of your plugin, but as I said, I don't know what exactly you need to do in your runnable, so you can pass whatever you need.

    And well, just make a new class, implement Runnable, and make a instance for the scheduler when you need it?
     
  12. Offline

    teej107

    To start a repeating task in a separate class, you need an instance of a Plugin since that is one of the required objects to create one. You can do this by passing your Plugin through the parameters of a method or constructor. You then need a Runnable. This can be a class that implements it or just an anonymous class.
     
  13. Offline

    BurnerDiamond

    @teej107

    So basically something like this...

    Code:
    public class Position {
        public Position(Main plugin) {
    @Konato_K
    @teej107

    I have a second question to ask.

    Basically I'm using my runnable to get the player location every second.

    Code:
    package me.bd.has;
    
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    import org.bukkit.scheduler.BukkitRunnable;
    
    import java.util.HashMap;
    
    /**
    * Created by maxmigliorini on 2/8/15.
    */
    public class Position {
    
        private HashMap<Player, Location> position = new HashMap<Player, Location>();
        private HashMap<Player, Integer> seconds = new HashMap<Player, Integer>();
    
        public Position(Main plugin) {
    
    
            plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
                @Override
                public void run() {
                   
                    for(Player p : Bukkit.getOnlinePlayers()) {
                   
                        if(position.get(p) != p.getLocation()) {
    
                            position.put(p, p.getLocation());
                            seconds.put(p, 0);
                       
                        }
                       
                        if(position.get(p) == p.getLocation()) {
                           
                            int time = seconds.get(p)++;
                           
                            seconds.put(p, )
                           
                        }
    
    
                    }
    
                }
            }, 0, 20L);
    
        }
    }

    How do I make it so from the hashmap I can do this.

    seconds.get(p)++;

    Basically add 1 from the integer from the hashmap.
     
    Last edited by a moderator: Feb 8, 2015
  14. Offline

    Konato_K

  15. Offline

    BurnerDiamond

    @Konato_K

    I can't do

    Seconds.put(p, seconds.get(p)++);

    It won't let me.
     
  16. Offline

    Konato_K

    @BurnerDiamond Maybe because "Seconds" has the S as uppercase?

    If the autounboxing doesn't work try to store the Integer somewhere to put it after you add one.
     
  17. Offline

    BurnerDiamond

    @Konato_K

    The capital S was just autocorrect.

    I tried just about everything.

    int x = seconds.get(p)++;

    Doesn't work
     
  18. Offline

    Konato_K

  19. Offline

    BurnerDiamond

    Unless I do

    X++

    @Konato_K

    It tells me variable expected.

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

    Konato_K

    @BurnerDiamond Yes, because the map returns a Integer, get the value of the integer and add 1 to it.
     
  21. Offline

    BurnerDiamond

    @Konato_K

    This should now work shouldn't it?

    Code:
    package me.bd.has;
    
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.scheduler.BukkitRunnable;
    
    import java.util.HashMap;
    
    /**
    * Created by maxmigliorini on 2/8/15.
    */
    public class Position {
    
        private HashMap<Player, Location> position = new HashMap<Player, Location>();
        private HashMap<Player, Integer> seconds = new HashMap<Player, Integer>();
    
        public Position(Main plugin) {
    
    
            plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
                @Override
                public void run() {
    
                    for(Player p : Bukkit.getOnlinePlayers()) {
    
                        if(position.get(p) != p.getLocation()) {
    
                            position.put(p, p.getLocation());
                            seconds.put(p, 1);
    
                        }
    
                        if(position.get(p) == p.getLocation()) {
    
                            int time = seconds.get(p);
    
                            seconds.put(p, time++);
    
                        }
    
                        if(seconds.get(p) == 5) {
                            p.getLocation().getBlock().setType(Material.BEACON);
                        }
    
    
                    }
    
                }
            }, 0, 20L);
    
        }
    }
    
    Do I have to put something in my main class:

    Code:
    package me.bd.has;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    /**
    * Created by maxmigliorini on 2/2/15.
    */
    
    public class Main extends JavaPlugin {
        public static Main plugin;
    
    
        @Override
        public void onEnable() {
    
            getServer().getPluginManager().registerEvents(new Blocks(), this);
            getServer().getPluginManager().registerEvents(new Menu(), this);
            getServer().getPluginManager().registerEvents(new Teams(), this);
    
        }
    
        @Override
        public void onDisable() {
    
        }
    
        private int timer = 60;
        public static boolean check = true;
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
            Player p = (Player) sender;
    
            if (cmd.getName().equalsIgnoreCase("start")) {
                if (p.isOp()) {
                    if (Bukkit.getOnlinePlayers().length == 100) {
                        p.sendMessage("Not enough players");
                    } else {
                        if(check == true) {
                            check = false;
                            p.sendMessage("Game will start");
                            this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                                @Override
                                public void run() {
                                    if (timer != 0) {
                                        timer--;
                                        for (Player p : Bukkit.getOnlinePlayers()) {
                                            p.setLevel(timer);
                                        }
    
    
                                        if (timer == 0) {
                                            check = true;
                                        }
                                    }
                                }
                            }, 0L, 20L);
                        }
                    }
                }
            }
            return false;
        }
    }
    
    It's not giving me errors, It's just not working.

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

    teej107

    @BurnerDiamond seconds.put(p, time++); You're still putting the old value in the Map. Not the new incremented value.
     
  23. Offline

    Konato_K

    @BurnerDiamond You need to create a Position instance in the onEnable (or wherever you use it)
     
  24. Offline

    BurnerDiamond

    @teej107
    I'm already doing it aren't I?

    @Konato_K
    How do I make an instance of it in my main class / onEnable?
     
  25. Offline

    teej107

  26. Offline

    BurnerDiamond

    @teej107

    seconds.put(p, time++);

    In my code... Already.

    @teej107
    @Konato_K

    It's only supposed to activate if

    if(gameStarted == true);

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

    teej107

    @BurnerDiamond Use the edit button rather than double posting!

    I told you what do do. Here is some test code. Hopefully the problem will make sense if you run it.
    Code:
    int i = 1;
    System.out.println(i++);
    i = 1;
    System.out.println(++i);
    i = 1;
    System.out.println(i + 1);
    Also, stop it with the static modifier. You don't need it.
    It is also useless to do
    Code:
    someBoolean == true
    You are using 3 booleans for an if statement and it's really repetitive.
     
  28. Offline

    BurnerDiamond

    @teej107

    I still don't quite get the ++ problem since I think I'm already doing it.

    If the Boolean is true should it automatically doing it? True or False?

    I will need the static modifier since I will have to access it from a seperate class, I didn't want to make an instance of the class for one HashMap.

    Secondly. I've already put time++ in my code!
     
  29. Offline

    Konato_K

    @BurnerDiamond

    Test his code

    You can just do "if(gameStarted)"

    No, you don't, you can add setters and getter methods for whatever you need, you do NOT need static.

    Are you making a Position instance like I said?
     
    teej107 likes this.
  30. Offline

    BurnerDiamond

    @Konato_K

    But why do I need to make an instance of it in the onEnable if it will go on only if the gameStarted == true?
     
Thread Status:
Not open for further replies.

Share This Page