1
- @file:Suppress(" unused" , " SENSELESS_COMPARISON" )
1
+ @file:Suppress(" unused" , " SENSELESS_COMPARISON" , " UNNECESSARY_SAFE_CALL " )
2
2
3
3
package com.mairwunnx.projectessentials.spawn
4
4
5
5
import com.mairwunnx.projectessentials.core.api.v1.configuration.ConfigurationAPI.getConfigurationByName
6
+ import com.mairwunnx.projectessentials.core.api.v1.extensions.asPlayerEntity
6
7
import com.mairwunnx.projectessentials.core.api.v1.localization.LocalizationAPI
7
8
import com.mairwunnx.projectessentials.core.api.v1.module.IModule
8
9
import com.mairwunnx.projectessentials.core.api.v1.providers.ProviderAPI
9
10
import com.mairwunnx.projectessentials.spawn.commands.SetSpawnCommand
10
11
import com.mairwunnx.projectessentials.spawn.commands.SpawnCommand
11
12
import com.mairwunnx.projectessentials.spawn.configurations.SpawnConfiguration
13
+ import net.minecraft.entity.player.PlayerEntity
12
14
import net.minecraft.entity.player.ServerPlayerEntity
13
15
import net.minecraft.world.dimension.DimensionType
16
+ import net.minecraft.world.dimension.DimensionType.getById
14
17
import net.minecraftforge.common.MinecraftForge.EVENT_BUS
15
18
import net.minecraftforge.event.entity.player.PlayerEvent
16
19
import net.minecraftforge.eventbus.api.EventPriority
@@ -25,7 +28,7 @@ val spawnConfiguration by lazy {
25
28
26
29
fun forceTeleportToSpawn (player : ServerPlayerEntity ) {
27
30
val targetWorld = player.server.getWorld(
28
- DimensionType . getById(spawnConfiguration.take().dimensionId) ? : DimensionType .OVERWORLD
31
+ getById(spawnConfiguration.take().dimensionId) ? : DimensionType .OVERWORLD
29
32
)
30
33
with (spawnConfiguration.take()) {
31
34
player.teleport(targetWorld, xPos + 0.5 , yPos + 0.5 , zPos + 0.5 , yaw, pitch)
@@ -75,20 +78,33 @@ class ModuleObject : IModule {
75
78
}
76
79
}
77
80
81
+ private val handledForSpawn = mutableSetOf<String >()
82
+
78
83
@SubscribeEvent(priority = EventPriority .HIGHEST )
79
84
fun onPlayerRespawn (event : PlayerEvent .Clone ) {
85
+ if (! event.isWasDeath) return
80
86
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
+ ) {
85
94
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 ,
88
96
player.rotationYaw, player.rotationPitch
89
- )
90
- }
97
+ ). let { return }
98
+ } else handledForSpawn.add(player.name.string)
91
99
}
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)
93
109
}
94
110
}
0 commit comments