-
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 3 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
11 changes: 11 additions & 0 deletions
11
...api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/CustomClickActionListener.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,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); | ||
| } |
18 changes: 18 additions & 0 deletions
18
...orking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/CustomClickActions.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,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<CustomClickActionListener> getListenerEvent(Identifier id) { | ||
| Objects.requireNonNull(id, "ID cannot be null"); | ||
| return CustomClickEventHandlerRegistry.getOrCreateListenerEvent(id); | ||
| } | ||
|
|
||
| private CustomClickActions() { | ||
| } | ||
| } | ||
43 changes: 43 additions & 0 deletions
43
...1/src/main/java/net/fabricmc/fabric/impl/networking/CustomClickActionHandlerRegistry.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,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<Identifier, Event<CustomClickActionListener>> REGISTRY = new HashMap<>(); | ||
|
|
||
| public static Event<CustomClickActionListener> 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<NbtElement> payload) { | ||
| Event<CustomClickActionListener> event = REGISTRY.get(id); | ||
| if (event != null) { | ||
| event.invoker().handleCustomClickAction(player, payload.orElse(null)); | ||
| } | ||
| } | ||
|
|
||
| private CustomClickEventHandlerRegistry() { | ||
| } | ||
| } |
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
38 changes: 38 additions & 0 deletions
38
...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,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( | ||
|
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); | ||
|
|
||
| // base method has a serverworld parameter, so the player should be a server player | ||
| CustomClickEventHandlerRegistry.invokeListenerEvent(id, (ServerPlayerEntity) player, payload); | ||
| } | ||
| } | ||
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.