Search in sources :

Example 1 with IMixinEventBus

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

the class SpongeForgeEventFactory method callNeighborNotifyEvent.

public static NotifyNeighborBlockEvent callNeighborNotifyEvent(Event event) {
    NotifyNeighborBlockEvent spongeEvent = (NotifyNeighborBlockEvent) event;
    LocatableBlock locatableBlock = spongeEvent.getCause().first(LocatableBlock.class).orElse(null);
    TileEntity tileEntitySource = spongeEvent.getCause().first(TileEntity.class).orElse(null);
    Location<World> sourceLocation = null;
    IBlockState state = null;
    if (locatableBlock != null) {
        Location<World> location = locatableBlock.getLocation();
        sourceLocation = location;
        state = (IBlockState) locatableBlock.getBlockState();
    } else if (tileEntitySource != null) {
        sourceLocation = tileEntitySource.getLocation();
        state = (IBlockState) sourceLocation.getBlock();
    } else {
        // should never happen but just in case it does
        return spongeEvent;
    }
    EnumSet<EnumFacing> facings = EnumSet.noneOf(EnumFacing.class);
    for (Map.Entry<Direction, BlockState> mapEntry : spongeEvent.getNeighbors().entrySet()) {
        if (mapEntry.getKey() != Direction.NONE) {
            facings.add(DirectionFacingProvider.getInstance().get(mapEntry.getKey()).get());
        }
    }
    if (facings.isEmpty()) {
        return spongeEvent;
    }
    BlockPos pos = ((IMixinLocation) (Object) sourceLocation).getBlockPos();
    net.minecraft.world.World world = (net.minecraft.world.World) sourceLocation.getExtent();
    // TODO - the boolean forced redstone bit needs to be set properly
    final NeighborNotifyEvent forgeEvent = new NeighborNotifyEvent(world, pos, state, facings, false);
    ((IMixinEventBus) MinecraftForge.EVENT_BUS).post(forgeEvent, true);
    if (forgeEvent.isCanceled()) {
        spongeEvent.setCancelled(true);
    }
    return spongeEvent;
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) IMixinLocation(org.spongepowered.common.interfaces.world.IMixinLocation) EnumFacing(net.minecraft.util.EnumFacing) World(org.spongepowered.api.world.World) IMixinWorld(org.spongepowered.common.interfaces.world.IMixinWorld) Direction(org.spongepowered.api.util.Direction) TileEntity(org.spongepowered.api.block.tileentity.TileEntity) IMixinEventBus(org.spongepowered.mod.interfaces.IMixinEventBus) NotifyNeighborBlockEvent(org.spongepowered.api.event.block.NotifyNeighborBlockEvent) BlockState(org.spongepowered.api.block.BlockState) IBlockState(net.minecraft.block.state.IBlockState) NeighborNotifyEvent(net.minecraftforge.event.world.BlockEvent.NeighborNotifyEvent) LocatableBlock(org.spongepowered.api.world.LocatableBlock) BlockPos(net.minecraft.util.math.BlockPos) Map(java.util.Map)

Example 2 with IMixinEventBus

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

the class SpongeForgeEventFactory method callBlockPlaceEvent.

public static ChangeBlockEvent.Place callBlockPlaceEvent(Event event) {
    ChangeBlockEvent.Place spongeEvent = (ChangeBlockEvent.Place) event;
    if (spongeEvent.getCause().root() instanceof Player) {
        EntityPlayer player = (EntityPlayer) spongeEvent.getCause().first(Player.class).get();
        net.minecraft.world.World world = player.world;
        final PhaseTracker phaseTracker = PhaseTracker.getInstance();
        final PhaseContext<?> currentContext = phaseTracker.getCurrentContext();
        PhaseContext<?> target = currentContext;
        if (currentContext instanceof UnwindingPhaseContext) {
            target = ((UnwindingPhaseContext) currentContext).getUnwindingContext();
        }
        PacketContext<?> context = target instanceof PacketContext<?> ? (PacketContext<?>) target : null;
        Packet<?> contextPacket = context != null ? context.getPacket() : null;
        if (contextPacket == null) {
            return spongeEvent;
        }
        if (spongeEvent.getTransactions().size() == 1) {
            BlockPos pos = VecHelper.toBlockPos(spongeEvent.getTransactions().get(0).getOriginal().getPosition());
            IBlockState state = (IBlockState) spongeEvent.getTransactions().get(0).getOriginal().getState();
            net.minecraftforge.common.util.BlockSnapshot blockSnapshot = new net.minecraftforge.common.util.BlockSnapshot(world, pos, state);
            IBlockState placedAgainst = Blocks.AIR.getDefaultState();
            EnumHand hand = EnumHand.MAIN_HAND;
            if (contextPacket instanceof CPacketPlayerTryUseItemOnBlock) {
                CPacketPlayerTryUseItemOnBlock packet = (CPacketPlayerTryUseItemOnBlock) contextPacket;
                EnumFacing facing = packet.getDirection();
                placedAgainst = blockSnapshot.getWorld().getBlockState(blockSnapshot.getPos().offset(facing.getOpposite()));
                hand = packet.getHand();
            }
            BlockEvent.PlaceEvent forgeEvent = new BlockEvent.PlaceEvent(blockSnapshot, placedAgainst, player, hand);
            ((IMixinEventBus) MinecraftForge.EVENT_BUS).post(forgeEvent, true);
            if (forgeEvent.isCanceled()) {
                spongeEvent.setCancelled(true);
            }
        } else {
            // multi
            Iterator<Transaction<BlockSnapshot>> iterator = spongeEvent.getTransactions().iterator();
            List<net.minecraftforge.common.util.BlockSnapshot> blockSnapshots = new ArrayList<>();
            while (iterator.hasNext()) {
                Transaction<BlockSnapshot> transaction = iterator.next();
                Location<World> location = transaction.getOriginal().getLocation().get();
                IBlockState state = (IBlockState) transaction.getOriginal().getState();
                BlockPos pos = new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ());
                net.minecraftforge.common.util.BlockSnapshot blockSnapshot = new net.minecraftforge.common.util.BlockSnapshot(world, pos, state);
                blockSnapshots.add(blockSnapshot);
            }
            IBlockState placedAgainst = Blocks.AIR.getDefaultState();
            EnumHand hand = EnumHand.MAIN_HAND;
            if (contextPacket instanceof CPacketPlayerTryUseItemOnBlock) {
                CPacketPlayerTryUseItemOnBlock packet = (CPacketPlayerTryUseItemOnBlock) contextPacket;
                EnumFacing facing = packet.getDirection();
                placedAgainst = blockSnapshots.get(0).getWorld().getBlockState(blockSnapshots.get(0).getPos().offset(facing.getOpposite()));
                hand = packet.getHand();
            }
            BlockEvent.MultiPlaceEvent forgeEvent = new BlockEvent.MultiPlaceEvent(blockSnapshots, placedAgainst, player, hand);
            ((IMixinEventBus) MinecraftForge.EVENT_BUS).post(forgeEvent, true);
            if (forgeEvent.isCanceled()) {
                spongeEvent.setCancelled(true);
            }
        }
    }
    return spongeEvent;
}
Also used : EnumFacing(net.minecraft.util.EnumFacing) ArrayList(java.util.ArrayList) UnwindingPhaseContext(org.spongepowered.common.event.tracking.phase.general.UnwindingPhaseContext) World(org.spongepowered.api.world.World) IMixinWorld(org.spongepowered.common.interfaces.world.IMixinWorld) ChangeBlockEvent(org.spongepowered.api.event.block.ChangeBlockEvent) EnumHand(net.minecraft.util.EnumHand) BlockPos(net.minecraft.util.math.BlockPos) Player(org.spongepowered.api.entity.living.player.Player) EntityPlayer(net.minecraft.entity.player.EntityPlayer) IBlockState(net.minecraft.block.state.IBlockState) IMixinBlockSnapshot(org.spongepowered.mod.interfaces.IMixinBlockSnapshot) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) CPacketPlayerTryUseItemOnBlock(net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock) IMixinEventBus(org.spongepowered.mod.interfaces.IMixinEventBus) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) Transaction(org.spongepowered.api.data.Transaction) EntityPlayer(net.minecraft.entity.player.EntityPlayer) ChangeBlockEvent(org.spongepowered.api.event.block.ChangeBlockEvent) BlockEvent(net.minecraftforge.event.world.BlockEvent) InteractBlockEvent(org.spongepowered.api.event.block.InteractBlockEvent) NotifyNeighborBlockEvent(org.spongepowered.api.event.block.NotifyNeighborBlockEvent)

Example 3 with IMixinEventBus

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

the class SpongeForgeEventFactory method callEntityInteractEvent.

private static InteractEntityEvent.Secondary callEntityInteractEvent(Event event) {
    InteractEntityEvent.Secondary spongeEvent = (InteractEntityEvent.Secondary) event;
    Optional<Player> player = spongeEvent.getCause().first(Player.class);
    if (!player.isPresent()) {
        return null;
    }
    final EntityPlayerMP entityPlayerMP = EntityUtil.toNative(player.get());
    final EnumHand hand = entityPlayerMP.getActiveHand();
    final EntityPlayer entityPlayer = (EntityPlayer) player.get();
    final Entity entity = (Entity) spongeEvent.getTargetEntity();
    final Vector3d hitVec = spongeEvent.getInteractionPoint().orElse(null);
    PlayerInteractEvent forgeEvent = null;
    if (hitVec != null) {
        forgeEvent = new PlayerInteractEvent.EntityInteractSpecific(entityPlayer, hand, entity, VecHelper.toVec3d(hitVec));
    } else {
        forgeEvent = new PlayerInteractEvent.EntityInteract(entityPlayer, hand, entity);
    }
    ((IMixinEventBus) MinecraftForge.EVENT_BUS).post(forgeEvent, true);
    if (forgeEvent.isCanceled()) {
        spongeEvent.setCancelled(true);
    }
    return spongeEvent;
}
Also used : 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) PlayerInteractEvent(net.minecraftforge.event.entity.player.PlayerInteractEvent) IMixinEventBus(org.spongepowered.mod.interfaces.IMixinEventBus) InteractEntityEvent(org.spongepowered.api.event.entity.InteractEntityEvent) Vector3d(com.flowpowered.math.vector.Vector3d) EnumHand(net.minecraft.util.EnumHand) EntityPlayer(net.minecraft.entity.player.EntityPlayer) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP)

Example 4 with IMixinEventBus

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

the class SpongeForgeEventFactory method callEntityItemPickupEvent.

public static ChangeInventoryEvent.Pickup.Pre callEntityItemPickupEvent(Event event) {
    ChangeInventoryEvent.Pickup.Pre spongeEvent = (ChangeInventoryEvent.Pickup.Pre) event;
    EntityItem entityItem = (EntityItem) spongeEvent.getTargetEntity();
    EntityItemPickupEvent forgeEvent = new EntityItemPickupEvent((EntityPlayer) spongeEvent.getCause().first(Player.class).get(), entityItem);
    ((IMixinEventBus) MinecraftForge.EVENT_BUS).post(forgeEvent, true);
    if (forgeEvent.isCanceled()) {
        spongeEvent.setCancelled(true);
    }
    return spongeEvent;
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) EntityPlayer(net.minecraft.entity.player.EntityPlayer) IMixinEventBus(org.spongepowered.mod.interfaces.IMixinEventBus) ChangeInventoryEvent(org.spongepowered.api.event.item.inventory.ChangeInventoryEvent) EntityItemPickupEvent(net.minecraftforge.event.entity.player.EntityItemPickupEvent) EntityItem(net.minecraft.entity.item.EntityItem)

Example 5 with IMixinEventBus

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

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