Search in sources :

Example 1 with Redirect

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

the class MixinNetHandlerPlayServer method onPlayerDropItem.

@Nullable
@Redirect(method = "processPlayerDigging", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/EntityPlayerMP;dropItem(Z)Lnet/minecraft/entity/item/EntityItem;"))
public EntityItem onPlayerDropItem(EntityPlayerMP player, boolean dropAll) {
    EntityItem item = null;
    ItemStack stack = this.player.inventory.getCurrentItem();
    if (!stack.isEmpty()) {
        int size = stack.getCount();
        item = this.player.dropItem(dropAll);
        // force client itemstack update if drop event was cancelled
        if (item == null && ((IMixinEntityPlayer) player).shouldRestoreInventory()) {
            Slot slot = this.player.openContainer.getSlotFromInventory(this.player.inventory, this.player.inventory.currentItem);
            int windowId = this.player.openContainer.windowId;
            stack.setCount(size);
            this.sendPacket(new SPacketSetSlot(windowId, slot.slotNumber, stack));
        }
    }
    return item;
}
Also used : SPacketSetSlot(net.minecraft.network.play.server.SPacketSetSlot) Slot(net.minecraft.inventory.Slot) SPacketSetSlot(net.minecraft.network.play.server.SPacketSetSlot) ItemStack(net.minecraft.item.ItemStack) EntityItem(net.minecraft.entity.item.EntityItem) Redirect(org.spongepowered.asm.mixin.injection.Redirect) Nullable(javax.annotation.Nullable)

Example 2 with Redirect

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

the class MixinNetHandlerPlayServer method onPlayerTick.

@Redirect(method = "update", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/EntityPlayerMP;onUpdateEntity()V"))
private void onPlayerTick(EntityPlayerMP player) {
    if (player.world.isRemote) {
        player.onUpdateEntity();
        return;
    }
    try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame();
        PlayerTickContext context = TickPhase.Tick.PLAYER.createPhaseContext().source(player).buildAndSwitch()) {
        Sponge.getCauseStackManager().pushCause(player);
        player.onUpdateEntity();
    }
}
Also used : PlayerTickContext(org.spongepowered.common.event.tracking.phase.tick.PlayerTickContext) CauseStackManager(org.spongepowered.api.event.CauseStackManager) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 3 with Redirect

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

the class MixinWorldServer method onUpdateTick.

// This ticks pending updates to blocks, Requires mixin for NextTickListEntry so we use the correct tracking
@SuppressWarnings("unchecked")
@Redirect(method = "tickUpdates", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/Block;updateTick(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/state/IBlockState;Ljava/util/Random;)V"))
private void onUpdateTick(Block block, net.minecraft.world.World worldIn, BlockPos pos, IBlockState state, Random rand) {
    final PhaseTracker phaseTracker = PhaseTracker.getInstance();
    final PhaseData phaseData = phaseTracker.getCurrentPhaseData();
    final IPhaseState phaseState = phaseData.state;
    if (phaseState.alreadyCapturingBlockTicks(phaseData.context) || phaseState.ignoresBlockUpdateTick(phaseData)) {
        block.updateTick(worldIn, pos, state, rand);
        return;
    }
    IMixinBlock spongeBlock = (IMixinBlock) block;
    spongeBlock.getTimingsHandler().startTiming();
    TrackingUtil.updateTickBlock(this, block, pos, state, rand);
    spongeBlock.getTimingsHandler().stopTiming();
}
Also used : IMixinBlock(org.spongepowered.common.interfaces.block.IMixinBlock) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) PhaseData(org.spongepowered.common.event.tracking.PhaseData) IPhaseState(org.spongepowered.common.event.tracking.IPhaseState) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 4 with Redirect

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

the class MixinWorldServer method onCreateScheduledBlockUpdate.

/*@Redirect(method = "updateBlockTick",
            at = @At(value = "INVOKE", target = "Lnet/minecraft/world/NextTickListEntry;setPriority(I)V"))
    private void onUpdateScheduledBlock(NextTickListEntry sbu, int priority) {
        this.onCreateScheduledBlockUpdate(sbu, priority);
    }*/
@// really scheduleUpdate
Redirect(// really scheduleUpdate
method = "updateBlockTick", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/NextTickListEntry;setPriority(I)V"))
private void onCreateScheduledBlockUpdate(NextTickListEntry sbu, int priority) {
    final PhaseTracker phaseTracker = PhaseTracker.getInstance();
    final IPhaseState phaseState = phaseTracker.getCurrentState();
    if (phaseState.ignoresScheduledUpdates()) {
        this.tmpScheduledObj = sbu;
        return;
    }
    sbu.setPriority(priority);
    ((IMixinNextTickListEntry) sbu).setWorld((WorldServer) (Object) this);
    this.tmpScheduledObj = sbu;
}
Also used : PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) IMixinNextTickListEntry(org.spongepowered.common.interfaces.IMixinNextTickListEntry) IPhaseState(org.spongepowered.common.event.tracking.IPhaseState) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 5 with Redirect

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

the class MixinWorldServer method onAddBlockEvent.

@Redirect(method = "addBlockEvent", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/WorldServer$ServerBlockEventList;add(Ljava/lang/Object;)Z", remap = false))
private boolean onAddBlockEvent(WorldServer.ServerBlockEventList list, Object obj, BlockPos pos, Block blockIn, int eventId, int eventParam) {
    final BlockEventData blockEventData = (BlockEventData) obj;
    IMixinBlockEventData blockEvent = (IMixinBlockEventData) blockEventData;
    // This is very common with pistons as they add block events while blocks are being notified.
    if (blockIn instanceof BlockPistonBase) {
        // We only fire pre events for pistons
        if (SpongeCommonEventFactory.handlePistonEvent(this, list, obj, pos, blockIn, eventId, eventParam)) {
            return false;
        }
        blockEvent.setCaptureBlocks(false);
    // TODO BLOCK_EVENT flag
    } else if (SpongeCommonEventFactory.callChangeBlockEventPre(this, pos).isCancelled()) {
        return false;
    }
    final PhaseTracker phaseTracker = PhaseTracker.getInstance();
    final PhaseData currentPhase = phaseTracker.getCurrentPhaseData();
    final IPhaseState phaseState = currentPhase.state;
    if (phaseState.ignoresBlockEvent()) {
        return list.add((BlockEventData) obj);
    }
    final PhaseContext<?> context = currentPhase.context;
    final LocatableBlock locatable = LocatableBlock.builder().location(new Location<>(this, pos.getX(), pos.getY(), pos.getZ())).state(this.getBlock(pos.getX(), pos.getY(), pos.getZ())).build();
    blockEvent.setTickBlock(locatable);
    phaseState.addNotifierToBlockEvent(context, this, pos, blockEvent);
    return list.add((BlockEventData) obj);
}
Also used : IMixinBlockEventData(org.spongepowered.common.interfaces.block.IMixinBlockEventData) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) PhaseData(org.spongepowered.common.event.tracking.PhaseData) BlockPistonBase(net.minecraft.block.BlockPistonBase) IPhaseState(org.spongepowered.common.event.tracking.IPhaseState) LocatableBlock(org.spongepowered.api.world.LocatableBlock) IMixinBlockEventData(org.spongepowered.common.interfaces.block.IMixinBlockEventData) BlockEventData(net.minecraft.block.BlockEventData) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Aggregations

Redirect (org.spongepowered.asm.mixin.injection.Redirect)72 ItemStack (net.minecraft.item.ItemStack)13 CauseStackManager (org.spongepowered.api.event.CauseStackManager)10 PhaseTracker (org.spongepowered.common.event.tracking.PhaseTracker)10 Vector3d (com.flowpowered.math.vector.Vector3d)7 World (net.minecraft.world.World)7 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)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 StackFrame (org.spongepowered.api.event.CauseStackManager.StackFrame)6 IMixinEntityPlayer (org.spongepowered.common.interfaces.entity.player.IMixinEntityPlayer)6 Nullable (javax.annotation.Nullable)5 EntityItem (net.minecraft.entity.item.EntityItem)5 Location (org.spongepowered.api.world.Location)5 ResourceLocation (net.minecraft.util.ResourceLocation)4 World (org.spongepowered.api.world.World)4 PhaseData (org.spongepowered.common.event.tracking.PhaseData)4 IMixinInventoryPlayer (org.spongepowered.common.interfaces.entity.player.IMixinInventoryPlayer)4