Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dungeon/Skyblock Extras #61

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions odinclient/src/main/kotlin/me/odinclient/ModCore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import net.minecraftforge.fml.common.Mod.EventHandler
import net.minecraftforge.fml.common.event.*
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.fml.common.gameevent.TickEvent
import kotlinx.coroutines.launch
import me.odinclient.config.ExtrasConfig

@Suppress("UNUSED_PARAMETER")
@Mod(
Expand Down
106 changes: 106 additions & 0 deletions odinclient/src/main/kotlin/me/odinclient/config/ExtrasConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package me.odinclient.config

import com.google.gson.GsonBuilder
import com.google.gson.JsonIOException
import com.google.gson.JsonSyntaxException
import com.google.gson.reflect.TypeToken
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import me.odinmain.OdinMain
import me.odinmain.features.impl.dungeon.DungeonWaypoints
import me.odinmain.utils.render.Color
import java.io.File
import java.io.IOException

/*
TODO: fully add following features
Room/Region Extras:
{
"region/room-name": {
"name-acts-as-id": {
"enabled": true,
"online": "url-if-its-shared-via-a-link",
"preBlocks": {
"minecraft:air": [
"0, 0, 0",
"0, 0, 1"
],
"minecraft:stained_glass[color=white]": [
"0, 0, 2",
"0, 0, 3"
]
}
}
}
}

*/


object ExtrasConfig {
private val gson =
GsonBuilder().registerTypeAdapter(Color::class.java, Color.ColorSerializer()).setPrettyPrinting().create()

var waypoints: MutableMap<String, MutableList<DungeonWaypoints.DungeonWaypoint>> = mutableMapOf()

private val dungeonExtrasConfigFile = File(OdinMain.mc.mcDataDir, "config/odin/extra-config-dungeon.json").apply {
try {
createNewFile()
} catch (e: Exception) {
println("Error initializing module config")
}
}

private val otherExtrasConfigFile = File(OdinMain.mc.mcDataDir, "config/odin/extra-config-other.json").apply {
try {
createNewFile()
} catch (e: Exception) {
println("Error initializing module config")
}
}

fun loadConfig() {
try {
with(dungeonExtrasConfigFile.bufferedReader().use { it.readText() }) {
if (this.isEmpty()) return

waypoints = gson.fromJson(
this,
object : TypeToken<MutableMap<String, MutableList<DungeonWaypoints.DungeonWaypoint>>>() {}.type
)
}
with(otherExtrasConfigFile.bufferedReader().use { it.readText() }) {
if (this.isEmpty()) return

waypoints = gson.fromJson(
this,
object : TypeToken<MutableMap<String, MutableList<DungeonWaypoints.DungeonWaypoint>>>() {}.type
)
}
} catch (e: JsonSyntaxException) {
println("Error parsing configs.")
println(e.message)
e.printStackTrace()
} catch (e: JsonIOException) {
println("Error reading configs.")
}
}

@OptIn(DelicateCoroutinesApi::class)
fun saveConfig() {
GlobalScope.launch(Dispatchers.IO) {
try {
dungeonExtrasConfigFile.bufferedWriter().use {
it.write(gson.toJson(waypoints))
}
otherExtrasConfigFile.bufferedWriter().use {
it.write(gson.toJson(waypoints))
}
} catch (e: IOException) {
println("Error saving Waypoint config.")
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package me.odinclient.utils.extras

import net.minecraft.util.BlockPos

// TODO: an importer from floppa and fme will be added in the future
data class ExtrasData(
// Room or region name because using core would make it harder to update the room search algorithm
val baseName: String,
val extraCategories: MutableList<ExtrasCategory> = mutableListOf()
)

data class ExtrasCategory(
// name that acts as kind of id base id is "base" if the user didn't specify one if a "id" already exists we just add a number to the end
val id: String,
// only creates the blocks if enabled
val enabled: Boolean,
// if online isn't "none" then it will try to load the category from the url
// in the url it should just be a config file in a raw format it will then try to search for the id in the config file
// and if it finds it will load/update the category
// also by using a command u can fully import it from an url
val online: String,
// blockStateString, list of blockPos
val extras: MutableMap<String, MutableSet<BlockPos>> = mutableMapOf()
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package me.odinclient.utils.extras

import net.minecraft.block.Block
import net.minecraft.block.properties.IProperty
import net.minecraft.block.state.IBlockState

object ExtrasHelper {
fun getStateFromString(blockStateString: String): IBlockState? {
if (!blockStateString.contains("[")) {
return Block.getBlockFromName(blockStateString).defaultState
}

val parts = blockStateString.split("\\[|\\]".toRegex()).dropLastWhile { it.isEmpty() }
.toTypedArray()
val block = Block.getBlockFromName(parts[0]) ?: return null

val blockState = arrayOf(block.defaultState)
for (property in parts[1].split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()) {
val propertyParts = property.split("=".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
blockState[0].properties.keys.stream()
.filter { prop: IProperty<*> ->
prop.name == propertyParts[0]
}
.findFirst()
.ifPresent { prop: IProperty<*> ->
prop.allowedValues.stream()
.filter { value -> value.toString() == propertyParts[1] }
.findFirst()
.ifPresent { value ->
blockState[0] = blockState[0]
.withProperty(prop, value)
}
}
}
return blockState[0]
}
}

private fun IBlockState.withProperty(iProperty: IProperty<*>, value: Comparable<Nothing>?): IBlockState? {
TODO("Not yet implemented")
}
2 changes: 1 addition & 1 deletion odinmain/src/main/kotlin/me/odinmain/OdinMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ object OdinMain {
config.mkdirs()
}
launch { WaypointConfig.loadConfig() }
launch { DungeonWaypointConfigCLAY.loadConfig() }
launch { DungeonWaypointConfig.loadConfig() }
}

@OptIn(DelicateCoroutinesApi::class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package me.odinmain.config

import com.google.gson.GsonBuilder
import com.google.gson.JsonIOException
import com.google.gson.JsonSyntaxException
import com.google.gson.reflect.TypeToken
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import me.odinmain.OdinMain.mc
import me.odinmain.features.impl.dungeon.DungeonWaypoints.DungeonWaypoint
import me.odinmain.features.impl.dungeon.DungeonWaypoints.WaypointCategory
import me.odinmain.utils.render.Color
import java.io.File
import java.io.IOException

object DungeonWaypointConfig {
private val gson =
GsonBuilder().registerTypeAdapter(Color::class.java, Color.ColorSerializer()).setPrettyPrinting().create()

var waypoints: MutableMap<String, MutableList<DungeonWaypoint>> = mutableMapOf()

var waypointsRooms: MutableMap<String, MutableList<WaypointCategory>> = mutableMapOf()
var waypointRegions: MutableMap<String, MutableList<WaypointCategory>> = mutableMapOf()


private val configFileRooms = File(mc.mcDataDir, "config/odin/dungeon-waypoint-config-rooms.json").apply {
try {
createNewFile()
} catch (e: Exception) {
println("Error initializing module config")
}
}

private val configFileOther = File(mc.mcDataDir, "config/odin/dungeon-waypoint-config-other.json").apply {
try {
createNewFile()
} catch (e: Exception) {
println("Error initializing module config")
}
}

fun loadConfig() {
try {
with(configFileRooms.bufferedReader().use { it.readText() }) {
if (this.isEmpty()) return

waypointsRooms = gson.fromJson(
this,
object : TypeToken<MutableMap<String, MutableList<WaypointCategory>>>() {}.type
)
}
with(configFileOther.bufferedReader().use { it.readText() }) {
if (this.isEmpty()) return

waypointRegions = gson.fromJson(
this,
object : TypeToken<MutableMap<String, MutableList<WaypointCategory>>>() {}.type
)
}
} catch (e: JsonSyntaxException) {
println("Error parsing configs.")
println(e.message)
e.printStackTrace()
} catch (e: JsonIOException) {
println("Error reading configs.")
}
}

@OptIn(DelicateCoroutinesApi::class)
fun saveConfig() {
GlobalScope.launch(Dispatchers.IO) {
try {
configFileRooms.bufferedWriter().use {
it.write(gson.toJson(waypointsRooms))
}
configFileOther.bufferedWriter().use {
it.write(gson.toJson(waypointRegions))
}
} catch (e: IOException) {
println("Error saving Waypoint config.")
}
}
}
}

This file was deleted.

Loading