| 1 | package net.minecraft.world.item;
|
| 2 |
|
| 3 | import java.util.Map;
|
| 4 | import net.minecraft.advancements.triggers.CriteriaTriggers;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.component.DataComponents;
|
| 7 | import net.minecraft.server.level.ServerPlayer;
|
| 8 | import net.minecraft.server.permissions.Permissions;
|
| 9 | import net.minecraft.sounds.SoundEvent;
|
| 10 | import net.minecraft.sounds.SoundSource;
|
| 11 | import net.minecraft.world.InteractionResult;
|
| 12 | import net.minecraft.world.entity.item.ItemEntity;
|
| 13 | import net.minecraft.world.entity.player.Player;
|
| 14 | import net.minecraft.world.item.component.BlockItemStateProperties;
|
| 15 | import net.minecraft.world.item.component.ItemContainerContents;
|
| 16 | import net.minecraft.world.item.component.TypedEntityData;
|
| 17 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 18 | import net.minecraft.world.item.context.UseOnContext;
|
| 19 | import net.minecraft.world.level.Level;
|
| 20 | import net.minecraft.world.level.block.Block;
|
| 21 | import net.minecraft.world.level.block.ShulkerBoxBlock;
|
| 22 | import net.minecraft.world.level.block.SoundType;
|
| 23 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 24 | import net.minecraft.world.level.block.entity.BlockEntityType;
|
| 25 | import net.minecraft.world.level.block.state.BlockState;
|
| 26 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 27 | import net.minecraft.world.level.storage.TagValueOutput;
|
| 28 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 29 | import org.jspecify.annotations.Nullable;
|
| 30 |
|
| 31 | public class BlockItem extends Item {
|
| 32 | @Deprecated
|
| 33 | private final Block block;
|
| 34 |
|
| 35 | public BlockItem(final Block block, final Item.Properties properties) {
|
| 36 | super(properties);
|
| 37 | this.block = block;
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | public InteractionResult useOn(final UseOnContext context) {
|
| 42 | InteractionResult placeResult = this.place(new BlockPlaceContext(context));
|
| 43 | if (placeResult.consumesAction()) {
|
| 44 | return placeResult;
|
| 45 | } else {
|
| 46 | InteractionResult result = super.useOn(context);
|
| 47 | if (result.consumesAction()) {
|
| 48 | return result;
|
| 49 | } else {
|
| 50 | return context.getItemInHand().has(DataComponents.CONSUMABLE) ? super.use(context.getLevel(), context.getPlayer(), context.getHand()) : placeResult;
|
| 51 | }
|
| 52 | }
|
| 53 | }
|
| 54 |
|
| 55 | public InteractionResult place(final BlockPlaceContext placeContext) {
|
| 56 | if (!this.getBlock().isEnabled(placeContext.getLevel().enabledFeatures())) {
|
| 57 | return InteractionResult.FAIL;
|
| 58 | }
|
| 59 |
|
| 60 | if (!placeContext.canPlace()) {
|
| 61 | return InteractionResult.FAIL;
|
| 62 | }
|
| 63 |
|
| 64 | BlockPlaceContext updatedPlaceContext = this.updatePlacementContext(placeContext);
|
| 65 | if (updatedPlaceContext == null) {
|
| 66 | return InteractionResult.FAIL;
|
| 67 | }
|
| 68 |
|
| 69 | BlockState placementState = this.getPlacementState(updatedPlaceContext);
|
| 70 | if (placementState == null) {
|
| 71 | return InteractionResult.FAIL;
|
| 72 | }
|
| 73 |
|
| 74 | if (!this.placeBlock(updatedPlaceContext, placementState)) {
|
| 75 | return InteractionResult.FAIL;
|
| 76 | }
|
| 77 |
|
| 78 | BlockPos pos = updatedPlaceContext.getClickedPos();
|
| 79 | Level level = updatedPlaceContext.getLevel();
|
| 80 | Player player = updatedPlaceContext.getPlayer();
|
| 81 | ItemStack itemStack = updatedPlaceContext.getItemInHand();
|
| 82 | BlockState placedState = level.getBlockState(pos);
|
| 83 | if (placedState.is(placementState.getBlock())) {
|
| 84 | placedState = updateBlockStateFromTag(pos, level, itemStack, placedState);
|
| 85 | updateCustomBlockEntityTag(level, player, pos, itemStack);
|
| 86 | updateBlockEntityComponents(level, pos, itemStack);
|
| 87 | placedState.getBlock().setPlacedBy(level, pos, placedState, player, itemStack);
|
| 88 | if (player instanceof ServerPlayer serverPlayer) {
|
| 89 | CriteriaTriggers.PLACED_BLOCK.trigger(serverPlayer, pos, itemStack);
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | SoundType soundType = placedState.getSoundType();
|
| 94 | level.playSound(player, pos, this.getPlaceSound(placedState), SoundSource.BLOCKS, (soundType.getVolume() + 1.0F) / 2.0F, soundType.getPitch() * 0.8F);
|
| 95 | level.gameEvent(GameEvent.BLOCK_PLACE, pos, GameEvent.Context.of(player, placedState));
|
| 96 | itemStack.consume(1, player);
|
| 97 | return InteractionResult.SUCCESS;
|
| 98 | }
|
| 99 |
|
| 100 | protected SoundEvent getPlaceSound(final BlockState blockState) {
|
| 101 | return blockState.getSoundType().getPlaceSound();
|
| 102 | }
|
| 103 |
|
| 104 | public @Nullable BlockPlaceContext updatePlacementContext(final BlockPlaceContext context) {
|
| 105 | return context;
|
| 106 | }
|
| 107 |
|
| 108 | private static void updateBlockEntityComponents(final Level level, final BlockPos pos, final ItemStack itemStack) {
|
| 109 | BlockEntity entity = level.getBlockEntity(pos);
|
| 110 | if (entity != null) {
|
| 111 | entity.applyComponentsFromItemStack(itemStack);
|
| 112 | entity.setChanged();
|
| 113 | }
|
| 114 | }
|
| 115 |
|
| 116 | protected @Nullable BlockState getPlacementState(final BlockPlaceContext context) {
|
| 117 | BlockState stateForPlacement = this.getBlock().getStateForPlacement(context);
|
| 118 | return stateForPlacement != null && this.canPlace(context, stateForPlacement) ? stateForPlacement : null;
|
| 119 | }
|
| 120 |
|
| 121 | private static BlockState updateBlockStateFromTag(final BlockPos pos, final Level level, final ItemStack itemStack, final BlockState placedState) {
|
| 122 | BlockItemStateProperties blockState = itemStack.getOrDefault(DataComponents.BLOCK_STATE, BlockItemStateProperties.EMPTY);
|
| 123 | if (blockState.isEmpty()) {
|
| 124 | return placedState;
|
| 125 | }
|
| 126 |
|
| 127 | BlockState modifiedState = blockState.apply(placedState);
|
| 128 | if (modifiedState != placedState) {
|
| 129 | level.setBlock(pos, modifiedState, 2);
|
| 130 | }
|
| 131 |
|
| 132 | return modifiedState;
|
| 133 | }
|
| 134 |
|
| 135 | protected boolean canPlace(final BlockPlaceContext context, final BlockState stateForPlacement) {
|
| 136 | Player player = context.getPlayer();
|
| 137 | return (!this.mustSurvive() || stateForPlacement.canSurvive(context.getLevel(), context.getClickedPos()))
|
| 138 | && context.getLevel().isUnobstructed(stateForPlacement, context.getClickedPos(), CollisionContext.placementContext(player));
|
| 139 | }
|
| 140 |
|
| 141 | protected boolean mustSurvive() {
|
| 142 | return true;
|
| 143 | }
|
| 144 |
|
| 145 | protected boolean placeBlock(final BlockPlaceContext context, final BlockState placementState) {
|
| 146 | return context.getLevel().setBlock(context.getClickedPos(), placementState, 11);
|
| 147 | }
|
| 148 |
|
| 149 | public static boolean updateCustomBlockEntityTag(final Level level, final @Nullable Player player, final BlockPos pos, final ItemStack itemStack) {
|
| 150 | if (level.isClientSide()) {
|
| 151 | return false;
|
| 152 | }
|
| 153 |
|
| 154 | TypedEntityData<BlockEntityType<?>> customData = itemStack.get(DataComponents.BLOCK_ENTITY_DATA);
|
| 155 | if (customData != null) {
|
| 156 | BlockEntity blockEntity = level.getBlockEntity(pos);
|
| 157 | if (blockEntity != null) {
|
| 158 | BlockEntityType<?> type = blockEntity.getType();
|
| 159 | if (type != customData.type()) {
|
| 160 | return false;
|
| 161 | }
|
| 162 |
|
| 163 | if (!type.onlyOpCanSetNbt() || player != null && player.canUseGameMasterBlocks()) {
|
| 164 | return customData.loadInto(blockEntity, level.registryAccess());
|
| 165 | }
|
| 166 |
|
| 167 | return false;
|
| 168 | }
|
| 169 | }
|
| 170 |
|
| 171 | return false;
|
| 172 | }
|
| 173 |
|
| 174 | @Override
|
| 175 | public boolean shouldPrintOpWarning(final ItemStack stack, final @Nullable Player player) {
|
| 176 | if (player != null && player.permissions().hasPermission(Permissions.COMMANDS_GAMEMASTER)) {
|
| 177 | TypedEntityData<BlockEntityType<?>> blockEntityData = stack.get(DataComponents.BLOCK_ENTITY_DATA);
|
| 178 | if (blockEntityData != null) {
|
| 179 | return blockEntityData.type().onlyOpCanSetNbt();
|
| 180 | }
|
| 181 | }
|
| 182 |
|
| 183 | return false;
|
| 184 | }
|
| 185 |
|
| 186 | public Block getBlock() {
|
| 187 | return this.block;
|
| 188 | }
|
| 189 |
|
| 190 | public void registerBlocks(final Map<Block, Item> map, final Item item) {
|
| 191 | map.put(this.getBlock(), item);
|
| 192 | }
|
| 193 |
|
| 194 | @Override
|
| 195 | public boolean canFitInsideContainerItems() {
|
| 196 | return !(this.getBlock() instanceof ShulkerBoxBlock);
|
| 197 | }
|
| 198 |
|
| 199 | @Override
|
| 200 | public void onDestroyed(final ItemEntity entity) {
|
| 201 | ItemContainerContents container = entity.getItem().set(DataComponents.CONTAINER, ItemContainerContents.EMPTY);
|
| 202 | if (container != null) {
|
| 203 | ItemUtils.onContainerDestroyed(entity, container.nonEmptyItemCopyStream());
|
| 204 | }
|
| 205 | }
|
| 206 |
|
| 207 | public static void setBlockEntityData(final ItemStack stack, final BlockEntityType<?> type, final TagValueOutput output) {
|
| 208 | output.discard("id");
|
| 209 | if (output.isEmpty()) {
|
| 210 | stack.remove(DataComponents.BLOCK_ENTITY_DATA);
|
| 211 | } else {
|
| 212 | BlockEntity.addEntityType(output, type);
|
| 213 | stack.set(DataComponents.BLOCK_ENTITY_DATA, TypedEntityData.of(type, output.buildResult()));
|
| 214 | }
|
| 215 | }
|
| 216 | }
|
| 217 |
|