Enderpearl Cooldown

Discussion in 'Spigot Plugin Development' started by Roimeiqui7, Aug 9, 2018.

  1. Hello guys, Im coding a FullPvP core and I'm trying to put a enderpearl cooldown using this tutorial: https://bukkit.org/threads/tutorial-better-cooldowns.165811/.

    Error: (spamming)
    Code (Text):
    [23:23:09 WARN]: [zFullPvP] Task #71 for zFullPvP v0.5.0 generated an exception
    java.lang.NumberFormatException: For input string: "8,2"
            at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source) ~[?:1.8.0_171]
            at sun.misc.FloatingDecimal.parseDouble(Unknown Source) ~[?:1.8.0_171]
            at java.lang.Double.parseDouble(Unknown Source) ~[?:1.8.0_171]
            at java.lang.Double.valueOf(Unknown Source) ~[?:1.8.0_171]
            at tasks.cooldownmanager.utilMath.trim(utilMath.java:14) ~[?:?]
            at tasks.cooldownmanager.utilMath.convert(utilMath.java:32) ~[?:?]
            at tasks.cooldownmanager.Cooldown.getRemaining(Cooldown.java:39) ~[?:?]
            at tasks.cooldownmanager.Cooldown.handleCooldowns(Cooldown.java:78) ~[?:?]
            at me.roimeiqui.zfullpvp.FullPvP$1.run(FullPvP.java:115) ~[?:?]
            at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftTask.run(CraftTask.java:71) ~[customspigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350) [customspigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:723) [customspigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [customspigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [customspigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [customspigot.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_171]
    Code:
    utilMath class:
    Code (Text):

    package me.roimeiqui.zfullpvp.tasks;
    import java.text.DecimalFormat;
    public class utilMath {
        public static double trim(double untrimmeded, int decimal) {
            String format = "#.#";
            for(int i = 1; i < decimal; i++) {
                format = format + "#";
            }
            DecimalFormat twoDec = new DecimalFormat(format);
            return Double.valueOf(twoDec.format(untrimmeded)).doubleValue();
        }
    }

     
    utilTime class:
    Code (Text):
    package me.roimeiqui.zfullpvp.tasks;

    public class utilTime {
        public static enum TimeUnit {
            BEST,
            DAYS,
            HOURS,
            MINUTES,
            SECONDS,
        }
       
        public static double convert(long time, TimeUnit unit, int decPoint) {
            if(unit == TimeUnit.BEST) {
                if(time < 60000L) unit = TimeUnit.SECONDS;
                else if(time < 3600000L) unit = TimeUnit.MINUTES;
                else if(time < 86400000L) unit = TimeUnit.HOURS;
                else unit = TimeUnit.DAYS;
            }
            if(unit == TimeUnit.SECONDS) return utilMath.trim(time / 1000.0D, decPoint);
            if(unit == TimeUnit.MINUTES) return utilMath.trim(time / 60000.0D, decPoint);
            if(unit == TimeUnit.HOURS) return utilMath.trim(time / 3600000.0D, decPoint);
            if(unit == TimeUnit.DAYS) return utilMath.trim(time / 86400000.0D, decPoint);
            return utilMath.trim(time, decPoint);
        }
    }
     
    AbilityCooldown class:
    Code (Text):
    package tasks.cooldownmanager;

    import java.util.HashMap;

    public class AbilityCooldown {

        public String ability = "";
        public String player = "";
        public long seconds;
        public long systime;
        public HashMap<String, AbilityCooldown> cooldownMap = new HashMap<String, AbilityCooldown>();

        public AbilityCooldown(String player, long seconds, long systime) {
            this.player = player;
            this.seconds = seconds;
            this.systime = systime;
        }

        public AbilityCooldown(String player) {
            this.player = player;
        }

     

    }
     
    Cooldown class:
    Code (Text):

    package me.roimeiqui.zfullpvp.tasks;
    import java.util.HashMap;
    import java.util.Iterator;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import me.roimeiqui.zfullpvp.utils.ChatUtil;
    public class Cooldown extends ChatUtil {
        public static HashMap<String, AbilityCooldown> cooldownPlayers = new HashMap<String, AbilityCooldown>();
        public static void add(String player, String ability, long seconds, long systime) {
            if (!cooldownPlayers.containsKey(player))
                cooldownPlayers.put(player, new AbilityCooldown(player));
            if (isCooling(player, ability))
                return;
            cooldownPlayers.get(player).cooldownMap.put(ability,
                    new AbilityCooldown(player, seconds * 1000, System.currentTimeMillis()));
        }
        public static boolean isCooling(String player, String ability) {
            if (!cooldownPlayers.containsKey(player))
                return false;
            if (!cooldownPlayers.get(player).cooldownMap.containsKey(ability))
                return false;
            return true;
        }
        public static double getRemaining(String player, String ability) {
            if (!cooldownPlayers.containsKey(player))
                return 0.0;
            if (!cooldownPlayers.get(player).cooldownMap.containsKey(ability))
                return 0.0;
            return utilTime.convert(
                    (cooldownPlayers.get(player).cooldownMap.get(ability).seconds
                            + cooldownPlayers.get(player).cooldownMap.get(ability).systime) - System.currentTimeMillis(),
                    utilTime.TimeUnit.SECONDS, 1);
        }
        public static void coolDurMessage(Player player, String ability) {
            if (player == null) {
                return;
            }
            if (!isCooling(player.getName(), ability)) {
                return;
            }
            player.sendMessage(ChatColor.translateAlternateColorCodes('&', ("&8[&3" + ability + "&8] &cYou cannot use this enderpearl for "
                    + getRemaining(player.getName(), ability))));
        }
        public static void removeCooldown(String player, String ability) {
            if (!cooldownPlayers.containsKey(player)) {
                return;
            }
            if (!cooldownPlayers.get(player).cooldownMap.containsKey(ability)) {
                return;
            }
            cooldownPlayers.get(player).cooldownMap.remove(ability);
            Player cPlayer = Bukkit.getPlayer(player);
            if (player != null) {
                cPlayer.sendMessage(ChatColor.GRAY + "You can now use " + ChatColor.AQUA + ability);
            }
        }
        public static void handleCooldowns() {
            if (cooldownPlayers.isEmpty()) {
                return;
            }
            for (Iterator<String> it = cooldownPlayers.keySet().iterator(); it.hasNext();) {
                String key = it.next();
                for (Iterator<String> iter = cooldownPlayers.get(key).cooldownMap.keySet().iterator(); iter.hasNext();) {
                    String name = iter.next();
                    if (getRemaining(key, name) <= 0.0) {
                        removeCooldown(key, name);
                    }
                }
            }
        }
    }

     
    Scoreboard class:
    Code (Text):
    package me.roimeiqui.zfullpvp.utils.scoreboard;

    import java.util.ArrayList;
    import java.util.List;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Scoreboard;

    import me.roimeiqui.zfullpvp.FullPvP;
    import me.roimeiqui.zfullpvp.utils.clansystem.objects.ClanManager;
    import me.roimeiqui.zfullpvp.utils.mysql.MySQLManager;
    import me.roimeiqui.zfullpvp.utils.staffsystem.StaffCMD;
    import me.roimeiqui.zfullpvp.utils.staffsystem.VanishCommand;
    import tasks.cooldownmanager.Cooldown;

    public class zScoreboard extends MySQLManager implements Listener {

        public static String color(String s) {
            return ChatColor.translateAlternateColorCodes('&', s);
        }

        static org.bukkit.scoreboard.Scoreboard s;

        public static void setScoreboard(Player p) {
            FullPvP pl = FullPvP.getPlugin(FullPvP.class);
            Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(pl, new Runnable() {
                public void run() {
                    final Scoreboard board = Bukkit.getServer().getScoreboardManager().getNewScoreboard();
                    final Objective o = board.registerNewObjective("Scoreboard", "dummy");
                    o.setDisplayName(Placeholders.replaceValues(p, "&6&lZalexMC &7| &fFullPvP"));
                    o.setDisplaySlot(DisplaySlot.SIDEBAR);
                    final List<String> text = new ArrayList<String>();
                    text.add("&7-+--------------------+-");
                    text.add("&6Online» &f%online%");
                    text.add("&6Kills» &f%kills%");
                    text.add("&6Deaths» &f%deaths%");
                    if (Cooldown.isCooling(p.getName(), "Enderpearl")) {
                        text.add("&6EnderPearl» &f%time%");
                    }
                    if (ClanManager.isInClan(p)) {
                        text.add("&6Clan» &f" + ClanManager.getClan(p));
                    }
                    if (StaffCMD.a.contains(p)) {
                        text.add("&7-+--------------------+-");
                        text.add("&6StaffMode» &aEnabled");
                        text.add("   &eGamemode: &f" + p.getGameMode());
                        text.add("   &eStaffChat: &f"
                                + new me.roimeiqui.zfullpvp.commands.messages.iPlayer(p).isStaffChat());
                        text.add("   &eVanished: &f" + VanishCommand.isV.contains(p));
                    }
                    text.add("&7-+--------------------+-");

                    int size = text.size();
                    String f = "";
                    for (final String s : text) {
                        f = Placeholders.replaceValues(p, s);
                        int currentLine = size - 1;
                        if (currentLine <= 15 && currentLine-- > 0) {
                            f = String.valueOf(f) + Placeholders.colorcodes[currentLine--];
                        }
                        int deaths = getDeaths(p.getUniqueId());
                        int kills = getKills(p.getUniqueId());
                        o.getScore(ChatColor.translateAlternateColorCodes('&',
                                f.replace("%deaths%", String.valueOf(deaths)).replace("%kills%", String.valueOf(kills))
                                        .replace("%online%", String.valueOf(Bukkit.getServer().getOnlinePlayers().size()))
                                        .replace("%time%",
                                                String.valueOf(Cooldown.getRemaining(p.getName(), "Enderpearl")))))
                                .setScore(--size);
                    }
                    p.setScoreboard(board);
                }
            }, 0L, 1L);
        }

        @EventHandler
        public void onJoin(PlayerJoinEvent e) {

            setScoreboard(e.getPlayer());

        }

    }
     
    And i didn't forgot to put this in the main class
    Code (Text):
    Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
               
                @Override
                public void run() {
                    Cooldown.handleCooldowns();
                }
            }, 1L, 1L);
    thanks for help <3
     
  2. As you can see there are a , in the stack trace. As i remember, numbers should use . in decimals
     
  3. Yeah but @SunStorm where is this ",". I can't find it!
     
  4. U invoked Integer.parseInt("8,2"). I bet its a typo so plz change ',' to '.'.
     
  5. that maybe coming from config. Look at these lines.

    at tasks.cooldownmanager.utilMath.trim(utilMath.java:14) ~[?:?] <- and this is the place when you called directly the Double.valueOf()
    at tasks.cooldownmanager.utilMath.convert(utilMath.java:32) ~[?:?]
    at tasks.cooldownmanager.Cooldown.getRemaining(Cooldown.java:39) ~[?:?] <- then this
    at tasks.cooldownmanager.Cooldown.handleCooldowns(Cooldown.java:78) ~[?:?] <- invooked first in your code

    the stack trace is going upward.

    If you want to learn coding, learn how to read stack traces. This is the general part of coding.
     
  6. omg, I readed this stack trace, but I can't fix it, this is why I'm looking for help @SunStorm
     
  7. From what it looks like, you are formatting the double named untrimmed to a localized string in a language that uses commas instead of decimals. Then, you are attempting to parse the localized string using the method Double.valueOf() (which does not support localization). This is stated in Double's javadoc:
    Edit: After a quick read-through, it looks like a simple fix would be to use twoDec.parse() instead of Double.valueOf()

    Edit2:
    Alternatively, you can multiply the double by 100 and save the result, then store it back as a double and divide by 100 to trim the excess digits. This is purely a guess, but I'm assuming the multiplication and division operations are much more efficient than running through a string parser (and it's localization independent!).
     
    #8 iPyronic, Aug 11, 2018
    Last edited: Aug 11, 2018
    • Like Like x 1
  8. Thanks for helping me
     

Share This Page