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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations