Skip to content

Commit 32ac79e

Browse files
committed
Incorrect bed position taking fixed, incorrect spawn position after death fixed.
Signed-off-by: Pavel Erokhin (MairwunNx) <[email protected]>
1 parent f41b630 commit 32ac79e

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

src/main/kotlin/com/mairwunnx/projectessentials/spawn/ModuleObject.kt

+27-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
@file:Suppress("unused", "SENSELESS_COMPARISON")
1+
@file:Suppress("unused", "SENSELESS_COMPARISON", "UNNECESSARY_SAFE_CALL")
22

33
package com.mairwunnx.projectessentials.spawn
44

55
import com.mairwunnx.projectessentials.core.api.v1.configuration.ConfigurationAPI.getConfigurationByName
6+
import com.mairwunnx.projectessentials.core.api.v1.extensions.asPlayerEntity
67
import com.mairwunnx.projectessentials.core.api.v1.localization.LocalizationAPI
78
import com.mairwunnx.projectessentials.core.api.v1.module.IModule
89
import com.mairwunnx.projectessentials.core.api.v1.providers.ProviderAPI
910
import com.mairwunnx.projectessentials.spawn.commands.SetSpawnCommand
1011
import com.mairwunnx.projectessentials.spawn.commands.SpawnCommand
1112
import com.mairwunnx.projectessentials.spawn.configurations.SpawnConfiguration
13+
import net.minecraft.entity.player.PlayerEntity
1214
import net.minecraft.entity.player.ServerPlayerEntity
1315
import net.minecraft.world.dimension.DimensionType
16+
import net.minecraft.world.dimension.DimensionType.getById
1417
import net.minecraftforge.common.MinecraftForge.EVENT_BUS
1518
import net.minecraftforge.event.entity.player.PlayerEvent
1619
import net.minecraftforge.eventbus.api.EventPriority
@@ -25,7 +28,7 @@ val spawnConfiguration by lazy {
2528

2629
fun forceTeleportToSpawn(player: ServerPlayerEntity) {
2730
val targetWorld = player.server.getWorld(
28-
DimensionType.getById(spawnConfiguration.take().dimensionId) ?: DimensionType.OVERWORLD
31+
getById(spawnConfiguration.take().dimensionId) ?: DimensionType.OVERWORLD
2932
)
3033
with(spawnConfiguration.take()) {
3134
player.teleport(targetWorld, xPos + 0.5, yPos + 0.5, zPos + 0.5, yaw, pitch)
@@ -75,20 +78,33 @@ class ModuleObject : IModule {
7578
}
7679
}
7780

81+
private val handledForSpawn = mutableSetOf<String>()
82+
7883
@SubscribeEvent(priority = EventPriority.HIGHEST)
7984
fun onPlayerRespawn(event: PlayerEvent.Clone) {
85+
if (!event.isWasDeath) return
8086
val player = event.original as ServerPlayerEntity
81-
if (player.bedPosition.isPresent) {
82-
player.server.worlds.forEach {
83-
val pos = player.getBedLocation(it.dimension.type)
84-
if (pos != null) {
87+
player.server.worlds.forEach {
88+
player.getBedLocation(it.dimension.type)?.let { pos ->
89+
if (
90+
PlayerEntity.checkBedValidRespawnPosition(
91+
player.server.getWorld(player.dimension), pos, false
92+
).isPresent
93+
) {
8594
player.teleport(
86-
it,
87-
pos.x.toDouble() + 0.5, pos.y.toDouble() + 0.5, pos.z.toDouble() + 0.5,
95+
it, pos.x.toDouble() + 0.5, pos.y.toDouble() + 0.5, pos.z.toDouble() + 0.5,
8896
player.rotationYaw, player.rotationPitch
89-
)
90-
}
97+
).let { return }
98+
} else handledForSpawn.add(player.name.string)
9199
}
92-
} else forceTeleportToSpawn(player)
100+
}
101+
handledForSpawn.add(player.name.string)
102+
}
103+
104+
@SubscribeEvent(priority = EventPriority.HIGHEST)
105+
fun onPlayerRespawnPost(event: PlayerEvent.PlayerRespawnEvent) {
106+
if (event.player.name.string !in handledForSpawn) return
107+
handledForSpawn.remove(event.player.name.string)
108+
forceTeleportToSpawn(event.player.asPlayerEntity)
93109
}
94110
}

0 commit comments

Comments
 (0)