Skip to content

Commit

Permalink
Merge pull request #3033 from Multiverse/ben/mv5/list-value-node
Browse files Browse the repository at this point in the history
Implement ListValueNode and modify actions
  • Loading branch information
benwoo1110 committed Sep 25, 2023
2 parents 454bcd3 + c78dbcd commit 0c37c9e
Show file tree
Hide file tree
Showing 17 changed files with 755 additions and 95 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.mvplugins.multiverse.core.commands;

import co.aikar.commands.annotation.CommandAlias;
import co.aikar.commands.annotation.CommandCompletion;
import co.aikar.commands.annotation.CommandPermission;
import co.aikar.commands.annotation.Description;
import co.aikar.commands.annotation.Flags;
import co.aikar.commands.annotation.Optional;
import co.aikar.commands.annotation.Subcommand;
import co.aikar.commands.annotation.Syntax;
import com.dumptruckman.minecraft.util.Logging;
import jakarta.inject.Inject;
import org.jetbrains.annotations.NotNull;
import org.jvnet.hk2.annotations.Service;

import org.mvplugins.multiverse.core.commandtools.MVCommandIssuer;
import org.mvplugins.multiverse.core.commandtools.MVCommandManager;
import org.mvplugins.multiverse.core.commandtools.MultiverseCommand;
import org.mvplugins.multiverse.core.configuration.handle.PropertyModifyAction;
import org.mvplugins.multiverse.core.configuration.handle.StringPropertyHandle;
import org.mvplugins.multiverse.core.world.MultiverseWorld;
import org.mvplugins.multiverse.core.world.WorldManager;

@Service
@CommandAlias("mv")
class ModifyCommand extends MultiverseCommand {

private final WorldManager worldManager;

@Inject
ModifyCommand(@NotNull MVCommandManager commandManager, WorldManager worldManager) {
super(commandManager);
this.worldManager = worldManager;
}

@Subcommand("modify")
@CommandPermission("multiverse.core.modify")
@CommandCompletion("@mvworlds:scope=both @propsmodifyaction @mvworldpropsname @mvworldpropsvalue")
@Syntax("[world] <set|add|remove|reset> <property> <value>")
@Description("")
void onModifyCommand(
MVCommandIssuer issuer,

@Flags("resolve=issuerAware")
@Syntax("[world]")
@Description("")
MultiverseWorld world,

@Syntax("<set|add|remove|reset>")
@Description("")
PropertyModifyAction action,

@Syntax("<property>")
@Description("")
String propertyName,

@Optional
@Syntax("[value]")
@Description("")
String propertyValue) {
StringPropertyHandle worldPropertyHandle = world.getStringPropertyHandle();
worldPropertyHandle.modifyProperty(propertyName, propertyValue, action).onSuccess(ignore -> {
issuer.sendMessage("Property %s set to %s for world %s.".formatted(
propertyName,
worldPropertyHandle.getProperty(propertyName).getOrNull(),
world.getName()));
worldManager.saveWorldsConfig();
}).onFailure(exception -> {
issuer.sendMessage("Failed to %s property %s to %s for world %s.".formatted(
action.name().toLowerCase(),
propertyName,
propertyValue,
world.getName()));
issuer.sendMessage(exception.getMessage());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.jvnet.hk2.annotations.Service;

import org.mvplugins.multiverse.core.config.MVCoreConfig;
import org.mvplugins.multiverse.core.configuration.handle.PropertyModifyAction;
import org.mvplugins.multiverse.core.destination.DestinationsProvider;
import org.mvplugins.multiverse.core.destination.ParsedDestination;
import org.mvplugins.multiverse.core.world.LoadedMultiverseWorld;
Expand Down Expand Up @@ -60,9 +61,12 @@ class MVCommandCompletions extends PaperCommandCompletions {
registerAsyncCompletion("flags", this::suggestFlags);
registerStaticCompletion("gamemodes", suggestEnums(GameMode.class));
registerStaticCompletion("gamerules", this::suggestGamerules);
registerStaticCompletion("mvconfigs", config.getStringPropertyHandle().getPropertyNames());
registerStaticCompletion("mvconfigs", config.getStringPropertyHandle().getAllPropertyNames());
registerAsyncCompletion("mvconfigvalues", this::suggestMVConfigValues);
registerAsyncCompletion("mvworlds", this::suggestMVWorlds);
registerAsyncCompletion("mvworldpropsname", this::suggestMVWorldPropsName);
registerAsyncCompletion("mvworldpropsvalue", this::suggestMVWorldPropsValue);
registerStaticCompletion("propsmodifyaction", suggestEnums(PropertyModifyAction.class));

setDefaultCompletion("destinations", ParsedDestination.class);
setDefaultCompletion("difficulties", Difficulty.class);
Expand Down Expand Up @@ -124,7 +128,7 @@ private Collection<String> suggestGamerules() {
private Collection<String> suggestMVConfigValues(BukkitCommandCompletionContext context) {
return Try.of(() -> context.getContextValue(String.class))
.map(propertyName -> config.getStringPropertyHandle()
.getPropertySuggestedValues(propertyName, context.getInput()))
.getSuggestedPropertyValue(propertyName, context.getInput(), PropertyModifyAction.SET))
.getOrElse(Collections.emptyList());
}

Expand Down Expand Up @@ -172,6 +176,23 @@ private List<String> getMVWorldNames(BukkitCommandCompletionContext context) {
return Collections.emptyList();
}

private Collection<String> suggestMVWorldPropsName(BukkitCommandCompletionContext context) {
return Try.of(() -> {
MultiverseWorld world = context.getContextValue(MultiverseWorld.class);
PropertyModifyAction action = context.getContextValue(PropertyModifyAction.class);
return world.getStringPropertyHandle().getModifiablePropertyNames(action);
}).getOrElse(Collections.emptyList());
}

private Collection<String> suggestMVWorldPropsValue(BukkitCommandCompletionContext context) {
return Try.of(() -> {
MultiverseWorld world = context.getContextValue(MultiverseWorld.class);
PropertyModifyAction action = context.getContextValue(PropertyModifyAction.class);
String propertyName = context.getContextValue(String.class);
return world.getStringPropertyHandle().getSuggestedPropertyValue(propertyName, context.getInput(), action);
}).getOrElse(Collections.emptyList());
}

private <T extends Enum<T>> Collection<String> suggestEnums(Class<T> enumClass) {
return EnumSet.allOf(enumClass).stream()
.map(Enum::name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ private MultiverseWorld parseMultiverseWorld(BukkitCommandExecutionContext conte
return world;
}
if (context.getIssuer().isPlayer()) {
return worldManager.getLoadedWorld(context.getIssuer().getPlayer().getWorld()).getOrNull();
return worldManager.getWorld(context.getIssuer().getPlayer().getWorld()).getOrNull();
}
if (context.isOptional()) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected void setUpNodes() {
valueNode.getDefaultValue())).onFailure(e -> {
Logging.warning("Failed to set node " + valueNode.getPath()
+ " to " + valueNode.getDefaultValue());
setDefault(valueNode);
reset(valueNode);
});
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.mvplugins.multiverse.core.configuration.handle;

import java.util.List;
import java.util.logging.Logger;

import io.vavr.control.Try;
Expand All @@ -9,6 +10,7 @@
import org.jetbrains.annotations.Nullable;

import org.mvplugins.multiverse.core.configuration.migration.ConfigMigrator;
import org.mvplugins.multiverse.core.configuration.node.ListValueNode;
import org.mvplugins.multiverse.core.configuration.node.NodeGroup;
import org.mvplugins.multiverse.core.configuration.node.ValueNode;

Expand Down Expand Up @@ -105,22 +107,73 @@ public <T> Try<Void> set(@NotNull ValueNode<T> node, T value) {
}

/**
* Gets the configuration. Mainly used for {@link StringPropertyHandle}.
* Adds an item to a list node.
*
* @return The configuration.
* @param node The list node to add the item to.
* @param itemValue The value of the item to add.
* @param <I> The type of the list item.
* @return Empty try if the item was added, try containing an error otherwise.
*/
@NotNull NodeGroup getNodes() {
return nodes;
public <I> Try<Void> add(@NotNull ListValueNode<I> node, I itemValue) {
return node.validateItem(itemValue).map(ignore -> {
var serialized = node.getItemSerializer() != null
? node.getItemSerializer().serialize(itemValue, node.getItemType())
: itemValue;
List valueList = config.getList(node.getPath());
if (valueList == null) {
throw new IllegalArgumentException("Cannot add item to non-list node");
}
valueList.add(serialized);
config.set(node.getPath(), valueList);
node.onSetItemValue(null, itemValue);
return null;
});
}

/**
* Removes an item from a list node.
*
* @param node The list node to remove the item from.
* @param itemValue The value of the item to remove.
* @param <I> The type of the list item.
* @return Empty try if the item was removed, try containing an error otherwise.
*/
public <I> Try<Void> remove(@NotNull ListValueNode<I> node, I itemValue) {
return node.validateItem(itemValue).map(ignore -> {
var serialized = node.getItemSerializer() != null
? node.getItemSerializer().serialize(itemValue, node.getItemType())
: itemValue;
List valueList = config.getList(node.getPath());
if (valueList == null) {
throw new IllegalArgumentException("Cannot remove item from non-list node");
}
if (!valueList.remove(serialized)) {
throw new IllegalArgumentException("Cannot remove item from list node");
}
config.set(node.getPath(), valueList);
node.onSetItemValue(itemValue, null);
return null;
});
}

/**
* Sets the default value of a node.
*
* @param node The node to set the default value of.
* @param <T> The type of the node value.
* @return Empty try if the value was set, try containing an error otherwise.
*/
public <T> void setDefault(@NotNull ValueNode<T> node) {
config.set(node.getPath(), node.getDefaultValue());
public <T> Try<Void> reset(@NotNull ValueNode<T> node) {
return Try.run(() -> config.set(node.getPath(), node.getDefaultValue()));
}

/**
* Gets the configuration. Mainly used for {@link StringPropertyHandle}.
*
* @return The configuration.
*/
@NotNull NodeGroup getNodes() {
return nodes;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.mvplugins.multiverse.core.configuration.handle;

/**
* The type of modification to a config.
*/
public enum PropertyModifyAction {
SET,
ADD,
REMOVE,
RESET
}

0 comments on commit 0c37c9e

Please sign in to comment.