|
| 1 | +/* |
| 2 | + * This program is free software; you can redistribute it and/or |
| 3 | + * modify it under the terms of the GNU Lesser General Public |
| 4 | + * License as published by the Free Software Foundation; either |
| 5 | + * version 3 of the License, or (at your option) any later version. |
| 6 | + * |
| 7 | + * This program is distributed in the hope that it will be useful, |
| 8 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 10 | + * Lesser General Public License for more details. |
| 11 | + * |
| 12 | + * You should have received a copy of the GNU Lesser General Public License |
| 13 | + * along with this program; if not, write to the Free Software Foundation, |
| 14 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 15 | + * |
| 16 | + * For more information, see the LICENSE file. |
| 17 | + */ |
| 18 | + |
| 19 | +package io.github.axolotlclient.oldanimations.mixin; |
| 20 | + |
| 21 | +import com.llamalad7.mixinextras.injector.ModifyExpressionValue; |
| 22 | +import com.llamalad7.mixinextras.injector.v2.WrapWithCondition; |
| 23 | +import io.github.axolotlclient.oldanimations.config.OldAnimationsConfig; |
| 24 | +import io.github.axolotlclient.oldanimations.util.ducks.IClientPlayerInteractionManager; |
| 25 | +import net.minecraft.client.ClientPlayerInteractionManager; |
| 26 | +import net.minecraft.client.Minecraft; |
| 27 | +import net.minecraft.client.network.handler.ClientPlayNetworkHandler; |
| 28 | +import net.minecraft.network.packet.Packet; |
| 29 | +import net.minecraft.util.math.BlockPos; |
| 30 | +import org.objectweb.asm.Opcodes; |
| 31 | +import org.spongepowered.asm.mixin.Final; |
| 32 | +import org.spongepowered.asm.mixin.Mixin; |
| 33 | +import org.spongepowered.asm.mixin.Shadow; |
| 34 | +import org.spongepowered.asm.mixin.injection.At; |
| 35 | + |
| 36 | +@Mixin(ClientPlayerInteractionManager.class) |
| 37 | +public abstract class ClientPlayerInteractionManagerMixin implements IClientPlayerInteractionManager { |
| 38 | + |
| 39 | + @Shadow |
| 40 | + private boolean isMiningBlock; |
| 41 | + |
| 42 | + @Shadow |
| 43 | + private float miningProgress; |
| 44 | + |
| 45 | + @Shadow |
| 46 | + @Final |
| 47 | + private Minecraft minecraft; |
| 48 | + |
| 49 | + @Shadow |
| 50 | + private BlockPos target; |
| 51 | + |
| 52 | + @ModifyExpressionValue(method = "stopMiningBlock", at = @At(value = "FIELD", opcode = Opcodes.GETFIELD, target = "Lnet/minecraft/client/ClientPlayerInteractionManager;isMiningBlock:Z")) |
| 53 | + private boolean axolotlclient$allowStateReset(boolean original) { |
| 54 | + if (OldAnimationsConfig.isEnabled() && OldAnimationsConfig.instance.miningProgressResetLogic.get()) { |
| 55 | + /* in 1.8, mining progress ONLY resets when the mining state is true. */ |
| 56 | + /* this may sound logical, however, it's entirely possible for there */ |
| 57 | + /* to be mining progress while the mining state is false in 1.8. */ |
| 58 | + |
| 59 | + /* this modification is not entirely needed to replicate 1.7 behavior since the modification done in */ |
| 60 | + /* axolotlclient$fixMiningStatePacketLogic tackles the issue head on */ |
| 61 | + return true; |
| 62 | + } |
| 63 | + return original; |
| 64 | + } |
| 65 | + |
| 66 | + @WrapWithCondition(method = "stopMiningBlock", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/handler/ClientPlayNetworkHandler;sendPacket(Lnet/minecraft/network/packet/Packet;)V")) |
| 67 | + private boolean axolotlclient$sendIfMiningBlock(ClientPlayNetworkHandler instance, Packet<?> packet) { |
| 68 | + if (OldAnimationsConfig.isEnabled() && OldAnimationsConfig.instance.miningProgressResetLogic.get()) { |
| 69 | + /* the mining abort packet should only be sent if the player is genuinely in the mining state like in 1.7 */ |
| 70 | + return isMiningBlock; |
| 71 | + } |
| 72 | + return true; |
| 73 | + } |
| 74 | + |
| 75 | + @ModifyExpressionValue(method = "updateBlockMining", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/ClientPlayerInteractionManager;isMiningBlock(Lnet/minecraft/util/math/BlockPos;)Z")) |
| 76 | + private boolean axolotlclient$fixMiningStatePacketLogic(boolean original) { |
| 77 | + if (OldAnimationsConfig.isEnabled() && OldAnimationsConfig.instance.miningProgressResetLogic.get()) { |
| 78 | + /* this code is not taken from 1.7 exactly, rather, it's an attempt to recreate a similar mining progress resetting behavior */ |
| 79 | + /* in 1.7, block mining progress resets when the player is not mining regardless of the circumstances */ |
| 80 | + /* this code is better than porting 1.7's code as it works with 1.8's packet order */ |
| 81 | + |
| 82 | + /* Moulberry (the goat) came up with the fix and gives a simple explanation in MC-255057 */ |
| 83 | + |
| 84 | + /* to really flesh out his explanation, the game basically determines whether the mining progress should be stopped/started */ |
| 85 | + /* based on whether the block that the player is mining is in the place that it was mined at after mining is complete. */ |
| 86 | + /* the game will also check whether the tool that is mining is the same tool that the player is holding. */ |
| 87 | + /* it does not consider the current mining progress or mining state of the block, just it's position and whether the mining tool's integrity. */ |
| 88 | + /* if the server spawns the block back instantly, therefore making the block occupy the same position as the one previously mined, */ |
| 89 | + /* then the client will think that previous mining operation is continuing just due to the fact that the block is still there in the same place */ |
| 90 | + /* which will result in a start mining packet not being sent at the appropriate moment. */ |
| 91 | + /* this results in a sort of de-sync where the mining progress continues to exist even after mining technically ended */ |
| 92 | + /* this can be easily amended by checking if mining state is currently in progress or complete. */ |
| 93 | + /* if mining is not in progress (false), then the if statement will fail and the else statement would be enacted. */ |
| 94 | + /* startMiningBlock will be invoked which will send a start mining packet appropriately, and the universe will be at peace at last. */ |
| 95 | + |
| 96 | + /* interestingly enough, startMiningBlock actually has this exact logic but inverted when determining which packet to send when attempting to mine */ |
| 97 | + return original && isMiningBlock; |
| 98 | + } |
| 99 | + return original; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void axolotlclient$fakeStopMiningBlock() { |
| 104 | + /* no mining abort packet shall be sent... grim don't hurt me :C */ |
| 105 | + isMiningBlock = false; |
| 106 | + miningProgress = 0.0F; |
| 107 | + minecraft.world.updateBlockMiningProgress(minecraft.player.getNetworkId(), target, -1); |
| 108 | + } |
| 109 | +} |
0 commit comments