minecraft-source/src/net/minecraft/world/level/block/EnchantmentTableBlock.java

120 lines
4.8 KiB
Java

package net.minecraft.world.level.block;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.LivingEntity;
import javax.annotation.Nullable;
import net.minecraft.network.chat.Component;
import net.minecraft.world.SimpleMenuProvider;
import net.minecraft.world.inventory.EnchantmentMenu;
import net.minecraft.world.inventory.ContainerLevelAccess;
import net.minecraft.world.Nameable;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.block.entity.EnchantmentTableBlockEntity;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.ParticleTypes;
import java.util.Random;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.shapes.VoxelShape;
public class EnchantmentTableBlock extends BaseEntityBlock {
protected static final VoxelShape SHAPE;
protected EnchantmentTableBlock(final Properties c) {
super(c);
}
@Override
public boolean useShapeForLightOcclusion(final BlockState byg) {
return true;
}
@Override
public VoxelShape getShape(final BlockState byg, final BlockGetter bjd, final BlockPos fk, final CollisionContext cvn) {
return EnchantmentTableBlock.SHAPE;
}
@Override
public void animateTick(final BlockState byg, final Level bjt, final BlockPos fk, final Random random) {
super.animateTick(byg, bjt, fk, random);
for (int integer6 = -2; integer6 <= 2; ++integer6) {
for (int integer7 = -2; integer7 <= 2; ++integer7) {
if (integer6 > -2 && integer6 < 2 && integer7 == -1) {
integer7 = 2;
}
if (random.nextInt(16) == 0) {
for (int integer8 = 0; integer8 <= 1; ++integer8) {
final BlockPos fk2 = fk.offset(integer6, integer8, integer7);
if (bjt.getBlockState(fk2).getBlock() == Blocks.BOOKSHELF) {
if (!bjt.isEmptyBlock(fk.offset(integer6 / 2, 0, integer7 / 2))) {
break;
}
bjt.addParticle(ParticleTypes.ENCHANT, fk.getX() + 0.5, fk.getY() + 2.0, fk.getZ() + 0.5, integer6 + random.nextFloat() - 0.5, integer8 - random.nextFloat() - 1.0f, integer7 + random.nextFloat() - 0.5);
}
}
}
}
}
}
@Override
public RenderShape getRenderShape(final BlockState byg) {
return RenderShape.MODEL;
}
@Override
public BlockEntity newBlockEntity(final BlockGetter bjd) {
return new EnchantmentTableBlockEntity();
}
@Override
public InteractionResult use(final BlockState byg, final Level bjt, final BlockPos fk, final Player ayg, final InteractionHand ajh, final BlockHitResult cvd) {
if (bjt.isClientSide) {
return InteractionResult.SUCCESS;
}
ayg.openMenu(byg.getMenuProvider(bjt, fk));
return InteractionResult.SUCCESS;
}
@Nullable
@Override
public MenuProvider getMenuProvider(final BlockState byg, final Level bjt, final BlockPos fk) {
final BlockEntity bwi5 = bjt.getBlockEntity(fk);
if (bwi5 instanceof EnchantmentTableBlockEntity) {
final Component lf6 = ((Nameable)bwi5).getDisplayName();
return new SimpleMenuProvider((integer, ayf, ayg) -> new EnchantmentMenu(integer, ayf, ContainerLevelAccess.create(bjt, fk)), lf6);
}
return null;
}
@Override
public void setPlacedBy(final Level bjt, final BlockPos fk, final BlockState byg, final LivingEntity akw, final ItemStack bek) {
if (bek.hasCustomHoverName()) {
final BlockEntity bwi7 = bjt.getBlockEntity(fk);
if (bwi7 instanceof EnchantmentTableBlockEntity) {
((EnchantmentTableBlockEntity)bwi7).setCustomName(bek.getHoverName());
}
}
}
@Override
public boolean isPathfindable(final BlockState byg, final BlockGetter bjd, final BlockPos fk, final PathComputationType cqo) {
return false;
}
static {
SHAPE = Block.box(0.0, 0.0, 0.0, 16.0, 12.0, 16.0);
}
}