Implement flags for version command.

This commit is contained in:
benwoo1110 2021-03-05 12:57:48 +08:00
parent a6d4e21b85
commit 7a86227a94
3 changed files with 76 additions and 18 deletions

View File

@ -91,9 +91,6 @@ public class MVCommandCompletions extends PaperCommandCompletions {
String[] args = context.getContextValue(String[].class); String[] args = context.getContextValue(String[].class);
CommandFlag<?> flag = (args.length <= 1) ? null : flagGroup.getByKey(args[args.length - 2]); CommandFlag<?> flag = (args.length <= 1) ? null : flagGroup.getByKey(args[args.length - 2]);
Logging.info(Arrays.toString(args));
Logging.info(String.valueOf(flag));
if (flag == null || flag.getValueRequirement() == ValueRequirement.NONE) { if (flag == null || flag.getValueRequirement() == ValueRequirement.NONE) {
// suggest new flags. // suggest new flags.
return getRemainingFlagIdentifiers(flagGroup, args); return getRemainingFlagIdentifiers(flagGroup, args);

View File

@ -1,14 +1,18 @@
package com.onarandombox.MultiverseCore.commandtools.flags; package com.onarandombox.MultiverseCore.commandtools.flags;
import co.aikar.commands.InvalidCommandArgument;
import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.utils.webpaste.PasteServiceType;
import org.bukkit.WorldType; import org.bukkit.WorldType;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Random; import java.util.Random;
import java.util.stream.Collectors;
public class MVFlags { public class MVFlags {
@ -18,7 +22,8 @@ public class MVFlags {
multiverse = plugin; multiverse = plugin;
} }
public static final CommandFlag<String> SEED = new RequiredCommandFlag<String>("Seed", "-s", String.class) { public static final CommandFlag<String> SEED = new RequiredCommandFlag<String>
("Seed", "-s", String.class) {
@Override @Override
public Collection<String> suggestValue() { public Collection<String> suggestValue() {
return Arrays.asList("seed", String.valueOf(new Random().nextLong())); return Arrays.asList("seed", String.valueOf(new Random().nextLong()));
@ -30,7 +35,8 @@ public class MVFlags {
} }
}; };
public static final CommandFlag<String> RANDOM_SEED = new OptionalCommandFlag<String>("Seed", "-s", String.class) { public static final CommandFlag<String> RANDOM_SEED = new OptionalCommandFlag<String>
("Seed", "-s", String.class) {
@Override @Override
public Collection<String> suggestValue() { public Collection<String> suggestValue() {
return Arrays.asList("seed", String.valueOf(new Random().nextLong())); return Arrays.asList("seed", String.valueOf(new Random().nextLong()));
@ -42,7 +48,8 @@ public class MVFlags {
} }
}; };
public static final CommandFlag<WorldType> WORLD_TYPE = new RequiredCommandFlag<WorldType>("WorldType", "-t", WorldType.class) { public static final CommandFlag<WorldType> WORLD_TYPE = new RequiredCommandFlag<WorldType>
("WorldType", "-t", WorldType.class) {
private final Map<String, WorldType> typeAlias = new HashMap<String, WorldType>(4){{ private final Map<String, WorldType> typeAlias = new HashMap<String, WorldType>(4){{
put("normal", WorldType.NORMAL); put("normal", WorldType.NORMAL);
@ -76,7 +83,8 @@ public class MVFlags {
} }
}; };
public static final CommandFlag<String> GENERATOR = new RequiredCommandFlag<String>("Generator", "-g", String.class) { public static final CommandFlag<String> GENERATOR = new RequiredCommandFlag<String>
("Generator", "-g", String.class) {
@Override @Override
public Collection<String> suggestValue() { public Collection<String> suggestValue() {
return multiverse.getMVWorldManager().getAvailableWorldGenerators(); return multiverse.getMVWorldManager().getAvailableWorldGenerators();
@ -98,7 +106,8 @@ public class MVFlags {
} }
}; };
public static final CommandFlag<Boolean> GENERATE_STRUCTURES = new RequiredCommandFlag<Boolean>("GenerateStructures", "-a", Boolean.class) { public static final CommandFlag<Boolean> GENERATE_STRUCTURES = new RequiredCommandFlag<Boolean>
("GenerateStructures", "-a", Boolean.class) {
@Override @Override
public Collection<String> suggestValue() { public Collection<String> suggestValue() {
return Arrays.asList("true", "false"); return Arrays.asList("true", "false");
@ -115,7 +124,8 @@ public class MVFlags {
} }
}; };
public static final CommandFlag<Boolean> SPAWN_ADJUST = new NoValueCommandFlag<Boolean>("AdjustSpawn", "-n", Boolean.class) { public static final CommandFlag<Boolean> SPAWN_ADJUST = new NoValueCommandFlag<Boolean>
("AdjustSpawn", "-n", Boolean.class) {
@Override @Override
public Boolean getValue() throws FlagParseFailedException { public Boolean getValue() throws FlagParseFailedException {
return false; return false;
@ -127,5 +137,50 @@ public class MVFlags {
} }
}; };
public static final CommandFlag<PasteServiceType> PASTE_SERVICE_TYPE = new OptionalCommandFlag<PasteServiceType>
("PasteServiceType", "--paste", PasteServiceType.class) {
private final List<String> pasteTypes = Arrays.stream(PasteServiceType.values())
.filter(pt -> pt != PasteServiceType.NONE)
.map(p -> p.toString().toLowerCase())
.collect(Collectors.toList());
@Override
public Collection<String> suggestValue() {
return pasteTypes;
}
@Override
public PasteServiceType getValue(@NotNull String input) throws FlagParseFailedException {
try {
return PasteServiceType.valueOf(input.toUpperCase());
}
catch (IllegalArgumentException e) {
throw new InvalidCommandArgument(String.format("Invalid paste service type '%s'.", input));
}
}
@Override
public PasteServiceType getValue() throws FlagParseFailedException {
return PasteServiceType.PASTEGG;
}
@Override
public PasteServiceType getDefaultValue() {
return PasteServiceType.NONE;
}
}.addAlias("-p");
public static final CommandFlag<Boolean> INCLUDE_PLUGIN_LIST = new NoValueCommandFlag<Boolean>
("IncludePlugins", "--include-plugin-list", Boolean.class) {
@Override
public Boolean getValue() throws FlagParseFailedException {
return true;
}
@Override
public Boolean getDefaultValue() {
return true;
}
}.addAlias("-pl");
} }

View File

@ -14,6 +14,9 @@ import co.aikar.commands.annotation.Description;
import co.aikar.commands.annotation.Subcommand; import co.aikar.commands.annotation.Subcommand;
import co.aikar.commands.annotation.Syntax; import co.aikar.commands.annotation.Syntax;
import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.commandtools.flags.FlagGroup;
import com.onarandombox.MultiverseCore.commandtools.flags.FlagResult;
import com.onarandombox.MultiverseCore.commandtools.flags.MVFlags;
import com.onarandombox.MultiverseCore.event.MVVersionEvent; import com.onarandombox.MultiverseCore.event.MVVersionEvent;
import com.onarandombox.MultiverseCore.utils.webpaste.PasteFailedException; import com.onarandombox.MultiverseCore.utils.webpaste.PasteFailedException;
import com.onarandombox.MultiverseCore.utils.webpaste.PasteService; import com.onarandombox.MultiverseCore.utils.webpaste.PasteService;
@ -25,6 +28,7 @@ import com.onarandombox.MultiverseCore.utils.webpaste.URLShortenerType;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -40,30 +44,31 @@ public class VersionCommand extends MultiverseCoreCommand {
public VersionCommand(MultiverseCore plugin) { public VersionCommand(MultiverseCore plugin) {
super(plugin); super(plugin);
this.setFlagGroup(FlagGroup.of(
MVFlags.PASTE_SERVICE_TYPE,
MVFlags.INCLUDE_PLUGIN_LIST
));
} }
@Subcommand("version") @Subcommand("version")
@CommandPermission("multiverse.core.version") @CommandPermission("multiverse.core.version")
@Syntax("[pastebin|hastebin|pastegg] [--include-plugin-list]") @Syntax("--paste [pastebin|hastebin|pastegg] [--include-plugin-list]")
@CommandCompletion("@pasteTypes --include-plugin-list") @CommandCompletion("@flags")
@Description("Dumps version info to the console, optionally to pastal service.") @Description("Dumps version info to the console, optionally to pastal service.")
public void onVersionCommand(@NotNull CommandSender sender, public void onVersionCommand(@NotNull CommandSender sender,
@NotNull @NotNull
@Syntax("[paste-service]") @Syntax("[paste-service]")
@Description("Website to upload your version info to.") @Description("Website to upload your version info to.")
PasteServiceType pasteType, String[] flagsArray) {
@Nullable FlagResult flags = FlagResult.parse(flagsArray, this.getFlagGroup());
@Syntax("[--include-plugin-list]")
@Description("Whether you want to have plugins list in version info.")
String includePlugin) {
MVVersionEvent versionEvent = new MVVersionEvent(); MVVersionEvent versionEvent = new MVVersionEvent();
this.addVersionInfoToEvent(versionEvent); this.addVersionInfoToEvent(versionEvent);
this.plugin.getServer().getPluginManager().callEvent(versionEvent); this.plugin.getServer().getPluginManager().callEvent(versionEvent);
if (includePlugin != null && (includePlugin.equalsIgnoreCase("--include-plugin-list") || includePlugin.equalsIgnoreCase("-pl"))) { if (flags.getValue(MVFlags.INCLUDE_PLUGIN_LIST)) {
versionEvent.appendVersionInfo('\n' + "Plugins: " + getPluginList()); versionEvent.appendVersionInfo('\n' + "Plugins: " + getPluginList());
versionEvent.putDetailedVersionInfo("plugins.txt", "Plugins: " + getPluginList()); versionEvent.putDetailedVersionInfo("plugins.txt", "Plugins: " + getPluginList());
} }
@ -75,7 +80,8 @@ public class VersionCommand extends MultiverseCoreCommand {
logToConsole(versionInfo); logToConsole(versionInfo);
if (pasteType == PasteServiceType.NONE) { PasteServiceType pasteType = flags.getValue(MVFlags.PASTE_SERVICE_TYPE);
if (pasteType == PasteServiceType.NONE && !(sender instanceof ConsoleCommandSender)) {
sender.sendMessage("Version info dumped to console! Please check your server logs."); sender.sendMessage("Version info dumped to console! Please check your server logs.");
return; return;
} }