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-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;
|
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;
|
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;
|
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;
|
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));
|
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
|
|
|
|
2024-02-02 23:35:42 +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;
|
|
|
|
}
|
|
|
|
|
2023-11-25 03:15:29 +01:00
|
|
|
@Override
|
2024-10-03 19:10:03 +02:00
|
|
|
protected VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
|
2023-11-25 03:15:29 +01:00
|
|
|
return SHAPES.get(state.get(PART).ordinal()).apply(BedBlock.getOppositePartDirection(state));
|
|
|
|
}
|
|
|
|
|
2024-03-22 20:09:50 +01:00
|
|
|
@Override
|
2024-10-03 19:10:03 +02:00
|
|
|
protected void onStateReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean moved) {
|
2024-03-22 20:09:50 +01:00
|
|
|
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
|
2024-04-05 23:01:26 +02:00
|
|
|
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));
|
|
|
|
}
|
2024-04-05 23:01:26 +02:00
|
|
|
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();
|
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),
|
2024-03-22 01:07:36 +01:00
|
|
|
WHITE(DyeColor.WHITE),
|
2023-11-02 19:24:35 +01:00
|
|
|
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")
|
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);
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|