Skip to content

Commit 5b832c5

Browse files
Alexander SöderbergCitymonstret
authored andcommitted
feature: update to cloud v2
This is still a W.I.P. as Cloud v2 is nowhere near ready to be released. There are no up-to-date builds in the OSS repository, so this currently relies on the local Maven repository.
1 parent b6a6878 commit 5b832c5

26 files changed

Lines changed: 158 additions & 192 deletions

build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ java {
2323

2424
repositories {
2525
mavenCentral()
26+
mavenLocal()
2627
maven { url = uri("https://repo.papermc.io/repository/maven-public/") }
2728
}
2829

@@ -82,6 +83,7 @@ tasks {
8283
compileJava {
8384
options.compilerArgs.addAll(arrayOf("-Xmaxerrs", "1000"))
8485
options.compilerArgs.add("-Xlint:all")
86+
options.compilerArgs.add("-parameters")
8587
for (disabledLint in arrayOf("processing", "path", "fallthrough", "serial"))
8688
options.compilerArgs.add("-Xlint:$disabledLint")
8789
options.isDeprecation = true

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
annotations = "24.1.0"
44

55
# Cloud command system
6-
cloud = "1.8.4"
6+
cloud = "2.0.0-SNAPSHOT"
77

88
# Gradle plugins
99
shadow = "8.1.1"

src/main/java/com/thevoxelbox/voxelsniper/command/CommandRegistry.java

Lines changed: 48 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,20 @@
55
import cloud.commandframework.annotations.Argument;
66
import cloud.commandframework.annotations.MethodCommandExecutionHandler;
77
import cloud.commandframework.annotations.injection.ParameterInjectorRegistry;
8-
import cloud.commandframework.arguments.CommandArgument;
9-
import cloud.commandframework.arguments.parser.StandardParameters;
10-
import cloud.commandframework.arguments.standard.EnumArgument;
8+
import cloud.commandframework.arguments.standard.EnumParser;
119
import cloud.commandframework.bukkit.BukkitCommandManager;
1210
import cloud.commandframework.bukkit.CloudBukkitCapabilities;
1311
import cloud.commandframework.captions.CaptionVariable;
1412
import cloud.commandframework.context.CommandContext;
1513
import cloud.commandframework.exceptions.ArgumentParseException;
16-
import cloud.commandframework.exceptions.CommandExecutionException;
1714
import cloud.commandframework.exceptions.InvalidCommandSenderException;
1815
import cloud.commandframework.exceptions.InvalidSyntaxException;
1916
import cloud.commandframework.exceptions.NoPermissionException;
17+
import cloud.commandframework.exceptions.handling.ExceptionHandler;
2018
import cloud.commandframework.exceptions.parsing.ParserException;
2119
import cloud.commandframework.execution.CommandExecutionCoordinator;
2220
import cloud.commandframework.execution.FilteringCommandSuggestionProcessor;
2321
import cloud.commandframework.keys.CloudKey;
24-
import cloud.commandframework.keys.SimpleCloudKey;
25-
import cloud.commandframework.meta.CommandMeta;
2622
import cloud.commandframework.paper.PaperCommandManager;
2723
import cloud.commandframework.services.types.ConsumerService;
2824
import com.fastasyncworldedit.core.configuration.Caption;
@@ -59,19 +55,19 @@
5955

6056
public class CommandRegistry {
6157

62-
public static final CloudKey<Snipe> SNIPE_KEY = createTypeKey(
58+
public static final CloudKey<Snipe> SNIPE_KEY = createCloudKey(
6359
"snipe", Snipe.class
6460
);
65-
public static final CloudKey<PerformerSnipe> PERFORMER_SNIPE_KEY = createTypeKey(
61+
public static final CloudKey<PerformerSnipe> PERFORMER_SNIPE_KEY = createCloudKey(
6662
"snipe", PerformerSnipe.class
6763
);
68-
public static final CloudKey<Toolkit> TOOLKIT_KEY = createTypeKey(
64+
public static final CloudKey<Toolkit> TOOLKIT_KEY = createCloudKey(
6965
"toolkit", Toolkit.class
7066
);
7167

7268
private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
7369
private static final String NO_DESCRIPTION = "No Description.";
74-
private static final CommandMeta.Key<Boolean> REQUIRE_TOOLKIT = createMetaKey(
70+
private static final CloudKey<Boolean> REQUIRE_TOOLKIT = createCloudKey(
7571
"require-toolkit",
7672
Boolean.class
7773
);
@@ -138,18 +134,16 @@ private BukkitCommandManager<SniperCommander> createCommandManager() throws Exce
138134
// Handles post-processor.
139135
commandManager.registerCommandPostProcessor(context -> {
140136
// Ensures that we are working with a voxel sniper annotated command.
141-
CommandContext<SniperCommander> commandContext = context.getCommandContext();
142-
Command<SniperCommander> command = context.getCommand();
143-
if (!(commandContext.getSender() instanceof Sniper sniper)
144-
|| !(command.getCommandExecutionHandler() instanceof MethodCommandExecutionHandler<SniperCommander> handler)) {
137+
CommandContext<SniperCommander> commandContext = context.commandContext();
138+
Command<SniperCommander> command = context.command();
139+
if (!(commandContext.sender() instanceof Sniper sniper)
140+
|| !(command.commandExecutionHandler() instanceof MethodCommandExecutionHandler<SniperCommander> handler)) {
145141
return;
146142
}
147143

148144
Toolkit toolkit;
149145
// Toolkit requirement relies on the custom annotation.
150-
if (command.getCommandMeta()
151-
.get(REQUIRE_TOOLKIT)
152-
.orElse(false)) {
146+
if (command.commandMeta().getOrDefault(REQUIRE_TOOLKIT, false)) {
153147
if ((toolkit = sniper.getCurrentToolkit()) == null) {
154148
sniper.print(Caption.of("voxelsniper.command.missing-toolkit"));
155149
ConsumerService.interrupt();
@@ -181,54 +175,49 @@ private BukkitCommandManager<SniperCommander> createCommandManager() throws Exce
181175
});
182176

183177
// Handles exceptions.
184-
commandManager.registerExceptionHandler(InvalidSyntaxException.class, (commander, e) ->
185-
commander.print(Caption.of(
178+
commandManager.exceptionController().registerHandler(InvalidSyntaxException.class, ctx ->
179+
ctx.context().sender().print(Caption.of(
186180
"voxelsniper.command.invalid-command-syntax",
187-
e.getCorrectSyntax()
188-
)));
189-
commandManager.registerExceptionHandler(InvalidCommandSenderException.class, (commander, e) ->
190-
commander.print(Caption.of(
181+
ctx.exception().getCorrectSyntax()
182+
))
183+
).registerHandler(InvalidCommandSenderException.class, ctx ->
184+
ctx.context().sender().print(Caption.of(
191185
"voxelsniper.command.invalid-sender-type",
192-
e.getRequiredSender().getSimpleName()
193-
)));
194-
commandManager.registerExceptionHandler(NoPermissionException.class, (commander, e) ->
195-
commander.print(Caption.of(
186+
ctx.exception().getRequiredSender().getSimpleName()
187+
))
188+
).registerHandler(NoPermissionException.class, ctx ->
189+
ctx.context().sender().print(Caption.of(
196190
"voxelsniper.command.missing-permission",
197-
e.getMissingPermission()
198-
)));
199-
commandManager.registerExceptionHandler(ArgumentParseException.class, (commander, e) -> {
200-
Throwable t = e.getCause();
201-
202-
if (t instanceof VoxelCommandElementParseException ve) {
203-
commander.print(ve.getErrorMessage());
204-
} else if (t instanceof EnumArgument.EnumParseException ee) {
205-
commander.print(Caption.of(
191+
ctx.exception().missingPermission()
192+
))
193+
).registerHandler(
194+
ArgumentParseException.class,
195+
ExceptionHandler.unwrappingHandler()
196+
).registerHandler(VoxelCommandElementParseException.class, ctx ->
197+
ctx.context().sender().print(ctx.exception().getErrorMessage())
198+
).registerHandler(EnumParser.EnumParseException.class, ctx ->
199+
ctx.context().sender().print(Caption.of(
206200
"voxelsniper.command.invalid-enum",
207-
ee.getInput(),
201+
ctx.exception().getInput(),
208202
VoxelSniperText.formatList(
209-
Arrays.stream(ee.getEnumClass().getEnumConstants()).toList(),
203+
Arrays.stream(ctx.exception().getEnumClass().getEnumConstants()).toList(),
210204
(value, value2) -> Integer.compare(value.ordinal(), value2.ordinal()),
211205
value -> TextComponent.of(value.name().toLowerCase(Locale.ROOT)),
212206
"voxelsniper.command.invalid-enum"
213207
)
214-
));
215-
} else if (t instanceof ParserException pe) {
216-
commander.print(Caption.of(
217-
pe.errorCaption()
218-
.getKey()
208+
))
209+
).registerHandler(ParserException.class, ctx ->
210+
ctx.context().sender().print(Caption.of(
211+
ctx.exception().errorCaption()
212+
.key()
219213
.replace("argument.parse.failure.", "voxelsniper.command.invalid-"),
220-
Arrays.stream(pe.captionVariables())
221-
.map(CaptionVariable::getValue)
214+
Arrays.stream(ctx.exception().captionVariables())
215+
.map(CaptionVariable::value)
222216
.toArray(Object[]::new)
223-
));
224-
} else {
225-
commander.print(Caption.of("voxelsniper.error.unexpected"));
226-
e.printStackTrace();
227-
}
228-
});
229-
commandManager.registerExceptionHandler(CommandExecutionException.class, (commander, e) -> {
230-
commander.print(Caption.of("voxelsniper.error.unexpected"));
231-
e.printStackTrace();
217+
))
218+
).registerHandler(Throwable.class, ctx -> {
219+
ctx.context().sender().print(Caption.of("voxelsniper.error.unexpected"));
220+
ctx.exception().printStackTrace();
232221
});
233222

234223
return commandManager;
@@ -238,11 +227,7 @@ private AnnotationParser<SniperCommander> createAnnotationParser(BukkitCommandMa
238227
// Creates the annotation parser.
239228
AnnotationParser<SniperCommander> annotationParser = new AnnotationParser<>(
240229
commandManager,
241-
SniperCommander.class,
242-
parserParameters -> CommandMeta.simple()
243-
.with(CommandMeta.DESCRIPTION, parserParameters
244-
.get(StandardParameters.DESCRIPTION, NO_DESCRIPTION))
245-
.build()
230+
SniperCommander.class
246231
);
247232

248233
// Handles the custom annotations.
@@ -290,7 +275,7 @@ private void handleDynamicRanges(
290275
MethodCommandExecutionHandler<SniperCommander> handler,
291276
Object instance
292277
) {
293-
SniperCommander commander = commandContext.getSender();
278+
SniperCommander commander = commandContext.sender();
294279
MethodCommandExecutionHandler.CommandMethodContext<SniperCommander> methodContext = handler.context();
295280

296281
// Dynamic range is based on executor instance fields, we must postprocess them manually.
@@ -310,9 +295,7 @@ private void handleDynamicRanges(
310295
argumentName = this.annotationParser.processString(argumentAnnotation.value());
311296
}
312297

313-
CommandArgument<SniperCommander, Number> argument =
314-
(CommandArgument<SniperCommander, Number>) methodContext.commandArguments().get(argumentName);
315-
double number = commandContext.get(argument).doubleValue();
298+
double number = commandContext.<Number>get(argumentName).doubleValue();
316299

317300
DynamicRange dynamicRangeAnnotation = parameter.getAnnotation(DynamicRange.class);
318301
String min = dynamicRangeAnnotation.min();
@@ -444,12 +427,8 @@ public AnnotationParser<SniperCommander> getAnnotationParser() {
444427
return annotationParser;
445428
}
446429

447-
private static <T> CloudKey<T> createTypeKey(String id, Class<T> clazz) {
448-
return SimpleCloudKey.of("voxelsniper-" + id, TypeToken.get(clazz));
449-
}
450-
451-
private static <T> CommandMeta.Key<T> createMetaKey(String id, Class<T> clazz) {
452-
return CommandMeta.Key.of(TypeToken.get(clazz), "voxelsniper-" + id);
430+
private static <T> CloudKey<T> createCloudKey(String id, Class<T> clazz) {
431+
return CloudKey.of("voxelsniper-" + id, TypeToken.get(clazz));
453432
}
454433

455434
}

src/main/java/com/thevoxelbox/voxelsniper/command/VoxelMethodCommandExecutionHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.lang.invoke.MethodHandle;
99
import java.lang.invoke.MethodHandles;
1010
import java.util.function.Function;
11+
import java.util.stream.Collectors;
1112

1213
public class VoxelMethodCommandExecutionHandler<C> extends MethodCommandExecutionHandler<C> {
1314

@@ -40,7 +41,7 @@ public void execute(@NonNull CommandContext<C> commandContext) {
4041
commandContext,
4142
commandContext.flags(),
4243
super.parameters()
43-
)
44+
).stream().map(ParameterValue::value).collect(Collectors.toList())
4445
);
4546
} catch (final Error e) {
4647
throw e;

src/main/java/com/thevoxelbox/voxelsniper/command/argument/AbstractFileArgument.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thevoxelbox.voxelsniper.command.argument;
22

33
import cloud.commandframework.context.CommandContext;
4+
import cloud.commandframework.context.CommandInput;
45
import cloud.commandframework.exceptions.parsing.NoInputProvidedException;
56
import com.fastasyncworldedit.core.configuration.Caption;
67
import com.thevoxelbox.voxelsniper.VoxelSniperPlugin;
@@ -11,8 +12,6 @@
1112
import java.io.IOException;
1213
import java.nio.file.Files;
1314
import java.nio.file.Path;
14-
import java.util.List;
15-
import java.util.Queue;
1615
import java.util.stream.Stream;
1716

1817
public abstract class AbstractFileArgument implements VoxelCommandElement {
@@ -41,29 +40,26 @@ public AbstractFileArgument(VoxelSniperPlugin plugin, Path rootPath, String exte
4140
}
4241
}
4342

44-
protected List<String> suggestFiles(CommandContext<SniperCommander> commandContext, String input) {
43+
protected Stream<String> suggestFiles(CommandContext<SniperCommander> commandContext, String input) {
4544
Path inputPath = rootPath.resolve(input);
4645
try (Stream<Path> files = Files.list(Files.isDirectory(inputPath) ? inputPath : inputPath.getParent())) {
4746
return files.map(path -> path.getFileName().toString())
48-
.flatMap(path -> Stream.of(path, path.replace(extension, "")))
49-
.toList();
47+
.flatMap(path -> Stream.of(path, path.replace(extension, "")));
5048
} catch (IOException e) {
5149
throw new RuntimeException(e);
5250
}
5351
}
5452

55-
protected File parseFile(CommandContext<SniperCommander> commandContext, Queue<String> inputQueue) {
56-
String input = inputQueue.peek();
57-
if (input == null) {
53+
protected File parseFile(CommandContext<SniperCommander> commandContext, CommandInput input) {
54+
if (input.peekString().isEmpty()) {
5855
throw new NoInputProvidedException(AbstractFileArgument.class, commandContext);
5956
}
6057

58+
final String fileName = input.readString();
6159
try {
62-
File file = rootPath.resolve(input.endsWith(extension) ? input : input + extension).toFile();
63-
inputQueue.remove();
64-
return file;
60+
return rootPath.resolve(fileName.endsWith(extension) ? fileName : fileName + extension).toFile();
6561
} catch (Exception e) {
66-
throw new VoxelCommandElementParseException(input, Caption.of(
62+
throw new VoxelCommandElementParseException(fileName, Caption.of(
6763
"voxelsniper.command.invalid-file",
6864
input
6965
));

src/main/java/com/thevoxelbox/voxelsniper/command/argument/AbstractPatternArgument.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thevoxelbox.voxelsniper.command.argument;
22

33
import cloud.commandframework.context.CommandContext;
4+
import cloud.commandframework.context.CommandInput;
45
import cloud.commandframework.exceptions.parsing.NoInputProvidedException;
56
import com.fastasyncworldedit.core.configuration.Caption;
67
import com.sk89q.worldedit.extension.input.InputParseException;
@@ -16,7 +17,6 @@
1617

1718
import java.util.List;
1819
import java.util.Locale;
19-
import java.util.Queue;
2020

2121
public abstract class AbstractPatternArgument<T extends Pattern> implements VoxelCommandElement {
2222

@@ -44,32 +44,31 @@ protected List<String> suggestPatterns(CommandContext<SniperCommander> commandCo
4444
return factory.getSuggestions(input);
4545
}
4646

47-
protected BrushPattern parsePattern(CommandContext<SniperCommander> commandContext, Queue<String> inputQueue) {
48-
String input = inputQueue.peek();
49-
if (input == null) {
47+
protected BrushPattern parsePattern(CommandContext<SniperCommander> commandContext, CommandInput input) {
48+
if (input.peekString().isEmpty()) {
5049
throw new NoInputProvidedException(AbstractPatternArgument.class, commandContext);
5150
}
5251

53-
SniperCommander commander = commandContext.getSender();
52+
SniperCommander commander = commandContext.sender();
5453
ParserContext parserContext = commander.createParserContext();
54+
String patternString = input.readString();
5555
try {
5656
T pattern = factory.parseFromInput(
57-
input.toLowerCase(Locale.ROOT),
57+
patternString.toLowerCase(Locale.ROOT),
5858
parserContext
5959
);
6060
CommandSender sender = commander.getCommandSender();
6161
if (!sender.hasPermission("voxelsniper.ignorelimitations")
62-
&& config.getLitesniperRestrictedMaterials().contains(getPatternResource(input, pattern))) {
63-
throw new VoxelCommandElementParseException(input, Caption.of(
62+
&& config.getLitesniperRestrictedMaterials().contains(getPatternResource(patternString, pattern))) {
63+
throw new VoxelCommandElementParseException(patternString, Caption.of(
6464
"voxelsniper.command.not-allowed",
6565
input
6666
));
6767
}
6868

69-
inputQueue.remove();
70-
return new BrushPattern(pattern, input);
69+
return new BrushPattern(pattern, patternString);
7170
} catch (InputParseException e) {
72-
throw new VoxelCommandElementParseException(input, Caption.of(
71+
throw new VoxelCommandElementParseException(patternString, Caption.of(
7372
parseExceptionCaptionKey,
7473
input
7574
));

0 commit comments

Comments
 (0)