From b9400631046b1c244de43d707159bc9f5bc3fe1e Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Thu, 22 Jul 2021 11:39:39 +0800 Subject: [PATCH] Implement new way of mapping enums with alias. --- .../commands/CreateCommand.java | 25 +++-- .../commands/EnvironmentCommand.java | 42 ++------ .../commands/ImportCommand.java | 18 +++- .../MultiverseCore/enums/MVEnums.java | 20 ++++ .../MultiverseCore/utils/EnumMapping.java | 101 ++++++++++++++++++ 5 files changed, 158 insertions(+), 48 deletions(-) create mode 100644 src/main/java/com/onarandombox/MultiverseCore/enums/MVEnums.java create mode 100644 src/main/java/com/onarandombox/MultiverseCore/utils/EnumMapping.java diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/CreateCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/CreateCommand.java index 6d8f3833..a47dccdc 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/CreateCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/CreateCommand.java @@ -9,6 +9,7 @@ package com.onarandombox.MultiverseCore.commands; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVWorldManager; +import com.onarandombox.MultiverseCore.enums.MVEnums; import com.pneumaticraft.commandhandler.CommandHandler; import org.bukkit.ChatColor; import org.bukkit.World.Environment; @@ -21,6 +22,7 @@ import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Optional; /** * Creates a new world and loads it. @@ -90,23 +92,22 @@ public class CreateCommand extends MultiverseCommand { return; } - Environment environment = EnvironmentCommand.getEnvFromString(env); - if (environment == null) { + // Parse Environment. + Optional environment = MVEnums.ENVIRONMENT.parseValue(env); + if (!environment.isPresent()) { sender.sendMessage(ChatColor.RED + "That is not a valid environment."); EnvironmentCommand.showEnvironments(sender); return; } - // If they didn't specify a type, default to NORMAL - if (typeString == null) { - typeString = "NORMAL"; - } - WorldType type = EnvironmentCommand.getWorldTypeFromString(typeString); - if (type == null) { + // Parse WorldType. If they didn't specify a type, default to normal. + Optional type = MVEnums.WORLD_TYPE.parseValue(typeString == null ? "normal" : typeString); + if (!type.isPresent()) { sender.sendMessage(ChatColor.RED + "That is not a valid World Type."); EnvironmentCommand.showWorldTypes(sender); return; } + // Determine if the generator is valid. #918 if (generator != null) { List genarray = new ArrayList(Arrays.asList(generator.split(":"))); @@ -122,7 +123,13 @@ public class CreateCommand extends MultiverseCommand { } Command.broadcastCommandMessage(sender, "Starting creation of world '" + worldName + "'..."); - if (this.worldManager.addWorld(worldName, environment, seed, type, allowStructures, generator, useSpawnAdjust)) { + if (this.worldManager.addWorld( + worldName, + environment.get(), + seed, type.get(), + allowStructures, + generator, + useSpawnAdjust)) { Command.broadcastCommandMessage(sender, "Complete!"); } else { Command.broadcastCommandMessage(sender, "FAILED."); diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/EnvironmentCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/EnvironmentCommand.java index f9ac8860..70bd1ae2 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/EnvironmentCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/EnvironmentCommand.java @@ -8,6 +8,7 @@ package com.onarandombox.MultiverseCore.commands; import com.onarandombox.MultiverseCore.MultiverseCore; +import com.onarandombox.MultiverseCore.enums.MVEnums; import org.bukkit.ChatColor; import org.bukkit.World; import org.bukkit.WorldType; @@ -68,25 +69,11 @@ public class EnvironmentCommand extends MultiverseCommand { * * @param type The WorldType as a {@link String} * @return The WorldType as a {@link WorldType} + * @deprecated Use {@link MVEnums#WORLD_TYPE} instead. */ + @Deprecated public static WorldType getWorldTypeFromString(String type) { - // Don't reference the enum directly as there aren't that many, and we can be more forgiving to users this way - if (type.equalsIgnoreCase("normal")) { - type = "NORMAL"; - } else if (type.equalsIgnoreCase("flat")) { - type = "FLAT"; - } else if (type.equalsIgnoreCase("largebiomes")) { - type = "LARGE_BIOMES"; - } else if (type.equalsIgnoreCase("amplified")) { - type = "AMPLIFIED"; - } - try { - // Now that we've converted a potentially unfriendly value - // to a friendly one, get it from the ENUM! - return WorldType.valueOf(type); - } catch (IllegalArgumentException e) { - return null; - } + return MVEnums.WORLD_TYPE.parseValue(type).orElse(null); } /** @@ -94,25 +81,10 @@ public class EnvironmentCommand extends MultiverseCommand { * * @param env The environment as {@link String} * @return The environment as {@link org.bukkit.World.Environment} + * @deprecated Use {@link MVEnums#ENVIRONMENT} instead. */ + @Deprecated public static World.Environment getEnvFromString(String env) { - env = env.toUpperCase(); - // Don't reference the enum directly as there aren't that many, and we can be more forgiving to users this way - if (env.equalsIgnoreCase("HELL") || env.equalsIgnoreCase("NETHER")) - env = "NETHER"; - - if (env.equalsIgnoreCase("END") || env.equalsIgnoreCase("THEEND") || env.equalsIgnoreCase("STARWARS")) - env = "THE_END"; - - if (env.equalsIgnoreCase("NORMAL") || env.equalsIgnoreCase("WORLD")) - env = "NORMAL"; - - try { - // Now that we've converted a potentially unfriendly value - // to a friendly one, get it from the ENUM! - return World.Environment.valueOf(env); - } catch (IllegalArgumentException e) { - return null; - } + return MVEnums.ENVIRONMENT.parseValue(env).orElse(null); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/ImportCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/ImportCommand.java index 7a52d5a1..6fcd6e7f 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/ImportCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/ImportCommand.java @@ -9,6 +9,7 @@ package com.onarandombox.MultiverseCore.commands; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVWorldManager; +import com.onarandombox.MultiverseCore.enums.MVEnums; import com.onarandombox.MultiverseCore.utils.WorldNameChecker; import com.pneumaticraft.commandhandler.CommandHandler; import org.bukkit.ChatColor; @@ -20,6 +21,7 @@ import org.bukkit.permissions.PermissionDefault; import java.io.File; import java.util.Collection; import java.util.List; +import java.util.Optional; /** * Imports a new world of the specified type. @@ -106,8 +108,8 @@ public class ImportCommand extends MultiverseCommand { } String env = args.get(1); - Environment environment = EnvironmentCommand.getEnvFromString(env); - if (environment == null) { + Optional environment = MVEnums.ENVIRONMENT.parseValue(env); + if (!environment.isPresent()) { sender.sendMessage(ChatColor.RED + "That is not a valid environment."); EnvironmentCommand.showEnvironments(sender); return; @@ -128,10 +130,18 @@ public class ImportCommand extends MultiverseCommand { sender.sendMessage("For a list of available world types, type: " + ChatColor.AQUA + "/mvenv"); } else { Command.broadcastCommandMessage(sender, String.format("Starting import of world '%s'...", worldName)); - if (this.worldManager.addWorld(worldName, environment, null, null, null, generator, useSpawnAdjust)) + if (this.worldManager.addWorld(worldName, + environment.get(), + null, + null, + null, + generator, + useSpawnAdjust)) { Command.broadcastCommandMessage(sender, ChatColor.GREEN + "Complete!"); - else + } + else { Command.broadcastCommandMessage(sender, ChatColor.RED + "Failed!"); + } } } } \ No newline at end of file diff --git a/src/main/java/com/onarandombox/MultiverseCore/enums/MVEnums.java b/src/main/java/com/onarandombox/MultiverseCore/enums/MVEnums.java new file mode 100644 index 00000000..23fa7d3f --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/enums/MVEnums.java @@ -0,0 +1,20 @@ +package com.onarandombox.MultiverseCore.enums; + +import com.onarandombox.MultiverseCore.utils.EnumMapping; +import org.bukkit.World; +import org.bukkit.WorldType; + +public class MVEnums { + + public static final EnumMapping ENVIRONMENT = EnumMapping.builder(World.Environment.class) + .addAlias("overworld", World.Environment.NORMAL) + .addAlias("hell", World.Environment.NETHER) + .addAlias("end", World.Environment.THE_END) + .addAlias("starwars", World.Environment.THE_END) + .mapWithoutUnderscore() + .build(); + + public static final EnumMapping WORLD_TYPE = EnumMapping.builder(WorldType.class) + .mapWithoutUnderscore() + .build(); +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/EnumMapping.java b/src/main/java/com/onarandombox/MultiverseCore/utils/EnumMapping.java new file mode 100644 index 00000000..21b29339 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/EnumMapping.java @@ -0,0 +1,101 @@ +package com.onarandombox.MultiverseCore.utils; + +import org.jetbrains.annotations.NotNull; + +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +public class EnumMapping> { + + public static > Builder builder(Class enumClass) { + return builder(enumClass, false); + } + + public static > Builder builder(Class enumClass, boolean caseSensitive) { + return new Builder<>(enumClass, caseSensitive); + } + + private final Class enumClass; + private final boolean caseSensitive; + private final Map enumsMap; + + public EnumMapping(Class enumClass, boolean caseSensitive) { + this.enumClass = enumClass; + this.caseSensitive = caseSensitive; + this.enumsMap = new HashMap<>(); + } + + @NotNull + public Optional parseValue(String value) { + T enumType = this.enumsMap.get(caseSensitive ? value : value.toLowerCase()); + if (enumType != null) { + return Optional.of(enumType); + } + try { + return Optional.of(Enum.valueOf(enumClass, value)); + } catch (IllegalArgumentException e) { + return Optional.empty(); + } + } + + @NotNull + public Map getEnumsMap() { + return Collections.unmodifiableMap(this.enumsMap); + } + + public boolean isCaseSensitive() { + return caseSensitive; + } + + public Class> getEnumClass() { + return enumClass; + } + + public static class Builder> { + + private final EnumMapping enumMapping; + + public Builder(Class enumClass, boolean caseSensitive) { + this.enumMapping = new EnumMapping<>(enumClass, caseSensitive); + if (!this.enumMapping.caseSensitive) { + addLowerCaseMap(); + } + } + + private void addLowerCaseMap() { + Arrays.stream(this.enumMapping.enumClass.getEnumConstants()).forEach(enumType -> { + addAlias(enumType.name(), enumType); + }); + } + + public Builder mapWithoutUnderscore() { + Arrays.stream(this.enumMapping.enumClass.getEnumConstants()).forEach(enumType -> { + addAlias(enumType.name().replace("_", ""), enumType); + }); + return this; + } + + @NotNull + public Builder addAlias(String aliasName, String enumString) { + this.enumMapping.parseValue(enumString).ifPresent(enumType -> addAlias(aliasName, enumType)); + return this; + } + + @NotNull + public Builder addAlias(String aliasName, T enumType) { + if (!this.enumMapping.caseSensitive) { + aliasName = aliasName.toLowerCase(); + } + this.enumMapping.enumsMap.computeIfAbsent(aliasName, name -> enumType); + return this; + } + + @NotNull + public EnumMapping build() { + return this.enumMapping; + } + } +}