Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow players to respawn in Cryogenic Chambers #388

Merged
merged 12 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public InteractionResult multiBlockUseWithoutItem(BlockState baseState, Level le
player.beginCryoSleep();
level.setBlockAndUpdate(basePos, baseState.setValue(OCCUPIED, true));

player.startSleepInBed(basePos).ifLeft(problem -> {
player.startSleepInBed(basePos.above()).ifLeft(problem -> {
switch(problem) {
case Player.BedSleepingProblem.OBSTRUCTED:
player.displayClientMessage(Component.translatable(Translations.Chat.CHAMBER_OBSTRUCTED), true);
Expand All @@ -214,6 +214,7 @@ public InteractionResult multiBlockUseWithoutItem(BlockState baseState, Level le

player.endCryoSleep();
level.setBlockAndUpdate(basePos, baseState.setValue(OCCUPIED, false));
player.setYRot(baseState.getValue(CryogenicChamberBlock.FACING).toYRot());
});
} else {
player.displayClientMessage(Component.translatable(Translations.Chat.CHAMBER_HOT, (int)(player.getCryogenicChamberCooldown() / TICKS.tickrate())), false);
Expand Down Expand Up @@ -244,4 +245,9 @@ public float getShadeBrightness(BlockState blockState, BlockGetter blockGetter,
public boolean propagatesSkylightDown(BlockState blockState, BlockGetter blockGetter, BlockPos blockPos) {
return true;
}

@Override
public boolean isPossibleToRespawnInThis(BlockState blockState) {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,9 @@ public float getShadeBrightness(BlockState blockState, BlockGetter blockGetter,
public boolean propagatesSkylightDown(BlockState blockState, BlockGetter blockGetter, BlockPos blockPos) {
return true;
}

@Override
public boolean isPossibleToRespawnInThis(BlockState blockState) {
return true;
}
}
19 changes: 15 additions & 4 deletions src/main/java/dev/galacticraft/mod/events/GCEventHandlers.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import dev.galacticraft.api.universe.celestialbody.landable.Landable;
import dev.galacticraft.api.universe.celestialbody.landable.teleporter.CelestialTeleporter;
import dev.galacticraft.mod.content.block.special.CryogenicChamberBlock;
import dev.galacticraft.mod.content.block.special.CryogenicChamberPart;
import dev.galacticraft.mod.misc.footprint.FootprintManager;
import dev.galacticraft.mod.network.s2c.FootprintRemovedPacket;
import dev.galacticraft.mod.util.Translations;
Expand Down Expand Up @@ -62,7 +63,7 @@ public static void init() {
}

public static InteractionResult allowCryogenicSleep(LivingEntity entity, BlockPos sleepingPos, BlockState state, boolean vanillaResult) {
if (state.getBlock() instanceof CryogenicChamberBlock) {
if (state.getBlock() instanceof CryogenicChamberPart && !state.getValue(CryogenicChamberPart.TOP)) {
return entity.isInCryoSleep()
? InteractionResult.SUCCESS
: InteractionResult.PASS;
Expand All @@ -74,7 +75,7 @@ public static Direction changeSleepPosition(LivingEntity entity, BlockPos sleepi
if (entity.isInCryoSleep()) {
BlockState state = entity.level().getBlockState(sleepingPos);

if (state.getBlock() instanceof CryogenicChamberBlock) return state.getValue(CryogenicChamberBlock.FACING);
if (state.getBlock() instanceof CryogenicChamberPart) return state.getValue(CryogenicChamberBlock.FACING);
}

return sleepingDirection;
Expand All @@ -101,9 +102,19 @@ public static void onWakeFromCryoSleep(LivingEntity entity, BlockPos sleepingPos
Level level = entity.level();
if (!level.isClientSide() && entity.isInCryoSleep()) {
entity.endCryoSleep();
BlockState baseState = level.getBlockState(sleepingPos);
BlockPos basePos = sleepingPos.below();
BlockState baseState = level.getBlockState(basePos);
float angle = 0.0F;
if (baseState.getBlock() instanceof CryogenicChamberBlock) {
level.setBlockAndUpdate(sleepingPos, baseState.setValue(BlockStateProperties.OCCUPIED, false));
level.setBlockAndUpdate(basePos, baseState.setValue(BlockStateProperties.OCCUPIED, false));
angle = baseState.getValue(CryogenicChamberBlock.FACING).toYRot();
entity.setYRot(angle);
}

ServerPlayer serverPlayer = (ServerPlayer) entity;
if (!(serverPlayer.getRespawnDimension() == level.dimension() && basePos.equals(serverPlayer.getRespawnPosition()))) {
boolean forceRespawn = false;
serverPlayer.setRespawnPosition(level.dimension(), basePos, angle, forceRespawn, true);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2019-2024 Team Galacticraft
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package dev.galacticraft.mod.mixin;

import dev.galacticraft.mod.content.block.special.CryogenicChamberBlock;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.portal.DimensionTransition;
import net.minecraft.world.phys.Vec3;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(DimensionTransition.class)
public abstract class DimensionTransitionMixin {
@Inject(method = "missingRespawnBlock", at = @At(value = "HEAD"), cancellable = true)
private static void findRespawnAndUseCryoChamber(ServerLevel overworld, Entity entity, DimensionTransition.PostDimensionTransition postDimensionTransition, CallbackInfoReturnable<DimensionTransition> cir) {
if (entity instanceof ServerPlayer serverPlayer) {
BlockPos blockPos = serverPlayer.getRespawnPosition();
float yaw = serverPlayer.getRespawnAngle();
ServerLevel serverLevel = serverPlayer.server.getLevel(serverPlayer.getRespawnDimension());
if (serverLevel != null && blockPos != null) {
if (gc$canRespawn(serverLevel, blockPos)) {
cir.setReturnValue(new DimensionTransition(serverLevel, blockPos.getBottomCenter(), Vec3.ZERO, yaw, 0.0f, postDimensionTransition));
}
}
}
}

@Unique
private static boolean gc$canRespawn(Level level, BlockPos blockPos) {
BlockState baseState = level.getBlockState(blockPos);
if (baseState.getBlock() instanceof CryogenicChamberBlock) {
Direction direction = baseState.getValue(CryogenicChamberBlock.FACING);
return gc$freeAt(level, blockPos.relative(direction)) && gc$freeAt(level, blockPos.above().relative(direction));
}
return false;
}

@Unique
private static boolean gc$freeAt(Level level, BlockPos blockPos) {
return !level.getBlockState(blockPos).isSuffocating(level, blockPos);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,10 @@ public void setCryogenicChamberCooldown(int cryogenicChamberCooldown) {
@Inject(method = "setPosToBed", at = @At("HEAD"), cancellable = true)
private void gc$setCryoSleepPos(BlockPos blockPos, CallbackInfo ci) {
if (isInCryoSleep()) {
Vec3 pos = blockPos.getBottomCenter();
this.setPos(pos.x, pos.y + 1F, pos.z);
Vec3 pos = blockPos.below().getBottomCenter();
this.setPos(pos.x, pos.y, pos.z);
this.setDeltaMovement(Vec3.ZERO);
this.hasImpulse = false;
ci.cancel();
}
}
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/dev/galacticraft/mod/mixin/ServerPlayerMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,15 @@ private void canStopRiding(CallbackInfo ci) {
}

@Inject(method = "bedBlocked", at = @At(value = "HEAD"), cancellable = true)
private void checkIfCryoBedBlocked(BlockPos blockPos, Direction direction, CallbackInfoReturnable<Boolean> cir){
if(this.level().getBlockState(blockPos).getBlock() instanceof CryogenicChamberBlock){
BlockPos blockPos2 = blockPos.relative(direction);

cir.setReturnValue(!this.gc$freeAt(blockPos2) || !this.gc$freeAt(blockPos2.above()));
private void checkIfCryoBedBlocked(BlockPos sleepingPos, Direction direction, CallbackInfoReturnable<Boolean> cir) {
BlockPos basePos = sleepingPos.below();
if (this.level().getBlockState(basePos).getBlock() instanceof CryogenicChamberBlock){
cir.setReturnValue(!this.gc$freeAt(basePos.relative(direction)) || !this.gc$freeAt(sleepingPos.relative(direction)));
}
}

@Unique
private boolean gc$freeAt(BlockPos blockPos){
private boolean gc$freeAt(BlockPos blockPos) {
return !this.level().getBlockState(blockPos).isSuffocating(this.level(), blockPos);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ private void rotateToMatchRocket(LivingEntity entity, PoseStack pose, float anim
if (entity.isInCryoSleep()) {
Direction direction = entity.getBedOrientation();
float j = direction != null ? sleepDirectionToRotationCryo(direction) : bodyYaw;
pose.translate(0, 0.82F, 0);
pose.mulPose(Axis.YP.rotationDegrees(j));
ci.cancel();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"parent": "minecraft:block/block",
"texture_size": [128, 128],
"textures": {
"cryo_chamber": "galacticraft:model/cryo_chamber",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"parent": "minecraft:block/block",
"texture_size": [128, 128],
"textures": {
"cryo_chamber": "galacticraft:model/cryo_chamber",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"parent": "minecraft:block/block",
"texture_size": [128, 128],
"textures": {
"cryo_chamber": "galacticraft:model/cryo_chamber",
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/galacticraft.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"BlockBehaviourMixin",
"BucketItemAccessor",
"BucketItemMixin",
"DimensionTransitionMixin",
"DimensionTypeMixin",
"EntityMixin",
"FlowingFluidMixin",
Expand Down