public static BlockEvent.PlaceEvent createBlockPlaceEvent(Event event) { if (!(event instanceof ChangeBlockEvent.Place)) { throw new IllegalArgumentException("Event is not a valid ChangeBlockEvent.Place event."); } ChangeBlockEvent.Place spongeEvent = (ChangeBlockEvent.Place) event; Location<World> location = spongeEvent.getTransactions().get(0).getOriginal().getLocation().get(); net.minecraft.world.World world = (net.minecraft.world.World) location.getExtent(); BlockPos pos = new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ()); BlockSnapshot replacementBlock = spongeEvent.getTransactions().get(0).getFinal(); IBlockState state = (IBlockState) replacementBlock.getState(); Optional<Player> player = spongeEvent.getCause().first(Player.class); if (!player.isPresent()) { return null; } net.minecraftforge.common.util.BlockSnapshot forgeSnapshot = new net.minecraftforge.common.util.BlockSnapshot(world, pos, state); BlockEvent.PlaceEvent forgeEvent = new BlockEvent.PlaceEvent( forgeSnapshot, world.getBlockState(pos), (EntityPlayer) player.get()); return forgeEvent; }
public static ChangeBlockEvent.Place callBlockPlaceEvent(Event event) { if (!(event instanceof ChangeBlockEvent.Place)) { throw new IllegalArgumentException("Event is not a valid ChangeBlockEventPlace"); } ChangeBlockEvent.Place spongeEvent = (ChangeBlockEvent.Place) event; if (spongeEvent.getCause().first(Player.class).isPresent()) { EntityPlayer player = (EntityPlayer) spongeEvent.getCause().first(Player.class).get(); net.minecraft.world.World world = (net.minecraft.world.World) spongeEvent.getTargetWorld(); 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(); if (StaticMixinHelper.packetPlayer != null && StaticMixinHelper.processingPacket instanceof C08PacketPlayerBlockPlacement) { C08PacketPlayerBlockPlacement packet = (C08PacketPlayerBlockPlacement) StaticMixinHelper.processingPacket; EnumFacing facing = EnumFacing.getFront(packet.getPlacedBlockDirection()); placedAgainst = blockSnapshot.world.getBlockState(blockSnapshot.pos.offset(facing.getOpposite())); } BlockEvent.PlaceEvent forgeEvent = new BlockEvent.PlaceEvent(blockSnapshot, placedAgainst, player); ((IMixinEventBus) MinecraftForge.EVENT_BUS).post(forgeEvent, true); if (forgeEvent.isCanceled()) { spongeEvent.getTransactions().get(0).setValid(false); } } 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(); if (StaticMixinHelper.packetPlayer != null && StaticMixinHelper.processingPacket instanceof C08PacketPlayerBlockPlacement) { C08PacketPlayerBlockPlacement packet = (C08PacketPlayerBlockPlacement) StaticMixinHelper.processingPacket; EnumFacing facing = EnumFacing.getFront(packet.getPlacedBlockDirection()); placedAgainst = blockSnapshots .get(0) .world .getBlockState(blockSnapshots.get(0).pos.offset(facing.getOpposite())); } BlockEvent.MultiPlaceEvent forgeEvent = new BlockEvent.MultiPlaceEvent(blockSnapshots, placedAgainst, player); ((IMixinEventBus) MinecraftForge.EVENT_BUS).post(forgeEvent, true); if (forgeEvent.isCanceled()) { while (iterator.hasNext()) { iterator.next().setValid(false); } } } } return spongeEvent; }