Dont understand what is going wrong HELP

Discussion in 'Spigot Plugin Development' started by danielm175, Apr 19, 2017.

  1. Hi this is my code but i dont understand what i am doing wrong here



    Code (Text):
            String lol = getConfig().getString("Gamemode");
           
             player.setGameMode(lol);


    That was my code and i am trying to allow players in the config to change the gamemode it works if i replace Gamemode with CREATIVE. But i want the user to change it in the config



    Config file:



    Code (Text):
    Gamemode: 'CREATIVE'
     
  2. You are trying to pass a String into a function that only takes a GameMode.
     
    • Optimistic Optimistic x 1
  3. Is their away to fix this so i can allow players to change the gamemode in the config or not?
     
  4. Change it to GameMode.CREATIVE
     
  5. You will need to create a switch statement, something like
    Code (Java):
    switch (lol) {
            case "SURVIVAL":
                player.setGameMode(GameMode.SURVIVAL);
                break;
            case "CREATIVE":
                player.setGameMode(GameMode.CREATIVE);
                break;

            default:
                break;
            }
     
    • Like Like x 1
    • Useful Useful x 1
  6. Just use GameMode#valueOf to convert the name to the enum object.
    Code (Text):
    player.setGameMode(GameMode.valueOf(config.getString("Gamemode")));
     
    • Agree Agree x 4
    • Like Like x 2
  7. You could also make use of enum#valueOf(String).
    Which means for you something along Gamemode.valueOf(lol);

    //Edit: Got ninja´d^^
     
  8. Choco

    Moderator

    Sidenote to the OP on this. When retrieving enum-based values from a configuration file, I recommend calling #toUpperCase(), and instead using an alternative method to prevent any exceptions. #valueOf() throws an IllegalArgumentException if the constant does not exist. To prevent this, using EnumUtils#getEnum() would instead return null rather than throwing an exception
    Code (Java):
    GameMode gamemode = EnumUtils.getEnum(GameMode.class, this.getConfig().getString("Gamemode").toUpperCase());
    if (gamemode != null) { // Or return if it is null
        player.setGameMode(gamemode);
    }
     
  9. or with a (better and) easier language like kotlin:
    Code (Java):
    val gamemode = EnumUtils.getEnum(GameMode::class.java, config.getString("Gamemode").toUpperCase()) ?: return
    player.gamemode = gamemode
     
  10. Choco

    Moderator

    Sure, but the OP is writing in Java ;) Also, I thought a syntax highlighting for Kotlin was implemented. Is that a thing now?
    Code (Kotlin):
    val gamemode = EnumUtils.getEnum(GameMode::class.java, config.getString("Gamemode").toUpperCase()) ?: return
    player.gamemode = gamemode
    EDIT: lolnope. I know it was PR'd. I guess Spigot just didn't update it yet
     
    • Like Like x 1
  11. In that case it would also help to define an extra helper function so that you don't have to pass the long and ugly looking class argument every time:
    Code (Java):
    inline fun <reified T : Enum<T>> getEnum(name:String): T? =
            EnumUtils.getEnum(T::class.java, name)
    And to call:
    Code (Java):
    val gamemode = getEnum<GameMode>(config.getString("Gamemode").toUpperCase())

    //OR:

    val gamemode: GameMode? = getEnum(config.getString("Gamemode").toUpperCase())
     
  12. Can't you just easily catch it? I mean, sure it looks a bit more cleaner, but performance wise?

    EDIT: Nope, the EnumUtil actually proves to be considerably faster.
     
    #12 ExpDev, Apr 26, 2017
    Last edited: Apr 26, 2017
    • Like Like x 1
    • Agree Agree x 1
  13. Code (Java):
        public static <E extends Enum<E>> E getEnum(final Class<E> enumClass, final String enumName) {
            if (enumName == null) {
                return null;
            }
            try {
                return Enum.valueOf(enumClass, enumName);
            } catch (final IllegalArgumentException ex) {
                return null;
            }
        }
    how?
    http://commons.apache.org/proper/co...-html/org/apache/commons/lang3/EnumUtils.html
     
  14. Idk. Check out my code:
    Code (Text):
    public static void main(String[] strings) {
        String s = "notcolor";

        long now = System.nanoTime();
        ChatColor cc;
        try {
            cc = ChatColor.valueOf(s.toUpperCase());
        } catch (IllegalArgumentException e) {
            cc = ChatColor.BLUE; // default
        }
        long dur = System.nanoTime() - now;
        System.out.println("Test 1 - Result: " + cc.toString() + ". Operation took " + dur + " nanoseconds.");

        s = "notcolor";

        now = System.nanoTime();
        cc = EnumUtils.getEnum(ChatColor.class, s.toUpperCase());
        if (cc == null) {
            cc = ChatColor.BLUE; // default
        }

        dur = System.nanoTime() - now;
        System.out.println("Test 2 - Result: " + cc.toString() + ". Operation took " + dur + " nanoseconds.");
    }
    Output:
    Code (Text):
    Test 1 - Result: §9. Operation took 52494520 nanoseconds.
    Test 2 - Result: §9. Operation took 2313695 nanoseconds.
    EDIT: Order shouldn't matter, nor assigning new value to object.
     
  15. Perform quantity (10k or more) and warm JVM up first (no result solving).