From a3cd3b7a51f97a26f52a8b73fb2349687c418cc9 Mon Sep 17 00:00:00 2001 From: TheDeathlyCow Date: Mon, 7 Jul 2025 05:20:17 -1000 Subject: [PATCH 01/26] implement a basic event system --- .../v1/CustomClickActionListener.java | 11 ++++ .../api/networking/v1/CustomClickActions.java | 18 ++++++ .../CustomClickEventHandlerRegistry.java | 43 +++++++++++++ .../ServerCommonNetworkHandlerMixin.java | 31 ++++++++++ .../networking/SignBlockEntityMixin.java | 38 ++++++++++++ .../fabric-networking-api-v1.mixins.json | 61 ++++++++++--------- 6 files changed, 172 insertions(+), 30 deletions(-) create mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/CustomClickActionListener.java create mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/CustomClickActions.java create mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CustomClickEventHandlerRegistry.java create mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/SignBlockEntityMixin.java diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/CustomClickActionListener.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/CustomClickActionListener.java new file mode 100644 index 0000000000..eb92cfc804 --- /dev/null +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/CustomClickActionListener.java @@ -0,0 +1,11 @@ +package net.fabricmc.fabric.api.networking.v1; + +import net.minecraft.nbt.NbtElement; +import net.minecraft.server.network.ServerPlayerEntity; + +import org.jetbrains.annotations.Nullable; + +@FunctionalInterface +public interface CustomClickActionListener { + void handleCustomClickAction(ServerPlayerEntity player, @Nullable NbtElement payload); +} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/CustomClickActions.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/CustomClickActions.java new file mode 100644 index 0000000000..6b5cfce0bf --- /dev/null +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/CustomClickActions.java @@ -0,0 +1,18 @@ +package net.fabricmc.fabric.api.networking.v1; + +import java.util.Objects; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.impl.networking.CustomClickEventHandlerRegistry; + +import net.minecraft.util.Identifier; + +public final class CustomClickActions { + public static Event getListenerEvent(Identifier id) { + Objects.requireNonNull(id, "ID cannot be null"); + return CustomClickEventHandlerRegistry.getOrCreateListenerEvent(id); + } + + private CustomClickActions() { + } +} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CustomClickEventHandlerRegistry.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CustomClickEventHandlerRegistry.java new file mode 100644 index 0000000000..9537d1e820 --- /dev/null +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CustomClickEventHandlerRegistry.java @@ -0,0 +1,43 @@ +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.server.network.ServerPlayerEntity; +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.CustomClickActionListener; + +public class CustomClickEventHandlerRegistry { + private static final Map> REGISTRY = new HashMap<>(); + + public static Event getOrCreateListenerEvent(Identifier id) { + return REGISTRY.computeIfAbsent( + id, + idx -> { + return EventFactory.createArrayBacked( + CustomClickActionListener.class, + listeners -> (player, payload) -> { + for (CustomClickActionListener listener : listeners) { + listener.handleCustomClickAction(player, payload); + } + } + ); + } + ); + } + + public static void invokeListenerEvent(Identifier id, ServerPlayerEntity player, Optional payload) { + Event event = REGISTRY.get(id); + if (event != null) { + event.invoker().handleCustomClickAction(player, payload.orElse(null)); + } + } + + private CustomClickEventHandlerRegistry() { + } +} 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..18f1324589 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 @@ -16,7 +16,21 @@ package net.fabricmc.fabric.mixin.networking; +import com.llamalad7.mixinextras.injector.wrapmethod.WrapMethod; +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.networking.v1.CustomClickActionListener; +import net.fabricmc.fabric.impl.networking.CustomClickEventHandlerRegistry; + +import net.minecraft.network.packet.c2s.common.CustomClickActionC2SPacket; + +import net.minecraft.server.network.ServerPlayNetworkHandler; + +import net.minecraft.server.network.ServerPlayerEntity; + import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @@ -55,4 +69,21 @@ private void onPlayPong(CommonPongC2SPacket packet, CallbackInfo ci) { addon.onPong(packet.getParameter()); } } + + @WrapMethod(method = "onCustomClickAction") + protected void overrideCustomClickAction(CustomClickActionC2SPacket packet, Operation original) { + original.call(packet); + } + + @Mixin(ServerPlayNetworkHandler.class) + private abstract static class ServerPlayNetworkHandlerMixin extends ServerCommonNetworkHandlerMixin { + @Shadow + public ServerPlayerEntity player; + + @Override + protected void overrideCustomClickAction(CustomClickActionC2SPacket packet, Operation original) { + super.overrideCustomClickAction(packet, original); + CustomClickEventHandlerRegistry.invokeListenerEvent(packet.id(), this.player, 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..3f2537e410 --- /dev/null +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/SignBlockEntityMixin.java @@ -0,0 +1,38 @@ +package net.fabricmc.fabric.mixin.networking; + +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; + +import com.llamalad7.mixinextras.sugar.Local; + +import net.fabricmc.fabric.impl.networking.CustomClickEventHandlerRegistry; + +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 org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; + +import java.util.Optional; + +@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); + + // base method has a serverworld parameter, so the player should be a server player + CustomClickEventHandlerRegistry.invokeListenerEvent(id, (ServerPlayerEntity) player, 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..c67503a571 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 @@ -1,33 +1,34 @@ { - "required": true, - "package": "net.fabricmc.fabric.mixin.networking", - "compatibilityLevel": "JAVA_21", - "mixins": [ - "PacketCodecDispatcherMixin", - "ClientConnectionMixin", - "CommandManagerMixin", - "CustomPayloadC2SPacketMixin", - "CustomPayloadS2CPacketMixin", - "CustomPayloadPacketCodecMixin", - "DebugConfigCommandMixin", - "EntityTrackerEntryMixin", - "LoginQueryRequestS2CPacketMixin", - "LoginQueryResponseC2SPacketMixin", - "PlayerManagerMixin", - "RegistryByteBufMixin", - "ServerCommonNetworkHandlerMixin", - "ServerConfigurationNetworkHandlerMixin", - "ServerLoginNetworkHandlerMixin", - "ServerPlayNetworkHandlerMixin", - "accessor.EntityTrackerAccessor", - "accessor.ServerCommonNetworkHandlerAccessor", - "accessor.ServerLoginNetworkHandlerAccessor", - "accessor.ServerChunkLoadingManagerAccessor" - ], - "injectors": { - "defaultRequire": 1 - }, - "overwrites": { - "requireAnnotations": true + "required": true, + "package": "net.fabricmc.fabric.mixin.networking", + "compatibilityLevel": "JAVA_21", + "mixins": [ + "ClientConnectionMixin", + "CommandManagerMixin", + "CustomPayloadC2SPacketMixin", + "CustomPayloadPacketCodecMixin", + "CustomPayloadS2CPacketMixin", + "DebugConfigCommandMixin", + "EntityTrackerEntryMixin", + "LoginQueryRequestS2CPacketMixin", + "LoginQueryResponseC2SPacketMixin", + "PacketCodecDispatcherMixin", + "PlayerManagerMixin", + "RegistryByteBufMixin", + "ServerCommonNetworkHandlerMixin", + "ServerCommonNetworkHandlerMixin$ServerPlayNetworkHandlerMixin", + "ServerConfigurationNetworkHandlerMixin", + "ServerLoginNetworkHandlerMixin", + "ServerPlayNetworkHandlerMixin", + "accessor.EntityTrackerAccessor", + "accessor.ServerChunkLoadingManagerAccessor", + "accessor.ServerCommonNetworkHandlerAccessor", + "accessor.ServerLoginNetworkHandlerAccessor" + ], + "injectors": { + "defaultRequire": 1 + }, + "overwrites": { + "requireAnnotations": true } } From f877689028f9a59ad68e16c9933adf2ff9526433 Mon Sep 17 00:00:00 2001 From: TheDeathlyCow Date: Mon, 7 Jul 2025 05:24:18 -1000 Subject: [PATCH 02/26] add sign block entity mixin to config and hopefully fix that weirdly large diff --- .../fabric-networking-api-v1.mixins.json | 63 ++++++++++--------- 1 file changed, 32 insertions(+), 31 deletions(-) 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 c67503a571..24d40dec10 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 @@ -1,34 +1,35 @@ { - "required": true, - "package": "net.fabricmc.fabric.mixin.networking", - "compatibilityLevel": "JAVA_21", - "mixins": [ - "ClientConnectionMixin", - "CommandManagerMixin", - "CustomPayloadC2SPacketMixin", - "CustomPayloadPacketCodecMixin", - "CustomPayloadS2CPacketMixin", - "DebugConfigCommandMixin", - "EntityTrackerEntryMixin", - "LoginQueryRequestS2CPacketMixin", - "LoginQueryResponseC2SPacketMixin", - "PacketCodecDispatcherMixin", - "PlayerManagerMixin", - "RegistryByteBufMixin", - "ServerCommonNetworkHandlerMixin", - "ServerCommonNetworkHandlerMixin$ServerPlayNetworkHandlerMixin", - "ServerConfigurationNetworkHandlerMixin", - "ServerLoginNetworkHandlerMixin", - "ServerPlayNetworkHandlerMixin", - "accessor.EntityTrackerAccessor", - "accessor.ServerChunkLoadingManagerAccessor", - "accessor.ServerCommonNetworkHandlerAccessor", - "accessor.ServerLoginNetworkHandlerAccessor" - ], - "injectors": { - "defaultRequire": 1 - }, - "overwrites": { - "requireAnnotations": true + "required": true, + "package": "net.fabricmc.fabric.mixin.networking", + "compatibilityLevel": "JAVA_21", + "mixins": [ + "ClientConnectionMixin", + "CommandManagerMixin", + "CustomPayloadC2SPacketMixin", + "CustomPayloadPacketCodecMixin", + "CustomPayloadS2CPacketMixin", + "DebugConfigCommandMixin", + "EntityTrackerEntryMixin", + "LoginQueryRequestS2CPacketMixin", + "LoginQueryResponseC2SPacketMixin", + "PacketCodecDispatcherMixin", + "PlayerManagerMixin", + "RegistryByteBufMixin", + "ServerCommonNetworkHandlerMixin", + "ServerCommonNetworkHandlerMixin$ServerPlayNetworkHandlerMixin", + "ServerConfigurationNetworkHandlerMixin", + "ServerLoginNetworkHandlerMixin", + "ServerPlayNetworkHandlerMixin", + "SignBlockEntityMixin", + "accessor.EntityTrackerAccessor", + "accessor.ServerChunkLoadingManagerAccessor", + "accessor.ServerCommonNetworkHandlerAccessor", + "accessor.ServerLoginNetworkHandlerAccessor" + ], + "injectors": { + "defaultRequire": 1 + }, + "overwrites": { + "requireAnnotations": true } } From 70d352ecfa32fd36377d1a25a242cf8ed8f30657 Mon Sep 17 00:00:00 2001 From: TheDeathlyCow Date: Mon, 7 Jul 2025 05:26:48 -1000 Subject: [PATCH 03/26] consistent registry name --- ...egistry.java => CustomClickActionHandlerRegistry.java} | 0 .../main/resources/fabric-networking-api-v1.mixins.json | 8 ++++---- 2 files changed, 4 insertions(+), 4 deletions(-) rename fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/{CustomClickEventHandlerRegistry.java => CustomClickActionHandlerRegistry.java} (100%) diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CustomClickEventHandlerRegistry.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CustomClickActionHandlerRegistry.java similarity index 100% rename from fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CustomClickEventHandlerRegistry.java rename to fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CustomClickActionHandlerRegistry.java 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 24d40dec10..ca5cffb3b3 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 @@ -3,16 +3,16 @@ "package": "net.fabricmc.fabric.mixin.networking", "compatibilityLevel": "JAVA_21", "mixins": [ + "PacketCodecDispatcherMixin", "ClientConnectionMixin", "CommandManagerMixin", "CustomPayloadC2SPacketMixin", - "CustomPayloadPacketCodecMixin", "CustomPayloadS2CPacketMixin", + "CustomPayloadPacketCodecMixin", "DebugConfigCommandMixin", "EntityTrackerEntryMixin", "LoginQueryRequestS2CPacketMixin", "LoginQueryResponseC2SPacketMixin", - "PacketCodecDispatcherMixin", "PlayerManagerMixin", "RegistryByteBufMixin", "ServerCommonNetworkHandlerMixin", @@ -22,9 +22,9 @@ "ServerPlayNetworkHandlerMixin", "SignBlockEntityMixin", "accessor.EntityTrackerAccessor", - "accessor.ServerChunkLoadingManagerAccessor", "accessor.ServerCommonNetworkHandlerAccessor", - "accessor.ServerLoginNetworkHandlerAccessor" + "accessor.ServerLoginNetworkHandlerAccessor", + "accessor.ServerChunkLoadingManagerAccessor" ], "injectors": { "defaultRequire": 1 From 7159fa73287681c719e594b6d5dbd6f9e7ad3a8b Mon Sep 17 00:00:00 2001 From: TheDeathlyCow Date: Mon, 7 Jul 2025 18:51:18 -1000 Subject: [PATCH 04/26] refactor to use context objects and separate config/play events --- .../v1/CustomClickActionEvents.java | 54 ++++++++++++++++ .../api/networking/v1/CustomClickActions.java | 18 ------ .../impl/networking/AbstractNetworkAddon.java | 5 ++ .../CustomClickActionHandlerRegistry.java | 43 ------------- .../CustomClickActionsRegistry.java | 63 +++++++++++++++++++ .../ServerConfigurationNetworkAddon.java | 13 ++++ .../server/ServerPlayNetworkAddon.java | 13 ++++ .../ServerCommonNetworkHandlerMixin.java | 30 +-------- .../networking/SignBlockEntityMixin.java | 10 ++- 9 files changed, 158 insertions(+), 91 deletions(-) create mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/CustomClickActionEvents.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/CustomClickActions.java delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CustomClickActionHandlerRegistry.java create mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CustomClickActionsRegistry.java 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..d2b727b1ae --- /dev/null +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/CustomClickActionEvents.java @@ -0,0 +1,54 @@ +package net.fabricmc.fabric.api.networking.v1; + +import java.util.Objects; + +import net.minecraft.server.network.ServerCommonNetworkHandler; + +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.Nullable; + +import net.minecraft.nbt.NbtElement; +import net.minecraft.server.network.ServerConfigurationNetworkHandler; +import net.minecraft.server.network.ServerPlayNetworkHandler; +import net.minecraft.util.Identifier; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.impl.networking.CustomClickActionsRegistry; + +public final class CustomClickActionEvents { + public static Event> playClickActionEvent(Identifier id) { + Objects.requireNonNull(id, "ID cannot be null"); + return CustomClickActionsRegistry.PLAY_REGISTRY.getOrCreateActionEvent(id); + } + + public static Event> configClickActionEvent(Identifier id) { + Objects.requireNonNull(id, "ID cannot be null"); + return CustomClickActionsRegistry.CONFIG_REGISTRY.getOrCreateActionEvent(id); + } + + @ApiStatus.NonExtendable + public interface CommonContext { + ServerCommonNetworkHandler handler(); + + @Nullable + NbtElement payload(); + } + + @ApiStatus.NonExtendable + public interface PlayContext extends CommonContext { + ServerPlayNetworkHandler handler(); + } + + @ApiStatus.NonExtendable + public interface ConfigContext extends CommonContext { + ServerConfigurationNetworkHandler handler(); + } + + @FunctionalInterface + public interface ClickActionReceived { + void handleCustomClickAction(T context); + } + + private CustomClickActionEvents() { + } +} diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/CustomClickActions.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/CustomClickActions.java deleted file mode 100644 index 6b5cfce0bf..0000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/CustomClickActions.java +++ /dev/null @@ -1,18 +0,0 @@ -package net.fabricmc.fabric.api.networking.v1; - -import java.util.Objects; - -import net.fabricmc.fabric.api.event.Event; -import net.fabricmc.fabric.impl.networking.CustomClickEventHandlerRegistry; - -import net.minecraft.util.Identifier; - -public final class CustomClickActions { - public static Event getListenerEvent(Identifier id) { - Objects.requireNonNull(id, "ID cannot be null"); - return CustomClickEventHandlerRegistry.getOrCreateListenerEvent(id); - } - - private CustomClickActions() { - } -} 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..f89c27ad79 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 @@ -26,6 +26,8 @@ import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; +import net.minecraft.nbt.NbtElement; + import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -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, @Nullable NbtElement payload) { + } } diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CustomClickActionHandlerRegistry.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CustomClickActionHandlerRegistry.java deleted file mode 100644 index 9537d1e820..0000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CustomClickActionHandlerRegistry.java +++ /dev/null @@ -1,43 +0,0 @@ -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.server.network.ServerPlayerEntity; -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.CustomClickActionListener; - -public class CustomClickEventHandlerRegistry { - private static final Map> REGISTRY = new HashMap<>(); - - public static Event getOrCreateListenerEvent(Identifier id) { - return REGISTRY.computeIfAbsent( - id, - idx -> { - return EventFactory.createArrayBacked( - CustomClickActionListener.class, - listeners -> (player, payload) -> { - for (CustomClickActionListener listener : listeners) { - listener.handleCustomClickAction(player, payload); - } - } - ); - } - ); - } - - public static void invokeListenerEvent(Identifier id, ServerPlayerEntity player, Optional payload) { - Event event = REGISTRY.get(id); - if (event != null) { - event.invoker().handleCustomClickAction(player, payload.orElse(null)); - } - } - - private CustomClickEventHandlerRegistry() { - } -} 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..adccaa3797 --- /dev/null +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CustomClickActionsRegistry.java @@ -0,0 +1,63 @@ +package net.fabricmc.fabric.impl.networking; + +import java.util.HashMap; +import java.util.Map; + +import net.fabricmc.fabric.api.networking.v1.CustomClickActionEvents; + +import net.minecraft.nbt.NbtElement; +import net.minecraft.server.network.ServerConfigurationNetworkHandler; +import net.minecraft.server.network.ServerPlayNetworkHandler; +import net.minecraft.util.Identifier; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.event.EventFactory; + +import org.jetbrains.annotations.Nullable; + +public final class CustomClickActionsRegistry { + public static final CustomClickActionsRegistry PLAY_REGISTRY = new CustomClickActionsRegistry<>(); + public static final CustomClickActionsRegistry CONFIG_REGISTRY = new CustomClickActionsRegistry<>(); + + private final Map>> registry = new HashMap<>(); + + public Event> getOrCreateActionEvent(Identifier id) { + return this.registry.computeIfAbsent( + id, + idx -> { + return EventFactory.createArrayBacked( + CustomClickActionEvents.ClickActionReceived.class, + listeners -> context -> { + for (CustomClickActionEvents.ClickActionReceived listener : listeners) { + listener.handleCustomClickAction(context); + } + } + ); + } + ); + } + + public void invokeListenerEvent(Identifier id, T context) { + Event> event = this.registry.get(id); + if (event != null) { + event.invoker().handleCustomClickAction(context); + } + } + + public record PlayContextImpl( + ServerPlayNetworkHandler handler, + @Nullable NbtElement payload + ) implements CustomClickActionEvents.PlayContext { + + } + + public record ConfigContextImpl( + ServerConfigurationNetworkHandler handler, + @Nullable NbtElement payload + ) implements CustomClickActionEvents.ConfigContext { + + } + + 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..b86c1da284 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 @@ -21,6 +21,11 @@ import java.util.Objects; import io.netty.channel.ChannelFutureListener; + +import net.fabricmc.fabric.impl.networking.CustomClickActionsRegistry; + +import net.minecraft.nbt.NbtElement; + import org.jetbrains.annotations.Nullable; import net.minecraft.network.NetworkPhase; @@ -197,6 +202,14 @@ public void setReconfiguring() { isReconfiguring = true; } + @Override + public void invokeCustomClickActionEvent(Identifier id, @Nullable NbtElement payload) { + CustomClickActionsRegistry.CONFIG_REGISTRY.invokeListenerEvent( + id, + new CustomClickActionsRegistry.ConfigContextImpl(this.handler, payload) + ); + } + private enum RegisterState { NOT_SENT, SENT, 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..70cc081580 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 @@ -20,6 +20,9 @@ import java.util.List; import java.util.Objects; +import net.fabricmc.fabric.impl.networking.CustomClickActionsRegistry; + +import net.minecraft.nbt.NbtElement; import net.minecraft.network.ClientConnection; import net.minecraft.network.NetworkPhase; import net.minecraft.network.packet.CustomPayload; @@ -38,6 +41,8 @@ import net.fabricmc.fabric.impl.networking.NetworkingImpl; import net.fabricmc.fabric.impl.networking.RegistrationPayload; +import org.jetbrains.annotations.Nullable; + public final class ServerPlayNetworkAddon extends AbstractChanneledNetworkAddon> { private final ServerPlayNetworkHandler handler; private final MinecraftServer server; @@ -144,6 +149,14 @@ public boolean requestedReconfigure() { return requestedReconfigure; } + @Override + public void invokeCustomClickActionEvent(Identifier id, @Nullable NbtElement payload) { + CustomClickActionsRegistry.PLAY_REGISTRY.invokeListenerEvent( + id, + new CustomClickActionsRegistry.PlayContextImpl(this.handler, payload) + ); + } + private record ContextImpl(MinecraftServer server, ServerPlayNetworkHandler handler, PacketSender responseSender) implements ServerPlayNetworking.Context { private ContextImpl { Objects.requireNonNull(server, "server"); 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 18f1324589..1e6a88f141 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 @@ -16,21 +16,9 @@ package net.fabricmc.fabric.mixin.networking; -import com.llamalad7.mixinextras.injector.wrapmethod.WrapMethod; -import com.llamalad7.mixinextras.injector.wrapoperation.Operation; - -import net.fabricmc.fabric.api.event.Event; -import net.fabricmc.fabric.api.networking.v1.CustomClickActionListener; -import net.fabricmc.fabric.impl.networking.CustomClickEventHandlerRegistry; - import net.minecraft.network.packet.c2s.common.CustomClickActionC2SPacket; -import net.minecraft.server.network.ServerPlayNetworkHandler; - -import net.minecraft.server.network.ServerPlayerEntity; - import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @@ -70,20 +58,8 @@ private void onPlayPong(CommonPongC2SPacket packet, CallbackInfo ci) { } } - @WrapMethod(method = "onCustomClickAction") - protected void overrideCustomClickAction(CustomClickActionC2SPacket packet, Operation original) { - original.call(packet); - } - - @Mixin(ServerPlayNetworkHandler.class) - private abstract static class ServerPlayNetworkHandlerMixin extends ServerCommonNetworkHandlerMixin { - @Shadow - public ServerPlayerEntity player; - - @Override - protected void overrideCustomClickAction(CustomClickActionC2SPacket packet, Operation original) { - super.overrideCustomClickAction(packet, original); - CustomClickEventHandlerRegistry.invokeListenerEvent(packet.id(), this.player, packet.payload()); - } + @Inject(method = "onCustomClickAction", at = @At("TAIL")) + protected void hookCustomClickActionEvent(CustomClickActionC2SPacket packet, CallbackInfo ci) { + getAddon().invokeCustomClickActionEvent(packet.id(), packet.payload().orElse(null)); } } 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 index 3f2537e410..9a398b6a28 100644 --- 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 @@ -5,7 +5,7 @@ import com.llamalad7.mixinextras.sugar.Local; -import net.fabricmc.fabric.impl.networking.CustomClickEventHandlerRegistry; +import net.fabricmc.fabric.impl.networking.CustomClickActionsRegistry; import net.minecraft.block.entity.SignBlockEntity; @@ -32,7 +32,11 @@ public class SignBlockEntityMixin { private void hookCustomClickActionListener(MinecraftServer instance, Identifier id, Optional payload, Operation original, @Local(argsOnly = true) PlayerEntity player) { original.call(instance, id, payload); - // base method has a serverworld parameter, so the player should be a server player - CustomClickEventHandlerRegistry.invokeListenerEvent(id, (ServerPlayerEntity) player, payload); + if (player instanceof ServerPlayerEntity serverPlayer) { + CustomClickActionsRegistry.PLAY_REGISTRY.invokeListenerEvent( + id, + new CustomClickActionsRegistry.PlayContextImpl(serverPlayer.networkHandler, payload.orElse(null)) + ); + } } } From 3db85ce715c8cce808d9f75c681bc3c15bd34704 Mon Sep 17 00:00:00 2001 From: TheDeathlyCow Date: Mon, 7 Jul 2025 19:17:01 -1000 Subject: [PATCH 05/26] add a dialog test --- .../CustomClickActionsRegistry.java | 2 -- .../fabric-networking-api-v1.mixins.json | 1 - .../play/NetworkingPlayPacketTest.java | 12 ++++++++ .../lang/en_us.json | 6 +++- .../dialog/custom_click_event.json | 29 +++++++++++++++++++ 5 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 fabric-networking-api-v1/src/testmod/resources/data/fabric-networking-api-v1-testmod/dialog/custom_click_event.json 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 index adccaa3797..f336b46bfe 100644 --- 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 @@ -48,14 +48,12 @@ public record PlayContextImpl( ServerPlayNetworkHandler handler, @Nullable NbtElement payload ) implements CustomClickActionEvents.PlayContext { - } public record ConfigContextImpl( ServerConfigurationNetworkHandler handler, @Nullable NbtElement payload ) implements CustomClickActionEvents.ConfigContext { - } private CustomClickActionsRegistry() { 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 ca5cffb3b3..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 @@ -16,7 +16,6 @@ "PlayerManagerMixin", "RegistryByteBufMixin", "ServerCommonNetworkHandlerMixin", - "ServerCommonNetworkHandlerMixin$ServerPlayNetworkHandlerMixin", "ServerConfigurationNetworkHandlerMixin", "ServerLoginNetworkHandlerMixin", "ServerPlayNetworkHandlerMixin", diff --git a/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/play/NetworkingPlayPacketTest.java b/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/play/NetworkingPlayPacketTest.java index 245a9c6e6c..4b1f05f4d9 100644 --- a/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/play/NetworkingPlayPacketTest.java +++ b/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/play/NetworkingPlayPacketTest.java @@ -28,6 +28,9 @@ import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.StringArgumentType; +import net.fabricmc.fabric.api.networking.v1.CustomClickActionEvents; + +import net.minecraft.nbt.NbtElement; import net.minecraft.network.PacketByteBuf; import net.minecraft.network.PacketCallbacks; import net.minecraft.network.RegistryByteBuf; @@ -131,6 +134,15 @@ public void onInitialize() { } } }); + + CustomClickActionEvents.playClickActionEvent(NetworkingTestmods.id("play_event")).register( + context -> { + NbtElement payload = context.payload(); + String payloadString = payload != null ? payload.toString() : "null"; + Text message = Text.translatable("key.fabric-networking-api-v1-testmod.customClick.play.received", payloadString); + context.handler().getPlayer().sendMessage(message); + } + ); } public record OverlayPacket(Text message) implements CustomPayload { 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..7ae3bef7f6 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,9 @@ { "key.category.fabric-networking-api-v1-testmod": "Fabric Network Test", "key.fabric-networking-api-v1-testmod.open": "Open channel tester", - "key.fabric-networking-api-v1-testmod.test": "Send test packet" + "key.fabric-networking-api-v1-testmod.test": "Send test packet", + "key.fabric-networking-api-v1-testmod.customClick.play.received": "Received play event with payload: %s", + "key.fabric-networking-api-v1-testmod.customClick.play.dialog.title": "Custom Click Event Test", + "key.fabric-networking-api-v1-testmod.customClick.play.dialog.button.noPayload": "Send with no payload", + "key.fabric-networking-api-v1-testmod.customClick.play.dialog.button.payload": "Send with payload" } diff --git a/fabric-networking-api-v1/src/testmod/resources/data/fabric-networking-api-v1-testmod/dialog/custom_click_event.json b/fabric-networking-api-v1/src/testmod/resources/data/fabric-networking-api-v1-testmod/dialog/custom_click_event.json new file mode 100644 index 0000000000..3ad9649bae --- /dev/null +++ b/fabric-networking-api-v1/src/testmod/resources/data/fabric-networking-api-v1-testmod/dialog/custom_click_event.json @@ -0,0 +1,29 @@ +{ + "type": "minecraft:multi_action", + "title": { + "translate": "key.fabric-networking-api-v1-testmod.customClick.play.dialog.title" + }, + "actions": [ + { + "label": { + "translate": "key.fabric-networking-api-v1-testmod.customClick.play.dialog.button.noPayload" + }, + "action": { + "type": "custom", + "id": "fabric-networking-api-v1-testmod:play_event" + } + }, + { + "label": { + "translate": "key.fabric-networking-api-v1-testmod.customClick.play.dialog.button.payload" + }, + "action": { + "type": "custom", + "id": "fabric-networking-api-v1-testmod:play_event", + "payload": { + "foo": "bar" + } + } + } + ] +} From 5e6b483c9abf31fae6a863f22a12b64cc8b73de9 Mon Sep 17 00:00:00 2001 From: TheDeathlyCow Date: Mon, 7 Jul 2025 19:19:18 -1000 Subject: [PATCH 06/26] apply spotless checks --- .../v1/CustomClickActionEvents.java | 19 ++++++++++-- .../v1/CustomClickActionListener.java | 20 +++++++++++-- .../impl/networking/AbstractNetworkAddon.java | 3 +- .../CustomClickActionsRegistry.java | 21 ++++++++++++-- .../ServerConfigurationNetworkAddon.java | 7 ++--- .../server/ServerPlayNetworkAddon.java | 5 ++-- .../ServerCommonNetworkHandlerMixin.java | 3 +- .../networking/SignBlockEntityMixin.java | 29 ++++++++++++++----- .../play/NetworkingPlayPacketTest.java | 3 +- .../lang/en_us.json | 10 +++---- 10 files changed, 86 insertions(+), 34 deletions(-) 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 index d2b727b1ae..b04b0adb38 100644 --- 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 @@ -1,13 +1,28 @@ +/* + * 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 net.minecraft.server.network.ServerCommonNetworkHandler; - import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; import net.minecraft.nbt.NbtElement; +import net.minecraft.server.network.ServerCommonNetworkHandler; import net.minecraft.server.network.ServerConfigurationNetworkHandler; import net.minecraft.server.network.ServerPlayNetworkHandler; import net.minecraft.util.Identifier; diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/CustomClickActionListener.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/CustomClickActionListener.java index eb92cfc804..d5ea3007e8 100644 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/CustomClickActionListener.java +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/CustomClickActionListener.java @@ -1,10 +1,26 @@ +/* + * 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.Nullable; + import net.minecraft.nbt.NbtElement; import net.minecraft.server.network.ServerPlayerEntity; -import org.jetbrains.annotations.Nullable; - @FunctionalInterface public interface CustomClickActionListener { void handleCustomClickAction(ServerPlayerEntity player, @Nullable NbtElement payload); 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 f89c27ad79..2ffccff323 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 @@ -26,12 +26,11 @@ import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; -import net.minecraft.nbt.NbtElement; - import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import net.minecraft.nbt.NbtElement; import net.minecraft.util.Identifier; /** 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 index f336b46bfe..bac26c126d 100644 --- 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 @@ -1,9 +1,25 @@ +/* + * 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 net.fabricmc.fabric.api.networking.v1.CustomClickActionEvents; +import org.jetbrains.annotations.Nullable; import net.minecraft.nbt.NbtElement; import net.minecraft.server.network.ServerConfigurationNetworkHandler; @@ -12,8 +28,7 @@ import net.fabricmc.fabric.api.event.Event; import net.fabricmc.fabric.api.event.EventFactory; - -import org.jetbrains.annotations.Nullable; +import net.fabricmc.fabric.api.networking.v1.CustomClickActionEvents; public final class CustomClickActionsRegistry { public static final CustomClickActionsRegistry PLAY_REGISTRY = new 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 b86c1da284..412300bf14 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 @@ -21,13 +21,9 @@ import java.util.Objects; import io.netty.channel.ChannelFutureListener; - -import net.fabricmc.fabric.impl.networking.CustomClickActionsRegistry; - -import net.minecraft.nbt.NbtElement; - 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; @@ -43,6 +39,7 @@ 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; 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 70cc081580..4a0c018542 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 @@ -20,7 +20,7 @@ import java.util.List; import java.util.Objects; -import net.fabricmc.fabric.impl.networking.CustomClickActionsRegistry; +import org.jetbrains.annotations.Nullable; import net.minecraft.nbt.NbtElement; import net.minecraft.network.ClientConnection; @@ -38,11 +38,10 @@ 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; -import org.jetbrains.annotations.Nullable; - public final class ServerPlayNetworkAddon extends AbstractChanneledNetworkAddon> { private final ServerPlayNetworkHandler handler; private final MinecraftServer server; 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 1e6a88f141..c144dbb023 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 @@ -16,8 +16,6 @@ package net.fabricmc.fabric.mixin.networking; -import net.minecraft.network.packet.c2s.common.CustomClickActionC2SPacket; - import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; @@ -25,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; 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 index 9a398b6a28..d2ceb6c13a 100644 --- 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 @@ -1,24 +1,37 @@ +/* + * 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 net.fabricmc.fabric.impl.networking.CustomClickActionsRegistry; +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 org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; - -import java.util.Optional; +import net.fabricmc.fabric.impl.networking.CustomClickActionsRegistry; @Mixin(SignBlockEntity.class) public class SignBlockEntityMixin { diff --git a/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/play/NetworkingPlayPacketTest.java b/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/play/NetworkingPlayPacketTest.java index 4b1f05f4d9..e49f2dcabc 100644 --- a/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/play/NetworkingPlayPacketTest.java +++ b/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/play/NetworkingPlayPacketTest.java @@ -28,8 +28,6 @@ import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.StringArgumentType; -import net.fabricmc.fabric.api.networking.v1.CustomClickActionEvents; - import net.minecraft.nbt.NbtElement; import net.minecraft.network.PacketByteBuf; import net.minecraft.network.PacketCallbacks; @@ -48,6 +46,7 @@ import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; +import net.fabricmc.fabric.api.networking.v1.CustomClickActionEvents; import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry; import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; 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 7ae3bef7f6..2053716957 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,9 +1,9 @@ { "key.category.fabric-networking-api-v1-testmod": "Fabric Network Test", - "key.fabric-networking-api-v1-testmod.open": "Open channel tester", - "key.fabric-networking-api-v1-testmod.test": "Send test packet", - "key.fabric-networking-api-v1-testmod.customClick.play.received": "Received play event with payload: %s", - "key.fabric-networking-api-v1-testmod.customClick.play.dialog.title": "Custom Click Event Test", "key.fabric-networking-api-v1-testmod.customClick.play.dialog.button.noPayload": "Send with no payload", - "key.fabric-networking-api-v1-testmod.customClick.play.dialog.button.payload": "Send with payload" + "key.fabric-networking-api-v1-testmod.customClick.play.dialog.button.payload": "Send with payload", + "key.fabric-networking-api-v1-testmod.customClick.play.dialog.title": "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" } From 745831a5678cae1bd04afef8fbfc9237b64f48b3 Mon Sep 17 00:00:00 2001 From: TheDeathlyCow Date: Mon, 7 Jul 2025 22:33:47 -1000 Subject: [PATCH 07/26] fix blank line checkstyle violation --- .../v1/CustomClickActionListener.java | 27 ------------------- .../CustomClickActionsRegistry.java | 1 + 2 files changed, 1 insertion(+), 27 deletions(-) delete mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/CustomClickActionListener.java diff --git a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/CustomClickActionListener.java b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/CustomClickActionListener.java deleted file mode 100644 index d5ea3007e8..0000000000 --- a/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/CustomClickActionListener.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * 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.Nullable; - -import net.minecraft.nbt.NbtElement; -import net.minecraft.server.network.ServerPlayerEntity; - -@FunctionalInterface -public interface CustomClickActionListener { - void handleCustomClickAction(ServerPlayerEntity player, @Nullable NbtElement 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 index bac26c126d..297a04323e 100644 --- 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 @@ -54,6 +54,7 @@ public Event> getOrCreateActionEv public void invokeListenerEvent(Identifier id, T context) { Event> event = this.registry.get(id); + if (event != null) { event.invoker().handleCustomClickAction(context); } From 517bf0cbb948f0d17645f6338b65031d3c89680e Mon Sep 17 00:00:00 2001 From: TheDeathlyCow Date: Mon, 7 Jul 2025 22:49:32 -1000 Subject: [PATCH 08/26] add javadoc to api methods --- .../v1/CustomClickActionEvents.java | 57 +++++++++---------- .../v1/CustomClickEventContext.java | 49 ++++++++++++++++ .../CustomClickActionsRegistry.java | 13 +++-- .../ServerConfigurationNetworkAddon.java | 4 +- 4 files changed, 86 insertions(+), 37 deletions(-) create mode 100644 fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/CustomClickEventContext.java 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 index b04b0adb38..521eed2093 100644 --- 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 @@ -18,49 +18,48 @@ import java.util.Objects; -import org.jetbrains.annotations.ApiStatus; -import org.jetbrains.annotations.Nullable; - -import net.minecraft.nbt.NbtElement; -import net.minecraft.server.network.ServerCommonNetworkHandler; -import net.minecraft.server.network.ServerConfigurationNetworkHandler; -import net.minecraft.server.network.ServerPlayNetworkHandler; 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 from a + * dialog. + */ public final class CustomClickActionEvents { - public static Event> playClickActionEvent(Identifier id) { + /** + * Gets an event that is invoked on the server when a custom click event is received during the PLAY phase. 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> playClickActionEvent(Identifier id) { Objects.requireNonNull(id, "ID cannot be null"); return CustomClickActionsRegistry.PLAY_REGISTRY.getOrCreateActionEvent(id); } - public static Event> configClickActionEvent(Identifier id) { + /** + * Gets an event that is invoked on the server when a custom click event is received during the CONFIGURATION phase. + * 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 + * CONFIGURATION phase. + */ + public static Event> configurationClickActionEvent(Identifier id) { Objects.requireNonNull(id, "ID cannot be null"); - return CustomClickActionsRegistry.CONFIG_REGISTRY.getOrCreateActionEvent(id); - } - - @ApiStatus.NonExtendable - public interface CommonContext { - ServerCommonNetworkHandler handler(); - - @Nullable - NbtElement payload(); - } - - @ApiStatus.NonExtendable - public interface PlayContext extends CommonContext { - ServerPlayNetworkHandler handler(); - } - - @ApiStatus.NonExtendable - public interface ConfigContext extends CommonContext { - ServerConfigurationNetworkHandler handler(); + return CustomClickActionsRegistry.CONFIGURATION_REGISTRY.getOrCreateActionEvent(id); } @FunctionalInterface - public interface ClickActionReceived { + public interface ClickActionReceived { + /** + * 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. + */ void handleCustomClickAction(T context); } 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..bb02426a56 --- /dev/null +++ b/fabric-networking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/CustomClickEventContext.java @@ -0,0 +1,49 @@ +package net.fabricmc.fabric.api.networking.v1; + +import net.minecraft.nbt.NbtElement; +import net.minecraft.server.network.ServerCommonNetworkHandler; + +import net.minecraft.server.network.ServerConfigurationNetworkHandler; +import net.minecraft.server.network.ServerPlayNetworkHandler; + +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.Nullable; + +/** + * Contains data about a {@linkplain net.minecraft.text.ClickEvent.Custom custom click event} when one is received on + * the server. + */ +public sealed interface CustomClickEventContext permits CustomClickEventContext.Play, CustomClickEventContext.Configuration { + /** + * The handler responsible for the event + */ + ServerCommonNetworkHandler handler(); + + /** + * The payload received with this event. If no payload is received, then this payload will be {@code null}. + */ + @Nullable + NbtElement payload(); + + /** + * 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 + */ + ServerPlayNetworkHandler handler(); + } + + /** + * 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 + */ + ServerConfigurationNetworkHandler handler(); + } +} 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 index 297a04323e..a546f8140d 100644 --- 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 @@ -28,11 +28,12 @@ import net.fabricmc.fabric.api.event.Event; import net.fabricmc.fabric.api.event.EventFactory; +import net.fabricmc.fabric.api.networking.v1.CustomClickEventContext; import net.fabricmc.fabric.api.networking.v1.CustomClickActionEvents; -public final class CustomClickActionsRegistry { - public static final CustomClickActionsRegistry PLAY_REGISTRY = new CustomClickActionsRegistry<>(); - public static final CustomClickActionsRegistry CONFIG_REGISTRY = new CustomClickActionsRegistry<>(); +public final class CustomClickActionsRegistry { + public static final CustomClickActionsRegistry PLAY_REGISTRY = new CustomClickActionsRegistry<>(); + public static final CustomClickActionsRegistry CONFIGURATION_REGISTRY = new CustomClickActionsRegistry<>(); private final Map>> registry = new HashMap<>(); @@ -63,13 +64,13 @@ public void invokeListenerEvent(Identifier id, T context) { public record PlayContextImpl( ServerPlayNetworkHandler handler, @Nullable NbtElement payload - ) implements CustomClickActionEvents.PlayContext { + ) implements CustomClickEventContext.Play { } - public record ConfigContextImpl( + public record ConfigurationContextImpl( ServerConfigurationNetworkHandler handler, @Nullable NbtElement payload - ) implements CustomClickActionEvents.ConfigContext { + ) implements CustomClickEventContext.Configuration { } 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 412300bf14..e10fcd2386 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 @@ -201,9 +201,9 @@ public void setReconfiguring() { @Override public void invokeCustomClickActionEvent(Identifier id, @Nullable NbtElement payload) { - CustomClickActionsRegistry.CONFIG_REGISTRY.invokeListenerEvent( + CustomClickActionsRegistry.CONFIGURATION_REGISTRY.invokeListenerEvent( id, - new CustomClickActionsRegistry.ConfigContextImpl(this.handler, payload) + new CustomClickActionsRegistry.ConfigurationContextImpl(this.handler, payload) ); } From 68fac724f5e7a484ec9a5fb419707715f9bad790 Mon Sep 17 00:00:00 2001 From: TheDeathlyCow Date: Mon, 7 Jul 2025 23:28:58 -1000 Subject: [PATCH 09/26] add configuration phase tests --- .../v1/CustomClickEventContext.java | 23 ++++++++-- .../CustomClickActionsRegistry.java | 2 +- .../NetworkingConfigurationTest.java | 44 +++++++++++++++++++ .../play/NetworkingPlayPacketTest.java | 14 ++++++ .../lang/en_us.json | 9 ++-- .../configuration_custom_click_event.json | 35 +++++++++++++++ ...vent.json => play_custom_click_event.json} | 12 +++-- 7 files changed, 128 insertions(+), 11 deletions(-) create mode 100644 fabric-networking-api-v1/src/testmod/resources/data/fabric-networking-api-v1-testmod/dialog/configuration_custom_click_event.json rename fabric-networking-api-v1/src/testmod/resources/data/fabric-networking-api-v1-testmod/dialog/{custom_click_event.json => play_custom_click_event.json} (71%) 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 index bb02426a56..6c70217972 100644 --- 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 @@ -1,14 +1,29 @@ +/* + * 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 org.jetbrains.annotations.Nullable; + import net.minecraft.nbt.NbtElement; import net.minecraft.server.network.ServerCommonNetworkHandler; - import net.minecraft.server.network.ServerConfigurationNetworkHandler; import net.minecraft.server.network.ServerPlayNetworkHandler; -import org.jetbrains.annotations.ApiStatus; -import org.jetbrains.annotations.Nullable; - /** * Contains data about a {@linkplain net.minecraft.text.ClickEvent.Custom custom click event} when one is received on * the server. 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 index a546f8140d..e55e77a0e8 100644 --- 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 @@ -28,8 +28,8 @@ import net.fabricmc.fabric.api.event.Event; import net.fabricmc.fabric.api.event.EventFactory; -import net.fabricmc.fabric.api.networking.v1.CustomClickEventContext; import net.fabricmc.fabric.api.networking.v1.CustomClickActionEvents; +import net.fabricmc.fabric.api.networking.v1.CustomClickEventContext; public final class CustomClickActionsRegistry { public static final CustomClickActionsRegistry PLAY_REGISTRY = new CustomClickActionsRegistry<>(); diff --git a/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/configuration/NetworkingConfigurationTest.java b/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/configuration/NetworkingConfigurationTest.java index 9438286dfe..25d72780c3 100644 --- a/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/configuration/NetworkingConfigurationTest.java +++ b/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/configuration/NetworkingConfigurationTest.java @@ -21,10 +21,16 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import net.minecraft.dialog.type.Dialog; +import net.minecraft.nbt.NbtElement; import net.minecraft.network.PacketByteBuf; import net.minecraft.network.codec.PacketCodec; import net.minecraft.network.packet.CustomPayload; 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.DebugConfigCommand; import net.minecraft.server.network.ServerPlayerConfigurationTask; import net.minecraft.text.Text; @@ -32,6 +38,7 @@ 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.PayloadTypeRegistry; import net.fabricmc.fabric.api.networking.v1.ServerConfigurationConnectionEvents; import net.fabricmc.fabric.api.networking.v1.ServerConfigurationNetworking; @@ -42,6 +49,7 @@ */ public class NetworkingConfigurationTest implements ModInitializer { private static final Logger LOGGER = LoggerFactory.getLogger(NetworkingConfigurationTest.class); + private static final RegistryKey CONFIGURATION_TEST_DIALOG = RegistryKey.of(RegistryKeys.DIALOG, NetworkingTestmods.id("configuration_custom_click_event")); @Override public void onInitialize() { @@ -73,6 +81,27 @@ public void onInitialize() { // Enable the vanilla debugconfig command CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> DebugConfigCommand.register(dispatcher, registryAccess)); + + ServerConfigurationConnectionEvents.CONFIGURE.register((handler, server) -> { + RegistryEntry testDialog = server.getRegistryManager() + .getOrThrow(RegistryKeys.DIALOG) + .getOrThrow(CONFIGURATION_TEST_DIALOG); + + // open the dialog screen on the client + // important: use a task to prevent this dialog from being quickly skipped over + handler.addTask(new TestDialogConfigurationTask(testDialog)); + }); + + CustomClickActionEvents.configurationClickActionEvent(NetworkingTestmods.id("configuration_event")).register( + context -> { + NbtElement payload = context.payload(); + String payloadString = payload != null ? payload.toString() : "null"; + NetworkingTestmods.LOGGER.info("Received configuration event with payload: {}", payloadString); + + // important: make sure to complete the task to continue to the game + context.handler().completeTask(TestDialogConfigurationTask.KEY); + } + ); } public record TestConfigurationTask(String data) implements ServerPlayerConfigurationTask { @@ -90,6 +119,21 @@ public Key getKey() { } } + 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; + } + } + public record ConfigurationPacket(String data) implements CustomPayload { public static final CustomPayload.Id ID = new Id<>(Identifier.of(NetworkingTestmods.ID, "configure")); public static final PacketCodec CODEC = CustomPayload.codecOf(ConfigurationPacket::write, ConfigurationPacket::new); diff --git a/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/play/NetworkingPlayPacketTest.java b/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/play/NetworkingPlayPacketTest.java index e49f2dcabc..fd791799a0 100644 --- a/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/play/NetworkingPlayPacketTest.java +++ b/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/play/NetworkingPlayPacketTest.java @@ -28,6 +28,7 @@ import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.StringArgumentType; +import net.minecraft.dialog.type.Dialog; import net.minecraft.nbt.NbtElement; import net.minecraft.network.PacketByteBuf; import net.minecraft.network.PacketCallbacks; @@ -36,6 +37,9 @@ import net.minecraft.network.codec.PacketCodecs; import net.minecraft.network.packet.CustomPayload; import net.minecraft.network.packet.s2c.play.BundleS2CPacket; +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.ServerPlayerEntity; import net.minecraft.text.Text; @@ -56,6 +60,8 @@ import net.fabricmc.loader.api.FabricLoader; public final class NetworkingPlayPacketTest implements ModInitializer { + private static final RegistryKey PLAY_TEST_DIALOG = RegistryKey.of(RegistryKeys.DIALOG, NetworkingTestmods.id("play_custom_click_event")); + private static boolean spamUnknownPackets = false; public static void sendToTestChannel(ServerPlayerEntity player, String stuff) { @@ -134,6 +140,14 @@ public void onInitialize() { } }); + ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> { + RegistryEntry testDialog = server.getRegistryManager() + .getOrThrow(RegistryKeys.DIALOG) + .getOrThrow(PLAY_TEST_DIALOG); + + handler.getPlayer().openDialog(testDialog); + }); + CustomClickActionEvents.playClickActionEvent(NetworkingTestmods.id("play_event")).register( context -> { NbtElement payload = context.payload(); 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 2053716957..e382823268 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,8 +1,11 @@ { "key.category.fabric-networking-api-v1-testmod": "Fabric Network Test", - "key.fabric-networking-api-v1-testmod.customClick.play.dialog.button.noPayload": "Send with no payload", - "key.fabric-networking-api-v1-testmod.customClick.play.dialog.button.payload": "Send with payload", - "key.fabric-networking-api-v1-testmod.customClick.play.dialog.title": "Custom Click Event 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 the CONFIGURATION event in CustomClickActionEvents. Click a button to invoke this 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 the PLAY event in CustomClickActionEvents. Click a button to invoke this 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..58c1eacdd0 --- /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:configuration_event" + } + }, + { + "label": { + "translate": "key.fabric-networking-api-v1-testmod.customClick.dialog.button.payload" + }, + "action": { + "type": "custom", + "id": "fabric-networking-api-v1-testmod:configuration_event", + "payload": { + "foo": "bar" + } + } + } + ] +} diff --git a/fabric-networking-api-v1/src/testmod/resources/data/fabric-networking-api-v1-testmod/dialog/custom_click_event.json b/fabric-networking-api-v1/src/testmod/resources/data/fabric-networking-api-v1-testmod/dialog/play_custom_click_event.json similarity index 71% rename from fabric-networking-api-v1/src/testmod/resources/data/fabric-networking-api-v1-testmod/dialog/custom_click_event.json rename to fabric-networking-api-v1/src/testmod/resources/data/fabric-networking-api-v1-testmod/dialog/play_custom_click_event.json index 3ad9649bae..3a72e44521 100644 --- a/fabric-networking-api-v1/src/testmod/resources/data/fabric-networking-api-v1-testmod/dialog/custom_click_event.json +++ b/fabric-networking-api-v1/src/testmod/resources/data/fabric-networking-api-v1-testmod/dialog/play_custom_click_event.json @@ -1,12 +1,18 @@ { "type": "minecraft:multi_action", "title": { - "translate": "key.fabric-networking-api-v1-testmod.customClick.play.dialog.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.play.dialog.button.noPayload" + "translate": "key.fabric-networking-api-v1-testmod.customClick.dialog.button.noPayload" }, "action": { "type": "custom", @@ -15,7 +21,7 @@ }, { "label": { - "translate": "key.fabric-networking-api-v1-testmod.customClick.play.dialog.button.payload" + "translate": "key.fabric-networking-api-v1-testmod.customClick.dialog.button.payload" }, "action": { "type": "custom", From 65c8eba8fa1cfac21f17e43f6e5602482d558c6e Mon Sep 17 00:00:00 2001 From: TheDeathlyCow Date: Mon, 7 Jul 2025 23:42:01 -1000 Subject: [PATCH 10/26] add missing full stops to javadoc --- .../fabric/api/networking/v1/CustomClickEventContext.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 index 6c70217972..c2f00e7be1 100644 --- 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 @@ -30,7 +30,7 @@ */ public sealed interface CustomClickEventContext permits CustomClickEventContext.Play, CustomClickEventContext.Configuration { /** - * The handler responsible for the event + * The handler responsible for the event. */ ServerCommonNetworkHandler handler(); @@ -46,7 +46,7 @@ public sealed interface CustomClickEventContext permits CustomClickEventContext. @ApiStatus.NonExtendable non-sealed interface Play extends CustomClickEventContext { /** - * The play handler responsible for the event + * The play handler responsible for the event. */ ServerPlayNetworkHandler handler(); } @@ -57,7 +57,7 @@ non-sealed interface Play extends CustomClickEventContext { @ApiStatus.NonExtendable non-sealed interface Configuration extends CustomClickEventContext { /** - * The configuration handler responsible for the event + * The configuration handler responsible for the event. */ ServerConfigurationNetworkHandler handler(); } From 519d68e1296fb22871a4a5ee105329ce8a5678a7 Mon Sep 17 00:00:00 2001 From: TheDeathlyCow Date: Tue, 8 Jul 2025 00:09:26 -1000 Subject: [PATCH 11/26] use a mock dialog packet system for testing --- .../NetworkingConfigurationTest.java | 34 +++++++++++++----- .../lang/en_us.json | 2 -- .../configuration_custom_click_event.json | 35 ------------------- .../NetworkingConfigurationClientTest.java | 22 ++++++++++++ 4 files changed, 47 insertions(+), 46 deletions(-) delete mode 100644 fabric-networking-api-v1/src/testmod/resources/data/fabric-networking-api-v1-testmod/dialog/configuration_custom_click_event.json diff --git a/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/configuration/NetworkingConfigurationTest.java b/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/configuration/NetworkingConfigurationTest.java index 25d72780c3..e227543136 100644 --- a/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/configuration/NetworkingConfigurationTest.java +++ b/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/configuration/NetworkingConfigurationTest.java @@ -56,6 +56,7 @@ public void onInitialize() { PayloadTypeRegistry.configurationS2C().register(ConfigurationPacket.ID, ConfigurationPacket.CODEC); PayloadTypeRegistry.configurationC2S().register(ConfigurationCompletePacket.ID, ConfigurationCompletePacket.CODEC); PayloadTypeRegistry.configurationC2S().register(ConfigurationStartPacket.ID, ConfigurationStartPacket.CODEC); + PayloadTypeRegistry.configurationS2C().register(MockShowDialogPacket.ID, MockShowDialogPacket.CODEC); ServerConfigurationConnectionEvents.CONFIGURE.register((handler, server) -> { if (ServerConfigurationNetworking.isReconfiguring(handler)) { @@ -83,13 +84,10 @@ public void onInitialize() { CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> DebugConfigCommand.register(dispatcher, registryAccess)); ServerConfigurationConnectionEvents.CONFIGURE.register((handler, server) -> { - RegistryEntry testDialog = server.getRegistryManager() - .getOrThrow(RegistryKeys.DIALOG) - .getOrThrow(CONFIGURATION_TEST_DIALOG); - - // open the dialog screen on the client + // open a dialog screen on the client + // note: normally you would actually use ShowDialogS2CPacket, a mock packet is used here for testing purposes // important: use a task to prevent this dialog from being quickly skipped over - handler.addTask(new TestDialogConfigurationTask(testDialog)); + handler.addTask(new TestDialogConfigurationTask(CONFIGURATION_TEST_DIALOG)); }); CustomClickActionEvents.configurationClickActionEvent(NetworkingTestmods.id("configuration_event")).register( @@ -119,13 +117,13 @@ public Key getKey() { } } - public record TestDialogConfigurationTask(RegistryEntry dialog) implements ServerPlayerConfigurationTask { + public record TestDialogConfigurationTask(RegistryKey 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); + var packet = new MockShowDialogPacket(dialog); + sender.accept(ServerConfigurationNetworking.createS2CPacket(packet)); } @Override @@ -152,6 +150,24 @@ public Id getId() { } } + public record MockShowDialogPacket(RegistryKey dialog) implements CustomPayload { + public static final CustomPayload.Id ID = new Id<>(Identifier.of(NetworkingTestmods.ID, "mock_show_dialog")); + public static final PacketCodec CODEC = CustomPayload.codecOf(MockShowDialogPacket::write, MockShowDialogPacket::new); + + public MockShowDialogPacket(PacketByteBuf buf) { + this(buf.readRegistryKey(RegistryKeys.DIALOG)); + } + + public void write(PacketByteBuf buf) { + buf.writeRegistryKey(dialog); + } + + @Override + public Id getId() { + return ID; + } + } + public static class ConfigurationCompletePacket implements CustomPayload { public static final ConfigurationCompletePacket INSTANCE = new ConfigurationCompletePacket(); public static final CustomPayload.Id ID = new Id<>(Identifier.of(NetworkingTestmods.ID, "configure_complete")); 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 e382823268..58116c0c33 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 @@ -2,8 +2,6 @@ "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 the CONFIGURATION event in CustomClickActionEvents. Click a button to invoke this 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 the PLAY event in CustomClickActionEvents. Click a button to invoke this 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", 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 deleted file mode 100644 index 58c1eacdd0..0000000000 --- a/fabric-networking-api-v1/src/testmod/resources/data/fabric-networking-api-v1-testmod/dialog/configuration_custom_click_event.json +++ /dev/null @@ -1,35 +0,0 @@ -{ - "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:configuration_event" - } - }, - { - "label": { - "translate": "key.fabric-networking-api-v1-testmod.customClick.dialog.button.payload" - }, - "action": { - "type": "custom", - "id": "fabric-networking-api-v1-testmod:configuration_event", - "payload": { - "foo": "bar" - } - } - } - ] -} diff --git a/fabric-networking-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/networking/client/configuration/NetworkingConfigurationClientTest.java b/fabric-networking-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/networking/client/configuration/NetworkingConfigurationClientTest.java index 2098d96c14..d18962d4b2 100644 --- a/fabric-networking-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/networking/client/configuration/NetworkingConfigurationClientTest.java +++ b/fabric-networking-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/networking/client/configuration/NetworkingConfigurationClientTest.java @@ -16,6 +16,13 @@ package net.fabricmc.fabric.test.networking.client.configuration; +import net.fabricmc.fabric.test.networking.NetworkingTestmods; + +import net.minecraft.nbt.NbtString; +import net.minecraft.network.packet.c2s.common.CustomClickActionC2SPacket; +import net.minecraft.network.packet.s2c.common.ShowDialogS2CPacket; +import net.minecraft.text.ClickEvent; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -24,6 +31,8 @@ import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationNetworking; import net.fabricmc.fabric.test.networking.configuration.NetworkingConfigurationTest; +import java.util.Optional; + public class NetworkingConfigurationClientTest implements ClientModInitializer { private static final Logger LOGGER = LoggerFactory.getLogger(NetworkingConfigurationTest.class); @@ -46,5 +55,18 @@ public void onInitializeClient() { LOGGER.info("Sending configuration start packet to server"); ClientConfigurationNetworking.send(NetworkingConfigurationTest.ConfigurationStartPacket.INSTANCE); }); + + // This is a test of the CONFIGURATION event in CustomClickActionEvents. When this packet is received, a message + // should appear in the log showing the event has been received by the server, with the payload. + // Rather than using an actual dialog (which breaks E2E client tests), this mocks the behaviour of clicking on a + // custom event button by sending the packet that such an action would normally send. + ClientConfigurationNetworking.registerGlobalReceiver(NetworkingConfigurationTest.MockShowDialogPacket.ID, (packet, context) -> { + context.responseSender().sendPacket( + new CustomClickActionC2SPacket( + NetworkingTestmods.id("configuration_event"), + Optional.of(NbtString.of("this is a payload")) + ) + ); + }); } } From 9d4734be875289430f1285d0e72b1c981f2ff5a5 Mon Sep 17 00:00:00 2001 From: TheDeathlyCow Date: Tue, 8 Jul 2025 00:18:48 -1000 Subject: [PATCH 12/26] remove join event forcing dialog --- .../play/NetworkingPlayPacketTest.java | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/play/NetworkingPlayPacketTest.java b/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/play/NetworkingPlayPacketTest.java index fd791799a0..2e641b08cd 100644 --- a/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/play/NetworkingPlayPacketTest.java +++ b/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/play/NetworkingPlayPacketTest.java @@ -108,6 +108,18 @@ public static void registerCommand(CommandDispatcher dispat ServerPlayNetworking.reconfigure(ctx.getSource().getPlayer()); return Command.SINGLE_SUCCESS; })) + .then(literal("testdialog").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; + })) ); } @@ -140,14 +152,6 @@ public void onInitialize() { } }); - ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> { - RegistryEntry testDialog = server.getRegistryManager() - .getOrThrow(RegistryKeys.DIALOG) - .getOrThrow(PLAY_TEST_DIALOG); - - handler.getPlayer().openDialog(testDialog); - }); - CustomClickActionEvents.playClickActionEvent(NetworkingTestmods.id("play_event")).register( context -> { NbtElement payload = context.payload(); From 404929439d2302960496bbd430fa955d41e55f6d Mon Sep 17 00:00:00 2001 From: TheDeathlyCow Date: Tue, 8 Jul 2025 00:28:44 -1000 Subject: [PATCH 13/26] run spotlessapply --- .../configuration/NetworkingConfigurationTest.java | 2 -- .../networking/play/NetworkingPlayPacketTest.java | 1 + .../NetworkingConfigurationClientTest.java | 13 +++++-------- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/configuration/NetworkingConfigurationTest.java b/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/configuration/NetworkingConfigurationTest.java index e227543136..ed5ce518d0 100644 --- a/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/configuration/NetworkingConfigurationTest.java +++ b/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/configuration/NetworkingConfigurationTest.java @@ -27,10 +27,8 @@ import net.minecraft.network.codec.PacketCodec; import net.minecraft.network.packet.CustomPayload; 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.DebugConfigCommand; import net.minecraft.server.network.ServerPlayerConfigurationTask; import net.minecraft.text.Text; diff --git a/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/play/NetworkingPlayPacketTest.java b/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/play/NetworkingPlayPacketTest.java index 2e641b08cd..5676d8314b 100644 --- a/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/play/NetworkingPlayPacketTest.java +++ b/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/play/NetworkingPlayPacketTest.java @@ -118,6 +118,7 @@ public static void registerCommand(CommandDispatcher dispat .getOrThrow(PLAY_TEST_DIALOG); player.openDialog(testDialog); } + return Command.SINGLE_SUCCESS; })) ); diff --git a/fabric-networking-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/networking/client/configuration/NetworkingConfigurationClientTest.java b/fabric-networking-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/networking/client/configuration/NetworkingConfigurationClientTest.java index d18962d4b2..713c44d3fe 100644 --- a/fabric-networking-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/networking/client/configuration/NetworkingConfigurationClientTest.java +++ b/fabric-networking-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/networking/client/configuration/NetworkingConfigurationClientTest.java @@ -16,23 +16,20 @@ package net.fabricmc.fabric.test.networking.client.configuration; -import net.fabricmc.fabric.test.networking.NetworkingTestmods; - -import net.minecraft.nbt.NbtString; -import net.minecraft.network.packet.c2s.common.CustomClickActionC2SPacket; -import net.minecraft.network.packet.s2c.common.ShowDialogS2CPacket; -import net.minecraft.text.ClickEvent; +import java.util.Optional; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import net.minecraft.nbt.NbtString; +import net.minecraft.network.packet.c2s.common.CustomClickActionC2SPacket; + import net.fabricmc.api.ClientModInitializer; import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationConnectionEvents; import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationNetworking; +import net.fabricmc.fabric.test.networking.NetworkingTestmods; import net.fabricmc.fabric.test.networking.configuration.NetworkingConfigurationTest; -import java.util.Optional; - public class NetworkingConfigurationClientTest implements ClientModInitializer { private static final Logger LOGGER = LoggerFactory.getLogger(NetworkingConfigurationTest.class); From 25cf3a7ac51f302d9d912f867910e4c57f61d774 Mon Sep 17 00:00:00 2001 From: TheDeathlyCow Date: Mon, 14 Jul 2025 02:16:42 -1000 Subject: [PATCH 14/26] move tests into own class and use a command to enable configuration test --- .../CustomClickActionsTest.java | 112 ++++++++++++++++++ .../NetworkingConfigurationTest.java | 58 --------- .../play/NetworkingPlayPacketTest.java | 27 ----- .../lang/en_us.json | 2 + .../configuration_custom_click_event.json | 35 ++++++ .../src/testmod/resources/fabric.mod.json | 3 +- .../NetworkingConfigurationClientTest.java | 19 --- 7 files changed, 151 insertions(+), 105 deletions(-) create mode 100644 fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/clickeventtest/CustomClickActionsTest.java create mode 100644 fabric-networking-api-v1/src/testmod/resources/data/fabric-networking-api-v1-testmod/dialog/configuration_custom_click_event.json 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..9634207ec8 --- /dev/null +++ b/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/clickeventtest/CustomClickActionsTest.java @@ -0,0 +1,112 @@ +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.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); + }); + + CustomClickActionEvents.playClickActionEvent(NetworkingTestmods.id("play_event")).register( + context -> { + NbtElement payload = context.payload(); + String payloadString = payload != null ? payload.toString() : "no payload"; + Text message = Text.translatable("key.fabric-networking-api-v1-testmod.customClick.play.received", payloadString); + context.handler().getPlayer().sendMessage(message); + } + ); + + 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.configurationClickActionEvent(NetworkingTestmods.id("configuration_event")).register( + context -> { + NbtElement payload = context.payload(); + String payloadString = payload != null ? payload.toString() : "no payload"; + NetworkingTestmods.LOGGER.info("Received configuration event with payload: {}", payloadString); + + // important: make sure to complete the task to continue to the game + context.handler().completeTask(TestDialogConfigurationTask.KEY); + + showDialogDuringConfiguration = false; + } + ); + } + + 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/java/net/fabricmc/fabric/test/networking/configuration/NetworkingConfigurationTest.java b/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/configuration/NetworkingConfigurationTest.java index ed5ce518d0..9438286dfe 100644 --- a/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/configuration/NetworkingConfigurationTest.java +++ b/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/configuration/NetworkingConfigurationTest.java @@ -21,14 +21,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import net.minecraft.dialog.type.Dialog; -import net.minecraft.nbt.NbtElement; import net.minecraft.network.PacketByteBuf; import net.minecraft.network.codec.PacketCodec; import net.minecraft.network.packet.CustomPayload; import net.minecraft.network.packet.Packet; -import net.minecraft.registry.RegistryKey; -import net.minecraft.registry.RegistryKeys; import net.minecraft.server.command.DebugConfigCommand; import net.minecraft.server.network.ServerPlayerConfigurationTask; import net.minecraft.text.Text; @@ -36,7 +32,6 @@ 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.PayloadTypeRegistry; import net.fabricmc.fabric.api.networking.v1.ServerConfigurationConnectionEvents; import net.fabricmc.fabric.api.networking.v1.ServerConfigurationNetworking; @@ -47,14 +42,12 @@ */ public class NetworkingConfigurationTest implements ModInitializer { private static final Logger LOGGER = LoggerFactory.getLogger(NetworkingConfigurationTest.class); - private static final RegistryKey CONFIGURATION_TEST_DIALOG = RegistryKey.of(RegistryKeys.DIALOG, NetworkingTestmods.id("configuration_custom_click_event")); @Override public void onInitialize() { PayloadTypeRegistry.configurationS2C().register(ConfigurationPacket.ID, ConfigurationPacket.CODEC); PayloadTypeRegistry.configurationC2S().register(ConfigurationCompletePacket.ID, ConfigurationCompletePacket.CODEC); PayloadTypeRegistry.configurationC2S().register(ConfigurationStartPacket.ID, ConfigurationStartPacket.CODEC); - PayloadTypeRegistry.configurationS2C().register(MockShowDialogPacket.ID, MockShowDialogPacket.CODEC); ServerConfigurationConnectionEvents.CONFIGURE.register((handler, server) -> { if (ServerConfigurationNetworking.isReconfiguring(handler)) { @@ -80,24 +73,6 @@ public void onInitialize() { // Enable the vanilla debugconfig command CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> DebugConfigCommand.register(dispatcher, registryAccess)); - - ServerConfigurationConnectionEvents.CONFIGURE.register((handler, server) -> { - // open a dialog screen on the client - // note: normally you would actually use ShowDialogS2CPacket, a mock packet is used here for testing purposes - // important: use a task to prevent this dialog from being quickly skipped over - handler.addTask(new TestDialogConfigurationTask(CONFIGURATION_TEST_DIALOG)); - }); - - CustomClickActionEvents.configurationClickActionEvent(NetworkingTestmods.id("configuration_event")).register( - context -> { - NbtElement payload = context.payload(); - String payloadString = payload != null ? payload.toString() : "null"; - NetworkingTestmods.LOGGER.info("Received configuration event with payload: {}", payloadString); - - // important: make sure to complete the task to continue to the game - context.handler().completeTask(TestDialogConfigurationTask.KEY); - } - ); } public record TestConfigurationTask(String data) implements ServerPlayerConfigurationTask { @@ -115,21 +90,6 @@ public Key getKey() { } } - public record TestDialogConfigurationTask(RegistryKey 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 MockShowDialogPacket(dialog); - sender.accept(ServerConfigurationNetworking.createS2CPacket(packet)); - } - - @Override - public Key getKey() { - return KEY; - } - } - public record ConfigurationPacket(String data) implements CustomPayload { public static final CustomPayload.Id ID = new Id<>(Identifier.of(NetworkingTestmods.ID, "configure")); public static final PacketCodec CODEC = CustomPayload.codecOf(ConfigurationPacket::write, ConfigurationPacket::new); @@ -148,24 +108,6 @@ public Id getId() { } } - public record MockShowDialogPacket(RegistryKey dialog) implements CustomPayload { - public static final CustomPayload.Id ID = new Id<>(Identifier.of(NetworkingTestmods.ID, "mock_show_dialog")); - public static final PacketCodec CODEC = CustomPayload.codecOf(MockShowDialogPacket::write, MockShowDialogPacket::new); - - public MockShowDialogPacket(PacketByteBuf buf) { - this(buf.readRegistryKey(RegistryKeys.DIALOG)); - } - - public void write(PacketByteBuf buf) { - buf.writeRegistryKey(dialog); - } - - @Override - public Id getId() { - return ID; - } - } - public static class ConfigurationCompletePacket implements CustomPayload { public static final ConfigurationCompletePacket INSTANCE = new ConfigurationCompletePacket(); public static final CustomPayload.Id ID = new Id<>(Identifier.of(NetworkingTestmods.ID, "configure_complete")); diff --git a/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/play/NetworkingPlayPacketTest.java b/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/play/NetworkingPlayPacketTest.java index 5676d8314b..5845028371 100644 --- a/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/play/NetworkingPlayPacketTest.java +++ b/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/play/NetworkingPlayPacketTest.java @@ -29,7 +29,6 @@ import com.mojang.brigadier.arguments.StringArgumentType; import net.minecraft.dialog.type.Dialog; -import net.minecraft.nbt.NbtElement; import net.minecraft.network.PacketByteBuf; import net.minecraft.network.PacketCallbacks; import net.minecraft.network.RegistryByteBuf; @@ -39,7 +38,6 @@ import net.minecraft.network.packet.s2c.play.BundleS2CPacket; 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.ServerPlayerEntity; import net.minecraft.text.Text; @@ -50,7 +48,6 @@ import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; -import net.fabricmc.fabric.api.networking.v1.CustomClickActionEvents; import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry; import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; @@ -60,8 +57,6 @@ import net.fabricmc.loader.api.FabricLoader; public final class NetworkingPlayPacketTest implements ModInitializer { - private static final RegistryKey PLAY_TEST_DIALOG = RegistryKey.of(RegistryKeys.DIALOG, NetworkingTestmods.id("play_custom_click_event")); - private static boolean spamUnknownPackets = false; public static void sendToTestChannel(ServerPlayerEntity player, String stuff) { @@ -108,19 +103,6 @@ public static void registerCommand(CommandDispatcher dispat ServerPlayNetworking.reconfigure(ctx.getSource().getPlayer()); return Command.SINGLE_SUCCESS; })) - .then(literal("testdialog").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; - })) ); } @@ -152,15 +134,6 @@ public void onInitialize() { } } }); - - CustomClickActionEvents.playClickActionEvent(NetworkingTestmods.id("play_event")).register( - context -> { - NbtElement payload = context.payload(); - String payloadString = payload != null ? payload.toString() : "null"; - Text message = Text.translatable("key.fabric-networking-api-v1-testmod.customClick.play.received", payloadString); - context.handler().getPlayer().sendMessage(message); - } - ); } public record OverlayPacket(Text message) implements CustomPayload { 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 58116c0c33..e382823268 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 @@ -2,6 +2,8 @@ "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 the CONFIGURATION event in CustomClickActionEvents. Click a button to invoke this 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 the PLAY event in CustomClickActionEvents. Click a button to invoke this 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", 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..58c1eacdd0 --- /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:configuration_event" + } + }, + { + "label": { + "translate": "key.fabric-networking-api-v1-testmod.customClick.dialog.button.payload" + }, + "action": { + "type": "custom", + "id": "fabric-networking-api-v1-testmod:configuration_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", diff --git a/fabric-networking-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/networking/client/configuration/NetworkingConfigurationClientTest.java b/fabric-networking-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/networking/client/configuration/NetworkingConfigurationClientTest.java index 713c44d3fe..2098d96c14 100644 --- a/fabric-networking-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/networking/client/configuration/NetworkingConfigurationClientTest.java +++ b/fabric-networking-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/networking/client/configuration/NetworkingConfigurationClientTest.java @@ -16,18 +16,12 @@ package net.fabricmc.fabric.test.networking.client.configuration; -import java.util.Optional; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import net.minecraft.nbt.NbtString; -import net.minecraft.network.packet.c2s.common.CustomClickActionC2SPacket; - import net.fabricmc.api.ClientModInitializer; import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationConnectionEvents; import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationNetworking; -import net.fabricmc.fabric.test.networking.NetworkingTestmods; import net.fabricmc.fabric.test.networking.configuration.NetworkingConfigurationTest; public class NetworkingConfigurationClientTest implements ClientModInitializer { @@ -52,18 +46,5 @@ public void onInitializeClient() { LOGGER.info("Sending configuration start packet to server"); ClientConfigurationNetworking.send(NetworkingConfigurationTest.ConfigurationStartPacket.INSTANCE); }); - - // This is a test of the CONFIGURATION event in CustomClickActionEvents. When this packet is received, a message - // should appear in the log showing the event has been received by the server, with the payload. - // Rather than using an actual dialog (which breaks E2E client tests), this mocks the behaviour of clicking on a - // custom event button by sending the packet that such an action would normally send. - ClientConfigurationNetworking.registerGlobalReceiver(NetworkingConfigurationTest.MockShowDialogPacket.ID, (packet, context) -> { - context.responseSender().sendPacket( - new CustomClickActionC2SPacket( - NetworkingTestmods.id("configuration_event"), - Optional.of(NbtString.of("this is a payload")) - ) - ); - }); } } From 66a1dc4bd55eb9c95ad15e10af2c2dd515942919 Mon Sep 17 00:00:00 2001 From: TheDeathlyCow Date: Mon, 14 Jul 2025 02:19:32 -1000 Subject: [PATCH 15/26] fix checkstyle --- .../clickeventtest/CustomClickActionsTest.java | 16 ++++++++++++++++ .../play/NetworkingPlayPacketTest.java | 3 --- 2 files changed, 16 insertions(+), 3 deletions(-) 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 index 9634207ec8..314881b0c9 100644 --- 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 @@ -1,3 +1,19 @@ +/* + * 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; diff --git a/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/play/NetworkingPlayPacketTest.java b/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/play/NetworkingPlayPacketTest.java index 5845028371..245a9c6e6c 100644 --- a/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/play/NetworkingPlayPacketTest.java +++ b/fabric-networking-api-v1/src/testmod/java/net/fabricmc/fabric/test/networking/play/NetworkingPlayPacketTest.java @@ -28,7 +28,6 @@ import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.StringArgumentType; -import net.minecraft.dialog.type.Dialog; import net.minecraft.network.PacketByteBuf; import net.minecraft.network.PacketCallbacks; import net.minecraft.network.RegistryByteBuf; @@ -36,8 +35,6 @@ import net.minecraft.network.codec.PacketCodecs; import net.minecraft.network.packet.CustomPayload; import net.minecraft.network.packet.s2c.play.BundleS2CPacket; -import net.minecraft.registry.RegistryKey; -import net.minecraft.registry.RegistryKeys; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; From d2ca80f5f9c9b9938cd530f0ff3828b9c72a01f4 Mon Sep 17 00:00:00 2001 From: TheDeathlyCow Date: Mon, 14 Jul 2025 02:38:06 -1000 Subject: [PATCH 16/26] convert to a single event for the registry --- .../v1/CustomClickActionEvents.java | 21 ++-------- .../v1/CustomClickEventContext.java | 24 +++++++++-- .../impl/networking/AbstractNetworkAddon.java | 3 +- .../CustomClickActionsRegistry.java | 29 +++++++------- .../ServerConfigurationNetworkAddon.java | 5 ++- .../server/ServerPlayNetworkAddon.java | 9 ++--- .../ServerCommonNetworkHandlerMixin.java | 2 +- .../networking/SignBlockEntityMixin.java | 4 +- .../CustomClickActionsTest.java | 40 ++++++++++++------- .../lang/en_us.json | 4 +- .../configuration_custom_click_event.json | 4 +- .../dialog/play_custom_click_event.json | 4 +- 12 files changed, 83 insertions(+), 66 deletions(-) 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 index 521eed2093..40ce8114a5 100644 --- 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 @@ -36,31 +36,18 @@ public final class CustomClickActionEvents { * @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> playClickActionEvent(Identifier id) { + public static Event clickActionReceivedEvent(Identifier id) { Objects.requireNonNull(id, "ID cannot be null"); - return CustomClickActionsRegistry.PLAY_REGISTRY.getOrCreateActionEvent(id); - } - - /** - * Gets an event that is invoked on the server when a custom click event is received during the CONFIGURATION phase. - * 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 - * CONFIGURATION phase. - */ - public static Event> configurationClickActionEvent(Identifier id) { - Objects.requireNonNull(id, "ID cannot be null"); - return CustomClickActionsRegistry.CONFIGURATION_REGISTRY.getOrCreateActionEvent(id); + return CustomClickActionsRegistry.getOrCreateActionEvent(id); } @FunctionalInterface - public interface ClickActionReceived { + public interface ClickActionReceived { /** * 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. */ - void handleCustomClickAction(T context); + void handleCustomClickAction(CustomClickEventContext context); } 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 index c2f00e7be1..0197c372a6 100644 --- 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 @@ -16,17 +16,20 @@ package net.fabricmc.fabric.api.networking.v1; +import java.util.Optional; + import org.jetbrains.annotations.ApiStatus; -import org.jetbrains.annotations.Nullable; import net.minecraft.nbt.NbtElement; 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. + * the server. Custom click events may be received either during the PLAY or in CONFIGURATION phases. If the event is + * received during PLAY, then a player entity will be provided. */ public sealed interface CustomClickEventContext permits CustomClickEventContext.Play, CustomClickEventContext.Configuration { /** @@ -34,11 +37,15 @@ public sealed interface CustomClickEventContext permits CustomClickEventContext. */ ServerCommonNetworkHandler handler(); + /** + * The player entity responsible for the event, if in the play phase. + */ + Optional player(); + /** * The payload received with this event. If no payload is received, then this payload will be {@code null}. */ - @Nullable - NbtElement payload(); + Optional payload(); /** * The context data when a custom click event is received during the PLAY phase on the server. @@ -60,5 +67,14 @@ non-sealed interface Configuration extends CustomClickEventContext { * The configuration handler responsible for the event. */ ServerConfigurationNetworkHandler handler(); + + /** + * The configuration phase is too early for an entity to have been created, so an entity is never returned. + * + * @return Returns an empty optional. + */ + default Optional player() { + return Optional.empty(); + } } } 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 2ffccff323..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; @@ -174,6 +175,6 @@ public final void handleDisconnect() { */ protected abstract boolean isReservedChannel(Identifier channelName); - public void invokeCustomClickActionEvent(Identifier id, @Nullable NbtElement payload) { + 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 index e55e77a0e8..c7edffc54e 100644 --- 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 @@ -18,12 +18,12 @@ import java.util.HashMap; import java.util.Map; - -import org.jetbrains.annotations.Nullable; +import java.util.Optional; import net.minecraft.nbt.NbtElement; import net.minecraft.server.network.ServerConfigurationNetworkHandler; import net.minecraft.server.network.ServerPlayNetworkHandler; +import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.util.Identifier; import net.fabricmc.fabric.api.event.Event; @@ -31,20 +31,17 @@ import net.fabricmc.fabric.api.networking.v1.CustomClickActionEvents; import net.fabricmc.fabric.api.networking.v1.CustomClickEventContext; -public final class CustomClickActionsRegistry { - public static final CustomClickActionsRegistry PLAY_REGISTRY = new CustomClickActionsRegistry<>(); - public static final CustomClickActionsRegistry CONFIGURATION_REGISTRY = new CustomClickActionsRegistry<>(); - - private final Map>> registry = new HashMap<>(); +public final class CustomClickActionsRegistry { + private static final Map> REGISTRY = new HashMap<>(); - public Event> getOrCreateActionEvent(Identifier id) { - return this.registry.computeIfAbsent( + public static Event getOrCreateActionEvent(Identifier id) { + return REGISTRY.computeIfAbsent( id, idx -> { return EventFactory.createArrayBacked( CustomClickActionEvents.ClickActionReceived.class, listeners -> context -> { - for (CustomClickActionEvents.ClickActionReceived listener : listeners) { + for (CustomClickActionEvents.ClickActionReceived listener : listeners) { listener.handleCustomClickAction(context); } } @@ -53,8 +50,8 @@ public Event> getOrCreateActionEv ); } - public void invokeListenerEvent(Identifier id, T context) { - Event> event = this.registry.get(id); + public static void invokeListenerEvent(Identifier id, CustomClickEventContext context) { + Event event = REGISTRY.get(id); if (event != null) { event.invoker().handleCustomClickAction(context); @@ -63,13 +60,17 @@ public void invokeListenerEvent(Identifier id, T context) { public record PlayContextImpl( ServerPlayNetworkHandler handler, - @Nullable NbtElement payload + Optional player, + Optional payload ) implements CustomClickEventContext.Play { + public PlayContextImpl(ServerPlayNetworkHandler handler, ServerPlayerEntity player, Optional payload) { + this(handler, Optional.of(player), payload); + } } public record ConfigurationContextImpl( ServerConfigurationNetworkHandler handler, - @Nullable NbtElement payload + Optional payload ) implements CustomClickEventContext.Configuration { } 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 e10fcd2386..5f4bc2c636 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,6 +19,7 @@ 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; @@ -200,8 +201,8 @@ public void setReconfiguring() { } @Override - public void invokeCustomClickActionEvent(Identifier id, @Nullable NbtElement payload) { - CustomClickActionsRegistry.CONFIGURATION_REGISTRY.invokeListenerEvent( + public void invokeCustomClickActionEvent(Identifier id, Optional payload) { + CustomClickActionsRegistry.invokeListenerEvent( id, new CustomClickActionsRegistry.ConfigurationContextImpl(this.handler, payload) ); 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 4a0c018542..aacdf1c444 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,8 +19,7 @@ import java.util.Collections; import java.util.List; import java.util.Objects; - -import org.jetbrains.annotations.Nullable; +import java.util.Optional; import net.minecraft.nbt.NbtElement; import net.minecraft.network.ClientConnection; @@ -149,10 +148,10 @@ public boolean requestedReconfigure() { } @Override - public void invokeCustomClickActionEvent(Identifier id, @Nullable NbtElement payload) { - CustomClickActionsRegistry.PLAY_REGISTRY.invokeListenerEvent( + public void invokeCustomClickActionEvent(Identifier id, Optional payload) { + CustomClickActionsRegistry.invokeListenerEvent( id, - new CustomClickActionsRegistry.PlayContextImpl(this.handler, payload) + new CustomClickActionsRegistry.PlayContextImpl(this.handler, this.handler.getPlayer(), payload) ); } 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 c144dbb023..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 @@ -59,6 +59,6 @@ private void onPlayPong(CommonPongC2SPacket packet, CallbackInfo ci) { @Inject(method = "onCustomClickAction", at = @At("TAIL")) protected void hookCustomClickActionEvent(CustomClickActionC2SPacket packet, CallbackInfo ci) { - getAddon().invokeCustomClickActionEvent(packet.id(), packet.payload().orElse(null)); + 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 index d2ceb6c13a..482544aa63 100644 --- 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 @@ -46,9 +46,9 @@ private void hookCustomClickActionListener(MinecraftServer instance, Identifier original.call(instance, id, payload); if (player instanceof ServerPlayerEntity serverPlayer) { - CustomClickActionsRegistry.PLAY_REGISTRY.invokeListenerEvent( + CustomClickActionsRegistry.invokeListenerEvent( id, - new CustomClickActionsRegistry.PlayContextImpl(serverPlayer.networkHandler, payload.orElse(null)) + new CustomClickActionsRegistry.PlayContextImpl(serverPlayer.networkHandler, serverPlayer, payload) ); } } 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 index 314881b0c9..358e82e994 100644 --- 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 @@ -39,6 +39,7 @@ 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; @@ -77,12 +78,15 @@ public void onInitialize() { this.registerCommand(dispatcher); }); - CustomClickActionEvents.playClickActionEvent(NetworkingTestmods.id("play_event")).register( + CustomClickActionEvents.clickActionReceivedEvent(NetworkingTestmods.id("test_event")).register( context -> { - NbtElement payload = context.payload(); - String payloadString = payload != null ? payload.toString() : "no payload"; - Text message = Text.translatable("key.fabric-networking-api-v1-testmod.customClick.play.received", payloadString); - context.handler().getPlayer().sendMessage(message); + context.player().ifPresent(player -> { + String payloadString = context.payload() + .map(NbtElement::toString) + .orElse("no payload"); + Text message = Text.translatable("key.fabric-networking-api-v1-testmod.customClick.play.received", payloadString); + player.sendMessage(message); + }); } ); @@ -97,16 +101,24 @@ public void onInitialize() { } }); - CustomClickActionEvents.configurationClickActionEvent(NetworkingTestmods.id("configuration_event")).register( + CustomClickActionEvents.clickActionReceivedEvent(NetworkingTestmods.id("test_event")).register( context -> { - NbtElement payload = context.payload(); - String payloadString = payload != null ? payload.toString() : "no payload"; - NetworkingTestmods.LOGGER.info("Received configuration event with payload: {}", payloadString); - - // important: make sure to complete the task to continue to the game - context.handler().completeTask(TestDialogConfigurationTask.KEY); - - showDialogDuringConfiguration = false; + switch (context) { + case CustomClickEventContext.Configuration configuration -> { + String payloadString = context.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 -> { + // ignores + } + } } ); } 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 e382823268..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 @@ -2,9 +2,9 @@ "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 the CONFIGURATION event in CustomClickActionEvents. Click a button to invoke this 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.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 the PLAY event in CustomClickActionEvents. Click a button to invoke this 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.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", 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 index 58c1eacdd0..219550f629 100644 --- 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 @@ -16,7 +16,7 @@ }, "action": { "type": "custom", - "id": "fabric-networking-api-v1-testmod:configuration_event" + "id": "fabric-networking-api-v1-testmod:test_event" } }, { @@ -25,7 +25,7 @@ }, "action": { "type": "custom", - "id": "fabric-networking-api-v1-testmod:configuration_event", + "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 index 3a72e44521..2dce078d01 100644 --- 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 @@ -16,7 +16,7 @@ }, "action": { "type": "custom", - "id": "fabric-networking-api-v1-testmod:play_event" + "id": "fabric-networking-api-v1-testmod:test_event" } }, { @@ -25,7 +25,7 @@ }, "action": { "type": "custom", - "id": "fabric-networking-api-v1-testmod:play_event", + "id": "fabric-networking-api-v1-testmod:test_event", "payload": { "foo": "bar" } From 25039920ede1cfd52d4095c49416adc7689d0d3e Mon Sep 17 00:00:00 2001 From: TheDeathlyCow Date: Mon, 14 Jul 2025 02:45:44 -1000 Subject: [PATCH 17/26] add an event to handle "any" custom click action being received --- .../v1/CustomClickActionEvents.java | 39 +++++++++++++++++-- .../CustomClickActionsRegistry.java | 12 +++--- .../CustomClickActionsTest.java | 11 +++++- 3 files changed, 51 insertions(+), 11 deletions(-) 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 index 40ce8114a5..1a9448d685 100644 --- 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 @@ -21,13 +21,27 @@ import net.minecraft.util.Identifier; import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.event.EventFactory; import net.fabricmc.fabric.impl.networking.CustomClickActionsRegistry; /** - * Events for listening to {@linkplain net.minecraft.text.ClickEvent.Custom custom click actions}, such as from a - * dialog. + * 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 { + /** + * Invoked when any custom click action is received. If you are only interested in listening to events with a + * specific ID, use {@link #customClickActionReceivedEvent(Identifier)}. + */ + public static final Event ON_ANY_CUSTOM_CLICK_ACTION_RECEIVED = EventFactory.createArrayBacked( + CustomClickActionReceived.class, + listeners -> (id, context) -> { + for (CustomClickActionReceived listener : listeners) { + listener.handleCustomClickAction(id, context); + } + } + ); + /** * Gets an event that is invoked on the server when a custom click event is received during the PLAY phase. The * returned event will only be invoked when a click event is received with the given ID. @@ -36,16 +50,33 @@ public final class CustomClickActionEvents { * @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 clickActionReceivedEvent(Identifier id) { + public static Event customClickActionReceivedEvent(Identifier id) { Objects.requireNonNull(id, "ID cannot be null"); return CustomClickActionsRegistry.getOrCreateActionEvent(id); } @FunctionalInterface - public interface ClickActionReceived { + public interface CustomClickActionReceived { + /** + * Handles any 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. + */ + void handleCustomClickAction(Identifier id, CustomClickEventContext context); + } + + @FunctionalInterface + public interface NamedCustomClickActionReceived { /** * Handles a custom click event on the server from a given context. + * + *

This event only works for click actions with a single ID registered with {@link #customClickActionReceivedEvent(Identifier)}, + * for generic events see {@link #ON_ANY_CUSTOM_CLICK_ACTION_RECEIVED}. + * * @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. */ void handleCustomClickAction(CustomClickEventContext context); } 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 index c7edffc54e..7125ff9c82 100644 --- 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 @@ -32,16 +32,16 @@ import net.fabricmc.fabric.api.networking.v1.CustomClickEventContext; public final class CustomClickActionsRegistry { - private static final Map> REGISTRY = new HashMap<>(); + private static final Map> REGISTRY = new HashMap<>(); - public static Event getOrCreateActionEvent(Identifier id) { + public static Event getOrCreateActionEvent(Identifier id) { return REGISTRY.computeIfAbsent( id, idx -> { return EventFactory.createArrayBacked( - CustomClickActionEvents.ClickActionReceived.class, + CustomClickActionEvents.NamedCustomClickActionReceived.class, listeners -> context -> { - for (CustomClickActionEvents.ClickActionReceived listener : listeners) { + for (CustomClickActionEvents.NamedCustomClickActionReceived listener : listeners) { listener.handleCustomClickAction(context); } } @@ -51,7 +51,9 @@ public static Event getOrCreateActi } public static void invokeListenerEvent(Identifier id, CustomClickEventContext context) { - Event event = REGISTRY.get(id); + CustomClickActionEvents.ON_ANY_CUSTOM_CLICK_ACTION_RECEIVED.invoker().handleCustomClickAction(id, context); + + Event event = REGISTRY.get(id); if (event != null) { event.invoker().handleCustomClickAction(context); 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 index 358e82e994..28e6bdd4d3 100644 --- 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 @@ -78,7 +78,14 @@ public void onInitialize() { this.registerCommand(dispatcher); }); - CustomClickActionEvents.clickActionReceivedEvent(NetworkingTestmods.id("test_event")).register( + CustomClickActionEvents.ON_ANY_CUSTOM_CLICK_ACTION_RECEIVED.register((id, context) -> { + String payloadString = context.payload() + .map(NbtElement::toString) + .orElse("no payload"); + NetworkingTestmods.LOGGER.info("Received custom click action with ID {} and payload: {}", id, payloadString); + }); + + CustomClickActionEvents.customClickActionReceivedEvent(NetworkingTestmods.id("test_event")).register( context -> { context.player().ifPresent(player -> { String payloadString = context.payload() @@ -101,7 +108,7 @@ public void onInitialize() { } }); - CustomClickActionEvents.clickActionReceivedEvent(NetworkingTestmods.id("test_event")).register( + CustomClickActionEvents.customClickActionReceivedEvent(NetworkingTestmods.id("test_event")).register( context -> { switch (context) { case CustomClickEventContext.Configuration configuration -> { From 4fc69f76474fac2925fbb98dae5457686de284d2 Mon Sep 17 00:00:00 2001 From: TheDeathlyCow Date: Mon, 14 Jul 2025 02:55:13 -1000 Subject: [PATCH 18/26] add override annotations to context --- .../fabric/api/networking/v1/CustomClickEventContext.java | 2 ++ 1 file changed, 2 insertions(+) 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 index 0197c372a6..9198e9e8cd 100644 --- 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 @@ -55,6 +55,7 @@ non-sealed interface Play extends CustomClickEventContext { /** * The play handler responsible for the event. */ + @Override ServerPlayNetworkHandler handler(); } @@ -66,6 +67,7 @@ non-sealed interface Configuration extends CustomClickEventContext { /** * The configuration handler responsible for the event. */ + @Override ServerConfigurationNetworkHandler handler(); /** From 05cc21bf79a4549f89d394b4ce37a8a882588838 Mon Sep 17 00:00:00 2001 From: TheDeathlyCow Date: Sat, 26 Jul 2025 23:08:53 -1000 Subject: [PATCH 19/26] remove player from event context --- .../networking/v1/CustomClickEventContext.java | 16 +--------------- .../networking/CustomClickActionsRegistry.java | 4 ---- .../server/ServerPlayNetworkAddon.java | 2 +- .../mixin/networking/SignBlockEntityMixin.java | 2 +- 4 files changed, 3 insertions(+), 21 deletions(-) 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 index 9198e9e8cd..77f1f1c7da 100644 --- 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 @@ -38,12 +38,7 @@ public sealed interface CustomClickEventContext permits CustomClickEventContext. ServerCommonNetworkHandler handler(); /** - * The player entity responsible for the event, if in the play phase. - */ - Optional player(); - - /** - * The payload received with this event. If no payload is received, then this payload will be {@code null}. + * The payload received with this event. If no payload is received, then this payload will be empty. */ Optional payload(); @@ -69,14 +64,5 @@ non-sealed interface Configuration extends CustomClickEventContext { */ @Override ServerConfigurationNetworkHandler handler(); - - /** - * The configuration phase is too early for an entity to have been created, so an entity is never returned. - * - * @return Returns an empty optional. - */ - default Optional player() { - return Optional.empty(); - } } } 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 index 7125ff9c82..3b96f0659b 100644 --- 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 @@ -62,12 +62,8 @@ public static void invokeListenerEvent(Identifier id, CustomClickEventContext co public record PlayContextImpl( ServerPlayNetworkHandler handler, - Optional player, Optional payload ) implements CustomClickEventContext.Play { - public PlayContextImpl(ServerPlayNetworkHandler handler, ServerPlayerEntity player, Optional payload) { - this(handler, Optional.of(player), payload); - } } public record ConfigurationContextImpl( 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 aacdf1c444..ea0b2c14a9 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 @@ -151,7 +151,7 @@ public boolean requestedReconfigure() { public void invokeCustomClickActionEvent(Identifier id, Optional payload) { CustomClickActionsRegistry.invokeListenerEvent( id, - new CustomClickActionsRegistry.PlayContextImpl(this.handler, this.handler.getPlayer(), payload) + new CustomClickActionsRegistry.PlayContextImpl(this.handler, 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 index 482544aa63..8f699cc46d 100644 --- 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 @@ -48,7 +48,7 @@ private void hookCustomClickActionListener(MinecraftServer instance, Identifier if (player instanceof ServerPlayerEntity serverPlayer) { CustomClickActionsRegistry.invokeListenerEvent( id, - new CustomClickActionsRegistry.PlayContextImpl(serverPlayer.networkHandler, serverPlayer, payload) + new CustomClickActionsRegistry.PlayContextImpl(serverPlayer.networkHandler, payload) ); } } From 3f593f0bfa782d3d359afaa9899981b1172457cc Mon Sep 17 00:00:00 2001 From: TheDeathlyCow Date: Sat, 26 Jul 2025 23:10:47 -1000 Subject: [PATCH 20/26] move test play handling into same listener as config --- .../CustomClickActionsTest.java | 25 ++++--------------- 1 file changed, 5 insertions(+), 20 deletions(-) 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 index 28e6bdd4d3..3030850120 100644 --- 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 @@ -78,25 +78,6 @@ public void onInitialize() { this.registerCommand(dispatcher); }); - CustomClickActionEvents.ON_ANY_CUSTOM_CLICK_ACTION_RECEIVED.register((id, context) -> { - String payloadString = context.payload() - .map(NbtElement::toString) - .orElse("no payload"); - NetworkingTestmods.LOGGER.info("Received custom click action with ID {} and payload: {}", id, payloadString); - }); - - CustomClickActionEvents.customClickActionReceivedEvent(NetworkingTestmods.id("test_event")).register( - context -> { - context.player().ifPresent(player -> { - String payloadString = context.payload() - .map(NbtElement::toString) - .orElse("no payload"); - Text message = Text.translatable("key.fabric-networking-api-v1-testmod.customClick.play.received", payloadString); - player.sendMessage(message); - }); - } - ); - ServerConfigurationConnectionEvents.CONFIGURE.register((handler, server) -> { if (showDialogDuringConfiguration) { RegistryEntry

testDialog = server.getRegistryManager() @@ -123,7 +104,11 @@ public void onInitialize() { showDialogDuringConfiguration = false; } case CustomClickEventContext.Play play -> { - // ignores + String payloadString = context.payload() + .map(NbtElement::toString) + .orElse("no payload"); + Text message = Text.translatable("key.fabric-networking-api-v1-testmod.customClick.play.received", payloadString); + play.handler().getPlayer().sendMessage(message); } } } From 3f9130ed03095ff178b52f1c120ab4666be65457 Mon Sep 17 00:00:00 2001 From: TheDeathlyCow Date: Sat, 26 Jul 2025 23:20:50 -1000 Subject: [PATCH 21/26] move context impls to respective addons --- .../networking/CustomClickActionsRegistry.java | 17 ----------------- .../server/ServerConfigurationNetworkAddon.java | 11 ++++++++++- .../server/ServerPlayNetworkAddon.java | 10 +++++++++- .../mixin/networking/SignBlockEntityMixin.java | 5 ++++- 4 files changed, 23 insertions(+), 20 deletions(-) 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 index 3b96f0659b..fd371e2cc8 100644 --- 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 @@ -18,12 +18,7 @@ import java.util.HashMap; import java.util.Map; -import java.util.Optional; -import net.minecraft.nbt.NbtElement; -import net.minecraft.server.network.ServerConfigurationNetworkHandler; -import net.minecraft.server.network.ServerPlayNetworkHandler; -import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.util.Identifier; import net.fabricmc.fabric.api.event.Event; @@ -60,18 +55,6 @@ public static void invokeListenerEvent(Identifier id, CustomClickEventContext co } } - public record PlayContextImpl( - ServerPlayNetworkHandler handler, - Optional payload - ) implements CustomClickEventContext.Play { - } - - public record ConfigurationContextImpl( - ServerConfigurationNetworkHandler handler, - Optional payload - ) implements CustomClickEventContext.Configuration { - } - 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 5f4bc2c636..6aaa65c108 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 @@ -22,6 +22,9 @@ import java.util.Optional; import io.netty.channel.ChannelFutureListener; + +import net.fabricmc.fabric.api.networking.v1.CustomClickEventContext; + import org.jetbrains.annotations.Nullable; import net.minecraft.nbt.NbtElement; @@ -204,7 +207,7 @@ public void setReconfiguring() { public void invokeCustomClickActionEvent(Identifier id, Optional payload) { CustomClickActionsRegistry.invokeListenerEvent( id, - new CustomClickActionsRegistry.ConfigurationContextImpl(this.handler, payload) + new ConfigurationContextImpl(this.handler, payload) ); } @@ -226,4 +229,10 @@ private record ContextImpl(MinecraftServer server, ServerConfigurationNetworkHan Objects.requireNonNull(responseSender, "responseSender"); } } + + private record ConfigurationContextImpl( + ServerConfigurationNetworkHandler handler, + Optional payload + ) 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 ea0b2c14a9..e15fa1eb82 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 @@ -21,6 +21,8 @@ import java.util.Objects; import java.util.Optional; +import net.fabricmc.fabric.api.networking.v1.CustomClickEventContext; + import net.minecraft.nbt.NbtElement; import net.minecraft.network.ClientConnection; import net.minecraft.network.NetworkPhase; @@ -151,7 +153,7 @@ public boolean requestedReconfigure() { public void invokeCustomClickActionEvent(Identifier id, Optional payload) { CustomClickActionsRegistry.invokeListenerEvent( id, - new CustomClickActionsRegistry.PlayContextImpl(this.handler, payload) + new PlayContextImpl(this.handler, payload) ); } @@ -167,4 +169,10 @@ public ServerPlayerEntity player() { return handler.getPlayer(); } } + + public record PlayContextImpl( + ServerPlayNetworkHandler handler, + Optional payload + ) implements CustomClickEventContext.Play { + } } 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 index 8f699cc46d..f61313a2e7 100644 --- 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 @@ -21,6 +21,9 @@ import com.llamalad7.mixinextras.injector.wrapoperation.Operation; import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; import com.llamalad7.mixinextras.sugar.Local; + +import net.fabricmc.fabric.impl.networking.server.ServerPlayNetworkAddon; + import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; @@ -48,7 +51,7 @@ private void hookCustomClickActionListener(MinecraftServer instance, Identifier if (player instanceof ServerPlayerEntity serverPlayer) { CustomClickActionsRegistry.invokeListenerEvent( id, - new CustomClickActionsRegistry.PlayContextImpl(serverPlayer.networkHandler, payload) + new ServerPlayNetworkAddon.PlayContextImpl(serverPlayer.networkHandler, payload) ); } } From 2ac4575dff09097be30189843549618f9bc94639 Mon Sep 17 00:00:00 2001 From: TheDeathlyCow Date: Sat, 26 Jul 2025 23:26:26 -1000 Subject: [PATCH 22/26] remove any event and move create event to own method --- .../v1/CustomClickActionEvents.java | 27 +------------- .../CustomClickActionsRegistry.java | 35 +++++++++---------- 2 files changed, 17 insertions(+), 45 deletions(-) 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 index 1a9448d685..a68280a2d7 100644 --- 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 @@ -21,7 +21,6 @@ import net.minecraft.util.Identifier; import net.fabricmc.fabric.api.event.Event; -import net.fabricmc.fabric.api.event.EventFactory; import net.fabricmc.fabric.impl.networking.CustomClickActionsRegistry; /** @@ -29,19 +28,6 @@ * from a custom dialog. */ public final class CustomClickActionEvents { - /** - * Invoked when any custom click action is received. If you are only interested in listening to events with a - * specific ID, use {@link #customClickActionReceivedEvent(Identifier)}. - */ - public static final Event ON_ANY_CUSTOM_CLICK_ACTION_RECEIVED = EventFactory.createArrayBacked( - CustomClickActionReceived.class, - listeners -> (id, context) -> { - for (CustomClickActionReceived listener : listeners) { - listener.handleCustomClickAction(id, context); - } - } - ); - /** * Gets an event that is invoked on the server when a custom click event is received during the PLAY phase. The * returned event will only be invoked when a click event is received with the given ID. @@ -50,23 +36,12 @@ public final class CustomClickActionEvents { * @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) { + public static Event customClickActionReceivedEvent(Identifier id) { Objects.requireNonNull(id, "ID cannot be null"); return CustomClickActionsRegistry.getOrCreateActionEvent(id); } - @FunctionalInterface public interface CustomClickActionReceived { - /** - * Handles any 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. - */ - void handleCustomClickAction(Identifier id, CustomClickEventContext context); - } - - @FunctionalInterface - public interface NamedCustomClickActionReceived { /** * Handles a custom click event on the server from a given context. * 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 index fd371e2cc8..a6021c9828 100644 --- 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 @@ -27,34 +27,31 @@ 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 -> { - return EventFactory.createArrayBacked( - CustomClickActionEvents.NamedCustomClickActionReceived.class, - listeners -> context -> { - for (CustomClickActionEvents.NamedCustomClickActionReceived listener : listeners) { - listener.handleCustomClickAction(context); - } - } - ); - } - ); + 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) { - CustomClickActionEvents.ON_ANY_CUSTOM_CLICK_ACTION_RECEIVED.invoker().handleCustomClickAction(id, context); - - Event event = REGISTRY.get(id); + Event event = REGISTRY.get(id); if (event != null) { event.invoker().handleCustomClickAction(context); } } + private static Event createNewEvent() { + return EventFactory.createArrayBacked( + CustomClickActionEvents.CustomClickActionReceived.class, + listeners -> context -> { + for (CustomClickActionEvents.CustomClickActionReceived listener : listeners) { + listener.handleCustomClickAction(context); + } + } + ); + } + private CustomClickActionsRegistry() { } } From 2c4a29f8e1fd3883b113e29fc2fa5915c7aa92fe Mon Sep 17 00:00:00 2001 From: TheDeathlyCow Date: Sat, 26 Jul 2025 23:28:45 -1000 Subject: [PATCH 23/26] clear up some javadoc --- .../api/networking/v1/CustomClickActionEvents.java | 11 +++++------ .../api/networking/v1/CustomClickEventContext.java | 4 +--- 2 files changed, 6 insertions(+), 9 deletions(-) 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 index a68280a2d7..ff1ee14f8e 100644 --- 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 @@ -29,8 +29,8 @@ */ public final class CustomClickActionEvents { /** - * Gets an event that is invoked on the server when a custom click event is received during the PLAY phase. The - * returned event will only be invoked when a click event is received with the given ID. + * 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 @@ -40,18 +40,17 @@ public static Event customClickActionReceivedEvent(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. * - *

This event only works for click actions with a single ID registered with {@link #customClickActionReceivedEvent(Identifier)}, - * for generic events see {@link #ON_ANY_CUSTOM_CLICK_ACTION_RECEIVED}. - * * @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. + * can be checked using switch-statement pattern matching (see testmod if unfamiliar with this + * syntax). */ void handleCustomClickAction(CustomClickEventContext context); } 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 index 77f1f1c7da..5f1792a9b8 100644 --- 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 @@ -24,12 +24,10 @@ 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. If the event is - * received during PLAY, then a player entity will be provided. + * 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 { /** From 991c6bda9e39e52490098cac4c2a2faf23795ddf Mon Sep 17 00:00:00 2001 From: TheDeathlyCow Date: Sat, 26 Jul 2025 23:32:50 -1000 Subject: [PATCH 24/26] move payload into event parameter --- .../api/networking/v1/CustomClickActionEvents.java | 6 +++++- .../api/networking/v1/CustomClickEventContext.java | 5 ----- .../impl/networking/CustomClickActionsRegistry.java | 10 ++++++---- .../server/ServerConfigurationNetworkAddon.java | 11 +++-------- .../networking/server/ServerPlayNetworkAddon.java | 11 +++-------- .../fabric/mixin/networking/SignBlockEntityMixin.java | 6 ++---- 6 files changed, 19 insertions(+), 30 deletions(-) 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 index ff1ee14f8e..ce458046e8 100644 --- 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 @@ -17,7 +17,9 @@ 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; @@ -51,8 +53,10 @@ public interface CustomClickActionReceived { * {@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); + 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 index 5f1792a9b8..3bcf035a4a 100644 --- 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 @@ -35,11 +35,6 @@ public sealed interface CustomClickEventContext permits CustomClickEventContext. */ ServerCommonNetworkHandler handler(); - /** - * The payload received with this event. If no payload is received, then this payload will be empty. - */ - Optional payload(); - /** * The context data when a custom click event is received during the PLAY phase on the server. */ 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 index a6021c9828..719d47ed60 100644 --- 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 @@ -18,7 +18,9 @@ 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; @@ -33,20 +35,20 @@ public static Event getOrCrea return REGISTRY.computeIfAbsent(id, idx -> createNewEvent()); } - public static void invokeListenerEvent(Identifier id, CustomClickEventContext context) { + public static void invokeListenerEvent(Identifier id, CustomClickEventContext context, Optional payload) { Event event = REGISTRY.get(id); if (event != null) { - event.invoker().handleCustomClickAction(context); + event.invoker().handleCustomClickAction(context, payload); } } private static Event createNewEvent() { return EventFactory.createArrayBacked( CustomClickActionEvents.CustomClickActionReceived.class, - listeners -> context -> { + listeners -> (context, payload) -> { for (CustomClickActionEvents.CustomClickActionReceived listener : listeners) { - listener.handleCustomClickAction(context); + listener.handleCustomClickAction(context, payload); } } ); 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 6aaa65c108..5408af51d6 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 @@ -205,10 +205,8 @@ public void setReconfiguring() { @Override public void invokeCustomClickActionEvent(Identifier id, Optional payload) { - CustomClickActionsRegistry.invokeListenerEvent( - id, - new ConfigurationContextImpl(this.handler, payload) - ); + var context = new ConfigurationContextImpl(this.handler); + CustomClickActionsRegistry.invokeListenerEvent(id, context, payload); } private enum RegisterState { @@ -230,9 +228,6 @@ private record ContextImpl(MinecraftServer server, ServerConfigurationNetworkHan } } - private record ConfigurationContextImpl( - ServerConfigurationNetworkHandler handler, - Optional payload - ) implements CustomClickEventContext.Configuration { + 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 e15fa1eb82..6e1b0cbc5f 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 @@ -151,10 +151,8 @@ public boolean requestedReconfigure() { @Override public void invokeCustomClickActionEvent(Identifier id, Optional payload) { - CustomClickActionsRegistry.invokeListenerEvent( - id, - new PlayContextImpl(this.handler, payload) - ); + var context = new PlayContextImpl(this.handler); + CustomClickActionsRegistry.invokeListenerEvent(id, context, payload); } private record ContextImpl(MinecraftServer server, ServerPlayNetworkHandler handler, PacketSender responseSender) implements ServerPlayNetworking.Context { @@ -170,9 +168,6 @@ public ServerPlayerEntity player() { } } - public record PlayContextImpl( - ServerPlayNetworkHandler handler, - Optional payload - ) implements CustomClickEventContext.Play { + public record PlayContextImpl(ServerPlayNetworkHandler handler) implements CustomClickEventContext.Play { } } 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 index f61313a2e7..26a0934f18 100644 --- 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 @@ -49,10 +49,8 @@ private void hookCustomClickActionListener(MinecraftServer instance, Identifier original.call(instance, id, payload); if (player instanceof ServerPlayerEntity serverPlayer) { - CustomClickActionsRegistry.invokeListenerEvent( - id, - new ServerPlayNetworkAddon.PlayContextImpl(serverPlayer.networkHandler, payload) - ); + var context = new ServerPlayNetworkAddon.PlayContextImpl(serverPlayer.networkHandler); + CustomClickActionsRegistry.invokeListenerEvent(id, context, payload); } } } From e25b3f66640e27a165b3f77a132e5710eded827b Mon Sep 17 00:00:00 2001 From: TheDeathlyCow Date: Sat, 26 Jul 2025 23:35:54 -1000 Subject: [PATCH 25/26] add player convenience method to the play context --- .../api/networking/v1/CustomClickEventContext.java | 12 ++++++++++++ .../clickeventtest/CustomClickActionsTest.java | 10 ++++------ 2 files changed, 16 insertions(+), 6 deletions(-) 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 index 3bcf035a4a..22edf66f6b 100644 --- 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 @@ -18,6 +18,8 @@ import java.util.Optional; +import net.minecraft.server.network.ServerPlayerEntity; + import org.jetbrains.annotations.ApiStatus; import net.minecraft.nbt.NbtElement; @@ -45,6 +47,16 @@ non-sealed interface Play extends CustomClickEventContext { */ @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(); + } } /** 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 index 3030850120..728c9199c9 100644 --- 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 @@ -90,11 +90,10 @@ public void onInitialize() { }); CustomClickActionEvents.customClickActionReceivedEvent(NetworkingTestmods.id("test_event")).register( - context -> { + (context, payload) -> { switch (context) { case CustomClickEventContext.Configuration configuration -> { - String payloadString = context.payload() - .map(NbtElement::toString) + String payloadString = payload.map(NbtElement::toString) .orElse("no payload"); NetworkingTestmods.LOGGER.info("Received configuration event with payload: {}", payloadString); @@ -104,11 +103,10 @@ public void onInitialize() { showDialogDuringConfiguration = false; } case CustomClickEventContext.Play play -> { - String payloadString = context.payload() - .map(NbtElement::toString) + String payloadString = payload.map(NbtElement::toString) .orElse("no payload"); Text message = Text.translatable("key.fabric-networking-api-v1-testmod.customClick.play.received", payloadString); - play.handler().getPlayer().sendMessage(message); + play.player().sendMessage(message); } } } From 150a4d08b9ad9ae91d46dc3595b552986978c311 Mon Sep 17 00:00:00 2001 From: TheDeathlyCow Date: Sat, 26 Jul 2025 23:45:10 -1000 Subject: [PATCH 26/26] import style fixes --- .../fabric/api/networking/v1/CustomClickEventContext.java | 6 +----- .../networking/server/ServerConfigurationNetworkAddon.java | 6 ++---- .../impl/networking/server/ServerPlayNetworkAddon.java | 5 ++--- .../fabric/mixin/networking/SignBlockEntityMixin.java | 7 +++---- 4 files changed, 8 insertions(+), 16 deletions(-) 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 index 22edf66f6b..a8aa17a3f6 100644 --- 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 @@ -16,16 +16,12 @@ package net.fabricmc.fabric.api.networking.v1; -import java.util.Optional; - -import net.minecraft.server.network.ServerPlayerEntity; - import org.jetbrains.annotations.ApiStatus; -import net.minecraft.nbt.NbtElement; 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 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 5408af51d6..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 @@ -22,9 +22,6 @@ import java.util.Optional; import io.netty.channel.ChannelFutureListener; - -import net.fabricmc.fabric.api.networking.v1.CustomClickEventContext; - import org.jetbrains.annotations.Nullable; import net.minecraft.nbt.NbtElement; @@ -37,6 +34,7 @@ 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; @@ -205,7 +203,7 @@ public void setReconfiguring() { @Override public void invokeCustomClickActionEvent(Identifier id, Optional payload) { - var context = new ConfigurationContextImpl(this.handler); + CustomClickEventContext context = new ConfigurationContextImpl(this.handler); CustomClickActionsRegistry.invokeListenerEvent(id, context, payload); } 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 6e1b0cbc5f..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 @@ -21,8 +21,6 @@ import java.util.Objects; import java.util.Optional; -import net.fabricmc.fabric.api.networking.v1.CustomClickEventContext; - import net.minecraft.nbt.NbtElement; import net.minecraft.network.ClientConnection; import net.minecraft.network.NetworkPhase; @@ -33,6 +31,7 @@ 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; @@ -151,7 +150,7 @@ public boolean requestedReconfigure() { @Override public void invokeCustomClickActionEvent(Identifier id, Optional payload) { - var context = new PlayContextImpl(this.handler); + CustomClickEventContext context = new PlayContextImpl(this.handler); CustomClickActionsRegistry.invokeListenerEvent(id, context, 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 index 26a0934f18..7955b6197a 100644 --- 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 @@ -21,9 +21,6 @@ import com.llamalad7.mixinextras.injector.wrapoperation.Operation; import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; import com.llamalad7.mixinextras.sugar.Local; - -import net.fabricmc.fabric.impl.networking.server.ServerPlayNetworkAddon; - import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; @@ -34,7 +31,9 @@ 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 { @@ -49,7 +48,7 @@ private void hookCustomClickActionListener(MinecraftServer instance, Identifier original.call(instance, id, payload); if (player instanceof ServerPlayerEntity serverPlayer) { - var context = new ServerPlayNetworkAddon.PlayContextImpl(serverPlayer.networkHandler); + CustomClickEventContext context = new ServerPlayNetworkAddon.PlayContextImpl(serverPlayer.networkHandler); CustomClickActionsRegistry.invokeListenerEvent(id, context, payload); } }