-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
198 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/kotlin/dev/hybridlabs/aquatic/config/EntitySpawnConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package dev.hybridlabs.aquatic.config | ||
|
||
import com.mojang.serialization.Codec | ||
import com.mojang.serialization.codecs.RecordCodecBuilder | ||
import net.minecraft.entity.EntityType | ||
import net.minecraft.entity.SpawnGroup | ||
import net.minecraft.registry.Registries | ||
import net.minecraft.registry.RegistryKeys | ||
import net.minecraft.registry.tag.TagKey | ||
import net.minecraft.world.biome.Biome | ||
|
||
data class EntitySpawnConfig( | ||
val type: EntityType<*>, | ||
val biomes: TagKey<Biome>, | ||
val group: SpawnGroup, | ||
val weight: Int, | ||
val minGroupSize: Int, | ||
val maxGroupSize: Int, | ||
) { | ||
companion object { | ||
val CODEC: Codec<EntitySpawnConfig> = RecordCodecBuilder.create { instance -> | ||
instance.group( | ||
Registries.ENTITY_TYPE.codec.fieldOf("type").forGetter(EntitySpawnConfig::type), | ||
TagKey.codec(RegistryKeys.BIOME).fieldOf("biomes").forGetter(EntitySpawnConfig::biomes), | ||
SpawnGroup.CODEC.fieldOf("group").forGetter(EntitySpawnConfig::group), | ||
Codec.INT.fieldOf("weight").forGetter(EntitySpawnConfig::weight), | ||
Codec.INT.fieldOf("min_group_size").forGetter(EntitySpawnConfig::minGroupSize), | ||
Codec.INT.fieldOf("max_group_size").forGetter(EntitySpawnConfig::maxGroupSize), | ||
).apply(instance, ::EntitySpawnConfig) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/main/kotlin/dev/hybridlabs/aquatic/config/HybridAquaticConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package dev.hybridlabs.aquatic.config | ||
|
||
import com.mojang.serialization.Codec | ||
import com.mojang.serialization.codecs.RecordCodecBuilder | ||
|
||
data class HybridAquaticConfig( | ||
/** | ||
* The version of the data stored. | ||
* Increase when the config needs to be reset, i.e. when new entity spawn configs are added. | ||
*/ | ||
val dataVersion: Int = 0, | ||
|
||
val entitySpawnConfig: List<EntitySpawnConfig> = EntitySpawnConfigGenerator.generate(), | ||
) { | ||
companion object { | ||
val CODEC: Codec<HybridAquaticConfig> = RecordCodecBuilder.create { instance -> | ||
instance.group( | ||
Codec.INT.fieldOf("data_version").forGetter(HybridAquaticConfig::dataVersion), | ||
EntitySpawnConfig.CODEC.listOf().fieldOf("spawn_configuration").forGetter(HybridAquaticConfig::entitySpawnConfig), | ||
).apply(instance, ::HybridAquaticConfig) | ||
} | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/main/kotlin/dev/hybridlabs/aquatic/config/HybridAquaticConfigHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package dev.hybridlabs.aquatic.config | ||
|
||
import com.google.gson.Gson | ||
import com.google.gson.GsonBuilder | ||
import com.google.gson.JsonElement | ||
import com.google.gson.JsonParser | ||
import com.mojang.datafixers.util.Pair | ||
import com.mojang.serialization.DataResult | ||
import com.mojang.serialization.JsonOps | ||
import java.io.File | ||
|
||
class HybridAquaticConfigHandler(val file: File, val defaultConfig: HybridAquaticConfig = HybridAquaticConfig()) { | ||
val backupFile = file.parentFile.resolve("${file.name}-backup") | ||
|
||
var config: HybridAquaticConfig = defaultConfig | ||
|
||
/** | ||
* Reads the config from the config file. | ||
* @return a data result of the config | ||
*/ | ||
fun read(): DataResult<Pair<HybridAquaticConfig, JsonElement>> { | ||
val json = file.reader().use(JsonParser::parseReader) | ||
return HybridAquaticConfig.CODEC.decode(JsonOps.INSTANCE, json) | ||
} | ||
|
||
/** | ||
* Reads and saves to memory the current config from its file. | ||
* @return whether the config was loaded successfully | ||
*/ | ||
fun load(): Boolean { | ||
val dataResult = read() | ||
val result = dataResult.result() | ||
|
||
if (!result.isPresent) { | ||
return false | ||
} | ||
|
||
config = result.get().first | ||
return true | ||
} | ||
|
||
fun save(): Boolean { | ||
val dataResult = HybridAquaticConfig.CODEC.encodeStart(JsonOps.INSTANCE, config) | ||
val result = dataResult.result() | ||
|
||
if (!result.isPresent) { | ||
return false | ||
} | ||
|
||
val json = result.get() | ||
val text = GSON.toJson(json) | ||
file.writeText(text) | ||
return true | ||
} | ||
|
||
/** | ||
* Backs up the config file. | ||
* @return whether the backup was successful | ||
*/ | ||
fun backup(): Boolean { | ||
return runCatching { | ||
backupFile.writeBytes(file.readBytes()) | ||
}.getOrNull() != null | ||
} | ||
|
||
companion object { | ||
val GSON: Gson = GsonBuilder().setPrettyPrinting().create() | ||
} | ||
} |