1.14.4 Problem with my Timer

Discussion in 'Spigot Plugin Development' started by RealRivex, Apr 2, 2020.

  1. Hey, i am trying to make a Timer for my Challenges, but i am getting very much errors. whatever i am trying to do with it, it shows an error and is so buggy and idk why, as i am new to timers.
    maybe you can help me:

    my main class:
    Code (Java):

    package main;

    import java.util.ArrayList;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.EnderDragon;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;

    import challenges.noCrafting;
    import challenges.noPlace;
    import commands.CommandTimer;
    import settings.Challenges;
    import utils.Data;

    public class main extends JavaPlugin implements CommandExecutor, Listener {
        public static boolean timerRunning = false;

        private CommandTimer ct;

        public static Inventory inv = null;

        @Override
        public void onEnable() {

            System.out.println("§7Plugin [§5Challenges§7] wurde aktiviert!");
            this.ct = new CommandTimer(this);
            getCommand("timer").setExecutor((CommandExecutor) this.ct);
            Bukkit.getPluginManager().registerEvents(this, this);
            PluginManager pm = Bukkit.getPluginManager();
            pm.registerEvents(new Challenges(), this);
            pm.registerEvents(new noCrafting(), this);
            pm.registerEvents(new join(), this);
            pm.registerEvents(new noPlace(), this);

            getCommand("crafting").setExecutor(new noCrafting());
            getCommand("place").setExecutor(new noPlace());

        }

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

            Player p = (Player) sender;

            if (cmd.getName().equalsIgnoreCase("settings")) {
                if (p.hasPermission("challenges.settings")) {
                    inv = p.getPlayer().getServer().createInventory(null, 45, "Challenges");
                    // -----------------------------------------------------------------------------------------------------------
                    ItemStack stack = new ItemStack(Material.CRAFTING_TABLE);
                    ItemMeta meta = stack.getItemMeta();
                    meta.setDisplayName("§7[§6Crafting§7]");
                    ArrayList<String> crafting = new ArrayList<String>();
                    if (noCrafting.crafting) {
                        crafting.add(0, "§eWenn §aaktiviert, §estirbst du wenn jemand ein §7Item §ecraftet!");
                        crafting.add(1, ""); // placeholder
                        crafting.add(2, "     §a»");
                        meta.setLore(crafting);
                        stack.setItemMeta(meta);
                    } else {

                        crafting.add(0, "§eWenn §aaktiviert, §estirbst du wenn jemand ein §7Item §ecraftet!");
                        crafting.add(1, "");
                        crafting.add(2, "     §4»");
                        meta.setLore(crafting);
                        stack.setItemMeta(meta);
                    }

                    // -----------------------------------------------------------------------------------------------------------

                    ItemStack stack2 = new ItemStack(Material.STONE);
                    ItemMeta meta2 = stack2.getItemMeta();
                    meta.setDisplayName("§7[§6Place§7]");
                    ArrayList<String> place = new ArrayList<String>();
                    if (noPlace.place) {
                        place.add(0, "§eWenn §aaktiviert, §estirbst du wenn jemand einen §7Block §eplatziert!");
                        place.add(1, ""); // placeholder
                        place.add(2, "     §a»");
                        meta2.setLore(place);
                        stack2.setItemMeta(meta2);
                    } else {

                        place.add(0, "§eWenn §aaktiviert, §estirbst du wenn jemand einen §7Block §eplatziert!");
                        place.add(1, "");
                        place.add(2, "     §4»");
                        meta2.setLore(place);
                        stack2.setItemMeta(meta2);
                    }
                    // -----------------------------------------------------------------------------------------------------------
                    ItemStack stack3 = new ItemStack(Material.COOKED_BEEF);
                    ItemMeta meta3 = stack3.getItemMeta();
                    meta3.setDisplayName("§7[§4Test§7]");
                    stack3.setItemMeta(meta3);
                    // -----------------------------------------------------------------------------------------------------------
                    inv.setItem(1, stack);
                    inv.setItem(4, stack2);
                    inv.setItem(7, stack3);
                    p.getPlayer().openInventory(inv);

                } else
                    p.sendMessage(Data.noPermission);
            }

            return false;
        }

        @Override
        public void onDisable() {
            System.out.println("§7Plugin [§5Challenges§7] wurde deaktiviert!");
        }

        @EventHandler
        public void dragon(EntityDeathEvent event) {
            LivingEntity dragon = event.getEntity();
            Player p = dragon.getKiller();
            if (dragon instanceof EnderDragon) {
                this.ct.resetTimerArgument(p);
            }
        }
    }
     
    my commandTimer class:
    Code (Java):
    package commands;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    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.PlayerJoinEvent;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.scheduler.BukkitTask;

    import main.main;
    import net.md_5.bungee.api.ChatMessageType;
    import net.md_5.bungee.api.chat.BaseComponent;
    import net.md_5.bungee.api.chat.TextComponent;
    import utils.Data;

    public class CommandTimer implements CommandExecutor, Listener {
        private main plugin;

        private BukkitTask timer;

        private BukkitTask pauseTimer;

        private int time = 0;

        public CommandTimer(main plugin) {
            this.plugin = plugin;
        }

        @EventHandler
        public void timer(PlayerJoinEvent event) {
            for (Player p : Bukkit.getOnlinePlayers()) {
                if (this.time == 0) {
                    p.spigot().sendMessage(ChatMessageType.ACTION_BAR,
                            (BaseComponent) new TextComponent(ChatColor.GOLD + "Timer pausiert!."));
                }
            }
        }

        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (sender instanceof Player) {
                Player p = (Player) sender;
                if (args.length == 0) {

                    p.sendMessage(ChatColor.RED + "/timer pause" + ChatColor.GRAY + ": Pausiert dem timer");
                    p.sendMessage(
                            ChatColor.RED + "/timer resume" + ChatColor.GRAY + ": Startet den Timer oder setzt ihn fort");
                    p.sendMessage(ChatColor.RED + "/timer reset" + ChatColor.GRAY + ": Resettet den Timer ");
                } else if (p.hasPermission("challenge.timer")) {
                    if (args[0].equalsIgnoreCase("start")) {
                        startTimerArgument(p);
                    } else if (args[0].equalsIgnoreCase("resume")) {
                        resumeTimerArgument(p);
                    } else if (args[0].equalsIgnoreCase("pause")) {
                        pauseTimerArgument(p);
                    } else if (args[0].equalsIgnoreCase("reset")) {
                        resetTimerArgument(p);
                    } else {
                        p.sendMessage(Data.prefix + "§4Falsches Argument!");
                    }
                } else {
                    p.sendMessage(Data.prefix + Data.noPermission);
                }
            } else {
                sender.sendMessage(Data.prefix + Data.noPlayer);

            }
            return true;
        }

        private void startTimerArgument(Player p) {
            if (this.time == 0) {
                startTimer();
                Bukkit.broadcastMessage(ChatColor.GRAY + "Der Timer wurde " + ChatColor.GREEN + "gestartet");
            } else {
                p.sendMessage(ChatColor.GRAY + "Der Timer wurde bereits" + ChatColor.GREEN + "gestartet");
            }
        }

        private void resumeTimerArgument(Player p) {
            startPauseTimer();
            if (this.time == 0) {
                Bukkit.broadcastMessage(ChatColor.GRAY + "Der Timer wurde " + ChatColor.GREEN + "gestartet");
            } else {
                Bukkit.broadcastMessage(ChatColor.GRAY + "Der Timer wurde " + ChatColor.GREEN + "fortgesetzt");
            }
        }

        private void pauseTimerArgument(Player p) {
            if (this.time != 0) {
                stopPauseTimer();
                Bukkit.broadcastMessage(ChatColor.GRAY + "Der Timer wurde " + ChatColor.RED + "pausiert");
                startPauseTimer();
            } else {
                p.sendMessage(ChatColor.RED + "Der Timer wurde nicht " + ChatColor.GREEN + "gestartet");
            }
        }

        public void resetTimerArgument(Player p) {
            if (this.time != 0) {
                stopTimer();
                Bukkit.broadcastMessage(ChatColor.GRAY + "Timer gestoppt bei: " + ChatColor.GREEN + getTimeString(this.time)
                        + ChatColor.GRAY + ".");
                this.time = 0;
                stopPauseTimer();
                Bukkit.broadcastMessage(ChatColor.GRAY + "Der Timer wurde " + ChatColor.GOLD + "zurückgesetzt");
            } else {
                p.sendMessage(ChatColor.RED + "Der Timer ist bereits auf 0");
            }
        }

        private void startTimer() {
            this.timer = Bukkit.getScheduler().runTaskTimer((Plugin) this.plugin, () -> {
                this.time++;
                for (Player p : Bukkit.getOnlinePlayers())
                    p.spigot().sendMessage(ChatMessageType.ACTION_BAR,
                            (BaseComponent) new TextComponent("Challenge: " + "§a" + getTimeString(this.time)));
            }, 0L, 20L);
            main.timerRunning = true;
        }

        private void stopTimer() {
            main.timerRunning = false;
            this.timer.cancel();
        }

        private void startPauseTimer() {
            this.pauseTimer = Bukkit.getScheduler().runTaskTimer((Plugin) this.plugin, () -> {
                for (Player p : Bukkit.getOnlinePlayers())
                    p.spigot().sendMessage(ChatMessageType.ACTION_BAR,
                            (BaseComponent) new TextComponent("§6Timer pausiert"));
            }, 0L, 20L);
        }

        private void stopPauseTimer() {
            this.pauseTimer.cancel();

        }

        private String getTimeString(int time) {
            int hours = time / 3600;
            int minutes = time % 3600 / 60;
            int seconds = time % 60;
            return String.format("%02d:%02d:%02d",
                    new Object[] { Integer.valueOf(hours), Integer.valueOf(minutes), Integer.valueOf(seconds) });
        }
    }
     
    and first, on every command used, i get an Null pointer exception in the console, and 2nd, if if i do /timer pause, it permanently shows that the timer is paused, on /timer start it starts normally, on /timer pause it shows me that the timer isnt started, and on /timer reset it resets, but still showing an null pointer exception.

    why?
     
  2. Please could you send the console errorss aswell as therse help debugging.
     
  3. Please could you send the console errorss aswell as therse help debugging.
     
  4. @OnlyTheBest only on timer pause i get this error now:


    Code (Text):
    [20:51:15] [Server thread/INFO]: VNGC issued server command: /timer pause
    [20:51:15] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'timer' in plugin Challenges v1.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[minecraft_server.jar:git-Spigot-56f8471-3e3818b]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) ~[minecraft_server.jar:git-Spigot-56f8471-3e3818b]
    at org.bukkit.craftbukkit.v1_14_R1.CraftServer.dispatchCommand(CraftServer.java:710) ~[minecraft_server.jar:git-Spigot-56f8471-3e3818b]
    at net.minecraft.server.v1_14_R1.PlayerConnection.handleCommand(PlayerConnection.java:1641) ~[minecraft_server.jar:git-Spigot-56f8471-3e3818b]
    at net.minecraft.server.v1_14_R1.PlayerConnection.a(PlayerConnection.java:1481) ~[minecraft_server.jar:git-Spigot-56f8471-3e3818b]
    at net.minecraft.server.v1_14_R1.PacketPlayInChat.a(PacketPlayInChat.java:47) ~[minecraft_server.jar:git-Spigot-56f8471-3e3818b]
    at net.minecraft.server.v1_14_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) ~[minecraft_server.jar:git-Spigot-56f8471-3e3818b]
    at net.minecraft.server.v1_14_R1.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[minecraft_server.jar:git-Spigot-56f8471-3e3818b]
    at net.minecraft.server.v1_14_R1.TickTask.run(SourceFile:18) [minecraft_server.jar:git-Spigot-56f8471-3e3818b]
    at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeTask(SourceFile:144) [minecraft_server.jar:git-Spigot-56f8471-3e3818b]
    at net.minecraft.server.v1_14_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) [minecraft_server.jar:git-Spigot-56f8471-3e3818b]
    at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeNext(SourceFile:118) [minecraft_server.jar:git-Spigot-56f8471-3e3818b]
    at net.minecraft.server.v1_14_R1.MinecraftServer.aX(MinecraftServer.java:909) [minecraft_server.jar:git-Spigot-56f8471-3e3818b]
    at net.minecraft.server.v1_14_R1.MinecraftServer.executeNext(MinecraftServer.java:902) [minecraft_server.jar:git-Spigot-56f8471-3e3818b]
    at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.awaitTasks(SourceFile:127) [minecraft_server.jar:git-Spigot-56f8471-3e3818b]
    at net.minecraft.server.v1_14_R1.MinecraftServer.sleepForTick(MinecraftServer.java:886) [minecraft_server.jar:git-Spigot-56f8471-3e3818b]
    at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:819) [minecraft_server.jar:git-Spigot-56f8471-3e3818b]
    at java.lang.Thread.run(Thread.java:834) [?:?]
    Caused by: java.lang.NullPointerException
    at commands.CommandTimer.stopPauseTimer(CommandTimer.java:140) ~[?:?]
    at commands.CommandTimer.pauseTimerArgument(CommandTimer.java:95) ~[?:?]
    at commands.CommandTimer.onCommand(CommandTimer.java:59) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[minecraft_server.jar:git-Spigot-56f8471-3e3818b]
    ... 17 more
    and if i do /timer resume, it should start it, or resume it paused, but its only showing "timer paused"
     
  5. Means that one of your variables is null.
    Error is on line 140 in method stopPauseTimer from class CommandTimer.
    So, it means that your pauseTimer is null at this point. Do some null check or initialize it in constructor.
     
  6. I reccomend you make it so only one instance of the plugin exists. This is shown in the 'coding mistakes' thread which is highlighted in this forum. Using get Instance etc would ensure that it is not null!
     
  7. Plugin instance is transfered through construtor here, you know.
     
  8. I meant one instance of the pauseTimer, sorry haha.
     

  9. how would i do this? never did that i think
     
  10. For the null check, you must do an if(this.pauseTimer == null) return; before you cancel it.
     
  11. so like this?
    Code (Java):
        private void pauseTimerArgument(Player p) {
            if (this.time != 0) {
                if (this.pauseTimer == null) {
                    return;
                }
                stopPauseTimer();
                Bukkit.broadcastMessage(ChatColor.GRAY + "Der Timer wurde " + ChatColor.RED + "pausiert");
                startPauseTimer();
            } else {
                p.sendMessage(ChatColor.RED + "Der Timer wurde nicht " + ChatColor.GREEN + "gestartet");
            }
        }
     
  12. yes