mirror of
https://github.com/Multiverse/Multiverse-Core.git
synced 2024-11-22 10:36:06 +01:00
Merge branch 'MV5' into ben/mv5/teleporter
# Conflicts: # src/main/java/org/mvplugins/multiverse/core/commands/RemoveCommand.java
This commit is contained in:
commit
980d6784df
@ -71,7 +71,7 @@ class CreateCommand extends MultiverseCommand {
|
||||
|
||||
@Subcommand("create")
|
||||
@CommandPermission("multiverse.core.create")
|
||||
@CommandCompletion("@empty @flags:groupName=mvcreatecommand")
|
||||
@CommandCompletion("@empty @environments @flags:groupName=mvcreatecommand")
|
||||
@Syntax("<name> <environment> --seed [seed] --generator [generator[:id]] --world-type [worldtype] --adjust-spawn --no-structures")
|
||||
@Description("{@@mv-core.create.description}")
|
||||
void onCreateCommand(
|
||||
|
@ -54,7 +54,7 @@ class ImportCommand extends MultiverseCommand {
|
||||
|
||||
@Subcommand("import")
|
||||
@CommandPermission("multiverse.core.import")
|
||||
@CommandCompletion("@mvworlds:scope=potential @flags:groupName=mvimportcommand")
|
||||
@CommandCompletion("@mvworlds:scope=potential @environments @flags:groupName=mvimportcommand")
|
||||
@Syntax("<name> <env> --generator [generator[:id]] --adjust-spawn")
|
||||
@Description("{@@mv-core.import.description")
|
||||
void onImportCommand(
|
||||
|
@ -23,6 +23,7 @@ import org.mvplugins.multiverse.core.commandtools.flags.CommandFlag;
|
||||
import org.mvplugins.multiverse.core.commandtools.flags.ParsedCommandFlags;
|
||||
import org.mvplugins.multiverse.core.utils.MVCorei18n;
|
||||
import org.mvplugins.multiverse.core.utils.result.Async;
|
||||
import org.mvplugins.multiverse.core.world.MultiverseWorld;
|
||||
import org.mvplugins.multiverse.core.world.WorldManager;
|
||||
import org.mvplugins.multiverse.core.world.helpers.PlayerWorldTeleporter;
|
||||
|
||||
@ -59,7 +60,7 @@ class RemoveCommand extends MultiverseCommand {
|
||||
@Conditions("mvworlds:scope=both @flags:groupName=mvremovecommand")
|
||||
@Syntax("<world>")
|
||||
@Description("{@@mv-core.remove.world.description}")
|
||||
String worldName,
|
||||
MultiverseWorld world,
|
||||
|
||||
@Optional
|
||||
@Syntax("[--remove-players]")
|
||||
@ -68,19 +69,19 @@ class RemoveCommand extends MultiverseCommand {
|
||||
ParsedCommandFlags parsedFlags = parseFlags(flags);
|
||||
|
||||
var future = parsedFlags.hasFlag(REMOVE_PLAYERS_FLAG)
|
||||
? worldManager.getLoadedWorld(worldName)
|
||||
? worldManager.getLoadedWorld(world)
|
||||
.map(playerWorldTeleporter::removeFromWorld)
|
||||
.getOrElse(Async.completedFuture(Collections.emptyList()))
|
||||
: Async.completedFuture(Collections.emptyList());
|
||||
|
||||
future.thenRun(() -> doWorldRemoving(issuer, worldName));
|
||||
future.thenRun(() -> doWorldRemoving(issuer, world));
|
||||
}
|
||||
|
||||
private void doWorldRemoving(MVCommandIssuer issuer, String worldName) {
|
||||
worldManager.removeWorld(worldName)
|
||||
private void doWorldRemoving(MVCommandIssuer issuer, MultiverseWorld world) {
|
||||
worldManager.removeWorld(world)
|
||||
.onSuccess(removedWorldName -> {
|
||||
Logging.fine("World remove success: " + removedWorldName);
|
||||
issuer.sendInfo(MVCorei18n.REMOVEWORLD_REMOVED, "{world}", removedWorldName);
|
||||
issuer.sendInfo(MVCorei18n.REMOVE_SUCCESS, "{world}", removedWorldName);
|
||||
}).onFailure(failure -> {
|
||||
Logging.fine("World remove failure: " + failure);
|
||||
issuer.sendError(failure.getFailureMessage());
|
||||
|
@ -3,6 +3,7 @@ package org.mvplugins.multiverse.core.commandtools;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@ -18,7 +19,10 @@ import com.dumptruckman.minecraft.util.Logging;
|
||||
import com.google.common.collect.Sets;
|
||||
import io.vavr.control.Try;
|
||||
import jakarta.inject.Inject;
|
||||
import org.bukkit.Difficulty;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.GameRule;
|
||||
import org.bukkit.World;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jvnet.hk2.annotations.Service;
|
||||
|
||||
@ -30,19 +34,18 @@ import org.mvplugins.multiverse.core.world.MultiverseWorld;
|
||||
import org.mvplugins.multiverse.core.world.WorldManager;
|
||||
|
||||
@Service
|
||||
public class MVCommandCompletions extends PaperCommandCompletions {
|
||||
class MVCommandCompletions extends PaperCommandCompletions {
|
||||
|
||||
protected final MVCommandManager commandManager;
|
||||
private final MVCommandManager commandManager;
|
||||
private final WorldManager worldManager;
|
||||
private final DestinationsProvider destinationsProvider;
|
||||
|
||||
@Inject
|
||||
public MVCommandCompletions(
|
||||
MVCommandCompletions(
|
||||
@NotNull MVCommandManager mvCommandManager,
|
||||
@NotNull WorldManager worldManager,
|
||||
@NotNull DestinationsProvider destinationsProvider,
|
||||
@NotNull MVCoreConfig config
|
||||
) {
|
||||
@NotNull MVCoreConfig config) {
|
||||
super(mvCommandManager);
|
||||
this.commandManager = mvCommandManager;
|
||||
this.worldManager = worldManager;
|
||||
@ -50,13 +53,19 @@ public class MVCommandCompletions extends PaperCommandCompletions {
|
||||
|
||||
registerAsyncCompletion("commands", this::suggestCommands);
|
||||
registerAsyncCompletion("destinations", this::suggestDestinations);
|
||||
registerStaticCompletion("difficulties", suggestEnums(Difficulty.class));
|
||||
registerStaticCompletion("environments", suggestEnums(World.Environment.class));
|
||||
registerAsyncCompletion("flags", this::suggestFlags);
|
||||
registerStaticCompletion("gamemodes", suggestEnums(GameMode.class));
|
||||
registerStaticCompletion("gamerules", this::suggestGamerules);
|
||||
registerStaticCompletion("mvconfigs", config.getNodes().getNames());
|
||||
registerAsyncCompletion("mvworlds", this::suggestMVWorlds);
|
||||
|
||||
setDefaultCompletion("destinations", ParsedDestination.class);
|
||||
setDefaultCompletion("difficulties", Difficulty.class);
|
||||
setDefaultCompletion("environments", World.Environment.class);
|
||||
setDefaultCompletion("flags", String[].class);
|
||||
setDefaultCompletion("gamemodes", GameMode.class);
|
||||
setDefaultCompletion("gamerules", GameRule.class);
|
||||
setDefaultCompletion("mvworlds", LoadedMultiverseWorld.class);
|
||||
}
|
||||
@ -94,7 +103,7 @@ public class MVCommandCompletions extends PaperCommandCompletions {
|
||||
}
|
||||
|
||||
return this.destinationsProvider
|
||||
.suggestDestinations((BukkitCommandIssuer)context.getIssuer(), context.getInput());
|
||||
.suggestDestinations((BukkitCommandIssuer) context.getIssuer(), context.getInput());
|
||||
}
|
||||
|
||||
private Collection<String> suggestFlags(@NotNull BukkitCommandCompletionContext context) {
|
||||
@ -152,4 +161,11 @@ public class MVCommandCompletions extends PaperCommandCompletions {
|
||||
Logging.severe("Invalid MVWorld scope: " + scope);
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
private <T extends Enum<T>> Collection<String> suggestEnums(Class<T> enumClass) {
|
||||
return EnumSet.allOf(enumClass).stream()
|
||||
.map(Enum::name)
|
||||
.map(String::toLowerCase)
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
|
@ -27,10 +27,11 @@ import org.mvplugins.multiverse.core.display.filters.DefaultContentFilter;
|
||||
import org.mvplugins.multiverse.core.display.filters.RegexContentFilter;
|
||||
import org.mvplugins.multiverse.core.utils.PlayerFinder;
|
||||
import org.mvplugins.multiverse.core.world.LoadedMultiverseWorld;
|
||||
import org.mvplugins.multiverse.core.world.MultiverseWorld;
|
||||
import org.mvplugins.multiverse.core.world.WorldManager;
|
||||
|
||||
@Service
|
||||
public class MVCommandContexts extends PaperCommandContexts {
|
||||
class MVCommandContexts extends PaperCommandContexts {
|
||||
|
||||
private final MVCommandManager mvCommandManager;
|
||||
private final DestinationsProvider destinationsProvider;
|
||||
@ -38,12 +39,11 @@ public class MVCommandContexts extends PaperCommandContexts {
|
||||
private final MVCoreConfig config;
|
||||
|
||||
@Inject
|
||||
public MVCommandContexts(
|
||||
MVCommandContexts(
|
||||
MVCommandManager mvCommandManager,
|
||||
DestinationsProvider destinationsProvider,
|
||||
WorldManager worldManager,
|
||||
MVCoreConfig config
|
||||
) {
|
||||
MVCoreConfig config) {
|
||||
super(mvCommandManager);
|
||||
this.mvCommandManager = mvCommandManager;
|
||||
this.destinationsProvider = destinationsProvider;
|
||||
@ -56,9 +56,10 @@ public class MVCommandContexts extends PaperCommandContexts {
|
||||
registerContext(ParsedDestination.class, this::parseDestination);
|
||||
registerContext(GameRule.class, this::parseGameRule);
|
||||
registerContext(GameRuleValue.class, this::parseGameRuleValue);
|
||||
registerIssuerAwareContext(LoadedMultiverseWorld.class, this::parseLoadedMultiverseWorld);
|
||||
registerIssuerAwareContext(LoadedMultiverseWorld[].class, this::parseLoadedMultiverseWorldArray);
|
||||
registerIssuerAwareContext(MultiverseWorld.class, this::parseMultiverseWorld);
|
||||
registerContext(MVConfigValue.class, this::parseMVConfigValue);
|
||||
registerIssuerAwareContext(LoadedMultiverseWorld.class, this::parseMVWorld);
|
||||
registerIssuerAwareContext(LoadedMultiverseWorld[].class, this::parseMVWorldArray);
|
||||
registerIssuerAwareContext(Player.class, this::parsePlayer);
|
||||
registerIssuerAwareContext(Player[].class, this::parsePlayerArray);
|
||||
}
|
||||
@ -129,40 +130,7 @@ public class MVCommandContexts extends PaperCommandContexts {
|
||||
return new GameRuleValue(resolvedValue);
|
||||
}
|
||||
|
||||
private MVConfigValue parseMVConfigValue(BukkitCommandExecutionContext context) {
|
||||
String configName = (String) context.getResolvedArg(String.class);
|
||||
if (Strings.isNullOrEmpty(configName)) {
|
||||
throw new InvalidCommandArgument("No config name specified.");
|
||||
}
|
||||
Option<Node> node = config.getNodes().findNode(configName);
|
||||
if (node.isEmpty()) {
|
||||
throw new InvalidCommandArgument("The config " + configName + " is not valid.");
|
||||
}
|
||||
|
||||
String valueString = context.getFirstArg();
|
||||
if (Strings.isNullOrEmpty(valueString)) {
|
||||
throw new InvalidCommandArgument("No config value specified.");
|
||||
}
|
||||
|
||||
if (!(node.get() instanceof ValueNode)) {
|
||||
context.popFirstArg();
|
||||
return new MVConfigValue(valueString);
|
||||
}
|
||||
|
||||
ContextResolver<?, BukkitCommandExecutionContext> resolver = getResolver(((ValueNode<?>) node.get()).getType());
|
||||
if (resolver == null) {
|
||||
context.popFirstArg();
|
||||
return new MVConfigValue(valueString);
|
||||
}
|
||||
|
||||
Object resolvedValue = resolver.getContext(context);
|
||||
if (resolvedValue == null) {
|
||||
throw new InvalidCommandArgument("The config value " + valueString + " is not valid for config " + configName + ".");
|
||||
}
|
||||
return new MVConfigValue(resolvedValue);
|
||||
}
|
||||
|
||||
private LoadedMultiverseWorld parseMVWorld(BukkitCommandExecutionContext context) {
|
||||
private LoadedMultiverseWorld parseLoadedMultiverseWorld(BukkitCommandExecutionContext context) {
|
||||
String resolve = context.getFlagValue("resolve", "");
|
||||
|
||||
// Get world based on sender only
|
||||
@ -205,7 +173,7 @@ public class MVCommandContexts extends PaperCommandContexts {
|
||||
throw new InvalidCommandArgument("World " + worldName + " is not a loaded multiverse world.");
|
||||
}
|
||||
|
||||
private LoadedMultiverseWorld[] parseMVWorldArray(BukkitCommandExecutionContext context) {
|
||||
private LoadedMultiverseWorld[] parseLoadedMultiverseWorldArray(BukkitCommandExecutionContext context) {
|
||||
String resolve = context.getFlagValue("resolve", "");
|
||||
|
||||
LoadedMultiverseWorld playerWorld = null;
|
||||
@ -265,6 +233,82 @@ public class MVCommandContexts extends PaperCommandContexts {
|
||||
throw new InvalidCommandArgument("World " + worldStrings + " is not a loaded multiverse world.");
|
||||
}
|
||||
|
||||
private MultiverseWorld parseMultiverseWorld(BukkitCommandExecutionContext context) {
|
||||
String resolve = context.getFlagValue("resolve", "");
|
||||
|
||||
// Get world based on sender only
|
||||
if (resolve.equals("issuerOnly")) {
|
||||
if (context.getIssuer().isPlayer()) {
|
||||
return worldManager.getWorld(context.getIssuer().getPlayer().getWorld()).getOrNull();
|
||||
}
|
||||
if (context.isOptional()) {
|
||||
return null;
|
||||
}
|
||||
throw new InvalidCommandArgument("This command can only be used by a player in a Multiverse World.");
|
||||
}
|
||||
|
||||
String worldName = context.getFirstArg();
|
||||
MultiverseWorld world = worldManager.getWorld(worldName).getOrNull();
|
||||
|
||||
// Get world based on input, fallback to sender if input is not a world
|
||||
if (resolve.equals("issuerAware")) {
|
||||
if (world != null) {
|
||||
context.popFirstArg();
|
||||
return world;
|
||||
}
|
||||
if (context.getIssuer().isPlayer()) {
|
||||
return worldManager.getLoadedWorld(context.getIssuer().getPlayer().getWorld()).getOrNull();
|
||||
}
|
||||
if (context.isOptional()) {
|
||||
return null;
|
||||
}
|
||||
throw new InvalidCommandArgument("Player is not in a Multiverse World.");
|
||||
}
|
||||
|
||||
// Get world based on input only
|
||||
if (world != null) {
|
||||
context.popFirstArg();
|
||||
return world;
|
||||
}
|
||||
if (context.isOptional()) {
|
||||
return null;
|
||||
}
|
||||
throw new InvalidCommandArgument("World " + worldName + " is not a loaded multiverse world.");
|
||||
}
|
||||
|
||||
private MVConfigValue parseMVConfigValue(BukkitCommandExecutionContext context) {
|
||||
String configName = (String) context.getResolvedArg(String.class);
|
||||
if (Strings.isNullOrEmpty(configName)) {
|
||||
throw new InvalidCommandArgument("No config name specified.");
|
||||
}
|
||||
Option<Node> node = config.getNodes().findNode(configName);
|
||||
if (node.isEmpty()) {
|
||||
throw new InvalidCommandArgument("The config " + configName + " is not valid.");
|
||||
}
|
||||
|
||||
String valueString = context.getFirstArg();
|
||||
if (Strings.isNullOrEmpty(valueString)) {
|
||||
throw new InvalidCommandArgument("No config value specified.");
|
||||
}
|
||||
|
||||
if (!(node.get() instanceof ValueNode)) {
|
||||
context.popFirstArg();
|
||||
return new MVConfigValue(valueString);
|
||||
}
|
||||
|
||||
ContextResolver<?, BukkitCommandExecutionContext> resolver = getResolver(((ValueNode<?>) node.get()).getType());
|
||||
if (resolver == null) {
|
||||
context.popFirstArg();
|
||||
return new MVConfigValue(valueString);
|
||||
}
|
||||
|
||||
Object resolvedValue = resolver.getContext(context);
|
||||
if (resolvedValue == null) {
|
||||
throw new InvalidCommandArgument("The config value " + valueString + " is not valid for config " + configName + ".");
|
||||
}
|
||||
return new MVConfigValue(resolvedValue);
|
||||
}
|
||||
|
||||
private Player parsePlayer(BukkitCommandExecutionContext context) {
|
||||
String resolve = context.getFlagValue("resolve", "");
|
||||
|
||||
|
@ -11,15 +11,26 @@ import org.mvplugins.multiverse.core.commandtools.flags.CommandFlagGroup;
|
||||
import org.mvplugins.multiverse.core.commandtools.flags.CommandFlagsManager;
|
||||
import org.mvplugins.multiverse.core.commandtools.flags.ParsedCommandFlags;
|
||||
|
||||
/**
|
||||
* Base class for all Multiverse commands.
|
||||
*/
|
||||
@Contract
|
||||
public abstract class MultiverseCommand extends BaseCommand {
|
||||
public abstract class MultiverseCommand extends BaseCommand {
|
||||
|
||||
/**
|
||||
* The command manager with multiverse specific methods.
|
||||
*/
|
||||
protected final MVCommandManager commandManager;
|
||||
/**
|
||||
* The flags manager for the above command manager.
|
||||
*/
|
||||
protected final CommandFlagsManager flagsManager;
|
||||
private String flagGroupName;
|
||||
private CommandFlagGroup.Builder flagGroupBuilder;
|
||||
|
||||
protected MultiverseCommand(@NotNull MVCommandManager commandManager) {
|
||||
this.commandManager = commandManager;
|
||||
this.flagsManager = commandManager.getFlagsManager();
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
@ -30,10 +41,22 @@ public abstract class MultiverseCommand extends BaseCommand {
|
||||
}
|
||||
}
|
||||
|
||||
protected CommandFlagsManager getFlagsManager() {
|
||||
return commandManager.getFlagsManager();
|
||||
private void registerFlagGroup(@NotNull CommandFlagGroup flagGroup) {
|
||||
if (flagGroupName != null) {
|
||||
throw new IllegalStateException("Flag group already registered! (name: " + flagGroupName + ")");
|
||||
}
|
||||
flagsManager.registerFlagGroup(flagGroup);
|
||||
flagGroupName = flagGroup.getName();
|
||||
Logging.finest("Registered flag group: " + flagGroupName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new flag to the flag builder.
|
||||
*
|
||||
* @param flag The flag to add.
|
||||
* @param <T> The type of the flag.
|
||||
* @return The flag.
|
||||
*/
|
||||
protected <T extends CommandFlag> T flag(T flag) {
|
||||
if (flagGroupBuilder == null) {
|
||||
flagGroupBuilder = CommandFlagGroup.builder("mv" + getClass().getSimpleName().toLowerCase());
|
||||
@ -43,16 +66,13 @@ public abstract class MultiverseCommand extends BaseCommand {
|
||||
return flag;
|
||||
}
|
||||
|
||||
protected void registerFlagGroup(@NotNull CommandFlagGroup flagGroup) {
|
||||
if (flagGroupName != null) {
|
||||
throw new IllegalStateException("Flag group already registered! (name: " + flagGroupName + ")");
|
||||
}
|
||||
getFlagsManager().registerFlagGroup(flagGroup);
|
||||
flagGroupName = flagGroup.getName();
|
||||
Logging.fine("Registered flag group: " + flagGroupName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses flags.
|
||||
*
|
||||
* @param flags The raw string array to parse into flags.
|
||||
* @return The parsed flags.
|
||||
*/
|
||||
protected @NotNull ParsedCommandFlags parseFlags(@NotNull String[] flags) {
|
||||
return getFlagsManager().parse(flagGroupName, flags);
|
||||
return flagsManager.parse(flagGroupName, flags);
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package org.mvplugins.multiverse.core.configuration.node;
|
||||
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import io.vavr.control.Option;
|
||||
import io.vavr.control.Try;
|
||||
@ -10,6 +11,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* A node that contains a value.
|
||||
*
|
||||
* @param <T> The type of the value.
|
||||
*/
|
||||
public class ConfigNode<T> extends ConfigHeaderNode implements ValueNode<T> {
|
||||
@ -19,19 +21,18 @@ public class ConfigNode<T> extends ConfigHeaderNode implements ValueNode<T> {
|
||||
*
|
||||
* @param path The path of the node.
|
||||
* @param type The type of the value.
|
||||
* @return The new builder.
|
||||
* @param <T> The type of the value.
|
||||
* @return The new builder.
|
||||
*/
|
||||
public static @NotNull <T> ConfigNode.Builder<T, ? extends ConfigNode.Builder<T, ?>> builder(
|
||||
@NotNull String path,
|
||||
@NotNull Class<T> type
|
||||
) {
|
||||
@NotNull Class<T> type) {
|
||||
return new ConfigNode.Builder<>(path, type);
|
||||
}
|
||||
|
||||
protected final @Nullable String name;
|
||||
protected final @NotNull Class<T> type;
|
||||
protected final @Nullable T defaultValue;
|
||||
protected final @Nullable Supplier<T> defaultValue;
|
||||
protected final @Nullable NodeSerializer<T> serializer;
|
||||
protected final @Nullable Function<T, Try<Void>> validator;
|
||||
protected final @Nullable BiConsumer<T, T> onSetValue;
|
||||
@ -41,11 +42,10 @@ public class ConfigNode<T> extends ConfigHeaderNode implements ValueNode<T> {
|
||||
@NotNull String[] comments,
|
||||
@Nullable String name,
|
||||
@NotNull Class<T> type,
|
||||
@Nullable T defaultValue,
|
||||
@Nullable Supplier<T> defaultValue,
|
||||
@Nullable NodeSerializer<T> serializer,
|
||||
@Nullable Function<T, Try<Void>> validator,
|
||||
@Nullable BiConsumer<T, T> onSetValue
|
||||
) {
|
||||
@Nullable BiConsumer<T, T> onSetValue) {
|
||||
super(path, comments);
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
@ -76,7 +76,10 @@ public class ConfigNode<T> extends ConfigHeaderNode implements ValueNode<T> {
|
||||
*/
|
||||
@Override
|
||||
public @Nullable T getDefaultValue() {
|
||||
return defaultValue;
|
||||
if (defaultValue != null) {
|
||||
return defaultValue.get();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public @Nullable NodeSerializer<T> getSerializer() {
|
||||
@ -115,7 +118,7 @@ public class ConfigNode<T> extends ConfigHeaderNode implements ValueNode<T> {
|
||||
|
||||
protected @Nullable String name;
|
||||
protected @NotNull final Class<T> type;
|
||||
protected @Nullable T defaultValue;
|
||||
protected @Nullable Supplier<T> defaultValue;
|
||||
protected @Nullable NodeSerializer<T> serializer;
|
||||
protected @Nullable Function<T, Try<Void>> validator;
|
||||
protected @Nullable BiConsumer<T, T> onSetValue;
|
||||
@ -142,8 +145,19 @@ public class ConfigNode<T> extends ConfigHeaderNode implements ValueNode<T> {
|
||||
* @return This builder.
|
||||
*/
|
||||
public @NotNull B defaultValue(@NotNull T defaultValue) {
|
||||
this.defaultValue = () -> defaultValue;
|
||||
return self();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the default value for this node.
|
||||
*
|
||||
* @param defaultValue The default value supplier.
|
||||
* @return This builder.
|
||||
*/
|
||||
public @NotNull B defaultValue(@NotNull Supplier<T> defaultValue) {
|
||||
this.defaultValue = defaultValue;
|
||||
return (B) this;
|
||||
return self();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -154,17 +168,29 @@ public class ConfigNode<T> extends ConfigHeaderNode implements ValueNode<T> {
|
||||
*/
|
||||
public @NotNull B name(@Nullable String name) {
|
||||
this.name = name;
|
||||
return (B) this;
|
||||
return self();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the serializer for this node.
|
||||
*
|
||||
* @param serializer The serializer for this node.
|
||||
* @return This builder.
|
||||
*/
|
||||
public @NotNull B serializer(@NotNull NodeSerializer<T> serializer) {
|
||||
this.serializer = serializer;
|
||||
return (B) this;
|
||||
return self();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the validator for this node.
|
||||
*
|
||||
* @param validator The validator for this node.
|
||||
* @return This builder.
|
||||
*/
|
||||
public @NotNull B validator(@NotNull Function<T, Try<Void>> validator) {
|
||||
this.validator = validator;
|
||||
return (B) this;
|
||||
return self();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -175,7 +201,7 @@ public class ConfigNode<T> extends ConfigHeaderNode implements ValueNode<T> {
|
||||
*/
|
||||
public @NotNull B onSetValue(@NotNull BiConsumer<T, T> onSetValue) {
|
||||
this.onSetValue = onSetValue;
|
||||
return (B) this;
|
||||
return self();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -183,7 +209,13 @@ public class ConfigNode<T> extends ConfigHeaderNode implements ValueNode<T> {
|
||||
*/
|
||||
@Override
|
||||
public @NotNull ConfigNode<T> build() {
|
||||
return new ConfigNode<>(path, comments.toArray(new String[0]), name, type, defaultValue, serializer, validator, onSetValue);
|
||||
return new ConfigNode<>(path, comments.toArray(new String[0]),
|
||||
name, type, defaultValue,serializer, validator, onSetValue);
|
||||
}
|
||||
|
||||
protected @NotNull B self() {
|
||||
//noinspection unchecked
|
||||
return (B) this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ import java.util.Map;
|
||||
import io.github.townyadvanced.commentedconfiguration.setting.CommentedNode;
|
||||
import io.vavr.control.Option;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* A collection of {@link CommentedNode}s, with mappings to nodes by name.
|
||||
@ -17,24 +18,32 @@ public class NodeGroup implements Collection<Node> {
|
||||
private final Collection<Node> nodes;
|
||||
private final Map<String, Node> nodesMap;
|
||||
|
||||
/**
|
||||
* Creates a new empty node group.
|
||||
*/
|
||||
public NodeGroup() {
|
||||
this.nodes = new ArrayList<>();
|
||||
this.nodesMap = new HashMap<>();
|
||||
}
|
||||
|
||||
public NodeGroup(Collection<Node> nodes) {
|
||||
/**
|
||||
* Creates a new node group with the given nodes.
|
||||
*
|
||||
* @param nodes The nodes to make up the group.
|
||||
*/
|
||||
public NodeGroup(@NotNull Collection<Node> nodes) {
|
||||
this.nodes = nodes;
|
||||
this.nodesMap = new HashMap<>(nodes.size());
|
||||
nodes.forEach(this::addNodeIndex);
|
||||
}
|
||||
|
||||
private void addNodeIndex(Node node) {
|
||||
private void addNodeIndex(@NotNull Node node) {
|
||||
if (node instanceof ValueNode) {
|
||||
((ValueNode<?>) node).getName().peek(name -> nodesMap.put(name, node));
|
||||
}
|
||||
}
|
||||
|
||||
private void removeNodeIndex(Node node) {
|
||||
private void removeNodeIndex(@NotNull Node node) {
|
||||
if (node instanceof ValueNode) {
|
||||
((ValueNode<?>) node).getName().peek(nodesMap::remove);
|
||||
}
|
||||
@ -45,32 +54,29 @@ public class NodeGroup implements Collection<Node> {
|
||||
*
|
||||
* @return The names of all nodes in this group.
|
||||
*/
|
||||
public Collection<String> getNames() {
|
||||
public @NotNull Collection<String> getNames() {
|
||||
return nodesMap.keySet();
|
||||
}
|
||||
|
||||
public Map<String, Node> getNodesMap() {
|
||||
return nodesMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the node with the given name.
|
||||
*
|
||||
* @param name The name of the node to get.
|
||||
* @return The node with the given name, or {@link Option.None} if no node with the given name exists.
|
||||
*/
|
||||
public Option<Node> findNode(String name) {
|
||||
public @NotNull Option<Node> findNode(@Nullable String name) {
|
||||
return Option.of(nodesMap.get(name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the node with the given name.
|
||||
*
|
||||
* @param name The name of the node to get.
|
||||
* @param type The type of the node to get.
|
||||
* @param name The name of node to get.
|
||||
* @param type The type of node to get.
|
||||
* @param <T> The type of node.
|
||||
* @return The node with the given name, or {@link Option.None} if no node with the given name exists.
|
||||
*/
|
||||
public <T extends Node> Option<T> findNode(String name, Class<T> type) {
|
||||
public <T extends Node> @NotNull Option<T> findNode(@Nullable String name, @NotNull Class<T> type) {
|
||||
return Option.of(nodesMap.get(name)).map(node -> type.isAssignableFrom(node.getClass()) ? (T) node : null);
|
||||
}
|
||||
|
||||
|
@ -43,6 +43,7 @@ public enum MVCorei18n implements MessageKeyProvider {
|
||||
// delete command
|
||||
DELETE_DELETING,
|
||||
DELETE_PROMPT,
|
||||
DELETE_SUCCESS,
|
||||
|
||||
// Dumps command
|
||||
DUMPS_DESCRIPTION,
|
||||
@ -76,6 +77,9 @@ public enum MVCorei18n implements MessageKeyProvider {
|
||||
RELOAD_RELOADING,
|
||||
RELOAD_SUCCESS,
|
||||
|
||||
// remove command
|
||||
REMOVE_SUCCESS,
|
||||
|
||||
// root MV command
|
||||
ROOT_TITLE,
|
||||
ROOT_HELP,
|
||||
@ -115,7 +119,6 @@ public enum MVCorei18n implements MessageKeyProvider {
|
||||
CREATEWORLD_WORLDEXISTLOADED,
|
||||
CREATEWORLD_BUKKITCREATIONFAILED,
|
||||
|
||||
DELETE_SUCCESS,
|
||||
DELETEWORLD_WORLDNONEXISTENT,
|
||||
DELETEWORLD_LOADFAILED,
|
||||
DELETEWORLD_WORLDFOLDERNOTFOUND,
|
||||
@ -133,7 +136,6 @@ public enum MVCorei18n implements MessageKeyProvider {
|
||||
LOADWORLD_WORLDEXISTLOADED,
|
||||
LOADWORLD_BUKKITCREATIONFAILED,
|
||||
|
||||
REMOVEWORLD_REMOVED,
|
||||
REMOVEWORLD_WORLDNONEXISTENT,
|
||||
|
||||
UNLOADWORLD_WORLDALREADYUNLOADING,
|
||||
|
@ -726,6 +726,17 @@ public class WorldManager {
|
||||
return !isLoadedWorld(worldName) && isWorld(worldName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a world that may or may not be loaded. It will an {@link LoadedMultiverseWorld} if the world is loaded,
|
||||
* otherwise returns an {@link MultiverseWorld} instance.
|
||||
*
|
||||
* @param world The bukkit world to get.
|
||||
* @return The world if it exists.
|
||||
*/
|
||||
public Option<MultiverseWorld> getWorld(@Nullable World world) {
|
||||
return Option.of(world).flatMap(this::getWorld);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a world that may or may not be loaded. It will an {@link LoadedMultiverseWorld} if the world is loaded,
|
||||
* otherwise returns an {@link MultiverseWorld} instance.
|
||||
|
@ -14,6 +14,7 @@ import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import org.mvplugins.multiverse.core.MultiverseCore;
|
||||
import org.mvplugins.multiverse.core.configuration.handle.ConfigurationSectionHandle;
|
||||
import org.mvplugins.multiverse.core.configuration.migration.BooleanMigratorAction;
|
||||
import org.mvplugins.multiverse.core.configuration.migration.ConfigMigrator;
|
||||
@ -23,7 +24,6 @@ import org.mvplugins.multiverse.core.configuration.migration.MoveMigratorAction;
|
||||
import org.mvplugins.multiverse.core.configuration.migration.NullStringMigratorAction;
|
||||
import org.mvplugins.multiverse.core.configuration.migration.VersionMigrator;
|
||||
import org.mvplugins.multiverse.core.world.LoadedMultiverseWorld;
|
||||
import org.mvplugins.multiverse.core.world.helpers.EnforcementHandler;
|
||||
|
||||
/**
|
||||
* Represents a world configuration.
|
||||
@ -37,9 +37,9 @@ public final class WorldConfig {
|
||||
WorldConfig(
|
||||
@NotNull String worldName,
|
||||
@NotNull ConfigurationSection configSection,
|
||||
@NotNull EnforcementHandler enforcementHandler) {
|
||||
@NotNull MultiverseCore multiverseCore) {
|
||||
this.worldName = worldName;
|
||||
this.configNodes = new WorldConfigNodes(enforcementHandler);
|
||||
this.configNodes = new WorldConfigNodes(multiverseCore);
|
||||
this.configHandle = ConfigurationSectionHandle.builder(configSection)
|
||||
.logger(Logging.getLogger())
|
||||
.nodes(configNodes.getNodes())
|
||||
|
@ -10,10 +10,10 @@ import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import org.mvplugins.multiverse.core.MultiverseCore;
|
||||
import org.mvplugins.multiverse.core.configuration.node.ConfigNode;
|
||||
import org.mvplugins.multiverse.core.configuration.node.Node;
|
||||
import org.mvplugins.multiverse.core.configuration.node.NodeGroup;
|
||||
import org.mvplugins.multiverse.core.world.config.AllowedPortalType;
|
||||
import org.mvplugins.multiverse.core.world.LoadedMultiverseWorld;
|
||||
import org.mvplugins.multiverse.core.world.helpers.EnforcementHandler;
|
||||
|
||||
@ -27,8 +27,8 @@ public class WorldConfigNodes {
|
||||
private EnforcementHandler enforcementHandler;
|
||||
private LoadedMultiverseWorld world = null;
|
||||
|
||||
WorldConfigNodes(@NotNull EnforcementHandler enforcementHandler) {
|
||||
this.enforcementHandler = enforcementHandler;
|
||||
WorldConfigNodes(@NotNull MultiverseCore multiverseCore) {
|
||||
this.enforcementHandler = multiverseCore.getService(EnforcementHandler.class);
|
||||
}
|
||||
|
||||
LoadedMultiverseWorld getWorld() {
|
||||
|
@ -21,7 +21,6 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jvnet.hk2.annotations.Service;
|
||||
|
||||
import org.mvplugins.multiverse.core.MultiverseCore;
|
||||
import org.mvplugins.multiverse.core.world.helpers.EnforcementHandler;
|
||||
|
||||
/**
|
||||
* Manages the worlds.yml file.
|
||||
@ -35,14 +34,14 @@ public final class WorldsConfigManager {
|
||||
private final File worldConfigFile;
|
||||
private YamlConfiguration worldsConfig;
|
||||
|
||||
private final EnforcementHandler enforcementHandler;
|
||||
private final MultiverseCore multiverseCore;
|
||||
|
||||
@Inject
|
||||
WorldsConfigManager(@NotNull MultiverseCore core, @NotNull EnforcementHandler enforcementHandler) {
|
||||
WorldsConfigManager(@NotNull MultiverseCore core, @NotNull MultiverseCore multiverseCore) {
|
||||
worldConfigMap = new HashMap<>();
|
||||
worldConfigFile = core.getDataFolder().toPath().resolve(CONFIG_FILENAME).toFile();
|
||||
|
||||
this.enforcementHandler = enforcementHandler;
|
||||
this.multiverseCore = multiverseCore;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -128,7 +127,7 @@ public final class WorldsConfigManager {
|
||||
WorldConfig newWorldConfig = new WorldConfig(
|
||||
worldName,
|
||||
getWorldConfigSection(worldName),
|
||||
enforcementHandler);
|
||||
multiverseCore);
|
||||
worldConfigMap.put(worldName, newWorldConfig);
|
||||
newWorldsAdded.add(newWorldConfig);
|
||||
});
|
||||
@ -183,7 +182,7 @@ public final class WorldsConfigManager {
|
||||
if (worldConfigMap.containsKey(worldName)) {
|
||||
throw new IllegalArgumentException("WorldConfig for world " + worldName + " already exists.");
|
||||
}
|
||||
WorldConfig worldConfig = new WorldConfig(worldName, getWorldConfigSection(worldName), enforcementHandler);
|
||||
WorldConfig worldConfig = new WorldConfig(worldName, getWorldConfigSection(worldName), multiverseCore);
|
||||
worldConfigMap.put(worldName, worldConfig);
|
||||
return worldConfig;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user