-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement SignedStringMapper (#28)
* Implement signed string mapper on fabric * fix service file name * Fix dependencies * Update for contributor api * fabric: Add signed argument test * remove mavenLocal
- Loading branch information
Showing
9 changed files
with
216 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
cloud-fabric/src/main/java/org/incendo/cloud/fabric/internal/FabricSignedStringFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// | ||
// 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.fabric.internal; | ||
|
||
import net.kyori.adventure.audience.Audience; | ||
import net.kyori.adventure.chat.ChatType; | ||
import net.kyori.adventure.chat.SignedMessage; | ||
import net.kyori.adventure.platform.fabric.impl.NonWrappingComponentSerializer; | ||
import net.kyori.adventure.text.Component; | ||
import net.minecraft.network.chat.PlayerChatMessage; | ||
import org.apiguardian.api.API; | ||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
import org.incendo.cloud.minecraft.modded.internal.ModdedSignedStringMapper; | ||
import org.incendo.cloud.minecraft.signed.SignedString; | ||
|
||
@API(status = API.Status.INTERNAL) | ||
public final class FabricSignedStringFactory implements ModdedSignedStringMapper.SignedStringFactory { | ||
@Override | ||
public SignedString create(final String str, final PlayerChatMessage signedMessage) { | ||
return new SignedStringImpl(str, signedMessage); | ||
} | ||
|
||
private record SignedStringImpl(String string, PlayerChatMessage rawSignedMessage) implements SignedString { | ||
|
||
@Override | ||
public @Nullable SignedMessage signedMessage() { | ||
return cast(this.rawSignedMessage); | ||
} | ||
|
||
@SuppressWarnings("DataFlowIssue") | ||
private static SignedMessage cast(final PlayerChatMessage message) { | ||
return (SignedMessage) ((Object) message); | ||
} | ||
|
||
@Override | ||
public void sendMessage(final Audience audience, final ChatType.Bound chatType, final Component unsigned) { | ||
final net.minecraft.network.chat.Component nativeComponent = NonWrappingComponentSerializer.INSTANCE.serialize(unsigned); | ||
final PlayerChatMessage playerChatMessage = this.rawSignedMessage.withUnsignedContent(nativeComponent); | ||
audience.sendMessage(cast(playerChatMessage), chatType); | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
.../org.incendo.cloud.minecraft.modded.internal.ModdedSignedStringMapper$SignedStringFactory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.incendo.cloud.fabric.internal.FabricSignedStringFactory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
...n/src/main/java/org/incendo/cloud/minecraft/modded/internal/ModdedSignedStringMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// | ||
// 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.modded.internal; | ||
|
||
import io.leangen.geantyref.TypeToken; | ||
import java.util.Map; | ||
import java.util.concurrent.CompletableFuture; | ||
import net.kyori.adventure.util.Services; | ||
import net.minecraft.commands.CommandSigningContext; | ||
import net.minecraft.commands.CommandSourceStack; | ||
import net.minecraft.commands.arguments.MessageArgument; | ||
import net.minecraft.network.chat.PlayerChatMessage; | ||
import org.apiguardian.api.API; | ||
import org.incendo.cloud.CommandManager; | ||
import org.incendo.cloud.brigadier.CloudBrigadierManager; | ||
import org.incendo.cloud.brigadier.parser.WrappedBrigadierParser; | ||
import org.incendo.cloud.context.CommandContext; | ||
import org.incendo.cloud.minecraft.signed.SignedGreedyStringParser; | ||
import org.incendo.cloud.minecraft.signed.SignedString; | ||
import org.incendo.cloud.minecraft.signed.SignedStringMapper; | ||
import org.incendo.cloud.parser.ArgumentParseResult; | ||
|
||
@API(status = API.Status.INTERNAL) | ||
public final class ModdedSignedStringMapper implements SignedStringMapper { | ||
private final SignedStringFactory factory; | ||
|
||
/** | ||
* Creates a new mapper. | ||
*/ | ||
public ModdedSignedStringMapper() { | ||
this.factory = Services.service(SignedStringFactory.class) | ||
.orElseThrow(() -> new IllegalStateException("Could not locate " + SignedStringFactory.class)); | ||
} | ||
|
||
@SuppressWarnings({"unchecked", "rawtypes"}) | ||
@Override | ||
public void registerBrigadier(final CommandManager<?> commandManager, final Object brigManager) { | ||
registerBrigadier((CloudBrigadierManager) brigManager); | ||
} | ||
|
||
private static <C> void registerBrigadier(final CloudBrigadierManager<C, ?> manager) { | ||
manager.registerMapping( | ||
new TypeToken<SignedGreedyStringParser<C>>() {}, | ||
builder -> builder.toConstant(MessageArgument.message()).cloudSuggestions() | ||
); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<ArgumentParseResult<SignedString>> apply( | ||
final CommandContext<?> ctx, | ||
final String str | ||
) { | ||
final CommandSourceStack stack = ctx.get(WrappedBrigadierParser.COMMAND_CONTEXT_BRIGADIER_NATIVE_SENDER); | ||
final Map<String, PlayerChatMessage> signedArgs; | ||
if (stack.getSigningContext() instanceof CommandSigningContext.SignedArguments) { | ||
signedArgs = ((CommandSigningContext.SignedArguments) stack.getSigningContext()).arguments(); | ||
} else { | ||
return ArgumentParseResult.successFuture(SignedString.unsigned(str)); | ||
} | ||
if (signedArgs.size() != 1) { | ||
throw new IllegalStateException(); | ||
} | ||
|
||
return ArgumentParseResult.successFuture( | ||
this.factory.create( | ||
str, | ||
signedArgs.entrySet().iterator().next().getValue() | ||
) | ||
); | ||
} | ||
|
||
public interface SignedStringFactory { | ||
/** | ||
* Creates a signed string. | ||
* | ||
* @param str raw string | ||
* @param signedMessage signed message | ||
* @return new signed string | ||
*/ | ||
SignedString create(String str, PlayerChatMessage signedMessage); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...rc/main/resources/META-INF/services/org.incendo.cloud.minecraft.signed.SignedStringMapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.incendo.cloud.minecraft.modded.internal.ModdedSignedStringMapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters