1- @file:Suppress(" unused" , " SENSELESS_COMPARISON" )
1+ @file:Suppress(" unused" , " SENSELESS_COMPARISON" , " UNNECESSARY_SAFE_CALL " )
22
33package com.mairwunnx.projectessentials.spawn
44
55import com.mairwunnx.projectessentials.core.api.v1.configuration.ConfigurationAPI.getConfigurationByName
6+ import com.mairwunnx.projectessentials.core.api.v1.extensions.asPlayerEntity
67import com.mairwunnx.projectessentials.core.api.v1.localization.LocalizationAPI
78import com.mairwunnx.projectessentials.core.api.v1.module.IModule
89import com.mairwunnx.projectessentials.core.api.v1.providers.ProviderAPI
910import com.mairwunnx.projectessentials.spawn.commands.SetSpawnCommand
1011import com.mairwunnx.projectessentials.spawn.commands.SpawnCommand
1112import com.mairwunnx.projectessentials.spawn.configurations.SpawnConfiguration
13+ import net.minecraft.entity.player.PlayerEntity
1214import net.minecraft.entity.player.ServerPlayerEntity
1315import net.minecraft.world.dimension.DimensionType
16+ import net.minecraft.world.dimension.DimensionType.getById
1417import net.minecraftforge.common.MinecraftForge.EVENT_BUS
1518import net.minecraftforge.event.entity.player.PlayerEvent
1619import net.minecraftforge.eventbus.api.EventPriority
@@ -25,7 +28,7 @@ val spawnConfiguration by lazy {
2528
2629fun 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 .func_213822_a(
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