342 lines
13 KiB
Java
342 lines
13 KiB
Java
package net.minecraft.world.level.block;
|
|
|
|
import net.minecraft.world.level.block.state.AbstractStateHolder;
|
|
import java.util.function.Consumer;
|
|
import net.minecraft.world.level.ItemLike;
|
|
import net.minecraft.world.item.Item;
|
|
import net.minecraft.world.inventory.AbstractContainerMenu;
|
|
import net.minecraft.world.Container;
|
|
import net.minecraft.world.phys.shapes.VoxelShape;
|
|
import net.minecraft.world.phys.shapes.CollisionContext;
|
|
import net.minecraft.world.level.material.PushReaction;
|
|
import java.util.Iterator;
|
|
import net.minecraft.ChatFormatting;
|
|
import net.minecraft.network.chat.TranslatableComponent;
|
|
import net.minecraft.world.ContainerHelper;
|
|
import net.minecraft.core.NonNullList;
|
|
import net.minecraft.network.chat.TextComponent;
|
|
import net.minecraft.world.item.TooltipFlag;
|
|
import net.minecraft.network.chat.Component;
|
|
import net.minecraft.world.entity.LivingEntity;
|
|
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
|
|
import java.util.List;
|
|
import net.minecraft.world.level.storage.loot.LootContext;
|
|
import net.minecraft.world.item.ItemStack;
|
|
import net.minecraft.world.entity.Entity;
|
|
import net.minecraft.world.entity.item.ItemEntity;
|
|
import net.minecraft.nbt.Tag;
|
|
import net.minecraft.nbt.CompoundTag;
|
|
import net.minecraft.world.level.block.state.StateDefinition;
|
|
import net.minecraft.world.item.BlockPlaceContext;
|
|
import net.minecraft.world.phys.AABB;
|
|
import net.minecraft.stats.Stats;
|
|
import net.minecraft.world.MenuProvider;
|
|
import net.minecraft.world.phys.shapes.Shapes;
|
|
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.Level;
|
|
import net.minecraft.core.BlockPos;
|
|
import net.minecraft.world.level.block.entity.ShulkerBoxBlockEntity;
|
|
import net.minecraft.world.level.block.entity.BlockEntity;
|
|
import net.minecraft.world.level.BlockGetter;
|
|
import net.minecraft.world.level.block.state.properties.Property;
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
import javax.annotation.Nullable;
|
|
import net.minecraft.world.item.DyeColor;
|
|
import net.minecraft.resources.ResourceLocation;
|
|
import net.minecraft.core.Direction;
|
|
import net.minecraft.world.level.block.state.properties.EnumProperty;
|
|
|
|
public class ShulkerBoxBlock extends BaseEntityBlock {
|
|
public static final EnumProperty<Direction> FACING;
|
|
public static final ResourceLocation CONTENTS;
|
|
@Nullable
|
|
private final DyeColor color;
|
|
|
|
public ShulkerBoxBlock(@Nullable final DyeColor bdg, final Properties c) {
|
|
super(c);
|
|
this.color = bdg;
|
|
this.registerDefaultState(((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).<Direction, Direction>setValue(ShulkerBoxBlock.FACING, Direction.UP));
|
|
}
|
|
|
|
@Override
|
|
public BlockEntity newBlockEntity(final BlockGetter bjd) {
|
|
return new ShulkerBoxBlockEntity(this.color);
|
|
}
|
|
|
|
@Override
|
|
public boolean isSuffocating(final BlockState byg, final BlockGetter bjd, final BlockPos fk) {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public RenderShape getRenderShape(final BlockState byg) {
|
|
return RenderShape.ENTITYBLOCK_ANIMATED;
|
|
}
|
|
|
|
@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;
|
|
}
|
|
if (ayg.isSpectator()) {
|
|
return InteractionResult.SUCCESS;
|
|
}
|
|
final BlockEntity bwi8 = bjt.getBlockEntity(fk);
|
|
if (bwi8 instanceof ShulkerBoxBlockEntity) {
|
|
final Direction fp9 = byg.<Direction>getValue(ShulkerBoxBlock.FACING);
|
|
final ShulkerBoxBlockEntity bxd11 = (ShulkerBoxBlockEntity)bwi8;
|
|
boolean boolean10;
|
|
if (bxd11.getAnimationStatus() == ShulkerBoxBlockEntity.AnimationStatus.CLOSED) {
|
|
final AABB cvc12 = Shapes.block().bounds().expandTowards(0.5f * fp9.getStepX(), 0.5f * fp9.getStepY(), 0.5f * fp9.getStepZ()).contract(fp9.getStepX(), fp9.getStepY(), fp9.getStepZ());
|
|
boolean10 = bjt.noCollision(cvc12.move(fk.relative(fp9)));
|
|
}
|
|
else {
|
|
boolean10 = true;
|
|
}
|
|
if (boolean10) {
|
|
ayg.openMenu(bxd11);
|
|
ayg.awardStat(Stats.OPEN_SHULKER_BOX);
|
|
}
|
|
return InteractionResult.SUCCESS;
|
|
}
|
|
return InteractionResult.PASS;
|
|
}
|
|
|
|
@Override
|
|
public BlockState getStateForPlacement(final BlockPlaceContext bcn) {
|
|
return ((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).<Direction, Direction>setValue(ShulkerBoxBlock.FACING, bcn.getClickedFace());
|
|
}
|
|
|
|
@Override
|
|
protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
|
|
a.add(ShulkerBoxBlock.FACING);
|
|
}
|
|
|
|
@Override
|
|
public void playerWillDestroy(final Level bjt, final BlockPos fk, final BlockState byg, final Player ayg) {
|
|
final BlockEntity bwi6 = bjt.getBlockEntity(fk);
|
|
if (bwi6 instanceof ShulkerBoxBlockEntity) {
|
|
final ShulkerBoxBlockEntity bxd7 = (ShulkerBoxBlockEntity)bwi6;
|
|
if (!bjt.isClientSide && ayg.isCreative() && !bxd7.isEmpty()) {
|
|
final ItemStack bek8 = getColoredItemStack(this.getColor());
|
|
final CompoundTag jt9 = bxd7.saveToTag(new CompoundTag());
|
|
if (!jt9.isEmpty()) {
|
|
bek8.addTagElement("BlockEntityTag", jt9);
|
|
}
|
|
if (bxd7.hasCustomName()) {
|
|
bek8.setHoverName(bxd7.getCustomName());
|
|
}
|
|
final ItemEntity avy10 = new ItemEntity(bjt, fk.getX(), fk.getY(), fk.getZ(), bek8);
|
|
avy10.setDefaultPickUpDelay();
|
|
bjt.addFreshEntity(avy10);
|
|
}
|
|
else {
|
|
bxd7.unpackLootTable(ayg);
|
|
}
|
|
}
|
|
super.playerWillDestroy(bjt, fk, byg, ayg);
|
|
}
|
|
|
|
@Override
|
|
public List<ItemStack> getDrops(final BlockState byg, LootContext.Builder a) {
|
|
final BlockEntity bwi4 = a.<BlockEntity>getOptionalParameter(LootContextParams.BLOCK_ENTITY);
|
|
if (bwi4 instanceof ShulkerBoxBlockEntity) {
|
|
final ShulkerBoxBlockEntity bxd5 = (ShulkerBoxBlockEntity)bwi4;
|
|
int integer4;
|
|
final ShulkerBoxBlockEntity shulkerBoxBlockEntity;
|
|
a = a.withDynamicDrop(ShulkerBoxBlock.CONTENTS, (cru, consumer) -> {
|
|
for (integer4 = 0; integer4 < shulkerBoxBlockEntity.getContainerSize(); ++integer4) {
|
|
consumer.accept(shulkerBoxBlockEntity.getItem(integer4));
|
|
}
|
|
return;
|
|
});
|
|
}
|
|
return super.getDrops(byg, a);
|
|
}
|
|
|
|
@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 ShulkerBoxBlockEntity) {
|
|
((ShulkerBoxBlockEntity)bwi7).setCustomName(bek.getHoverName());
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onRemove(final BlockState byg1, final Level bjt, final BlockPos fk, final BlockState byg4, final boolean boolean5) {
|
|
if (byg1.getBlock() == byg4.getBlock()) {
|
|
return;
|
|
}
|
|
final BlockEntity bwi7 = bjt.getBlockEntity(fk);
|
|
if (bwi7 instanceof ShulkerBoxBlockEntity) {
|
|
bjt.updateNeighbourForOutputSignal(fk, byg1.getBlock());
|
|
}
|
|
super.onRemove(byg1, bjt, fk, byg4, boolean5);
|
|
}
|
|
|
|
@Override
|
|
public void appendHoverText(final ItemStack bek, @Nullable final BlockGetter bjd, final List<Component> list, final TooltipFlag bft) {
|
|
super.appendHoverText(bek, bjd, list, bft);
|
|
final CompoundTag jt6 = bek.getTagElement("BlockEntityTag");
|
|
if (jt6 != null) {
|
|
if (jt6.contains("LootTable", 8)) {
|
|
list.add(new TextComponent("???????"));
|
|
}
|
|
if (jt6.contains("Items", 9)) {
|
|
final NonNullList<ItemStack> fy7 = NonNullList.<ItemStack>withSize(27, ItemStack.EMPTY);
|
|
ContainerHelper.loadAllItems(jt6, fy7);
|
|
int integer8 = 0;
|
|
int integer9 = 0;
|
|
for (final ItemStack bek2 : fy7) {
|
|
if (!bek2.isEmpty()) {
|
|
++integer9;
|
|
if (integer8 > 4) {
|
|
continue;
|
|
}
|
|
++integer8;
|
|
final Component lf12 = bek2.getHoverName().deepCopy();
|
|
lf12.append(" x").append(String.valueOf(bek2.getCount()));
|
|
list.add(lf12);
|
|
}
|
|
}
|
|
if (integer9 - integer8 > 0) {
|
|
list.add(new TranslatableComponent("container.shulkerBox.more", new Object[] { integer9 - integer8 }).withStyle(ChatFormatting.ITALIC));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public PushReaction getPistonPushReaction(final BlockState byg) {
|
|
return PushReaction.DESTROY;
|
|
}
|
|
|
|
@Override
|
|
public VoxelShape getShape(final BlockState byg, final BlockGetter bjd, final BlockPos fk, final CollisionContext cvn) {
|
|
final BlockEntity bwi6 = bjd.getBlockEntity(fk);
|
|
if (bwi6 instanceof ShulkerBoxBlockEntity) {
|
|
return Shapes.create(((ShulkerBoxBlockEntity)bwi6).getBoundingBox(byg));
|
|
}
|
|
return Shapes.block();
|
|
}
|
|
|
|
@Override
|
|
public boolean hasAnalogOutputSignal(final BlockState byg) {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public int getAnalogOutputSignal(final BlockState byg, final Level bjt, final BlockPos fk) {
|
|
return AbstractContainerMenu.getRedstoneSignalFromContainer((Container)bjt.getBlockEntity(fk));
|
|
}
|
|
|
|
@Override
|
|
public ItemStack getCloneItemStack(final BlockGetter bjd, final BlockPos fk, final BlockState byg) {
|
|
final ItemStack bek5 = super.getCloneItemStack(bjd, fk, byg);
|
|
final ShulkerBoxBlockEntity bxd6 = (ShulkerBoxBlockEntity)bjd.getBlockEntity(fk);
|
|
final CompoundTag jt7 = bxd6.saveToTag(new CompoundTag());
|
|
if (!jt7.isEmpty()) {
|
|
bek5.addTagElement("BlockEntityTag", jt7);
|
|
}
|
|
return bek5;
|
|
}
|
|
|
|
@Nullable
|
|
public static DyeColor getColorFromItem(final Item bef) {
|
|
return getColorFromBlock(Block.byItem(bef));
|
|
}
|
|
|
|
@Nullable
|
|
public static DyeColor getColorFromBlock(final Block bpe) {
|
|
if (bpe instanceof ShulkerBoxBlock) {
|
|
return ((ShulkerBoxBlock)bpe).getColor();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static Block getBlockByColor(@Nullable final DyeColor bdg) {
|
|
if (bdg == null) {
|
|
return Blocks.SHULKER_BOX;
|
|
}
|
|
switch (bdg) {
|
|
case WHITE: {
|
|
return Blocks.WHITE_SHULKER_BOX;
|
|
}
|
|
case ORANGE: {
|
|
return Blocks.ORANGE_SHULKER_BOX;
|
|
}
|
|
case MAGENTA: {
|
|
return Blocks.MAGENTA_SHULKER_BOX;
|
|
}
|
|
case LIGHT_BLUE: {
|
|
return Blocks.LIGHT_BLUE_SHULKER_BOX;
|
|
}
|
|
case YELLOW: {
|
|
return Blocks.YELLOW_SHULKER_BOX;
|
|
}
|
|
case LIME: {
|
|
return Blocks.LIME_SHULKER_BOX;
|
|
}
|
|
case PINK: {
|
|
return Blocks.PINK_SHULKER_BOX;
|
|
}
|
|
case GRAY: {
|
|
return Blocks.GRAY_SHULKER_BOX;
|
|
}
|
|
case LIGHT_GRAY: {
|
|
return Blocks.LIGHT_GRAY_SHULKER_BOX;
|
|
}
|
|
case CYAN: {
|
|
return Blocks.CYAN_SHULKER_BOX;
|
|
}
|
|
default: {
|
|
return Blocks.PURPLE_SHULKER_BOX;
|
|
}
|
|
case BLUE: {
|
|
return Blocks.BLUE_SHULKER_BOX;
|
|
}
|
|
case BROWN: {
|
|
return Blocks.BROWN_SHULKER_BOX;
|
|
}
|
|
case GREEN: {
|
|
return Blocks.GREEN_SHULKER_BOX;
|
|
}
|
|
case RED: {
|
|
return Blocks.RED_SHULKER_BOX;
|
|
}
|
|
case BLACK: {
|
|
return Blocks.BLACK_SHULKER_BOX;
|
|
}
|
|
}
|
|
}
|
|
|
|
@Nullable
|
|
public DyeColor getColor() {
|
|
return this.color;
|
|
}
|
|
|
|
public static ItemStack getColoredItemStack(@Nullable final DyeColor bdg) {
|
|
return new ItemStack(getBlockByColor(bdg));
|
|
}
|
|
|
|
@Override
|
|
public BlockState rotate(final BlockState byg, final Rotation btr) {
|
|
return ((AbstractStateHolder<O, BlockState>)byg).<Direction, Direction>setValue(ShulkerBoxBlock.FACING, btr.rotate(byg.<Direction>getValue(ShulkerBoxBlock.FACING)));
|
|
}
|
|
|
|
@Override
|
|
public BlockState mirror(final BlockState byg, final Mirror bsr) {
|
|
return byg.rotate(bsr.getRotation(byg.<Direction>getValue(ShulkerBoxBlock.FACING)));
|
|
}
|
|
|
|
static {
|
|
FACING = DirectionalBlock.FACING;
|
|
CONTENTS = new ResourceLocation("contents");
|
|
}
|
|
}
|