Search in sources :

Example 1 with IMixinEntityLivingBase

use of org.spongepowered.common.interfaces.entity.IMixinEntityLivingBase in project SpongeCommon by SpongePowered.

the class DamageableDataProcessor method set.

@SuppressWarnings("unchecked")
@Override
protected boolean set(EntityLivingBase dataHolder, Map<Key<?>, Object> keyValues) {
    final Optional<EntitySnapshot> lastAttacker = (Optional<EntitySnapshot>) keyValues.get(Keys.LAST_ATTACKER);
    if (lastAttacker == null || !lastAttacker.isPresent()) {
        dataHolder.setRevengeTarget(null);
        return true;
    }
    if (lastAttacker.get().getUniqueId().isPresent()) {
        final Optional<Entity> optionalEntity = lastAttacker.get().restore();
        if (optionalEntity.isPresent()) {
            final Entity entity = optionalEntity.get();
            if (entity.isLoaded() && entity instanceof EntityLivingBase) {
                dataHolder.setRevengeTarget((EntityLivingBase) entity);
                ((IMixinEntityLivingBase) dataHolder).setLastDamage(((Optional<Double>) keyValues.get(Keys.LAST_DAMAGE)).orElse(0D));
                return true;
            }
        }
    }
    return false;
}
Also used : Entity(org.spongepowered.api.entity.Entity) EntitySnapshot(org.spongepowered.api.entity.EntitySnapshot) Optional(java.util.Optional) EntityLivingBase(net.minecraft.entity.EntityLivingBase) IMixinEntityLivingBase(org.spongepowered.common.interfaces.entity.IMixinEntityLivingBase) IMixinEntityLivingBase(org.spongepowered.common.interfaces.entity.IMixinEntityLivingBase)

Example 2 with IMixinEntityLivingBase

use of org.spongepowered.common.interfaces.entity.IMixinEntityLivingBase 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 3 with IMixinEntityLivingBase

use of org.spongepowered.common.interfaces.entity.IMixinEntityLivingBase in project SpongeForge by SpongePowered.

the class SpongeForgeEventFactory method callLivingDropsEvent.

@SuppressWarnings("unchecked")
public static DropItemEvent.Destruct callLivingDropsEvent(Event event) {
    DropItemEvent.Destruct spongeEvent = (DropItemEvent.Destruct) event;
    final Cause cause = spongeEvent.getCause();
    Object source = cause.root();
    Optional<DamageSource> damageSource = cause.first(DamageSource.class);
    final Optional<Entity> spawnEntity = cause.first(Entity.class);
    final Optional<SpawnType> spawnType = cause.getContext().get(EventContextKeys.SPAWN_TYPE);
    if (!spawnType.isPresent() || !damageSource.isPresent() || !spawnEntity.isPresent()) {
        // Mods expect EntityJoinWorldEvent to trigger
        callEntityJoinWorldEvent(spongeEvent);
        return spongeEvent;
    }
    Entity entity = spawnEntity.get();
    if (entity == null || !(entity instanceof EntityLivingBase)) {
        return spongeEvent;
    }
    LivingDropsEvent forgeEvent = null;
    if (entity instanceof EntityPlayer) {
        EntityPlayer player = (EntityPlayer) entity;
        forgeEvent = new PlayerDropsEvent(player, (net.minecraft.util.DamageSource) damageSource.get(), (List<EntityItem>) (List<?>) spongeEvent.getEntities(), ((IMixinEntityLivingBase) entity).getRecentlyHit() > 0);
    } else {
        forgeEvent = new LivingDropsEvent((EntityLivingBase) entity, (net.minecraft.util.DamageSource) damageSource.get(), (List<EntityItem>) (List<?>) spongeEvent.getEntities(), 0, ((IMixinEntityLivingBase) entity).getRecentlyHit() > 0);
    }
    ((IMixinEventBus) MinecraftForge.EVENT_BUS).post(forgeEvent, true);
    if (forgeEvent.isCanceled()) {
        spongeEvent.setCancelled(true);
    } else {
        callEntityJoinWorldEvent(spongeEvent);
    }
    return spongeEvent;
}
Also used : DropItemEvent(org.spongepowered.api.event.item.inventory.DropItemEvent) TileEntity(org.spongepowered.api.block.tileentity.TileEntity) Entity(net.minecraft.entity.Entity) SpawnType(org.spongepowered.api.event.cause.entity.spawn.SpawnType) DamageSource(org.spongepowered.api.event.cause.entity.damage.source.DamageSource) LivingDropsEvent(net.minecraftforge.event.entity.living.LivingDropsEvent) IMixinEventBus(org.spongepowered.mod.interfaces.IMixinEventBus) PlayerDropsEvent(net.minecraftforge.event.entity.player.PlayerDropsEvent) IMixinInitCause(org.spongepowered.common.interfaces.IMixinInitCause) Cause(org.spongepowered.api.event.cause.Cause) IMixinEntityLivingBase(org.spongepowered.common.interfaces.entity.IMixinEntityLivingBase) EntityLivingBase(net.minecraft.entity.EntityLivingBase) EntityPlayer(net.minecraft.entity.player.EntityPlayer) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Aggregations

IMixinEntityLivingBase (org.spongepowered.common.interfaces.entity.IMixinEntityLivingBase)3 EntityLivingBase (net.minecraft.entity.EntityLivingBase)2 ImmutableList (com.google.common.collect.ImmutableList)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Optional (java.util.Optional)1 Entity (net.minecraft.entity.Entity)1 EntityPlayer (net.minecraft.entity.player.EntityPlayer)1 LivingDropsEvent (net.minecraftforge.event.entity.living.LivingDropsEvent)1 PlayerDropsEvent (net.minecraftforge.event.entity.player.PlayerDropsEvent)1 TileEntity (org.spongepowered.api.block.tileentity.TileEntity)1 Entity (org.spongepowered.api.entity.Entity)1 EntitySnapshot (org.spongepowered.api.entity.EntitySnapshot)1 CauseStackManager (org.spongepowered.api.event.CauseStackManager)1 StackFrame (org.spongepowered.api.event.CauseStackManager.StackFrame)1 Cause (org.spongepowered.api.event.cause.Cause)1 DamageSource (org.spongepowered.api.event.cause.entity.damage.source.DamageSource)1 SpawnType (org.spongepowered.api.event.cause.entity.spawn.SpawnType)1 DropItemEvent (org.spongepowered.api.event.item.inventory.DropItemEvent)1 Redirect (org.spongepowered.asm.mixin.injection.Redirect)1