Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: SignedGreedyStringParser #50

Merged
merged 6 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions cloud-minecraft-signed-arguments/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
plugins {
id("conventions.base")
id("conventions.publishing")
}

dependencies {
api(libs.cloud.core)
compileOnly(libs.brigadier)
compileOnlyApi(libs.adventureApi)
api(projects.cloudBrigadier)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// MIT License
//
// Copyright (c) 2024 Incendo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package org.incendo.cloud.minecraft.signed;

import com.mojang.brigadier.arguments.StringArgumentType;
import io.leangen.geantyref.TypeToken;
import org.apiguardian.api.API;
import org.incendo.cloud.brigadier.BrigadierManagerHolder;
import org.incendo.cloud.brigadier.CloudBrigadierManager;

@API(status = API.Status.INTERNAL)
public final class SignedArguments {
private SignedArguments() {
}

/**
* Returns whether Adventure is present.
*
* @return whether Adventure is present
*/
public static boolean adventurePresent() {
try {
Class.forName("net.kyori.adventure.chat.SignedMessage");
return true;
} catch (final ClassNotFoundException ignore) {
return false;
}
}

/**
* Registers the default Brigadier mapping for {@link SignedGreedyStringParser} if the manager
* is a {@link BrigadierManagerHolder}.
*
* @param brigadierManager command manager
* @param <C> command sender type
*/
@SuppressWarnings("unchecked")
public static <C> void registerDefaultBrigadierMapping(final Object brigadierManager) {
((CloudBrigadierManager<C, ?>) brigadierManager).registerMapping(
new TypeToken<SignedGreedyStringParser<C>>() {},
builder -> builder.toConstant(StringArgumentType.greedyString()).cloudSuggestions()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// MIT License
//
// Copyright (c) 2024 Incendo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package org.incendo.cloud.minecraft.signed;

import org.apiguardian.api.API;
import org.incendo.cloud.CommandManager;
import org.incendo.cloud.brigadier.CloudBrigadierManager;
import org.incendo.cloud.brigadier.argument.BrigadierMappingContributor;

@API(status = API.Status.INTERNAL)
public final class SignedBrigadierContributor implements BrigadierMappingContributor {
@Override
public <C, S> void contribute(
final CommandManager<C> commandManager,
final CloudBrigadierManager<C, S> brigadierManager
) {
if (SignedArguments.adventurePresent()) {
SignedStringMapper.get().registerBrigadier(commandManager, brigadierManager);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//
// MIT License
//
// Copyright (c) 2024 Incendo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package org.incendo.cloud.minecraft.signed;

import io.leangen.geantyref.TypeToken;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiFunction;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.incendo.cloud.context.CommandContext;
import org.incendo.cloud.context.CommandInput;
import org.incendo.cloud.parser.ArgumentParseResult;
import org.incendo.cloud.parser.ArgumentParser;
import org.incendo.cloud.parser.ParserDescriptor;
import org.incendo.cloud.parser.standard.StringParser;

/**
* Parser for signed greedy strings.
*
* @param <C> command sender type
*/
@DefaultQualifier(NonNull.class)
public final class SignedGreedyStringParser<C> implements
ArgumentParser.FutureArgumentParser<C, SignedString>,
ParserDescriptor<C, SignedString> {

private final ArgumentParser<C, SignedString> wrapped;

/**
* Creates a {@link SignedGreedyStringParser} with the specified {@link SignedStringMapper mapper}.
*
* @param <C> command sender type
* @param mapper signed string mapper
* @return parser
*/
public static <C> SignedGreedyStringParser<C> signedGreedyStringParser(final SignedStringMapper mapper) {
return new SignedGreedyStringParser<>(mapper);
}

/**
* Creates a {@link SignedGreedyStringParser} with the default {@link SignedStringMapper mapper}.
*
* @param <C> command sender type
* @return parser
*/
public static <C> SignedGreedyStringParser<C> signedGreedyStringParser() {
return signedGreedyStringParser(SignedStringMapper.get());
}

@SuppressWarnings({"unchecked", "rawtypes"})
private SignedGreedyStringParser(final SignedStringMapper mapper) {
this.wrapped = StringParser.<C>greedyStringParser().parser().flatMapSuccess((BiFunction) mapper);
}

@Override
public CompletableFuture<ArgumentParseResult<SignedString>> parseFuture(
final CommandContext<C> commandContext,
final CommandInput commandInput
) {
return this.wrapped.parseFuture(commandContext, commandInput);
}

@Override
public ArgumentParser<C, SignedString> parser() {
return this;
}

@Override
public TypeToken<SignedString> valueType() {
return TypeToken.get(SignedString.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// MIT License
//
// Copyright (c) 2024 Incendo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package org.incendo.cloud.minecraft.signed;

import org.apiguardian.api.API;
import org.incendo.cloud.parser.ParserContributor;
import org.incendo.cloud.parser.ParserRegistry;

@API(status = API.Status.INTERNAL)
public final class SignedParserContributor implements ParserContributor {
@Override
public <C> void contribute(final ParserRegistry<C> registry) {
if (SignedArguments.adventurePresent()) {
SignedStringMapper.get().registerParser(registry);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//
// MIT License
//
// Copyright (c) 2024 Incendo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package org.incendo.cloud.minecraft.signed;

import java.util.Objects;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.chat.ChatType;
import net.kyori.adventure.chat.SignedMessage;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;

@DefaultQualifier(NonNull.class)
public interface SignedString {
/**
* Returns the raw string message.
*
* @return raw string message
*/
String string();

/**
* Returns the signed message if the argument was actually signed by the client, otherwise {@code null}.
*
* @return signed message
*/
@Nullable SignedMessage signedMessage();

/**
* Sends the signed message with the provided unsigned content.
*
* <p>If the message is not signed, it will be sent as a system message.</p>
*
* @param audience audience
* @param chatType chat type, for if the message is signed
* @param unsigned unsigned content
*/
void sendMessage(Audience audience, ChatType.Bound chatType, Component unsigned);

/**
* Sends the signed message with the provided unsigned content.
*
* <p>If the message is not signed, it will be sent as a system message.</p>
*
* @param audience audience
* @param chatType chat type, for if the message is signed
* @param unsigned unsigned content
*/
default void sendMessage(final Audience audience, final ChatType chatType, final Component unsigned) {
this.sendMessage(audience, chatType.bind(unsigned), unsigned);
}

/**
* Creates a new unsigned string wrapping {@code message}.
*
* @param message raw string message
* @return unsigned string
*/
static SignedString unsigned(final String message) {
return new Unsigned(message);
}

final class Unsigned implements SignedString {
private final String string;

private Unsigned(final String string) {
this.string = Objects.requireNonNull(string, "string");
}

@Override
public @Nullable SignedMessage signedMessage() {
return null;
}

@Override
public void sendMessage(final Audience audience, final ChatType.Bound chatType, final Component unsigned) {
audience.sendMessage(unsigned);
}

@Override
public String string() {
return this.string;
}
}
}
Loading