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.

46 lines
1.8 KiB

package net.deliciousreya.minecraftportal.model
import net.deliciousreya.minecraftportal.extensions.toLocation
import net.deliciousreya.minecraftportal.extensions.toMaterial
import net.deliciousreya.minecraftportal.extensions.toMineralProto
import net.deliciousreya.minecraftportal.extensions.toProto
import net.deliciousreya.minecraftportal.proto.PortalSaveDataProtos
import org.bukkit.Material
import org.bukkit.Server
import org.bukkit.plugin.Plugin
import org.bukkit.scheduler.BukkitRunnable
import java.lang.IllegalArgumentException
fun PortalSaveDataProtos.Portal.toPortal(server: Server, portalTypes: Map<Material, PortalType>): Portal {
val portalType = portalTypes[mineral.toMaterial()] ?: throw IllegalArgumentException("No portal type for $mineral")
val result = Portal(PortalFrame(lowerLeftFrontCorner.toLocation(server), exitDirection.toDirection()), portalType)
return result
}
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())
.build()
}
}