| 1 | package net.minecraft.core.component;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import io.netty.buffer.ByteBuf;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Optional;
|
| 8 | import java.util.function.IntFunction;
|
| 9 | import net.minecraft.advancements.triggers.CriteriaTriggers;
|
| 10 | import net.minecraft.core.BlockPos;
|
| 11 | import net.minecraft.core.Direction;
|
| 12 | import net.minecraft.core.Holder;
|
| 13 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 14 | import net.minecraft.core.registries.Registries;
|
| 15 | import net.minecraft.core.registries.codec.RegistryCodecs;
|
| 16 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 17 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 18 | import net.minecraft.network.codec.StreamCodec;
|
| 19 | import net.minecraft.resources.ResourceKey;
|
| 20 | import net.minecraft.server.level.ServerLevel;
|
| 21 | import net.minecraft.server.level.ServerPlayer;
|
| 22 | import net.minecraft.sounds.SoundEvent;
|
| 23 | import net.minecraft.sounds.SoundEvents;
|
| 24 | import net.minecraft.sounds.SoundSource;
|
| 25 | import net.minecraft.util.ByIdMap;
|
| 26 | import net.minecraft.util.ExtraCodecs;
|
| 27 | import net.minecraft.util.StringRepresentable;
|
| 28 | import net.minecraft.world.InteractionHand;
|
| 29 | import net.minecraft.world.InteractionResult;
|
| 30 | import net.minecraft.world.entity.Entity;
|
| 31 | import net.minecraft.world.entity.player.Player;
|
| 32 | import net.minecraft.world.item.ItemStack;
|
| 33 | import net.minecraft.world.item.context.UseOnContext;
|
| 34 | import net.minecraft.world.level.Level;
|
| 35 | import net.minecraft.world.level.block.Block;
|
| 36 | import net.minecraft.world.level.block.ChestBlock;
|
| 37 | import net.minecraft.world.level.block.CopperChestBlock;
|
| 38 | import net.minecraft.world.level.block.LevelEvent;
|
| 39 | import net.minecraft.world.level.block.state.BlockState;
|
| 40 | import net.minecraft.world.level.block.state.properties.ChestType;
|
| 41 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 42 | import net.minecraft.world.level.levelgen.blockpredicates.BlockPredicate;
|
| 43 | import net.minecraft.world.level.levelgen.feature.stateproviders.BlockStateProvider;
|
| 44 | import net.minecraft.world.level.levelgen.feature.stateproviders.RuleBasedStateProvider;
|
| 45 | import net.minecraft.world.level.storage.loot.LootTable;
|
| 46 | import org.jspecify.annotations.Nullable;
|
| 47 |
|
| 48 | public record BlockTransformer(List<BlockTransformer.BlockTransformData> transforms) {
|
| 49 | public static final Codec<BlockTransformer> DIRECT_CODEC = BlockTransformer.BlockTransformData.CODEC
|
| 50 | .listOf(1, 200)
|
| 51 | .xmap(BlockTransformer::new, BlockTransformer::transforms);
|
| 52 | public static final Codec<Holder<BlockTransformer>> CODEC = RegistryCodecs.holder(Registries.BLOCK_TRANSFORMER);
|
| 53 | public static final StreamCodec<RegistryFriendlyByteBuf, Holder<BlockTransformer>> STREAM_CODEC = ByteBufCodecs.holderRegistry(Registries.BLOCK_TRANSFORMER);
|
| 54 |
|
| 55 | public InteractionResult transformBlock(final UseOnContext context) {
|
| 56 | if (playerHasBlockingItemUseIntent(context)) {
|
| 57 | return InteractionResult.PASS;
|
| 58 | }
|
| 59 |
|
| 60 | BlockPos pos = context.getClickedPos();
|
| 61 | Level level = context.getLevel();
|
| 62 |
|
| 63 | for (BlockTransformer.BlockTransformData transformData : this.transforms) {
|
| 64 | Direction clickedFace = context.getClickedFace();
|
| 65 | if (!transformData.disallowedFaces().contains(clickedFace)) {
|
| 66 | BlockState newBlockState = transformData.blockStateProvider.value().getOptionalState(level, level.getRandom(), pos);
|
| 67 | if (newBlockState != null) {
|
| 68 | BlockState updatedShape = transformData.updateFromNeighbors ? Block.updateFromNeighbourShapes(newBlockState, level, pos) : newBlockState;
|
| 69 | Player player = context.getPlayer();
|
| 70 | ItemStack itemInHand = context.getItemInHand();
|
| 71 | if (player instanceof ServerPlayer serverPlayer) {
|
| 72 | CriteriaTriggers.ITEM_USED_ON_BLOCK.trigger(serverPlayer, pos, itemInHand);
|
| 73 | }
|
| 74 |
|
| 75 | BlockState oldBlockState = level.getBlockState(pos);
|
| 76 | if (level instanceof ServerLevel serverLevel) {
|
| 77 | transformData.loot
|
| 78 | .ifPresent(
|
| 79 | lt -> Block.dropFromBlockInteractLootTable(
|
| 80 | serverLevel,
|
| 81 | (ResourceKey<LootTable>)lt,
|
| 82 | pos,
|
| 83 | oldBlockState,
|
| 84 | level.getBlockEntity(pos),
|
| 85 | itemInHand,
|
| 86 | player,
|
| 87 | (sl, stack) -> transformData.dropStrategy.resourcePopper.pop(sl, pos, clickedFace, stack)
|
| 88 | )
|
| 89 | );
|
| 90 | }
|
| 91 |
|
| 92 | if (itemInHand.isStackable()) {
|
| 93 | itemInHand.consume(transformData.consumeOnUse ? 1 : 0, player);
|
| 94 | } else if (player != null) {
|
| 95 | itemInHand.hurtAndBreak(transformData.itemDamagePerUse, player, context.getHand().asEquipmentSlot());
|
| 96 | }
|
| 97 |
|
| 98 | level.setBlock(pos, updatedShape, 11);
|
| 99 | level.playSound(player, pos, transformData.sound.value(), SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 100 | transformData.particle.send(level, player, pos);
|
| 101 | level.gameEvent(GameEvent.BLOCK_CHANGE, pos, GameEvent.Context.of(player, updatedShape));
|
| 102 | if (transformData.transformType == BlockTransformer.TransformType.COPPER_CHEST
|
| 103 | && oldBlockState.getBlock() instanceof CopperChestBlock
|
| 104 | && oldBlockState.getValue(ChestBlock.TYPE) != ChestType.SINGLE) {
|
| 105 | BlockPos neighborPos = ChestBlock.getConnectedBlockPos(pos, oldBlockState);
|
| 106 | level.gameEvent(GameEvent.BLOCK_CHANGE, neighborPos, GameEvent.Context.of(player, level.getBlockState(neighborPos)));
|
| 107 | transformData.particle.send(level, player, neighborPos);
|
| 108 | }
|
| 109 |
|
| 110 | return InteractionResult.SUCCESS;
|
| 111 | }
|
| 112 | }
|
| 113 | }
|
| 114 |
|
| 115 | return InteractionResult.PASS;
|
| 116 | }
|
| 117 |
|
| 118 | private static boolean playerHasBlockingItemUseIntent(final UseOnContext context) {
|
| 119 | Player player = context.getPlayer();
|
| 120 | return context.getHand().equals(InteractionHand.MAIN_HAND) && player.getOffhandItem().has(DataComponents.BLOCKS_ATTACKS) && !player.isSecondaryUseActive();
|
| 121 | }
|
| 122 |
|
| 123 | public record BlockTransformData(
|
| 124 | Holder<BlockStateProvider> blockStateProvider,
|
| 125 | Holder<SoundEvent> sound,
|
| 126 | BlockTransformer.TransformParticle particle,
|
| 127 | List<Direction> disallowedFaces,
|
| 128 | Optional<ResourceKey<LootTable>> loot,
|
| 129 | BlockTransformer.DropStrategy dropStrategy,
|
| 130 | boolean updateFromNeighbors,
|
| 131 | BlockTransformer.TransformType transformType,
|
| 132 | boolean consumeOnUse,
|
| 133 | int itemDamagePerUse
|
| 134 | ) {
|
| 135 | public static final Codec<BlockTransformer.BlockTransformData> CODEC = RecordCodecBuilder.create(
|
| 136 | i -> i.group(
|
| 137 | BlockStateProvider.CODEC.fieldOf("block_state_provider").forGetter(BlockTransformer.BlockTransformData::blockStateProvider),
|
| 138 | SoundEvent.CODEC
|
| 139 | .optionalFieldOf("sound", BuiltInRegistries.SOUND_EVENT.wrapAsHolder(SoundEvents.EMPTY))
|
| 140 | .forGetter(BlockTransformer.BlockTransformData::sound),
|
| 141 | BlockTransformer.TransformParticle.CODEC
|
| 142 | .optionalFieldOf("particle", BlockTransformer.TransformParticle.NONE)
|
| 143 | .forGetter(BlockTransformer.BlockTransformData::particle),
|
| 144 | Direction.CODEC.listOf().optionalFieldOf("disallowed_faces", List.of()).forGetter(BlockTransformer.BlockTransformData::disallowedFaces),
|
| 145 | LootTable.KEY_CODEC.optionalFieldOf("loot").forGetter(BlockTransformer.BlockTransformData::loot),
|
| 146 | BlockTransformer.DropStrategy.CODEC
|
| 147 | .optionalFieldOf("drop_strategy", BlockTransformer.DropStrategy.FROM_MIDDLE)
|
| 148 | .forGetter(BlockTransformer.BlockTransformData::dropStrategy),
|
| 149 | Codec.BOOL.optionalFieldOf("update_from_neighbors", true).forGetter(BlockTransformer.BlockTransformData::updateFromNeighbors),
|
| 150 | BlockTransformer.TransformType.CODEC
|
| 151 | .optionalFieldOf("transform_type", BlockTransformer.TransformType.SINGLE_BLOCK)
|
| 152 | .forGetter(BlockTransformer.BlockTransformData::transformType),
|
| 153 | Codec.BOOL.optionalFieldOf("consume_on_use", true).forGetter(BlockTransformer.BlockTransformData::consumeOnUse),
|
| 154 | ExtraCodecs.NON_NEGATIVE_INT.optionalFieldOf("item_damage_per_use", 0).forGetter(BlockTransformer.BlockTransformData::itemDamagePerUse)
|
| 155 | )
|
| 156 | .apply(i, BlockTransformer.BlockTransformData::new)
|
| 157 | );
|
| 158 | public static final StreamCodec<RegistryFriendlyByteBuf, BlockTransformer.BlockTransformData> STREAM_CODEC = StreamCodec.composite(
|
| 159 | ByteBufCodecs.fromCodecWithRegistries(BlockStateProvider.CODEC),
|
| 160 | BlockTransformer.BlockTransformData::blockStateProvider,
|
| 161 | SoundEvent.STREAM_CODEC,
|
| 162 | BlockTransformer.BlockTransformData::sound,
|
| 163 | BlockTransformer.TransformParticle.STREAM_CODEC,
|
| 164 | BlockTransformer.BlockTransformData::particle,
|
| 165 | Direction.STREAM_CODEC.apply(ByteBufCodecs.list()),
|
| 166 | BlockTransformer.BlockTransformData::disallowedFaces,
|
| 167 | ResourceKey.streamCodec(Registries.LOOT_TABLE).apply(ByteBufCodecs::optional),
|
| 168 | BlockTransformer.BlockTransformData::loot,
|
| 169 | BlockTransformer.DropStrategy.STREAM_CODEC,
|
| 170 | BlockTransformer.BlockTransformData::dropStrategy,
|
| 171 | ByteBufCodecs.BOOL,
|
| 172 | BlockTransformer.BlockTransformData::updateFromNeighbors,
|
| 173 | BlockTransformer.TransformType.STREAM_CODEC,
|
| 174 | BlockTransformer.BlockTransformData::transformType,
|
| 175 | ByteBufCodecs.BOOL,
|
| 176 | BlockTransformer.BlockTransformData::consumeOnUse,
|
| 177 | ByteBufCodecs.VAR_INT,
|
| 178 | BlockTransformer.BlockTransformData::itemDamagePerUse,
|
| 179 | BlockTransformer.BlockTransformData::new
|
| 180 | );
|
| 181 |
|
| 182 | public static BlockTransformer.BlockTransformData.Builder builder(final Holder<BlockStateProvider> targetStateProvider) {
|
| 183 | return new BlockTransformer.BlockTransformData.Builder(targetStateProvider);
|
| 184 | }
|
| 185 |
|
| 186 | public static BlockTransformer.BlockTransformData.Builder builder(final BlockStateProvider targetStateProvider) {
|
| 187 | return builder(Holder.direct(targetStateProvider));
|
| 188 | }
|
| 189 |
|
| 190 | public static BlockTransformer.BlockTransformData.Builder builder(final BlockPredicate predicate, final Block block) {
|
| 191 | return builder(RuleBasedStateProvider.builder().ifTrueThenProvide(predicate, block).build());
|
| 192 | }
|
| 193 |
|
| 194 | public static class Builder {
|
| 195 | private final Holder<BlockStateProvider> targetStateProvider;
|
| 196 | private Holder<SoundEvent> sound = BuiltInRegistries.SOUND_EVENT.wrapAsHolder(SoundEvents.EMPTY);
|
| 197 | private BlockTransformer.TransformParticle particle = BlockTransformer.TransformParticle.NONE;
|
| 198 | private List<Direction> disallowedFaces = List.of();
|
| 199 | private Optional<ResourceKey<LootTable>> loot = Optional.empty();
|
| 200 | private BlockTransformer.DropStrategy dropStrategy = BlockTransformer.DropStrategy.FROM_MIDDLE;
|
| 201 | private boolean updateFromNeighbors = true;
|
| 202 | private BlockTransformer.TransformType transformType = BlockTransformer.TransformType.SINGLE_BLOCK;
|
| 203 | private boolean consumeOnUse = true;
|
| 204 | private int itemDamagePerUse = 1;
|
| 205 |
|
| 206 | private Builder(final Holder<BlockStateProvider> targetStateProvider) {
|
| 207 | this.targetStateProvider = targetStateProvider;
|
| 208 | }
|
| 209 |
|
| 210 | public BlockTransformer.BlockTransformData.Builder sound(final Holder<SoundEvent> sound) {
|
| 211 | this.sound = sound;
|
| 212 | return this;
|
| 213 | }
|
| 214 |
|
| 215 | public BlockTransformer.BlockTransformData.Builder particle(final BlockTransformer.TransformParticle particle) {
|
| 216 | this.particle = particle;
|
| 217 | return this;
|
| 218 | }
|
| 219 |
|
| 220 | public BlockTransformer.BlockTransformData.Builder disallowedFaces(final List<Direction> disallowedFaces) {
|
| 221 | this.disallowedFaces = disallowedFaces;
|
| 222 | return this;
|
| 223 | }
|
| 224 |
|
| 225 | public BlockTransformer.BlockTransformData.Builder loot(final ResourceKey<LootTable> loot) {
|
| 226 | this.loot = Optional.of(loot);
|
| 227 | return this;
|
| 228 | }
|
| 229 |
|
| 230 | public BlockTransformer.BlockTransformData.Builder dropStrategy(final BlockTransformer.DropStrategy dropStrategy) {
|
| 231 | this.dropStrategy = dropStrategy;
|
| 232 | return this;
|
| 233 | }
|
| 234 |
|
| 235 | public BlockTransformer.BlockTransformData.Builder updateFromNeighbors(final boolean updateFromNeighbors) {
|
| 236 | this.updateFromNeighbors = updateFromNeighbors;
|
| 237 | return this;
|
| 238 | }
|
| 239 |
|
| 240 | public BlockTransformer.BlockTransformData.Builder transformType(final BlockTransformer.TransformType transformType) {
|
| 241 | this.transformType = transformType;
|
| 242 | return this;
|
| 243 | }
|
| 244 |
|
| 245 | public BlockTransformer.BlockTransformData.Builder consumeOnUse(final boolean consumeOnUse) {
|
| 246 | this.consumeOnUse = consumeOnUse;
|
| 247 | return this;
|
| 248 | }
|
| 249 |
|
| 250 | public BlockTransformer.BlockTransformData.Builder itemDamagePerUse(final int itemDamagePerUse) {
|
| 251 | this.itemDamagePerUse = itemDamagePerUse;
|
| 252 | return this;
|
| 253 | }
|
| 254 |
|
| 255 | public BlockTransformer.BlockTransformData build() {
|
| 256 | return new BlockTransformer.BlockTransformData(
|
| 257 | this.targetStateProvider,
|
| 258 | this.sound,
|
| 259 | this.particle,
|
| 260 | this.disallowedFaces,
|
| 261 | this.loot,
|
| 262 | this.dropStrategy,
|
| 263 | this.updateFromNeighbors,
|
| 264 | this.transformType,
|
| 265 | this.consumeOnUse,
|
| 266 | this.itemDamagePerUse
|
| 267 | );
|
| 268 | }
|
| 269 | }
|
| 270 | }
|
| 271 |
|
| 272 | public enum DropStrategy implements StringRepresentable {
|
| 273 | CLICKED_FACE(0, "clicked_face", BlockTransformer.DropStrategy.ResourcePopper.FROM_FACE),
|
| 274 | FROM_MIDDLE(1, "from_middle", BlockTransformer.DropStrategy.ResourcePopper.FROM_MIDDLE);
|
| 275 |
|
| 276 | private final int id;
|
| 277 | private final String name;
|
| 278 | private final BlockTransformer.DropStrategy.ResourcePopper resourcePopper;
|
| 279 | public static final Codec<BlockTransformer.DropStrategy> CODEC = StringRepresentable.fromValues(BlockTransformer.DropStrategy::values);
|
| 280 | private static final IntFunction<BlockTransformer.DropStrategy> BY_ID = ByIdMap.continuous(
|
| 281 | BlockTransformer.DropStrategy::getId, values(), ByIdMap.OutOfBoundsStrategy.ZERO
|
| 282 | );
|
| 283 | public static final StreamCodec<ByteBuf, BlockTransformer.DropStrategy> STREAM_CODEC = ByteBufCodecs.idMapper(BY_ID, BlockTransformer.DropStrategy::getId);
|
| 284 |
|
| 285 | DropStrategy(final int id, final String name, final BlockTransformer.DropStrategy.ResourcePopper resourcePopper) {
|
| 286 | this.id = id;
|
| 287 | this.name = name;
|
| 288 | this.resourcePopper = resourcePopper;
|
| 289 | }
|
| 290 |
|
| 291 | @Override
|
| 292 | public String getSerializedName() {
|
| 293 | return this.name;
|
| 294 | }
|
| 295 |
|
| 296 | public int getId() {
|
| 297 | return this.id;
|
| 298 | }
|
| 299 |
|
| 300 | public void pop(final Level level, final BlockPos pos, final Direction direction, final ItemStack stack) {
|
| 301 | this.resourcePopper.pop(level, pos, direction, stack);
|
| 302 | }
|
| 303 |
|
| 304 | @FunctionalInterface
|
| 305 | public interface ResourcePopper {
|
| 306 | BlockTransformer.DropStrategy.ResourcePopper FROM_FACE = Block::popResourceFromFace;
|
| 307 | BlockTransformer.DropStrategy.ResourcePopper FROM_MIDDLE = (level, pos, var2, stack) -> Block.popResource(level, pos, stack);
|
| 308 |
|
| 309 | void pop(final Level level, final BlockPos pos, final Direction direction, final ItemStack stack);
|
| 310 | }
|
| 311 | }
|
| 312 |
|
| 313 | public enum TransformParticle implements StringRepresentable {
|
| 314 | NONE(0, "none", 0),
|
| 315 | SCRAPE(1, "scrape", 3005),
|
| 316 | WAX_ON(2, "wax_on", 3003),
|
| 317 | WAX_OFF(3, "wax_off", 3004);
|
| 318 |
|
| 319 | private final int id;
|
| 320 | private final String name;
|
| 321 | private final @LevelEvent.Value int levelEvent;
|
| 322 | public static final Codec<BlockTransformer.TransformParticle> CODEC = StringRepresentable.fromValues(BlockTransformer.TransformParticle::values);
|
| 323 | private static final IntFunction<BlockTransformer.TransformParticle> BY_ID = ByIdMap.continuous(
|
| 324 | BlockTransformer.TransformParticle::getId, values(), ByIdMap.OutOfBoundsStrategy.ZERO
|
| 325 | );
|
| 326 | public static final StreamCodec<ByteBuf, BlockTransformer.TransformParticle> STREAM_CODEC = ByteBufCodecs.idMapper(
|
| 327 | BY_ID, BlockTransformer.TransformParticle::getId
|
| 328 | );
|
| 329 |
|
| 330 | TransformParticle(final int id, final String name, final @LevelEvent.Value int levelEvent) {
|
| 331 | this.id = id;
|
| 332 | this.name = name;
|
| 333 | this.levelEvent = levelEvent;
|
| 334 | }
|
| 335 |
|
| 336 | public void send(final Level level, final @Nullable Entity player, final BlockPos pos) {
|
| 337 | if (!this.equals(NONE)) {
|
| 338 | level.levelEvent(player, this.getLevelEvent(), pos, 0);
|
| 339 | }
|
| 340 | }
|
| 341 |
|
| 342 | @Override
|
| 343 | public String getSerializedName() {
|
| 344 | return this.name;
|
| 345 | }
|
| 346 |
|
| 347 | public int getId() {
|
| 348 | return this.id;
|
| 349 | }
|
| 350 |
|
| 351 | public @LevelEvent.Value int getLevelEvent() {
|
| 352 | return this.levelEvent;
|
| 353 | }
|
| 354 | }
|
| 355 |
|
| 356 | public enum TransformType implements StringRepresentable {
|
| 357 | SINGLE_BLOCK(0, "single_block"),
|
| 358 | COPPER_CHEST(1, "copper_chest");
|
| 359 |
|
| 360 | private final int id;
|
| 361 | private final String name;
|
| 362 | public static final Codec<BlockTransformer.TransformType> CODEC = StringRepresentable.fromValues(BlockTransformer.TransformType::values);
|
| 363 | private static final IntFunction<BlockTransformer.TransformType> BY_ID = ByIdMap.continuous(
|
| 364 | BlockTransformer.TransformType::getId, values(), ByIdMap.OutOfBoundsStrategy.ZERO
|
| 365 | );
|
| 366 | public static final StreamCodec<ByteBuf, BlockTransformer.TransformType> STREAM_CODEC = ByteBufCodecs.idMapper(BY_ID, BlockTransformer.TransformType::getId);
|
| 367 |
|
| 368 | TransformType(final int id, final String name) {
|
| 369 | this.id = id;
|
| 370 | this.name = name;
|
| 371 | }
|
| 372 |
|
| 373 | @Override
|
| 374 | public String getSerializedName() {
|
| 375 | return this.name;
|
| 376 | }
|
| 377 |
|
| 378 | public int getId() {
|
| 379 | return this.id;
|
| 380 | }
|
| 381 | }
|
| 382 | }
|
| 383 |
|