Add swapping when attacking breaks tool

This commit is contained in:
Shadowfacts 2019-10-22 16:23:57 -04:00
parent cb7b224335
commit 63064d3d76
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
4 changed files with 68 additions and 5 deletions

View File

@ -0,0 +1,49 @@
package net.shadowfacts.autoswap.mixin;
import net.minecraft.item.ItemStack;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.shadowfacts.autoswap.AutoSwap;
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;
/**
* @author shadowfacts
*/
@Mixin(ServerPlayNetworkHandler.class)
public abstract class MixinServerPlayNetworkHandler {
@Shadow
private ServerPlayerEntity player;
private ItemStack heldStackBeforeAttack = ItemStack.EMPTY;
@Inject(
method = "onPlayerInteractEntity",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/server/network/ServerPlayerEntity;attack(Lnet/minecraft/entity/Entity;)V"
)
)
private void beforePlayerAttack(CallbackInfo cb) {
this.heldStackBeforeAttack = player.inventory.getMainHandStack().copy();
}
@Inject(
method = "onPlayerInteractEntity",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/server/network/ServerPlayerEntity;attack(Lnet/minecraft/entity/Entity;)V",
shift = At.Shift.AFTER
)
)
private void afterPlayerAttack(CallbackInfo cb) {
AutoSwap.INSTANCE.afterAttack(player.inventory, heldStackBeforeAttack);
heldStackBeforeAttack = ItemStack.EMPTY;
}
}

View File

@ -1,6 +1,5 @@
package net.shadowfacts.autoswap.mixin; package net.shadowfacts.autoswap.mixin;
import net.minecraft.entity.player.PlayerInventory;
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;
@ -24,12 +23,14 @@ public abstract class MixinServerPlayerInteractionManager {
@Shadow @Shadow
private ServerPlayerEntity player; private ServerPlayerEntity player;
@Shadow
abstract boolean isCreative();
private ItemStack heldStackAtBeginningOfBreak = ItemStack.EMPTY; private ItemStack heldStackAtBeginningOfBreak = 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) {
PlayerInventory playerInv = player.inventory; heldStackAtBeginningOfBreak = player.inventory.getMainHandStack().copy();
heldStackAtBeginningOfBreak = playerInv.getInvStack(playerInv.selectedSlot).copy();
} }
@Inject(method = "tryBreakBlock", at = @At("RETURN")) @Inject(method = "tryBreakBlock", at = @At("RETURN"))
@ -45,7 +46,7 @@ public abstract class MixinServerPlayerInteractionManager {
private ActionResult useOnBlock(ItemStack self, ItemUsageContext context) { private ActionResult useOnBlock(ItemStack self, ItemUsageContext context) {
ItemStack selfCopy = self.copy(); ItemStack selfCopy = self.copy();
ActionResult result = self.useOnBlock(context); ActionResult result = self.useOnBlock(context);
if (result == ActionResult.SUCCESS) { if (!isCreative() && result == ActionResult.SUCCESS) {
AutoSwap.INSTANCE.onUseOnItem(player, player.inventory, selfCopy); AutoSwap.INSTANCE.onUseOnItem(player, player.inventory, selfCopy);
} }
return result; return result;

View File

@ -44,4 +44,16 @@ object AutoSwap: ModInitializer {
} }
} }
fun afterAttack(playerInv: PlayerInventory, heldStackBeforeAttack: ItemStack) {
val heldStack = playerInv.mainHandStack
if (!heldStackBeforeAttack.isEmpty && heldStack.isEmpty) {
val index = playerInv.main.indexOfFirst {
ItemStack.areItemsEqualIgnoreDamage(heldStackBeforeAttack, it)
}
if (index >= 0) {
playerInv.swapSlotWithHotbar(index)
}
}
}
} }

View File

@ -3,7 +3,8 @@
"package": "net.shadowfacts.autoswap.mixin", "package": "net.shadowfacts.autoswap.mixin",
"compatibilityLevel": "JAVA_8", "compatibilityLevel": "JAVA_8",
"mixins": [ "mixins": [
"MixinServerPlayerInteractionManager" "MixinServerPlayerInteractionManager",
"MixinServerPlayNetworkHandler"
], ],
"client": [ "client": [
], ],