Solved Error with Timer Plugin

Discussion in 'Plugin Development' started by QxDev, Jun 13, 2019.

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

    QxDev

    Hey Guys,

    I tried to make a plugin that does like a timer... but the timer isn't working. If you type /timer it only says "Der Server ist nun geöffnet!" and kicks you from the Server with the message "Der Server wurde geschlossen! Bis morgen!"

    I don't know what the error is... so can anyone of you find it? I would be really thankful.

    I'm using Spigot 1.8.8 for my Server right now

    The Code:

    Code:
    package de.qxdev.timer;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerLoginEvent;
    import org.bukkit.event.server.ServerListPingEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
    
    
            boolean timerS = true;
            public int left = 15;
           
            @Override
            public void onEnable() {
               
                System.out.println("[Timer] Plugin loaded 25%");
                System.out.println("[Timer] Plugin loaded 50%");
                System.out.println("[Timer] Plugin loaded 75%");
                System.out.println("[Timer] Plugin loaded 100%");
                System.out.println("[Timer] Plugin successfully loaded!");
               
                Bukkit.getServer().getPluginManager().registerEvents(this, this);
               
                saveConfig();
               
                timerS = getConfig().get("timer") != null ? getConfig().getBoolean("timer") : false;
            }
           
            @EventHandler
            public void logIn(PlayerLoginEvent e){
             if(timerS){
                 if(e.getPlayer().isOp() == false && e.getPlayer().hasPermission("timer.join") == false) {
                     e.disallow(null, "§cDer Server ist momentan noch geschlossen!");
                 }
             }
            }
           
            @EventHandler
            public void onPing(ServerListPingEvent event){
             if(timerS){
                 event.setMaxPlayers(0);
             }
            }
           
           
            @Override
            public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
               
                if (command.getName().equalsIgnoreCase("timer")) {
                    if (sender.hasPermission("timer.start") || sender.isOp()) {
    
                        if (args.length == 0) {
                           
                                int timer = 15;
                               
                                while (timer <= 0) {
                                   
                                    this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                                        
                                        public void run() {
                                           
                                            for(Player pp : Bukkit.getOnlinePlayers()) {
                                                pp.sendMessage("In " + left + " Sekunden geht es los!");
                                            }
                                           
                                            left--;
                                           
                                        }
                                       
                                    }, 20L);
                                   
                                    timer--;
                                   
                                }
                               
                                timerS = false;
                               
                                for(Player pp : Bukkit.getOnlinePlayers()) {
                                    pp.sendMessage("Der Server ist nun geöffnet!");
                                }
                               
                                this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                                    
                                    public void run() {
                                       
                                        for(Player pp : Bukkit.getOnlinePlayers()) {
                                            pp.sendMessage("Noch " + left + " Minuten übrig!");
                                        }
                                       
                                    }
                                   
                                }, 20L);
                               
                                this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                                    
                                    public void run() {
                                       
                                        for(Player pp : Bukkit.getOnlinePlayers()) {
                                            pp.sendMessage("Noch 15 Minuten übrig!");
                                        }
                                       
                                    }
                                   
                                }, 20L);
                               
                                timer = 2;
                                left = 10;
                               
                                while (timer <= 0) {
                                   
                                    this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                                        
                                        public void run() {
                                           
                                            for(Player pp : Bukkit.getOnlinePlayers()) {
                                                pp.sendMessage("Noch " + left + " Minuten übrig!");
                                            }
                                           
                                            left = left - 5;
                                           
                                        }
                                       
                                    }, 20L);
                                   
                                }
                               
                                timer = 4;
                                left = 4;
                               
                                while (timer <= 0) {
                                   
                                    this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                                        
                                        public void run() {
                                           
                                            for(Player pp : Bukkit.getOnlinePlayers()) {
                                                pp.sendMessage("Noch " + left + " Minuten übrig!");
                                            }
                                           
                                            left = left - 5;
                                           
                                        }
                                       
                                    }, 20L);
                               
                                }
                               
                                this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                                    
                                    public void run() {
                                       
                                        for(Player pp : Bukkit.getOnlinePlayers()) {
                                            pp.sendMessage("Noch 30 Sekunden übrig!");
                                        }
                                       
                                        left--;
                                       
                                    }
                                   
                                }, 20L);
                               
                                this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                                    
                                    public void run() {
                                       
                                        for(Player pp : Bukkit.getOnlinePlayers()) {
                                            pp.sendMessage("Noch 15 Sekunden übrig!");
                                        }
                                       
                                    }
                                   
                                }, 20L);
                               
                                this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                                    
                                    public void run() {
                                       
                                        for(Player pp : Bukkit.getOnlinePlayers()) {
                                            pp.sendMessage("Noch 10 Sekunden übrig!");
                                        }
                                       
                                    }
                                   
                                }, 20L);
                               
                                this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                                    
                                    public void run() {
                                       
                                        for(Player pp : Bukkit.getOnlinePlayers()) {
                                            pp.sendMessage("Noch 5 Sekunden übrig!");
                                        }
                                       
                                    }
                                   
                                }, 20L);
                               
                                timer = 4;
                                left = 4;
                               
                                while (timer <= 0) {
                               
                                    this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                                        
                                        public void run() {
                                           
                                            for(Player pp : Bukkit.getOnlinePlayers()) {
                                                pp.sendMessage("Noch " + left + " Sekunden übrig!");
                                            }
                                           
                                            left--;
                                           
                                        }
                                       
                                    }, 20L);
                               
                                }
                               
                                for(Player pp : Bukkit.getOnlinePlayers()) {
                                     pp.kickPlayer("Der Server wurde geschlossen! Bis morgen!");
                                     timerS = true;
                                }
                               
                           
                            getConfig().set("timer",timerS);
                            saveConfig();
                           
                            return true;
                        }else{
                            sender.sendMessage("§8[§6Timer§8] §cBitte benutze §6/timer start");
                        }
    
                        return false;
                    } else {
                        sender.sendMessage("§8[§6Timer§8] §cDazu hast du keine Rechte!");
                        sender.sendMessage("§8[§6Timer§8] §aWenn du meinst, dass das ein Fehler ist, wende dich bitte an unser Team!");
                    }
                    return false;
                }else{
                    sender.sendMessage("§8[§6Timer§8] §cBitte benutze §6/timer start");
                }
               
               
                return false;
            }
           
    
        }
    
    Thanks for all replies

    QxDev
     
  2. Offline

    Kars

    @QxDev Sounds about right. That is what the code does.

    It schedules a bunch of tasks to send all online players a message with some delay, then immediately kicks all online players.

    I'm guessing you want to kick everyone after the timer ends? Schedule the kicking part in a task with delay. Just because the code is located under the scheduling of the "countdown" tasks does not mean it will wait for them to execute.
     
  3. Offline

    bwfcwalshy Retired Staff

    You can just do `timerS = this.getConfig().getBoolean("timer", false);`

    Also don't compare a boolean with a boolean, it just results in a boolean :p you can just use the statement and if true it will fire or negative it. `if (variable)` or `if (!variable)`

    You make a new delayed task every time your while fires. Just use scheduleRepeatingTask
    You can have 1 timer only and clean this code up a lot.
     
  4. Offline

    QxDev

    Okay thank you... I now how to do it now
     

    Attached Files:

Thread Status:
Not open for further replies.

Share This Page