Search in sources :

Example 11 with Redirect

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

the class MixinCommandSummon method onAttemptSpawnEntity.

@Redirect(method = "execute", at = @At(value = "INVOKE", target = ENTITY_LIST_CREATE_FROM_NBT))
private Entity onAttemptSpawnEntity(NBTTagCompound nbt, World world, double x, double y, double z, boolean b, MinecraftServer server, ICommandSender sender, String[] args) {
    if ("Minecart".equals(nbt.getString(NbtDataUtil.ENTITY_TYPE_ID))) {
        nbt.setString(NbtDataUtil.ENTITY_TYPE_ID, EntityMinecart.Type.values()[nbt.getInteger(NbtDataUtil.MINECART_TYPE)].getName());
        nbt.removeTag(NbtDataUtil.MINECART_TYPE);
    }
    Class<? extends Entity> entityClass = SpongeImplHooks.getEntityClass(new ResourceLocation(nbt.getString(NbtDataUtil.ENTITY_TYPE_ID)));
    if (entityClass == null) {
        return null;
    }
    EntityType type = EntityTypeRegistryModule.getInstance().getForClass(entityClass);
    if (type == null) {
        return null;
    }
    Transform<org.spongepowered.api.world.World> transform = new Transform<>(((org.spongepowered.api.world.World) world), new Vector3d(x, y, z));
    try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
        Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.PLACEMENT);
        ConstructEntityEvent.Pre event = SpongeEventFactory.createConstructEntityEventPre(Sponge.getCauseStackManager().getCurrentCause(), type, transform);
        SpongeImpl.postEvent(event);
        return event.isCancelled() ? null : AnvilChunkLoader.readWorldEntityPos(nbt, world, x, y, z, b);
    }
}
Also used : World(net.minecraft.world.World) EntityType(org.spongepowered.api.entity.EntityType) ConstructEntityEvent(org.spongepowered.api.event.entity.ConstructEntityEvent) Vector3d(com.flowpowered.math.vector.Vector3d) CauseStackManager(org.spongepowered.api.event.CauseStackManager) ResourceLocation(net.minecraft.util.ResourceLocation) Transform(org.spongepowered.api.entity.Transform) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 12 with Redirect

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

the class MixinEntity method onEntityCollideWithBlock.

@Redirect(method = "move", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/Block;" + "onEntityWalk(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/entity/Entity;)V"))
public void onEntityCollideWithBlock(Block block, net.minecraft.world.World world, BlockPos pos, net.minecraft.entity.Entity entity) {
    // if block can't collide, return
    if (!((IMixinBlock) block).hasCollideLogic()) {
        return;
    }
    if (world.isRemote) {
        block.onEntityWalk(world, pos, entity);
        return;
    }
    IBlockState state = world.getBlockState(pos);
    this.setCurrentCollidingBlock((BlockState) state);
    if (!SpongeCommonEventFactory.handleCollideBlockEvent(block, world, pos, state, entity, Direction.NONE)) {
        block.onEntityWalk(world, pos, entity);
        this.lastCollidedBlockPos = pos;
    }
    this.setCurrentCollidingBlock(null);
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 13 with Redirect

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

the class MixinEntityLivingBase method causeTrackDeathUpdate.

@Redirect(method = "onEntityUpdate", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/EntityLivingBase;onDeathUpdate()V"))
private void causeTrackDeathUpdate(EntityLivingBase entityLivingBase) {
    if (!entityLivingBase.world.isRemote) {
        final PhaseTracker phaseTracker = PhaseTracker.getInstance();
        try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame();
            PhaseContext<?> context = EntityPhase.State.DEATH_UPDATE.createPhaseContext().source(entityLivingBase).buildAndSwitch()) {
            Sponge.getCauseStackManager().pushCause(entityLivingBase);
            ((IMixinEntityLivingBase) entityLivingBase).onSpongeDeathUpdate();
        }
    } else {
        ((IMixinEntityLivingBase) entityLivingBase).onSpongeDeathUpdate();
    }
}
Also used : PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame) CauseStackManager(org.spongepowered.api.event.CauseStackManager) IMixinEntityLivingBase(org.spongepowered.common.interfaces.entity.IMixinEntityLivingBase) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 14 with Redirect

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

the class MixinWorldServer method onFireBlockEvent.

// special handling for Pistons since they use their own event system
@Redirect(method = "sendQueuedBlockEvents", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/WorldServer;fireBlockEvent(Lnet/minecraft/block/BlockEventData;)Z"))
private boolean onFireBlockEvent(net.minecraft.world.WorldServer worldIn, BlockEventData event) {
    final PhaseTracker phaseTracker = PhaseTracker.getInstance();
    final IPhaseState phaseState = phaseTracker.getCurrentState();
    if (phaseState.ignoresBlockEvent()) {
        return fireBlockEvent(event);
    }
    return TrackingUtil.fireMinecraftBlockEvent(worldIn, event);
}
Also used : PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) IPhaseState(org.spongepowered.common.event.tracking.IPhaseState) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 15 with Redirect

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

the class MixinEntityFireworkRocket method useEntitySource.

@Redirect(method = "dealExplosionDamage", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/EntityLivingBase;attackEntityFrom" + "(Lnet/minecraft/util/DamageSource;F)Z"))
public boolean useEntitySource(EntityLivingBase entityLivingBase, DamageSource source, float amount) {
    final DamageSource originalFireworks = DamageSource.FIREWORKS;
    DamageSource.FIREWORKS = new EntityDamageSource(originalFireworks.damageType, (Entity) (Object) this).setExplosion();
    final boolean result = entityLivingBase.attackEntityFrom(DamageSource.FIREWORKS, amount);
    DamageSource.FIREWORKS = originalFireworks;
    return result;
}
Also used : DamageSource(net.minecraft.util.DamageSource) EntityDamageSource(net.minecraft.util.EntityDamageSource) EntityDamageSource(net.minecraft.util.EntityDamageSource) 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