Browse Source

Add swapping when attacking breaks tool

master
Shadowfacts 2 years ago
parent
commit
63064d3d76
Signed by: shadowfacts GPG Key ID: 94A5AB95422746E5
  1. 49
      src/main/java/net/shadowfacts/autoswap/mixin/MixinServerPlayNetworkHandler.java
  2. 9
      src/main/java/net/shadowfacts/autoswap/mixin/MixinServerPlayerInteractionManager.java
  3. 12
      src/main/kotlin/net/shadowfacts/autoswap/AutoSwap.kt
  4. 3
      src/main/resources/autoswap.mixins.json

49
src/main/java/net/shadowfacts/autoswap/mixin/MixinServerPlayNetworkHandler.java

@ -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;
}
}

9
src/main/java/net/shadowfacts/autoswap/mixin/MixinServerPlayerInteractionManager.java

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

12
src/main/kotlin/net/shadowfacts/autoswap/AutoSwap.kt

@ -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)
}
}
}
}

3
src/main/resources/autoswap.mixins.json

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

Loading…
Cancel
Save