use of org.spongepowered.api.world.LocatableBlock in project SpongeCommon by SpongePowered.
the class PlaceBlockPacketState method addNotifierToBlockEvent.
@Override
public void addNotifierToBlockEvent(BasicPacketContext context, IMixinWorldServer mixinWorldServer, BlockPos pos, IMixinBlockEventData blockEvent) {
final Player player = Sponge.getCauseStackManager().getCurrentCause().first(Player.class).get();
final Location<World> location = new Location<>(player.getWorld(), pos.getX(), pos.getY(), pos.getZ());
final LocatableBlock locatableBlock = LocatableBlock.builder().location(location).state(location.getBlock()).build();
blockEvent.setTickBlock(locatableBlock);
blockEvent.setSourceUser(player);
}
use of org.spongepowered.api.world.LocatableBlock in project SpongeCommon by SpongePowered.
the class BlockTickPhaseState method spawnEntityOrCapture.
@Override
public boolean spawnEntityOrCapture(BlockTickContext context, Entity entity, int chunkX, int chunkZ) {
final LocatableBlock locatableBlock = getLocatableBlockSourceFromContext(context);
final Optional<User> owner = context.getOwner();
final Optional<User> notifier = context.getNotifier();
final User entityCreator = notifier.orElseGet(() -> owner.orElse(null));
try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
Sponge.getCauseStackManager().pushCause(locatableBlock);
Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.EXPERIENCE);
if (notifier.isPresent()) {
Sponge.getCauseStackManager().addContext(EventContextKeys.NOTIFIER, notifier.get());
}
if (owner.isPresent()) {
Sponge.getCauseStackManager().addContext(EventContextKeys.OWNER, owner.get());
}
if (entity instanceof EntityXPOrb) {
final ArrayList<Entity> entities = new ArrayList<>(1);
entities.add(entity);
final SpawnEntityEvent event = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), entities);
SpongeImpl.postEvent(event);
if (!event.isCancelled()) {
for (Entity anEntity : event.getEntities()) {
if (entityCreator != null) {
EntityUtil.toMixin(anEntity).setCreator(entityCreator.getUniqueId());
}
EntityUtil.getMixinWorld(entity).forceSpawnEntity(anEntity);
}
return true;
}
return false;
}
final List<Entity> nonExpEntities = new ArrayList<>(1);
nonExpEntities.add(entity);
Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.BLOCK_SPAWNING);
final SpawnEntityEvent spawnEntityEvent = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), nonExpEntities);
SpongeImpl.postEvent(spawnEntityEvent);
if (!spawnEntityEvent.isCancelled()) {
for (Entity anEntity : spawnEntityEvent.getEntities()) {
if (entityCreator != null) {
EntityUtil.toMixin(anEntity).setCreator(entityCreator.getUniqueId());
}
EntityUtil.getMixinWorld(entity).forceSpawnEntity(anEntity);
}
return true;
}
}
return false;
}
use of org.spongepowered.api.world.LocatableBlock 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.api.world.LocatableBlock in project SpongeCommon by SpongePowered.
the class TrackingUtil method updateTickBlock.
public static void updateTickBlock(IMixinWorldServer mixinWorld, Block block, BlockPos pos, IBlockState state, Random random) {
final WorldServer minecraftWorld = mixinWorld.asMinecraftWorld();
try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
Sponge.getCauseStackManager().pushCause(minecraftWorld);
if (ShouldFire.TICK_BLOCK_EVENT) {
BlockSnapshot snapshot = mixinWorld.createSpongeBlockSnapshot(state, state, pos, BlockChangeFlags.NONE);
final TickBlockEvent event = SpongeEventFactory.createTickBlockEventScheduled(Sponge.getCauseStackManager().getCurrentCause(), snapshot);
SpongeImpl.postEvent(event);
if (event.isCancelled()) {
return;
}
}
final LocatableBlock locatable = LocatableBlock.builder().location(new Location<>(mixinWorld.asSpongeWorld(), pos.getX(), pos.getY(), pos.getZ())).state((BlockState) state).build();
Sponge.getCauseStackManager().pushCause(locatable);
IPhaseState<BlockTickContext> phase = ((IMixinBlock) block).requiresBlockCapture() ? TickPhase.Tick.BLOCK : TickPhase.Tick.NO_CAPTURE_BLOCK;
final BlockTickContext phaseContext = phase.createPhaseContext().source(locatable);
checkAndAssignBlockTickConfig(block, minecraftWorld, phaseContext);
final PhaseTracker phaseTracker = PhaseTracker.getInstance();
// We have to associate any notifiers in case of scheduled block updates from other sources
final PhaseData current = phaseTracker.getCurrentPhaseData();
final IPhaseState<?> currentState = current.state;
((IPhaseState) currentState).appendNotifierPreBlockTick(mixinWorld, pos, current.context, phaseContext);
try (PhaseContext<?> context = phaseContext.buildAndSwitch()) {
block.updateTick(minecraftWorld, pos, state, random);
} catch (Exception | NoClassDefFoundError e) {
phaseTracker.printExceptionFromPhase(e, phaseContext);
}
}
}
use of org.spongepowered.api.world.LocatableBlock in project SpongeCommon by SpongePowered.
the class SpongeCommonEventFactory method callNotifyNeighborEvent.
@SuppressWarnings("rawtypes")
public static NotifyNeighborBlockEvent callNotifyNeighborEvent(World world, BlockPos sourcePos, EnumSet notifiedSides) {
final PhaseTracker phaseTracker = PhaseTracker.getInstance();
final PhaseData peek = phaseTracker.getCurrentPhaseData();
final PhaseContext<?> context = peek.context;
// Don't fire notify events during world gen or while restoring
if (peek.state.isWorldGeneration() || peek.state == State.RESTORING_BLOCKS) {
return null;
}
try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
final BlockState blockstate = (BlockState) ((net.minecraft.world.World) world).getBlockState(sourcePos);
final LocatableBlock locatable = LocatableBlock.builder().location(new Location<World>(world, sourcePos.getX(), sourcePos.getY(), sourcePos.getZ())).state(blockstate).build();
if (context.getNotifier().isPresent()) {
context.addNotifierAndOwnerToCauseStack();
} else {
final IMixinChunk mixinChunk = (IMixinChunk) ((WorldServer) world).getChunkFromBlockCoords(sourcePos);
mixinChunk.getBlockNotifier(sourcePos).ifPresent(user -> Sponge.getCauseStackManager().addContext(EventContextKeys.NOTIFIER, user));
mixinChunk.getBlockOwner(sourcePos).ifPresent(owner -> Sponge.getCauseStackManager().addContext(EventContextKeys.OWNER, owner));
}
Sponge.getCauseStackManager().pushCause(locatable);
final Map<Direction, BlockState> neighbors = new HashMap<>();
for (Object obj : notifiedSides) {
EnumFacing notifiedSide = (EnumFacing) obj;
BlockPos offset = sourcePos.offset(notifiedSide);
Direction direction = DirectionFacingProvider.getInstance().getKey(notifiedSide).get();
Location<World> location = new Location<>(world, VecHelper.toVector3i(offset));
if (location.getBlockY() >= 0 && location.getBlockY() <= 255) {
neighbors.put(direction, location.getBlock());
}
}
// ImmutableMap<Direction, BlockState> originalNeighbors =
// ImmutableMap.copyOf(neighbors);
NotifyNeighborBlockEvent event = SpongeEventFactory.createNotifyNeighborBlockEvent(Sponge.getCauseStackManager().getCurrentCause(), neighbors, neighbors);
SpongeImpl.postEvent(event);
return event;
}
}
Aggregations