Skip to content

Commit

Permalink
fix: work around back
Browse files Browse the repository at this point in the history
  • Loading branch information
zly2006 committed Jun 6, 2024
1 parent c0a7532 commit ee67261
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.github.zly2006.enclosure.access;

public interface MiningStatusAccess {
boolean success();
boolean enclosure$success();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.github.zly2006.enclosure.mixin.workaround;

import com.github.zly2006.enclosure.access.MiningStatusAccess;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.network.listener.ClientPlayPacketListener;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket;
import net.minecraft.network.packet.s2c.play.BlockUpdateS2CPacket;
import net.minecraft.network.packet.s2c.play.PlayerActionResponseS2CPacket;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

/**
* This mixin is a work-around for the Minecraft mining system.
*/
@Mixin(ServerPlayNetworkHandler.class)
public class MixinServerPlayNetworkHandler {
@Shadow public ServerPlayerEntity player;

public MixinServerPlayNetworkHandler(ServerPlayerEntity player) {
this.player = player;
}

@Inject(
method = "onPlayerAction",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/server/network/ServerPlayNetworkHandler;updateSequence(I)V",
shift = At.Shift.AFTER
)
)
private void onPlayerAction(PlayerActionC2SPacket packet, CallbackInfo ci) {
switch (packet.getAction()) {
case START_DESTROY_BLOCK, STOP_DESTROY_BLOCK, ABORT_DESTROY_BLOCK -> {
if (!((MiningStatusAccess) player.interactionManager).enclosure$success()) {
// firstly, respond to the client's mining request
// Note: the client will roll back the block changes when receiving this packet,
// but it will **NOT** roll back the block entity.
if (player.networkHandler.sequence > -1) {
player.networkHandler.sendPacket(new PlayerActionResponseS2CPacket(player.networkHandler.sequence));
player.networkHandler.sequence = -1;
}
// then, sync the block to the client
player.networkHandler.sendPacket(new BlockUpdateS2CPacket(player.getWorld(), packet.getPos()));
BlockEntity entity = player.getWorld().getBlockEntity(packet.getPos());
if (entity != null) {
Packet<ClientPlayPacketListener> syncPacket = entity.toUpdatePacket();
if (syncPacket != null) {
// finally, sync the block entity to the client
player.networkHandler.sendPacket(syncPacket);
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.github.zly2006.enclosure.mixin.workaround;

import com.github.zly2006.enclosure.access.MiningStatusAccess;
import net.minecraft.server.network.ServerPlayerInteractionManager;
import net.minecraft.util.math.BlockPos;
import org.spongepowered.asm.mixin.Mixin;
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;

@Mixin(ServerPlayerInteractionManager.class)
public abstract class MixinServerPlayerInteractionManager implements MiningStatusAccess {
@Unique
boolean miningSuccess = false;
@Inject(method = "onBlockBreakingAction", at = @At("HEAD"))
private void afterBreakingBlock(BlockPos pos, boolean success, int sequence, String reason, CallbackInfo ci) {
miningSuccess = success;
}

@Override
public boolean enclosure$success() {
return miningSuccess;
}
}
4 changes: 3 additions & 1 deletion src/main/resources/enclosure.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
"MixinVehicleEntity",
"MixinWitherEntity",
"MixinWitherSkullBlock",
"workaround.MixinRegistrySyncManager"
"workaround.MixinRegistrySyncManager",
"workaround.MixinServerPlayerInteractionManager",
"workaround.MixinServerPlayNetworkHandler"
]
}

0 comments on commit ee67261

Please sign in to comment.