Can anyone help me find the problem with a "no enum constant" error?

Discussion in 'Spigot Plugin Development' started by GregZ_, Jun 14, 2015.

  1. This is probably a very simple error that i should have spotted but after a hour of looking for it and not being able to find it i think an unbiased view of the code might be needed.

    This is the error i am getting:
    Code (Text):
    [17:20:12] [Server thread/ERROR]: Error occurred while enabling SurvivalGames v1.0.6 (Is it up to date?)
    java.lang.IllegalArgumentException: No enum constant com.GregZ_.survivalgames.game.GameState.JoinLobby
        at java.lang.Enum.valueOf(Unknown Source) ~[?:1.8.0_45]
        at com.GregZ_.survivalgames.game.GameState.valueOf(GameState.java:1) ~[?:?]
        at com.GregZ_.survivalgames.sign.SignManager.reload(SignManager.java:55) ~[?:?]
        at com.GregZ_.survivalgames.sign.SignManager.<init>(SignManager.java:32) ~[?:?]
        at com.GregZ_.survivalgames.SurvivalGames.onEnable(SurvivalGames.java:130) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot-1.8.3.jar:git-Spigot-dbe012b-61ef214]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:335) [spigot-1.8.3.jar:git-Spigot-dbe012b-61ef214]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot-1.8.3.jar:git-Spigot-dbe012b-61ef214]
        at org.bukkit.craftbukkit.v1_8_R2.CraftServer.loadPlugin(CraftServer.java:356) [spigot-1.8.3.jar:git-Spigot-dbe012b-61ef214]
        at org.bukkit.craftbukkit.v1_8_R2.CraftServer.enablePlugins(CraftServer.java:316) [spigot-1.8.3.jar:git-Spigot-dbe012b-61ef214]
        at org.bukkit.craftbukkit.v1_8_R2.CraftServer.reload(CraftServer.java:746) [spigot-1.8.3.jar:git-Spigot-dbe012b-61ef214]
        at org.bukkit.Bukkit.reload(Bukkit.java:534) [spigot-1.8.3.jar:git-Spigot-dbe012b-61ef214]
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot-1.8.3.jar:git-Spigot-dbe012b-61ef214]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot-1.8.3.jar:git-Spigot-dbe012b-61ef214]
        at org.bukkit.craftbukkit.v1_8_R2.CraftServer.dispatchCommand(CraftServer.java:646) [spigot-1.8.3.jar:git-Spigot-dbe012b-61ef214]
        at org.bukkit.craftbukkit.v1_8_R2.CraftServer.dispatchServerCommand(CraftServer.java:632) [spigot-1.8.3.jar:git-Spigot-dbe012b-61ef214]
        at net.minecraft.server.v1_8_R2.DedicatedServer.aN(DedicatedServer.java:405) [spigot-1.8.3.jar:git-Spigot-dbe012b-61ef214]
        at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:369) [spigot-1.8.3.jar:git-Spigot-dbe012b-61ef214]
        at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:651) [spigot-1.8.3.jar:git-Spigot-dbe012b-61ef214]
        at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java:554) [spigot-1.8.3.jar:git-Spigot-dbe012b-61ef214]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
    The problem is that i have checked through my code and the only enum like that that i use that is similar to this is JOINLOBBY.

    The emum looks like this:
    [​IMG]
    Any help would be appreciated and thank you for reading this.
     
  2. I think it may be that you wrote "JoinLobby" and not "JOINLOBBY" but that's only a quick speculation.
     
  3. Enums are CaSeSeNsItIvE!
    Do you get any compiler-errors?
     
  4. I get no compiler errors and when I used the file search in eclipse and it found nothing that was cased incorrectly for that enum.
     
  5. Can we see the signmanager class?
     
  6. Sure.
    Code (Text):
    package com.GregZ_.survivalgames.sign;

    import java.util.HashMap;
    import java.util.List;
    import java.util.Map.Entry;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.block.Sign;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;

    import com.GregZ_.survivalgames.SurvivalGames;
    import com.GregZ_.survivalgames.Util;
    import com.GregZ_.survivalgames.commands.messages.MessageHandler;
    import com.GregZ_.survivalgames.game.Game;
    import com.GregZ_.survivalgames.game.GameState;

    public class SignManager {

        private String[] design = new String[4];
        private String[] leaveDesign = new String[4];
        private HashMap<Location, String> signs = new HashMap<>();
        private HashMap<GameState, String> translations = new HashMap<>();
        private boolean arena, playersleft;

        public SignManager() {
            reload();
        }

        public void reload() {
            FileConfiguration c = SurvivalGames.signs;
            for (int i = 1; i <= 4; i++) {
                design[i - 1] = ChatColor.translateAlternateColorCodes('&',
                        c.getString("Sign.Line." + i));
            }

            arena = c.getBoolean("Sign.LeftClick.Show current arena");
            playersleft = c.getBoolean("Sign.LeftClick.Show players remain");

            leaveDesign[0] = ChatColor.translateAlternateColorCodes('&',
                    c.getString("Sign.LeavePrefix"));
            for (int i = 2; i <= 4; i++) {
                leaveDesign[i - 1] = ChatColor.translateAlternateColorCodes('&',
                        c.getString("Sign.Leave.Line." + i));
            }

            for (String key : c.getConfigurationSection("Translations.").getKeys(
                    false)) {
                translations.put(
                        GameState.valueOf(key),
                        ChatColor.translateAlternateColorCodes('&',
                                c.getString("Translations." + key)));
            }

            List<String> s = c.getStringList("Sign.List");
            int a = 0;
            for (String key : s) {
                String[] split = key.split(":");

                Location loc = Util.parseLocation(split[0]);
                if (loc != null) {
                    signs.put(loc, split[1]);
                }

                a++;
            }
            System.out.println((char) 27 + "[32;1m[SurvivalGames] " + (char) 27
                    + "[36;1m" + a + " signs loaded!" + (char) 27 + "[0m");
        }

        public void addSign(Player p, final Location loc, final String lobby) {
            if (!signs.containsKey(loc)) {
                List<String> signs = SurvivalGames.signs.getStringList("Sign.List");
                signs.add(Util.serializeLocation(loc, false) + ":" + lobby);
                SurvivalGames.signs.set("Sign.List", signs);
                SurvivalGames.saveSigns();
                this.signs.put(loc, lobby);

                Bukkit.getScheduler().scheduleSyncDelayedTask(
                        SurvivalGames.instance, new Runnable() {
                            public void run() {
                                updateSign(loc, lobby);
                            }
                        }, 1L);
                p.sendMessage(MessageHandler.getMessage("prefix")
                        + "You've created the join sign successfully!");
            }
        }

        public boolean isSign(Location loc) {
            return signs.containsKey(loc);
        }

        public void removeSign(Player p, Location loc) {
            if (signs.containsKey(loc)) {
                String lobby = signs.get(loc);
                this.signs.remove(loc);
                List<String> signs = SurvivalGames.signs.getStringList("Sign.List");
                signs.remove(Util.serializeLocation(loc, false) + ":" + lobby);
                SurvivalGames.signs.set("Sign.List", signs);
                SurvivalGames.saveSigns();
                p.sendMessage(MessageHandler.getMessage("prefix")
                        + "You've removed the join sign successfully!");
            }
        }

        public String[] getLeaveSignDesign() {
            return leaveDesign;
        }

        public String getLobby(Location loc) {
            if (signs.containsKey(loc))
                return signs.get(loc);
            return null;
        }

        public void sendInfo(CommandSender sender, String lobby) {
            Game g = SurvivalGames.gameManager.getGame(lobby);
            if (g != null) {
                sender.sendMessage(MessageHandler.getMessage("game-sign-info")
                        .replace("%0%", lobby));
                if (g.getState() == GameState.INGAME
                        || g.getState() == GameState.DEATHMATCH
                        || g.getState() == GameState.COOLDOWN) {
                    if (arena)
                        sender.sendMessage(MessageHandler.getMessage(
                                "game-sign-arena").replace("%0%",
                                g.getCurrentArena().getName()));
                    if (playersleft) {
                        String s = g.getAlivePlayers();
                        sender.sendMessage(MessageHandler
                                .getMessage("game-sign-playersleft")
                                .replace("%1%", s)
                                .replace(
                                        "%0%",
                                        Integer.valueOf(g.getPlayingUsers())
                                                .toString()));
                    }
                } else {
                    sender.sendMessage(MessageHandler
                            .getMessage("game-sign-noinfo"));
                }
            } else {
                sender.sendMessage(MessageHandler.getMessage("join-unknown-game")
                        .replace("%0%", lobby));
            }
        }

        public void updateSigns() {
            for (Entry<Location, String> s : signs.entrySet()) {
                Location loc = s.getKey();
                if (loc != null && loc.getWorld() != null) {
                    updateSign(loc, s.getValue());
                }
            }
        }

        public void updateSign(Location loc, String lobby) {
            Block b = loc.getBlock();
            if (b.getType() == Material.SIGN_POST
                    || b.getType() == Material.WALL_SIGN) {
                Sign s = (Sign) b.getState();
                Game g = SurvivalGames.gameManager.getGame(lobby);
                if (g != null) {
                    String state = translations.get(g.getState());
                    for (int i = 0; i < 4; i++) {
                        s.setLine(
                                i,
                                design[i]
                                        .replace("%name%", g.getName())
                                        .replace("%state%", state)
                                        .replace(
                                                "%currentplayers%",
                                                Integer.valueOf(g.getPlayingUsers())
                                                        .toString())
                                        .replace(
                                                "%requiredplayers%",
                                                Integer.valueOf(
                                                        g.getRequiredPlayers())
                                                        .toString())
                                        .replace(
                                                "%maxplayers%",
                                                Integer.valueOf(
                                                        g.getMaximumPlayers())
                                                        .toString()));
                    }
                    s.update();
                } else {
                    if (SurvivalGames.database.contains("Games." + lobby)) {
                        s.setLine(1, "§4Game not");
                        s.setLine(2, "§4loaded!");
                    } else {
                        s.setLine(1, "§4Game not");
                        s.setLine(2, "§4found!");
                    }
                    s.update();
                }
            }
        }

    }
     
     
  7. Honestly, it seems that your enumeration constant does not even appear in your code. Did you make sure that the plugin you uploaded was the latest build you made?
     
  8. The enum is not used in the signmannager class it is used in the other areas and that is the latest build.
     
  9. Ah, I guess I got your mistake:
    Code (Text):
    for (String key : c.getConfigurationSection("Translations.").getKeys(
                    false)) {
                translations.put(
                        GameState.valueOf(key),
                        ChatColor.translateAlternateColorCodes('&',
                                c.getString("Translations." + key)));
            }
     
    You might invoking GameState.valueOf( ... ) with a wrong translation (or however you call it). The Java Documentation says:
    inside the documentation of Enum.valueOf( ... ) . So maybe you should check your configuration / translation file if it contains a translation named JoinLobby instead of JOINLOBBY.
     
  10. a simple (yet possibly flawed in the long term fix) is to getString and .toUpperCase(). That will make it all caps which essentially are your enum values.
     
  11. You should rather not do this. If there's only one enumeration constant with another naming convention (hopefully there's not, though) you will be wondering even more as it works for any other combination.