Help with broadcasting

Discussion in 'Plugin Development' started by DragonSlayer1920, Jul 25, 2013.

Thread Status:
Not open for further replies.
  1. Now, I'm fairly new to coding and make a couple mistakes now and then so this may be a quick fix but I could still use it
    Problem: I have coded a minigame plugin and I made a timer for the lobby, It works perfect except for the Game Starting... If Test.canStart is true then this happen
    http://imgur.com/S5fhUtI
    Or here is it in the consle
    Code:
    2013-07-25 18:53:59 [INFO] est] Game Starting... 
    2013-07-25 18:54:00 [INFO] est] Game Starting...
    2013-07-25 18:54:01 [INFO] est] Game Starting...
    2013-07-25 18:54:02 [INFO] est] Game Starting...
    2013-07-25 18:54:03 [INFO] est] Game Starting...
    2013-07-25 18:54:04 [INFO] est] Game Starting...
    2013-07-25 18:54:05 [INFO] est] Game Starting...
    2013-07-25 18:54:06 [INFO] est] Game Starting...
    2013-07-25 18:54:07 [INFO] [Test] Game Starting...
    And my consle's spammed
    Code:
    if(Test.timeInSeconds == 0) {
                [COLOR=#ff0000]if(Test.canStart)[/COLOR]
    [COLOR=#ff0000]                Bukkit.broadcastMessage(ChatColor.AQUA + "[Test] " + ChatColor.GRAY + "Game Starting...");[/COLOR]
                else {
                    Bukkit.broadcastMessage(ChatColor.AQUA + "[Test] " + ChatColor.GRAY + "Not EnoughPlayers, Restarting Timer");
     
  2. Offline

    Compressions

    DragonSlayer1920 Cancel the task after you get the desirable output.
    Code:
    this.cancel();
    or if you used a scheduler, assign the method an int:
    Code:
    Bukkit.getServer().getScheduler().cancelTask(taskId);
     
  3. Compressions , Thanks for responding so fast, anyways ill try that now
    EDIT: I'm using a scheduler, so where in my code would i put
    Code:
    Bukkit.getServer().getScheduler().canceltasl(taskId);
     
  4. Offline

    Compressions

  5. Compressions Didn't work, could you right me a little code?
     
  6. Offline

    soulofw0lf

    there is no reason to bump your own post 4 times in less than 15 minutes.
     
  7. Offline

    xTrollxDudex

  8. What exactly do you even mean

    Same with you xTrollxDudex
     
  9. Offline

    soulofw0lf

    bumping your own posting is posting on it after you were the last to post without anyone else posting in order to keep your post towards the top of the page, general rule of thumb around here is don't bump your post within the first 12 hours.
     
  10. Offline

    xTrollxDudex

    DragonSlayer1920
    He means that it looks like you are trying to bump the thread, bumping is when you reply to your thread with the intenet to move it up ao more people will see. A bump in this community is allowed only after there has been no activity whatsoever for 24 hours or more.

    Edit: ninja'd by soulofw0lf, its's actually 24 hours mate
     
  11. Oh sorry I didn't know
     
  12. Offline

    soulofw0lf

    learning new stuff daily
     
  13. Offline

    xTrollxDudex

  14. So now that we have got over all this bumping stuff can we please get back to my problem at hand.
     
  15. Offline

    xTrollxDudex

  16. Well, my errors are that if the game can start it spams the message "Game Starting... Good Luck" Over and Over and Over. I have to classes my main and my other with the timer.

    The main functions fine and everything in the timer works except what I said above here is a little more code then I said in my first post.

    Code:
    public class GameLoop implements Runnable {
     
        public void run() {
            if(Test.timeInSeconds >= 60 && Test.timeInSeconds % 60 == 0) {
                broadcastTimeUntillStart(true);
            }
            if(MinersCreed.timeInSeconds <= 30 && MinersCreed.timeInSeconds % 15 == 0
                    && MinersCreed.timeInSeconds > 0) {
                broadcastTimeUntillStart(true);
            }
            if(Test.timeInSeconds <= 10 && Test.timeInSeconds > 0) {
                broadcastTimeUntillStart(false);
                note();
            }
            if(Test.timeInSeconds == 0) {
                if(Test.canStart)
                    Bukkit.broadcastMessage(ChatColor.AQUA + "[Test] " + ChatColor.GRAY + "Game Starting... ");
                else {
                    Bukkit.broadcastMessage(ChatColor.AQUA + "[Test] " + ChatColor.GRAY + "Not Enough Players, Restarting Timer");
                    resetNote();
                    MinersCreed.timeInSeconds = 121;
                }
            }
            if (Test.timeInSeconds > 0){
                Test.timeInSeconds--;
            }
        }
     
        private void resetNote() {
            for(Player player : Bukkit.getOnlinePlayers()) {
                player.playSound(player.getLocation(), Sound.ANVIL_USE, 10, 1);
            }
        }
     
        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 broadcastTimeUntillStart(boolean extraArgs) {
            if (extraArgs) {
                Bukkit.broadcastMessage("EXTRA INFO!");
            }
         
            if(Test.timeInSeconds % 60 == 0 && Test.timeInSeconds >= 60) {
                Bukkit.broadcastMessage(ChatColor.AQUA + "Lobby: " + ChatColor.WHITE + Bukkit.getOnlinePlayers().length + ChatColor.AQUA + "/" + Bukkit.getMaxPlayers());
                Bukkit.broadcastMessage(Test.timeInSeconds  / 60 + " Minutes remain");
            } else if(Test.timeInSeconds % 15 == 0 && Test.timeInSeconds < 60) {
                Bukkit.broadcastMessage(Test.timeInSeconds + " Seconds remain");
            }
         
        }
     
    }
     
  17. Offline

    soulofw0lf

    if(Test.canStart)
    Bukkit.broadcastMessage(ChatColor.AQUA + "[Test] " + ChatColor.GRAY + "Game Starting... ");
    else {


    you don't have anything going on there so it is constantly at 0 seconds. you need to cancel out of your timer and put in your code to start your game.
     
  18. I have tried many methods of adding more code to Test.canStart but for some reason it gives me an error
     
  19. Offline

    soulofw0lf

    if(Test.canStart){
    ^^
    Bukkit.broadcastMessage(ChatColor.AQUA + "[Test] " + ChatColor.GRAY + "Game Starting... ");
    //just by adding these brackets you're fine
    \/
    } else {
     
  20. Offline

    xTrollxDudex

    DragonSlayer1920
    You can add an Id field to the runnable.
    PHP:
    public class Loop implements Runnable{
    int id;

    @
    Override
    public void run(){
    //code
    }
    //rest here
    public int getId(){return this.id;}
    public 
    void setId(int id){this.id id;}
    }
    //in the runnable
    Loop l = new Loop()
    l.setId(Bukkit.getScheduler().scheduleSyncRepeatingTask(thisl0L1L);
    while(
    /*this integer is larger,smaller or not equal to another...*/){
    if(
    /*the value equals what you want*/){
    Bukkit.getScheduler().cancelTask(l.getId());
    }
     
  21. Offline

    soulofw0lf

    while that is nice and fine and all i highly recommend using bukkit runnable...
    public void imAMethod(){
    new BukkitRunnable(){
    @Override
    public void run(){
    if (cancelconditionismet){
    cancel();
    }
    }
    }.runTaskTimer(plugin, 20, 20);
    }
     
  22. Thanks everyone for the suggestions but none work I get errors for everyone


    Anyone still there soulofw0lf xTrollxDudex
     
  23. Offline

    xTrollxDudex

  24. Offline

    Tarestudio

    DragonSlayer1920
    Take the extension with the id, mentioned by xTrollxDudex
    Now instead of using 'Bukkit.getScheduler().cancelTask(l.getId());' outside of the runnable, put it to your canStart-condition. Something like:
    Code:java
    1. if(Test.canStart) {
    2. Bukkit.broadcastMessage(ChatColor.AQUA + "[Test] " + ChatColor.GRAY + "Game Starting... ");
    3. Bukkit.getScheduler().cancelTask(id);
    4. } else {
     
Thread Status:
Not open for further replies.

Share This Page