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

240 lines
8.6 KiB
Java
Raw Normal View History

2023-10-31 21:38:28 +01:00
package com.minelittlepony.unicopia.block;
import java.util.List;
2023-10-31 21:38:28 +01:00
import java.util.Locale;
import java.util.function.Function;
2023-10-31 21:38:28 +01:00
import org.jetbrains.annotations.Nullable;
2023-10-31 21:38:28 +01:00
import com.minelittlepony.unicopia.item.BedsheetsItem;
import com.minelittlepony.unicopia.util.VoxelShapeUtil;
2023-12-30 11:55:26 +01:00
import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
2023-10-31 21:38:28 +01:00
import net.minecraft.block.BedBlock;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
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;
2024-03-22 21:34:27 +01:00
import net.minecraft.entity.mob.PiglinBrain;
import net.minecraft.entity.player.PlayerEntity;
2023-10-31 21:38:28 +01:00
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
2024-09-26 22:36:35 +02:00
import net.minecraft.registry.RegistryWrapper.WrapperLookup;
2024-03-22 21:34:27 +01:00
import net.minecraft.registry.tag.BlockTags;
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;
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;
2024-03-22 21:34:27 +01:00
import net.minecraft.world.event.GameEvent;
2023-10-31 21:38:28 +01:00
public class FancyBedBlock extends BedBlock {
2023-12-30 11:55:26 +01:00
public static final MapCodec<FancyBedBlock> CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
Codec.STRING.fieldOf("base").forGetter(b -> b.base),
BedBlock.createSettingsCodec()
).apply(instance, FancyBedBlock::new));
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
protected final String base;
2023-10-31 21:38:28 +01:00
public FancyBedBlock(String base, Settings settings) {
super(DyeColor.WHITE, settings);
this.base = base;
}
2023-12-30 11:55:26 +01:00
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public MapCodec<BedBlock> getCodec() {
return (MapCodec)CODEC;
}
@Override
2024-10-03 19:10:03 +02:00
protected VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
return SHAPES.get(state.get(PART).ordinal()).apply(BedBlock.getOppositePartDirection(state));
}
@Override
2024-10-03 19:10:03 +02:00
protected void onStateReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean moved) {
if (state.hasBlockEntity() && !state.isOf(newState.getBlock()) && state.get(PART) == BedPart.HEAD) {
world.getBlockEntity(pos, UBlockEntities.FANCY_BED).ifPresent(tile -> {
SheetPattern pattern = tile.getPattern();
if (pattern != SheetPattern.NONE) {
dropStack(world, pos, BedsheetsItem.forPattern(pattern).getDefaultStack());
}
});
}
super.onStateReplaced(state, world, pos, newState, moved);
}
2024-03-22 21:34:27 +01:00
@Override
public BlockState onBreak(World world, BlockPos pos, BlockState state, PlayerEntity player) {
2024-03-22 21:34:27 +01:00
// MC-269785
BedPart part = state.get(PART);
BlockPos otherHalfPos = pos.offset(getDirectionTowardsOtherPart(part, state.get(FACING)));
BlockState otherHalfState = world.getBlockState(otherHalfPos);
if (/*!world.isClient &&*/ player.isCreative() && part == BedPart.FOOT && otherHalfState.isOf(this) && otherHalfState.get(PART) == BedPart.HEAD) {
if (!world.isClient) {
world.setBlockState(otherHalfPos, otherHalfState.getFluidState().getBlockState(), Block.NOTIFY_ALL | Block.SKIP_DROPS);
}
spawnBreakParticles(world, player, otherHalfPos, otherHalfState);
if (state.isIn(BlockTags.GUARDED_BY_PIGLINS)) {
PiglinBrain.onGuardedBlockInteracted(player, false);
}
world.emitGameEvent(GameEvent.BLOCK_DESTROY, pos, GameEvent.Emitter.of(player, state));
} else {
spawnBreakParticles(world, player, pos, state);
if (state.isIn(BlockTags.GUARDED_BY_PIGLINS)) {
PiglinBrain.onGuardedBlockInteracted(player, false);
}
world.emitGameEvent(GameEvent.BLOCK_DESTROY, pos, GameEvent.Emitter.of(player, state));
}
return state;
2024-03-22 21:34:27 +01:00
}
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
2024-09-26 22:36:35 +02:00
public void readNbt(NbtCompound nbt, WrapperLookup lookup) {
2023-10-31 21:38:28 +01:00
pattern = SheetPattern.byId(nbt.getString("pattern"));
}
@Override
2024-09-26 22:36:35 +02:00
protected void writeNbt(NbtCompound nbt, WrapperLookup lookup) {
2023-10-31 21:38:28 +01:00
nbt.putString("pattern", pattern.asString());
}
@Override
2024-09-26 22:36:35 +02:00
public NbtCompound toInitialChunkDataNbt(WrapperLookup lookup) {
return createNbt(lookup);
2023-10-31 21:38:28 +01:00
}
public String getBase() {
return ((FancyBedBlock)getCachedState().getBlock()).base;
}
public void setPattern(SheetPattern pattern) {
this.pattern = pattern;
markDirty();
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 {
NONE(DyeColor.WHITE),
WHITE(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),
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")
2023-12-15 21:38:34 +01:00
public static final EnumCodec<SheetPattern> CODEC = StringIdentifiable.createCodec(SheetPattern::values);
2023-10-31 21:38:28 +01:00
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);
}
}
}