You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
118 lines
4.1 KiB
118 lines
4.1 KiB
package net.deliciousreya.minecraftportal.model
|
|
|
|
import net.deliciousreya.minecraftportal.proto.PortalSaveDataProtos
|
|
import org.bukkit.Location
|
|
import org.bukkit.entity.Entity
|
|
import org.bukkit.entity.LivingEntity
|
|
import org.bukkit.plugin.Plugin
|
|
import org.bukkit.potion.PotionEffect
|
|
import org.bukkit.potion.PotionEffectType
|
|
import org.bukkit.scheduler.BukkitRunnable
|
|
|
|
class Teleportation(val portalType: PortalType, val plugin: Plugin) {
|
|
val leftEntities = LinkedHashSet<Entity>()
|
|
val rightEntities = LinkedHashSet<Entity>()
|
|
val allEntities = LinkedHashSet<Entity>()
|
|
var currentTask: BukkitRunnable? = null
|
|
var sparklesTask: DoSparkles? = null
|
|
val leftPortal = portalType.newestActivePortal!!
|
|
val rightPortal = portalType.oldestActivePortal!!
|
|
|
|
fun cancelFor(entity: Entity) {
|
|
leftEntities.remove(entity)
|
|
rightEntities.remove(entity)
|
|
allEntities.remove(entity)
|
|
if (allEntities.isEmpty()) {
|
|
cancelTeleportation()
|
|
}
|
|
}
|
|
|
|
fun cancelEffectsOn(entity: Entity) {
|
|
if (entity is LivingEntity) {
|
|
entity.removePotionEffect(PotionEffectType.BLINDNESS)
|
|
entity.removePotionEffect(PotionEffectType.CONFUSION)
|
|
}
|
|
}
|
|
|
|
fun cancelTeleportation() {
|
|
leftPortal.frame.open()
|
|
rightPortal.frame.open()
|
|
leftPortal.frame.playTeleportCanceledEffect()
|
|
rightPortal.frame.playTeleportCanceledEffect()
|
|
cancelEffectsOnAll()
|
|
leftEntities.clear()
|
|
rightEntities.clear()
|
|
allEntities.clear()
|
|
currentTask?.cancel()
|
|
currentTask = null
|
|
sparklesTask?.cancel()
|
|
sparklesTask = null
|
|
}
|
|
|
|
fun cancelEffectsOnAll() {
|
|
for (entity in allEntities) {
|
|
cancelEffectsOn(entity)
|
|
}
|
|
}
|
|
|
|
fun start() {
|
|
leftEntities.addAll(leftPortal.frame.getEntities())
|
|
rightEntities.addAll(rightPortal.frame.getEntities())
|
|
allEntities.addAll(leftEntities)
|
|
allEntities.addAll(rightEntities)
|
|
for (entity in allEntities) {
|
|
if (entity is LivingEntity) {
|
|
entity.addPotionEffect(PotionEffect(PotionEffectType.CONFUSION, 200, 1, false, false, false))
|
|
entity.addPotionEffect(PotionEffect(PotionEffectType.BLINDNESS, 200, 1, false, false, false))
|
|
}
|
|
}
|
|
leftPortal.frame.close()
|
|
rightPortal.frame.close()
|
|
leftPortal.frame.playTeleporterActivatedEffect()
|
|
rightPortal.frame.playTeleporterActivatedEffect()
|
|
val task = DoTeleport()
|
|
task.runTaskLater(plugin, 100)
|
|
currentTask = task
|
|
val sparkles = DoSparkles()
|
|
sparkles.runTaskTimer(plugin, 10, 15)
|
|
sparklesTask = sparkles
|
|
}
|
|
|
|
inner class DoSparkles : BukkitRunnable() {
|
|
override fun run() {
|
|
leftPortal.frame.playTeleporterActiveEffect()
|
|
rightPortal.frame.playTeleporterActiveEffect()
|
|
}
|
|
}
|
|
|
|
inner class DoTeleport : BukkitRunnable() {
|
|
override fun run() {
|
|
for(leftEntity in leftEntities) {
|
|
val relativeLocation = leftPortal.frame.getRelativeLocationFromAbsoluteLocation(leftEntity.location)
|
|
val destination = rightPortal.frame.getAbsoluteLocationFromRelativeLocation(relativeLocation)
|
|
leftEntity.teleport(destination)
|
|
}
|
|
for(rightEntity in rightEntities) {
|
|
val relativeLocation = rightPortal.frame.getRelativeLocationFromAbsoluteLocation(rightEntity.location)
|
|
val destination = leftPortal.frame.getAbsoluteLocationFromRelativeLocation(relativeLocation)
|
|
rightEntity.teleport(destination)
|
|
}
|
|
leftPortal.frame.playTeleportTravelEffect()
|
|
rightPortal.frame.playTeleportTravelEffect()
|
|
val task = OpenDoors()
|
|
task.runTaskLater(plugin, 100)
|
|
currentTask = task
|
|
}
|
|
}
|
|
|
|
inner class OpenDoors: BukkitRunnable() {
|
|
override fun run() {
|
|
leftPortal.frame.open()
|
|
rightPortal.frame.open()
|
|
cancelEffectsOnAll()
|
|
currentTask = null
|
|
sparklesTask?.cancel()
|
|
sparklesTask = null
|
|
}
|
|
}
|
|
} |