Skip to content

Commit f08f6ed

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 461e6ad commit f08f6ed

26 files changed

Lines changed: 155 additions & 189 deletions

build.gradle.kts

Lines changed: 1 addition & 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

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: 46 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
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;
@@ -17,12 +15,11 @@
1715
import cloud.commandframework.exceptions.InvalidCommandSenderException;
1816
import cloud.commandframework.exceptions.InvalidSyntaxException;
1917
import cloud.commandframework.exceptions.NoPermissionException;
18+
import cloud.commandframework.exceptions.handling.ExceptionHandler;
2019
import cloud.commandframework.exceptions.parsing.ParserException;
2120
import cloud.commandframework.execution.CommandExecutionCoordinator;
2221
import cloud.commandframework.execution.FilteringCommandSuggestionProcessor;
2322
import cloud.commandframework.keys.CloudKey;
24-
import cloud.commandframework.keys.SimpleCloudKey;
25-
import cloud.commandframework.meta.CommandMeta;
2623
import cloud.commandframework.paper.PaperCommandManager;
2724
import cloud.commandframework.services.types.ConsumerService;
2825
import com.fastasyncworldedit.core.configuration.Caption;
@@ -59,19 +56,19 @@
5956

6057
public class CommandRegistry {
6158

62-
public static final CloudKey<Snipe> SNIPE_KEY = createTypeKey(
59+
public static final CloudKey<Snipe> SNIPE_KEY = createCloudKey(
6360
"snipe", Snipe.class
6461
);
65-
public static final CloudKey<PerformerSnipe> PERFORMER_SNIPE_KEY = createTypeKey(
62+
public static final CloudKey<PerformerSnipe> PERFORMER_SNIPE_KEY = createCloudKey(
6663
"snipe", PerformerSnipe.class
6764
);
68-
public static final CloudKey<Toolkit> TOOLKIT_KEY = createTypeKey(
65+
public static final CloudKey<Toolkit> TOOLKIT_KEY = createCloudKey(
6966
"toolkit", Toolkit.class
7067
);
7168

7269
private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
7370
private static final String NO_DESCRIPTION = "No Description.";
74-
private static final CommandMeta.Key<Boolean> REQUIRE_TOOLKIT = createMetaKey(
71+
private static final CloudKey<Boolean> REQUIRE_TOOLKIT = createCloudKey(
7572
"require-toolkit",
7673
Boolean.class
7774
);
@@ -140,16 +137,14 @@ private BukkitCommandManager<SniperCommander> createCommandManager() throws Exce
140137
// Ensures that we are working with a voxel sniper annotated command.
141138
CommandContext<SniperCommander> commandContext = context.getCommandContext();
142139
Command<SniperCommander> command = context.getCommand();
143-
if (!(commandContext.getSender() instanceof Sniper sniper)
144-
|| !(command.getCommandExecutionHandler() instanceof MethodCommandExecutionHandler<SniperCommander> handler)) {
140+
if (!(commandContext.sender() instanceof Sniper sniper)
141+
|| !(command.commandExecutionHandler() instanceof MethodCommandExecutionHandler<SniperCommander> handler)) {
145142
return;
146143
}
147144

148145
Toolkit toolkit;
149146
// Toolkit requirement relies on the custom annotation.
150-
if (command.getCommandMeta()
151-
.get(REQUIRE_TOOLKIT)
152-
.orElse(false)) {
147+
if (command.commandMeta().getOrDefault(REQUIRE_TOOLKIT, false)) {
153148
if ((toolkit = sniper.getCurrentToolkit()) == null) {
154149
sniper.print(Caption.of("voxelsniper.command.missing-toolkit"));
155150
ConsumerService.interrupt();
@@ -181,54 +176,49 @@ private BukkitCommandManager<SniperCommander> createCommandManager() throws Exce
181176
});
182177

183178
// Handles exceptions.
184-
commandManager.registerExceptionHandler(InvalidSyntaxException.class, (commander, e) ->
185-
commander.print(Caption.of(
179+
commandManager.exceptionController().registerHandler(InvalidSyntaxException.class, ctx ->
180+
ctx.context().sender().print(Caption.of(
186181
"voxelsniper.command.invalid-command-syntax",
187-
e.getCorrectSyntax()
188-
)));
189-
commandManager.registerExceptionHandler(InvalidCommandSenderException.class, (commander, e) ->
190-
commander.print(Caption.of(
182+
ctx.exception().getCorrectSyntax()
183+
))
184+
).registerHandler(InvalidCommandSenderException.class, ctx ->
185+
ctx.context().sender().print(Caption.of(
191186
"voxelsniper.command.invalid-sender-type",
192-
e.getRequiredSender().getSimpleName()
193-
)));
194-
commandManager.registerExceptionHandler(NoPermissionException.class, (commander, e) ->
195-
commander.print(Caption.of(
187+
ctx.exception().getRequiredSender().getSimpleName()
188+
))
189+
).registerHandler(NoPermissionException.class, ctx ->
190+
ctx.context().sender().print(Caption.of(
196191
"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(
192+
ctx.exception().missingPermission()
193+
))
194+
).registerHandler(
195+
ArgumentParseException.class,
196+
ExceptionHandler.unwrappingHandler()
197+
).registerHandler(VoxelCommandElementParseException.class, ctx ->
198+
ctx.context().sender().print(ctx.exception().getErrorMessage())
199+
).registerHandler(EnumParser.EnumParseException.class, ctx ->
200+
ctx.context().sender().print(Caption.of(
206201
"voxelsniper.command.invalid-enum",
207-
ee.getInput(),
202+
ctx.exception().getInput(),
208203
VoxelSniperText.formatList(
209-
Arrays.stream(ee.getEnumClass().getEnumConstants()).toList(),
204+
Arrays.stream(ctx.exception().getEnumClass().getEnumConstants()).toList(),
210205
(value, value2) -> Integer.compare(value.ordinal(), value2.ordinal()),
211206
value -> TextComponent.of(value.name().toLowerCase(Locale.ROOT)),
212207
"voxelsniper.command.invalid-enum"
213208
)
214-
));
215-
} else if (t instanceof ParserException pe) {
216-
commander.print(Caption.of(
217-
pe.errorCaption()
218-
.getKey()
209+
))
210+
).registerHandler(ParserException.class, ctx ->
211+
ctx.context().sender().print(Caption.of(
212+
ctx.exception().errorCaption()
213+
.key()
219214
.replace("argument.parse.failure.", "voxelsniper.command.invalid-"),
220-
Arrays.stream(pe.captionVariables())
221-
.map(CaptionVariable::getValue)
215+
Arrays.stream(ctx.exception().captionVariables())
216+
.map(CaptionVariable::value)
222217
.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();
218+
))
219+
).registerHandler(Throwable.class, ctx -> {
220+
ctx.context().sender().print(Caption.of("voxelsniper.error.unexpected"));
221+
ctx.exception().printStackTrace();
232222
});
233223

234224
return commandManager;
@@ -238,11 +228,7 @@ private AnnotationParser<SniperCommander> createAnnotationParser(BukkitCommandMa
238228
// Creates the annotation parser.
239229
AnnotationParser<SniperCommander> annotationParser = new AnnotationParser<>(
240230
commandManager,
241-
SniperCommander.class,
242-
parserParameters -> CommandMeta.simple()
243-
.with(CommandMeta.DESCRIPTION, parserParameters
244-
.get(StandardParameters.DESCRIPTION, NO_DESCRIPTION))
245-
.build()
231+
SniperCommander.class
246232
);
247233

248234
// Handles the custom annotations.
@@ -290,7 +276,7 @@ private void handleDynamicRanges(
290276
MethodCommandExecutionHandler<SniperCommander> handler,
291277
Object instance
292278
) {
293-
SniperCommander commander = commandContext.getSender();
279+
SniperCommander commander = commandContext.sender();
294280
MethodCommandExecutionHandler.CommandMethodContext<SniperCommander> methodContext = handler.context();
295281

296282
// Dynamic range is based on executor instance fields, we must postprocess them manually.
@@ -310,9 +296,7 @@ private void handleDynamicRanges(
310296
argumentName = this.annotationParser.processString(argumentAnnotation.value());
311297
}
312298

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

317301
DynamicRange dynamicRangeAnnotation = parameter.getAnnotation(DynamicRange.class);
318302
String min = dynamicRangeAnnotation.min();
@@ -444,12 +428,8 @@ public AnnotationParser<SniperCommander> getAnnotationParser() {
444428
return annotationParser;
445429
}
446430

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);
431+
private static <T> CloudKey<T> createCloudKey(String id, Class<T> clazz) {
432+
return CloudKey.of("voxelsniper-" + id, TypeToken.get(clazz));
453433
}
454434

455435
}

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
));

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

Lines changed: 7 additions & 9 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.registry.Keyed;
@@ -9,9 +10,8 @@
910
import com.thevoxelbox.voxelsniper.command.VoxelCommandElement;
1011
import com.thevoxelbox.voxelsniper.sniper.SniperCommander;
1112

12-
import java.util.List;
1313
import java.util.Locale;
14-
import java.util.Queue;
14+
import java.util.stream.Stream;
1515

1616
public abstract class AbstractRegistryArgument<T extends Keyed> implements VoxelCommandElement {
1717

@@ -33,17 +33,16 @@ public AbstractRegistryArgument(VoxelSniperPlugin plugin, NamespacedRegistry<T>
3333
this.parseExceptionCaptionKey = parseExceptionCaptionKey;
3434
}
3535

36-
protected List<String> suggestValues(CommandContext<SniperCommander> commandContext, String input) {
37-
return registry.getSuggestions(input)
38-
.toList();
36+
protected Stream<String> suggestValues(CommandContext<SniperCommander> commandContext, String input) {
37+
return registry.getSuggestions(input);
3938
}
4039

41-
protected T parseValue(CommandContext<SniperCommander> commandContext, Queue<String> inputQueue) {
42-
String input = inputQueue.peek();
43-
if (input == null) {
40+
protected T parseValue(CommandContext<SniperCommander> commandContext, CommandInput commandInput) {
41+
if (commandInput.peekString().isEmpty()) {
4442
throw new NoInputProvidedException(AbstractRegistryArgument.class, commandContext);
4543
}
4644

45+
final String input = commandInput.readString();
4746
T value = registry.get(input.toLowerCase(Locale.ROOT));
4847
if (value == null) {
4948
throw new VoxelCommandElementParseException(input, Caption.of(
@@ -52,7 +51,6 @@ protected T parseValue(CommandContext<SniperCommander> commandContext, Queue<Str
5251
));
5352
}
5453

55-
inputQueue.remove();
5654
return value;
5755
}
5856

0 commit comments

Comments
 (0)