diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/CustomClickActionEvents.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/CustomClickActionEvents.java new file mode 100644 index 0000000000..ce458046e8 --- /dev/null +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/CustomClickActionEvents.java @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.fabricmc.fabric.api.networking.v1; + +import java.util.Objects; +import java.util.Optional; + +import net.minecraft.nbt.NbtElement; +import net.minecraft.util.Identifier; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.impl.networking.CustomClickActionsRegistry; + +/** + * Events for listening to {@linkplain net.minecraft.text.ClickEvent.Custom custom click actions}, such as those invoked + * from a custom dialog. + */ +public final class CustomClickActionEvents { + /** + * Gets an event that is invoked on the server when a custom click event is received. The returned event will only + * be invoked when a click event is received with the given ID. + * + * @param id The of the ID click event to listen to. + * @return Returns an event that will be invoked when a click event with the given ID is received during the PLAY + * phase. + */ + public static Event customClickActionReceivedEvent(Identifier id) { + Objects.requireNonNull(id, "ID cannot be null"); + return CustomClickActionsRegistry.getOrCreateActionEvent(id); + } + + @FunctionalInterface + public interface CustomClickActionReceived { + /** + * Handles a custom click event on the server from a given context. + * + * @param context The context of the event, contains the handler responsible for the action and the payload. + * Will either be an instance of {@link CustomClickEventContext.Play} or + * {@link CustomClickEventContext.Configuration}, depending on when this event was invoked. This + * can be checked using switch-statement pattern matching (see testmod if unfamiliar with this + * syntax). + * @param payload The payload received with this event. If no payload is received, then this payload will be + * empty. + */ + void handleCustomClickAction(CustomClickEventContext context, Optional payload); + } + + private CustomClickActionEvents() { + } +} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/CustomClickEventContext.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/CustomClickEventContext.java new file mode 100644 index 0000000000..a8aa17a3f6 --- /dev/null +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/CustomClickEventContext.java @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.fabricmc.fabric.api.networking.v1; + +import org.jetbrains.annotations.ApiStatus; + +import net.minecraft.server.network.ServerCommonNetworkHandler; +import net.minecraft.server.network.ServerConfigurationNetworkHandler; +import net.minecraft.server.network.ServerPlayNetworkHandler; +import net.minecraft.server.network.ServerPlayerEntity; + +/** + * Contains data about a {@linkplain net.minecraft.text.ClickEvent.Custom custom click event} when one is received on + * the server. Custom click events may be received either during the PLAY or in CONFIGURATION phases. + */ +public sealed interface CustomClickEventContext permits CustomClickEventContext.Play, CustomClickEventContext.Configuration { + /** + * The handler responsible for the event. + */ + ServerCommonNetworkHandler handler(); + + /** + * The context data when a custom click event is received during the PLAY phase on the server. + */ + @ApiStatus.NonExtendable + non-sealed interface Play extends CustomClickEventContext { + /** + * The play handler responsible for the event. + */ + @Override + ServerPlayNetworkHandler handler(); + + /** + * The player responsible for the event. + * + * @return Returns exactly the same player as calling {@link ServerPlayNetworkHandler#getPlayer()} on the result + * of {@link #handler()}. + */ + default ServerPlayerEntity player() { + return handler().getPlayer(); + } + } + + /** + * The context data when a custom click event is received during the CONFIGURATION phase on the server. + */ + @ApiStatus.NonExtendable + non-sealed interface Configuration extends CustomClickEventContext { + /** + * The configuration handler responsible for the event. + */ + @Override + ServerConfigurationNetworkHandler handler(); + } +} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/AbstractNetworkAddon.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/AbstractNetworkAddon.java index 3ce0a675c9..9930ba262d 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/AbstractNetworkAddon.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/AbstractNetworkAddon.java @@ -20,6 +20,7 @@ import java.util.HashSet; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.locks.Lock; @@ -30,6 +31,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import net.minecraft.nbt.NbtElement; import net.minecraft.util.Identifier; /** @@ -172,4 +174,7 @@ public final void handleDisconnect() { * @return whether the channel is reserved */ protected abstract boolean isReservedChannel(Identifier channelName); + + public void invokeCustomClickActionEvent(Identifier id, Optional payload) { + } } diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CustomClickActionsRegistry.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CustomClickActionsRegistry.java new file mode 100644 index 0000000000..719d47ed60 --- /dev/null +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CustomClickActionsRegistry.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.fabricmc.fabric.impl.networking; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +import net.minecraft.nbt.NbtElement; +import net.minecraft.util.Identifier; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.event.EventFactory; +import net.fabricmc.fabric.api.networking.v1.CustomClickActionEvents; +import net.fabricmc.fabric.api.networking.v1.CustomClickEventContext; + +public final class CustomClickActionsRegistry { + private static final Map> REGISTRY = new HashMap<>(); + + public static Event getOrCreateActionEvent(Identifier id) { + return REGISTRY.computeIfAbsent(id, idx -> createNewEvent()); + } + + public static void invokeListenerEvent(Identifier id, CustomClickEventContext context, Optional payload) { + Event event = REGISTRY.get(id); + + if (event != null) { + event.invoker().handleCustomClickAction(context, payload); + } + } + + private static Event createNewEvent() { + return EventFactory.createArrayBacked( + CustomClickActionEvents.CustomClickActionReceived.class, + listeners -> (context, payload) -> { + for (CustomClickActionEvents.CustomClickActionReceived listener : listeners) { + listener.handleCustomClickAction(context, payload); + } + } + ); + } + + private CustomClickActionsRegistry() { + } +} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerConfigurationNetworkAddon.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerConfigurationNetworkAddon.java index a61ef7e613..d4e35074c7 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerConfigurationNetworkAddon.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerConfigurationNetworkAddon.java @@ -19,10 +19,12 @@ import java.util.Collections; import java.util.List; import java.util.Objects; +import java.util.Optional; import io.netty.channel.ChannelFutureListener; import org.jetbrains.annotations.Nullable; +import net.minecraft.nbt.NbtElement; import net.minecraft.network.NetworkPhase; import net.minecraft.network.packet.BrandCustomPayload; import net.minecraft.network.packet.CustomPayload; @@ -32,12 +34,14 @@ import net.minecraft.server.network.ServerConfigurationNetworkHandler; import net.minecraft.util.Identifier; +import net.fabricmc.fabric.api.networking.v1.CustomClickEventContext; import net.fabricmc.fabric.api.networking.v1.PacketSender; import net.fabricmc.fabric.api.networking.v1.S2CConfigurationChannelEvents; import net.fabricmc.fabric.api.networking.v1.ServerConfigurationConnectionEvents; import net.fabricmc.fabric.api.networking.v1.ServerConfigurationNetworking; import net.fabricmc.fabric.impl.networking.AbstractChanneledNetworkAddon; import net.fabricmc.fabric.impl.networking.ChannelInfoHolder; +import net.fabricmc.fabric.impl.networking.CustomClickActionsRegistry; import net.fabricmc.fabric.impl.networking.NetworkingImpl; import net.fabricmc.fabric.impl.networking.RegistrationPayload; import net.fabricmc.fabric.mixin.networking.accessor.ServerCommonNetworkHandlerAccessor; @@ -197,6 +201,12 @@ public void setReconfiguring() { isReconfiguring = true; } + @Override + public void invokeCustomClickActionEvent(Identifier id, Optional payload) { + CustomClickEventContext context = new ConfigurationContextImpl(this.handler); + CustomClickActionsRegistry.invokeListenerEvent(id, context, payload); + } + private enum RegisterState { NOT_SENT, SENT, @@ -215,4 +225,7 @@ private record ContextImpl(MinecraftServer server, ServerConfigurationNetworkHan Objects.requireNonNull(responseSender, "responseSender"); } } + + private record ConfigurationContextImpl(ServerConfigurationNetworkHandler handler) implements CustomClickEventContext.Configuration { + } } diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerPlayNetworkAddon.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerPlayNetworkAddon.java index ed5ef1be72..1a4ade3775 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerPlayNetworkAddon.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/server/ServerPlayNetworkAddon.java @@ -19,7 +19,9 @@ import java.util.Collections; import java.util.List; import java.util.Objects; +import java.util.Optional; +import net.minecraft.nbt.NbtElement; import net.minecraft.network.ClientConnection; import net.minecraft.network.NetworkPhase; import net.minecraft.network.packet.CustomPayload; @@ -29,12 +31,14 @@ import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.util.Identifier; +import net.fabricmc.fabric.api.networking.v1.CustomClickEventContext; import net.fabricmc.fabric.api.networking.v1.PacketSender; import net.fabricmc.fabric.api.networking.v1.S2CPlayChannelEvents; import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; import net.fabricmc.fabric.impl.networking.AbstractChanneledNetworkAddon; import net.fabricmc.fabric.impl.networking.ChannelInfoHolder; +import net.fabricmc.fabric.impl.networking.CustomClickActionsRegistry; import net.fabricmc.fabric.impl.networking.NetworkingImpl; import net.fabricmc.fabric.impl.networking.RegistrationPayload; @@ -144,6 +148,12 @@ public boolean requestedReconfigure() { return requestedReconfigure; } + @Override + public void invokeCustomClickActionEvent(Identifier id, Optional payload) { + CustomClickEventContext context = new PlayContextImpl(this.handler); + CustomClickActionsRegistry.invokeListenerEvent(id, context, payload); + } + private record ContextImpl(MinecraftServer server, ServerPlayNetworkHandler handler, PacketSender responseSender) implements ServerPlayNetworking.Context { private ContextImpl { Objects.requireNonNull(server, "server"); @@ -156,4 +166,7 @@ public ServerPlayerEntity player() { return handler.getPlayer(); } } + + public record PlayContextImpl(ServerPlayNetworkHandler handler) implements CustomClickEventContext.Play { + } } diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/ServerCommonNetworkHandlerMixin.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/ServerCommonNetworkHandlerMixin.java index 18dff3b203..33a31f8bfb 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/ServerCommonNetworkHandlerMixin.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/ServerCommonNetworkHandlerMixin.java @@ -23,6 +23,7 @@ import net.minecraft.network.packet.CustomPayload; import net.minecraft.network.packet.c2s.common.CommonPongC2SPacket; +import net.minecraft.network.packet.c2s.common.CustomClickActionC2SPacket; import net.minecraft.network.packet.c2s.common.CustomPayloadC2SPacket; import net.minecraft.server.network.ServerCommonNetworkHandler; @@ -55,4 +56,9 @@ private void onPlayPong(CommonPongC2SPacket packet, CallbackInfo ci) { addon.onPong(packet.getParameter()); } } + + @Inject(method = "onCustomClickAction", at = @At("TAIL")) + protected void hookCustomClickActionEvent(CustomClickActionC2SPacket packet, CallbackInfo ci) { + getAddon().invokeCustomClickActionEvent(packet.id(), packet.payload()); + } } diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/SignBlockEntityMixin.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/SignBlockEntityMixin.java new file mode 100644 index 0000000000..7955b6197a --- /dev/null +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/SignBlockEntityMixin.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.fabricmc.fabric.mixin.networking; + +import java.util.Optional; + +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; +import com.llamalad7.mixinextras.sugar.Local; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; + +import net.minecraft.block.entity.SignBlockEntity; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.nbt.NbtElement; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.util.Identifier; + +import net.fabricmc.fabric.api.networking.v1.CustomClickEventContext; +import net.fabricmc.fabric.impl.networking.CustomClickActionsRegistry; +import net.fabricmc.fabric.impl.networking.server.ServerPlayNetworkAddon; + +@Mixin(SignBlockEntity.class) +public class SignBlockEntityMixin { + @WrapOperation( + method = "runCommandClickEvent", + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/server/MinecraftServer;handleCustomClickAction(Lnet/minecraft/util/Identifier;Ljava/util/Optional;)V" + ) + ) + private void hookCustomClickActionListener(MinecraftServer instance, Identifier id, Optional payload, Operation original, @Local(argsOnly = true) PlayerEntity player) { + original.call(instance, id, payload); + + if (player instanceof ServerPlayerEntity serverPlayer) { + CustomClickEventContext context = new ServerPlayNetworkAddon.PlayContextImpl(serverPlayer.networkHandler); + CustomClickActionsRegistry.invokeListenerEvent(id, context, payload); + } + } +} diff --git a/fabric-networking-api-v1/src/main/resources/fabric-networking-api-v1.mixins.json b/fabric-networking-api-v1/src/main/resources/fabric-networking-api-v1.mixins.json index f9803508b8..270a154b6f 100644 --- a/fabric-networking-api-v1/src/main/resources/fabric-networking-api-v1.mixins.json +++ b/fabric-networking-api-v1/src/main/resources/fabric-networking-api-v1.mixins.json @@ -19,6 +19,7 @@ "ServerConfigurationNetworkHandlerMixin", "ServerLoginNetworkHandlerMixin", "ServerPlayNetworkHandlerMixin", + "SignBlockEntityMixin", "accessor.EntityTrackerAccessor", "accessor.ServerCommonNetworkHandlerAccessor", "accessor.ServerLoginNetworkHandlerAccessor", diff --git a/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/clickeventtest/CustomClickActionsTest.java b/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/clickeventtest/CustomClickActionsTest.java new file mode 100644 index 0000000000..728c9199c9 --- /dev/null +++ b/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/clickeventtest/CustomClickActionsTest.java @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.fabricmc.fabric.test.networking.clickeventtest; + +import static net.minecraft.server.command.CommandManager.literal; + +import java.util.function.Consumer; + +import com.mojang.brigadier.Command; +import com.mojang.brigadier.CommandDispatcher; + +import net.minecraft.dialog.type.Dialog; +import net.minecraft.nbt.NbtElement; +import net.minecraft.network.packet.Packet; +import net.minecraft.network.packet.s2c.common.ShowDialogS2CPacket; +import net.minecraft.registry.RegistryKey; +import net.minecraft.registry.RegistryKeys; +import net.minecraft.registry.entry.RegistryEntry; +import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.server.network.ServerPlayerConfigurationTask; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.text.Text; +import net.minecraft.util.Identifier; + +import net.fabricmc.api.ModInitializer; +import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; +import net.fabricmc.fabric.api.networking.v1.CustomClickActionEvents; +import net.fabricmc.fabric.api.networking.v1.CustomClickEventContext; +import net.fabricmc.fabric.api.networking.v1.ServerConfigurationConnectionEvents; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +import net.fabricmc.fabric.test.networking.NetworkingTestmods; + +public class CustomClickActionsTest implements ModInitializer { + private static final RegistryKey PLAY_TEST_DIALOG = RegistryKey.of(RegistryKeys.DIALOG, NetworkingTestmods.id("play_custom_click_event")); + private static final RegistryKey CONFIGURATION_TEST_DIALOG = RegistryKey.of(RegistryKeys.DIALOG, NetworkingTestmods.id("configuration_custom_click_event")); + private boolean showDialogDuringConfiguration = false; + + private void registerCommand(CommandDispatcher dispatcher) { + dispatcher.register(literal("networktestcommand") + .then(literal("testPlayClickAction").executes(ctx -> { + ServerPlayerEntity player = ctx.getSource().getPlayer(); + + if (player != null) { + RegistryEntry testDialog = ctx.getSource() + .getRegistryManager() + .getOrThrow(RegistryKeys.DIALOG) + .getOrThrow(PLAY_TEST_DIALOG); + player.openDialog(testDialog); + } + + return Command.SINGLE_SUCCESS; + })) + .then(literal("testConfigurationClickAction").executes(ctx -> { + showDialogDuringConfiguration = true; + ServerPlayNetworking.reconfigure(ctx.getSource().getPlayer()); + return Command.SINGLE_SUCCESS; + })) + ); + } + + @Override + public void onInitialize() { + CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> { + this.registerCommand(dispatcher); + }); + + ServerConfigurationConnectionEvents.CONFIGURE.register((handler, server) -> { + if (showDialogDuringConfiguration) { + RegistryEntry testDialog = server.getRegistryManager() + .getOrThrow(RegistryKeys.DIALOG) + .getOrThrow(CONFIGURATION_TEST_DIALOG); + + // important: use a task to prevent this dialog from being quickly skipped over + handler.addTask(new TestDialogConfigurationTask(testDialog)); + } + }); + + CustomClickActionEvents.customClickActionReceivedEvent(NetworkingTestmods.id("test_event")).register( + (context, payload) -> { + switch (context) { + case CustomClickEventContext.Configuration configuration -> { + String payloadString = payload.map(NbtElement::toString) + .orElse("no payload"); + NetworkingTestmods.LOGGER.info("Received configuration event with payload: {}", payloadString); + + // important: make sure to complete the task to continue to the game + configuration.handler().completeTask(TestDialogConfigurationTask.KEY); + + showDialogDuringConfiguration = false; + } + case CustomClickEventContext.Play play -> { + String payloadString = payload.map(NbtElement::toString) + .orElse("no payload"); + Text message = Text.translatable("key.fabric-networking-api-v1-testmod.customClick.play.received", payloadString); + play.player().sendMessage(message); + } + } + } + ); + } + + public record TestDialogConfigurationTask(RegistryEntry dialog) implements ServerPlayerConfigurationTask { + public static final Key KEY = new Key(Identifier.of(NetworkingTestmods.ID, "configure_dialog").toString()); + + @Override + public void sendPacket(Consumer> sender) { + var packet = new ShowDialogS2CPacket(dialog); + sender.accept(packet); + } + + @Override + public Key getKey() { + return KEY; + } + } +} diff --git a/fabric-networking-api-v1/src/testmod/resources/assets/fabric-networking-api-v1-testmod/lang/en_us.json b/fabric-networking-api-v1/src/testmod/resources/assets/fabric-networking-api-v1-testmod/lang/en_us.json index 0c0921f6b6..d09054010b 100644 --- a/fabric-networking-api-v1/src/testmod/resources/assets/fabric-networking-api-v1-testmod/lang/en_us.json +++ b/fabric-networking-api-v1/src/testmod/resources/assets/fabric-networking-api-v1-testmod/lang/en_us.json @@ -1,5 +1,12 @@ { "key.category.fabric-networking-api-v1-testmod": "Fabric Network Test", + "key.fabric-networking-api-v1-testmod.customClick.dialog.button.noPayload": "Send with no payload", + "key.fabric-networking-api-v1-testmod.customClick.dialog.button.payload": "Send with payload", + "key.fabric-networking-api-v1-testmod.customClick.dialog.configuration.body": "This is a test of CustomClickActionEvents registry in the CONFIGURATION phase. Click a button to invoke this test event with or without a payload. When you click a button, a message should appear in the log showing the event has been received by the server, with the payload.", + "key.fabric-networking-api-v1-testmod.customClick.dialog.configuration.title": "Configuration Phase Custom Click Event Test", + "key.fabric-networking-api-v1-testmod.customClick.dialog.play.body": "This is a test of CustomClickActionEvents registry in the PLAY phase. Click a button to invoke this test event with or without a payload. When you click a button, a message should appear in the chat showing the event has been received by the server, with the payload.", + "key.fabric-networking-api-v1-testmod.customClick.dialog.play.title": "Play Phase Custom Click Event Test", + "key.fabric-networking-api-v1-testmod.customClick.play.received": "Received play event with payload: %s", "key.fabric-networking-api-v1-testmod.open": "Open channel tester", "key.fabric-networking-api-v1-testmod.test": "Send test packet" } diff --git a/fabric-networking-api-v1/src/testmod/resources/data/fabric-networking-api-v1-testmod/dialog/configuration_custom_click_event.json b/fabric-networking-api-v1/src/testmod/resources/data/fabric-networking-api-v1-testmod/dialog/configuration_custom_click_event.json new file mode 100644 index 0000000000..219550f629 --- /dev/null +++ b/fabric-networking-api-v1/src/testmod/resources/data/fabric-networking-api-v1-testmod/dialog/configuration_custom_click_event.json @@ -0,0 +1,35 @@ +{ + "type": "minecraft:multi_action", + "title": { + "translate": "key.fabric-networking-api-v1-testmod.customClick.dialog.configuration.title" + }, + "body": { + "type": "minecraft:plain_message", + "contents": { + "translate": "key.fabric-networking-api-v1-testmod.customClick.dialog.configuration.body" + } + }, + "actions": [ + { + "label": { + "translate": "key.fabric-networking-api-v1-testmod.customClick.dialog.button.noPayload" + }, + "action": { + "type": "custom", + "id": "fabric-networking-api-v1-testmod:test_event" + } + }, + { + "label": { + "translate": "key.fabric-networking-api-v1-testmod.customClick.dialog.button.payload" + }, + "action": { + "type": "custom", + "id": "fabric-networking-api-v1-testmod:test_event", + "payload": { + "foo": "bar" + } + } + } + ] +} diff --git a/fabric-networking-api-v1/src/testmod/resources/data/fabric-networking-api-v1-testmod/dialog/play_custom_click_event.json b/fabric-networking-api-v1/src/testmod/resources/data/fabric-networking-api-v1-testmod/dialog/play_custom_click_event.json new file mode 100644 index 0000000000..2dce078d01 --- /dev/null +++ b/fabric-networking-api-v1/src/testmod/resources/data/fabric-networking-api-v1-testmod/dialog/play_custom_click_event.json @@ -0,0 +1,35 @@ +{ + "type": "minecraft:multi_action", + "title": { + "translate": "key.fabric-networking-api-v1-testmod.customClick.dialog.play.title" + }, + "body": { + "type": "minecraft:plain_message", + "contents": { + "translate": "key.fabric-networking-api-v1-testmod.customClick.dialog.play.body" + } + }, + "actions": [ + { + "label": { + "translate": "key.fabric-networking-api-v1-testmod.customClick.dialog.button.noPayload" + }, + "action": { + "type": "custom", + "id": "fabric-networking-api-v1-testmod:test_event" + } + }, + { + "label": { + "translate": "key.fabric-networking-api-v1-testmod.customClick.dialog.button.payload" + }, + "action": { + "type": "custom", + "id": "fabric-networking-api-v1-testmod:test_event", + "payload": { + "foo": "bar" + } + } + } + ] +} diff --git a/fabric-networking-api-v1/src/testmod/resources/fabric.mod.json b/fabric-networking-api-v1/src/testmod/resources/fabric.mod.json index b567341c4c..031ccca955 100644 --- a/fabric-networking-api-v1/src/testmod/resources/fabric.mod.json +++ b/fabric-networking-api-v1/src/testmod/resources/fabric.mod.json @@ -15,7 +15,8 @@ "net.fabricmc.fabric.test.networking.configuration.NetworkingConfigurationTest", "net.fabricmc.fabric.test.networking.keybindreciever.NetworkingKeybindPacketTest", "net.fabricmc.fabric.test.networking.login.NetworkingLoginQueryTest", - "net.fabricmc.fabric.test.networking.play.NetworkingPlayPacketTest" + "net.fabricmc.fabric.test.networking.play.NetworkingPlayPacketTest", + "net.fabricmc.fabric.test.networking.clickeventtest.CustomClickActionsTest" ], "client": [ "net.fabricmc.fabric.test.networking.client.channeltest.NetworkingChannelClientTest",