Skip to content

Commit

Permalink
Add: teleport inter dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
vulcandragi committed Sep 11, 2024
1 parent e2ea001 commit 3269f93
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -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() {
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,7 +13,7 @@ import net.minecraft.entity.player.EntityPlayerMP
import java.util.UUID

class PlayerTickHandler : NyarukoHandlerBase() {
private val playerPositions = mutableMapOf<UUID, PlayerPositionViewModel>()
private val playerPositions = mutableMapOf<UUID, PositionViewModel>()

@SubscribeEvent(priority = EventPriority.HIGHEST)
fun onPlayerTick(event: PlayerTickEvent) {
Expand All @@ -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
Expand All @@ -37,7 +37,7 @@ class PlayerTickHandler : NyarukoHandlerBase() {
playerMoveEvent.send()

if (playerMoveEvent.isCanceled) {
player.teleportTo(oldPosition)
player.teleport(oldPosition)
playerPositions[player.uniqueID] = oldPosition
}
} else {
Expand Down
44 changes: 44 additions & 0 deletions src/main/kotlin/com/fardragi/nyaruko/extensions/EntityPlayer.kt
Original file line number Diff line number Diff line change
@@ -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
)
)
}
14 changes: 0 additions & 14 deletions src/main/kotlin/com/fardragi/nyaruko/extensions/Player.kt

This file was deleted.

2 changes: 0 additions & 2 deletions src/main/kotlin/com/fardragi/nyaruko/models/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,3 @@ class User(id: EntityID<UUID>) : Entity<UUID>(id) {
)
}
}


29 changes: 29 additions & 0 deletions src/main/kotlin/com/fardragi/nyaruko/utils/NyarukoTeleporter.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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) {
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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()
)
}

0 comments on commit 3269f93

Please sign in to comment.