Task #184 for KitPVP v1.0 generated an exception

Discussion in 'Spigot Plugin Development' started by MaxKrissigo, May 29, 2016.

  1. I'm getting this spammed in the console:
    Code (Text):
    [16:55:43 WARN]: [KitPVP] Task #184 for KitPVP v1.0 generated an exception
    java.lang.ClassCastException: org.bukkit.craftbukkit.v1_9_R1.scoreboard.CraftScoreboard cannot be cast to me.MaxKrissigo.KitPVP.Scoreboard
        at me.MaxKrissigo.KitPVP.Scoreboard$1.run(Scoreboard.java:81) ~[?:?]
        at org.bukkit.craftbukkit.v1_9_R1.scheduler.CraftTask.run(CraftTask.java:71) ~[spigot.jar:git-Spigot-e6f93f4-935f18b]
        at org.bukkit.craftbukkit.v1_9_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.MinecraftServer.D(MinecraftServer.java:729) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:400) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:660) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:559) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_92]
     
    I'm trying to transfer some of my code, when I moved it,
    import org.bukkit.scoreboard.Scoreboard; for some reason has an error in the new class, so I deleted it and then needed to add casts to the rest of my code. It worked in the other class, no idea why thats happening. With this current error, when I try changing it, I'm stuck in a loop like I can't seem to solve it withough messing up the other thing etc. Here is my code:
    Code (Text):
    package me.MaxKrissigo.KitPVP;

    import java.util.Arrays;
    import java.util.HashMap;
    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.entity.PlayerDeathEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.ScoreboardManager;


    public class Scoreboard implements Listener {

        HashMap<String, Scoreboard> playerSb = new HashMap<String,Scoreboard>();
       
        SettingsManager settings = SettingsManager.getInstance();
       
        @EventHandler
        public void onPlayerQuit(PlayerQuitEvent e) {
            Player p = e.getPlayer();
            String pUUID = p.getUniqueId().toString();
            settings.getStats().set("Players." + pUUID + ".CurrentKillStreak", 0);
        }
       
        @EventHandler
        public void onPlayerJoinEvent(PlayerJoinEvent e) {
            Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(Main.plugin, new Runnable()  {

                public void run() {
                   
                    Player p = e.getPlayer();
                    String uuid = p.getUniqueId().toString();
                   
                    ScoreboardManager manager = Bukkit.getScoreboardManager();
                    org.bukkit.scoreboard.Scoreboard board = manager.getNewScoreboard();
                    Objective objective = ((org.bukkit.scoreboard.Scoreboard) board).registerNewObjective("health", "dummy");
                    objective.setDisplaySlot(DisplaySlot.SIDEBAR);
                    objective.setDisplayName(ChatColor.BOLD + "" + ChatColor.GOLD + "Kit " + "PVP" + ChatColor.RED + " [BETA]");
                   
                    int kills = settings.getStats().getInt("Players." + uuid + ".Kills");
                    int deaths = settings.getStats().getInt("Players." + uuid + ".Deaths");
                    int currentKillStreak = settings.getStats().getInt("Players." + uuid + ".CurrentKillStreak");
                    int bestKillStreak = settings.getStats().getInt("Players." + uuid + ".BestKillStreak");
                    int xp = p.getTotalExperience();
                    String rank = settings.getRanks().getString("Ranks." + uuid);
                    int prestige = settings.getRanks().getInt("Prestige." + uuid);
                    double kdr = (double)kills/deaths;
                   
                    List<Score> scores = Arrays.asList(
                            objective.getScore("                        "),
                            objective.getScore("Kills: " + (int)kills),
                            objective.getScore("Deaths: " + (int)deaths),
                            objective.getScore("KDR: " + (double)kdr),
                            objective.getScore("                        "),
                            objective.getScore("Best Kill Streak: " + (int)bestKillStreak),
                            objective.getScore("Current Kill Streak: " + (int)currentKillStreak),
                            objective.getScore("                           "),
                            objective.getScore("XP: " + xp),
                            objective.getScore("                            "),
                            objective.getScore("Rank: " + rank),
                            objective.getScore("Prestige: " + prestige));
                           
                       
                   
                    int order = scores.size();
                    for(Score s : scores){
                        s.setScore(order);
                        order--;
                    }
                   
                    p.setScoreboard((org.bukkit.scoreboard.Scoreboard) board);
                   
                    playerSb.put(p.getName(), (Scoreboard) board);
                   
                }
               
            }, 1, 1);
           
            Player p = e.getPlayer();
            String uuid = p.getUniqueId().toString();
           
            if (!settings.getStats().contains("Players." + uuid)) {
                settings.getStats().set("Players." + uuid + ".Kills", 0);
                settings.getStats().set("Players." + uuid + ".Deaths", 0);      
                settings.getStats().set("Players." + uuid + ".Rank", "REC");
                settings.getStats().set("Players." + uuid + ".CurrentKillStreak", 0);
                settings.getStats().set("Players." + uuid + ".BestKillStreak", 0);
                settings.saveStats();
            }
        }

        @EventHandler
        public void onPlayerDeath(PlayerDeathEvent e) {
            Player p = e.getEntity();      
           
            e.getDrops().clear();
            e.setDroppedExp(0);
            if(p.getKiller() instanceof Player) {
               
                Player k = p.getKiller();
                String pUUID = p.getUniqueId().toString();
                String kUUID = k.getUniqueId().toString();
                int kills = settings.getStats().getInt("Players." + kUUID + ".Kills");
                int deaths = settings.getStats().getInt("Players." + pUUID + ".Deaths");
                int kcurrentKillStreak = settings.getStats().getInt("Players." + kUUID + ".CurrentKillStreak");
                int kbestKillStreak = settings.getStats().getInt("Players." + kUUID + ".BestKillStreak");
               
                settings.getStats().set("Players." + kUUID + ".Kills", kills +1);
                settings.getStats().set("Players." + pUUID + ".Deaths", deaths +1);
                settings.getStats().set("Players." + kUUID + ".CurrentKillStreak", kcurrentKillStreak +1);
                settings.getStats().set("Players." + pUUID + ".CurrentKillStreak", 0);
               
                if (kcurrentKillStreak == kbestKillStreak) {
                    settings.getStats().set("Players." + kUUID + ".BestKillStreak", deaths +1);
                }
                settings.saveStats();
               
                ScoreboardManager manager = Bukkit.getScoreboardManager();
                Scoreboard board = (Scoreboard) manager.getNewScoreboard();
                Objective objective = ((org.bukkit.scoreboard.Scoreboard) board).registerNewObjective("health", "dummy");
                objective.setDisplaySlot(DisplaySlot.SIDEBAR);
                objective.setDisplayName(ChatColor.BOLD + "" + ChatColor.GOLD + "Kit " + "PVP" + ChatColor.RED + " [BETA]");
                double kdr = (double)kills/deaths;
               
                List<Score> scores = Arrays.asList(
                       
                        objective.getScore(""),
                        objective.getScore(""),
                        objective.getScore("Kills: " + (int)kills),
                        objective.getScore("Deaths: " + (int)deaths),
                        objective.getScore("KDR: " + (double)kdr),
                        objective.getScore(""),
                        objective.getScore("Best Kill Streak: "),
                        objective.getScore("Current Kill Streak: "),
                        objective.getScore(""));
                       
                   
               
                int order = scores.size();
                for(Score s : scores){
                    s.setScore(order);
                    order--;
                }
               
                p.setScoreboard((org.bukkit.scoreboard.Scoreboard) board);
                k.setScoreboard((org.bukkit.scoreboard.Scoreboard) board);
               
                playerSb.put(p.getName(), board);
                playerSb.put(k.getName(), board);
               
               
            }
           
        }
       
    }
     
     
  2. Change

    playerSb.put(p.getName(), (Scoreboard) board);

    to

    playerSb.put(p.getName(), (org.bukkit.scoreboard.Scoreboard) board);
     
  3. That gives me this error and wants me to cast it to Scoreboard again:
    The method put(java.lang.String, me.MaxKrissigo.KitPVP.Scoreboard) in the type HashMap<String,Scoreboard> is not applicable for the arguments (java.lang.String,

    org.bukkit.scoreboard.Scoreboard)
     
  4. What's on line 81 of your code?
    Code (PHP):
    at me.MaxKrissigo.KitPVP.Scoreboard$1.run(Scoreboard.java:81) ~[?:?]
     
  5. I've changed my code a bit, but I believe it was one of these lines of code:
    Code (Text):
                    p.setScoreboard((org.bukkit.scoreboard.Scoreboard) board);
                   
                    playerSb.put(p.getName(), (Scoreboard) board);        
     
  6. It may be an error with the import; compare the imports of your old one with your new one and their places. (if spigot.jar is first or java 8 etc..)
     
  7. One of my imports isn't working it is this one: org.bukkit.scoreboard.Scoreboard and the error is
    The import org.bukkit.scoreboard.Scoreboard conflicts with a type defined in the same file. Although I can't find where else it would be defined?
     
  8. I meant build paths, sorry.
     
  9. There pretty much the same, except I just updated one of them to 1.9.4.
    EDIT: I've got the shaded and non shaded spigot api first and then the JRE system library.