Solved Objective already exists

Discussion in 'Plugin Development' started by CheesyFreezy, Apr 21, 2015.

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

    CheesyFreezy

    Hi hi developers,

    I really need your help. I've been working for 1-2 months trying to make a custom survival games plugin. The problem is that i can't create an objective which already exists.....

    What i'm trying to do in this piece of code is that there will be displayed a healthbar under the name of the player. But i can't create this objective because it already exists.

    Code:
    Code:
        @SuppressWarnings("deprecation")
        public void SetHealth(Player player) {
            ScoreboardManager sbManager = Bukkit.getServer().getScoreboardManager();
            SGhealth = sbManager.getNewScoreboard();
           
            if(SGhealth.getObjective("sgHealth") == null) {
                healthObj = SGscoreboard.registerNewObjective("sgHealth", "dummy");
                healthObj.setDisplayName("" + ChatColor.RED + "❤ ");
                healthObj.setDisplaySlot(DisplaySlot.BELOW_NAME);
               
                Damageable playerD = (Damageable) player;
                Score healthScore = healthObj.getScore(player);
                healthScore.setScore((int) playerD.getHealth());
            } else {
                healthObj = SGscoreboard.getObjective("sgHealth");
                healthObj.setDisplayName("" + ChatColor.RED + "❤ ");
                healthObj.setDisplaySlot(DisplaySlot.BELOW_NAME);
               
                Damageable playerD = (Damageable) player;
                Score healthScore = healthObj.getScore(player);
                healthScore.setScore((int) playerD.getHealth());
            }
           
            player.setScoreboard(SGhealth);
        }
    Error:
    StackTrace (open)

    [18:50:40] [Server thread/WARN]: [ArcictMC] Task #180 for ArcictMC v0.1.7 generated an exception
    java.lang.IllegalArgumentException: An objective of name 'sgHealth' already exists
    at org.apache.commons.lang.Validate.isTrue(Validate.java:136) ~[craftbukkit.jar:git-Spigot-da32fe1-df0b4c1]
    at org.bukkit.craftbukkit.v1_8_R1.scoreboard.CraftScoreboard.registerNewObjective(CraftScoreboard.java:40) ~[craftbukkit.jar:git-Spigot-da32fe1-df0b4c1]
    at org.bukkit.craftbukkit.v1_8_R1.scoreboard.CraftScoreboard.registerNewObjective(CraftScoreboard.java:20) ~[craftbukkit.jar:git-Spigot-da32fe1-df0b4c1]
    at me.CheesyFreezy.Scoreboard.Scoreboard.SetHealth(Scoreboard.java:219) ~[?:?]
    at me.CheesyFreezy.Main.Core$2.run(Core.java:255) ~[?:?]
    at org.bukkit.craftbukkit.v1_8_R1.scheduler.CraftTask.run(CraftTask.java:71) ~[craftbukkit.jar:git-Spigot-da32fe1-df0b4c1]
    at org.bukkit.craftbukkit.v1_8_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350) [craftbukkit.jar:git-Spigot-da32fe1-df0b4c1]
    at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:697) [craftbukkit.jar:git-Spigot-da32fe1-df0b4c1]
    at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [craftbukkit.jar:git-Spigot-da32fe1-df0b4c1]
    at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:627) [craftbukkit.jar:git-Spigot-da32fe1-df0b4c1]
    at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:530) [craftbukkit.jar:git-Spigot-da32fe1-df0b4c1]
    at java.lang.Thread.run(Unknown Source) [?:1.7.0_71]
     
  2. Offline

    meguy26

    @CheesyFreezy
    First of all I hope your copy-paste messed up because SGhealth does not have a type and healthObj is not defined...

    Second of all, is it possible that you are importing the wrong scoreboard? According to the stacktrace you are using CraftScoreboard. I am using org.bukkit.scoreboard.Scoreboard and can succesfully register the same object multiple times.
     
  3. Offline

    pie_flavor

    @meguy26 Scoreboard is an interface, and CraftScoreboard is a class. CraftScoreboard would show up in your own stacktraces if they existed because it's the actual class that is used at runtime. Also, a copy paste cannot 'screw up' like that but it is more likely that SGhealth is a member instead of local.
    @CheesyFreezy This is very perplexing because nothing has really happened yet when the scoreboard is registered. What other plugins do you have running?

    Also, you have a lot of code repeated. Why not just create the variable, then initialize it inside the if statement, then do the rest of the code?
     
  4. Offline

    bcohen9685

    at org.apache.commons.lang.Validate.isTrue(Validate.java:136) ~[craftbukkit.jar:git-Spigot-da32fe1-df0b4c1]

    Now we know exactly where the issue is. In your "Validate.java" on line 136 there's an issue.

    (No spoon feeding allowed)
     
  5. Offline

    CheesyFreezy

    @pie_flavor I've got no other plugins running, except for this one. I know i'm repeating the same code.
    @bcohen9685 That error is not of one of my classes. Read the stacktrace better. It says at me.CheesyFreezy.Scoreboard.Scoreboard.SetHealth(Scoreboard.java:219)
     
Thread Status:
Not open for further replies.

Share This Page