if statement can't detect last step..

Discussion in 'Plugin Development' started by PlayFriik, Dec 13, 2014.

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

    PlayFriik

    Hello, I am trying to make that plugin broadcasts timeleft time..
    Everything works except the last one: it don't broadcast that the time is over... I have tried so many things, but still, just nothing.

    UltraHardcoreCommad
    Code:
    package net.playfriik.ultrahardcore.commands;
    
    import net.playfriik.ultrahardcore.UltraHardcore;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    
    public class UltraHardcoreCommand implements CommandExecutor {
    
        public boolean isRunning = false;
        UltraHardcore plugin;
    
        public UltraHardcoreCommand(UltraHardcore plugin) {
            this.plugin = plugin;
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (args.length == 0) {
                sender.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "UltraHardcore");
                sender.sendMessage(ChatColor.GRAY + "/uhc start" + ChatColor.DARK_GRAY + " - " + ChatColor.AQUA + "Start the game.");
                sender.sendMessage(ChatColor.GRAY + "/uhc stop" + ChatColor.DARK_GRAY + " - " + ChatColor.AQUA + "Stop the game.");
                sender.sendMessage(ChatColor.GRAY + "/uhc join" + ChatColor.DARK_GRAY + " - " + ChatColor.AQUA + "Join the game.");
                sender.sendMessage(ChatColor.GRAY + "/uhc leave" + ChatColor.DARK_GRAY + " - " + ChatColor.AQUA + "Leave the game.");
            }
    
            if (args.length == 1) {
                if (isRunning == false) {
                    if (args[0].equalsIgnoreCase("start")) {
                        isRunning = true;
                        gameTime();
                        sender.sendMessage(ChatColor.GREEN + "The game has been started!");
                    }
                }
    
                if (isRunning == true) {
                    if (args[0].equalsIgnoreCase("stop")) {
                        isRunning = false;
                        Bukkit.getScheduler().cancelAllTasks();
                        sender.sendMessage(ChatColor.RED + "The game has been stopped!");
                    }
                }
            }
            return true;
        }
    
        public void gameTime() {
            Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                public void run() {
                    int gameTime = 1 + plugin.getConfig().getInt("gameTime");
                    gameTime--;
                
                    if (gameTime == 1) {
                        Bukkit.broadcastMessage(ChatColor.RED + "Meetup started!");
                        meetupTime();
                        Bukkit.getScheduler().cancelTask(gameTime);
                    } else {
                        if (gameTime == 2) {
                            Bukkit.broadcastMessage(ChatColor.GREEN + "1 minute until meetup!");
                        } else {
                            if (gameTime == 6) {
                                Bukkit.broadcastMessage(ChatColor.GREEN + "5 minutes left until meetup");
                            } else {
                                if (gameTime == 11) {
                                    Bukkit.broadcastMessage(ChatColor.GREEN + "10 minutes left until meetup");
                                } else {
                                    if (gameTime == 21) {
                                        Bukkit.broadcastMessage(ChatColor.GREEN + "20 minutes left until meetup");
                                    } else {
                                        if (gameTime == 31) {
                                            Bukkit.broadcastMessage(ChatColor.GREEN + "30 minutes left until meetup");
                                        } else {
                                            if (gameTime == 61) {
                                                Bukkit.broadcastMessage(ChatColor.GREEN + "60 minutes left until meetup");
                                            } else {
                                                if (gameTime == 91) {
                                                    Bukkit.broadcastMessage(ChatColor.GREEN + "90 minutes left until meetup");
                                                } else {
                                                    if (gameTime == 121) {
                                                        Bukkit.broadcastMessage(ChatColor.GREEN + "120 minutes left until meetup");
                                                    } else {
                                                        if (gameTime == 151) {
                                                            Bukkit.broadcastMessage(ChatColor.GREEN + "150 minutes left until meetup");
                                                        } else {
                                                            if (gameTime == 181) {
                                                                Bukkit.broadcastMessage(ChatColor.GREEN + "180 minutes left until meetup");
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }, 60 * 20);
        }
    
        public void meetupTime() {
            Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    
                public void run() {
                    int meetupTime = 1 + plugin.getConfig().getInt("meetupTime");
                    meetupTime--;
    
                    if (meetupTime == 1) {
                        Bukkit.broadcastMessage(ChatColor.RED + "Meetup has been ended!");
                        Bukkit.getScheduler().cancelTask(meetupTime);
                    } else {
                        if (meetupTime == 2) {
                            Bukkit.broadcastMessage(ChatColor.GREEN + "1 minute until draw!");
                        } else {
                            if (meetupTime == 6) {
                                Bukkit.broadcastMessage(ChatColor.GREEN + "5 minutes until draw!");
                            } else {
                                if (meetupTime == 11) {
                                    Bukkit.broadcastMessage(ChatColor.GREEN + "10 minutes until draw!");
                                } else {
                                    if (meetupTime == 21) {
                                        Bukkit.broadcastMessage(ChatColor.GREEN + "20 minutes until draw!");
                                    } else {
                                        if (meetupTime == 31) {
                                            Bukkit.broadcastMessage(ChatColor.GREEN + "30 minutes until draw!");
                                        } else {
                                            if (meetupTime == 61) {
                                                Bukkit.broadcastMessage(ChatColor.GREEN + "60 minutes until draw!");
                                            } else {
                                                if (meetupTime == 91) {
                                                    Bukkit.broadcastMessage(ChatColor.GREEN + "90 minutes until draw!");
                                                } else {
                                                    if (meetupTime == 121) {
                                                        Bukkit.broadcastMessage(ChatColor.GREEN + "120 minutes until draw!");
                                                    } else {
                                                        if (meetupTime == 151) {
                                                            Bukkit.broadcastMessage(ChatColor.GREEN + "150 minutes until draw!");
                                                        } else {
                                                            if (meetupTime == 181) {
                                                                Bukkit.broadcastMessage(ChatColor.GREEN + "180 minutes until draw!");
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }, 60 * 20);
        }
    }
    It don't detect or work:
    Code:
                   if (gameTime == 1) {
                       Bukkit.broadcastMessage(ChatColor.RED + "Meetup started!");
                       meetupTime();
                       Bukkit.getScheduler().cancelTask(gameTime);
                   }
    
     
  2. Offline

    CodingAddiction

    Instead of using huge schedulers, I would just make a class that manages all of this with System.currentTimeMillis(). If you wanna do it this way, do some research on runnables and the System.currentTimeMillis() long value. Making countdowns with schedulers is super glitchy and inefficient.
     
  3. Offline

    Tehmaker

    @PlayFriik

    Code:
    [LIST=1]
    [*] public void gameTime() {
    [*]        Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    [*]            public void run() {
    [*]                int gameTime = 1 + plugin.getConfig().getInt("gameTime");
    [*]                gameTime--;
    [*]           PRINT GAMETIME TO PLAYER HERE
    [*]                if (gameTime == 1) {
                       PRINT GAMETIME TO PLAYER HERE
    [*]                    Bukkit.broadcastMessage(ChatColor.RED + "Meetup started!");
    [*]                    meetupTime();
    [*]                    Bukkit.getScheduler().cancelTask(gameTime);
    [*]                } else {
                          PRINT GAMETIME TO PLAYER HERE
    [*]                    if (gameTime == 2) {
    [*]                        Bukkit.broadcastMessage(ChatColor.GREEN + "1 minute until meetup!");
    [*]                    } else {
    [*]                        if (gameTime == 6) {
    [*]                            Bukkit.broadcastMessage(ChatColor.GREEN + "5 minutes left until meetup");
    [*]                        } else {
    [*]                            if (gameTime == 11) {
    [*]                                Bukkit.broadcastMessage(ChatColor.GREEN + "10 minutes left until meetup");
    [*]                            } else {
    [*]                                if (gameTime == 21) {
    [*]                                    Bukkit.broadcastMessage(ChatColor.GREEN + "20 minutes left until meetup");
    [*]                                } else {
    [*]                                    if (gameTime == 31) {
    [*]                                        Bukkit.broadcastMessage(ChatColor.GREEN + "30 minutes left until meetup");
    [*]                                    } else {
    [*]                                        if (gameTime == 61) {
    [*]                                            Bukkit.broadcastMessage(ChatColor.GREEN + "60 minutes left until meetup");
    [*]                                        } else {
    [*]                                            if (gameTime == 91) {
    [*]                                                Bukkit.broadcastMessage(ChatColor.GREEN + "90 minutes left until meetup");
    [*]                                            } else {
    [*]                                                if (gameTime == 121) {
    [*]                                                    Bukkit.broadcastMessage(ChatColor.GREEN + "120 minutes left until meetup");
    [*]                                                } else {
    [*]                                                    if (gameTime == 151) {
    [*]                                                        Bukkit.broadcastMessage(ChatColor.GREEN + "150 minutes left until meetup");
    [*]                                                    } else {
    [*]                                                        if (gameTime == 181) {
    [*]                                                            Bukkit.broadcastMessage(ChatColor.GREEN + "180 minutes left until meetup");
    [*]                                                        }
    [*]                                                    }
    [*]                                                }
    [*]                                            }
    [*]                                        }
    [*]                                    }
    [*]                                }
    [*]                            }
    [*]                        }
    [*]                    }
    [*]                }
    [*]            }
    [*]        }, 60 * 20);
    [*]    }
    [/LIST]
    
    Try debugging like this, and checking what the gametime actually is. I feel like it just isn't 1....



    How so?
     
  4. Offline

    CodingAddiction

    Since you'll have to keep giving the time value a new value every certain amount of time, it does make it inefficient.

    EDIT: It may work, but a ton of these countdowns in a server would make it lag a lot.
     
  5. Offline

    PlayFriik

    [15:10:42 INFO]: The game has been started!
    [15:11:42 INFO]: Testing2
    [15:11:42 INFO]: Testing2
    [15:11:42 INFO]: 1 minute until meetup!

    And it stuck at this point, the last if is glitched or sth
     
  6. Offline

    Rocoty

    @CodingAddiction Not sure if you realise the value of currentTimeMillis() is updated 1000 times a second. My point is, updating an int value every second is NOT inefficient.


    @PlayFriik Have you ever heard of switch-statements? You should try it to make your code tidier.
     
  7. Offline

    hexaan

    @PlayFriik

    Look good at your logic.

    You keep setting gameTime to whatever is in the config and then doing gameTime--;

    So every time you run that code you will get the same result.
    Code:
    int gameTime = 1 + plugin.getConfig().getInt("gameTime");
    gameTime--;
     
  8. Offline

    PlayFriik

    Yep, I have and I even learned it. :)
    Wait a second. Why it is not saying every minute "1 minute until meetup" then?
     
    Last edited: Dec 13, 2014
  9. Offline

    Rocoty

    @PlayFriik Could you post all your code and the output when you run it? Also check if you get console errors, and post them if you have.
     
  10. Offline

    PlayFriik

    @Rocoty

    UltraHardcore:
    Code:
    package net.playfriik.ultrahardcore;
    
    import net.playfriik.ultrahardcore.commands.UltraHardcoreCommand;
    import net.playfriik.ultrahardcore.listeners.PlayerJoinListener;
    
    import org.bukkit.Bukkit;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class UltraHardcore extends JavaPlugin implements Listener {
     
        public void onEnable() {
            Bukkit.getLogger().info("[UltraHardcore] UltraHardcore v" + getDescription().getVersion() + " is enabled!");
            Bukkit.getPluginCommand("ultrahardcore").setExecutor(new UltraHardcoreCommand(this));
            Bukkit.getPluginManager().registerEvents(new PlayerJoinListener(), this);
            saveDefaultConfig();
        }
     
        public void onDisable() {
            Bukkit.getLogger().info("[UltraHardcore] UltraHardcore v" + getDescription().getVersion() + " is disabled!");
        }
    }
    
    UltraHardcoreCommand:
    Code:
    package net.playfriik.ultrahardcore.commands;
    
    import net.playfriik.ultrahardcore.UltraHardcore;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    
    public class UltraHardcoreCommand implements CommandExecutor {
    
        public boolean isRunning = false;
        UltraHardcore plugin;
        int gameTime = plugin.getConfig().getInt("gameTime");
        int meetupTime = plugin.getConfig().getInt("meetupTime");
    
        public UltraHardcoreCommand(UltraHardcore plugin) {
            this.plugin = plugin;
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (args.length == 0) {
                sender.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "UltraHardcore");
                sender.sendMessage(ChatColor.GRAY + "/uhc start" + ChatColor.DARK_GRAY + " - " + ChatColor.AQUA + "Start the game.");
                sender.sendMessage(ChatColor.GRAY + "/uhc stop" + ChatColor.DARK_GRAY + " - " + ChatColor.AQUA + "Stop the game.");
                sender.sendMessage(ChatColor.GRAY + "/uhc join" + ChatColor.DARK_GRAY + " - " + ChatColor.AQUA + "Join the game.");
                sender.sendMessage(ChatColor.GRAY + "/uhc leave" + ChatColor.DARK_GRAY + " - " + ChatColor.AQUA + "Leave the game.");
            }
    
            if (args.length == 1) {
                if (isRunning == false) {
                    if (args[0].equalsIgnoreCase("start")) {
                        isRunning = true;
                        gameTime();
                        sender.sendMessage(ChatColor.GREEN + "The game has been started!");
                    }
                }
    
                if (isRunning == true) {
                    if (args[0].equalsIgnoreCase("stop")) {
                        isRunning = false;
                        Bukkit.getScheduler().cancelAllTasks();
                        sender.sendMessage(ChatColor.RED + "The game has been stopped!");
                    }
                }
            }
            return true;
        }
     
        public void gameTime() {
            Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                public void run() {
                    gameTime--;
                    if (gameTime == 0) {
                        Bukkit.getScheduler().cancelTask(gameTime);
                        Bukkit.broadcastMessage(ChatColor.RED + "Meetup started!");
                        meetupTime();
                    }
                    if (gameTime == 1) {
                        Bukkit.broadcastMessage(ChatColor.GREEN + "1 minute until meetup!");
                    }
                    if (gameTime == 5) {
                        Bukkit.broadcastMessage(ChatColor.GREEN + "5 minutes left until meetup!");
                    }
                    if (gameTime == 10) {
                        Bukkit.broadcastMessage(ChatColor.GREEN + "10 minutes left until meetup!");
                    }
                    if (gameTime == 20) {
                        Bukkit.broadcastMessage(ChatColor.GREEN + "20 minutes left until meetup!");
                    }
                    if (gameTime == 30) {
                        Bukkit.broadcastMessage(ChatColor.GREEN + "30 minutes left until meetup!");
                    }
                    if (gameTime == 60) {
                        Bukkit.broadcastMessage(ChatColor.GREEN + "60 minutes left until meetup!");
                    }
                    if (gameTime == 90) {
                        Bukkit.broadcastMessage(ChatColor.GREEN + "90 minutes left until meetup!");
                    }
                    if (gameTime == 120) {
                        Bukkit.broadcastMessage(ChatColor.GREEN + "120 minutes left until meetup!");
                    }
                    if (gameTime == 150) {
                        Bukkit.broadcastMessage(ChatColor.GREEN + "150 minutes left until meetup!");
                    }
                    if (gameTime == 180) {
                        Bukkit.broadcastMessage(ChatColor.GREEN + "180 minutes left until meetup!");
                    }
                }
            }, 60 * 20);
        }
    
        public void meetupTime() {
            Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                public void run() {
                    meetupTime--;
                    if (meetupTime == 0) {
                        Bukkit.getScheduler().cancelTask(meetupTime);
                        Bukkit.broadcastMessage(ChatColor.RED + "Meetup has been ended!");
                        meetupTime();
                    }
                    if (meetupTime == 1) {
                        Bukkit.broadcastMessage(ChatColor.GREEN + "1 minute until draw!");
                    }
                    if (meetupTime == 5) {
                        Bukkit.broadcastMessage(ChatColor.GREEN + "5 minutes left until draw!");
                    }
                    if (meetupTime == 10) {
                        Bukkit.broadcastMessage(ChatColor.GREEN + "10 minutes left until draw!");
                    }
                    if (meetupTime == 20) {
                        Bukkit.broadcastMessage(ChatColor.GREEN + "20 minutes left until draw!");
                    }
                    if (meetupTime == 30) {
                        Bukkit.broadcastMessage(ChatColor.GREEN + "30 minutes left until draw!");
                    }
                    if (meetupTime == 60) {
                        Bukkit.broadcastMessage(ChatColor.GREEN + "60 minutes left until draw!");
                    }
                    if (meetupTime == 90) {
                        Bukkit.broadcastMessage(ChatColor.GREEN + "90 minutes left until draw!");
                    }
                    if (meetupTime == 120) {
                        Bukkit.broadcastMessage(ChatColor.GREEN + "120 minutes left until draw!");
                    }
                    if (meetupTime == 150) {
                        Bukkit.broadcastMessage(ChatColor.GREEN + "150 minutes left until draw!");
                    }
                    if (meetupTime == 180) {
                        Bukkit.broadcastMessage(ChatColor.GREEN + "180 minutes left until draw!");
                    }
                }
            }, 60 * 20);
        }
    }
    
    PlayerJoinListener:
    Code:
    package net.playfriik.ultrahardcore.listeners;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Sound;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    
    public class PlayerJoinListener implements Listener {
     
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            Player player = e.getPlayer();
            player.playSound(player.getLocation(), Sound.NOTE_PLING, 1, 1);
            player.sendMessage(ChatColor.GREEN + "Welcome to UltraHardcore!");
        }
    }
    
    Error on console:
    Code:
    [19:03:02 ERROR]: Error occurred while enabling UltraHardcore v0.1.0 (Is it up t
    o date?)
    java.lang.NullPointerException
            at net.playfriik.ultrahardcore.commands.UltraHardcoreCommand.<init>(Ultr
    aHardcoreCommand.java:15) ~[?:?]
            at net.playfriik.ultrahardcore.UltraHardcore.onEnable(UltraHardcore.java
    :14) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[S
    pigot.jar:git-Spigot-"ec7c160"]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:335) [Spigot.jar:git-Spigot-"ec7c160"]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:405) [Spigot.jar:git-Spigot-"ec7c160"]
            at org.bukkit.craftbukkit.v1_8_R1.CraftServer.loadPlugin(CraftServer.jav
    a:352) [Spigot.jar:git-Spigot-"ec7c160"]
            at org.bukkit.craftbukkit.v1_8_R1.CraftServer.enablePlugins(CraftServer.
    java:313) [Spigot.jar:git-Spigot-"ec7c160"]
            at net.minecraft.server.v1_8_R1.MinecraftServer.q(MinecraftServer.java:4
    02) [Spigot.jar:git-Spigot-"ec7c160"]
            at net.minecraft.server.v1_8_R1.MinecraftServer.k(MinecraftServer.java:3
    70) [Spigot.jar:git-Spigot-"ec7c160"]
            at net.minecraft.server.v1_8_R1.MinecraftServer.a(MinecraftServer.java:3
    25) [Spigot.jar:git-Spigot-"ec7c160"]
            at net.minecraft.server.v1_8_R1.DedicatedServer.init(DedicatedServer.jav
    a:211) [Spigot.jar:git-Spigot-"ec7c160"]
            at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java
    :498) [Spigot.jar:git-Spigot-"ec7c160"]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_20]
     
  11. Offline

    Skionz

  12. Offline

    PlayFriik

    Do I have to initialize it on command class? and if so, what method?
     
  13. Offline

    Skionz

    @PlayFriik Initialize your main class in your main class and pass it to other classes through constructors.
     
    teej107 likes this.
Thread Status:
Not open for further replies.

Share This Page