Skip to content

Commit

Permalink
fix bugs + beds on the ceiling
Browse files Browse the repository at this point in the history
  • Loading branch information
Thepigcat76 committed May 2, 2024
1 parent 89fab1b commit 5f702c9
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,114 @@


import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.npc.Villager;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.BedBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BedPart;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class InvertedBedBlock extends BedBlock {
public static final BooleanProperty CEILING = BooleanProperty.create("upside_down");
private static final VoxelShape REGULAR_SHAPE = Block.box(0, 0, 0, 16, 9, 16);
private static final VoxelShape CEILING_SHAPE = Block.box(0, 7, 0, 16, 16, 16);

public InvertedBedBlock(DyeColor p_49454_, Properties p_49455_) {
super(p_49454_, p_49455_);
}

@Nullable
@Override
public BlockState getStateForPlacement(BlockPlaceContext pContext) {
BlockState stateForPlacement = super.getStateForPlacement(pContext);
return stateForPlacement != null
? stateForPlacement.setValue(CEILING, pContext.getNearestLookingVerticalDirection().equals(Direction.UP))
: null;
}

@Override
public @NotNull VoxelShape getShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, CollisionContext pContext) {
return pState.getValue(CEILING)
? CEILING_SHAPE
: REGULAR_SHAPE;
}

@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> pBuilder) {
super.createBlockStateDefinition(pBuilder.add(CEILING));
}

@Override
public BlockEntity newBlockEntity(BlockPos pPos, BlockState pState) {
return new InvertedBedBlockEntity(pPos, pState);
}

@Override
public InteractionResult use(BlockState pState, Level pLevel, BlockPos pPos, Player pPlayer, InteractionHand pHand, BlockHitResult pHit) {
if (pLevel.isClientSide) {
return InteractionResult.CONSUME;
} else {
if (pState.getValue(PART) != BedPart.HEAD) {
pPos = pPos.relative(pState.getValue(FACING));
pState = pLevel.getBlockState(pPos);
if (!pState.is(this)) {
return InteractionResult.CONSUME;
}
}

if (!canSetSpawn(pLevel)) {
pLevel.removeBlock(pPos, false);
BlockPos blockpos = pPos.relative(pState.getValue(FACING).getOpposite());
if (pLevel.getBlockState(blockpos).is(this)) {
pLevel.removeBlock(blockpos, false);
}

Vec3 vec3 = pPos.getCenter();
pLevel.explode(null, pLevel.damageSources().badRespawnPointExplosion(vec3), null, vec3, 5.0F, true, Level.ExplosionInteraction.BLOCK);
return InteractionResult.SUCCESS;
} else if (pState.getValue(OCCUPIED)) {
if (!this.kickVillagerOutOfBed(pLevel, pPos)) {
pPlayer.displayClientMessage(Component.translatable("block.minecraft.bed.occupied"), true);
}

return InteractionResult.SUCCESS;
} else {
pPlayer.startSleepInBed(pPos).ifLeft(problem -> {
if (pLevel.isNight()) {
pPlayer.displayClientMessage(Component.translatable("block.invertedbed.bed.not_possible_now"), true);
} else {
if (problem.getMessage() != null) {
pPlayer.displayClientMessage(problem.getMessage(), true);
}
}
});
return InteractionResult.SUCCESS;
}
}
}

private boolean kickVillagerOutOfBed(Level pLevel, BlockPos pPos) {
List<Villager> list = pLevel.getEntitiesOfClass(Villager.class, new AABB(pPos), LivingEntity::isSleeping);
if (list.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,15 @@ public void render(InvertedBedBlockEntity pBlockEntity, float pPartialTick, Pose
pBuffer,
blockstate.getValue(InvertedBedBlock.PART) == BedPart.HEAD ? this.headRoot : this.footRoot,
blockstate.getValue(InvertedBedBlock.FACING),
blockstate.getValue(InvertedBedBlock.CEILING),
material,
i,
pPackedOverlay,
false
);
} else {
this.renderPiece(pPoseStack, pBuffer, this.headRoot, Direction.SOUTH, material, pPackedLight, pPackedOverlay, false);
this.renderPiece(pPoseStack, pBuffer, this.footRoot, Direction.SOUTH, material, pPackedLight, pPackedOverlay, true);
this.renderPiece(pPoseStack, pBuffer, this.headRoot, Direction.SOUTH, false, material, pPackedLight, pPackedOverlay, false);
this.renderPiece(pPoseStack, pBuffer, this.footRoot, Direction.SOUTH, false, material, pPackedLight, pPackedOverlay, true);
}
}

Expand All @@ -113,13 +114,18 @@ private void renderPiece(
MultiBufferSource pBufferSource,
ModelPart pModelPart,
Direction pDirection,
boolean upsideDown,
Material pMaterial,
int pPackedLight,
int pPackedOverlay,
boolean pFoot
) {
pPoseStack.pushPose();
pPoseStack.translate(0.0F, 0, pFoot ? -1.0F : 0.0F);
float y = 0;
if (upsideDown) {
y = 0.4375f;
}
pPoseStack.translate(0.0F, y, pFoot ? -1.0F : 0.0F);
pPoseStack.mulPose(Axis.XP.rotationDegrees(90.0F));
if (pDirection.equals(Direction.EAST) || pDirection.equals(Direction.WEST)) {
pPoseStack.mulPose(Axis.XN.rotationDegrees(180.0F));
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/invertedbed/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
"block.invertedbed.brown_inverted_bed":"Brown Inverted Bed",
"block.invertedbed.green_inverted_bed":"Green Inverted Bed",
"block.invertedbed.black_inverted_bed":"Black Inverted Bed",
"block.invertedbed.bed.not_possible_now": "You can sleep only at day",
"itemGroup.invertedbed":"Inverted Bed"
}

0 comments on commit 5f702c9

Please sign in to comment.