2023-10-31 21:38:28 +01:00
|
|
|
package com.minelittlepony.unicopia.block;
|
|
|
|
|
2023-11-25 03:15:29 +01:00
|
|
|
import java.util.List;
|
2023-10-31 21:38:28 +01:00
|
|
|
import java.util.Locale;
|
2023-11-25 03:15:29 +01:00
|
|
|
import java.util.function.Function;
|
2023-10-31 21:38:28 +01:00
|
|
|
|
2023-11-02 19:24:35 +01:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
|
2023-10-31 21:38:28 +01:00
|
|
|
import com.minelittlepony.unicopia.item.BedsheetsItem;
|
2023-11-25 03:15:29 +01:00
|
|
|
import com.minelittlepony.unicopia.util.VoxelShapeUtil;
|
2023-10-31 21:38:28 +01:00
|
|
|
|
|
|
|
import net.minecraft.block.BedBlock;
|
|
|
|
import net.minecraft.block.Block;
|
|
|
|
import net.minecraft.block.BlockState;
|
2023-11-25 03:15:29 +01:00
|
|
|
import net.minecraft.block.ShapeContext;
|
2023-10-31 21:38:28 +01:00
|
|
|
import net.minecraft.block.entity.BedBlockEntity;
|
|
|
|
import net.minecraft.block.entity.BlockEntity;
|
|
|
|
import net.minecraft.block.entity.BlockEntityType;
|
|
|
|
import net.minecraft.block.enums.BedPart;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NbtCompound;
|
2023-11-25 03:37:10 +01:00
|
|
|
import net.minecraft.server.world.ServerWorld;
|
2023-10-31 21:38:28 +01:00
|
|
|
import net.minecraft.util.DyeColor;
|
|
|
|
import net.minecraft.util.StringIdentifiable;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraft.util.math.Direction;
|
2023-11-25 03:15:29 +01:00
|
|
|
import net.minecraft.util.shape.VoxelShape;
|
|
|
|
import net.minecraft.util.shape.VoxelShapes;
|
|
|
|
import net.minecraft.world.BlockView;
|
2023-10-31 21:38:28 +01:00
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
|
|
|
public class FancyBedBlock extends BedBlock {
|
2023-11-25 03:15:29 +01:00
|
|
|
private static final List<Function<Direction, VoxelShape>> SHAPES = List.of(
|
|
|
|
VoxelShapeUtil.rotator(VoxelShapes.union(
|
|
|
|
createCuboidShape(0, 3, 1, 16, 9, 16),
|
|
|
|
createCuboidShape(-0.5, 0, 1, 1.5, 13, 4),
|
|
|
|
createCuboidShape(14.5, 0, 1, 16.5, 13, 4),
|
|
|
|
createCuboidShape(1.5, 1, 0, 14.5, 16, 3)
|
|
|
|
)),
|
|
|
|
VoxelShapeUtil.rotator(VoxelShapes.union(
|
|
|
|
createCuboidShape(0, 3, 0, 16, 9, 16),
|
|
|
|
createCuboidShape(-0.5, 0, -1, 2.5, 10, 2),
|
|
|
|
createCuboidShape(13.5, 0, -1, 16.5, 10, 2),
|
|
|
|
createCuboidShape(1.5, 1, -2, 14.5, 12, 1)
|
|
|
|
))
|
|
|
|
);
|
2023-10-31 21:38:28 +01:00
|
|
|
|
|
|
|
private final String base;
|
|
|
|
|
|
|
|
public FancyBedBlock(String base, Settings settings) {
|
|
|
|
super(DyeColor.WHITE, settings);
|
|
|
|
this.base = base;
|
|
|
|
}
|
|
|
|
|
2023-11-25 03:15:29 +01:00
|
|
|
@Override
|
|
|
|
public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
|
|
|
|
return SHAPES.get(state.get(PART).ordinal()).apply(BedBlock.getOppositePartDirection(state));
|
|
|
|
}
|
|
|
|
|
2023-10-31 21:38:28 +01:00
|
|
|
@Override
|
|
|
|
public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
|
|
|
|
return new Tile(pos, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setBedPattern(World world, BlockPos pos, SheetPattern pattern) {
|
|
|
|
world.getBlockEntity(pos, UBlockEntities.FANCY_BED).ifPresent(tile -> {
|
|
|
|
ItemStack stack = BedsheetsItem.forPattern(tile.getPattern()).getDefaultStack();
|
|
|
|
if (!stack.isEmpty()) {
|
|
|
|
Block.dropStack(world, pos, stack);
|
|
|
|
}
|
|
|
|
tile.setPattern(pattern);
|
|
|
|
BlockState state = tile.getCachedState();
|
|
|
|
BlockPos other = pos.offset(getDirectionTowardsOtherPart(state.get(PART), state.get(FACING)));
|
|
|
|
world.getBlockEntity(other, UBlockEntities.FANCY_BED).ifPresent(tile2 -> {
|
|
|
|
tile2.setPattern(pattern);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Direction getDirectionTowardsOtherPart(BedPart part, Direction direction) {
|
|
|
|
return part == BedPart.FOOT ? direction : direction.getOpposite();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class Tile extends BedBlockEntity {
|
|
|
|
private SheetPattern pattern = SheetPattern.NONE;
|
|
|
|
|
|
|
|
public Tile(BlockPos pos, BlockState state) {
|
|
|
|
super(pos, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BlockEntityType<?> getType() {
|
|
|
|
return UBlockEntities.FANCY_BED;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readNbt(NbtCompound nbt) {
|
|
|
|
pattern = SheetPattern.byId(nbt.getString("pattern"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void writeNbt(NbtCompound nbt) {
|
|
|
|
nbt.putString("pattern", pattern.asString());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public NbtCompound toInitialChunkDataNbt() {
|
|
|
|
return createNbt();
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getBase() {
|
|
|
|
return ((FancyBedBlock)getCachedState().getBlock()).base;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPattern(SheetPattern pattern) {
|
|
|
|
this.pattern = pattern;
|
|
|
|
markDirty();
|
2023-11-25 03:37:10 +01:00
|
|
|
if (world instanceof ServerWorld sw) {
|
|
|
|
sw.getChunkManager().markForUpdate(getPos());
|
|
|
|
}
|
2023-10-31 21:38:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public SheetPattern getPattern() {
|
|
|
|
return pattern;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public enum SheetPattern implements StringIdentifiable {
|
2023-11-02 19:24:35 +01:00
|
|
|
NONE(DyeColor.WHITE),
|
|
|
|
LIGHT_GRAY(DyeColor.LIGHT_GRAY),
|
|
|
|
GRAY(DyeColor.GRAY),
|
|
|
|
BLACK(DyeColor.BLACK),
|
|
|
|
BROWN(DyeColor.BROWN),
|
|
|
|
RED(DyeColor.RED),
|
2023-11-11 01:34:06 +01:00
|
|
|
ORANGE(DyeColor.ORANGE),
|
2023-11-02 19:24:35 +01:00
|
|
|
YELLOW(DyeColor.YELLOW),
|
|
|
|
LIME(DyeColor.LIME),
|
|
|
|
GREEN(DyeColor.GREEN),
|
|
|
|
CYAN(DyeColor.CYAN),
|
|
|
|
LIGHT_BLUE(DyeColor.LIGHT_BLUE),
|
2023-11-11 01:34:06 +01:00
|
|
|
BLUE(DyeColor.BLUE),
|
2023-11-02 19:24:35 +01:00
|
|
|
PURPLE(DyeColor.PURPLE),
|
|
|
|
MAGENTA(DyeColor.MAGENTA),
|
2023-11-11 01:34:06 +01:00
|
|
|
PINK(DyeColor.PINK),
|
2023-11-02 19:24:35 +01:00
|
|
|
|
|
|
|
APPLE(null),
|
|
|
|
BARS(null),
|
|
|
|
CHECKER(null),
|
2023-11-11 01:34:06 +01:00
|
|
|
KELP(null),
|
|
|
|
RAINBOW(null),
|
|
|
|
RAINBOW_BPW(null),
|
|
|
|
RAINBOW_BPY(null),
|
|
|
|
RAINBOW_PBG(null),
|
|
|
|
RAINBOW_PWR(null);
|
2023-10-31 21:38:28 +01:00
|
|
|
|
|
|
|
@SuppressWarnings("deprecation")
|
|
|
|
public static final Codec<SheetPattern> CODEC = StringIdentifiable.createCodec(SheetPattern::values);
|
|
|
|
|
|
|
|
private final String name = name().toLowerCase(Locale.ROOT);
|
2023-11-02 19:24:35 +01:00
|
|
|
@Nullable
|
|
|
|
private final DyeColor color;
|
|
|
|
|
|
|
|
SheetPattern(@Nullable DyeColor color) {
|
|
|
|
this.color = color;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
public DyeColor getColor() {
|
|
|
|
return color;
|
|
|
|
}
|
2023-10-31 21:38:28 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String asString() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("deprecation")
|
|
|
|
public static SheetPattern byId(String id) {
|
|
|
|
return CODEC.byId(id, NONE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|