Skip to content

Commit

Permalink
⚙️ The code is much cleaner now!
Browse files Browse the repository at this point in the history
  • Loading branch information
AmarokIce committed May 8, 2023
1 parent b2a9405 commit e78cb91
Show file tree
Hide file tree
Showing 49 changed files with 235 additions and 833 deletions.
193 changes: 42 additions & 151 deletions src/main/kotlin/club/someoneice/ovo/core/CoreRunner.kt
Original file line number Diff line number Diff line change
@@ -1,187 +1,78 @@
package club.someoneice.ovo.core

import club.someoneice.ovo.OVOMain
import club.someoneice.ovo.core.`object`.DataList
import club.someoneice.ovo.data.*
import club.someoneice.ovo.json.JsonReaderBean
import club.someoneice.ovo.json.Sandman
import club.someoneice.ovo.json.helper.JsonProcessor
import club.someoneice.ovo.json.reader.Group
import club.someoneice.ovo.json.reader.Item
import club.someoneice.ovo.json.reader.*
import com.google.common.reflect.TypeToken
import com.google.gson.Gson
import java.io.BufferedReader
import java.io.File
import java.io.FileNotFoundException
import java.io.FileReader
import java.util.*
import kotlin.collections.List

class CoreRunner {
private val gson: Gson = Gson()
private val basePath: String = "${System.getProperty("user.dir")}\\ovo"
private val packageInfoFilePath: File = File("$basePath\\ovo_package.json")
private var packagePathList = ArrayList<String>()

private val group = Group()
private val item = Item()
private val food = ItemFoods()
private val gift = ItemGifts()
private val tool = ItemTools()
private val swords = ItemSwords()
private val block = ItemBlocks()
private val recipes = Recipes()

private val JsonProcessor = JsonProcessor(group, item, block, food, gift, swords, tool)
private val group = JsonReaderBean<Group>()
private val item = JsonReaderBean<ItemData>()
private val food = JsonReaderBean<ItemFood>()
private val gift = JsonReaderBean<ItemGift>()
private val tool = JsonReaderBean<ItemTool>()
private val swords = JsonReaderBean<ItemWeapon>()
private val block = JsonReaderBean<BlockData>()

init {
try {
if (!packageInfoFilePath.isFile) {
OVOMain.Logger.info("ovo_package.json is not find!")
Sandman.nullSandman()
} else {
readerPackage()
if (packageInfoFilePath.isFile) {
JsonReaderBean<String>().init(packageInfoFilePath, packagePathList)
init()
}
} else OVOMain.Logger.info("ovo_package.json is not find!")
} catch (_: FileNotFoundException) {
throw FileNotFoundException("package.json is not find!")
Sandman.nullSandman()
}
}

private fun init() {
if (packagePathList.size < 1) return
for (i in packagePathList){
if (!File("${basePath}\\${i}").isDirectory || !File("${basePath}\\${i}\\info.json").isFile) {
continue
} else {
OVOMain.Logger.info("Such package: ${basePath}\\${i}")
val modid: String? = readInfo(File("${basePath}\\${i}\\info.json"))
getFile("${basePath}\\${i}")

DataProcessor(modid)

// Finish read this package, and now clear the DataList.
DataClearer()
}
}
}

private fun getFile(baseFile: String) {
if (File("${baseFile}\\Group.json").isFile) {
group.init(File("${baseFile}\\Group.json"))
}
for (i in packagePathList) {
if (File("${basePath}\\${i}").isDirectory || !File("${basePath}\\${i}\\info.json").isFile) {
OVOMain.Logger.info("Such pockage: ${basePath}\\${i}")
val modid: String? =
(JsonReaderBean<HashMap<String, String>>().readWithOriginal(File("${basePath}\\${i}\\info.json")) as HashMap<String, String>)["modid"]

if (File("${baseFile}\\Item.json").isFile) {
item.getToolType(JsonProcessor, File("${baseFile}\\Item.json"))
} else if (File("${baseFile}\\Item").isDirectory) {
val fileList: Array<File> = File("${baseFile}\\Item").listFiles() as Array<File>
for (i in fileList) {
item.getToolType(JsonProcessor, i)
}
}
// Scanning the OVOPackage! And add all we want!
scanning("${basePath}\\${i}")

if (File("${baseFile}\\ItemFood.json").isFile) {
food.getToolType(JsonProcessor, File("${baseFile}\\ItemFood.json"))
} else if (File("${baseFile}\\ItemFood").isDirectory) {
val fileList: Array<File> = File("${baseFile}\\ItemFood").listFiles() as Array<File>
for (i in fileList) {
food.getToolType(JsonProcessor, i)
}
}

if (File("${baseFile}\\ItemGift.json").isFile) {
gift.getToolType(JsonProcessor, File("${baseFile}\\ItemGift.json"))
} else if (File("${baseFile}\\ItemGift").isDirectory) {
val fileList: Array<File> = File("${baseFile}\\ItemGift").listFiles() as Array<File>
for (i in fileList) {
gift.getToolType(JsonProcessor, i)
}
}

if (File("${baseFile}\\ItemTool.json").isFile) {
tool.getToolType(JsonProcessor, File("${baseFile}\\ItemTool.json"))
} else if (File("${baseFile}\\ItemTool").isDirectory) {
val fileList: Array<File> = File("${baseFile}\\ItemTool").listFiles() as Array<File>
for (i in fileList) {
tool.getToolType(JsonProcessor, i)
}
}

if (File("${baseFile}\\ItemWeapons.json").isFile) {
swords.getToolType(JsonProcessor, File("${baseFile}\\ItemWeapons.json"))
} else if (File("${baseFile}\\ItemWeapons").isDirectory) {
val fileList: Array<File> = File("${baseFile}\\ItemWeapons").listFiles() as Array<File>
for (i in fileList) {
swords.getToolType(JsonProcessor, i)
}
}

if (File("${baseFile}\\Block.json").isFile) {
block.getToolType(JsonProcessor, File("${baseFile}\\Block.json"))
} else if (File("${baseFile}\\Block").isDirectory) {
val fileList: Array<File> = File("${baseFile}\\Block").listFiles() as Array<File>
for (i in fileList) {
block.getToolType(JsonProcessor, i)
}
}

if (!Info.dataMode) return

if (File("${baseFile}\\Recipes.json").isFile) {
recipes.init(File("${baseFile}\\Recipes.json"))
} else if (File("${baseFile}\\Recipes").isDirectory) {
val fileList: Array<File> = File("${baseFile}\\Recipes").listFiles() as Array<File>
for (i in fileList) {
recipes.init(i)
DataProcessor(modid)
DataList.init()
}
}
}

private fun readerPackage() {
val text = StringBuffer()
val buffreader = BufferedReader(FileReader(packageInfoFilePath))

try {
while (true) {
val str = buffreader.readLine()
if (str == null) break else text.append(str.toString())
@OptIn(ExperimentalStdlibApi::class)
private fun scanning(baseFile: String) {
val initPath = File(baseFile)
if (!initPath.exists() || !initPath.isDirectory) return
for (file in initPath.listFiles()!!) {
val name: String = if (file.isDirectory) file.name else file.nameWithoutExtension
when (name.lowercase()) {
"Item".lowercase() -> handle(file, item, DataList.dataItem)
"ItemFood".lowercase() -> handle(file, food, DataList.dataItemFood)
"ItemGift".lowercase() -> handle(file, gift, DataList.dataItemGift)
"ItemTool".lowercase() -> handle(file, tool, DataList.dataItemTool)
"ItemWeapons".lowercase() -> handle(file, swords, DataList.dataItemWeapons)
"Block".lowercase() -> handle(file, block, DataList.dataBlock)
"Group".lowercase() -> {
if (file.isDirectory) continue
group.init(file, DataList.dataGroup)
}
}

buffreader.close()
val output: String = text.toString()
packagePathList = gson.fromJson(output, (object: TypeToken<List<String>> () {} .type) )

} catch (_: Exception) {
Sandman.nullSandman()
}
}

private fun readInfo(infoPath: File): String? {
val text = StringBuffer()
val buffreader = BufferedReader(FileReader(infoPath))

try {
while (true) {
val str = buffreader.readLine()
if (str == null) break else text.append(str.toString())
}

buffreader.close()
val output: String = text.toString()
val infoList: Map<String, String> = gson.fromJson(output, (object: TypeToken<HashMap<String, String>> () {} .type) )
if (infoList.containsKey("data") && infoList["data"] == "true") {
OVOMain.Logger.info("Now will output Data!")
Info.dataMode = true
}

if (infoList.containsKey("modid")) {
OVOMain.Logger.info("Find modid ${infoList["modid"]}")
return infoList["modid"]
}

} catch (_: Exception) {
Sandman.nullSandman()
}

return Sandman.missingNo() as String?
private fun <T> handle(file: File, json: JsonReaderBean<T>, list: ArrayList<T>) {
if (file.isDirectory) for(f in file.listFiles()!!) json.init(f, list)
else json.init(file, list)
}
}
15 changes: 0 additions & 15 deletions src/main/kotlin/club/someoneice/ovo/core/DataClearer.kt

This file was deleted.

16 changes: 5 additions & 11 deletions src/main/kotlin/club/someoneice/ovo/core/DataProcessor.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package club.someoneice.ovo.core

import club.someoneice.ovo.OVOMain
import club.someoneice.ovo.core.`object`.DataList
import club.someoneice.ovo.core.`object`.Info
import club.someoneice.ovo.data.Group
import club.someoneice.ovo.json.data.CoreDataOutput
import club.someoneice.ovo.json.data.helper.RecipeJsonHelper
import club.someoneice.ovo.util.*
import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder
import net.minecraft.block.AbstractBlock
import net.minecraft.block.Material
import net.minecraft.item.FoodComponent
import net.minecraft.item.Item
import net.minecraft.item.ItemGroup
import net.minecraft.item.ItemStack
import net.minecraft.item.Items
import net.minecraft.item.ToolMaterials
import net.minecraft.item.*
import net.minecraft.util.Identifier


Expand All @@ -23,10 +20,6 @@ class DataProcessor(private val mod_id: String?) {
toolGroupProcessing()
toolItemProcessing()
toolBlockProcessing()
if (Info.dataMode) {
recipeDataProcessor()
// TODO - Lang...
}
}

private fun toolInfoProcessing() {
Expand Down Expand Up @@ -86,6 +79,7 @@ class DataProcessor(private val mod_id: String?) {

else -> ToolMaterials.WOOD
}

ItemWeapons(Item.Settings().group(DataList.getGroup[weapons.group]).maxCount(1), weapons, mate)
}
}
Expand All @@ -98,7 +92,7 @@ class DataProcessor(private val mod_id: String?) {
}
}

public fun recipeDataProcessor() {
private fun recipeDataProcessor() {
for (recipe in DataList.dataRecipe) {
CoreDataOutput(RecipeJsonHelper(), recipe)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
package club.someoneice.ovo.core
package club.someoneice.ovo.core.`object`

import club.someoneice.ovo.data.*
import net.minecraft.item.ItemGroup
import java.util.*

object DataList {
var getGroup: HashMap<String, ItemGroup> = HashMap<String, ItemGroup>()

var dataItem = ArrayList<ItemData>()
var dataItemGift = ArrayList<ItemGift>()
var dataItemFood = ArrayList<ItemFoods>()
var dataItemFood = ArrayList<ItemFood>()
var dataItemTool = ArrayList<ItemTool>()
var dataItemWeapons = ArrayList<ItemKnife>()
var dataItemWeapons = ArrayList<ItemWeapon>()

var dataBlock = ArrayList<BlockData>()

var dataGroup = ArrayList<Group>()

var dataRecipe = ArrayList<Recipes>()

fun init() {
dataItem.clear()
dataBlock.clear()
dataGroup.clear()
dataItemFood.clear()
dataItemTool.clear()
dataItemWeapons.clear()
dataItemGift.clear()

dataRecipe.clear()
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package club.someoneice.ovo.core
package club.someoneice.ovo.core.`object`

object Info {
var modid: String = ""
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/club/someoneice/ovo/data/BlockData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import club.someoneice.ovo.IDataGem

data class BlockData(
val name: String,
val hard: Float,
val drop_item: String,
val group: String
val hard: Float = 0.1F,
val drop_item: String = "null",
val group: String = "null"
): IDataGem
4 changes: 2 additions & 2 deletions src/main/kotlin/club/someoneice/ovo/data/ItemData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import club.someoneice.ovo.IDataGem

data class ItemData(
val name: String,
val max_size: Int,
val group: String
val max_size: Int = 64,
val group: String = "null"
): IDataGem
16 changes: 16 additions & 0 deletions src/main/kotlin/club/someoneice/ovo/data/ItemFood.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package club.someoneice.ovo.data

import club.someoneice.ovo.IDataGem

data class ItemFood(
val name: String,
val max_size: Int = 64,
val hunger: Int,
val saturation: Float,
val wolf: Boolean = false,
val always_eat: Boolean = false,
val fast_food: Boolean = false,
val isDrink: Boolean = false,
val return_item: String = "null",
val group: String = "null"
): IDataGem
16 changes: 0 additions & 16 deletions src/main/kotlin/club/someoneice/ovo/data/ItemFoods.kt

This file was deleted.

Loading

0 comments on commit e78cb91

Please sign in to comment.