Search in sources :

Example 26 with LocatableBlock

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);
    }
}
Also used : PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) BlockState(org.spongepowered.api.block.BlockState) IBlockState(net.minecraft.block.state.IBlockState) CauseStackManager(org.spongepowered.api.event.CauseStackManager) IPhaseState(org.spongepowered.common.event.tracking.IPhaseState) LocatableBlock(org.spongepowered.api.world.LocatableBlock) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) World(org.spongepowered.api.world.World) Nullable(javax.annotation.Nullable) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 27 with LocatableBlock

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);
    }
}
Also used : PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) BlockState(org.spongepowered.api.block.BlockState) IBlockState(net.minecraft.block.state.IBlockState) IPhaseState(org.spongepowered.common.event.tracking.IPhaseState) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) LocatableBlock(org.spongepowered.api.world.LocatableBlock) World(org.spongepowered.api.world.World) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 28 with LocatableBlock

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();
}
Also used : RefreshGameEvent(org.spongepowered.api.event.lifecycle.RefreshGameEvent) ServerLevelBridge(org.spongepowered.common.bridge.server.level.ServerLevelBridge) Command(org.spongepowered.api.command.Command) URL(java.net.URL) SpongeGameConfigs(org.spongepowered.common.config.SpongeGameConfigs) ItemStack(org.spongepowered.api.item.inventory.ItemStack) PluginMetadata(org.spongepowered.plugin.metadata.PluginMetadata) Method(java.lang.reflect.Method) LinearComponents(net.kyori.adventure.text.LinearComponents) TextComponent(net.kyori.adventure.text.TextComponent) TextColor(net.kyori.adventure.text.format.TextColor) LocatableBlock(org.spongepowered.api.world.LocatableBlock) Collection(java.util.Collection) Launch(org.spongepowered.common.launch.Launch) Sponge(org.spongepowered.api.Sponge) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) NamedTextColor(net.kyori.adventure.text.format.NamedTextColor) Keys(org.spongepowered.api.data.Keys) List(java.util.List) Optional(java.util.Optional) JoinConfiguration(net.kyori.adventure.text.JoinConfiguration) Player(org.spongepowered.api.entity.living.player.Player) HoverEvent(net.kyori.adventure.text.event.HoverEvent) ServerLocation(org.spongepowered.api.world.server.ServerLocation) PluginContributor(org.spongepowered.plugin.metadata.model.PluginContributor) Timings(co.aikar.timings.Timings) NonNull(org.checkerframework.checker.nullness.qual.NonNull) ServerWorld(org.spongepowered.api.world.server.ServerWorld) LocalDateTime(java.time.LocalDateTime) CompletableFuture(java.util.concurrent.CompletableFuture) ClickEvent(net.kyori.adventure.text.event.ClickEvent) ServerLevel(net.minecraft.server.level.ServerLevel) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) Style(net.kyori.adventure.text.format.Style) SpongeEventManager(org.spongepowered.common.event.manager.SpongeEventManager) Parameter(org.spongepowered.api.command.parameter.Parameter) Component(net.kyori.adventure.text.Component) MBeanServer(javax.management.MBeanServer) ResourceKey(org.spongepowered.api.ResourceKey) RayTrace(org.spongepowered.api.util.blockray.RayTrace) ManagementFactory(java.lang.management.ManagementFactory) Nullable(org.checkerframework.checker.nullness.qual.Nullable) CommonParameters(org.spongepowered.api.command.parameter.CommonParameters) CommandResult(org.spongepowered.api.command.CommandResult) LevelBridge(org.spongepowered.common.bridge.world.level.LevelBridge) Identity(net.kyori.adventure.identity.Identity) TextDecoration(net.kyori.adventure.text.format.TextDecoration) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) DecimalFormat(java.text.DecimalFormat) SpongeCommon(org.spongepowered.common.SpongeCommon) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) File(java.io.File) SpongeTimingsFactory(co.aikar.timings.sponge.SpongeTimingsFactory) CommandMapping(org.spongepowered.api.command.manager.CommandMapping) Entity(net.minecraft.world.entity.Entity) PluginContainer(org.spongepowered.plugin.PluginContainer) DateTimeFormatter(java.time.format.DateTimeFormatter) SpongeConfigs(org.spongepowered.common.applaunch.config.core.SpongeConfigs) CommandContext(org.spongepowered.api.command.parameter.CommandContext) SpongeComponents(org.spongepowered.api.adventure.SpongeComponents) Mth(net.minecraft.util.Mth) CompletableFuture(java.util.concurrent.CompletableFuture) Player(org.spongepowered.api.entity.living.player.Player) Command(org.spongepowered.api.command.Command) ServerLocation(org.spongepowered.api.world.server.ServerLocation) LocatableBlock(org.spongepowered.api.world.LocatableBlock) TextComponent(net.kyori.adventure.text.TextComponent) Component(net.kyori.adventure.text.Component) Identity(net.kyori.adventure.identity.Identity)

Example 29 with LocatableBlock

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!"));
}
Also used : NonNull(org.checkerframework.checker.nullness.qual.NonNull) LocatableBlock(org.spongepowered.api.world.LocatableBlock) ArgumentReader(org.spongepowered.api.command.parameter.ArgumentReader) CommandCause(org.spongepowered.api.command.CommandCause) Component(net.kyori.adventure.text.Component) ArgumentParseException(org.spongepowered.api.command.exception.ArgumentParseException) ResourceKey(org.spongepowered.api.ResourceKey) CommandContext(org.spongepowered.api.command.parameter.CommandContext) ResourceKeyedZeroAdvanceValueParameter(org.spongepowered.common.command.brigadier.argument.ResourceKeyedZeroAdvanceValueParameter) Optional(java.util.Optional) RayTrace(org.spongepowered.api.util.blockray.RayTrace) Living(org.spongepowered.api.entity.living.Living) RayTraceResult(org.spongepowered.api.util.blockray.RayTraceResult) ServerLocation(org.spongepowered.api.world.server.ServerLocation) Living(org.spongepowered.api.entity.living.Living) NonNull(org.checkerframework.checker.nullness.qual.NonNull) RayTraceResult(org.spongepowered.api.util.blockray.RayTraceResult) NonNull(org.checkerframework.checker.nullness.qual.NonNull)

Example 30 with LocatableBlock

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);
    }
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) SpongeLocatableBlockBuilder(org.spongepowered.common.world.server.SpongeLocatableBlockBuilder) TickBlockEvent(org.spongepowered.api.event.block.TickBlockEvent) LocatableBlock(org.spongepowered.api.world.LocatableBlock) SpongeBlockSnapshot(org.spongepowered.common.block.SpongeBlockSnapshot) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) FluidTickContext(org.spongepowered.common.event.tracking.phase.tick.FluidTickContext) BlockState(org.spongepowered.api.block.BlockState) Timing(co.aikar.timings.Timing) TimingBridge(org.spongepowered.common.bridge.TimingBridge)

Aggregations

LocatableBlock (org.spongepowered.api.world.LocatableBlock)34 BlockState (org.spongepowered.api.block.BlockState)13 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)9 IBlockState (net.minecraft.block.state.IBlockState)8 StackFrame (org.spongepowered.api.event.CauseStackManager.StackFrame)8 World (org.spongepowered.api.world.World)8 PhaseTracker (org.spongepowered.common.event.tracking.PhaseTracker)8 Entity (org.spongepowered.api.entity.Entity)7 CauseStackManager (org.spongepowered.api.event.CauseStackManager)7 IMixinWorldServer (org.spongepowered.common.interfaces.world.IMixinWorldServer)7 SpongeLocatableBlockBuilder (org.spongepowered.common.world.server.SpongeLocatableBlockBuilder)7 ArrayList (java.util.ArrayList)6 ServerLevel (net.minecraft.server.level.ServerLevel)6 TickBlockEvent (org.spongepowered.api.event.block.TickBlockEvent)6 SpongeBlockSnapshot (org.spongepowered.common.block.SpongeBlockSnapshot)6 List (java.util.List)5 Optional (java.util.Optional)4 Collectors (java.util.stream.Collectors)4 Sponge (org.spongepowered.api.Sponge)4 Player (org.spongepowered.api.entity.living.player.Player)4