Solved system millies

Discussion in 'Spigot Plugin Development' started by Kompye, Dec 3, 2019.

  1. "the local variable parkourTime may not have been initialized" when trying to do
    Code (Java):
    parkourTime = (parkourTime - System.currentTimeMillis()) / 1000;
    but if I try for example
    Code (Java):
    parkourTime = parkourTime - 1;
    it has no problem
     
  2. Above I have
    Code (Java):
    parkourTime = System.currentTimeMillis();
     
  3. paste the whole class. These snippets don't tell me much
     
  4. Code (Java):
    package net.etheral.listeners;

    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Sound;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.scheduler.BukkitRunnable;

    import net.etheral.EtheralParkour;
    import net.etheral.utils.Utils;
    import net.md_5.bungee.api.ChatMessageType;
    import net.md_5.bungee.api.chat.TextComponent;

    public class PressurePlateListener implements Listener{

        EtheralParkour plugin;
         
        public PressurePlateListener(EtheralParkour instanceOfMain) {
            this.plugin = instanceOfMain;
        }
     
        @EventHandler
        public void pressurePlatePress(PlayerInteractEvent e) {
            if(e.getAction().equals(Action.PHYSICAL)) {
                Player player = (Player) e.getPlayer();
             
                Location startLocation = (Location) plugin.getConfig().get("start");
                Location finishLocation = (Location) plugin.getConfig().get("finish");
                double parkourTime;
             
                if(e.getClickedBlock().getLocation().equals(startLocation) && !plugin.parkourersList.contains(player.getUniqueId())) {
                        parkourTime = System.currentTimeMillis();
                        plugin.parkourersList.add(player.getUniqueId());
                        player.sendMessage(Utils.Chat("&b&lParkour&e > &aAi inceput parkourul! Foloseste comanda /parkour leave pentru a iesi."));
                        player.playSound(player.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1.0F, 1.0F);
                        new BukkitRunnable() {
                            int elapsedSeconds = 0;
                            String showTime = ChatColor.GOLD + "00:00";
                            String minutes = "0";
                            String seconds = "0";
                            @Override
                            public void run() {
                                if(!plugin.parkourersList.contains(player.getUniqueId())) {
                                    showTime = ChatColor.AQUA + minutes + ":" + seconds;
                                    player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(showTime));
                                    cancel();
                                }
                                player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(showTime));
                                elapsedSeconds++;
                                if(Integer.valueOf(minutes) < 9) {
                                    minutes = "0" + String.valueOf(elapsedSeconds / 60);
                                } else {
                                    minutes = String.valueOf(elapsedSeconds / 60);
                                }
                                if(Integer.valueOf(seconds) < 9) {
                                    seconds = "0" + String.valueOf(elapsedSeconds % 60);
                                } else {
                                    seconds = String.valueOf(elapsedSeconds % 60);
                                }
                                showTime = ChatColor.GOLD + minutes + ":" + seconds;
                            }
                        }.runTaskTimer(plugin, 0, 20);
                        return;
                }
                else if(e.getClickedBlock().getLocation().equals(finishLocation) && plugin.parkourersList.contains(player.getUniqueId())) {
                        parkourTime = (parkourTime - System.currentTimeMillis()) / 1000;
                        plugin.parkourersList.remove(player.getUniqueId());
                        player.sendMessage(Utils.Chat("&b&lParkour&e > &bAi ajuns la finalul parkourului!"));
                        player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1.0F, 1.0F);
                }
            }
        }

    }
     
    it's an error in eclipse not in the console
     
  5. Ok, you're getting the error because when
    Code (Java):
    parkourTime = (parkourTime - System.currentTimeMillis()) / 1000;
    runs,
    Code (Java):
    parkourTime = System.currentTimeMillis();
    had never run before since they are in different if-statements.
     
    • Useful Useful x 1
  6. Please do not use double for times, use long instead.
     
    • Like Like x 2
    • Agree Agree x 1
  7. And what should I do to fix it? I have to calculate the time difference between the starting point and the finish.
     
  8. double parkourTime; does not have a value (not initialized).
    You could just set parkourTime to 0 before calling, and then check if it's 0 before calling;
    Code (Text):
    parkourTime = (parkourTime - System.currentTimeMillis()) / 1000;
     
    • Informative Informative x 1
  9. wtf now my plugin disables on startup
    Code (Text):
    [18:50:56] [Server thread/INFO]: [EtheralParkour] Enabling EtheralParkour v1.0
    [18:50:56] [Server thread/ERROR]: Error occurred while enabling EtheralParkour v1.0 (Is it up to date?)
    java.lang.NullPointerException: source cannot be null
        at com.gmail.filoghost.holographicdisplays.util.Validator.notNull(Validator.java:7) ~[?:?]
        at com.gmail.filoghost.holographicdisplays.object.BackendAPI.createHologram(BackendAPI.java:23) ~[?:?]
        at com.gmail.filoghost.holographicdisplays.api.HologramsAPI.createHologram(HologramsAPI.java:27) ~[?:?]
        at net.etheral.EtheralParkour.generateHologram(EtheralParkour.java:42) ~[?:?]
        at net.etheral.EtheralParkour.onEnable(EtheralParkour.java:38) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[paper-1.12.2.jar:git-Paper-1574]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:316) ~[paper-1.12.2.jar:git-Paper-1574]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) ~[paper-1.12.2.jar:git-Paper-1574]
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin(CraftServer.java:395) ~[paper-1.12.2.jar:git-Paper-1574]
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins(CraftServer.java:344) ~[paper-1.12.2.jar:git-Paper-1574]
        at net.minecraft.server.v1_12_R1.MinecraftServer.t(MinecraftServer.java:442) ~[paper-1.12.2.jar:git-Paper-1574]
        at net.minecraft.server.v1_12_R1.MinecraftServer.l(MinecraftServer.java:403) ~[paper-1.12.2.jar:git-Paper-1574]
        at net.minecraft.server.v1_12_R1.MinecraftServer.a(MinecraftServer.java:341) ~[paper-1.12.2.jar:git-Paper-1574]
        at net.minecraft.server.v1_12_R1.DedicatedServer.init(DedicatedServer.java:289) ~[paper-1.12.2.jar:git-Paper-1574]
        at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:616) ~[paper-1.12.2.jar:git-Paper-1574]
        at java.lang.Thread.run(Thread.java:748) [?:1.8.0_222]
    [18:50:56] [Server thread/INFO]: [EtheralParkour] Disabling EtheralParkour v1.0
     
    it worked until now
     
  10. we literally don't know without seeing code. Also indicate which line is 38
     
  11. This is the main class
    Code (Java):
    package net.etheral;

    import java.util.HashSet;
    import java.util.UUID;

    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitRunnable;

    import com.gmail.filoghost.holographicdisplays.api.Hologram;
    import com.gmail.filoghost.holographicdisplays.api.HologramsAPI;

    import net.etheral.commands.EpsetCommand;
    import net.etheral.commands.ParkourCommand;
    import net.etheral.listeners.ClickHotbarItemListener;
    import net.etheral.listeners.PlayerLeaveListener;
    import net.etheral.listeners.PressurePlateListener;
    import net.etheral.utils.Utils;

    public class EtheralParkour extends JavaPlugin{
       
        public HashSet<UUID> parkourersList = new HashSet<UUID>();
        public Hologram hologram = null;

        @Override
        public void onEnable() {
            if(!Bukkit.getPluginManager().isPluginEnabled("HolographicDisplays")) {
                getLogger().warning("HolographicDisplays nu e activat, hologramele nu o sa functioneze.");
            }
            saveDefaultConfig();
            getServer().getPluginManager().registerEvents(new PressurePlateListener(this), this);
            getServer().getPluginManager().registerEvents(new ClickHotbarItemListener(this), this);
            getServer().getPluginManager().registerEvents(new PlayerLeaveListener(this), this);
            getCommand("epset").setExecutor(new EpsetCommand(this));
            getCommand("parkour").setExecutor(new ParkourCommand(this));
            generateHologram();
        }
       
        public void generateHologram() {
            hologram = HologramsAPI.createHologram(this, (Location) getConfig().get("Hologram.location"));
            hologram.appendTextLine(Utils.Chat("&b&lPARKOUR"));
            hologram.appendTextLine(Utils.Chat("&6Top Times"));
            new BukkitRunnable() {
                @Override
                public void run() {
                    //TextLine top1 = hologram.appendTextLine(Utils.Chat("&f&l1: " + getConfig().getString("Hologram.topTimes.1.time")));
                }
            }.runTaskTimer(this, 0, 20);
        }

    }
     
    line 38 is
    Code (Java):
    }
     
  12. It's not line 38, but 42;
    Code (Text):
    at net.etheral.EtheralParkour.generateHologram(EtheralParkour.java:42) ~[?:?]
    Make sure the location you're trying to get from your config actually exists.
    Code (Text):
    hologram = HologramsAPI.createHologram(this, (Location) getConfig().get("Hologram.location"));
     
    • Useful Useful x 1
  13. Ok I made a check and it fixed, but when I try to make the hologram via command I still get an error
    Code (Text):
    [19:37:10 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'epset' in plugin EtheralParkour v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[paper-1.12.2.jar:git-Paper-1574]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:151) ~[paper-1.12.2.jar:git-Paper-1574]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:685) ~[paper-1.12.2.jar:git-Paper-1574]
            at net.minecraft.server.v1_12_R1.PlayerConnection.handleCommand(PlayerConnection.java:1479) ~[paper-1.12.2.jar:git-Paper-1574]
            at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1284) ~[paper-1.12.2.jar:git-Paper-1574]
            at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) ~[paper-1.12.2.jar:git-Paper-1574]
            at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:5) ~[paper-1.12.2.jar:git-Paper-1574]
            at net.minecraft.server.v1_12_R1.PlayerConnectionUtils.lambda$ensureMainThread$0(PlayerConnectionUtils.java:14) ~[paper-1.12.2.jar:git-Paper-1574]
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) ~[?:1.8.0_222]
            at java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[?:1.8.0_222]
            at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) ~[paper-1.12.2.jar:git-Paper-1574]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:850) ~[paper-1.12.2.jar:git-Paper-1574]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:423) ~[paper-1.12.2.jar:git-Paper-1574]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:774) ~[paper-1.12.2.jar:git-Paper-1574]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:666) ~[paper-1.12.2.jar:git-Paper-1574]
            at java.lang.Thread.run(Thread.java:748) [?:1.8.0_222]
    Caused by: java.lang.NullPointerException: source cannot be null
            at com.gmail.filoghost.holographicdisplays.util.Validator.notNull(Validator.java:7) ~[?:?]
            at com.gmail.filoghost.holographicdisplays.object.BackendAPI.createHologram(BackendAPI.java:23) ~[?:?]
            at com.gmail.filoghost.holographicdisplays.api.HologramsAPI.createHologram(HologramsAPI.java:27) ~[?:?]
            at net.etheral.EtheralParkour.generateHologram(EtheralParkour.java:44) ~[?:?]
            at net.etheral.commands.EpsetCommand.onCommand(EpsetCommand.java:35) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[paper-1.12.2.jar:git-Paper-1574]
            ... 15 more
     
    Config:
    Code (YAML):
    Hologram:
    topTimes
    :
      1
    :
       time
    : 0
       author
    : null
      1
    :
       time
    : 0
       author
    : null
      2
    :
       time
    : 0
       author
    : null
      3
    :
       time
    : 0
       author
    : null
      4
    :
       time
    : 0
       author
    : null
      5
    :
       time
    : 0
       author
    : null
    location:
    Main:
    Code (Java):
    package net.etheral;

    import java.util.HashSet;
    import java.util.UUID;

    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitRunnable;

    import com.gmail.filoghost.holographicdisplays.api.Hologram;
    import com.gmail.filoghost.holographicdisplays.api.HologramsAPI;
    import com.gmail.filoghost.holographicdisplays.api.line.TextLine;

    import net.etheral.commands.EpsetCommand;
    import net.etheral.commands.ParkourCommand;
    import net.etheral.listeners.ClickHotbarItemListener;
    import net.etheral.listeners.PlayerLeaveListener;
    import net.etheral.listeners.PressurePlateListener;
    import net.etheral.utils.Utils;

    public class EtheralParkour extends JavaPlugin{
       
        public HashSet<UUID> parkourersList = new HashSet<UUID>();
        public Hologram hologram = null;

        @Override
        public void onEnable() {
            if(!Bukkit.getPluginManager().isPluginEnabled("HolographicDisplays")) {
                getLogger().warning("HolographicDisplays nu e activat, hologramele nu o sa functioneze.");
            }
            saveDefaultConfig();
            getServer().getPluginManager().registerEvents(new PressurePlateListener(this), this);
            getServer().getPluginManager().registerEvents(new ClickHotbarItemListener(this), this);
            getServer().getPluginManager().registerEvents(new PlayerLeaveListener(this), this);
            getCommand("epset").setExecutor(new EpsetCommand(this));
            getCommand("parkour").setExecutor(new ParkourCommand(this));
            if((Location) getConfig().get("Hologram.location") != null) {
            generateHologram();
            }
        }
       
        public void generateHologram() {
            hologram = HologramsAPI.createHologram(this, (Location) getConfig().get("Hologram.location"));
            hologram.appendTextLine(Utils.Chat("&b&lPARKOUR"));
            hologram.appendTextLine(Utils.Chat("&6Top Times"));
            new BukkitRunnable() {
                @Override
                public void run() {
                    TextLine top1 = hologram.appendTextLine(Utils.Chat("&f&l1: " + getConfig().getString("Hologram.topTimes.1.time")));
                }
            }.runTaskTimer(this, 0, 20);
        }

    }
     
    Command:
    Code (Java):
    package net.etheral.commands;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    import net.etheral.EtheralParkour;
    import net.md_5.bungee.api.ChatColor;

    public class EpsetCommand implements CommandExecutor{

        EtheralParkour plugin;
       
        public EpsetCommand(EtheralParkour instanceOfMain) {
            this.plugin = instanceOfMain;
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
            if(player.hasPermission("EtheralParkour.admin")) {
            if(args[0].equalsIgnoreCase("start")) {
                plugin.getConfig().set("start", player.getLocation().getBlock().getLocation());
                plugin.saveConfig();
                player.sendMessage("Start set!");
            }
            else if(args[0].equalsIgnoreCase("finish")) {
                plugin.getConfig().set("finish", player.getLocation().getBlock().getLocation());
                plugin.saveConfig();
                player.sendMessage("Finish set!");
            }
            else if(args[0].equalsIgnoreCase("stats")) {
                if(plugin.hologram == null) {
                    plugin.generateHologram();
                } else {
                    plugin.hologram.teleport(player.getLocation());
                }
            }
            }
            else {
                player.sendMessage(ChatColor.RED + "Nu ai permisiunea sa folosesti aceasta comanda!");
            }
            return true;
        }
       
    }

     
     
  14. I don't ever see you set the value of location in your config though;
    Code (Text):
    if((Location) getConfig().get("Hologram.location") != null) {
            generateHologram();
            }
    Code (Text):
    location:
     
  15. It's in the command class
    Code (Java):
            else if(args[0].equalsIgnoreCase("stats")) {
                if(plugin.hologram == null) {
                    plugin.generateHologram();
                } else {
                    plugin.hologram.teleport(player.getLocation());
                }
            }
     
  16. No it's not. I don't see anything similar to getConfig.set("location", location);
     
    • Useful Useful x 1
  17. ohhh you were right I deleted it after for some reason. I added it to this method but I still get the error
    Code (Java):
        public void generateHologram() {
            hologram = HologramsAPI.createHologram(this, (Location) getConfig().get("Hologram.location"));
            getConfig().set("Hologram.location", hologram.getLocation());
            saveConfig();
            hologram.appendTextLine(Utils.Chat("&b&lPARKOUR"));
            hologram.appendTextLine(Utils.Chat("&6Top Times"));
            new BukkitRunnable() {
                @Override
                public void run() {
                    TextLine top1 = hologram.appendTextLine(Utils.Chat("&f&l1: " + getConfig().getString("Hologram.topTimes.1.time")));
                }
            }.runTaskTimer(this, 0, 20);
        }
     
  18. Fixed it! But now another question, how can I modify a hologram line?
     
  19. You'd have to play around with the functions available to you from the api. I'm not familiar with it, so I can't help you with that unfortunately.
     
    • Friendly Friendly x 1
  20. I can't get it working. I will make a new thread.