package net.minecraft.network.chat.contents; import com.mojang.brigadier.StringReader; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.serialization.Codec; import com.mojang.serialization.MapCodec; import com.mojang.serialization.codecs.RecordCodecBuilder; import java.util.stream.Stream; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.arguments.coordinates.BlockPosArgument; import net.minecraft.commands.arguments.coordinates.Coordinates; import net.minecraft.core.BlockPos; import net.minecraft.nbt.CompoundTag; import net.minecraft.server.level.ServerLevel; import net.minecraft.world.level.block.entity.BlockEntity; import org.jetbrains.annotations.Nullable; public record BlockDataSource(String posPattern, @Nullable Coordinates compiledPos) implements DataSource { public static final MapCodec SUB_CODEC = RecordCodecBuilder.mapCodec( instance -> instance.group(Codec.STRING.fieldOf("block").forGetter(BlockDataSource::posPattern)).apply(instance, BlockDataSource::new) ); public static final DataSource.Type TYPE = new DataSource.Type<>(SUB_CODEC, "block"); public BlockDataSource(String posPattern) { this(posPattern, compilePos(posPattern)); } @Nullable private static Coordinates compilePos(String posPattern) { try { return BlockPosArgument.blockPos().parse(new StringReader(posPattern)); } catch (CommandSyntaxException var2) { return null; } } @Override public Stream getData(CommandSourceStack source) { if (this.compiledPos != null) { ServerLevel serverLevel = source.getLevel(); BlockPos blockPos = this.compiledPos.getBlockPos(source); if (serverLevel.isLoaded(blockPos)) { BlockEntity blockEntity = serverLevel.getBlockEntity(blockPos); if (blockEntity != null) { return Stream.of(blockEntity.saveWithFullMetadata(source.registryAccess())); } } } return Stream.empty(); } @Override public DataSource.Type type() { return TYPE; } public String toString() { return "block=" + this.posPattern; } public boolean equals(Object object) { return this == object ? true : object instanceof BlockDataSource blockDataSource && this.posPattern.equals(blockDataSource.posPattern); } public int hashCode() { return this.posPattern.hashCode(); } }