Compare commits

...

2 Commits

Author SHA1 Message Date
Shadowfacts d5873821ab
Bump version 2019-10-22 16:25:52 -04:00
Shadowfacts 63064d3d76
Add swapping when attacking breaks tool 2019-10-22 16:23:57 -04:00
5 changed files with 69 additions and 6 deletions

View File

@ -16,7 +16,7 @@ base {
}
group = "net.shadowfacts"
version = "0.1.0"
version = "0.2.0"
val minecraftVersion = "1.14.4"
val yarnMappings = "1.14.4+build.9"

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

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",
"compatibilityLevel": "JAVA_8",
"mixins": [
"MixinServerPlayerInteractionManager"
"MixinServerPlayerInteractionManager",
"MixinServerPlayNetworkHandler"
],
"client": [
],