Fix using item in off-hand slot not being swapped

This commit is contained in:
Shadowfacts 2019-10-22 18:29:44 -04:00
parent 12d4d9ed6d
commit 39a02c2619
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 34 additions and 12 deletions

View File

@ -1,11 +1,15 @@
package net.shadowfacts.autoswap.mixin; package net.shadowfacts.autoswap.mixin;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUsageContext; import net.minecraft.item.ItemUsageContext;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.network.ServerPlayerInteractionManager; import net.minecraft.server.network.ServerPlayerInteractionManager;
import net.minecraft.util.ActionResult; import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.shadowfacts.autoswap.AutoSwap; import net.shadowfacts.autoswap.AutoSwap;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Shadow;
@ -14,6 +18,8 @@ import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect; import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import java.awt.dnd.Autoscroll;
/** /**
* @author shadowfacts * @author shadowfacts
*/ */
@ -27,6 +33,7 @@ public abstract class MixinServerPlayerInteractionManager {
abstract boolean isCreative(); abstract boolean isCreative();
private ItemStack heldStackAtBeginningOfBreak = ItemStack.EMPTY; private ItemStack heldStackAtBeginningOfBreak = ItemStack.EMPTY;
private ItemStack heldStackAtBeginningOfUse = ItemStack.EMPTY;
@Inject(method = "tryBreakBlock", at = @At("HEAD")) @Inject(method = "tryBreakBlock", at = @At("HEAD"))
private void beginTryBreakBlock(BlockPos pos, CallbackInfoReturnable<Void> cb) { private void beginTryBreakBlock(BlockPos pos, CallbackInfoReturnable<Void> cb) {
@ -39,17 +46,27 @@ public abstract class MixinServerPlayerInteractionManager {
heldStackAtBeginningOfBreak = ItemStack.EMPTY; heldStackAtBeginningOfBreak = ItemStack.EMPTY;
} }
@Redirect( @Inject(
method = "interactBlock", method = "interactBlock",
at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;useOnBlock(Lnet/minecraft/item/ItemUsageContext;)Lnet/minecraft/util/ActionResult;") at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;useOnBlock(Lnet/minecraft/item/ItemUsageContext;)Lnet/minecraft/util/ActionResult;")
) )
private ActionResult useOnBlock(ItemStack self, ItemUsageContext context) { private void beforeUseOnBlock(PlayerEntity player, World world, ItemStack heldStack, Hand hand, BlockHitResult result, CallbackInfoReturnable<ActionResult> cb) {
ItemStack selfCopy = self.copy(); heldStackAtBeginningOfUse = heldStack.copy();
ActionResult result = self.useOnBlock(context);
if (!isCreative() && result == ActionResult.SUCCESS) {
AutoSwap.INSTANCE.onUseOnItem(player, player.inventory, selfCopy);
} }
return result;
@Inject(
method = "interactBlock",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/item/ItemStack;useOnBlock(Lnet/minecraft/item/ItemUsageContext;)Lnet/minecraft/util/ActionResult;",
shift = At.Shift.AFTER
)
)
private void afterUseOnBlock(PlayerEntity player, World world, ItemStack heldStack, Hand hand, BlockHitResult result, CallbackInfoReturnable<ActionResult> cb) {
if (!isCreative() && cb.getReturnValue() == ActionResult.SUCCESS) {
AutoSwap.INSTANCE.afterUseOnBlock(this.player, player.inventory, heldStackAtBeginningOfUse, hand);
}
heldStackAtBeginningOfUse = ItemStack.EMPTY;
} }
} }

View File

@ -8,6 +8,7 @@ import net.minecraft.entity.player.PlayerEntity
import net.minecraft.entity.player.PlayerInventory import net.minecraft.entity.player.PlayerInventory
import net.minecraft.item.ItemStack import net.minecraft.item.ItemStack
import net.minecraft.server.network.ServerPlayerEntity import net.minecraft.server.network.ServerPlayerEntity
import net.minecraft.util.Hand
import net.minecraft.util.math.BlockPos import net.minecraft.util.math.BlockPos
/** /**
@ -30,16 +31,20 @@ object AutoSwap: ModInitializer {
} }
} }
fun onUseOnItem(player: ServerPlayerEntity, playerInv: PlayerInventory, heldStackBeforeUse: ItemStack) { fun afterUseOnBlock(player: ServerPlayerEntity, playerInv: PlayerInventory, heldStackBeforeUse: ItemStack, hand: Hand) {
val heldStack = playerInv.mainHandStack val heldStack = player.getStackInHand(hand)
if (!player.isCreative && heldStack.isEmpty) { if (!player.isCreative && heldStack.isEmpty) {
val index = playerInv.main.indexOfFirst { val index = playerInv.main.indexOfFirst {
ItemStack.areItemsEqualIgnoreDamage(heldStackBeforeUse, it) ItemStack.areItemsEqualIgnoreDamage(heldStackBeforeUse, it)
} }
if (index >= 0) { if (index >= 0) {
playerInv.swapSlotWithHotbar(index) val newStack = playerInv.removeInvStack(index)
val newStack = playerInv.mainHandStack player.setStackInHand(hand, newStack)
player.networkHandler.sendPacket(EntityEquipmentUpdateS2CPacket(player.entityId, EquipmentSlot.MAINHAND, newStack)) val slot = when (hand) {
Hand.MAIN_HAND -> EquipmentSlot.MAINHAND
Hand.OFF_HAND -> EquipmentSlot.OFFHAND
}
player.networkHandler.sendPacket(EntityEquipmentUpdateS2CPacket(player.entityId, slot, newStack))
} }
} }
} }