Unicopia/src/main/java/com/minelittlepony/unicopia/block/FancyBedBlock.java

148 lines
4.3 KiB
Java
Raw Normal View History

2023-10-31 21:38:28 +01:00
package com.minelittlepony.unicopia.block;
import java.util.Locale;
import org.jetbrains.annotations.Nullable;
2023-10-31 21:38:28 +01:00
import com.minelittlepony.unicopia.item.BedsheetsItem;
import net.minecraft.block.BedBlock;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
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;
import net.minecraft.util.DyeColor;
import net.minecraft.util.StringIdentifiable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World;
public class FancyBedBlock extends BedBlock {
private final String base;
public FancyBedBlock(String base, Settings settings) {
super(DyeColor.WHITE, settings);
this.base = base;
}
@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();
}
public SheetPattern getPattern() {
return pattern;
}
}
public enum SheetPattern implements StringIdentifiable {
NONE(DyeColor.WHITE),
LIGHT_GRAY(DyeColor.LIGHT_GRAY),
GRAY(DyeColor.GRAY),
BLACK(DyeColor.BLACK),
BROWN(DyeColor.BROWN),
RED(DyeColor.RED),
ORANGE(DyeColor.ORANGE),//
YELLOW(DyeColor.YELLOW),
LIME(DyeColor.LIME),
GREEN(DyeColor.GREEN),
CYAN(DyeColor.CYAN),
LIGHT_BLUE(DyeColor.LIGHT_BLUE),
BLUE(DyeColor.BLUE),//
PURPLE(DyeColor.PURPLE),
MAGENTA(DyeColor.MAGENTA),
PINK(DyeColor.PINK), //
APPLE(null),
BARS(null),
CHECKER(null),
RAINBOW(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);
@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);
}
}
}