AttackBlockCallback: creative fix and javadoc update (#2691)

* AttackBlockCallback: creative fix and block break delay

* More return value clarifications

* Revert CONSUME_PARTIAL behavior

* Apply suggestions from code review

Co-authored-by: Juuz <6596629+Juuxel@users.noreply.github.com>

Co-authored-by: Juuz <6596629+Juuxel@users.noreply.github.com>
This commit is contained in:
Technici4n 2022-11-29 19:04:08 +01:00 committed by GitHub
parent 2063beff56
commit 422b77fbfd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 19 deletions

View file

@ -19,6 +19,7 @@ package net.fabricmc.fabric.mixin.event.interaction.client;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@ -67,6 +68,18 @@ public abstract class ClientPlayerInteractionManagerMixin {
@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) {
fabric_fireAttackBlockCallback(pos, direction, info);
}
@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/world/GameMode;isCreative()Z", ordinal = 0), method = "updateBlockBreakingProgress", cancellable = true)
public void method_2902(BlockPos pos, Direction direction, CallbackInfoReturnable<Boolean> info) {
if (gameMode.isCreative()) {
fabric_fireAttackBlockCallback(pos, direction, info);
}
}
@Unique
private void fabric_fireAttackBlockCallback(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) {
@ -80,20 +93,6 @@ public abstract class ClientPlayerInteractionManagerMixin {
}
}
@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/world/GameMode;isCreative()Z", ordinal = 0), method = "updateBlockBreakingProgress", cancellable = true)
public void method_2902(BlockPos pos, Direction direction, CallbackInfoReturnable<Boolean> info) {
if (!gameMode.isCreative()) {
return;
}
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);
}
}
@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerInteractionManager;sendSequencedPacket(Lnet/minecraft/client/world/ClientWorld;Lnet/minecraft/client/network/SequencedPacketCreator;)V"), method = "interactBlock", cancellable = true)
public void interactBlock(ClientPlayerEntity player, Hand hand, BlockHitResult blockHitResult, CallbackInfoReturnable<ActionResult> info) {
// hook interactBlock between the world border check and the actual block interaction to invoke the use block event first

View file

@ -30,12 +30,19 @@ import net.fabricmc.fabric.api.event.EventFactory;
* Callback for left-clicking ("attacking") a block.
* Is hooked in before the spectator check, so make sure to check for the player's game mode as well!
*
* <p>Upon return:
* <ul><li>SUCCESS cancels further processing and, on the client, sends a packet to the server.
* <li>PASS falls back to further processing.
* <li>FAIL cancels further processing and does not send a packet to the server.</ul>
* <p>On the logical client, the return values have the following meaning:
* <ul>
* <li>SUCCESS cancels further processing, causes a hand swing, and sends a packet to the server.</li>
* <li>CONSUME cancels further processing, and sends a packet to the server. It does NOT cause a hand swing.</li>
* <li>PASS falls back to further processing.</li>
* <li>FAIL cancels further processing and does not send a packet to the server.</li>
* </ul>
*
* <p>ATTACK_BLOCK does not let you control the packet sending process yet.
* <p>On the logical server, the return values have the following meaning:
* <ul>
* <li>PASS falls back to further processing.</li>
* <li>Any other value cancels further processing.</li>
* </ul>
*/
public interface AttackBlockCallback {
Event<AttackBlockCallback> EVENT = EventFactory.createArrayBacked(AttackBlockCallback.class,