Events Following Timer

Discussion in 'Plugin Development' started by BajanAmerican, Feb 12, 2013.

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

    BajanAmerican

    Code:
    public class CloudWars extends JavaPlugin implements Listener {
        public final Logger logger = Logger.getLogger("Minecraft");
        public static CloudWars plugin;
        public static int gameLoop = 0;
        public static int timeInSeconds;
        public static boolean canStart;
     
        public static ArrayList<Player> red = new ArrayList<Player>();
        public static ArrayList<Player> blue = new ArrayList<Player>();
     
        @Override
        public void onDisable() {
            getServer().getScheduler().cancelTask(gameLoop);
            this.logger.info("DISABLED!");
        }
     
        @Override
        public void onEnable() {
            timeInSeconds = 120;
            canStart = false;
            gameLoop = getServer().getScheduler().scheduleSyncRepeatingTask(this,
                    new GameLoop(), 20l, 20l);
            PluginDescriptionFile pdfFile = this.getDescription();
            logger.info("[" + pdfFile.getName() + "] v" + pdfFile.getVersion()
                    + " Has Been Enabled!");
        }
     
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
                canStart = Bukkit.getOnlinePlayers().length >= 4;
                event.setJoinMessage(null);
                Player p = event.getPlayer();
                Random rnd = new Random();
                World world = getServer().getWorld(getName());
                Location RedSpawn = new Location(world, 34, 71, 73);
                Location BlueSpawn = new Location(world, -25, 71, -85);
                if (rnd.nextInt(100) > 50) {
                    red.add(p);
                    p.sendMessage(ChatColor.LIGHT_PURPLE + "[Cloud Wars] "
                            + ChatColor.RED + "YOU ARE ON THE RED TEAM!");
                    p.teleport(RedSpawn);
                } else {
                    blue.add(p);
                    p.sendMessage(ChatColor.LIGHT_PURPLE + "[Cloud Wars] "
                            + ChatColor.BLUE + "YOU ARE ON THE BLUE TEAM!");
                    p.teleport(BlueSpawn);
                    if (red.size() > blue.size()) {
                        blue.add(p);
                    } else if (red.size() < blue.size()) {
                        red.add(p);
                    } else {
                        Integer team = new Random().nextInt(2);
                        if (team == 1) {
                            red.add(p);
                        } else {
                            blue.add(p);
                        }
                    }
                }
            }
     
        @EventHandler
        public void onPlayerLeave(PlayerQuitEvent event) {
            canStart = Bukkit.getOnlinePlayers().length - 1 >= 4;
        }
     
     
     
    public class GameLoop implements Runnable {
     
        public void run() {
            if (CloudWars.timeInSeconds >= 60 && CloudWars.timeInSeconds % 60 == 0) {
                broadcastTimeUntilStart(true);
            }
            if (CloudWars.timeInSeconds <= 30 && CloudWars.timeInSeconds % 15 == 0
                    && CloudWars.timeInSeconds > 0) {
                broadcastTimeUntilStart(true);
            }
            if (CloudWars.timeInSeconds <= 10 && CloudWars.timeInSeconds > 0) {
                broadcastTimeUntilStart(false);
                note();
            }
            if (CloudWars.timeInSeconds == 0) {
                if (CloudWars.canStart)
                    Bukkit.broadcastMessage(ChatColor.LIGHT_PURPLE
                            + "[Cloud Wars] " + ChatColor.RED
                            + "THE GAME HAS BEGUN!");
                else {
                    Bukkit.broadcastMessage(ChatColor.LIGHT_PURPLE
                            + "[Cloud Wars] " + ChatColor.DARK_AQUA
                            + "Not Enough Players! Resetting Timer!");
                    resetNote();
                    CloudWars.timeInSeconds = 121;
                }
            }
            if (CloudWars.timeInSeconds > 0) {
                CloudWars.timeInSeconds--;
            }
        }
    Please help. The timer does what it is suppose to, but after that, the game cannot start and it gives me the message "Not Enough Players! Resetting Timer!" even though there are more than 4 players. Literally that's what happens every time. Please respond! I really need some help here!
     
  2. Offline

    Tamewolf

    Rather than rewriting that same bit multiple times, you should create a method that can be called to adjust "canstart".

    Code:
    public void CanStart(int OnlinePlayers){
        if(){
            canStart = true;
        } else canStart = false;
    }
    Then replace all the parts where you have "canStart = bool;" with CanStart(Bukkit.getOnlinePlayers().length);

    You could also have another one that updates a variable that stores the amount of people online and adjust that with the join and quit events, saving the typing out of all that getOnlinePlayers() all the time.
     
  3. Offline

    BajanAmerican


    I know what you are saying, but my question is how do you start the split of the teams, and teleport them to their appropriate spawn point after the timer has reached o. I understand what you are saying timer-wise purposes, but I just need to know how to cast the event after the timer has reached 0. Thank you man!
     
  4. Offline

    Tamewolf

    The issue seems to be coming from that particular boolean. I would check to see how that is returning in your current code, and if it's not returning the value you want it to, adjust it.

    Considering the timer works, I'd add some debug messages that display what canStart is equal to when things that would change the boolean occur.
     
  5. Offline

    BajanAmerican

    So, I worked on the code a bit and the same error seems to keep arising. I did what you told me to do, and it just seems not to work. If you could tell me what I am doing wrong I would really appriciate it. I only started real programming a few months ago, and I am trying to make my first "move", if you want to call it that :) Honestly man, thank you!
    Code:
    public class CloudWars extends JavaPlugin implements Listener {
        public final Logger logger = Logger.getLogger("Minecraft");
        public static CloudWars plugin;
        public static int gameLoop = 0;
        public static int timeInSeconds;
        public static boolean canStart;
     
        public static ArrayList<Player> red = new ArrayList<Player>();
        public static ArrayList<Player> blue = new ArrayList<Player>();
     
        @Override
        public void onDisable() {
            getServer().getScheduler().cancelTask(gameLoop);
            this.logger.info("DISABLED!");
        }
     
        @Override
        public void onEnable() {
            timeInSeconds = 120;
            gameLoop = getServer().getScheduler().scheduleSyncRepeatingTask(this,
                    new GameLoop(), 20l, 20l);
            PluginDescriptionFile pdfFile = this.getDescription();
            logger.info("[" + pdfFile.getName() + "] v" + pdfFile.getVersion()
                    + " Has Been Enabled!");
        }
     
        public void CanStart(int OnlinePlayers){
            if(Bukkit.getOnlinePlayers().length >= 4){
                canStart = true;
            } else {
                canStart = false;
                Bukkit.broadcastMessage(ChatColor.BOLD + "THIS IS COMING FROM THE VOID");
            }
        }
     
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
                CanStart(Bukkit.getOnlinePlayers().length);
                event.setJoinMessage(null);
                Player p = event.getPlayer();
                Random rnd = new Random();
                World world = getServer().getWorld(getName());
                Location RedSpawn = new Location(world, 34, 71, 73);
                Location BlueSpawn = new Location(world, -25, 71, -85);
                if(timeInSeconds == 0 && CloudWars.canStart){
                if (rnd.nextInt(100) > 50) {
                    red.add(p);
                    p.sendMessage(ChatColor.LIGHT_PURPLE + "[Cloud Wars] "
                            + ChatColor.RED + "YOU ARE ON THE RED TEAM!");
                    p.teleport(RedSpawn);
                } else {
                    blue.add(p);
                    p.sendMessage(ChatColor.LIGHT_PURPLE + "[Cloud Wars] "
                            + ChatColor.BLUE + "YOU ARE ON THE BLUE TEAM!");
                    p.teleport(BlueSpawn);
                    if (red.size() > blue.size()) {
                        blue.add(p);
                    } else if (red.size() < blue.size()) {
                        red.add(p);
                    } else {
                        Integer team = new Random().nextInt(2);
                        if (team == 1) {
                            red.add(p);
                        } else {
                            blue.add(p);
                        }
                    }
                }
            }
        }
     
        @EventHandler
        public void onPlayerLeave(PlayerQuitEvent event) {
            CanStart(Bukkit.getOnlinePlayers().length - 1);
        }
     
     
     
    *NEW CLASS*
     
    public class GameLoop implements Runnable {
     
        public static ArrayList<Player> red = new ArrayList<Player>();
        public static ArrayList<Player> blue = new ArrayList<Player>();
     
        public void run() {
            if (CloudWars.timeInSeconds >= 60 && CloudWars.timeInSeconds % 60 == 0) {
                broadcastTimeUntilStart(true);
            }
            if (CloudWars.timeInSeconds <= 30 && CloudWars.timeInSeconds % 15 == 0
                    && CloudWars.timeInSeconds > 0) {
                broadcastTimeUntilStart(true);
            }
            if (CloudWars.timeInSeconds <= 10 && CloudWars.timeInSeconds > 0) {
                broadcastTimeUntilStart(false);
                note();
            }
            if (CloudWars.timeInSeconds == 0) {
                if (CloudWars.canStart)
                    Bukkit.broadcastMessage(ChatColor.LIGHT_PURPLE
                            + "[Cloud Wars] " + ChatColor.RED
                            + "THE GAME HAS BEGUN!");
                    else {
                    Bukkit.broadcastMessage(ChatColor.YELLOW + "THIS IS A BUG!");
                    resetNote();
                    CloudWars.timeInSeconds = 121;
                }
            }
            if (CloudWars.timeInSeconds > 0) {
                CloudWars.timeInSeconds--;
            }
        }
     
        private void resetNote() {
            for (Player player : Bukkit.getOnlinePlayers()) {
                player.playSound(player.getLocation(), Sound.NOTE_PIANO, 15, 2);
            }
        }
     
        private void note() {
            for (final Player player : Bukkit.getOnlinePlayers()) {
                final Location l = player.getLocation();
                new Thread(new Runnable() {
     
                    public void run() {
                        try {
                            for (int i = 0; i < 4; i++) {
                                player.playEffect(l, Effect.CLICK1, 1);
                                Thread.sleep(450);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        }
     
        private void broadcastTimeUntilStart(boolean extraArgs) {
            if (extraArgs) {
                Bukkit.broadcastMessage(ChatColor.GOLD + "FABIAN IS GAY!");
            }
            if (CloudWars.timeInSeconds % 60 == 0 && CloudWars.timeInSeconds >= 60) {
                Bukkit.broadcastMessage(ChatColor.LIGHT_PURPLE + "[Cloud Wars] "
                        + ChatColor.GOLD + CloudWars.timeInSeconds
                        + ChatColor.GREEN + " Seconds Until Start!");
            } else if (CloudWars.timeInSeconds < 30) {
                Bukkit.broadcastMessage(ChatColor.LIGHT_PURPLE + "[Cloud Wars] "
                        + ChatColor.GOLD + CloudWars.timeInSeconds
                        + ChatColor.GREEN + " Seconds Until The Game Starts!");
            }
        }
    }
    Once again, thanks!
     
Thread Status:
Not open for further replies.

Share This Page