use of org.spongepowered.api.world.LocatableBlock in project SpongeForge by SpongePowered.
the class MixinBlockLeaves method onBreakBlock.
@Redirect(method = "breakBlock", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/Block;beginLeavesDecay(Lnet/minecraft/block/state/IBlockState;Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;)V", remap = false))
public void onBreakBlock(Block block, IBlockState state, net.minecraft.world.World worldIn, BlockPos pos) {
if (!worldIn.isRemote) {
final PhaseTracker phaseTracker = PhaseTracker.getInstance();
final IPhaseState currentState = phaseTracker.getCurrentState();
final boolean isBlockAlready = currentState.getPhase() != TrackingPhases.BLOCK;
@Nullable PhaseContext<?> blockDecay = null;
final boolean isWorldGen = currentState.isWorldGeneration();
if (isBlockAlready && !isWorldGen) {
final LocatableBlock locatable = LocatableBlock.builder().location(new Location<World>((World) worldIn, pos.getX(), pos.getY(), pos.getZ())).state((BlockState) state).build();
blockDecay = BlockPhase.State.BLOCK_DECAY.createPhaseContext().source(locatable);
}
try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame();
PhaseContext<?> context = blockDecay != null ? blockDecay.buildAndSwitch() : null) {
frame.addContext(EventContextKeys.LEAVES_DECAY, (World) worldIn);
if (SpongeCommonEventFactory.callChangeBlockEventPre((IMixinWorldServer) worldIn, pos).isCancelled()) {
return;
}
block.beginLeavesDecay(state, worldIn, pos);
}
} else {
block.beginLeavesDecay(state, worldIn, pos);
}
}
use of org.spongepowered.api.world.LocatableBlock in project SpongeForge by SpongePowered.
the class MixinBlockLog method onBreakBlock.
@Redirect(method = "breakBlock", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/Block;beginLeavesDecay(Lnet/minecraft/block/state/IBlockState;Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;)V", remap = false))
public void onBreakBlock(Block block, IBlockState state, net.minecraft.world.World worldIn, BlockPos pos) {
if (!worldIn.isRemote) {
if (SpongeCommonEventFactory.callChangeBlockEventPre((IMixinWorldServer) worldIn, pos).isCancelled()) {
return;
}
final PhaseTracker phaseTracker = PhaseTracker.getInstance();
final IPhaseState currentState = phaseTracker.getCurrentState();
final boolean isBlockAlready = currentState.getPhase() != TrackingPhases.BLOCK;
final boolean isWorldGen = currentState.isWorldGeneration();
if (isBlockAlready && !isWorldGen) {
final LocatableBlock locatable = LocatableBlock.builder().location(new Location<World>((World) worldIn, pos.getX(), pos.getY(), pos.getZ())).state((BlockState) state).build();
BlockPhase.State.BLOCK_DECAY.createPhaseContext().source(locatable).buildAndSwitch();
}
block.beginLeavesDecay(state, worldIn, pos);
if (isBlockAlready && !isWorldGen) {
phaseTracker.completePhase(BlockPhase.State.BLOCK_DECAY);
}
} else {
block.beginLeavesDecay(state, worldIn, pos);
}
}
use of org.spongepowered.api.world.LocatableBlock in project SpongeCommon by SpongePowered.
the class SpongeCommand method infoSubcommand.
private Command.Parameterized infoSubcommand() {
final Command.Parameterized blockInfoAtCommand = Command.builder().addParameter(CommonParameters.LOCATION_ONLINE_ONLY).executor(context -> {
final ServerLocation serverLocation = context.requireOne(CommonParameters.LOCATION_ONLINE_ONLY);
final CompletableFuture<Component> creator = this.userIdToComponent(serverLocation.get(Keys.CREATOR).orElse(null));
final CompletableFuture<Component> notifier = this.userIdToComponent(serverLocation.get(Keys.NOTIFIER).orElse(null));
CompletableFuture.allOf(creator, notifier).thenAcceptAsync(x -> context.sendMessage(Identity.nil(), Component.text().content("Block Info: ").color(TextColor.color(SpongeCommand.GREEN)).append(Component.text(serverLocation.blockPosition().toString()).hoverEvent(ItemStack.builder().fromBlockState(serverLocation.block()).build().createSnapshot())).append(Component.newline()).append(Component.text("Creator: ", TextColor.color(SpongeCommand.MINT))).append(creator.join()).append(Component.newline()).append(Component.text("Notifier: ", TextColor.color(SpongeCommand.MINT))).append(notifier.join()).build()), SpongeCommon.server());
return CommandResult.success();
}).build();
final Command.Parameterized blockInfoLookingAt = Command.builder().executor(context -> {
if (!(context.cause().root() instanceof Player)) {
return CommandResult.error(Component.text("Player required", TextColor.color(SpongeCommand.RED)));
}
final Player player = (Player) context.cause().root();
return RayTrace.block().sourceEyePosition(player).direction(player).select(RayTrace.nonAir()).limit(10).execute().map(result -> {
final LocatableBlock locatableBlock = result.selectedObject();
final CompletableFuture<Component> creator = this.userIdToComponent(locatableBlock.world().get(locatableBlock.blockPosition(), Keys.CREATOR).orElse(null));
final CompletableFuture<Component> notifier = this.userIdToComponent(locatableBlock.world().get(locatableBlock.blockPosition(), Keys.CREATOR).orElse(null));
CompletableFuture.allOf(creator, notifier).thenAcceptAsync(x -> context.sendMessage(Identity.nil(), Component.text().content("Block Info: ").color(TextColor.color(SpongeCommand.GREEN)).append(Component.text(locatableBlock.blockPosition().toString()).hoverEvent(ItemStack.builder().fromBlockState(locatableBlock.blockState()).build().createSnapshot())).append(Component.newline()).append(Component.text("Creator: ", TextColor.color(SpongeCommand.MINT))).append(creator.join()).append(Component.newline()).append(Component.text("Notifier: ", TextColor.color(SpongeCommand.MINT))).append(notifier.join()).build()), SpongeCommon.server());
return CommandResult.success();
}).orElseGet(() -> CommandResult.error(Component.text("Failed to find any block in range", NamedTextColor.RED)));
}).build();
final Command.Parameterized entityLookingAt = Command.builder().executor(context -> {
if (!(context.cause().root() instanceof Player)) {
return CommandResult.error(Component.text("Player required", TextColor.color(SpongeCommand.RED)));
}
final Player player = (Player) context.cause().root();
return RayTrace.entity().sourceEyePosition(player).direction(player).limit(10).execute().map(result -> {
final org.spongepowered.api.entity.Entity entity = result.selectedObject();
final CompletableFuture<Component> creator = this.userIdToComponent(entity.get(Keys.CREATOR).orElse(null));
final CompletableFuture<Component> notifier = this.userIdToComponent(entity.get(Keys.NOTIFIER).orElse(null));
CompletableFuture.allOf(creator, notifier).thenAcceptAsync(x -> context.sendMessage(Identity.nil(), Component.text().content("Entity Info: ").color(TextColor.color(SpongeCommand.GREEN)).append(entity.type().asComponent().hoverEvent(entity)).append(Component.newline()).append(Component.text("Creator: ", TextColor.color(SpongeCommand.MINT))).append(creator.join()).append(Component.newline()).append(Component.text("Notifier: ", TextColor.color(SpongeCommand.MINT))).append(notifier.join()).build()), SpongeCommon.server());
return CommandResult.success();
}).orElseGet(() -> CommandResult.error(Component.text("Failed to find any block in range", NamedTextColor.RED)));
}).build();
return Command.builder().addChild(blockInfoAtCommand, "blockAt").addChild(blockInfoLookingAt, "block").addChild(entityLookingAt, "entity").permission("sponge.command.info").build();
}
use of org.spongepowered.api.world.LocatableBlock in project SpongeCommon by SpongePowered.
the class SpongeTargetBlockValueParameter method parseValue.
@Override
@NonNull
public Optional<? extends ServerLocation> parseValue(@NonNull final CommandCause cause, final ArgumentReader.@NonNull Mutable reader) throws ArgumentParseException {
final Object root = cause.cause().root();
if (root instanceof Living) {
final Living living = (Living) root;
final Optional<RayTraceResult<@NonNull LocatableBlock>> rayTraceResult = RayTrace.block().sourceEyePosition(living).direction(living.headDirection()).limit(30).continueWhileBlock(RayTrace.onlyAir()).select(RayTrace.nonAir()).continueWhileEntity(r -> false).execute();
if (rayTraceResult.isPresent()) {
return rayTraceResult.map(x -> x.selectedObject().serverLocation());
}
throw reader.createException(Component.text("The cause root is not looking at a block!"));
}
throw reader.createException(Component.text("The cause root must be a Living!"));
}
use of org.spongepowered.api.world.LocatableBlock in project SpongeCommon by SpongePowered.
the class TrackingUtil method updateTickFluid.
public static void updateTickFluid(final TrackedWorldBridge mixinWorld, final FluidState fluidState, final BlockPos pos) {
final ServerLevel world = (ServerLevel) mixinWorld;
final org.spongepowered.api.world.server.ServerWorld apiWorld = (org.spongepowered.api.world.server.ServerWorld) world;
final net.minecraft.world.level.block.state.BlockState blockState = fluidState.createLegacyBlock();
if (ShouldFire.TICK_BLOCK_EVENT) {
final BlockSnapshot snapshot = mixinWorld.bridge$createSnapshot(blockState, pos, BlockChangeFlags.NONE);
final TickBlockEvent event = SpongeEventFactory.createTickBlockEventScheduled(PhaseTracker.getCauseStackManager().currentCause(), snapshot);
SpongeCommon.post(event);
if (event.isCancelled()) {
return;
}
}
final LocatableBlock locatable = new SpongeLocatableBlockBuilder().world(apiWorld).position(pos.getX(), pos.getY(), pos.getZ()).state((BlockState) blockState).build();
final FluidTickContext phaseContext = TickPhase.Tick.FLUID.createPhaseContext(PhaseTracker.SERVER).source(locatable).fluid(fluidState);
// We have to associate any notifiers in case of scheduled block updates from other sources
final PhaseContext<@NonNull ?> currentContext = PhaseTracker.getInstance().getPhaseContext();
currentContext.appendNotifierPreBlockTick(world, pos, phaseContext);
try (final PhaseContext<?> context = phaseContext;
final Timing timing = ((TimingBridge) blockState.getBlock()).bridge$timings()) {
timing.startTiming();
context.buildAndSwitch();
PhaseTracker.LOGGER.trace(TrackingUtil.FLUID_TICK, () -> "Wrapping Fluid Tick: " + fluidState.toString());
fluidState.tick(world, pos);
} catch (final Exception | NoClassDefFoundError e) {
PhasePrinter.printExceptionFromPhase(PhaseTracker.getInstance().stack, e, phaseContext);
}
}
Aggregations