diff --git a/src/main/kotlin/com/fardragi/nyaruko/core/events/PlayerMoveEvent.kt b/src/main/kotlin/com/fardragi/nyaruko/core/events/PlayerMoveEvent.kt index a113425..1fc2f2d 100644 --- a/src/main/kotlin/com/fardragi/nyaruko/core/events/PlayerMoveEvent.kt +++ b/src/main/kotlin/com/fardragi/nyaruko/core/events/PlayerMoveEvent.kt @@ -1,14 +1,14 @@ package com.fardragi.nyaruko.core.events import com.fardragi.nyaruko.shared.NyarukoEvent -import com.fardragi.nyaruko.viewmodels.PlayerPositionViewModel +import com.fardragi.nyaruko.viewmodels.PositionViewModel import cpw.mods.fml.common.eventhandler.Cancelable import net.minecraft.entity.player.EntityPlayerMP @Cancelable data class PlayerMoveEvent( val player: EntityPlayerMP, - val old: PlayerPositionViewModel, - val new: PlayerPositionViewModel + val old: PositionViewModel, + val new: PositionViewModel ) : NyarukoEvent() { } diff --git a/src/main/kotlin/com/fardragi/nyaruko/core/handlers/PlayerTickHandler.kt b/src/main/kotlin/com/fardragi/nyaruko/core/handlers/PlayerTickHandler.kt index d4ab170..1e1c4d4 100644 --- a/src/main/kotlin/com/fardragi/nyaruko/core/handlers/PlayerTickHandler.kt +++ b/src/main/kotlin/com/fardragi/nyaruko/core/handlers/PlayerTickHandler.kt @@ -1,9 +1,9 @@ package com.fardragi.nyaruko.core.handlers import com.fardragi.nyaruko.core.events.PlayerMoveEvent -import com.fardragi.nyaruko.extensions.teleportTo +import com.fardragi.nyaruko.extensions.teleport import com.fardragi.nyaruko.shared.handlers.NyarukoHandlerBase -import com.fardragi.nyaruko.viewmodels.PlayerPositionViewModel +import com.fardragi.nyaruko.viewmodels.PositionViewModel import cpw.mods.fml.common.eventhandler.EventPriority import cpw.mods.fml.common.eventhandler.SubscribeEvent import cpw.mods.fml.common.gameevent.PlayerEvent.PlayerLoggedOutEvent @@ -13,7 +13,7 @@ import net.minecraft.entity.player.EntityPlayerMP import java.util.UUID class PlayerTickHandler : NyarukoHandlerBase() { - private val playerPositions = mutableMapOf() + private val playerPositions = mutableMapOf() @SubscribeEvent(priority = EventPriority.HIGHEST) fun onPlayerTick(event: PlayerTickEvent) { @@ -23,12 +23,12 @@ class PlayerTickHandler : NyarukoHandlerBase() { val player = event.player as EntityPlayerMP if (player.uniqueID !in playerPositions) { - playerPositions[player.uniqueID] = PlayerPositionViewModel(player) + playerPositions[player.uniqueID] = PositionViewModel(player) return } playerPositions[player.uniqueID]?.let { oldPosition -> - val newPosition = PlayerPositionViewModel(player) + val newPosition = PositionViewModel(player) if (oldPosition.block != newPosition.block) { playerPositions[player.uniqueID] = newPosition @@ -37,7 +37,7 @@ class PlayerTickHandler : NyarukoHandlerBase() { playerMoveEvent.send() if (playerMoveEvent.isCanceled) { - player.teleportTo(oldPosition) + player.teleport(oldPosition) playerPositions[player.uniqueID] = oldPosition } } else { diff --git a/src/main/kotlin/com/fardragi/nyaruko/extensions/EntityPlayer.kt b/src/main/kotlin/com/fardragi/nyaruko/extensions/EntityPlayer.kt new file mode 100644 index 0000000..283e6fb --- /dev/null +++ b/src/main/kotlin/com/fardragi/nyaruko/extensions/EntityPlayer.kt @@ -0,0 +1,44 @@ +package com.fardragi.nyaruko.extensions + +import com.fardragi.nyaruko.utils.NyarukoTeleporter +import com.fardragi.nyaruko.viewmodels.PositionViewModel +import net.minecraft.entity.player.EntityPlayer +import net.minecraft.entity.player.EntityPlayerMP +import net.minecraft.server.MinecraftServer + +fun EntityPlayer.teleport(position: PositionViewModel) { + val server = MinecraftServer.getServer() + + if (dimension == position.dimension) { + setLocationAndAngles( + position.positionX, + position.positionY, + position.positionZ, + position.rotationYaw, + position.rotationPitch + ) + setPositionAndUpdate( + position.positionX, + position.positionY, + position.positionZ, + ) + return + } + + if (isRiding) { + dismountEntity(ridingEntity) + } + + val newWorld = server.worldServerForDimension(position.dimension) + + server.configurationManager.transferPlayerToDimension( + this as EntityPlayerMP, position.dimension, NyarukoTeleporter( + newWorld, + position.positionX, + position.positionY, + position.positionZ, + position.rotationYaw, + position.rotationPitch + ) + ) +} diff --git a/src/main/kotlin/com/fardragi/nyaruko/extensions/Player.kt b/src/main/kotlin/com/fardragi/nyaruko/extensions/Player.kt deleted file mode 100644 index 4786de3..0000000 --- a/src/main/kotlin/com/fardragi/nyaruko/extensions/Player.kt +++ /dev/null @@ -1,14 +0,0 @@ -package com.fardragi.nyaruko.extensions - -import com.fardragi.nyaruko.viewmodels.PlayerPositionViewModel -import net.minecraft.entity.player.EntityPlayerMP - -fun EntityPlayerMP.teleportTo(position: PlayerPositionViewModel) { - this.playerNetServerHandler.setPlayerLocation( - position.positionX, - position.positionY, - position.positionZ, - position.rotationYaw, - position.rotationPitch - ) -} diff --git a/src/main/kotlin/com/fardragi/nyaruko/models/User.kt b/src/main/kotlin/com/fardragi/nyaruko/models/User.kt index ea312e4..644d68a 100644 --- a/src/main/kotlin/com/fardragi/nyaruko/models/User.kt +++ b/src/main/kotlin/com/fardragi/nyaruko/models/User.kt @@ -29,5 +29,3 @@ class User(id: EntityID) : Entity(id) { ) } } - - diff --git a/src/main/kotlin/com/fardragi/nyaruko/utils/NyarukoTeleporter.kt b/src/main/kotlin/com/fardragi/nyaruko/utils/NyarukoTeleporter.kt new file mode 100644 index 0000000..824f810 --- /dev/null +++ b/src/main/kotlin/com/fardragi/nyaruko/utils/NyarukoTeleporter.kt @@ -0,0 +1,29 @@ +package com.fardragi.nyaruko.utils + +import net.minecraft.entity.Entity +import net.minecraft.world.Teleporter +import net.minecraft.world.WorldServer + +class NyarukoTeleporter( + worldIn: WorldServer, + private val positionX: Double, + private val positionY: Double, + private val positionZ: Double, + private val yaw: Float, + private val pitch: Float +) : + Teleporter(worldIn) { + override fun placeInPortal(entity: Entity, x: Double, y: Double, z: Double, r: Float) { + entity.setLocationAndAngles(positionX, positionY, positionZ, yaw, pitch) + } + + override fun placeInExistingPortal( + par1Entity: Entity?, + par2: Double, + par4: Double, + par6: Double, + par8: Float + ): Boolean { + return false + } +} diff --git a/src/main/kotlin/com/fardragi/nyaruko/viewmodels/BlockViewModel.kt b/src/main/kotlin/com/fardragi/nyaruko/viewmodels/BlockViewModel.kt index 98e359a..ae70128 100644 --- a/src/main/kotlin/com/fardragi/nyaruko/viewmodels/BlockViewModel.kt +++ b/src/main/kotlin/com/fardragi/nyaruko/viewmodels/BlockViewModel.kt @@ -1,4 +1,4 @@ package com.fardragi.nyaruko.viewmodels -data class BlockViewModel(val x: Int, val y: Int, val z: Int) { +data class BlockViewModel(val dimension: Int, val x: Int, val y: Int, val z: Int) { } diff --git a/src/main/kotlin/com/fardragi/nyaruko/viewmodels/PlayerPositionViewModel.kt b/src/main/kotlin/com/fardragi/nyaruko/viewmodels/PlayerPositionViewModel.kt deleted file mode 100644 index 27e9bc9..0000000 --- a/src/main/kotlin/com/fardragi/nyaruko/viewmodels/PlayerPositionViewModel.kt +++ /dev/null @@ -1,14 +0,0 @@ -package com.fardragi.nyaruko.viewmodels - -import net.minecraft.entity.player.EntityPlayerMP -import kotlin.math.floor - -data class PlayerPositionViewModel(val player: EntityPlayerMP) { - val positionX = player.posX; - val positionY = player.posY; - val positionZ = player.posZ - val rotationYaw = player.rotationYaw - val rotationPitch = player.rotationPitch - - val block get() = BlockViewModel(floor(positionX).toInt(), floor(positionY).toInt(), floor(positionZ).toInt()) -} diff --git a/src/main/kotlin/com/fardragi/nyaruko/viewmodels/PositionViewModel.kt b/src/main/kotlin/com/fardragi/nyaruko/viewmodels/PositionViewModel.kt new file mode 100644 index 0000000..28a29db --- /dev/null +++ b/src/main/kotlin/com/fardragi/nyaruko/viewmodels/PositionViewModel.kt @@ -0,0 +1,21 @@ +package com.fardragi.nyaruko.viewmodels + +import net.minecraft.entity.Entity +import kotlin.math.floor + +data class PositionViewModel(val player: Entity) { + val dimension = player.dimension + val positionX = player.posX + val positionY = player.posY + val positionZ = player.posZ + val rotationYaw = player.rotationYaw + val rotationPitch = player.rotationPitch + + val block + get() = BlockViewModel( + dimension, + floor(positionX).toInt(), + floor(positionY).toInt(), + floor(positionZ).toInt() + ) +}