Compare commits

...
7 changed files with 167 additions and 0 deletions
+2
View File
@@ -200,6 +200,8 @@
- Added molang particle queries for getting distance to targeted entities
- Added debug renderer for poseable entity locators
- Added time zone as a config option
- Added optional boolean to use time zone for MoLang function "date_local_time" and "date_of"
- Added "irlDateRange" and "irlTimeRange" as a condition for spawning
- Added an optional MoValue to either use system timezone when set as true, or use the supplied string as a timezone for MoLang functions `date_local_time` and `date_of`. If the timezone supplied fails to parse this function now returns `0.0`.
### Additions
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2023 Cobblemon Contributors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package com.cobblemon.mod.common.api.spawning
import java.time.LocalDate
open class IrlDateRange (
val startDate: LocalDate,
val stopDate: LocalDate? = null
) {
operator fun contains(date: LocalDate): Boolean {
return if(stopDate != null) {
(startDate.isEqual(date) || startDate.isBefore(date)) && (stopDate.isAfter(date) || stopDate.isEqual(date))
} else {
startDate.isEqual(date)
}
}
}
@@ -0,0 +1,20 @@
/*
* Copyright (C) 2023 Cobblemon Contributors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package com.cobblemon.mod.common.api.spawning
import java.time.LocalTime
open class IrlTimeRange(
val startTime: LocalTime,
val stopTime: LocalTime
) {
operator fun contains(time: LocalTime): Boolean {
return (time.isAfter(startTime) || time == startTime) && time.isBefore(stopTime)
}
}
@@ -74,6 +74,8 @@ object SpawnLoader {
.registerTypeAdapter(NPCClass::class.java, NPCClassReferenceAdapter)
.registerTypeAdapter(Expression::class.java, ExpressionAdapter)
.registerTypeAdapter(ExpressionLike::class.java, ExpressionLikeAdapter)
.registerTypeAdapter(IrlTimeRange::class.java, IrlTimeRangeAdapter)
.registerTypeAdapter(IrlDateRange::class.java, IrlDateRangeAdapter)
.create()
var deserializingConditionClass: Class<out SpawningCondition<*>>? = null
@@ -8,7 +8,10 @@
package com.cobblemon.mod.common.api.spawning.condition
import com.cobblemon.mod.common.Cobblemon
import com.cobblemon.mod.common.api.conditional.RegistryLikeCondition
import com.cobblemon.mod.common.api.spawning.IrlDateRange
import com.cobblemon.mod.common.api.spawning.IrlTimeRange
import com.cobblemon.mod.common.api.spawning.MoonPhaseRange
import com.cobblemon.mod.common.api.spawning.TimeRange
import com.cobblemon.mod.common.api.spawning.position.SpawnablePosition
@@ -21,6 +24,8 @@ import net.minecraft.tags.TagKey
import net.minecraft.world.level.biome.Biome
import net.minecraft.world.level.levelgen.WorldgenRandom
import net.minecraft.world.level.levelgen.structure.Structure
import java.time.LocalDate
import java.time.LocalTime
/**
* The root of spawning conditions that can be applied to a spawnable position. What type
@@ -58,6 +63,8 @@ abstract class SpawningCondition<T : SpawnablePosition> {
var timeRange: TimeRange? = null
var structures: MutableList<Either<ResourceLocation, TagKey<Structure>>>? = null
var isSlimeChunk: Boolean? = null
var irlTimeRange: IrlTimeRange? = null
var irlDateRange: IrlDateRange? = null
var markers: MutableList<String>? = null
@Transient
@@ -129,6 +136,10 @@ abstract class SpawningCondition<T : SpawnablePosition> {
val random = Random(seed)
return random.nextInt(10) == 0*/
} else if (irlTimeRange != null && !irlTimeRange!!.contains(LocalTime.now(Cobblemon.config.timeZoneId))){
return false
} else if (irlDateRange != null && !irlDateRange!!.contains(LocalDate.now(Cobblemon.config.timeZoneId))){
return false
}
return true
@@ -160,6 +171,10 @@ abstract class SpawningCondition<T : SpawnablePosition> {
return false
if (structures != null && structures!!.any { it == null })
return false
if(irlTimeRange != null && irlTimeRange!!.stopTime.isBefore(irlTimeRange!!.startTime))
return false
if(irlDateRange != null && irlDateRange!!.stopDate != null && irlDateRange!!.stopDate!!.isBefore(irlDateRange!!.startDate))
return false
return true
}
}
@@ -0,0 +1,59 @@
/*
* Copyright (C) 2023 Cobblemon Contributors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package com.cobblemon.mod.common.util.adapters
import com.cobblemon.mod.common.api.spawning.IrlDateRange
import com.google.gson.JsonDeserializationContext
import com.google.gson.JsonDeserializer
import com.google.gson.JsonElement
import com.google.gson.JsonPrimitive
import com.google.gson.JsonSerializationContext
import com.google.gson.JsonSerializer
import java.lang.reflect.Type
import java.time.LocalDate
import java.time.format.DateTimeFormatter
object IrlDateRangeAdapter: JsonSerializer<IrlDateRange>, JsonDeserializer<IrlDateRange> {
val dateFormat: DateTimeFormatter = DateTimeFormatter.ofPattern("M/d")
//parsing dates requires a year instead of just assuming this year so we do jank
val dateFormatWithYear: DateTimeFormatter = DateTimeFormatter.ofPattern("uuuu/M/d")
override fun serialize(
src: IrlDateRange,
typeOfSrc: Type,
context: JsonSerializationContext
): JsonElement {
return if(src.stopDate != null){
JsonPrimitive(src.startDate.format(dateFormat) + "-" + src.stopDate.format(dateFormat))
} else {
JsonPrimitive(src.startDate.format(dateFormat))
}
}
override fun deserialize(
json: JsonElement,
typeOfT: Type,
context: JsonDeserializationContext
): IrlDateRange {
val year = LocalDate.now().year.toString()
return if(json.asString.contains("-")){
val dateStrings = json.asString.split("-")
IrlDateRange(
startDate = LocalDate.parse(year + "/" + dateStrings[0], dateFormatWithYear),
stopDate = LocalDate.parse(year + "/" + dateStrings[1], dateFormatWithYear)
)
} else {
IrlDateRange(
startDate = LocalDate.parse(year + "/" + json.asString, dateFormatWithYear)
)
}
}
}
@@ -0,0 +1,45 @@
/*
* Copyright (C) 2023 Cobblemon Contributors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package com.cobblemon.mod.common.util.adapters
import com.cobblemon.mod.common.api.spawning.IrlTimeRange
import com.google.gson.JsonDeserializationContext
import com.google.gson.JsonDeserializer
import com.google.gson.JsonElement
import com.google.gson.JsonPrimitive
import com.google.gson.JsonSerializationContext
import com.google.gson.JsonSerializer
import java.lang.reflect.Type
import java.time.LocalTime
import java.time.format.DateTimeFormatter
object IrlTimeRangeAdapter: JsonSerializer<IrlTimeRange>, JsonDeserializer<IrlTimeRange> {
val timeFormat: DateTimeFormatter = DateTimeFormatter.ofPattern("H:mm")
override fun serialize(
src: IrlTimeRange,
typeOfSrc: Type,
context: JsonSerializationContext
): JsonElement {
return JsonPrimitive(src.startTime.format(timeFormat) + "-" + src.stopTime.format(timeFormat))
}
override fun deserialize(
json: JsonElement,
typeOfT: Type,
context: JsonDeserializationContext
): IrlTimeRange {
val timeStrings = json.asString.split("-")
return IrlTimeRange(
startTime = LocalTime.parse(timeStrings[0], timeFormat),
stopTime = LocalTime.parse(timeStrings[1], timeFormat)
)
}
}