|
|
@ -1,8 +1,12 @@ |
|
|
|
package net.deliciousreya.minecraftportal.model |
|
|
|
package net.deliciousreya.minecraftportal.model |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.google.common.collect.ImmutableList |
|
|
|
import com.google.common.collect.ImmutableMap |
|
|
|
import com.google.common.collect.ImmutableMap |
|
|
|
import com.google.protobuf.InvalidProtocolBufferException |
|
|
|
import com.google.protobuf.InvalidProtocolBufferException |
|
|
|
|
|
|
|
import net.deliciousreya.minecraftportal.extensions.COLOR_MAPPING |
|
|
|
import net.deliciousreya.minecraftportal.extensions.MINERAL_MAPPING |
|
|
|
import net.deliciousreya.minecraftportal.extensions.MINERAL_MAPPING |
|
|
|
|
|
|
|
import net.deliciousreya.minecraftportal.extensions.toColorProto |
|
|
|
|
|
|
|
import net.deliciousreya.minecraftportal.extensions.toMaterial |
|
|
|
import net.deliciousreya.minecraftportal.proto.PortalSaveDataProtos |
|
|
|
import net.deliciousreya.minecraftportal.proto.PortalSaveDataProtos |
|
|
|
import org.bukkit.Location |
|
|
|
import org.bukkit.Location |
|
|
|
import org.bukkit.Material |
|
|
|
import org.bukkit.Material |
|
|
@ -16,17 +20,27 @@ import java.util.logging.Logger |
|
|
|
class PortalDataStore (){ |
|
|
|
class PortalDataStore (){ |
|
|
|
var saveDataTo: File? = null |
|
|
|
var saveDataTo: File? = null |
|
|
|
var useBackup: File? = null |
|
|
|
var useBackup: File? = null |
|
|
|
val portalTypes: ImmutableMap<Material, PortalType> |
|
|
|
private val userColors: MutableMap<String, Material> = mutableMapOf() |
|
|
|
|
|
|
|
private val portalTypes: ImmutableMap<Material, ImmutableMap<Material, PortalType>> |
|
|
|
|
|
|
|
private val allPortals: ImmutableList<PortalType> |
|
|
|
init { |
|
|
|
init { |
|
|
|
val portalTypesBuilder = ImmutableMap.builder<Material, PortalType>() |
|
|
|
val allPortalsBuilder = ImmutableList.builder<PortalType>() |
|
|
|
|
|
|
|
val portalTypesBuilder = ImmutableMap.builder<Material, ImmutableMap<Material, PortalType>>() |
|
|
|
for (mineralType in MINERAL_MAPPING.keys) { |
|
|
|
for (mineralType in MINERAL_MAPPING.keys) { |
|
|
|
portalTypesBuilder.put(mineralType, PortalType(mineralType)) |
|
|
|
val colors = ImmutableMap.Builder<Material, PortalType>() |
|
|
|
|
|
|
|
for (color in COLOR_MAPPING.keys) { |
|
|
|
|
|
|
|
val portalType = PortalType(mineralType, color) |
|
|
|
|
|
|
|
colors.put(color, portalType) |
|
|
|
|
|
|
|
allPortalsBuilder.add(portalType) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
portalTypesBuilder.put(mineralType, colors.build()) |
|
|
|
} |
|
|
|
} |
|
|
|
portalTypes = portalTypesBuilder.build() |
|
|
|
portalTypes = portalTypesBuilder.build() |
|
|
|
|
|
|
|
allPortals = allPortalsBuilder.build() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun isLocationInPortalOrChamber(location: Location): PortalFrame? { |
|
|
|
fun isLocationInPortalOrChamber(location: Location): PortalFrame? { |
|
|
|
for (type in this.portalTypes.values) { |
|
|
|
for (type in this.allPortals) { |
|
|
|
if (type.isEmpty) { |
|
|
|
if (type.isEmpty) { |
|
|
|
continue |
|
|
|
continue |
|
|
|
} |
|
|
|
} |
|
|
@ -39,7 +53,7 @@ class PortalDataStore (){ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun isLocationInPortalChamber(location: Location): PortalFrame? { |
|
|
|
fun isLocationInPortalChamber(location: Location): PortalFrame? { |
|
|
|
for (type in this.portalTypes.values) { |
|
|
|
for (type in this.allPortals) { |
|
|
|
if (type.isEmpty) { |
|
|
|
if (type.isEmpty) { |
|
|
|
continue |
|
|
|
continue |
|
|
|
} |
|
|
|
} |
|
|
@ -90,7 +104,7 @@ class PortalDataStore (){ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun validateWorldOnStartup(logger: Logger) { |
|
|
|
fun validateWorldOnStartup(logger: Logger) { |
|
|
|
for (portalType in this.portalTypes.values) { |
|
|
|
for (portalType in this.allPortals) { |
|
|
|
val oldPortal = portalType.oldestActivePortal |
|
|
|
val oldPortal = portalType.oldestActivePortal |
|
|
|
val newPortal = portalType.newestActivePortal |
|
|
|
val newPortal = portalType.newestActivePortal |
|
|
|
if (newPortal != null && !validatePortal(newPortal)) { |
|
|
|
if (newPortal != null && !validatePortal(newPortal)) { |
|
|
@ -106,11 +120,11 @@ class PortalDataStore (){ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun validatePortal(portal: Portal): Boolean { |
|
|
|
fun validatePortal(portal: Portal): Boolean { |
|
|
|
return portal.frame.mineral == portal.type.mineral && portal.frame.isActivePortal() |
|
|
|
return portal.frame.mineral == portal.type.mineral && portal.frame.color == portal.type.color && portal.frame.isActivePortal() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun launchEffectsOnStartup(plugin: Plugin) { |
|
|
|
fun launchEffectsOnStartup(plugin: Plugin) { |
|
|
|
for (portalType in this.portalTypes.values) { |
|
|
|
for (portalType in this.allPortals) { |
|
|
|
startEffectsFor(portalType, plugin) |
|
|
|
startEffectsFor(portalType, plugin) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -174,7 +188,7 @@ class PortalDataStore (){ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun getPortalType(frame: PortalFrame): PortalType { |
|
|
|
fun getPortalType(frame: PortalFrame): PortalType { |
|
|
|
return portalTypes[frame.mineral] ?: throw IllegalArgumentException("There are no portals of type ${frame.mineral}") |
|
|
|
return portalTypes[frame.mineral]?.get(frame.color) ?: throw IllegalArgumentException("There are no portals of type ${frame.mineral}/${frame.color}") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** Gets the other portal, if the given frame is one of the portals in a pair. */ |
|
|
|
/** Gets the other portal, if the given frame is one of the portals in a pair. */ |
|
|
@ -185,7 +199,7 @@ class PortalDataStore (){ |
|
|
|
|
|
|
|
|
|
|
|
/** Forgets all the data without actually affecting the world. */ |
|
|
|
/** Forgets all the data without actually affecting the world. */ |
|
|
|
fun clear() { |
|
|
|
fun clear() { |
|
|
|
for (portalType in this.portalTypes.values) { |
|
|
|
for (portalType in this.allPortals) { |
|
|
|
portalType.oldestActivePortal?.stopEffects() |
|
|
|
portalType.oldestActivePortal?.stopEffects() |
|
|
|
portalType.newestActivePortal?.stopEffects() |
|
|
|
portalType.newestActivePortal?.stopEffects() |
|
|
|
portalType.clear() |
|
|
|
portalType.clear() |
|
|
@ -206,11 +220,14 @@ class PortalDataStore (){ |
|
|
|
} |
|
|
|
} |
|
|
|
olderPortal.type.loadPairedPortals(olderPortal, newerPortal) |
|
|
|
olderPortal.type.loadPairedPortals(olderPortal, newerPortal) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
for (userData in proto.userDataList) { |
|
|
|
|
|
|
|
userColors[userData.name] = userData.color.toMaterial() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun toProto(): PortalSaveDataProtos.PortalSaveData { |
|
|
|
fun toProto(): PortalSaveDataProtos.PortalSaveData { |
|
|
|
val builder = PortalSaveDataProtos.PortalSaveData.newBuilder() |
|
|
|
val builder = PortalSaveDataProtos.PortalSaveData.newBuilder() |
|
|
|
for (portalType in this.portalTypes.values) { |
|
|
|
for (portalType in this.allPortals) { |
|
|
|
when { |
|
|
|
when { |
|
|
|
portalType.isEmpty -> { |
|
|
|
portalType.isEmpty -> { |
|
|
|
// Nothing here to save. Just skip it. |
|
|
|
// Nothing here to save. Just skip it. |
|
|
@ -223,6 +240,9 @@ class PortalDataStore (){ |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
for (user in this.userColors.keys) { |
|
|
|
|
|
|
|
builder.addUserData(PortalSaveDataProtos.UserData.newBuilder().setName(user).setColor(this.userColors[user]?.toColorProto())) |
|
|
|
|
|
|
|
} |
|
|
|
return builder.build() |
|
|
|
return builder.build() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -233,4 +253,19 @@ class PortalDataStore (){ |
|
|
|
fun stopTeleporting(portal: PortalFrame) { |
|
|
|
fun stopTeleporting(portal: PortalFrame) { |
|
|
|
getPortalType(portal).cancelTeleportation() |
|
|
|
getPortalType(portal).cancelTeleportation() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private val defaultColor = Material.GRAY_STAINED_GLASS |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun getColorFor(name: String): Material { |
|
|
|
|
|
|
|
return userColors.getOrDefault(name, defaultColor) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun setColorFor(name: String, color: Material) { |
|
|
|
|
|
|
|
if (color == defaultColor) { |
|
|
|
|
|
|
|
userColors.remove(name) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
userColors[name] = color |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
onAfterChanged() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |