Skip to content

Commit

Permalink
adjust and fix vertical coffin wake up and respawn position
Browse files Browse the repository at this point in the history
fix #1427
  • Loading branch information
Cheaterpaul committed Oct 12, 2024
1 parent 1bf26de commit aa934f8
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 7 deletions.
22 changes: 19 additions & 3 deletions src/main/java/de/teamlapen/vampirism/blocks/CoffinBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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<Vec3> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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
*/
Expand Down Expand Up @@ -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;
}
}

0 comments on commit aa934f8

Please sign in to comment.