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

    teej107

    No. Do you know what OOP is?
    http://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html

     
  2. Offline

    Konato_K

    @BurnerDiamond Well, I assumed you wanted your task to run in your onEnable, if you need it somewhere else then do it somewhere else, just be sure that you use it.
     
  3. Offline

    BurnerDiamond

    @teej107

    I was trying to avoid it but okay:

    public void getHashMap(HashMap hashmap) {
    return this.hashmap;

    Correct?

    @Konato_K

    Since this is a min game plugin I want it to run when gameStarted is true.

    I changed the code to this.

    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) {
    
            if(gameStarter == true) {
            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);
        }
    }
     
    Last edited by a moderator: Feb 9, 2015
  4. Offline

    Konato_K

    @BurnerDiamond You're not defining gameStarted in Position?

    Anyway, getting the whole hashmap it's pointless, the best would make little methods for the operations you're going to use from the Map (add, remove, put, etc)
     
  5. Offline

    BurnerDiamond

    @Konato_K

    This should work shouldn't it?

    Because it's not setting/sending me a message

    Code:
    public class Position {
    
        private HashMap<Player, Location> position = new HashMap<Player, Location>();
        private HashMap<Player, Integer> seconds = new HashMap<Player, Integer>();
    
        private boolean gameStarted = false;
    
        public Position(Main plugin) {
    
            if (gameStarted == false) {
    
    
                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);
                                p.sendMessage("You've transformed");
                            }
                        }
    
                    }
                }, 0, 20L);
    
            }
        }
    }
     
  6. Offline

    Konato_K

    @BurnerDiamond It looks like it will work, but did you made a instance of Position somewhere? Otherwise it will never be called.

    Edit: Why are you comparing Locations with == ?
     
  7. Offline

    BurnerDiamond

    @Konato_K I'll change the locations.

    So I would have to do

    if(gameStarted == false) {
    this.run();
     
  8. Offline

    nverdier

    @BurnerDiamond No, you have to actually create an instance in another class and call the method from there, have you done this? And you can just use
    Code:
    if (!gameStarted) {
    //Stuff
    }
     
  9. Offline

    BurnerDiamond

    @nverdier

    I was able to make a scheduler in my main class, why can't I also do it In this class?
     
  10. Offline

    nverdier

    @BurnerDiamond You can! Just create a constructor to pass an instance of the main class, and then schedule away!
     
  11. Offline

    BurnerDiamond

    @Konato_K
    @nverdier

    It won't let me pass an instance and this is what I did.

    Code:
    public void getInstance(Position position) {
            return this.getInstance();
    @nverdier

    Why am I passing an instance of the main class if I need the runnable to WORK in my main class. Wouldn't I have to pass an instance of my position class?

    Current classes:

    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;
    
        public Main() {
        }
    
        @Override
        public void onEnable() {
    
            getServer().getPluginManager().registerEvents(new Blocks(), this);
            getServer().getPluginManager().registerEvents(new Menu(), this);
            getServer().getPluginManager().registerEvents(new Teams(), this);
    
            plugin = this;
    
        }
    
        @Override
        public void onDisable() {
    
            plugin = null;
    
        }
    
    
    
    
        private int timer = 60;
        public boolean check = true;
        public boolean gameStarted = false;
    
        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;
                                        }
    
                                        if (timer == 10) {
                                            for (Player p : Bukkit.getOnlinePlayers()) {
                                                p.sendMessage("Game starting in 10 seconds ");
                                               
                                            }
                                        }
                                    }
                                }
                            }, 0L, 20L);
                        }
                    }
                }
            }
            return false;
        }
    }
    
    
    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);
                            p.sendMessage("You've transformed");
    
                        }
                    }
                }
            }, 0, 20L);
        }
    
    }
    I need the code to run in the main class.

    I honestly don't know how to send an instance to my main class and access run() since it keeps giving me errors. I've tried for hours. I tried making:

    private Position position;

    I don't know what to do...

    EDIT: I just noticed I double posted, I'm sorry

    EDIT by Timtower: merged posts. X 2
     
    Last edited by a moderator: Feb 9, 2015
Thread Status:
Not open for further replies.

Share This Page