Fix AttackBlockCallback on the client not sending a packet to the server ()

This commit is contained in:
Technici4n 2021-12-03 14:34:52 +01:00 committed by GitHub
parent 63c0105ab0
commit bfa23f17d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 69 additions and 8 deletions
fabric-events-interaction-v0/src
main/java/net/fabricmc/fabric/mixin/event/interaction
testmod
java/net/fabricmc/fabric/test/event/interaction
resources

View file

@ -31,6 +31,7 @@ import net.minecraft.client.world.ClientWorld;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket;
import net.minecraft.network.packet.c2s.play.PlayerInteractBlockC2SPacket;
import net.minecraft.network.packet.c2s.play.PlayerInteractEntityC2SPacket;
import net.minecraft.network.packet.c2s.play.PlayerInteractItemC2SPacket;
@ -52,7 +53,7 @@ import net.fabricmc.fabric.api.event.player.UseEntityCallback;
import net.fabricmc.fabric.api.event.player.UseItemCallback;
@Mixin(ClientPlayerInteractionManager.class)
public class MixinClientPlayerInteractionManager {
public abstract class MixinClientPlayerInteractionManager {
@Shadow
private MinecraftClient client;
@Shadow
@ -60,13 +61,21 @@ public class MixinClientPlayerInteractionManager {
@Shadow
private GameMode gameMode;
@Shadow
protected abstract void sendPlayerAction(PlayerActionC2SPacket.Action action, BlockPos pos, Direction direction);
@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/world/GameMode;isCreative()Z", ordinal = 0), method = "attackBlock", cancellable = true)
public void attackBlock(BlockPos pos, Direction direction, CallbackInfoReturnable<Boolean> info) {
ActionResult result = AttackBlockCallback.EVENT.invoker().interact(client.player, client.world, Hand.MAIN_HAND, pos, direction);
if (result != ActionResult.PASS) {
// Returning true will spawn particles and trigger the animation of the hand -> only for SUCCESS.
info.setReturnValue(result == ActionResult.SUCCESS);
info.cancel();
// We also need to let the server process the action if it's accepted.
if (result.isAccepted()) {
this.sendPlayerAction(PlayerActionC2SPacket.Action.START_DESTROY_BLOCK, pos, direction);
}
}
}
@ -79,8 +88,8 @@ public class MixinClientPlayerInteractionManager {
ActionResult result = AttackBlockCallback.EVENT.invoker().interact(client.player, client.world, Hand.MAIN_HAND, pos, direction);
if (result != ActionResult.PASS) {
// Returning true will spawn particles and trigger the animation of the hand -> only for SUCCESS.
info.setReturnValue(result == ActionResult.SUCCESS);
info.cancel();
}
}
@ -94,7 +103,6 @@ public class MixinClientPlayerInteractionManager {
}
info.setReturnValue(result);
info.cancel();
}
}
@ -108,8 +116,6 @@ public class MixinClientPlayerInteractionManager {
}
info.setReturnValue(result.getResult());
info.cancel();
return;
}
}
@ -137,8 +143,6 @@ public class MixinClientPlayerInteractionManager {
}
info.setReturnValue(result);
info.cancel();
return;
}
}
}

View file

@ -0,0 +1,56 @@
/*
* 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 org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import net.minecraft.block.Blocks;
import net.minecraft.item.Items;
import net.minecraft.util.ActionResult;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.player.AttackBlockCallback;
public class AttackBlockTests implements ModInitializer {
private static final Logger LOGGER = LogManager.getLogger();
@Override
public void onInitialize() {
AttackBlockCallback.EVENT.register((player, world, hand, pos, side) -> {
LOGGER.info("AttackBlockCallback: before chest/lava hook (client-side = %s)".formatted(world.isClient));
return ActionResult.PASS;
});
// If a chest is attacked and the player holds a lava bucket, delete it!
AttackBlockCallback.EVENT.register((player, world, hand, pos, side) -> {
if (!player.isSpectator() && world.canPlayerModifyAt(player, pos)) {
if (world.getBlockState(pos).isOf(Blocks.CHEST)) {
if (player.getStackInHand(hand).isOf(Items.LAVA_BUCKET)) {
world.setBlockState(pos, Blocks.AIR.getDefaultState());
return ActionResult.success(world.isClient);
}
}
}
return ActionResult.PASS;
});
AttackBlockCallback.EVENT.register((player, world, hand, pos, side) -> {
LOGGER.info("AttackBlockCallback: after chest/lava hook (client-side = %s)".formatted(world.isClient));
return ActionResult.PASS;
});
}
}

View file

@ -10,6 +10,7 @@
},
"entrypoints": {
"main": [
"net.fabricmc.fabric.test.event.interaction.AttackBlockTests",
"net.fabricmc.fabric.test.event.interaction.PlayerBreakBlockTests",
"net.fabricmc.fabric.test.event.interaction.PlayerPickBlockTests"
]