Search in sources :

Example 41 with Redirect

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

the class OcelotMixin method impl$ThrowTameEvent.

@Redirect(method = "mobInteract", at = @At(value = "INVOKE", target = "Ljava/util/Random;nextInt(I)I", ordinal = 0, remap = false))
private int impl$ThrowTameEvent(Random rand, int bound, Player player, InteractionHand hand) {
    ItemStack stack = player.getItemInHand(hand);
    int random = rand.nextInt(bound);
    if (random == 0) {
        stack.setCount(stack.getCount() + 1);
        try (CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
            frame.pushCause(player);
            if (!SpongeCommon.post(SpongeEventFactory.createTameEntityEvent(frame.currentCause(), (Ocelot) this))) {
                stack.setCount(stack.getCount() - 1);
                return random;
            }
        }
    }
    return 1;
}
Also used : CauseStackManager(org.spongepowered.api.event.CauseStackManager) ItemStack(net.minecraft.world.item.ItemStack) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 42 with Redirect

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

the class FallingBlockEntityMixin method spongeAttackFallingOrAnvil.

// @formatter:on
@SuppressWarnings("ConstantConditions")
@Redirect(method = "causeFallDamage", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/Entity;hurt(Lnet/minecraft/world/damagesource/DamageSource;F)Z"))
private boolean spongeAttackFallingOrAnvil(final Entity entity, final DamageSource source, final float damage) {
    if (entity.level.isClientSide) {
        return entity.hurt(source, damage);
    }
    final boolean isAnvil = this.blockState.getBlock().is(BlockTags.ANVIL);
    try {
        if (isAnvil) {
            final MinecraftFallingBlockDamageSource anvil = new MinecraftFallingBlockDamageSource("anvil", (FallingBlockEntity) (Object) this);
            ((DamageSourceBridge) (Object) anvil).bridge$setAnvilSource();
            return entity.hurt(DamageSource.ANVIL, damage);
        } else {
            final MinecraftFallingBlockDamageSource fallingblock = new MinecraftFallingBlockDamageSource("fallingblock", (FallingBlockEntity) (Object) this);
            ((DamageSourceBridge) (Object) fallingblock).bridge$setFallingBlockSource();
            return entity.hurt(DamageSource.FALLING_BLOCK, damage);
        }
    } finally {
        if (isAnvil) {
            ((DamageSourceBridge) source).bridge$setAnvilSource();
        } else {
            ((DamageSourceBridge) source).bridge$setFallingBlockSource();
        }
    }
}
Also used : DamageSourceBridge(org.spongepowered.common.bridge.world.damagesource.DamageSourceBridge) MinecraftFallingBlockDamageSource(org.spongepowered.common.util.MinecraftFallingBlockDamageSource) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 43 with Redirect

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

the class ServerGamePacketListenerImplMixin_Inventory method impl$onHandleUseItem.

@Redirect(method = "handleUseItem", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/level/ServerPlayerGameMode;useItem(Lnet/minecraft/server/level/ServerPlayer;Lnet/minecraft/world/level/Level;Lnet/minecraft/world/item/ItemStack;Lnet/minecraft/world/InteractionHand;)Lnet/minecraft/world/InteractionResult;"))
private InteractionResult impl$onHandleUseItem(final ServerPlayerGameMode serverPlayerGameMode, final ServerPlayer param0, final Level param1, final ItemStack param2, final InteractionHand param3) {
    final PhaseContext<@NonNull ?> context = PhaseTracker.SERVER.getPhaseContext();
    final TransactionalCaptureSupplier transactor = context.getTransactor();
    final InteractionResult result = serverPlayerGameMode.useItem(param0, param1, param2, param3);
    try (EffectTransactor ignored = transactor.logPlayerInventoryChangeWithEffect(this.player, PlayerInventoryTransaction.EventCreator.STANDARD)) {
        // capture
        this.player.inventoryMenu.broadcastChanges();
    }
    return result;
// TrackingUtil.processBlockCaptures called by UseItemPacketState
}
Also used : InteractionResult(net.minecraft.world.InteractionResult) 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 44 with Redirect

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

the class ServerGamePacketListenerImplMixin_Inventory method impl$onBroadcastCreativeActionResult.

@Redirect(method = "handleSetCreativeModeSlot", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/inventory/InventoryMenu;broadcastChanges()V"))
private void impl$onBroadcastCreativeActionResult(final InventoryMenu inventoryMenu, final ServerboundSetCreativeModeSlotPacket packetIn) {
    final PhaseContext<@NonNull ?> context = PhaseTracker.SERVER.getPhaseContext();
    final TransactionalCaptureSupplier transactor = context.getTransactor();
    final ItemStack itemstack = packetIn.getItem();
    // TODO handle vanilla sending a bunch of creative events (previously ignoring events within 100ms)
    try (final EffectTransactor ignored = transactor.logCreativeClickContainer(packetIn.getSlotNum(), ItemStackUtil.snapshotOf(itemstack), this.player)) {
    }
    inventoryMenu.broadcastChanges();
}
Also used : TransactionalCaptureSupplier(org.spongepowered.common.event.tracking.context.transaction.TransactionalCaptureSupplier) EffectTransactor(org.spongepowered.common.event.tracking.context.transaction.EffectTransactor) ItemStack(net.minecraft.world.item.ItemStack) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 45 with Redirect

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

the class ServerGamePacketListenerImplMixin_Inventory method impl$onBroadcastCreativeActionResult.

@Redirect(method = "handleSetCreativeModeSlot", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/level/ServerPlayer;drop(Lnet/minecraft/world/item/ItemStack;Z)Lnet/minecraft/world/entity/item/ItemEntity;"))
private ItemEntity impl$onBroadcastCreativeActionResult(final ServerPlayer serverPlayer, final ItemStack stack, final boolean param1) {
    final PhaseContext<@NonNull ?> context = PhaseTracker.SERVER.getPhaseContext();
    final TransactionalCaptureSupplier transactor = context.getTransactor();
    try (final EffectTransactor ignored = transactor.logCreativeClickContainer(-1, ItemStackUtil.snapshotOf(stack), this.player)) {
        return serverPlayer.drop(stack, param1);
    }
}
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)

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