Search in sources :

Example 6 with IMixinEventBus

use of org.spongepowered.mod.interfaces.IMixinEventBus in project SpongeForge by SpongePowered.

the class SpongeForgeEventFactory method callWorldLoadEvent.

private static LoadWorldEvent callWorldLoadEvent(Event event) {
    LoadWorldEvent spongeEvent = (LoadWorldEvent) event;
    // Since Forge only uses a single save handler, we need to make sure to pass the overworld's handler.
    // This makes sure that mods dont attempt to save/read their data from the wrong location.
    final net.minecraft.world.World minecraftWorld = (net.minecraft.world.World) spongeEvent.getTargetWorld();
    ((IMixinWorld) spongeEvent.getTargetWorld()).setCallingWorldEvent(true);
    ((IMixinChunkProviderServer) minecraftWorld.getChunkProvider()).setForceChunkRequests(true);
    ((IMixinEventBus) MinecraftForge.EVENT_BUS).post(new WorldEvent.Load(minecraftWorld), true);
    ((IMixinChunkProviderServer) minecraftWorld.getChunkProvider()).setForceChunkRequests(false);
    ((IMixinWorld) minecraftWorld).setCallingWorldEvent(false);
    return spongeEvent;
}
Also used : IMixinEventBus(org.spongepowered.mod.interfaces.IMixinEventBus) LoadWorldEvent(org.spongepowered.api.event.world.LoadWorldEvent) IMixinWorld(org.spongepowered.common.interfaces.world.IMixinWorld) LoadWorldEvent(org.spongepowered.api.event.world.LoadWorldEvent) EntityJoinWorldEvent(net.minecraftforge.event.entity.EntityJoinWorldEvent) UnloadWorldEvent(org.spongepowered.api.event.world.UnloadWorldEvent) WorldEvent(net.minecraftforge.event.world.WorldEvent) SaveWorldEvent(org.spongepowered.api.event.world.SaveWorldEvent) TargetWorldEvent(org.spongepowered.api.event.world.TargetWorldEvent) World(org.spongepowered.api.world.World) IMixinWorld(org.spongepowered.common.interfaces.world.IMixinWorld) IMixinChunkProviderServer(org.spongepowered.common.interfaces.world.gen.IMixinChunkProviderServer)

Example 7 with IMixinEventBus

use of org.spongepowered.mod.interfaces.IMixinEventBus 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 8 with IMixinEventBus

use of org.spongepowered.mod.interfaces.IMixinEventBus 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)

Example 9 with IMixinEventBus

use of org.spongepowered.mod.interfaces.IMixinEventBus 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)

Example 10 with IMixinEventBus

use of org.spongepowered.mod.interfaces.IMixinEventBus in project SpongeForge by SpongePowered.

the class SpongeForgeEventFactory method callExplosionEventDetonate.

@SuppressWarnings("unchecked")
private static ExplosionEvent.Detonate callExplosionEventDetonate(Event event) {
    ExplosionEvent.Detonate spongeEvent = (ExplosionEvent.Detonate) event;
    Explosion explosion = (Explosion) spongeEvent.getExplosion();
    net.minecraftforge.event.world.ExplosionEvent.Detonate forgeEvent = new net.minecraftforge.event.world.ExplosionEvent.Detonate((net.minecraft.world.World) spongeEvent.getTargetWorld(), explosion, (List<Entity>) (List<?>) spongeEvent.getEntities());
    explosion.getAffectedBlockPositions().clear();
    for (Location<World> x : spongeEvent.getAffectedLocations()) {
        explosion.getAffectedBlockPositions().add(VecHelper.toBlockPos(x.getPosition()));
    }
    ((IMixinEventBus) MinecraftForge.EVENT_BUS).post(forgeEvent, true);
    if (forgeEvent.isCanceled()) {
        spongeEvent.setCancelled(true);
    }
    ((ExplosionEvent.Detonate) event).getAffectedLocations().clear();
    for (BlockPos pos : forgeEvent.getAffectedBlocks()) {
        ((ExplosionEvent.Detonate) event).getAffectedLocations().add(new Location<>(spongeEvent.getTargetWorld(), VecHelper.toVector3i(pos)));
    }
    return spongeEvent;
}
Also used : TileEntity(org.spongepowered.api.block.tileentity.TileEntity) Entity(net.minecraft.entity.Entity) Explosion(net.minecraft.world.Explosion) ExplosionEvent(org.spongepowered.api.event.world.ExplosionEvent) World(org.spongepowered.api.world.World) IMixinWorld(org.spongepowered.common.interfaces.world.IMixinWorld) IMixinEventBus(org.spongepowered.mod.interfaces.IMixinEventBus) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) BlockPos(net.minecraft.util.math.BlockPos)

Aggregations

IMixinEventBus (org.spongepowered.mod.interfaces.IMixinEventBus)19 TileEntity (org.spongepowered.api.block.tileentity.TileEntity)7 Entity (net.minecraft.entity.Entity)6 EntityPlayer (net.minecraft.entity.player.EntityPlayer)6 World (org.spongepowered.api.world.World)6 IMixinWorld (org.spongepowered.common.interfaces.world.IMixinWorld)6 EntityJoinWorldEvent (net.minecraftforge.event.entity.EntityJoinWorldEvent)5 Player (org.spongepowered.api.entity.living.player.Player)5 BlockPos (net.minecraft.util.math.BlockPos)4 ArrayList (java.util.ArrayList)3 EntityItem (net.minecraft.entity.item.EntityItem)3 EnumFacing (net.minecraft.util.EnumFacing)3 EnumHand (net.minecraft.util.EnumHand)3 WorldEvent (net.minecraftforge.event.world.WorldEvent)3 DamageSource (org.spongepowered.api.event.cause.entity.damage.source.DamageSource)3 ImmutableList (com.google.common.collect.ImmutableList)2 List (java.util.List)2 IBlockState (net.minecraft.block.state.IBlockState)2 EntityLivingBase (net.minecraft.entity.EntityLivingBase)2 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)2