Why does my first scheduler task work, but not the others?

Discussion in 'Plugin Development' started by InkzzzMC, Jan 21, 2015.

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

    InkzzzMC

    I'm creating a plugin, and it does many things once someone joins the game, but it only runs if(numbers == 20);

    Why don't the others run?

    The int number is 20.



    Code:
    Code:
        @SuppressWarnings("deprecation")
        @EventHandler()
        public void OnJoinEvent(final PlayerLoginEvent e) {
            final Player player = e.getPlayer();
    
            this.getServer().getScheduler()
                    .scheduleAsyncRepeatingTask(this, new Runnable() {
    
                        public void run() {
    
                            if (number != -1) {
                                if (number != 0) {
                                    if (number == 20) {
                                        for (int i = 0; i <= 120; i++) {
                                            player.sendMessage(" ");
                                            String w = getConfig().getString(
                                                    "bank-world");
                                            double x = getConfig().getDouble(
                                                    "Bank-Spawn.X");
                                            double y = getConfig().getDouble(
                                                    "Bank-Spawn.Y");
                                            double z = getConfig().getDouble(
                                                    "Bank-Spawn.Z");
                                            double yaw = getConfig().getDouble(
                                                    "bank-spawn.yaw");
                                            double pitch = getConfig().getDouble(
                                                    "bank-spawn.pitch");
                                            Location bank = new Location(
                                                    getServer().getWorld(w), x, y,
                                                    z, (float) yaw, (float) pitch);
                                            player.teleport(bank);
                                        }
                                        number--;
                                        if (number == 16) {
    
                                            String PoliceMSG = getConfig()
                                                    .getString("Police-Message");
                                            player.sendMessage(ChatColor.DARK_GRAY
                                                    + ""
                                                    + ChatColor.BOLD
                                                    + "========================================");
                                            player.sendMessage(" ");
                                            player.sendMessage(ChatColor
                                                    .translateAlternateColorCodes(
                                                            '&', PoliceMSG)
                                                    + "");
                                            player.sendMessage(" ");
                                            player.sendMessage(ChatColor.DARK_GRAY
                                                    + ""
                                                    + ChatColor.BOLD
                                                    + "========================================");
                                            player.playSound(player.getLocation(),
                                                    Sound.NOTE_PIANO, 1, 0);
                                        }
                                        number--;
                                        if (number == 12) {
                                            String RobberMSG = getConfig()
                                                    .getString("Robber-Message");
                                            String PlayerNameColor = getConfig()
                                                    .getString("Robber-Name-Color");
                                            String Robber = e.getPlayer().getName();
                                            player.sendMessage(ChatColor.DARK_GRAY
                                                    + ""
                                                    + ChatColor.BOLD
                                                    + "========================================");
                                            player.sendMessage(" ");
                                            player.sendMessage(ChatColor
                                                    .translateAlternateColorCodes(
                                                            '&', PlayerNameColor)
                                                    + Robber
                                                    + ""
                                                    + (ChatColor
                                                            .translateAlternateColorCodes(
                                                                    '&', RobberMSG) + " "));
                                            player.sendMessage(" ");
                                            player.sendMessage(ChatColor.DARK_GRAY
                                                    + ""
                                                    + ChatColor.BOLD
                                                    + "========================================");
                                            player.playSound(player.getLocation(),
                                                    Sound.NOTE_PIANO, 1, 0);
                                        }
                                        number--;
                                        if (number == 8) {
                                            player.sendMessage(ChatColor.DARK_GRAY
                                                    + ""
                                                    + ChatColor.BOLD
                                                    + "========================================");
                                            player.sendMessage(" ");
                                            player.sendMessage(" ");
                                            player.sendMessage(RED + "Warp" + GRAY
                                                    + " ›› " + WHITE
                                                    + "Warping to world spawn!");
                                            player.sendMessage(" ");
                                            player.sendMessage(" ");
                                            player.sendMessage(ChatColor.DARK_GRAY
                                                    + ""
                                                    + ChatColor.BOLD
                                                    + "========================================");
                                            player.playSound(player.getLocation(),
                                                    Sound.NOTE_PIANO, 1, 0);
                                        }
                                        number--;
                                        if (number == 4) {
                                            String w = getConfig().getString(
                                                    "spawn-world");
                                            double x = getConfig().getDouble(
                                                    "Spawn.X");
                                            double y = getConfig().getDouble(
                                                    "Spawn.Y");
                                            double z = getConfig().getDouble(
                                                    "Spawn.Z");
                                            double yaw = getConfig().getDouble(
                                                    "spawn.yaw");
                                            double pitch = getConfig().getDouble(
                                                    "spawn.pitch");
                                            Location spawn = new Location(
                                                    getServer().getWorld(w), x, y,
                                                    z, (float) yaw, (float) pitch);
                                            player.teleport(spawn);
    
                                            player.sendMessage(ChatColor.DARK_GRAY
                                                    + ""
                                                    + ChatColor.BOLD
                                                    + "========================================");
                                            player.sendMessage(" ");
                                            player.sendMessage(" ");
                                            player.sendMessage(RED
                                                    + "Warp"
                                                    + GRAY
                                                    + " ›› "
                                                    + WHITE
                                                    + "You have teleported to the prisons spawn!");
                                            player.sendMessage(" ");
                                            player.sendMessage(" ");
                                            player.sendMessage(ChatColor.DARK_GRAY
                                                    + ""
                                                    + ChatColor.BOLD
                                                    + "========================================");
                                            player.playSound(player.getLocation(),
                                                    Sound.NOTE_PIANO, 1, 0);
                                        }
                                        number--;
    
                                    }
                                }
                            }
    
                        }
                    }, 0L, 20L);
    Thanks.
     
  2. Offline

    ColonelHedgehog

    Well, for one thing, you're using un-threadsafe methods within your Async task.
     
  3. Offline

    fireblast709

    @InkzzzMC
    Code:
    if(number == 20)
    {
        if(number == 15)
        {
            System.out.println("Hello there, I am never executed.");
            System.out.println("That's because your if-statements are nested incorrectly ;)");
        }
    }
     
  4. Offline

    InkzzzMC

    How are they wrongly set out? Mine are exactly like your's. @fireblast709
     
  5. Offline

    fireblast709

    @InkzzzMC well mine are incorrect (read the code, especially the System.out.printlns)
     
  6. Offline

    InkzzzMC

    Un-safe?

    So how should they be nested? @fireblast709

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

    fireblast709

    @InkzzzMC
    Code:
    if(number == 20)
    and
    Code:
    if(number == 15)
    should never be nested in each other: number cannot be 20 and 15 at the same time :p.
     
  8. Offline

    InkzzzMC

    @fireblast709 I did that then they all play at the same time -_-

    code:
    Code:
                            if (number != -1);
                                if (number != 0);
                                    if (number == 20);
                                        for (int i = 0; i <= 120; i++) {
                                            player.sendMessage(" ");
                                        }
                                            String w = getConfig().getString(
                                                    "bank-world");
                                            double x = getConfig().getDouble(
                                                    "Bank-Spawn.X");
                                            double y = getConfig().getDouble(
                                                    "Bank-Spawn.Y");
                                            double z = getConfig().getDouble(
                                                    "Bank-Spawn.Z");
                                            double yaw = getConfig().getDouble(
                                                    "bank-spawn.yaw");
                                            double pitch = getConfig().getDouble(
                                                    "bank-spawn.pitch");
                                            Location bank = new Location(
                                                    getServer().getWorld(w), x, y,
                                                    z, (float) yaw, (float) pitch);
                                            player.teleport(bank);
                                       
                                        number--;
                                        if (number == 16);
    
                                            final String PoliceMSG = getConfig()
                                                    .getString("Police-Message");
                                            player.sendMessage(ChatColor.DARK_GRAY
                                                    + ""
                                                    + ChatColor.BOLD
                                                    + "========================================");
                                            player.sendMessage(" ");
                                            player.sendMessage(ChatColor
                                                    .translateAlternateColorCodes(
                                                            '&', PoliceMSG)
                                                    + "");
                                            player.sendMessage(" ");
                                            player.sendMessage(ChatColor.DARK_GRAY
                                                    + ""
                                                    + ChatColor.BOLD
                                                    + "========================================");
                                            player.playSound(player.getLocation(),
                                                    Sound.NOTE_PIANO, 1, 0);
                                       
                                        number--;
                                        if (number == 12);
                                            String RobberMSG = getConfig()
                                                    .getString("Robber-Message");
                                            String PlayerNameColor = getConfig()
                                                    .getString("Robber-Name-Color");
                                            String Robber = e.getPlayer().getName();
                                            player.sendMessage(ChatColor.DARK_GRAY
                                                    + ""
                                                    + ChatColor.BOLD
                                                    + "========================================");
                                            player.sendMessage(" ");
                                            player.sendMessage(ChatColor
                                                    .translateAlternateColorCodes(
                                                            '&', PlayerNameColor)
                                                    + Robber
                                                    + ""
                                                    + (ChatColor
                                                            .translateAlternateColorCodes(
                                                                    '&', RobberMSG) + " "));
                                            player.sendMessage(" ");
                                            player.sendMessage(ChatColor.DARK_GRAY
                                                    + ""
                                                    + ChatColor.BOLD
                                                    + "========================================");
                                            player.playSound(player.getLocation(),
                                                    Sound.NOTE_PIANO, 1, 0);
                                       
                                        number--;
                                        if (number == 8);
                                            player.sendMessage(ChatColor.DARK_GRAY
                                                    + ""
                                                    + ChatColor.BOLD
                                                    + "========================================");
                                            player.sendMessage(" ");
                                            player.sendMessage(" ");
                                            player.sendMessage(RED + "Warp" + GRAY
                                                    + " ›› " + WHITE
                                                    + "Warping to world spawn!");
                                            player.sendMessage(" ");
                                            player.sendMessage(" ");
                                            player.sendMessage(ChatColor.DARK_GRAY
                                                    + ""
                                                    + ChatColor.BOLD
                                                    + "========================================");
                                            player.playSound(player.getLocation(),
                                                    Sound.NOTE_PIANO, 1, 0);
                                       
                                         number--;
                                        if (number == 4);
                                            String w1 = getConfig().getString(
                                                    "spawn-world");
                                            double x1 = getConfig().getDouble(
                                                    "Spawn.X");
                                            double y1 = getConfig().getDouble(
                                                    "Spawn.Y");
                                            double z1 = getConfig().getDouble(
                                                    "Spawn.Z");
                                            double yaw1 = getConfig().getDouble(
                                                    "spawn.yaw");
                                            double pitch1 = getConfig().getDouble(
                                                    "spawn.pitch");
                                            Location spawn = new Location(
                                                    getServer().getWorld(w1), x1, y1,
                                                    z1, (float) yaw1, (float) pitch1);
                                            player.teleport(spawn);
    
                                            player.sendMessage(ChatColor.DARK_GRAY
                                                    + ""
                                                    + ChatColor.BOLD
                                                    + "========================================");
                                            player.sendMessage(" ");
                                            player.sendMessage(" ");
                                            player.sendMessage(RED
                                                    + "Warp"
                                                    + GRAY
                                                    + " ›› "
                                                    + WHITE
                                                    + "You have teleported to the prisons spawn!");
                                            player.sendMessage(" ");
                                            player.sendMessage(" ");
                                            player.sendMessage(ChatColor.DARK_GRAY
                                                    + ""
                                                    + ChatColor.BOLD
                                                    + "========================================");
                                            player.playSound(player.getLocation(),
                                                    Sound.NOTE_PIANO, 1, 0);
                                        }
    
    
                                   
                               
                           
                    }, 0L, 20L);
    
     
  9. Offline

    Lolmewn

    @InkzzzMC Think of it like this: If number is 20, how can it ever be 15 in the same block of code?
     
  10. Offline

    InkzzzMC

    @Lolmewn I still don't understand what I need to change from this?

    Code:

    Code:
        @SuppressWarnings("deprecation")
        @EventHandler()
        public void OnJoinEvent(final PlayerLoginEvent e) {
            final Player player = e.getPlayer();
    
            this.getServer().getScheduler()
                    .scheduleAsyncRepeatingTask(this, new Runnable() {
    
                        public void run() {
    
                            if (number != -1) {
                                if (number != 0) {
                                    if (number == 20) {
                                        for (int i = 0; i <= 120; i++) {
                                            player.sendMessage(" ");
                                        }
                                            String w = getConfig().getString(
                                                    "bank-world");
                                            double x = getConfig().getDouble(
                                                    "Bank-Spawn.X");
                                            double y = getConfig().getDouble(
                                                    "Bank-Spawn.Y");
                                            double z = getConfig().getDouble(
                                                    "Bank-Spawn.Z");
                                            double yaw = getConfig().getDouble(
                                                    "bank-spawn.yaw");
                                            double pitch = getConfig().getDouble(
                                                    "bank-spawn.pitch");
                                            Location bank = new Location(
                                                    getServer().getWorld(w), x, y,
                                                    z, (float) yaw, (float) pitch);
                                            player.teleport(bank);
                                    }
                                       
                                        number--;
                                        if (number == 16) {
    
                                            final String PoliceMSG = getConfig()
                                                    .getString("Police-Message");
                                            player.sendMessage(ChatColor.DARK_GRAY
                                                    + ""
                                                    + ChatColor.BOLD
                                                    + "========================================");
                                            player.sendMessage(" ");
                                            player.sendMessage(ChatColor
                                                    .translateAlternateColorCodes(
                                                            '&', PoliceMSG)
                                                    + "");
                                            player.sendMessage(" ");
                                            player.sendMessage(ChatColor.DARK_GRAY
                                                    + ""
                                                    + ChatColor.BOLD
                                                    + "========================================");
                                            player.playSound(player.getLocation(),
                                                    Sound.NOTE_PIANO, 1, 0);
                                        }
                                        number--;
                                        if (number == 12) {
                                            String RobberMSG = getConfig()
                                                    .getString("Robber-Message");
                                            String PlayerNameColor = getConfig()
                                                    .getString("Robber-Name-Color");
                                            String Robber = e.getPlayer().getName();
                                            player.sendMessage(ChatColor.DARK_GRAY
                                                    + ""
                                                    + ChatColor.BOLD
                                                    + "========================================");
                                            player.sendMessage(" ");
                                            player.sendMessage(ChatColor
                                                    .translateAlternateColorCodes(
                                                            '&', PlayerNameColor)
                                                    + Robber
                                                    + ""
                                                    + (ChatColor
                                                            .translateAlternateColorCodes(
                                                                    '&', RobberMSG) + " "));
                                            player.sendMessage(" ");
                                            player.sendMessage(ChatColor.DARK_GRAY
                                                    + ""
                                                    + ChatColor.BOLD
                                                    + "========================================");
                                            player.playSound(player.getLocation(),
                                                    Sound.NOTE_PIANO, 1, 0);
                                        }
                                        number--;
                                        if (number == 8) {
                                            player.sendMessage(ChatColor.DARK_GRAY
                                                    + ""
                                                    + ChatColor.BOLD
                                                    + "========================================");
                                            player.sendMessage(" ");
                                            player.sendMessage(" ");
                                            player.sendMessage(RED + "Warp" + GRAY
                                                    + " ›› " + WHITE
                                                    + "Warping to world spawn!");
                                            player.sendMessage(" ");
                                            player.sendMessage(" ");
                                            player.sendMessage(ChatColor.DARK_GRAY
                                                    + ""
                                                    + ChatColor.BOLD
                                                    + "========================================");
                                            player.playSound(player.getLocation(),
                                                    Sound.NOTE_PIANO, 1, 0);
                                        }
                                         number--;
                                        if (number == 4) {
                                            String w = getConfig().getString(
                                                    "spawn-world");
                                            double x = getConfig().getDouble(
                                                    "Spawn.X");
                                            double y = getConfig().getDouble(
                                                    "Spawn.Y");
                                            double z = getConfig().getDouble(
                                                    "Spawn.Z");
                                            double yaw = getConfig().getDouble(
                                                    "spawn.yaw");
                                            double pitch = getConfig().getDouble(
                                                    "spawn.pitch");
                                            Location spawn = new Location(
                                                    getServer().getWorld(w), x, y,
                                                    z, (float) yaw, (float) pitch);
                                            player.teleport(spawn);
    
                                            player.sendMessage(ChatColor.DARK_GRAY
                                                    + ""
                                                    + ChatColor.BOLD
                                                    + "========================================");
                                            player.sendMessage(" ");
                                            player.sendMessage(" ");
                                            player.sendMessage(RED
                                                    + "Warp"
                                                    + GRAY
                                                    + " ›› "
                                                    + WHITE
                                                    + "You have teleported to the prisons spawn!");
                                            player.sendMessage(" ");
                                            player.sendMessage(" ");
                                            player.sendMessage(ChatColor.DARK_GRAY
                                                    + ""
                                                    + ChatColor.BOLD
                                                    + "========================================");
                                            player.playSound(player.getLocation(),
                                                    Sound.NOTE_PIANO, 1, 0);
                                        }
                    }
    
    
                            }
                        }
                               
                           
                    }, 0L, 20L);
     
  11. Offline

    ColonelHedgehog

    Things involving world changing shouldn't be called asynchronously.
     
  12. Offline

    Lolmewn

    @InkzzzMC Read what I said, then read your code again. Then find
    if (number != -1) { if (number != 0) { if (number == 20) { ... }}}
     
  13. Offline

    mythbusterma

    @InkzzzMC

    Well, to be fair, a number that equals 20 will never equal -1 or 0.

    Perhaps your efforts would be better focused elsewhere? Perhaps The Java™ Tutorials?
     
  14. Offline

    InkzzzMC

    @mythbusterma @Lolmewn

    Now it only does if (number == 20) {

    How do I make it count down? Where do I put number--; ?

    Code:

    Code:
        @SuppressWarnings("deprecation")
        @EventHandler()
        public void OnJoinEvent(final PlayerLoginEvent e) {
            final Player player = e.getPlayer();
    
            this.getServer().getScheduler()
                    .scheduleAsyncRepeatingTask(this, new Runnable() {
    
                        public void run() {
    
                            if (number != -1) {
                                if (number != 0) {
                                    if (number == 20) {
                                        for (int i = 0; i <= 120; i++) {
                                            player.sendMessage(" ");
                                        }
                                        String w = getConfig().getString(
                                                "bank-world");
                                        double x = getConfig().getDouble(
                                                "Bank-Spawn.X");
                                        double y = getConfig().getDouble(
                                                "Bank-Spawn.Y");
                                        double z = getConfig().getDouble(
                                                "Bank-Spawn.Z");
                                        double yaw = getConfig().getDouble(
                                                "bank-spawn.yaw");
                                        double pitch = getConfig().getDouble(
                                                "bank-spawn.pitch");
                                        Location bank = new Location(getServer()
                                                .getWorld(w), x, y, z, (float) yaw,
                                                (float) pitch);
                                        player.teleport(bank);
                                    if (number == 16) {
    
                                        final String PoliceMSG = getConfig()
                                                .getString("Police-Message");
                                        player.sendMessage(ChatColor.DARK_GRAY
                                                + ""
                                                + ChatColor.BOLD
                                                + "========================================");
                                        player.sendMessage(" ");
                                        player.sendMessage(ChatColor
                                                .translateAlternateColorCodes('&',
                                                        PoliceMSG)
                                                + "");
                                        player.sendMessage(" ");
                                        player.sendMessage(ChatColor.DARK_GRAY
                                                + ""
                                                + ChatColor.BOLD
                                                + "========================================");
                                        player.playSound(player.getLocation(),
                                            Sound.NOTE_PIANO, 1, 0);
                                    if (number == 12) {
                                        String RobberMSG = getConfig().getString(
                                                "Robber-Message");
                                        String PlayerNameColor = getConfig()
                                                .getString("Robber-Name-Color");
                                         String Robber = e.getPlayer().getName();
                                        player.sendMessage(ChatColor.DARK_GRAY
                                                + ""
                                                + ChatColor.BOLD
                                                + "========================================");
                                        player.sendMessage(" ");
                                        player.sendMessage(ChatColor
                                                .translateAlternateColorCodes('&',
                                                        PlayerNameColor)
                                                + Robber
                                                + ""
                                                + (ChatColor
                                                        .translateAlternateColorCodes(
                                                                '&', RobberMSG) + " "));
                                        player.sendMessage(" ");
                                        player.sendMessage(ChatColor.DARK_GRAY
                                                + ""
                                                + ChatColor.BOLD
                                                + "========================================");
                                        player.playSound(player.getLocation(),
                                                Sound.NOTE_PIANO, 1, 0);
                                    if (number == 8) {
                                        player.sendMessage(ChatColor.DARK_GRAY
                                                + ""
                                                + ChatColor.BOLD
                                                + "========================================");
                                        player.sendMessage(" ");
                                        player.sendMessage(" ");
                                        player.sendMessage(RED + "Warp" + GRAY
                                                + " ›› " + WHITE
                                                + "Warping to world spawn!");
                                        player.sendMessage(" ");
                                        player.sendMessage(" ");
                                        player.sendMessage(ChatColor.DARK_GRAY
                                                + ""
                                                + ChatColor.BOLD
                                                + "========================================");
                                        player.playSound(player.getLocation(),
                                                Sound.NOTE_PIANO, 1, 0);
                                    if (number == 4) {
                                        String w1 = getConfig().getString(
                                                "spawn-world");
                                        double x1 = getConfig().getDouble("Spawn.X");
                                        double y1 = getConfig().getDouble("Spawn.Y");
                                        double z1 = getConfig().getDouble("Spawn.Z");
                                        double yaw1 = getConfig().getDouble(
                                                "spawn.yaw");
                                        double pitch1 = getConfig().getDouble(
                                                "spawn.pitch");
                                        Location spawn = new Location(getServer()
                                                .getWorld(w1), x1, y1, z1, (float) yaw1,
                                                (float) pitch1);
                                        player.teleport(spawn);
    
                                        player.sendMessage(ChatColor.DARK_GRAY
                                                + ""
                                                + ChatColor.BOLD
                                                + "========================================");
                                        player.sendMessage(" ");
                                        player.sendMessage(" ");
                                        player.sendMessage(RED
                                                + "Warp"
                                                + GRAY
                                                + " ›› "
                                                + WHITE
                                                + "You have teleported to the prisons spawn!");
                                        player.sendMessage(" ");
                                        player.sendMessage(" ");
                                        player.sendMessage(ChatColor.DARK_GRAY
                                                + ""
                                                + ChatColor.BOLD
                                                + "========================================");
                                        player.playSound(player.getLocation(),
                                                Sound.NOTE_PIANO, 1, 0);
                                    }
                                   
                                    }
                                   
                                    }
                                   
                                    }
                                   
                                    }
                                   
                                }
                               
    
                            }
                            number--;
                        }
    
                    }, 0L, 20L);
     
  15. Offline

    ColonelHedgehog

    Dude. Ditch the Async method.
     
  16. Offline

    Lolmewn

    @ColonelHedgehog I don't think he'll do that if he doesn't even understand that -1 != 0 != 20.
     
Thread Status:
Not open for further replies.

Share This Page