Skip to content

Commit

Permalink
Implement ComponentMessageThrowable conversions and fix registry awar…
Browse files Browse the repository at this point in the history
…e signed string conversions
  • Loading branch information
jpenilla committed Nov 30, 2024
1 parent 80a86dd commit 296b91a
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 9 deletions.
2 changes: 2 additions & 0 deletions cloud-fabric/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ dependencies {
localRuntime(libs.cloud.minecraft.signed.arguments)
modLocalRuntime(libs.adventureFabric)
"modTestmodImplementation"(libs.adventureFabric)
"testmodImplementation"(libs.cloud.minecraft.extras)
localRuntime(libs.cloud.minecraft.extras)
}

val testmodJar by tasks.registering(Jar::class) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// 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.fabricmc.api.EnvType;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.loader.api.FabricLoader;
import org.apiguardian.api.API;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.incendo.cloud.minecraft.modded.internal.AdventureSupport;

@API(status = API.Status.INTERNAL)
@DefaultQualifier(NonNull.class)
public final class CloudFabricEntrypoint implements ModInitializer {

@Override
public void onInitialize() {
if (FabricLoader.getInstance().isModLoaded("adventure-platform-fabric")) {
ServerLifecycleEvents.SERVER_STARTING.register(AdventureSupport.get()::setupServer);
ServerLifecycleEvents.SERVER_STOPPED.register(AdventureSupport.get()::removeServer);
if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) {
AdventureSupport.get().setupClient();
}
}
}
}
3 changes: 2 additions & 1 deletion cloud-fabric/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

"entrypoints": {
"main": [
"org.incendo.cloud.fabric.internal.LateRegistrationCatcher"
"org.incendo.cloud.fabric.internal.LateRegistrationCatcher",
"org.incendo.cloud.fabric.internal.CloudFabricEntrypoint"
]
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import net.fabricmc.loader.api.metadata.ModMetadata;
Expand Down Expand Up @@ -61,9 +63,9 @@
import org.incendo.cloud.fabric.FabricServerCommandManager;
import org.incendo.cloud.fabric.testmod.mixin.GiveCommandAccess;
import org.incendo.cloud.key.CloudKey;
import org.incendo.cloud.minecraft.extras.MinecraftExceptionHandler;
import org.incendo.cloud.minecraft.modded.data.Coordinates;
import org.incendo.cloud.minecraft.modded.data.Coordinates.ColumnCoordinates;
import org.incendo.cloud.minecraft.modded.data.MultipleEntitySelector;
import org.incendo.cloud.minecraft.modded.data.MultiplePlayerSelector;
import org.incendo.cloud.minecraft.modded.parser.NamedColorParser;
import org.incendo.cloud.minecraft.modded.parser.RegistryEntryParser;
Expand All @@ -89,6 +91,11 @@ public void onInitialize() {
final FabricServerCommandManager<CommandSourceStack> manager =
FabricServerCommandManager.createNative(ExecutionCoordinator.simpleCoordinator());

AtomicReference<MinecraftServerAudiences> audiences = new AtomicReference<>();
ServerLifecycleEvents.SERVER_STARTING.register((server) -> audiences.set(MinecraftServerAudiences.of(server)));
MinecraftExceptionHandler.<CommandSourceStack>create(stack -> audiences.get().audience(stack))
.registerTo(manager);

final Command.Builder<CommandSourceStack> base = manager.commandBuilder("cloudtest");

final CloudKey<String> name = CloudKey.of("name", String.class);
Expand Down Expand Up @@ -224,15 +231,16 @@ public void onInitialize() {
.map(Suggestion::suggestion)
.collect(Collectors.toList())))
.parser((commandContext, commandInput) -> {
final ModMetadata meta = FabricLoader.getInstance().getModContainer(commandInput.readString())
final String s = commandInput.readString();
final ModMetadata meta = FabricLoader.getInstance().getModContainer(s)
.map(ModContainer::getMetadata)
.orElse(null);
if (meta != null) {
return ArgumentParseResult.success(meta);
}
return ArgumentParseResult.failure(new IllegalArgumentException(String.format(
"No mod with id '%s'",
commandInput.peek()
s
)));
})
.build();
Expand Down Expand Up @@ -269,7 +277,7 @@ public void onInitialize() {
.required("targets", multiplePlayerSelectorParser())
.required("location", vec3Parser(false))
.handler(ctx -> {
final MultipleEntitySelector selector = ctx.get("targets");
final MultiplePlayerSelector selector = ctx.get("targets");
final Vec3 location = ctx.<Coordinates>get("location").position();
selector.values().forEach(target -> {
target.placePortalTicket(new BlockPos(Mth.floor(location.x()), Mth.floor(location.y()), Mth.floor(location.z())));
Expand Down
2 changes: 2 additions & 0 deletions cloud-minecraft-modded-common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ dependencies {
compileOnly(libs.fabricLoader)

modCompileOnly(libs.adventureFabric)

compileOnly(libs.cloud.minecraft.extras)
}

tasks.withType(AbstractRemapJarTask::class).configureEach {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
//
// 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 java.util.Objects;
import net.kyori.adventure.platform.modcommon.MinecraftAudiences;
import net.kyori.adventure.platform.modcommon.MinecraftClientAudiences;
import net.kyori.adventure.platform.modcommon.MinecraftServerAudiences;
import net.minecraft.server.MinecraftServer;
import org.apiguardian.api.API;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;

@API(status = API.Status.INTERNAL)
@DefaultQualifier(NonNull.class)
public final class AdventureSupport {
private static final AdventureSupport INSTANCE;

static {
INSTANCE = new AdventureSupport();
INSTANCE.setupConverter();
}

private @Nullable MinecraftAudiences client;
private @Nullable MinecraftAudiences server;

private AdventureSupport() {
}

@SuppressWarnings("EmptyCatch")
private void setupConverter() {
try {
ComponentMessageThrowableConverter.setup(this);
} catch (final LinkageError ignored) {
}
}

/**
* Set up the client audience.
*/
public void setupClient() {
this.client = MinecraftClientAudiences.of();
}

/**
* Set up the server audience.
*
* @param server server
*/
public void setupServer(final MinecraftServer server) {
this.server = MinecraftServerAudiences.of(server);
}

/**
* Shutdown the server audience.
*
* @param server server
*/
@SuppressWarnings("unused")
public void removeServer(final MinecraftServer server) {
this.server = null;
}

/**
* Get the MinecraftAudiences instance.
*
* @return audiences
*/
public MinecraftAudiences audiences() {
final @Nullable MinecraftAudiences server = this.server;
if (server == null) {
return Objects.requireNonNull(this.client, "No audiences present");
} else {
return this.server;
}
}

/**
* Returns the AdventureSupport instance.
*
* @return the instance
*/
public static AdventureSupport get() {
return INSTANCE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// 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 com.mojang.brigadier.exceptions.CommandSyntaxException;
import org.apiguardian.api.API;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.incendo.cloud.minecraft.extras.MinecraftExceptionHandler;

@API(status = API.Status.INTERNAL)
@DefaultQualifier(NonNull.class)
final class ComponentMessageThrowableConverter implements MinecraftExceptionHandler.ComponentMessageThrowableConverter {

private final AdventureSupport adventureSupport;

ComponentMessageThrowableConverter(final AdventureSupport adventureSupport) {
this.adventureSupport = adventureSupport;
}

static void setup(final AdventureSupport adventureSupport) {
MinecraftExceptionHandler.ComponentMessageThrowableConverterHolder.converter(
new ComponentMessageThrowableConverter(adventureSupport)
);
}

@Override
public Throwable maybeConvert(final Throwable thr) {
if (thr instanceof CommandSyntaxException cse) {
return (Exception) this.adventureSupport.audiences().asComponentThrowable(cse);
}
return thr;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.chat.ChatType;
import net.kyori.adventure.chat.SignedMessage;
import net.kyori.adventure.platform.modcommon.impl.NonWrappingComponentSerializer;
import net.kyori.adventure.platform.modcommon.MinecraftAudiences;
import net.kyori.adventure.text.Component;
import net.minecraft.network.chat.PlayerChatMessage;
import org.apiguardian.api.API;
Expand All @@ -42,7 +42,7 @@ public final class ModdedSignedStringFactory implements ModdedSignedStringMapper
*/
public ModdedSignedStringFactory() {
// Trigger service load failure when this isn't present
Objects.requireNonNull(NonWrappingComponentSerializer.class.getName());
Objects.requireNonNull(MinecraftAudiences.class.getName());
}

@Override
Expand All @@ -64,7 +64,8 @@ private static SignedMessage cast(final PlayerChatMessage 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 net.minecraft.network.chat.Component nativeComponent =
AdventureSupport.get().audiences().asNative(unsigned);
final PlayerChatMessage playerChatMessage = this.rawSignedMessage.withUnsignedContent(nativeComponent);
audience.sendMessage(cast(playerChatMessage), chatType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.neoforged.bus.api.EventPriority;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.ModList;
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.server.ServerStartingEvent;
import net.neoforged.neoforge.event.server.ServerStoppedEvent;
import net.neoforged.neoforge.server.permission.events.PermissionGatherEvent;
import net.neoforged.neoforge.server.permission.nodes.PermissionDynamicContext;
import net.neoforged.neoforge.server.permission.nodes.PermissionNode;
Expand All @@ -43,6 +47,7 @@
import org.incendo.cloud.Command;
import org.incendo.cloud.execution.ExecutionCoordinator;
import org.incendo.cloud.internal.CommandNode;
import org.incendo.cloud.minecraft.modded.internal.AdventureSupport;
import org.incendo.cloud.permission.AndPermission;
import org.incendo.cloud.permission.OrPermission;
import org.incendo.cloud.permission.Permission;
Expand All @@ -58,8 +63,22 @@ public final class CloudNeoForgeEntrypoint {

/**
* Creates a {@link CloudNeoForgeEntrypoint}.
*
* @param modBus mod event bus
*/
public CloudNeoForgeEntrypoint() {
public CloudNeoForgeEntrypoint(final IEventBus modBus) {
if (ModList.get().isLoaded("adventure_platform_neoforge")) {
NeoForge.EVENT_BUS.addListener(EventPriority.HIGHEST, (ServerStartingEvent event) -> {
AdventureSupport.get().setupServer(event.getServer());
});
NeoForge.EVENT_BUS.addListener((ServerStoppedEvent event) -> {
AdventureSupport.get().removeServer(event.getServer());
});
modBus.addListener(FMLClientSetupEvent.class, event -> {
AdventureSupport.get().setupClient();
});
}

NeoForge.EVENT_BUS.addListener(EventPriority.HIGHEST, (ServerStartingEvent event) -> serverStartingCalled = true);
NeoForge.EVENT_BUS.addListener(EventPriority.LOW, CloudNeoForgeEntrypoint::registerPermissions);

Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ cloud-minecraft-bom = { module = "org.incendo:cloud-minecraft-bom", version.ref
cloud-core = { module = "org.incendo:cloud-core", version.ref = "cloud" }
cloud-brigadier = { module = "org.incendo:cloud-brigadier", version.ref = "cloudMinecraft" }
cloud-minecraft-signed-arguments = { module = "org.incendo:cloud-minecraft-signed-arguments", version.ref = "cloudMinecraft" }
cloud-minecraft-extras = { module = "org.incendo:cloud-minecraft-extras", version.ref = "cloudMinecraft" }
adventureApi = { group = "net.kyori", name = "adventure-api", version = "4.15.0" }
adventureMod = { module = "net.kyori:adventure-platform-mod-shared", version.ref = "adventureMod" }
adventureFabric = { module = "net.kyori:adventure-platform-fabric", version.ref = "adventureMod" }
Expand Down

0 comments on commit 296b91a

Please sign in to comment.