Move command completion, context, conditions to new world manager api

This commit is contained in:
Ben Woo 2023-09-04 00:16:44 +08:00
parent 08b87cba39
commit 4eb8dfbc67
No known key found for this signature in database
GPG Key ID: FB2A3645536E12C8
11 changed files with 83 additions and 71 deletions

View File

@ -7,10 +7,10 @@ import co.aikar.commands.annotation.Description;
import co.aikar.commands.annotation.Flags;
import co.aikar.commands.annotation.Subcommand;
import com.onarandombox.MultiverseCore.api.LocationManipulation;
import com.onarandombox.MultiverseCore.api.MVWorld;
import com.onarandombox.MultiverseCore.commandtools.MVCommandManager;
import com.onarandombox.MultiverseCore.commandtools.MultiverseCommand;
import com.onarandombox.MultiverseCore.utils.MVCorei18n;
import com.onarandombox.MultiverseCore.worldnew.MVWorld;
import jakarta.inject.Inject;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
@ -40,12 +40,12 @@ public class CoordinatesCommand extends MultiverseCommand {
Player player,
@Flags("resolve=issuerOnly")
MVWorld world
MVWorld world
) {
issuer.sendInfo(MVCorei18n.COORDINATES_INFO_TITLE);
issuer.sendInfo(MVCorei18n.COORDINATES_INFO_WORLD, "{world}", world.getName());
issuer.sendInfo(MVCorei18n.COORDINATES_INFO_ALIAS, "{alias}", world.getColoredWorldString());
issuer.sendInfo(MVCorei18n.COORDINATES_INFO_WORLDSCALE, "{scale}", String.valueOf(world.getScaling()));
issuer.sendInfo(MVCorei18n.COORDINATES_INFO_ALIAS, "{alias}", world.getAlias());
issuer.sendInfo(MVCorei18n.COORDINATES_INFO_WORLDSCALE, "{scale}", String.valueOf(world.getScale()));
issuer.sendInfo(MVCorei18n.COORDINATES_INFO_COORDINATES, "{coordinates}", locationManipulation.strCoords(player.getLocation()));
issuer.sendInfo(MVCorei18n.COORDINATES_INFO_DIRECTION, "{direction}", locationManipulation.getDirection(player.getLocation()));
}

View File

@ -42,7 +42,7 @@ public class DeleteCommand extends MultiverseCommand {
public void onDeleteCommand(MVCommandIssuer issuer,
@Single
//@Conditions("worldname:scope=both")
@Conditions("worldname:scope=both")
@Syntax("<world>")
@Description("The world you want to delete.")
String worldName

View File

@ -8,13 +8,14 @@ import co.aikar.commands.annotation.Description;
import co.aikar.commands.annotation.Flags;
import co.aikar.commands.annotation.Subcommand;
import co.aikar.commands.annotation.Syntax;
import com.onarandombox.MultiverseCore.api.MVWorld;
import com.onarandombox.MultiverseCore.commandtools.MVCommandManager;
import com.onarandombox.MultiverseCore.commandtools.MultiverseCommand;
import com.onarandombox.MultiverseCore.commandtools.context.GameRuleValue;
import com.onarandombox.MultiverseCore.utils.MVCorei18n;
import com.onarandombox.MultiverseCore.worldnew.MVWorld;
import jakarta.inject.Inject;
import org.bukkit.GameRule;
import org.bukkit.World;
import org.jetbrains.annotations.NotNull;
import org.jvnet.hk2.annotations.Service;
@ -51,7 +52,8 @@ public class GameruleCommand extends MultiverseCommand {
boolean success = true;
for(MVWorld world : worlds) {
// Set gamerules and add false to list if it fails
if (!world.getCBWorld().setGameRule(gamerule, value)) {
World bukkitWorld = world.getBukkitWorld().getOrNull();
if (bukkitWorld == null || !bukkitWorld.setGameRule(gamerule, value)) {
issuer.sendError(MVCorei18n.GAMERULE_FAILED,
"{gamerule}", gamerule.getName(),
"{value}", value.toString(),

View File

@ -40,7 +40,7 @@ public class LoadCommand extends MultiverseCommand {
public void onLoadCommand(MVCommandIssuer issuer,
@Single
//@Conditions("worldname:scope=unloaded")
@Conditions("worldname:scope=unloaded")
@Syntax("<world>")
@Description("{@@mv-core.load.world.description}")
String worldName

View File

@ -37,7 +37,7 @@ public class RemoveCommand extends MultiverseCommand {
public void onRemoveCommand(MVCommandIssuer issuer,
@Single
//@Conditions("mvworlds:scope=both")
@Conditions("mvworlds:scope=both")
@Syntax("<world>")
@Description("{@@mv-core.remove.world.description}")
String worldName

View File

@ -11,6 +11,7 @@ import com.onarandombox.MultiverseCore.commandtools.MVCommandIssuer;
import com.onarandombox.MultiverseCore.commandtools.MVCommandManager;
import com.onarandombox.MultiverseCore.commandtools.MultiverseCommand;
import com.onarandombox.MultiverseCore.utils.MVCorei18n;
import com.onarandombox.MultiverseCore.worldnew.MVWorld;
import com.onarandombox.MultiverseCore.worldnew.WorldManager;
import jakarta.inject.Inject;
import org.jetbrains.annotations.NotNull;
@ -37,10 +38,10 @@ public class UnloadCommand extends MultiverseCommand {
@Syntax("<world>")
@Description("{@@mv-core.unload.world.description}")
String worldName // TODO: Use world object
MVWorld world
) {
issuer.sendInfo(MVCorei18n.UNLOAD_UNLOADING, "{world}", worldName);
worldManager.unloadWorld(worldName)
issuer.sendInfo(MVCorei18n.UNLOAD_UNLOADING, "{world}", world.getAlias());
worldManager.unloadWorld(world)
.onSuccess((success) -> {
Logging.fine("World unload success: " + success);
issuer.sendInfo(success.getReasonMessage());

View File

@ -1,6 +1,24 @@
package com.onarandombox.MultiverseCore.commandtools;
import java.util.ArrayList;
import co.aikar.commands.BukkitCommandCompletionContext;
import co.aikar.commands.BukkitCommandIssuer;
import co.aikar.commands.CommandIssuer;
import co.aikar.commands.PaperCommandCompletions;
import co.aikar.commands.RegisteredCommand;
import co.aikar.commands.RootCommand;
import com.dumptruckman.minecraft.util.Logging;
import com.google.common.collect.Sets;
import com.onarandombox.MultiverseCore.config.MVCoreConfig;
import com.onarandombox.MultiverseCore.destination.DestinationsProvider;
import com.onarandombox.MultiverseCore.destination.ParsedDestination;
import com.onarandombox.MultiverseCore.worldnew.MVWorld;
import com.onarandombox.MultiverseCore.worldnew.OfflineWorld;
import com.onarandombox.MultiverseCore.worldnew.WorldManager;
import jakarta.inject.Inject;
import org.bukkit.GameRule;
import org.jetbrains.annotations.NotNull;
import org.jvnet.hk2.annotations.Service;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
@ -9,34 +27,17 @@ import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import co.aikar.commands.BukkitCommandCompletionContext;
import co.aikar.commands.BukkitCommandIssuer;
import co.aikar.commands.CommandIssuer;
import co.aikar.commands.PaperCommandCompletions;
import co.aikar.commands.RegisteredCommand;
import co.aikar.commands.RootCommand;
import com.google.common.collect.Sets;
import com.onarandombox.MultiverseCore.api.MVWorld;
import com.onarandombox.MultiverseCore.api.MVWorldManager;
import com.onarandombox.MultiverseCore.config.MVCoreConfig;
import com.onarandombox.MultiverseCore.destination.DestinationsProvider;
import com.onarandombox.MultiverseCore.destination.ParsedDestination;
import jakarta.inject.Inject;
import org.bukkit.GameRule;
import org.jetbrains.annotations.NotNull;
import org.jvnet.hk2.annotations.Service;
@Service
public class MVCommandCompletions extends PaperCommandCompletions {
protected final MVCommandManager commandManager;
private final MVWorldManager worldManager;
private final WorldManager worldManager;
private final DestinationsProvider destinationsProvider;
@Inject
public MVCommandCompletions(
@NotNull MVCommandManager mvCommandManager,
@NotNull MVWorldManager worldManager,
@NotNull WorldManager worldManager,
@NotNull DestinationsProvider destinationsProvider,
@NotNull MVCoreConfig config
) {
@ -124,23 +125,26 @@ public class MVCommandCompletions extends PaperCommandCompletions {
private List<String> getMVWorldNames(BukkitCommandCompletionContext context) {
String scope = context.getConfig("scope", "loaded");
List<String> worlds = new ArrayList<>();
switch (scope) {
case "both":
worlds.addAll(worldManager.getUnloadedWorlds());
case "loaded":
worldManager.getMVWorlds()
case "both" -> {
return worldManager.getOfflineWorlds().stream().map(OfflineWorld::getName).toList();
}
case "loaded" -> {
return worldManager.getMVWorlds()
.stream()
.map(MVWorld::getName)
.forEach(worlds::add);
break;
case "unloaded":
worlds.addAll(worldManager.getUnloadedWorlds());
break;
case "potential":
worlds.addAll(worldManager.getPotentialWorlds());
break;
.toList();
}
case "unloaded" -> {
return worldManager.getOfflineOnlyWorlds().stream()
.map(OfflineWorld::getName)
.toList();
}
case "potential" -> {
return Collections.emptyList(); // TODO: Implement getPotentialWorlds
}
}
return worlds;
Logging.severe("Invalid MVWorld scope: " + scope);
return Collections.emptyList();
}
}

View File

@ -8,17 +8,18 @@ import co.aikar.commands.ConditionContext;
import co.aikar.commands.ConditionFailedException;
import com.onarandombox.MultiverseCore.api.MVWorldManager;
import com.onarandombox.MultiverseCore.world.WorldNameChecker;
import com.onarandombox.MultiverseCore.worldnew.WorldManager;
import org.jetbrains.annotations.NotNull;
public class MVCommandConditions {
static void load(MVCommandManager commandManager, MVWorldManager worldManager) {
static void load(MVCommandManager commandManager, WorldManager worldManager) {
new MVCommandConditions(commandManager, worldManager);
}
private final MVWorldManager worldManager;
private final WorldManager worldManager;
private final MVCommandManager commandManager;
private MVCommandConditions(@NotNull MVCommandManager commandManager, @NotNull MVWorldManager worldManager) {
private MVCommandConditions(@NotNull MVCommandManager commandManager, @NotNull WorldManager worldManager) {
this.worldManager = worldManager;
this.commandManager = commandManager;
registerConditions();
@ -46,7 +47,7 @@ public class MVCommandConditions {
break;
// Worlds that are unloaded
case "unloaded":
if (!this.worldManager.hasUnloadedWorld(worldName, false)) {
if (!this.worldManager.isOfflineOnlyWorld(worldName)) {
if (this.worldManager.isMVWorld(worldName)) {
throw new ConditionFailedException("World with name '" + worldName + "' is loaded already!");
}
@ -55,20 +56,20 @@ public class MVCommandConditions {
break;
// World that are loaded or unloaded
case "both":
if (!this.worldManager.hasUnloadedWorld(worldName, true)) {
if (!this.worldManager.isOfflineWorld(worldName)) {
throw new ConditionFailedException("World with name '" + worldName + "' does not exist!");
}
break;
// World that are does not exist
case "new":
if (this.worldManager.hasUnloadedWorld(worldName, true)) {
if (this.worldManager.isOfflineWorld(worldName)) {
throw new ConditionFailedException("World with name '" + worldName + "' already exists!");
}
switch (WorldNameChecker.checkName(worldName)) {
case INVALID_CHARS:
throw new ConditionFailedException("World name '" + worldName + "' contains invalid characters!");
case BLACKLISTED:
throw new ConditionFailedException("World name '" + worldName + "' is used for critical server operations and is blacklisted!");
case INVALID_CHARS ->
throw new ConditionFailedException("World name '" + worldName + "' contains invalid characters!");
case BLACKLISTED ->
throw new ConditionFailedException("World name '" + worldName + "' is used for critical server operations and is blacklisted!");
}
break;
// Probably a typo happened here

View File

@ -1,7 +1,6 @@
package com.onarandombox.MultiverseCore.commandtools;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import co.aikar.commands.BukkitCommandExecutionContext;
@ -10,8 +9,6 @@ import co.aikar.commands.InvalidCommandArgument;
import co.aikar.commands.PaperCommandContexts;
import co.aikar.commands.contexts.ContextResolver;
import com.google.common.base.Strings;
import com.onarandombox.MultiverseCore.api.MVWorld;
import com.onarandombox.MultiverseCore.api.MVWorldManager;
import com.onarandombox.MultiverseCore.commandtools.context.GameRuleValue;
import com.onarandombox.MultiverseCore.commandtools.context.MVConfigValue;
import com.onarandombox.MultiverseCore.config.MVCoreConfig;
@ -23,6 +20,8 @@ import com.onarandombox.MultiverseCore.display.filters.ContentFilter;
import com.onarandombox.MultiverseCore.display.filters.DefaultContentFilter;
import com.onarandombox.MultiverseCore.display.filters.RegexContentFilter;
import com.onarandombox.MultiverseCore.utils.PlayerFinder;
import com.onarandombox.MultiverseCore.worldnew.MVWorld;
import com.onarandombox.MultiverseCore.worldnew.WorldManager;
import io.vavr.control.Option;
import jakarta.inject.Inject;
import org.bukkit.GameRule;
@ -34,14 +33,14 @@ public class MVCommandContexts extends PaperCommandContexts {
private final MVCommandManager mvCommandManager;
private final DestinationsProvider destinationsProvider;
private final MVWorldManager worldManager;
private final WorldManager worldManager;
private final MVCoreConfig config;
@Inject
public MVCommandContexts(
MVCommandManager mvCommandManager,
DestinationsProvider destinationsProvider,
MVWorldManager worldManager,
WorldManager worldManager,
MVCoreConfig config
) {
super(mvCommandManager);
@ -168,7 +167,7 @@ public class MVCommandContexts extends PaperCommandContexts {
// Get world based on sender only
if (resolve.equals("issuerOnly")) {
if (context.getIssuer().isPlayer()) {
return worldManager.getMVWorld(context.getIssuer().getPlayer().getWorld());
return worldManager.getMVWorld(context.getIssuer().getPlayer().getWorld()).getOrNull();
}
if (context.isOptional()) {
return null;
@ -177,7 +176,7 @@ public class MVCommandContexts extends PaperCommandContexts {
}
String worldName = context.getFirstArg();
MVWorld world = worldManager.getMVWorld(worldName);
MVWorld world = worldManager.getMVWorld(worldName).getOrNull();
// Get world based on input, fallback to sender if input is not a world
if (resolve.equals("issuerAware")) {
@ -186,7 +185,7 @@ public class MVCommandContexts extends PaperCommandContexts {
return world;
}
if (context.getIssuer().isPlayer()) {
return worldManager.getMVWorld(context.getIssuer().getPlayer().getWorld());
return worldManager.getMVWorld(context.getIssuer().getPlayer().getWorld()).getOrNull();
}
if (context.isOptional()) {
return null;
@ -210,7 +209,7 @@ public class MVCommandContexts extends PaperCommandContexts {
MVWorld playerWorld = null;
if (context.getIssuer().isPlayer()) {
playerWorld = worldManager.getMVWorld(context.getIssuer().getPlayer().getWorld());
playerWorld = worldManager.getMVWorld(context.getIssuer().getPlayer().getWorld()).getOrNull();
}
// Get world based on sender only
@ -225,14 +224,14 @@ public class MVCommandContexts extends PaperCommandContexts {
}
String worldStrings = context.getFirstArg();
String[] worldNames = worldStrings.split(",");
String[] worldNames = worldStrings == null ? new String[0] : worldStrings.split(",");
Set<MVWorld> worlds = new HashSet<>(worldNames.length);
for (String worldName : worldNames) {
if ("*".equals(worldName)) {
worlds.addAll(worldManager.getMVWorlds());
break;
}
MVWorld world = worldManager.getMVWorld(worldName);
MVWorld world = worldManager.getMVWorld(worldName).getOrNull();
if (world == null) {
throw new InvalidCommandArgument("World " + worldName + " is not a loaded multiverse world.");
}
@ -259,7 +258,7 @@ public class MVCommandContexts extends PaperCommandContexts {
context.popFirstArg();
return worlds.toArray(new MVWorld[0]);
}
if (!context.isOptional()) {
if (context.isOptional()) {
return null;
}
throw new InvalidCommandArgument("World " + worldStrings + " is not a loaded multiverse world.");
@ -303,7 +302,7 @@ public class MVCommandContexts extends PaperCommandContexts {
context.popFirstArg();
return player;
}
if (!context.isOptional()) {
if (context.isOptional()) {
return null;
}
throw new InvalidCommandArgument("Player " + playerIdentifier + " not found.");
@ -348,7 +347,7 @@ public class MVCommandContexts extends PaperCommandContexts {
context.popFirstArg();
return players;
}
if (!context.isOptional()) {
if (context.isOptional()) {
return null;
}
throw new InvalidCommandArgument("Player " + playerIdentifier + " not found.");

View File

@ -14,6 +14,7 @@ import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.api.MVWorldManager;
import com.onarandombox.MultiverseCore.commandtools.flags.CommandFlagsManager;
import com.onarandombox.MultiverseCore.commandtools.queue.CommandQueueManager;
import com.onarandombox.MultiverseCore.worldnew.WorldManager;
import jakarta.inject.Inject;
import jakarta.inject.Provider;
import org.bukkit.Bukkit;
@ -39,7 +40,7 @@ public class MVCommandManager extends PaperCommandManager {
@NotNull CommandQueueManager commandQueueManager,
@NotNull Provider<MVCommandContexts> commandContextsProvider,
@NotNull Provider<MVCommandCompletions> commandCompletionsProvider,
@NotNull MVWorldManager worldManager
@NotNull WorldManager worldManager
) {
super(plugin);
this.flagsManager = flagsManager;

View File

@ -302,6 +302,10 @@ public class WorldManager {
return offlineWorldsMap.values().stream().filter(world -> !world.isLoaded()).toList();
}
public boolean isOfflineOnlyWorld(@Nullable String worldName) {
return !isMVWorld(worldName) && isOfflineWorld(worldName);
}
public Option<OfflineWorld> getOfflineWorld(@Nullable String worldName) {
return Option.of(offlineWorldsMap.get(worldName));
}