-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
91 additions
and
2 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
src/main/java/com/github/zly2006/enclosure/access/MiningStatusAccess.java
This file contains 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package com.github.zly2006.enclosure.access; | ||
|
||
public interface MiningStatusAccess { | ||
boolean success(); | ||
boolean enclosure$success(); | ||
} |
62 changes: 62 additions & 0 deletions
62
...ain/java/com/github/zly2006/enclosure/mixin/workaround/MixinServerPlayNetworkHandler.java
This file contains 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,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); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...va/com/github/zly2006/enclosure/mixin/workaround/MixinServerPlayerInteractionManager.java
This file contains 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,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; | ||
} | ||
} |
This file contains 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