Search in sources :

Example 16 with IMixinEventBus

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

the class SpongeForgeEventFactory method callWorldSaveEvent.

private static SaveWorldEvent callWorldSaveEvent(Event event) {
    SaveWorldEvent spongeEvent = (SaveWorldEvent) 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.
    ((IMixinWorld) spongeEvent.getTargetWorld()).setCallingWorldEvent(true);
    ((IMixinEventBus) MinecraftForge.EVENT_BUS).post(new WorldEvent.Save((net.minecraft.world.World) spongeEvent.getTargetWorld()), true);
    ((IMixinWorld) spongeEvent.getTargetWorld()).setCallingWorldEvent(false);
    return spongeEvent;
}
Also used : IMixinEventBus(org.spongepowered.mod.interfaces.IMixinEventBus) 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) SaveWorldEvent(org.spongepowered.api.event.world.SaveWorldEvent)

Example 17 with IMixinEventBus

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

the class SpongeForgeEventFactory method createPlayerInteractEvent.

// Bulk Event Handling
private static InteractBlockEvent createPlayerInteractEvent(Event event) {
    InteractBlockEvent spongeEvent = (InteractBlockEvent) event;
    Player player = spongeEvent.getCause().first(Player.class).orElse(null);
    // Forge doesn't support left-click AIR
    if (player == null || (spongeEvent instanceof InteractBlockEvent.Primary && spongeEvent.getTargetBlock() == BlockSnapshot.NONE)) {
        return spongeEvent;
    }
    BlockPos pos = VecHelper.toBlockPos(spongeEvent.getTargetBlock().getPosition());
    EnumFacing face = DirectionFacingProvider.getInstance().get(spongeEvent.getTargetSide()).orElse(null);
    Vec3d hitVec = null;
    final EntityPlayerMP entityPlayerMP = EntityUtil.toNative(player);
    if (spongeEvent.getInteractionPoint().isPresent()) {
        hitVec = VecHelper.toVec3d(spongeEvent.getInteractionPoint().get());
    }
    if (spongeEvent instanceof InteractBlockEvent.Primary) {
        PlayerInteractEvent.LeftClickBlock forgeEvent = new PlayerInteractEvent.LeftClickBlock(entityPlayerMP, pos, face, hitVec);
        ((IMixinEventBus) MinecraftForge.EVENT_BUS).post(forgeEvent, true);
        if (forgeEvent.isCanceled()) {
            spongeEvent.setCancelled(true);
        }
    } else if (face != null && spongeEvent instanceof InteractBlockEvent.Secondary) {
        EnumHand hand = spongeEvent instanceof InteractBlockEvent.Secondary.MainHand ? EnumHand.MAIN_HAND : EnumHand.OFF_HAND;
        PlayerInteractEvent.RightClickBlock forgeEvent = new PlayerInteractEvent.RightClickBlock(entityPlayerMP, hand, pos, face, hitVec);
        ((IMixinEventBus) MinecraftForge.EVENT_BUS).post(forgeEvent, true);
        if (forgeEvent.isCanceled()) {
            spongeEvent.setCancelled(true);
        }
        // Mods have higher priority
        if (forgeEvent.getUseItem() != Result.DEFAULT) {
            ((InteractBlockEvent.Secondary) spongeEvent).setUseItemResult(getTristateFromResult(forgeEvent.getUseItem()));
        }
        if (forgeEvent.getUseBlock() != Result.DEFAULT) {
            ((InteractBlockEvent.Secondary) spongeEvent).setUseBlockResult(getTristateFromResult(forgeEvent.getUseBlock()));
        }
    }
    return spongeEvent;
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) EntityPlayer(net.minecraft.entity.player.EntityPlayer) EnumFacing(net.minecraft.util.EnumFacing) PlayerInteractEvent(net.minecraftforge.event.entity.player.PlayerInteractEvent) Vec3d(net.minecraft.util.math.Vec3d) IMixinEventBus(org.spongepowered.mod.interfaces.IMixinEventBus) InteractBlockEvent(org.spongepowered.api.event.block.InteractBlockEvent) EnumHand(net.minecraft.util.EnumHand) BlockPos(net.minecraft.util.math.BlockPos) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP)

Example 18 with IMixinEventBus

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

the class SpongeForgeEventFactory method callChunkLoadEvent.

private static LoadChunkEvent callChunkLoadEvent(Event event) {
    LoadChunkEvent spongeEvent = (LoadChunkEvent) event;
    final net.minecraft.world.chunk.Chunk chunk = (net.minecraft.world.chunk.Chunk) spongeEvent.getTargetChunk();
    ((IMixinEventBus) MinecraftForge.EVENT_BUS).post(new ChunkEvent.Load(chunk), true);
    return spongeEvent;
}
Also used : LoadChunkEvent(org.spongepowered.api.event.world.chunk.LoadChunkEvent) IMixinEventBus(org.spongepowered.mod.interfaces.IMixinEventBus) ChunkEvent(net.minecraftforge.event.world.ChunkEvent) TargetChunkEvent(org.spongepowered.api.event.world.chunk.TargetChunkEvent) LoadChunkEvent(org.spongepowered.api.event.world.chunk.LoadChunkEvent) UnloadChunkEvent(org.spongepowered.api.event.world.chunk.UnloadChunkEvent) PopulateChunkEvent(net.minecraftforge.event.terraingen.PopulateChunkEvent) Chunk(org.spongepowered.api.world.Chunk)

Example 19 with IMixinEventBus

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

the class SpongeForgeEventFactory method callLivingDeathEvent.

// unused
public static DestructEntityEvent.Death callLivingDeathEvent(Event event) {
    DestructEntityEvent.Death spongeEvent = (DestructEntityEvent.Death) event;
    if (!spongeEvent.getCause().first(DamageSource.class).isPresent()) {
        return spongeEvent;
    }
    EntityLivingBase entity = (net.minecraft.entity.EntityLivingBase) spongeEvent.getTargetEntity();
    net.minecraft.util.DamageSource damageSource = (net.minecraft.util.DamageSource) spongeEvent.getCause().first(DamageSource.class).get();
    LivingDeathEvent forgeEvent = new LivingDeathEvent(entity, damageSource);
    ((IMixinEventBus) MinecraftForge.EVENT_BUS).post(forgeEvent, true);
    return spongeEvent;
}
Also used : IMixinEventBus(org.spongepowered.mod.interfaces.IMixinEventBus) DestructEntityEvent(org.spongepowered.api.event.entity.DestructEntityEvent) DamageSource(org.spongepowered.api.event.cause.entity.damage.source.DamageSource) IMixinEntityLivingBase(org.spongepowered.common.interfaces.entity.IMixinEntityLivingBase) EntityLivingBase(net.minecraft.entity.EntityLivingBase) LivingDeathEvent(net.minecraftforge.event.entity.living.LivingDeathEvent)

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