Search in sources :

Example 1 with EntityJoinWorldEvent

use of net.minecraftforge.event.entity.EntityJoinWorldEvent in project SpongeForge by SpongePowered.

the class SpongeForgeEventFactory method handleCustomStack.

// Copied from ForgeInternalHandler.onEntityJoinWorld, but with modifications
private static void handleCustomStack(SpawnEntityEvent event) {
    // Sponge start - iterate over entities
    ListIterator<org.spongepowered.api.entity.Entity> it = event.getEntities().listIterator();
    while (it.hasNext()) {
        // Sponge - use entity from event
        Entity entity = (Entity) it.next();
        if (entity.getClass().equals(EntityItem.class)) {
            ItemStack stack = ((EntityItem) entity).getItem();
            if (stack.isEmpty()) {
                // Sponge - continue instead of return
                continue;
            }
            Item item = stack.getItem();
            if (item == null) {
                // Sponge - technically, this check is in the wrong place, as it normally runs in Forge's listener (i.e. after 'beforeModifications' listener)
                // However, it's really only a sanity check for something that should never happen, so it's fine
                FMLLog.warning("Attempted to add a EntityItem to the world with a invalid item at " + "(%2.2f,  %2.2f, %2.2f), this is most likely a config issue between you and the server. Please double check your configs", entity.posX, entity.posY, entity.posZ);
                entity.setDead();
                // Sponge - use our setCancelled method
                event.setCancelled(true);
                // Sponge - continue instead of return
                continue;
            }
            if (item.hasCustomEntity(stack)) {
                // Sponge - use world from entity
                Entity newEntity = item.createEntity(entity.getEntityWorld(), entity, stack);
                if (newEntity != null) {
                    entity.setDead();
                    // event.setCanceled(true); Sponge - don't cancel the event
                    // Sponge start - fire cancelled event, and return new event.
                    // We don't need to set 'StaticMixinForgeHelper.preventInternalForgeEntityListener' to 'true',
                    // since Forge's listener only handled uncancelled events
                    EntityJoinWorldEvent cancelledEvent = new EntityJoinWorldEvent(entity, entity.getEntityWorld());
                    cancelledEvent.setCanceled(true);
                    ((IMixinEventBus) MinecraftForge.EVENT_BUS).post(cancelledEvent, true);
                    if (!cancelledEvent.isCanceled()) {
                        SpongeImpl.getLogger().error("A mod has un-cancelled the EntityJoinWorld event for the original EntityItem (from before Item#createEntity is called). This is almost certainly a terrible idea!");
                    }
                    it.set((org.spongepowered.api.entity.Entity) newEntity);
                    continue;
                // Sponge end
                }
            }
        }
    }
}
Also used : TileEntity(org.spongepowered.api.block.tileentity.TileEntity) Entity(net.minecraft.entity.Entity) Item(net.minecraft.item.Item) EntityItem(net.minecraft.entity.item.EntityItem) EntityJoinWorldEvent(net.minecraftforge.event.entity.EntityJoinWorldEvent) IMixinEventBus(org.spongepowered.mod.interfaces.IMixinEventBus) ItemStack(net.minecraft.item.ItemStack) EntityItem(net.minecraft.entity.item.EntityItem)

Example 2 with EntityJoinWorldEvent

use of net.minecraftforge.event.entity.EntityJoinWorldEvent in project SpongeForge by SpongePowered.

the class SpongeForgeEventFactory method callEntityJoinWorldEvent.

public static SpawnEntityEvent callEntityJoinWorldEvent(Event event) {
    SpawnEntityEvent spongeEvent = (SpawnEntityEvent) event;
    ListIterator<org.spongepowered.api.entity.Entity> iterator = spongeEvent.getEntities().listIterator();
    if (spongeEvent.getEntities().size() == 0) {
        return spongeEvent;
    }
    // used to avoid player item restores when set to dead
    boolean canCancelEvent = true;
    while (iterator.hasNext()) {
        org.spongepowered.api.entity.Entity entity = iterator.next();
        EntityJoinWorldEvent forgeEvent = new EntityJoinWorldEvent((Entity) entity, (net.minecraft.world.World) entity.getLocation().getExtent());
        boolean prev = StaticMixinForgeHelper.preventInternalForgeEntityListener;
        StaticMixinForgeHelper.preventInternalForgeEntityListener = true;
        ((IMixinEventBus) MinecraftForge.EVENT_BUS).post(forgeEvent, true);
        StaticMixinForgeHelper.preventInternalForgeEntityListener = prev;
        Entity mcEntity = (Entity) entity;
        if (mcEntity.isDead) {
            // Don't restore packet item if a mod wants it dead
            // Mods such as Flux-Networks kills the entity item to spawn a custom one
            canCancelEvent = false;
        }
        if (forgeEvent.isCanceled()) {
            iterator.remove();
        }
    }
    if (spongeEvent.getEntities().size() == 0 && canCancelEvent) {
        spongeEvent.setCancelled(true);
    }
    return spongeEvent;
}
Also used : TileEntity(org.spongepowered.api.block.tileentity.TileEntity) Entity(net.minecraft.entity.Entity) SpawnEntityEvent(org.spongepowered.api.event.entity.SpawnEntityEvent) EntityJoinWorldEvent(net.minecraftforge.event.entity.EntityJoinWorldEvent) IMixinEventBus(org.spongepowered.mod.interfaces.IMixinEventBus)

Aggregations

Entity (net.minecraft.entity.Entity)2 EntityJoinWorldEvent (net.minecraftforge.event.entity.EntityJoinWorldEvent)2 TileEntity (org.spongepowered.api.block.tileentity.TileEntity)2 IMixinEventBus (org.spongepowered.mod.interfaces.IMixinEventBus)2 EntityItem (net.minecraft.entity.item.EntityItem)1 Item (net.minecraft.item.Item)1 ItemStack (net.minecraft.item.ItemStack)1 SpawnEntityEvent (org.spongepowered.api.event.entity.SpawnEntityEvent)1