mirror of
https://github.com/Multiverse/Multiverse-Core.git
synced 2024-11-29 05:55:48 +01:00
Implement new way of mapping enums with alias.
This commit is contained in:
parent
1a8a97daf1
commit
b940063104
@ -9,6 +9,7 @@ package com.onarandombox.MultiverseCore.commands;
|
|||||||
|
|
||||||
import com.onarandombox.MultiverseCore.MultiverseCore;
|
import com.onarandombox.MultiverseCore.MultiverseCore;
|
||||||
import com.onarandombox.MultiverseCore.api.MVWorldManager;
|
import com.onarandombox.MultiverseCore.api.MVWorldManager;
|
||||||
|
import com.onarandombox.MultiverseCore.enums.MVEnums;
|
||||||
import com.pneumaticraft.commandhandler.CommandHandler;
|
import com.pneumaticraft.commandhandler.CommandHandler;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.World.Environment;
|
import org.bukkit.World.Environment;
|
||||||
@ -21,6 +22,7 @@ import java.io.File;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new world and loads it.
|
* Creates a new world and loads it.
|
||||||
@ -90,23 +92,22 @@ public class CreateCommand extends MultiverseCommand {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Environment environment = EnvironmentCommand.getEnvFromString(env);
|
// Parse Environment.
|
||||||
if (environment == null) {
|
Optional<Environment> environment = MVEnums.ENVIRONMENT.parseValue(env);
|
||||||
|
if (!environment.isPresent()) {
|
||||||
sender.sendMessage(ChatColor.RED + "That is not a valid environment.");
|
sender.sendMessage(ChatColor.RED + "That is not a valid environment.");
|
||||||
EnvironmentCommand.showEnvironments(sender);
|
EnvironmentCommand.showEnvironments(sender);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If they didn't specify a type, default to NORMAL
|
// Parse WorldType. If they didn't specify a type, default to normal.
|
||||||
if (typeString == null) {
|
Optional<WorldType> type = MVEnums.WORLD_TYPE.parseValue(typeString == null ? "normal" : typeString);
|
||||||
typeString = "NORMAL";
|
if (!type.isPresent()) {
|
||||||
}
|
|
||||||
WorldType type = EnvironmentCommand.getWorldTypeFromString(typeString);
|
|
||||||
if (type == null) {
|
|
||||||
sender.sendMessage(ChatColor.RED + "That is not a valid World Type.");
|
sender.sendMessage(ChatColor.RED + "That is not a valid World Type.");
|
||||||
EnvironmentCommand.showWorldTypes(sender);
|
EnvironmentCommand.showWorldTypes(sender);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine if the generator is valid. #918
|
// Determine if the generator is valid. #918
|
||||||
if (generator != null) {
|
if (generator != null) {
|
||||||
List<String> genarray = new ArrayList<String>(Arrays.asList(generator.split(":")));
|
List<String> genarray = new ArrayList<String>(Arrays.asList(generator.split(":")));
|
||||||
@ -122,7 +123,13 @@ public class CreateCommand extends MultiverseCommand {
|
|||||||
}
|
}
|
||||||
Command.broadcastCommandMessage(sender, "Starting creation of world '" + worldName + "'...");
|
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!");
|
Command.broadcastCommandMessage(sender, "Complete!");
|
||||||
} else {
|
} else {
|
||||||
Command.broadcastCommandMessage(sender, "FAILED.");
|
Command.broadcastCommandMessage(sender, "FAILED.");
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
package com.onarandombox.MultiverseCore.commands;
|
package com.onarandombox.MultiverseCore.commands;
|
||||||
|
|
||||||
import com.onarandombox.MultiverseCore.MultiverseCore;
|
import com.onarandombox.MultiverseCore.MultiverseCore;
|
||||||
|
import com.onarandombox.MultiverseCore.enums.MVEnums;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
import org.bukkit.WorldType;
|
import org.bukkit.WorldType;
|
||||||
@ -68,25 +69,11 @@ public class EnvironmentCommand extends MultiverseCommand {
|
|||||||
*
|
*
|
||||||
* @param type The WorldType as a {@link String}
|
* @param type The WorldType as a {@link String}
|
||||||
* @return The WorldType as a {@link WorldType}
|
* @return The WorldType as a {@link WorldType}
|
||||||
|
* @deprecated Use {@link MVEnums#WORLD_TYPE} instead.
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public static WorldType getWorldTypeFromString(String type) {
|
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
|
return MVEnums.WORLD_TYPE.parseValue(type).orElse(null);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -94,25 +81,10 @@ public class EnvironmentCommand extends MultiverseCommand {
|
|||||||
*
|
*
|
||||||
* @param env The environment as {@link String}
|
* @param env The environment as {@link String}
|
||||||
* @return The environment as {@link org.bukkit.World.Environment}
|
* @return The environment as {@link org.bukkit.World.Environment}
|
||||||
|
* @deprecated Use {@link MVEnums#ENVIRONMENT} instead.
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public static World.Environment getEnvFromString(String env) {
|
public static World.Environment getEnvFromString(String env) {
|
||||||
env = env.toUpperCase();
|
return MVEnums.ENVIRONMENT.parseValue(env).orElse(null);
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@ package com.onarandombox.MultiverseCore.commands;
|
|||||||
|
|
||||||
import com.onarandombox.MultiverseCore.MultiverseCore;
|
import com.onarandombox.MultiverseCore.MultiverseCore;
|
||||||
import com.onarandombox.MultiverseCore.api.MVWorldManager;
|
import com.onarandombox.MultiverseCore.api.MVWorldManager;
|
||||||
|
import com.onarandombox.MultiverseCore.enums.MVEnums;
|
||||||
import com.onarandombox.MultiverseCore.utils.WorldNameChecker;
|
import com.onarandombox.MultiverseCore.utils.WorldNameChecker;
|
||||||
import com.pneumaticraft.commandhandler.CommandHandler;
|
import com.pneumaticraft.commandhandler.CommandHandler;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
@ -20,6 +21,7 @@ import org.bukkit.permissions.PermissionDefault;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Imports a new world of the specified type.
|
* Imports a new world of the specified type.
|
||||||
@ -106,8 +108,8 @@ public class ImportCommand extends MultiverseCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String env = args.get(1);
|
String env = args.get(1);
|
||||||
Environment environment = EnvironmentCommand.getEnvFromString(env);
|
Optional<Environment> environment = MVEnums.ENVIRONMENT.parseValue(env);
|
||||||
if (environment == null) {
|
if (!environment.isPresent()) {
|
||||||
sender.sendMessage(ChatColor.RED + "That is not a valid environment.");
|
sender.sendMessage(ChatColor.RED + "That is not a valid environment.");
|
||||||
EnvironmentCommand.showEnvironments(sender);
|
EnvironmentCommand.showEnvironments(sender);
|
||||||
return;
|
return;
|
||||||
@ -128,10 +130,18 @@ public class ImportCommand extends MultiverseCommand {
|
|||||||
sender.sendMessage("For a list of available world types, type: " + ChatColor.AQUA + "/mvenv");
|
sender.sendMessage("For a list of available world types, type: " + ChatColor.AQUA + "/mvenv");
|
||||||
} else {
|
} else {
|
||||||
Command.broadcastCommandMessage(sender, String.format("Starting import of world '%s'...", worldName));
|
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!");
|
Command.broadcastCommandMessage(sender, ChatColor.GREEN + "Complete!");
|
||||||
else
|
}
|
||||||
|
else {
|
||||||
Command.broadcastCommandMessage(sender, ChatColor.RED + "Failed!");
|
Command.broadcastCommandMessage(sender, ChatColor.RED + "Failed!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
@ -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<World.Environment> 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<WorldType> WORLD_TYPE = EnumMapping.builder(WorldType.class)
|
||||||
|
.mapWithoutUnderscore()
|
||||||
|
.build();
|
||||||
|
}
|
@ -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<T extends Enum<T>> {
|
||||||
|
|
||||||
|
public static <T extends Enum<T>> Builder<T> builder(Class<T> enumClass) {
|
||||||
|
return builder(enumClass, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T extends Enum<T>> Builder<T> builder(Class<T> enumClass, boolean caseSensitive) {
|
||||||
|
return new Builder<>(enumClass, caseSensitive);
|
||||||
|
}
|
||||||
|
|
||||||
|
private final Class<T> enumClass;
|
||||||
|
private final boolean caseSensitive;
|
||||||
|
private final Map<String, T> enumsMap;
|
||||||
|
|
||||||
|
public EnumMapping(Class<T> enumClass, boolean caseSensitive) {
|
||||||
|
this.enumClass = enumClass;
|
||||||
|
this.caseSensitive = caseSensitive;
|
||||||
|
this.enumsMap = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public Optional<T> 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<String, T> getEnumsMap() {
|
||||||
|
return Collections.unmodifiableMap(this.enumsMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isCaseSensitive() {
|
||||||
|
return caseSensitive;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Class<? extends Enum<T>> getEnumClass() {
|
||||||
|
return enumClass;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Builder<T extends Enum<T>> {
|
||||||
|
|
||||||
|
private final EnumMapping<T> enumMapping;
|
||||||
|
|
||||||
|
public Builder(Class<T> 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<T> mapWithoutUnderscore() {
|
||||||
|
Arrays.stream(this.enumMapping.enumClass.getEnumConstants()).forEach(enumType -> {
|
||||||
|
addAlias(enumType.name().replace("_", ""), enumType);
|
||||||
|
});
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public Builder<T> addAlias(String aliasName, String enumString) {
|
||||||
|
this.enumMapping.parseValue(enumString).ifPresent(enumType -> addAlias(aliasName, enumType));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public Builder<T> addAlias(String aliasName, T enumType) {
|
||||||
|
if (!this.enumMapping.caseSensitive) {
|
||||||
|
aliasName = aliasName.toLowerCase();
|
||||||
|
}
|
||||||
|
this.enumMapping.enumsMap.computeIfAbsent(aliasName, name -> enumType);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public EnumMapping<T> build() {
|
||||||
|
return this.enumMapping;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user