Files
src
.gitignore
build.gradle.kts
settings.gradle.kts
Code
18 834
src/main/kotlin/com/portal2craft/managers/PortalManager.kt
package com.portal2craft.managers
import com.portal2craft.Portal2Craft
import com.portal2craft.models.Portal
import com.portal2craft.models.PortalPair
import com.portal2craft.models.PortalType
import org.bukkit.Color
import org.bukkit.Location
import org.bukkit.Particle
import org.bukkit.block.BlockFace
import org.bukkit.entity.Entity
import org.bukkit.entity.Player
import org.bukkit.scheduler.BukkitRunnable
import org.bukkit.util.Vector
import java.util.UUID
import java.util.concurrent.ConcurrentHashMap
class PortalManager(private val plugin: Portal2Craft) {
private val portalPairs = ConcurrentHashMap<UUID, PortalPair>()
private val teleportCooldowns = ConcurrentHashMap<UUID, Long>()
private val portalLocations = ConcurrentHashMap<Location, Portal>()
init {
startParticleTask()
}
fun createPortal(player: Player, location: Location, type: PortalType, facing: BlockFace) {
val pair = portalPairs.getOrPut(player.uniqueId) {
PortalPair(player.uniqueId, null, null)
}
val oldPortal = when (type) {
PortalType.BLUE -> pair.bluePortal
PortalType.ORANGE -> pair.orangePortal
}
oldPortal?.let { portalLocations.remove(it.location) }
val portal = Portal(UUID.randomUUID(), location.clone(), type, facing, player.uniqueId)
when (type) {
PortalType.BLUE -> pair.bluePortal = portal
PortalType.ORANGE -> pair.orangePortal = portal
}
portalLocations[location] = portal
playPortalCreateEffect(location, type)
}
fun getPortalAt(location: Location): Portal? {
return portalLocations[location]
}
fun teleportThroughPortal(entity: Entity, portal: Portal) {
val pair = portalPairs[portal.owner] ?: return
if (!pair.isComplete()) return
val cooldown = plugin.config.getLong("portal.cooldown-ms", 100)
val now = System.currentTimeMillis()
val lastTeleport = teleportCooldowns[entity.uniqueId] ?: 0
if (now - lastTeleport < cooldown) return
val destination = pair.getOtherPortal(portal.type) ?: return
val newLocation = destination.location.clone()
newLocation.direction = getExitDirection(portal.facing, destination.facing, entity.location.direction)
val offset = getOffsetFromFace(destination.facing)
newLocation.add(offset)
if (plugin.config.getBoolean("portal.preserve-velocity", true)) {
val velocity = entity.velocity
val rotatedVelocity = rotateVelocity(velocity, portal.facing, destination.facing)
entity.teleport(newLocation)
entity.velocity = rotatedVelocity
} else {
entity.teleport(newLocation)
}
teleportCooldowns[entity.uniqueId] = now
playTeleportEffect(newLocation)
}
fun clearPortals(player: Player) {
val pair = portalPairs.remove(player.uniqueId) ?: return
pair.bluePortal?.let { portalLocations.remove(it.location) }
pair.orangePortal?.let { portalLocations.remove(it.location) }
}
fun clearAllPortals() {
portalPairs.clear()
portalLocations.clear()
teleportCooldowns.clear()
}
private fun startParticleTask() {
object : BukkitRunnable() {
override fun run() {
if (!plugin.config.getBoolean("portal.particle-effects", true)) return
portalLocations.values.forEach { portal ->
displayPortalParticles(portal)
}
}
}.runTaskTimer(plugin, 0L, 5L)
}
private fun displayPortalParticles(portal: Portal) {
val color = when (portal.type) {
PortalType.BLUE -> Color.BLUE
PortalType.ORANGE -> Color.fromRGB(255, 165, 0)
}
val loc = portal.location
val world = loc.world ?: return
val normal = getOffsetFromFace(portal.facing).normalize()
val offset = normal.multiply(0.1)
for (i in 0..3) {
val particleLoc = loc.clone().add(offset)
world.spawnParticle(
Particle.DUST,
particleLoc,
2,
0.3, 0.3, 0.0,
Particle.DustOptions(color, 1.0f)
)
}
}
private fun playPortalCreateEffect(location: Location, type: PortalType) {
val world = location.world ?: return
val color = when (type) {
PortalType.BLUE -> Color.BLUE
PortalType.ORANGE -> Color.fromRGB(255, 165, 0)
}
world.spawnParticle(
Particle.DUST,
location,
50,
0.5, 0.5, 0.5,
Particle.DustOptions(color, 2.0f)
)
}
private fun playTeleportEffect(location: Location) {
val world = location.world ?: return
world.spawnParticle(Particle.PORTAL, location, 30, 0.3, 0.3, 0.3, 0.1)
}
private fun getOffsetFromFace(face: BlockFace): Vector {
return when (face) {
BlockFace.NORTH -> Vector(0.0, 0.0, -0.5)
BlockFace.SOUTH -> Vector(0.0, 0.0, 0.5)
BlockFace.EAST -> Vector(0.5, 0.0, 0.0)
BlockFace.WEST -> Vector(-0.5, 0.0, 0.0)
BlockFace.UP -> Vector(0.0, 0.5, 0.0)
BlockFace.DOWN -> Vector(0.0, -0.5, 0.0)
else -> Vector(0.0, 0.0, 0.0)
}
}
private fun getExitDirection(entryFace: BlockFace, exitFace: BlockFace, currentDirection: Vector): Vector {
return currentDirection.clone()
}
private fun rotateVelocity(velocity: Vector, entryFace: BlockFace, exitFace: BlockFace): Vector {
return velocity.clone()
}
}