Skip to content

Commit

Permalink
feat(minecraft-extras): switch examples to compiled snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
Citymonstret committed Feb 4, 2024
1 parent 67d6748 commit d782de9
Show file tree
Hide file tree
Showing 11 changed files with 236 additions and 159 deletions.
6 changes: 3 additions & 3 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ insert_final_newline = true
indent_size = 4

[*.java]
indent_size = 4
indent_size = 2
max_line_length = 130
tab_width = 4
ij_continuation_indent_size = 8
tab_width = 2
ij_continuation_indent_size = 2
ij_formatter_off_tag = @formatter:off
ij_formatter_on_tag = @formatter:on
ij_formatter_tags_enabled = false
Expand Down
1 change: 1 addition & 0 deletions code/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {

dependencies {
implementation(libs.cloud.core)
implementation(libs.cloud.minecraft.extras)
}

indra {
Expand Down
4 changes: 3 additions & 1 deletion code/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
[versions]
indra = "3.1.3"

cloud = "2.0.0-beta.1"
cloud = "2.0.0-SNAPSHOT"
cloudMinecraft = "2.0.0-SNAPSHOT"

[plugins]
indra = { id = "net.kyori.indra", version.ref = "indra" }

[libraries]
cloud-core = { group = "org.incendo", name = "cloud-core", version.ref = "cloud" }
cloud-minecraft-extras = { group = "org.incendo", name = "cloud-minecraft-extras", version.ref = "cloudMinecraft" }
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,31 @@
*/
public class AggregateParserExample {

public void example(final Command.@NonNull Builder<CommandSender> commandBuilder) {
// --8<-- [start:snippet]
final AggregateParser<CommandSender, Location> locationParser = AggregateParser
.<CommandSender>builder()
.withComponent("world", stringParser())
.withComponent("x", integerParser())
.withComponent("y", integerParser())
.withComponent("z", integerParser())
.withMapper(Location.class, (commandContext, aggregateCommandContext) -> {
final String world = aggregateCommandContext.get("world");
final int x = aggregateCommandContext.get("x");
final int y = aggregateCommandContext.get("y");
final int z = aggregateCommandContext.get("z");
return ArgumentParseResult.successFuture(new Location(world, x, y, z));
}).build();
// --8<-- [end:snippet]
public void example(final Command.@NonNull Builder<CommandSender> commandBuilder) {
// --8<-- [start:snippet]
final AggregateParser<CommandSender, Location> locationParser = AggregateParser
.<CommandSender>builder()
.withComponent("world", stringParser())
.withComponent("x", integerParser())
.withComponent("y", integerParser())
.withComponent("z", integerParser())
.withMapper(Location.class, (commandContext, aggregateCommandContext) -> {
final String world = aggregateCommandContext.get("world");
final int x = aggregateCommandContext.get("x");
final int y = aggregateCommandContext.get("y");
final int z = aggregateCommandContext.get("z");
return ArgumentParseResult.successFuture(new Location(world, x, y, z));
}).build();
// --8<-- [end:snippet]
}

public static final class Location {

public Location(final String world, final int x, final int y, final int z) {
}
}

public static final class Location {
public static final class CommandSender {

public Location(final String world, final int x, final int y, final int z) {
}
}

public static final class CommandSender {

}
}
}
26 changes: 13 additions & 13 deletions code/src/main/java/org/incendo/cloud/snippet/EitherExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
*/
public class EitherExample<C> {

public void example(final Command.@NonNull Builder<C> commandBuilder) {
// --8<-- [start:snippet]
commandBuilder.required("either", ArgumentParser.firstOf(integerParser(), booleanParser()))
.handler(context -> {
Either<Integer, Boolean> either = context.get("either");
if (either.primary().isPresent()){
int integer = either.primary().get();
} else {
boolean bool = either.fallback().get();
}
});
// --8<-- [end:snippet]
}
public void example(final Command.@NonNull Builder<C> commandBuilder) {
// --8<-- [start:snippet]
commandBuilder.required("either", ArgumentParser.firstOf(integerParser(), booleanParser()))
.handler(context -> {
Either<Integer, Boolean> either = context.get("either");
if (either.primary().isPresent()) {
int integer = either.primary().get();
} else {
boolean bool = either.fallback().get();
}
});
// --8<-- [end:snippet]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,34 @@

public final class UUIDParseException extends ParserException {

private final String input;

public UUIDParseException(final @NonNull String input, final @NonNull CommandContext<?> context) {
super(
UUIDParser.class,
context,
StandardCaptionKeys.ARGUMENT_PARSE_FAILURE_UUID,
CaptionVariable.of("input", input));
this.input = input;
private final String input;

public UUIDParseException(final @NonNull String input, final @NonNull CommandContext<?> context) {
super(
UUIDParser.class,
context,
StandardCaptionKeys.ARGUMENT_PARSE_FAILURE_UUID,
CaptionVariable.of("input", input)
);
this.input = input;
}

public String input() {
return this.input;
}

public boolean equals(final Object o) {
if (this == o) {
return true;
} else if (o != null && this.getClass() == o.getClass()) {
final UUIDParseException that = (UUIDParseException) o;
return this.input.equals(that.input());
} else {
return false;
}
}

public String input() {
return this.input;
}

public boolean equals(final Object o) {
if (this == o) {
return true;
} else if (o != null && this.getClass() == o.getClass()) {
final UUIDParseException that = (UUIDParseException) o;
return this.input.equals(that.input());
} else {
return false;
}
}

public int hashCode() {
return Objects.hash(new Object[]{this.input});
}
public int hashCode() {
return Objects.hash(new Object[]{this.input});
}
}
26 changes: 13 additions & 13 deletions code/src/main/java/org/incendo/cloud/snippet/UUIDParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
// --8<-- [start:snippet]
public class UUIDParser<C> implements ArgumentParser<C, UUID> {

@Override
public @NonNull ArgumentParseResult<UUID> parse(
@NonNull CommandContext<C> context,
@NonNull CommandInput commandInput
) {
final String input = commandInput.peekString(); // Does not remove the string from the input!
try {
final UUID uuid = UUID.fromString(input);
commandInput.readString(); // Removes the string from the input.
return ArgumentParseResult.success(uuid);
} catch (final IllegalArgumentException e) {
return ArgumentParseResult.failure(new UUIDParseException(input, context));
}
@Override
public @NonNull ArgumentParseResult<UUID> parse(
@NonNull CommandContext<C> context,
@NonNull CommandInput commandInput
) {
final String input = commandInput.peekString(); // Does not remove the string from the input!
try {
final UUID uuid = UUID.fromString(input);
commandInput.readString(); // Removes the string from the input.
return ArgumentParseResult.success(uuid);
} catch (final IllegalArgumentException e) {
return ArgumentParseResult.failure(new UUIDParseException(input, context));
}
}
}
// --8<-- [end:snippet]
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.incendo.cloud.snippet.minecraft;

import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.format.NamedTextColor;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.CommandManager;
import org.incendo.cloud.minecraft.extras.MinecraftExceptionHandler;

import static net.kyori.adventure.text.Component.text;

public class MinecraftExceptionHandlerExample {

public void nativeExceptionHandler() {
// --8<-- [start:native]
MinecraftExceptionHandler.createNative()
.decorator(component -> text()
.append(text("[Example] ", NamedTextColor.DARK_RED))
.append(component)
.build()
);
// --8<-- [end:native]
}

public void completeExample(final @NonNull CommandManager<NativeSenderType> manager) {
// --8<-- [start:complete]
MinecraftExceptionHandler.<NativeSenderType>createNative()
.defaultHandlers()
.decorator(component -> text()
.append(text("[Example] ", NamedTextColor.DARK_RED))
.append(component)
.build()
).registerTo(manager);
// --8<-- [end:complete]
}

public static final class NativeSenderType implements Audience {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package org.incendo.cloud.snippet.minecraft;

import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.format.NamedTextColor;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.CommandManager;
import org.incendo.cloud.component.DefaultValue;
import org.incendo.cloud.help.result.CommandEntry;
import org.incendo.cloud.minecraft.extras.AudienceProvider;
import org.incendo.cloud.minecraft.extras.MinecraftHelp;
import org.incendo.cloud.suggestion.Suggestion;
import org.incendo.cloud.suggestion.SuggestionProvider;

import java.util.stream.Collectors;

import static org.incendo.cloud.parser.standard.StringParser.greedyStringParser;

public class MinecraftHelpExample {

public @NonNull MinecraftHelp<NativeSenderType> nativeHelp(final @NonNull CommandManager<NativeSenderType> commandManager) {
// --8<-- [start:native]
MinecraftHelp<NativeSenderType> help = MinecraftHelp.<NativeSenderType>builder()
.commandManager(commandManager)
.audienceProvider(AudienceProvider.nativeAudience())
.commandPrefix("/helpcommand")
.colors(MinecraftHelp.helpColors(NamedTextColor.GREEN, NamedTextColor.RED,
NamedTextColor.AQUA, NamedTextColor.BLACK, NamedTextColor.WHITE
))
/* other settings... */
.build();
// --8<-- [end:native]
return help;
}

public void nonNativeHelp(final @NonNull CommandManager<SenderType> commandManager) {
// --8<-- [start:non_native]
AudienceProvider<SenderType> audienceProvider = SenderType::audience;
MinecraftHelp<SenderType> help = MinecraftHelp.<SenderType>builder()
.commandManager(commandManager)
.audienceProvider(audienceProvider)
.commandPrefix("/helpcommand")
.colors(MinecraftHelp.helpColors(NamedTextColor.GREEN, NamedTextColor.RED,
NamedTextColor.AQUA, NamedTextColor.BLACK, NamedTextColor.WHITE
))
/* other settings... */
.build();
// --8<-- [end:non_native]
}

public void helpCommand(final @NonNull CommandManager<NativeSenderType> commandManager) {
final MinecraftHelp<NativeSenderType> help = this.nativeHelp(commandManager);
// --8<-- [start:help_command]
commandManager.command(
commandManager.commandBuilder("helpcommand")
.optional("query", greedyStringParser(), DefaultValue.constant(""))
.handler(context -> {
help.queryCommands(context.get("query"), context.sender());
})
);
// --8<-- [end:help_command]
commandManager.command(
commandManager.commandBuilder("helpcommand")
// --8<-- [start:help_suggestions]
.optional(
"query",
greedyStringParser(),
DefaultValue.constant(""),
SuggestionProvider.blocking((ctx, in) -> commandManager.createHelpHandler()
.queryRootIndex(ctx.sender())
.entries()
.stream()
.map(CommandEntry::syntax)
.map(Suggestion::simple)
.collect(Collectors.toList())
)
)
// --8<-- [end:help_suggestions]
.handler(context -> {
help.queryCommands(context.get("query"), context.sender());
})
);
}

public static final class NativeSenderType implements Audience {

}

public static abstract class SenderType {

public abstract @NonNull Audience audience();
}
}
Loading

0 comments on commit d782de9

Please sign in to comment.