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'
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; }
Just use GameMode#valueOf to convert the name to the enum object. Code (Text): player.setGameMode(GameMode.valueOf(config.getString("Gamemode")));
You could also make use of enum#valueOf(String). Which means for you something along Gamemode.valueOf(lol); //Edit: Got ninja´d^^
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); }
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
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
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())
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.
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
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.