From 00066f1e8dbafcaf259542635fa3e33c53b2e84e Mon Sep 17 00:00:00 2001 From: Frieder Hannenheim <41985738+FriederHannenheim@users.noreply.github.com> Date: Sun, 20 Jun 2021 16:25:14 +0200 Subject: [PATCH] Fix ClientPickBlockApplyCallback.EVENT not working as expected Fixes #1493 (#1498) Co-authored-by: Player <player@player.to> --- .../interaction/MixinMinecraftClient.java | 2 +- .../interaction/PlayerPickBlockTests.java | 41 +++++++++++++++++++ .../src/testmod/resources/fabric.mod.json | 3 +- 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 fabric-events-interaction-v0/src/testmod/java/net/fabricmc/fabric/test/event/interaction/PlayerPickBlockTests.java diff --git a/fabric-events-interaction-v0/src/main/java/net/fabricmc/fabric/mixin/event/interaction/MixinMinecraftClient.java b/fabric-events-interaction-v0/src/main/java/net/fabricmc/fabric/mixin/event/interaction/MixinMinecraftClient.java index 7ecfe6a14..0c0e5c366 100644 --- a/fabric-events-interaction-v0/src/main/java/net/fabricmc/fabric/mixin/event/interaction/MixinMinecraftClient.java +++ b/fabric-events-interaction-v0/src/main/java/net/fabricmc/fabric/mixin/event/interaction/MixinMinecraftClient.java @@ -105,7 +105,7 @@ public abstract class MixinMinecraftClient { @Shadow public abstract ItemStack addBlockEntityNbt(ItemStack itemStack_1, BlockEntity blockEntity_1); - @ModifyVariable(at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/PlayerInventory;getSlotWithStack(Lnet/minecraft/item/ItemStack;)I"), method = "doItemPick", ordinal = 0) + @ModifyVariable(at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/PlayerInventory;getSlotWithStack(Lnet/minecraft/item/ItemStack;)I", shift = At.Shift.BEFORE), method = "doItemPick", ordinal = 0) public ItemStack modifyItemPick(ItemStack stack) { MinecraftClient client = (MinecraftClient) (Object) this; ItemStack result = ClientPickBlockApplyCallback.EVENT.invoker().pick(client.player, client.crosshairTarget, stack); diff --git a/fabric-events-interaction-v0/src/testmod/java/net/fabricmc/fabric/test/event/interaction/PlayerPickBlockTests.java b/fabric-events-interaction-v0/src/testmod/java/net/fabricmc/fabric/test/event/interaction/PlayerPickBlockTests.java new file mode 100644 index 000000000..f548cb4b7 --- /dev/null +++ b/fabric-events-interaction-v0/src/testmod/java/net/fabricmc/fabric/test/event/interaction/PlayerPickBlockTests.java @@ -0,0 +1,41 @@ +/* + * 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.event.interaction; + +import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; +import net.minecraft.util.Hand; + +import net.fabricmc.api.ModInitializer; +import net.fabricmc.fabric.api.event.client.player.ClientPickBlockApplyCallback; + +public class PlayerPickBlockTests implements ModInitializer { + @Override + public void onInitialize() { + ClientPickBlockApplyCallback.EVENT.register((player, result, stack) -> { + if (player.getStackInHand(Hand.MAIN_HAND).getItem() == Items.DIAMOND) { + return new ItemStack(Items.OBSIDIAN); + } + + if (stack.getItem() == Items.GRASS_BLOCK) { + return ItemStack.EMPTY; + } + + return stack; + }); + } +} diff --git a/fabric-events-interaction-v0/src/testmod/resources/fabric.mod.json b/fabric-events-interaction-v0/src/testmod/resources/fabric.mod.json index fc45679c7..04b0c08a6 100644 --- a/fabric-events-interaction-v0/src/testmod/resources/fabric.mod.json +++ b/fabric-events-interaction-v0/src/testmod/resources/fabric.mod.json @@ -10,7 +10,8 @@ }, "entrypoints": { "main": [ - "net.fabricmc.fabric.test.event.interaction.PlayerBreakBlockTests" + "net.fabricmc.fabric.test.event.interaction.PlayerBreakBlockTests", + "net.fabricmc.fabric.test.event.interaction.PlayerPickBlockTests" ] } }