Search in sources :

Example 46 with Redirect

use of org.spongepowered.asm.mixin.injection.Redirect in project SpongeCommon by SpongePowered.

the class ServerGamePacketListenerImplMixin_Inventory method impl$onHandlePlayerCommandOpenInventory.

@Redirect(method = "handlePlayerCommand", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/animal/horse/AbstractHorse;openInventory(Lnet/minecraft/world/entity/player/Player;)V"))
private void impl$onHandlePlayerCommandOpenInventory(final AbstractHorse abstractHorse, final Player player) {
    final PhaseContext<@NonNull ?> context = PhaseTracker.SERVER.getPhaseContext();
    try (final EffectTransactor ignored = context.getTransactor().logOpenInventory(player)) {
        abstractHorse.openInventory(player);
        context.getTransactor().logContainerSet(player);
    }
}
Also used : EffectTransactor(org.spongepowered.common.event.tracking.context.transaction.EffectTransactor) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 47 with Redirect

use of org.spongepowered.asm.mixin.injection.Redirect in project SpongeCommon by SpongePowered.

the class ItemEntityMixin_Inventory method spongeImpl$throwPickupEventForAddItem.

@Redirect(method = "playerTouch", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/player/Inventory;add(Lnet/minecraft/world/item/ItemStack;)Z"))
private boolean spongeImpl$throwPickupEventForAddItem(final Inventory inventory, final ItemStack itemStack, final Player player) {
    final PhaseContext<@NonNull ?> context = PhaseTracker.SERVER.getPhaseContext();
    final TransactionalCaptureSupplier transactor = context.getTransactor();
    final boolean added;
    try (final EffectTransactor ignored = transactor.logPlayerInventoryChangeWithEffect(player, PlayerInventoryTransaction.EventCreator.PICKUP)) {
        added = inventory.add(itemStack);
    }
    if (!TrackingUtil.processBlockCaptures(context)) {
        // if PickupEvent was cancelled return false
        return false;
    }
    return added;
}
Also used : TransactionalCaptureSupplier(org.spongepowered.common.event.tracking.context.transaction.TransactionalCaptureSupplier) EffectTransactor(org.spongepowered.common.event.tracking.context.transaction.EffectTransactor) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 48 with Redirect

use of org.spongepowered.asm.mixin.injection.Redirect in project SpongeCommon by SpongePowered.

the class AbstractContainerMenuMixin_Inventory method inventory$wrapDoClickWithTransaction.

@Redirect(method = "clicked", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/inventory/AbstractContainerMenu;doClick(IILnet/minecraft/world/inventory/ClickType;Lnet/minecraft/world/entity/player/Player;)Lnet/minecraft/world/item/ItemStack;"))
private ItemStack inventory$wrapDoClickWithTransaction(final AbstractContainerMenu menu, final int slotId, final int dragType, final ClickType clickType, final Player player) {
    if (((LevelBridge) player.level).bridge$isFake()) {
        return this.shadow$doClick(slotId, dragType, clickType, player);
    }
    final PhaseContext<@NonNull ?> context = PhaseTracker.SERVER.getPhaseContext();
    final TransactionalCaptureSupplier transactor = context.getTransactor();
    try (final EffectTransactor ignored = transactor.logClickContainer(menu, slotId, dragType, clickType, player)) {
        this.impl$isClicking = true;
        return this.shadow$doClick(slotId, dragType, clickType, player);
    } finally {
        this.impl$isClicking = false;
    }
}
Also used : LevelBridge(org.spongepowered.common.bridge.world.level.LevelBridge) TransactionalCaptureSupplier(org.spongepowered.common.event.tracking.context.transaction.TransactionalCaptureSupplier) EffectTransactor(org.spongepowered.common.event.tracking.context.transaction.EffectTransactor) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 49 with Redirect

use of org.spongepowered.asm.mixin.injection.Redirect in project SpongeCommon by SpongePowered.

the class CraftingMenuMixin_Inventory method beforeSlotChangedCraftingGrid.

// Crafting Preview
@Redirect(method = "slotChangedCraftingGrid", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/inventory/ResultContainer;setItem(ILnet/minecraft/world/item/ItemStack;)V"))
private static void beforeSlotChangedCraftingGrid(final ResultContainer resultContainer, final int slotId, final ItemStack itemStack, final int param0, final Level param1, final Player player, final CraftingContainer craftingContainer, final ResultContainer resultcontainer) {
    resultContainer.setItem(slotId, itemStack);
    final Inventory inv = ((Inventory) player.containerMenu).query(QueryTypes.INVENTORY_TYPE.get().of(CraftingInventory.class));
    if (!(inv instanceof CraftingInventory)) {
        SpongeCommon.logger().warn("Detected crafting but Sponge could not get a CraftingInventory for " + player.containerMenu.getClass().getName());
        return;
    }
    final PhaseContext<@NonNull ?> context = PhaseTracker.SERVER.getPhaseContext();
    if (!context.isRestoring()) {
        final TransactionalCaptureSupplier transactor = context.getTransactor();
        transactor.logCraftingPreview((ServerPlayer) player, (CraftingInventory) inv, craftingContainer);
    }
}
Also used : CraftingInventory(org.spongepowered.api.item.inventory.crafting.CraftingInventory) TransactionalCaptureSupplier(org.spongepowered.common.event.tracking.context.transaction.TransactionalCaptureSupplier) Inventory(org.spongepowered.api.item.inventory.Inventory) CraftingInventory(org.spongepowered.api.item.inventory.crafting.CraftingInventory) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 50 with Redirect

use of org.spongepowered.asm.mixin.injection.Redirect in project SpongeCommon by SpongePowered.

the class SkullBlockEntityMixin method onUpdateProfile.

/**
 * @reason Don't block the main thread while attempting to lookup a game profile.
 */
@Redirect(method = "updateOwnerProfile()V", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/block/entity/SkullBlockEntity;updateGameprofile(Lcom/mojang/authlib/GameProfile;)Lcom/mojang/authlib/GameProfile;"))
private GameProfile onUpdateProfile(final GameProfile input) {
    this.cancelResolveFuture();
    if (input == null) {
        return null;
    }
    if (input.isComplete() && input.getProperties().containsKey("textures")) {
        return input;
    }
    final GameProfileManager manager = Sponge.server().gameProfileManager();
    CompletableFuture<org.spongepowered.api.profile.GameProfile> future = null;
    if (input.getId() != null) {
        future = manager.profile(input.getId());
    } else if (!StringUtil.isNullOrEmpty(input.getName())) {
        future = manager.profile(input.getName());
    }
    if (future == null) {
        return input;
    }
    future.thenAcceptAsync(profile -> {
        this.owner = SpongeGameProfile.toMcProfile(profile);
        this.setChanged();
    }, SpongeCommon.server());
    this.impl$currentProfileFuture = future;
    return input;
}
Also used : GameProfileManager(org.spongepowered.api.profile.GameProfileManager) GameProfile(com.mojang.authlib.GameProfile) SpongeGameProfile(org.spongepowered.common.profile.SpongeGameProfile) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Aggregations

Redirect (org.spongepowered.asm.mixin.injection.Redirect)165 CauseStackManager (org.spongepowered.api.event.CauseStackManager)25 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)15 ItemStack (net.minecraft.item.ItemStack)13 EffectTransactor (org.spongepowered.common.event.tracking.context.transaction.EffectTransactor)13 PhaseTracker (org.spongepowered.common.event.tracking.PhaseTracker)12 TransactionalCaptureSupplier (org.spongepowered.common.event.tracking.context.transaction.TransactionalCaptureSupplier)11 ArrayList (java.util.ArrayList)9 ItemStack (net.minecraft.world.item.ItemStack)9 Nullable (javax.annotation.Nullable)8 Vector3d (com.flowpowered.math.vector.Vector3d)7 World (net.minecraft.world.World)7 ServerLocation (org.spongepowered.api.world.server.ServerLocation)7 DamageSourceBridge (org.spongepowered.common.bridge.world.damagesource.DamageSourceBridge)7 IPhaseState (org.spongepowered.common.event.tracking.IPhaseState)7 IBlockState (net.minecraft.block.state.IBlockState)6 Entity (net.minecraft.entity.Entity)6 EntityPlayer (net.minecraft.entity.player.EntityPlayer)6 ServerLevel (net.minecraft.server.level.ServerLevel)6 EntityItem (net.minecraft.entity.item.EntityItem)5