1
0
Fork 0
The portal creating plugin for Minecraft.
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.

166 lines
6.2 KiB

package net.deliciousreya.minecraftportal.model
import net.deliciousreya.minecraftportal.proto.PortalSaveDataProtos
import org.bukkit.Location
import org.bukkit.Material
import org.bukkit.plugin.Plugin
import java.lang.IllegalArgumentException
import java.lang.IllegalStateException
class PortalType(val mineral: Material) {
var oldestActivePortal: Portal? = null
var newestActivePortal: Portal? = null
val isEmpty: Boolean get() = oldestActivePortal == null && newestActivePortal == null
val isPaired: Boolean get() = oldestActivePortal != null && newestActivePortal != null
val getOnlyPortal: Portal get() = if (!isEmpty && !isPaired) oldestActivePortal!! else throw IllegalStateException("Can't getOnlyPortal for empty or paired portal type $this")
fun toProto(): PortalSaveDataProtos.PortalPair {
val builder = PortalSaveDataProtos.PortalPair.newBuilder()
val oldestPortal = oldestActivePortal
val newestPortal = newestActivePortal
if (oldestPortal != null) {
builder.older = oldestPortal.toProto()
}
if (newestPortal != null) {
builder.newer = newestPortal.toProto()
}
return builder.build()
}
fun addOrReplacePortal(portal: Portal): Portal? {
if (portal.type != this) {
throw DeserializationException("Tried to add $portal which didn't belong to ${this}")
}
return when {
this.isEmpty -> {
this.oldestActivePortal = portal
null
}
else -> {
val replacedPortal = newestActivePortal
replacedPortal?.stopEffects()
replacedPortal?.frame?.playPortalsUnlinkedEffect()
if (replacedPortal != null) {
cancelTeleportation()
}
portal.frame.playPortalsLinkedEffect()
this.newestActivePortal = portal
replacedPortal
}
}
}
fun loadUnpairedPortal(portal: Portal) {
if (!this.isEmpty) {
throw DeserializationException("Already have a value for ${this} when adding $portal")
}
if (portal.type != this) {
throw DeserializationException("Tried to add $portal which didn't belong to ${this}")
}
this.oldestActivePortal = portal
}
fun loadPairedPortals(olderPortal: Portal, newerPortal: Portal) {
if (!this.isEmpty) {
throw DeserializationException("Already have a value for ${this} when adding $olderPortal and $newerPortal")
}
if (olderPortal.type != this) {
throw DeserializationException("Tried to add $olderPortal which didn't belong to ${this}")
}
if (newerPortal.type != this) {
throw DeserializationException("Tried to add $newerPortal which didn't belong to ${this}")
}
this.oldestActivePortal = olderPortal
this.newestActivePortal = newerPortal
}
private var teleportation: Teleportation? = null
fun beginTeleportation(plugin: Plugin) {
val teleportation = Teleportation(this, plugin)
teleportation.start()
this.teleportation = teleportation
}
fun cancelTeleportation() {
teleportation?.cancelTeleportation()
teleportation = null
}
fun clear() {
oldestActivePortal = null
newestActivePortal = null
}
override fun toString():String {
return "PortalType{mineral=$mineral, oldestActivePortal=$oldestActivePortal, newestActivePortal=$newestActivePortal}"
}
/**
* Removes the given portal. If that brought this down to one portal and the other portal should become closed,
* returns it. Otherwise (if that frame wasn't for either portal or if there are none left) returns null.
*/
fun removePortalWithFrame(frame: PortalFrame): Portal? {
val isPaired = this.isPaired
if (oldestActivePortal?.frame == frame) {
oldestActivePortal?.stopEffects()
newestActivePortal?.stopEffects()
if (isPaired) {
oldestActivePortal?.frame?.playPortalsUnlinkedEffect()
newestActivePortal?.frame?.playPortalsUnlinkedEffect()
}
cancelTeleportation()
oldestActivePortal = newestActivePortal
} else if (newestActivePortal?.frame != frame) {
return null
}
oldestActivePortal?.stopEffects()
newestActivePortal?.stopEffects()
cancelTeleportation()
if (isPaired) {
oldestActivePortal?.frame?.playPortalsUnlinkedEffect()
newestActivePortal?.frame?.playPortalsUnlinkedEffect()
}
newestActivePortal = null
return oldestActivePortal
}
/** Gets the matching portal - null if there is none, or if that isn't one of the portals of this type */
fun getOtherPortal(frame: PortalFrame): Portal? {
return when (frame) {
oldestActivePortal?.frame -> newestActivePortal
newestActivePortal?.frame -> oldestActivePortal
else -> null
}
}
fun isLocationInPortalChamber(location: Location): Portal? {
if (oldestActivePortal?.frame?.lowerLeftFrontCorner?.world == location.world
&& oldestActivePortal?.frame?.portalInsideBoundingBox?.contains(location.toVector()) == true
) {
return oldestActivePortal
}
if (newestActivePortal?.frame?.lowerLeftFrontCorner?.world == location.world
&& newestActivePortal?.frame?.portalInsideBoundingBox?.contains(location.toVector()) == true
) {
return newestActivePortal
}
return null
}
fun isLocationInPortalOrChamber(location: Location): Portal? {
if (oldestActivePortal?.frame?.lowerLeftFrontCorner?.world == location.world
&& oldestActivePortal?.frame?.fullPortalBoundingBox?.contains(location.toVector()) == true
) {
return oldestActivePortal
}
if (newestActivePortal?.frame?.lowerLeftFrontCorner?.world == location.world
&& newestActivePortal?.frame?.fullPortalBoundingBox?.contains(location.toVector()) == true
) {
return newestActivePortal
}
return null
}
}