Skip to content

Commit

Permalink
🍍 PineappleRecipeBook make we can read json from Jar!
Browse files Browse the repository at this point in the history
  • Loading branch information
AmarokIce committed Jul 31, 2023
1 parent b726c06 commit 860d23a
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions src/main/kotlin/club/someoneice/ovo/core/JarRunner.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package club.someoneice.ovo.core

import club.someoneice.ovo.core.`object`.DataList
import club.someoneice.ovo.util.gson
import club.someoneice.togocup.recipebook.JarUtil
import com.google.common.collect.Lists
import com.google.common.collect.Maps
import com.google.common.collect.Sets
import com.google.gson.reflect.TypeToken

class JarRunner {
companion object {
val set: HashSet<String> = Sets.newHashSet(
"Item".lowercase(),
"ItemFood".lowercase(),
"ItemGift".lowercase(),
"ItemTool".lowercase(),
"ItemWeapons".lowercase(),
"Block".lowercase(),
"Recipe".lowercase(),
"DeleteRecipe".lowercase(),
"Biomes".lowercase(),
"Group".lowercase()
)
}

init {
val mappings: HashMap<String, ArrayList<JarUtil.UrlBuffered>> = Maps.newHashMap()
val infoMappings: HashMap<String, JarUtil.UrlBuffered> = Maps.newHashMap()
fun <E>ArrayList<E>.addElement(e: E): ArrayList<E> {
this.add(e)
return this;
}

for (buffered in JarUtil.getInstance().dataSet) {
val urlName = buffered.fileUrl;
if (!urlName.contains("ovo/")) continue
val name = urlName.replace("ovo/", "").substring(0, urlName.replace("ovo/", "").indexOf("/"))
mappings[name] = mappings.getOrDefault(name, Lists.newArrayList()).addElement(buffered)
if (urlName.contains("info.json")) infoMappings[name] = buffered
}

for (key in mappings.keys) {
val mapping: ArrayList<JarUtil.UrlBuffered> = mappings[key]!!
if (mapping.isNotEmpty() && infoMappings.containsKey(key)) {
val type = object: TypeToken<HashMap<String, String>>() {}.type
val modid: String? =
(gson.fromJson(JarUtil.getInstance().readFileFromUrl(infoMappings[key]), type) as HashMap<String, String>).let { it["modid"] }
for (url in mappings[key]!!) {
if (url == infoMappings[key]) continue
val fileName =
url.fileUrl.substring(url.fileUrl.lastIndexOf("/") + 1, url.fileUrl.lastIndexOf("."))
(
if (set.contains(fileName.lowercase())) fileName
else {
val path = url.fileUrl.replace("ovo/${key}/", "")
val filePath = path.substring(0, path.indexOf("/"))
if (set.contains(filePath.lowercase())) filePath
else null
}
)?.let {
val txt = JarUtil.getInstance().readFileFromUrl(url) ?: return@let
when (it.lowercase()) {
"Item".lowercase() -> CoreRunner.item.init (txt, DataList.dataItem)
"ItemFood".lowercase() -> CoreRunner.food.init (txt, DataList.dataItemFood)
"ItemGift".lowercase() -> CoreRunner.gift.init (txt, DataList.dataItemGift)
"ItemTool".lowercase() -> CoreRunner.tool.init (txt, DataList.dataItemTool)
"ItemWeapons".lowercase() -> CoreRunner.swords.init (txt, DataList.dataItemWeapons)
"Block".lowercase() -> CoreRunner.block.init (txt, DataList.dataBlock)
"Recipe".lowercase() -> CoreRunner.recipes.init (txt, DataList.dataRecipes)
"DeleteRecipe".lowercase() -> CoreRunner.delete_recipes.init (txt, DataList.dataDeleteRecipes)
"Biomes".lowercase() -> CoreRunner.biomes.init (txt, DataList.dataBiomes)
"Group".lowercase() -> CoreRunner.group.init (txt, DataList.dataGroup)
}
}
}

DataProcessor(modid).init()
DataList.init()
}
}
}
}

0 comments on commit 860d23a

Please sign in to comment.