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

adventure-platform-mod 6.x #68

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion cloud-fabric/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ dependencies {
api(libs.cloud.brigadier)

compileOnly(libs.cloud.minecraft.signed.arguments)
modCompileOnly(libs.adventureFabric)

offlineLinkedJavadoc(project(":cloud-minecraft-modded-common"))
localRuntime(project(":cloud-minecraft-modded-common", configuration = "namedElements"))
Expand Down Expand Up @@ -92,9 +91,16 @@ val testmod: SourceSet by sourceSets.creating {
dependencies.add(implementationConfigurationName, main.output)
}

loom {
createRemapConfigurations(testmod)
}

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,58 @@
//
// 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 java.util.Objects;
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 {

@SuppressWarnings("EmptyCatch")
@Override
public void onInitialize() {
if (FabricLoader.getInstance().isModLoaded("adventure-platform-fabric")) {
try {
Objects.requireNonNull(
Class.forName("net.kyori.adventure.platform.modcommon.MinecraftAudiences").getName()
);

ServerLifecycleEvents.SERVER_STARTING.register(AdventureSupport.get()::setupServer);
ServerLifecycleEvents.SERVER_STOPPED.register(AdventureSupport.get()::removeServer);
if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) {
AdventureSupport.get().setupClient();
}
} catch (final ClassNotFoundException ignored) {
}
}
}
}

This file was deleted.

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,16 +27,18 @@
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;
import net.fabricmc.loader.api.metadata.Person;
import net.kyori.adventure.chat.ChatType;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.platform.fabric.AdventureCommandSourceStack;
import net.kyori.adventure.platform.fabric.FabricServerAudiences;
import net.kyori.adventure.platform.modcommon.AdventureCommandSourceStack;
import net.kyori.adventure.platform.modcommon.MinecraftServerAudiences;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.minecraft.ChatFormatting;
import net.minecraft.commands.CommandSourceStack;
Expand All @@ -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 All @@ -281,7 +289,7 @@ public void onInitialize() {
.required("message", signedGreedyStringParser())
.handler(ctx -> {
final AdventureCommandSourceStack audience =
FabricServerAudiences.of(ctx.sender().getServer()).audience(ctx.sender());
MinecraftServerAudiences.of(ctx.sender().getServer()).audience(ctx.sender());

final SignedString message = ctx.get("message");

Expand Down
3 changes: 3 additions & 0 deletions cloud-minecraft-modded-common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ dependencies {
minecraft(libs.minecraft)
mappings(loom.officialMojangMappings())
compileOnly(libs.fabricLoader)

compileOnly(libs.adventureMod)
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) {
// cloud-minecraft-extras not present
}
}

/**
* 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");
}
return 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 @@ -21,21 +21,30 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package org.incendo.cloud.fabric.internal;
package org.incendo.cloud.minecraft.modded.internal;

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.platform.fabric.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;
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 {
public final class ModdedSignedStringFactory implements ModdedSignedStringMapper.SignedStringFactory {

/**
* Creates a new {@link ModdedSignedStringFactory}.
*/
public ModdedSignedStringFactory() {
// Trigger service load failure when this isn't present
Objects.requireNonNull(MinecraftAudiences.class.getName());
}

@Override
public SignedString create(final String str, final PlayerChatMessage signedMessage) {
return new SignedStringImpl(str, signedMessage);
Expand All @@ -55,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
Loading