Search in sources :

Example 1 with SpawnType

use of org.spongepowered.api.event.cause.entity.spawn.SpawnType 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)

Example 2 with SpawnType

use of org.spongepowered.api.event.cause.entity.spawn.SpawnType in project SpongeForge by SpongePowered.

the class SpongeForgeEventFactory method callItemTossEvent.

public static DropItemEvent.Dispense callItemTossEvent(Event event) {
    DropItemEvent.Dispense spongeEvent = (DropItemEvent.Dispense) event;
    if (spongeEvent.getEntities().size() <= 0) {
        return spongeEvent;
    }
    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();
    EntityItem item = (EntityItem) spongeEvent.getEntities().get(0);
    if (entity == null || item == null || item.getItem() == null || !(entity instanceof Player)) {
        return spongeEvent;
    }
    ItemTossEvent forgeEvent = new ItemTossEvent(item, (EntityPlayerMP) entity);
    ((IMixinEventBus) MinecraftForge.EVENT_BUS).post(forgeEvent, true);
    if (forgeEvent.isCanceled()) {
        if (item.isDead) {
            // Mods such as Flux-Networks kills the entity item to spawn a custom one
            return spongeEvent;
        }
        spongeEvent.setCancelled(true);
    } else {
        // Forge treats EntityJoinWorldEvent separately from Toss so we need to call it here
        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) Player(org.spongepowered.api.entity.living.player.Player) EntityPlayer(net.minecraft.entity.player.EntityPlayer) SpawnType(org.spongepowered.api.event.cause.entity.spawn.SpawnType) DamageSource(org.spongepowered.api.event.cause.entity.damage.source.DamageSource) IMixinEventBus(org.spongepowered.mod.interfaces.IMixinEventBus) ItemTossEvent(net.minecraftforge.event.entity.item.ItemTossEvent) IMixinInitCause(org.spongepowered.common.interfaces.IMixinInitCause) Cause(org.spongepowered.api.event.cause.Cause) EntityItem(net.minecraft.entity.item.EntityItem)

Aggregations

Entity (net.minecraft.entity.Entity)2 EntityPlayer (net.minecraft.entity.player.EntityPlayer)2 TileEntity (org.spongepowered.api.block.tileentity.TileEntity)2 Cause (org.spongepowered.api.event.cause.Cause)2 DamageSource (org.spongepowered.api.event.cause.entity.damage.source.DamageSource)2 SpawnType (org.spongepowered.api.event.cause.entity.spawn.SpawnType)2 DropItemEvent (org.spongepowered.api.event.item.inventory.DropItemEvent)2 IMixinInitCause (org.spongepowered.common.interfaces.IMixinInitCause)2 IMixinEventBus (org.spongepowered.mod.interfaces.IMixinEventBus)2 ImmutableList (com.google.common.collect.ImmutableList)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 EntityLivingBase (net.minecraft.entity.EntityLivingBase)1 EntityItem (net.minecraft.entity.item.EntityItem)1 ItemTossEvent (net.minecraftforge.event.entity.item.ItemTossEvent)1 LivingDropsEvent (net.minecraftforge.event.entity.living.LivingDropsEvent)1 PlayerDropsEvent (net.minecraftforge.event.entity.player.PlayerDropsEvent)1 Player (org.spongepowered.api.entity.living.player.Player)1 IMixinEntityLivingBase (org.spongepowered.common.interfaces.entity.IMixinEntityLivingBase)1