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.
57 lines
2.4 KiB
57 lines
2.4 KiB
package net.deliciousreya.minecraftportal.model
|
|
|
|
import net.deliciousreya.minecraftportal.extensions.*
|
|
import net.deliciousreya.minecraftportal.proto.PortalSaveDataProtos
|
|
import org.bukkit.Material
|
|
import org.bukkit.Server
|
|
import org.bukkit.plugin.Plugin
|
|
import java.lang.IllegalArgumentException
|
|
|
|
fun PortalSaveDataProtos.Portal.Mineral.asColor(): PortalSaveDataProtos.Portal.Color {
|
|
if (this == PortalSaveDataProtos.Portal.Mineral.UNKNOWN_MINERAL) {
|
|
throw IllegalArgumentException("No portal color for $this")
|
|
}
|
|
val material = toMaterial()
|
|
val matchingGlass = MINERAL_TYPES[material] ?: throw IllegalArgumentException("No portal color for $this: no glass matches")
|
|
return COLOR_MAPPING[matchingGlass] ?: throw IllegalArgumentException("No portal color for $this: no color matches")
|
|
}
|
|
|
|
fun PortalSaveDataProtos.Portal.toPortal(server: Server, portalTypes: Map<Material, Map<Material, PortalType>>): Portal {
|
|
val mineralMap = portalTypes[mineral.toMaterial()] ?: throw IllegalArgumentException("No portal type for $mineral")
|
|
val effectiveColor = if (color == PortalSaveDataProtos.Portal.Color.UNKNOWN_COLOR) {
|
|
mineral.asColor()
|
|
} else {
|
|
color
|
|
}
|
|
val portalType = mineralMap[effectiveColor.toMaterial()] ?: throw IllegalArgumentException("No portal type for $color")
|
|
return Portal(PortalFrame(lowerLeftFrontCorner.toLocation(server), exitDirection.toDirection()), portalType)
|
|
}
|
|
|
|
class Portal(val frame: PortalFrame, val type: PortalType) {
|
|
var portalSounds: PortalFrame.MakePortalSound? = null
|
|
var portalSparkles: PortalFrame.MakePortalSparkles? = null
|
|
|
|
fun startEffects(plugin: Plugin) {
|
|
stopEffects()
|
|
portalSounds = frame.MakePortalSound()
|
|
portalSparkles = frame.MakePortalSparkles()
|
|
portalSounds?.runTaskTimer(plugin, 0, 120)
|
|
portalSparkles?.runTaskTimer(plugin, 0, 20)
|
|
}
|
|
|
|
fun stopEffects() {
|
|
portalSounds?.cancel()
|
|
portalSparkles?.cancel()
|
|
portalSounds = null
|
|
portalSparkles = null
|
|
}
|
|
|
|
fun toProto(): PortalSaveDataProtos.Portal {
|
|
return PortalSaveDataProtos.Portal.newBuilder()
|
|
.setLowerLeftFrontCorner(frame.lowerLeftFrontCorner.toProto())
|
|
.setExitDirection(frame.direction.toProto())
|
|
.setMineral(frame.mineral.toMineralProto())
|
|
.setColor(frame.color.toColorProto())
|
|
.build()
|
|
}
|
|
} |