| 1 | package net.minecraft.commands.arguments.blocks;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.util.Set;
|
| 5 | import java.util.function.Predicate;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.core.HolderLookup;
|
| 8 | import net.minecraft.nbt.CompoundTag;
|
| 9 | import net.minecraft.nbt.NbtUtils;
|
| 10 | import net.minecraft.server.level.ServerLevel;
|
| 11 | import net.minecraft.util.ProblemReporter;
|
| 12 | import net.minecraft.world.level.block.Block;
|
| 13 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 14 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 15 | import net.minecraft.world.level.block.state.BlockState;
|
| 16 | import net.minecraft.world.level.block.state.pattern.BlockInWorld;
|
| 17 | import net.minecraft.world.level.block.state.properties.Property;
|
| 18 | import net.minecraft.world.level.storage.TagValueInput;
|
| 19 | import net.minecraft.world.level.storage.TagValueOutput;
|
| 20 | import org.jspecify.annotations.Nullable;
|
| 21 | import org.slf4j.Logger;
|
| 22 |
|
| 23 | public class BlockInput implements Predicate<BlockInWorld> {
|
| 24 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 25 | private final BlockState state;
|
| 26 | private final Set<Property<?>> properties;
|
| 27 | private final @Nullable CompoundTag tag;
|
| 28 |
|
| 29 | public BlockInput(final BlockState state, final Set<Property<?>> properties, final @Nullable CompoundTag tag) {
|
| 30 | this.state = state;
|
| 31 | this.properties = properties;
|
| 32 | this.tag = tag;
|
| 33 | }
|
| 34 |
|
| 35 | public BlockState getState() {
|
| 36 | return this.state;
|
| 37 | }
|
| 38 |
|
| 39 | public Set<Property<?>> getDefinedProperties() {
|
| 40 | return this.properties;
|
| 41 | }
|
| 42 |
|
| 43 | public boolean test(final BlockInWorld blockInWorld) {
|
| 44 | BlockState state = blockInWorld.getState();
|
| 45 | if (!state.is(this.state.getBlock())) {
|
| 46 | return false;
|
| 47 | }
|
| 48 |
|
| 49 | for (Property<?> property : this.properties) {
|
| 50 | if (state.getValue(property) != this.state.getValue(property)) {
|
| 51 | return false;
|
| 52 | }
|
| 53 | }
|
| 54 |
|
| 55 | if (this.tag == null) {
|
| 56 | return true;
|
| 57 | }
|
| 58 |
|
| 59 | BlockEntity entity = blockInWorld.getEntity();
|
| 60 | return entity != null && NbtUtils.compareNbt(this.tag, entity.saveWithFullMetadata(blockInWorld.getLevel().registryAccess()), true);
|
| 61 | }
|
| 62 |
|
| 63 | public boolean test(final ServerLevel level, final BlockPos pos) {
|
| 64 | return this.test(new BlockInWorld(level, pos, false));
|
| 65 | }
|
| 66 |
|
| 67 | public boolean place(final ServerLevel level, final BlockPos pos, final @Block.UpdateFlags int update) {
|
| 68 | BlockState state = (update & 16) != 0 ? this.state : Block.updateFromNeighbourShapes(this.state, level, pos);
|
| 69 | if (state.isAir()) {
|
| 70 | state = this.state;
|
| 71 | }
|
| 72 |
|
| 73 | state = this.overwriteWithDefinedProperties(state);
|
| 74 | boolean affected = false;
|
| 75 | if (level.setBlock(pos, state, update)) {
|
| 76 | affected = true;
|
| 77 | }
|
| 78 |
|
| 79 | if (this.tag != null) {
|
| 80 | BlockEntity entity = level.getBlockEntity(pos);
|
| 81 | if (entity != null) {
|
| 82 | try (ProblemReporter.ScopedCollector reporter = new ProblemReporter.ScopedCollector(LOGGER)) {
|
| 83 | HolderLookup.Provider registries = level.registryAccess();
|
| 84 | ProblemReporter blockEntityReporter = reporter.forChild(entity.problemPath());
|
| 85 | TagValueOutput initialOutput = TagValueOutput.createWithContext(blockEntityReporter.forChild(() -> "(before)"), registries);
|
| 86 | entity.saveWithoutMetadata(initialOutput);
|
| 87 | CompoundTag before = initialOutput.buildResult();
|
| 88 | entity.loadWithComponents(TagValueInput.create(reporter, registries, this.tag));
|
| 89 | TagValueOutput updatedOutput = TagValueOutput.createWithContext(blockEntityReporter.forChild(() -> "(after)"), registries);
|
| 90 | entity.saveWithoutMetadata(updatedOutput);
|
| 91 | CompoundTag after = updatedOutput.buildResult();
|
| 92 | if (!after.equals(before)) {
|
| 93 | affected = true;
|
| 94 | entity.setChanged();
|
| 95 | level.getChunkSource().blockChanged(pos);
|
| 96 | }
|
| 97 | }
|
| 98 | }
|
| 99 | }
|
| 100 |
|
| 101 | return affected;
|
| 102 | }
|
| 103 |
|
| 104 | private BlockState overwriteWithDefinedProperties(BlockState state) {
|
| 105 | if (state == this.state) {
|
| 106 | return state;
|
| 107 | }
|
| 108 |
|
| 109 | for (Property<?> property : this.properties) {
|
| 110 | state = BlockBehaviour.BlockStateBase.copyProperty(this.state, state, property);
|
| 111 | }
|
| 112 |
|
| 113 | return state;
|
| 114 | }
|
| 115 | }
|
| 116 |
|