minecraft-source/src/net/minecraft/world/item/BlockItem.java

196 lines
7.7 KiB
Java

package net.minecraft.world.item;
import net.minecraft.world.level.block.state.AbstractStateHolder;
import java.util.Map;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.network.chat.Component;
import java.util.List;
import net.minecraft.core.NonNullList;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.level.block.state.properties.Property;
import java.util.Iterator;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.nbt.CompoundTag;
import javax.annotation.Nullable;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.sounds.SoundSource;
import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.level.block.Block;
public class BlockItem extends Item {
@Deprecated
private final Block block;
public BlockItem(final Block bpe, final Properties a) {
super(a);
this.block = bpe;
}
@Override
public InteractionResult useOn(final UseOnContext bfw) {
final InteractionResult aji3 = this.place(new BlockPlaceContext(bfw));
if (aji3 != InteractionResult.SUCCESS && this.isEdible()) {
return this.use(bfw.level, bfw.player, bfw.hand).getResult();
}
return aji3;
}
public InteractionResult place(final BlockPlaceContext bcn) {
if (!bcn.canPlace()) {
return InteractionResult.FAIL;
}
final BlockPlaceContext bcn2 = this.updatePlacementContext(bcn);
if (bcn2 == null) {
return InteractionResult.FAIL;
}
final BlockState byg4 = this.getPlacementState(bcn2);
if (byg4 == null) {
return InteractionResult.FAIL;
}
if (!this.placeBlock(bcn2, byg4)) {
return InteractionResult.FAIL;
}
final BlockPos fk5 = bcn2.getClickedPos();
final Level bjt6 = bcn2.getLevel();
final Player ayg7 = bcn2.getPlayer();
final ItemStack bek8 = bcn2.getItemInHand();
BlockState byg5 = bjt6.getBlockState(fk5);
final Block bpe10 = byg5.getBlock();
if (bpe10 == byg4.getBlock()) {
byg5 = this.updateBlockStateFromTag(fk5, bjt6, bek8, byg5);
this.updateCustomBlockEntityTag(fk5, bjt6, ayg7, bek8, byg5);
bpe10.setPlacedBy(bjt6, fk5, byg5, ayg7, bek8);
if (ayg7 instanceof ServerPlayer) {
CriteriaTriggers.PLACED_BLOCK.trigger((ServerPlayer)ayg7, fk5, bek8);
}
}
final SoundType buj11 = byg5.getSoundType();
bjt6.playSound(ayg7, fk5, this.getPlaceSound(byg5), SoundSource.BLOCKS, (buj11.getVolume() + 1.0f) / 2.0f, buj11.getPitch() * 0.8f);
bek8.shrink(1);
return InteractionResult.SUCCESS;
}
protected SoundEvent getPlaceSound(final BlockState byg) {
return byg.getSoundType().getPlaceSound();
}
@Nullable
public BlockPlaceContext updatePlacementContext(final BlockPlaceContext bcn) {
return bcn;
}
protected boolean updateCustomBlockEntityTag(final BlockPos fk, final Level bjt, @Nullable final Player ayg, final ItemStack bek, final BlockState byg) {
return updateCustomBlockEntityTag(bjt, ayg, fk, bek);
}
@Nullable
protected BlockState getPlacementState(final BlockPlaceContext bcn) {
final BlockState byg3 = this.getBlock().getStateForPlacement(bcn);
return (byg3 != null && this.canPlace(bcn, byg3)) ? byg3 : null;
}
private BlockState updateBlockStateFromTag(final BlockPos fk, final Level bjt, final ItemStack bek, final BlockState byg) {
BlockState byg2 = byg;
final CompoundTag jt7 = bek.getTag();
if (jt7 != null) {
final CompoundTag jt8 = jt7.getCompound("BlockStateTag");
final StateDefinition<Block, BlockState> byh9 = byg2.getBlock().getStateDefinition();
for (final String string11 : jt8.getAllKeys()) {
final Property<?> bzj12 = byh9.getProperty(string11);
if (bzj12 != null) {
final String string12 = jt8.get(string11).getAsString();
byg2 = BlockItem.updateState(byg2, bzj12, string12);
}
}
}
if (byg2 != byg) {
bjt.setBlock(fk, byg2, 2);
}
return byg2;
}
private static <T extends Comparable<T>> BlockState updateState(final BlockState byg, final Property<T> bzj, final String string) {
return bzj.getValue(string).<BlockState>map(comparable -> (BlockState)((AbstractStateHolder<Object, Object>)byg).<T, Comparable>setValue(bzj, comparable)).orElse(byg);
}
protected boolean canPlace(final BlockPlaceContext bcn, final BlockState byg) {
final Player ayg4 = bcn.getPlayer();
final CollisionContext cvn5 = (ayg4 == null) ? CollisionContext.empty() : CollisionContext.of(ayg4);
return (!this.mustSurvive() || byg.canSurvive(bcn.getLevel(), bcn.getClickedPos())) && bcn.getLevel().isUnobstructed(byg, bcn.getClickedPos(), cvn5);
}
protected boolean mustSurvive() {
return true;
}
protected boolean placeBlock(final BlockPlaceContext bcn, final BlockState byg) {
return bcn.getLevel().setBlock(bcn.getClickedPos(), byg, 11);
}
public static boolean updateCustomBlockEntityTag(final Level bjt, @Nullable final Player ayg, final BlockPos fk, final ItemStack bek) {
final MinecraftServer minecraftServer5 = bjt.getServer();
if (minecraftServer5 == null) {
return false;
}
final CompoundTag jt6 = bek.getTagElement("BlockEntityTag");
if (jt6 != null) {
final BlockEntity bwi7 = bjt.getBlockEntity(fk);
if (bwi7 != null) {
if (!bjt.isClientSide && bwi7.onlyOpCanSetNbt() && (ayg == null || !ayg.canUseGameMasterBlocks())) {
return false;
}
final CompoundTag jt7 = bwi7.save(new CompoundTag());
final CompoundTag jt8 = jt7.copy();
jt7.merge(jt6);
jt7.putInt("x", fk.getX());
jt7.putInt("y", fk.getY());
jt7.putInt("z", fk.getZ());
if (!jt7.equals(jt8)) {
bwi7.load(jt7);
bwi7.setChanged();
return true;
}
}
}
return false;
}
@Override
public String getDescriptionId() {
return this.getBlock().getDescriptionId();
}
@Override
public void fillItemCategory(final CreativeModeTab bda, final NonNullList<ItemStack> fy) {
if (this.allowdedIn(bda)) {
this.getBlock().fillItemCategory(bda, fy);
}
}
@Override
public void appendHoverText(final ItemStack bek, @Nullable final Level bjt, final List<Component> list, final TooltipFlag bft) {
super.appendHoverText(bek, bjt, list, bft);
this.getBlock().appendHoverText(bek, bjt, list, bft);
}
public Block getBlock() {
return this.block;
}
public void registerBlocks(final Map<Block, Item> map, final Item bef) {
map.put(this.getBlock(), bef);
}
}