2019-10-22 15:05:49 +00:00
|
|
|
package net.shadowfacts.autoswap.mixin;
|
|
|
|
|
2019-10-22 22:29:44 +00:00
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
2019-10-22 15:05:49 +00:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.item.ItemUsageContext;
|
|
|
|
import net.minecraft.server.network.ServerPlayerEntity;
|
|
|
|
import net.minecraft.server.network.ServerPlayerInteractionManager;
|
|
|
|
import net.minecraft.util.ActionResult;
|
2019-10-22 22:29:44 +00:00
|
|
|
import net.minecraft.util.Hand;
|
|
|
|
import net.minecraft.util.hit.BlockHitResult;
|
2019-10-22 15:05:49 +00:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2019-10-22 22:29:44 +00:00
|
|
|
import net.minecraft.world.World;
|
2019-10-22 15:05:49 +00:00
|
|
|
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.Redirect;
|
|
|
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
|
|
|
|
2019-10-22 22:29:44 +00:00
|
|
|
import java.awt.dnd.Autoscroll;
|
|
|
|
|
2019-10-22 15:05:49 +00:00
|
|
|
/**
|
|
|
|
* @author shadowfacts
|
|
|
|
*/
|
|
|
|
@Mixin(ServerPlayerInteractionManager.class)
|
|
|
|
public abstract class MixinServerPlayerInteractionManager {
|
|
|
|
|
|
|
|
@Shadow
|
|
|
|
private ServerPlayerEntity player;
|
|
|
|
|
2019-10-22 20:23:57 +00:00
|
|
|
@Shadow
|
|
|
|
abstract boolean isCreative();
|
|
|
|
|
2019-10-22 15:05:49 +00:00
|
|
|
private ItemStack heldStackAtBeginningOfBreak = ItemStack.EMPTY;
|
2019-10-22 22:29:44 +00:00
|
|
|
private ItemStack heldStackAtBeginningOfUse = ItemStack.EMPTY;
|
2019-10-22 15:05:49 +00:00
|
|
|
|
|
|
|
@Inject(method = "tryBreakBlock", at = @At("HEAD"))
|
|
|
|
private void beginTryBreakBlock(BlockPos pos, CallbackInfoReturnable<Void> cb) {
|
2019-10-22 20:23:57 +00:00
|
|
|
heldStackAtBeginningOfBreak = player.inventory.getMainHandStack().copy();
|
2019-10-22 15:05:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Inject(method = "tryBreakBlock", at = @At("RETURN"))
|
|
|
|
private void endTryBreakBlock(BlockPos pos, CallbackInfoReturnable<Void> cb) {
|
|
|
|
AutoSwap.INSTANCE.endBreakBlock(player.inventory, heldStackAtBeginningOfBreak);
|
|
|
|
heldStackAtBeginningOfBreak = ItemStack.EMPTY;
|
|
|
|
}
|
|
|
|
|
2019-10-22 22:29:44 +00:00
|
|
|
@Inject(
|
2019-10-22 15:05:49 +00:00
|
|
|
method = "interactBlock",
|
|
|
|
at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;useOnBlock(Lnet/minecraft/item/ItemUsageContext;)Lnet/minecraft/util/ActionResult;")
|
|
|
|
)
|
2019-10-22 22:29:44 +00:00
|
|
|
private void beforeUseOnBlock(PlayerEntity player, World world, ItemStack heldStack, Hand hand, BlockHitResult result, CallbackInfoReturnable<ActionResult> cb) {
|
|
|
|
heldStackAtBeginningOfUse = heldStack.copy();
|
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
2019-10-22 15:05:49 +00:00
|
|
|
}
|
2019-10-22 22:29:44 +00:00
|
|
|
heldStackAtBeginningOfUse = ItemStack.EMPTY;
|
2019-10-22 15:05:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|