From aa934f800fd7c09d416135d5a65ba4ea746e9357 Mon Sep 17 00:00:00 2001 From: cheaterpaul Date: Sat, 12 Oct 2024 22:55:24 +0200 Subject: [PATCH] adjust and fix vertical coffin wake up and respawn position fix #1427 --- .../vampirism/blocks/CoffinBlock.java | 22 +++++++- .../entity/player/ModPlayerEventHandler.java | 55 +++++++++++++++++-- 2 files changed, 70 insertions(+), 7 deletions(-) diff --git a/src/main/java/de/teamlapen/vampirism/blocks/CoffinBlock.java b/src/main/java/de/teamlapen/vampirism/blocks/CoffinBlock.java index a61cff9455..79b871cdef 100644 --- a/src/main/java/de/teamlapen/vampirism/blocks/CoffinBlock.java +++ b/src/main/java/de/teamlapen/vampirism/blocks/CoffinBlock.java @@ -9,10 +9,12 @@ import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.network.chat.Component; +import net.minecraft.server.level.ServerPlayer; import net.minecraft.util.StringRepresentable; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.Pose; import net.minecraft.world.entity.player.Player; @@ -49,6 +51,7 @@ import java.util.HashMap; import java.util.Map; +import java.util.Optional; import static net.minecraft.world.level.block.state.properties.BlockStateProperties.HORIZONTAL_FACING; @@ -216,7 +219,7 @@ public InteractionResult use(@NotNull BlockState state, @NotNull Level worldIn, } else { final BlockPos finalPos = pos; BlockState finalState = state; - player.startSleepInBed(pos).ifLeft(sleepResult1 -> { + player.startSleepInBed(state.getValue(VERTICAL) ? pos.below() : pos).ifLeft(sleepResult1 -> { if (sleepResult1 != null) { player.displayClientMessage(sleepResults.getOrDefault(sleepResult1, sleepResult1.getMessage()), true); } @@ -258,14 +261,27 @@ public static void setCoffinSleepPosition(@NotNull Player player, @NotNull Block return; } } - player.setPos(blockPos.getX() + x, blockPos.getY() - 1, blockPos.getZ() + z); - player.setBoundingBox(new AABB(blockPos.getX() + x - 0.2, blockPos.getY() - 0.8, blockPos.getZ() + z - 0.2, blockPos.getX() + x + 0.2, blockPos.getY() + 0.4, blockPos.getZ() + z + 0.2)); + player.setPos(blockPos.getX() + x, blockPos.getY() - 0.05, blockPos.getZ() + z); + player.setBoundingBox(new AABB(blockPos.getX() + x - 0.2, blockPos.getY() + 0.15, blockPos.getZ() + z - 0.2, blockPos.getX() + x + 0.2, blockPos.getY() + 1.3, blockPos.getZ() + z + 0.2)); } else { player.setPos(blockPos.getX() + 0.5D, blockPos.getY() + 0.2D, blockPos.getZ() + 0.5D); player.setBoundingBox(player.dimensions.makeBoundingBox(blockPos.getX() + 0.5D, blockPos.getY() + 0.2D, blockPos.getZ() + 0.5D).deflate(0.3)); } } + @Override + public Optional getRespawnPosition(BlockState state, EntityType type, LevelReader levelReader, BlockPos pos, float orientation, @Nullable LivingEntity entity) { + if (state.getValue(VERTICAL)) { + if (state.getValue(PART) == CoffinPart.HEAD) { + pos = pos.below(); + } + pos = pos.relative(state.getValue(HORIZONTAL_FACING).getOpposite()); + return Optional.of(pos.getCenter()); + } else { + return Optional.of(pos.getCenter()); + } + } + public @NotNull BlockPos getOtherPos(@NotNull BlockPos pos, @NotNull BlockState state) { if (state.getValue(VERTICAL)) { if (state.getValue(PART) == CoffinPart.FOOT) { diff --git a/src/main/java/de/teamlapen/vampirism/entity/player/ModPlayerEventHandler.java b/src/main/java/de/teamlapen/vampirism/entity/player/ModPlayerEventHandler.java index 425c978a43..28e2dabd58 100755 --- a/src/main/java/de/teamlapen/vampirism/entity/player/ModPlayerEventHandler.java +++ b/src/main/java/de/teamlapen/vampirism/entity/player/ModPlayerEventHandler.java @@ -2,6 +2,7 @@ import com.google.common.base.Throwables; import com.google.common.collect.ImmutableList; +import com.mojang.datafixers.util.Either; import de.teamlapen.vampirism.REFERENCE; import de.teamlapen.vampirism.api.EnumStrength; import de.teamlapen.vampirism.api.VampirismAPI; @@ -19,10 +20,7 @@ import de.teamlapen.vampirism.blocks.TentBlock; import de.teamlapen.vampirism.blocks.mother.MotherBlock; import de.teamlapen.vampirism.config.VampirismConfig; -import de.teamlapen.vampirism.core.ModBlocks; -import de.teamlapen.vampirism.core.ModEffects; -import de.teamlapen.vampirism.core.ModFluids; -import de.teamlapen.vampirism.core.ModItems; +import de.teamlapen.vampirism.core.*; import de.teamlapen.vampirism.effects.VampirismPoisonEffect; import de.teamlapen.vampirism.effects.VampirismPotion; import de.teamlapen.vampirism.entity.factions.FactionPlayerHandler; @@ -36,6 +34,7 @@ import de.teamlapen.vampirism.util.RegUtil; import de.teamlapen.vampirism.util.TotemHelper; import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.MutableComponent; import net.minecraft.network.protocol.Packet; @@ -62,8 +61,10 @@ import net.minecraft.world.level.GameType; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.HorizontalDirectionalBlock; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.phys.Vec3; import net.minecraftforge.common.capabilities.ForgeCapabilities; import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.event.AttachCapabilitiesEvent; @@ -87,6 +88,8 @@ import java.util.Optional; import java.util.Set; +import static net.minecraft.world.entity.player.Player.BedSleepingProblem.*; + /** * Event handler for player related events */ @@ -545,4 +548,48 @@ public void onPlayerGameMode(PlayerEvent.PlayerChangeGameModeEvent event) { FactionPlayerHandler.getOpt(event.getEntity()).ifPresent(handler -> handler.getCurrentFactionPlayer().ifPresent(factionPlayer -> factionPlayer.getActionHandler().deactivateAllActions())); } } + + @SubscribeEvent + public void canStartSleeping(PlayerSleepInBedEvent event) { + BlockPos pos = event.getPos(); + Level level = event.getEntity().level(); + BlockState state = event.getEntity().level().getBlockState(pos); + if (Helper.isVampire(event.getEntity()) && state.is(ModTags.Blocks.COFFIN)) { + if (!level.dimensionType().natural()) { + event.setResult(NOT_POSSIBLE_HERE); + } else if (!bedInRange(event.getEntity(), pos, state.getValue(CoffinBlock.VERTICAL) ? Direction.DOWN : state.getValue(HorizontalDirectionalBlock.FACING).getOpposite())) { + event.setResult(TOO_FAR_AWAY); + } else if (state.getValue(CoffinBlock.VERTICAL)) { + BlockPos relative = event.getPos().relative(state.getValue(HorizontalDirectionalBlock.FACING).getOpposite()); + if (obstructedAt(level, relative) || obstructedAt(level, relative.above())) { + event.setResult(Player.BedSleepingProblem.OBSTRUCTED); + return; + } + } else { + BlockPos above = event.getPos().above(); + if (obstructedAt(level, above) || obstructedAt(level, above.relative(state.getValue(HorizontalDirectionalBlock.FACING).getOpposite()))) { + event.setResult(Player.BedSleepingProblem.OBSTRUCTED); + return; + } + } + if (!net.minecraftforge.event.ForgeEventFactory.fireSleepingTimeCheck(event.getEntity(), event.getOptionalPos())) { + event.setResult(Player.BedSleepingProblem.NOT_POSSIBLE_NOW); + } else { + event.setResult((Player.BedSleepingProblem) null); + } + } + } + + private static boolean obstructedAt(Level level, BlockPos pos) { + return level.getBlockState(pos).isSuffocating(level, pos); + } + + private static boolean bedInRange(Player player, BlockPos pos ,Direction direction) { + return isReachableBedBlock(player, pos) && isReachableBedBlock(player, pos.relative(direction)); + } + + private static boolean isReachableBedBlock(Player player, BlockPos pPos) { + Vec3 vec3 = Vec3.atBottomCenterOf(pPos); + return Math.abs(player.getX() - vec3.x()) <= 3.0D && Math.abs(player.getY() - vec3.y()) <= 2.0D && Math.abs(player.getZ() - vec3.z()) <= 3.0D; + } }