-
Notifications
You must be signed in to change notification settings - Fork 508
Event handling for custom click actions #4740
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TheDeathlyCow
wants to merge
28
commits into
FabricMC:1.21.7
Choose a base branch
from
TheDeathlyCow:custom-click-action-registry
base: 1.21.7
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 15 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
a3cd3b7
implement a basic event system
TheDeathlyCow f877689
add sign block entity mixin to config and hopefully fix that weirdly …
TheDeathlyCow 70d352e
consistent registry name
TheDeathlyCow 7159fa7
refactor to use context objects and separate config/play events
TheDeathlyCow 3db85ce
add a dialog test
TheDeathlyCow 5e6b483
apply spotless checks
TheDeathlyCow 745831a
fix blank line checkstyle violation
TheDeathlyCow 517bf0c
add javadoc to api methods
TheDeathlyCow 68fac72
add configuration phase tests
TheDeathlyCow 65c8eba
add missing full stops to javadoc
TheDeathlyCow 6819567
Merge branch '1.21.7' into custom-click-action-registry
TheDeathlyCow 519d68e
use a mock dialog packet system for testing
TheDeathlyCow 9d4734b
remove join event forcing dialog
TheDeathlyCow 52caa7d
Merge branch 'custom-click-action-registry' of https://github.com/The…
TheDeathlyCow 4049294
run spotlessapply
TheDeathlyCow 25cf3a7
move tests into own class and use a command to enable configuration test
TheDeathlyCow 66a1dc4
fix checkstyle
TheDeathlyCow d2ca80f
convert to a single event for the registry
TheDeathlyCow 2503992
add an event to handle "any" custom click action being received
TheDeathlyCow 4fc69f7
add override annotations to context
TheDeathlyCow 05cc21b
remove player from event context
TheDeathlyCow 3f593f0
move test play handling into same listener as config
TheDeathlyCow 3f9130e
move context impls to respective addons
TheDeathlyCow 2ac4575
remove any event and move create event to own method
TheDeathlyCow 2c4a29f
clear up some javadoc
TheDeathlyCow 991c6bd
move payload into event parameter
TheDeathlyCow e25b3f6
add player convenience method to the play context
TheDeathlyCow 150a4d0
import style fixes
TheDeathlyCow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
...g-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/CustomClickActionEvents.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /* | ||
| * 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.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 { | ||
| /** | ||
| * 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<ClickActionReceived<CustomClickEventContext.Play>> playClickActionEvent(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<ClickActionReceived<CustomClickEventContext.Configuration>> configurationClickActionEvent(Identifier id) { | ||
| Objects.requireNonNull(id, "ID cannot be null"); | ||
| return CustomClickActionsRegistry.CONFIGURATION_REGISTRY.getOrCreateActionEvent(id); | ||
| } | ||
|
|
||
| @FunctionalInterface | ||
| public interface ClickActionReceived<T extends CustomClickEventContext> { | ||
| /** | ||
| * 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); | ||
| } | ||
|
|
||
| private CustomClickActionEvents() { | ||
| } | ||
| } | ||
64 changes: 64 additions & 0 deletions
64
...g-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/CustomClickEventContext.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * Copyright (c) 2016, 2017, 2018, 2019 FabricMC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package net.fabricmc.fabric.api.networking.v1; | ||
|
|
||
| import 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; | ||
|
|
||
| /** | ||
| * 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 { | ||
modmuss50 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /** | ||
| * 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}. | ||
TheDeathlyCow marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| @Nullable | ||
| NbtElement payload(); | ||
TheDeathlyCow marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * 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 { | ||
TheDeathlyCow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /** | ||
| * The play handler responsible for the event. | ||
| */ | ||
| ServerPlayNetworkHandler handler(); | ||
TheDeathlyCow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * 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(); | ||
TheDeathlyCow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CustomClickActionsRegistry.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| /* | ||
| * 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 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.api.event.EventFactory; | ||
| import net.fabricmc.fabric.api.networking.v1.CustomClickActionEvents; | ||
| import net.fabricmc.fabric.api.networking.v1.CustomClickEventContext; | ||
|
|
||
| public final class CustomClickActionsRegistry<T extends CustomClickEventContext> { | ||
| public static final CustomClickActionsRegistry<CustomClickEventContext.Play> PLAY_REGISTRY = new CustomClickActionsRegistry<>(); | ||
| public static final CustomClickActionsRegistry<CustomClickEventContext.Configuration> CONFIGURATION_REGISTRY = new CustomClickActionsRegistry<>(); | ||
TheDeathlyCow marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| private final Map<Identifier, Event<CustomClickActionEvents.ClickActionReceived<T>>> registry = new HashMap<>(); | ||
|
|
||
| public Event<CustomClickActionEvents.ClickActionReceived<T>> getOrCreateActionEvent(Identifier id) { | ||
| return this.registry.computeIfAbsent( | ||
| id, | ||
| idx -> { | ||
TheDeathlyCow marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return EventFactory.createArrayBacked( | ||
| CustomClickActionEvents.ClickActionReceived.class, | ||
| listeners -> context -> { | ||
| for (CustomClickActionEvents.ClickActionReceived<T> listener : listeners) { | ||
| listener.handleCustomClickAction(context); | ||
| } | ||
| } | ||
| ); | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| public void invokeListenerEvent(Identifier id, T context) { | ||
| Event<CustomClickActionEvents.ClickActionReceived<T>> event = this.registry.get(id); | ||
|
|
||
| if (event != null) { | ||
| event.invoker().handleCustomClickAction(context); | ||
| } | ||
| } | ||
|
|
||
| public record PlayContextImpl( | ||
TheDeathlyCow marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ServerPlayNetworkHandler handler, | ||
| @Nullable NbtElement payload | ||
| ) implements CustomClickEventContext.Play { | ||
| } | ||
|
|
||
| public record ConfigurationContextImpl( | ||
| ServerConfigurationNetworkHandler handler, | ||
| @Nullable NbtElement payload | ||
| ) implements CustomClickEventContext.Configuration { | ||
| } | ||
|
|
||
| private CustomClickActionsRegistry() { | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...rking-api-v1/src/main/java/net/fabricmc/fabric/mixin/networking/SignBlockEntityMixin.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| * Copyright (c) 2016, 2017, 2018, 2019 FabricMC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package net.fabricmc.fabric.mixin.networking; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import com.llamalad7.mixinextras.injector.wrapoperation.Operation; | ||
| import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; | ||
| import com.llamalad7.mixinextras.sugar.Local; | ||
| import org.spongepowered.asm.mixin.Mixin; | ||
| import org.spongepowered.asm.mixin.injection.At; | ||
|
|
||
| import net.minecraft.block.entity.SignBlockEntity; | ||
| import net.minecraft.entity.player.PlayerEntity; | ||
| import net.minecraft.nbt.NbtElement; | ||
| import net.minecraft.server.MinecraftServer; | ||
| import net.minecraft.server.network.ServerPlayerEntity; | ||
| import net.minecraft.util.Identifier; | ||
|
|
||
| import net.fabricmc.fabric.impl.networking.CustomClickActionsRegistry; | ||
|
|
||
| @Mixin(SignBlockEntity.class) | ||
| public class SignBlockEntityMixin { | ||
| @WrapOperation( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why a wrap operation and not just an inject?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. an |
||
| 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<NbtElement> payload, Operation<Void> original, @Local(argsOnly = true) PlayerEntity player) { | ||
| original.call(instance, id, payload); | ||
|
|
||
| if (player instanceof ServerPlayerEntity serverPlayer) { | ||
| CustomClickActionsRegistry.PLAY_REGISTRY.invokeListenerEvent( | ||
| id, | ||
| new CustomClickActionsRegistry.PlayContextImpl(serverPlayer.networkHandler, payload.orElse(null)) | ||
| ); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.