Skip to content

Commit

Permalink
make entity class open
Browse files Browse the repository at this point in the history
  • Loading branch information
12rcu committed May 23, 2024
1 parent 099e034 commit e2556d9
Show file tree
Hide file tree
Showing 10 changed files with 394 additions and 94 deletions.
254 changes: 251 additions & 3 deletions src/main/kotlin/com/lop/devtools/monstera/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

package com.lop.devtools.monstera

import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.lop.devtools.monstera.addon.dev.ResourceLoader
import com.lop.devtools.monstera.addon.entity.resource.copyDefaultTextureTo
import com.lop.devtools.monstera.addon.entity.resource.generateDefaultGeo
Expand All @@ -13,15 +15,224 @@ import java.nio.file.Path
import java.util.*
import java.util.concurrent.TimeUnit
import kotlin.io.path.Path
import kotlin.io.path.absolutePathString

@DslMarker
annotation class ConfigDSL

@ConfigDSL
@Deprecated(
"Use a config file, this function does not correcly apply config data!",
ReplaceWith("loadConfig()", "com.lop.devtools.monstera.Config")
)
fun config(projectName: String, data: Config.() -> Unit): Config {
return Config(projectName).apply(data)
}

@ConfigDSL
fun loadConfig(
conf: String = "${System.getProperty("user.dir")}/monstera.json",
local: String = "${System.getProperty("user.dir")}/monstera-local.json"
) = loadConfig(File(conf), File(local))

@ConfigDSL
fun loadConfig(
conf: File,
local: File
): Result<Config> {
if (!conf.exists()) {
interactiveConfig(conf)
}
if(!local.exists()) {
createLocalFile(local)
}

try {
val gson = Gson()
val monsteraConfig: MonsteraConfig = gson.fromJson(conf.readText(), MonsteraConfig::class.java)
val monsteraLocalConfig: MonsteraLocalConfig =
if (local.exists())
gson.fromJson(local.readText(), MonsteraLocalConfig::class.java)
else
MonsteraLocalConfig()

val behPath = Path(
System.getProperty("user.dir"),
monsteraLocalConfig.buildPath,
"development_behavior_packs",
monsteraConfig.projectShort.uppercase() + "_BP"
)
val resPath = Path(
System.getProperty("user.dir"),
monsteraLocalConfig.buildPath,
"development_resource_packs",
monsteraConfig.projectShort.uppercase() + "_RP"
)

val config = Config(
projectName = monsteraConfig.name,
namespace = monsteraConfig.namespace,
projectShort = monsteraConfig.projectShort,
description = monsteraConfig.description,
version = monsteraConfig.version,
authors = monsteraConfig.authors,
world = monsteraConfig.world?.let { File(System.getProperty("user.dir"), it) } ?: File(),
worldUUID = UUID.fromString(monsteraConfig.worldUUID),
worldModuleUUID = UUID.fromString(monsteraConfig.worldModuleUUID),
behUUID = UUID.fromString(monsteraConfig.behUUID),
behModUUID = UUID.fromString(monsteraConfig.behModuleUUID),
behScriptUUID = UUID.fromString(monsteraConfig.scriptUUID),
resUUID = UUID.fromString(monsteraConfig.resUUID),
resModUUID = UUID.fromString(monsteraConfig.resModuleUUID),
targetMcVersion = monsteraConfig.targetMcVersion,
hashFileNames = monsteraConfig.hashFileNames,
behPath = behPath,
resPath = resPath,
scriptingVersion = monsteraConfig.scriptingVersion ?: "1.8.0",

comMojangPath = monsteraLocalConfig.minecraftDir?.let { Path(it) }
?: Path(
System.getenv("LOCALAPPDATA") ?: "",
"Packages",
"Microsoft.MinecraftUWP_8wekyb3d8bbwe",
"LocalState",
"games",
"com.mojang"
),
paths = Config.AddonPaths(
behBase = behPath,
resBase = resPath,
behEntity = behPath.resolve(monsteraConfig.minecraftPaths.behEntity),
behAnimController = behPath.resolve(monsteraConfig.minecraftPaths.behAnimController),
behAnim = behPath.resolve(monsteraConfig.minecraftPaths.behAnim),
behRecipe = behPath.resolve(monsteraConfig.minecraftPaths.behRecipes),
behBlocks = behPath.resolve(monsteraConfig.minecraftPaths.behBlocks),
behItems = behPath.resolve(monsteraConfig.minecraftPaths.behItems),
behSpawnRules = behPath.resolve(monsteraConfig.minecraftPaths.behSpawnRules),
behTrading = behPath.resolve(monsteraConfig.minecraftPaths.behTrading),
behTexts = behPath.resolve(monsteraConfig.minecraftPaths.behTexts),
behScripts = behPath.resolve(monsteraConfig.minecraftPaths.behScripts),
behMcFunction = behPath.resolve(monsteraConfig.minecraftPaths.behMcFunction),
resAnim = resPath.resolve(monsteraConfig.minecraftPaths.resAnim),
resItem = resPath.resolve(monsteraConfig.minecraftPaths.resItem),
resModels = resPath.resolve(monsteraConfig.minecraftPaths.resModels),
resMaterials = resPath.resolve(monsteraConfig.minecraftPaths.resMaterials),
resParticles = resPath.resolve(monsteraConfig.minecraftPaths.resParticles),
resRenderControllers = resPath.resolve(monsteraConfig.minecraftPaths.resRenderControllers),
resSounds = resPath.resolve(monsteraConfig.minecraftPaths.resSounds),
resTextures = resPath.resolve(monsteraConfig.minecraftPaths.resTextures),
resAttachable = resPath.resolve(monsteraConfig.minecraftPaths.resAttachable),
resTexts = resPath.resolve(monsteraConfig.minecraftPaths.resTexts),
),
formatVersions = Config.FormatVersions(
getVersionAsString(monsteraConfig.targetMcVersion),
behEntity = monsteraConfig.minecraftFormatVersions.behEntity,
resEntity = monsteraConfig.minecraftFormatVersions.resEntity,
resItem = monsteraConfig.minecraftFormatVersions.resItem,
behItem = monsteraConfig.minecraftFormatVersions.behItem,
behAnimation = monsteraConfig.minecraftFormatVersions.behAnim,
behBlock = monsteraConfig.minecraftFormatVersions.behBlock,
behRecipe = monsteraConfig.minecraftFormatVersions.behRecipe,
behSpawnRules = monsteraConfig.minecraftFormatVersions.behSpawnRule,
behAnimController = monsteraConfig.minecraftFormatVersions.behAnimController,
resAnimController = monsteraConfig.minecraftFormatVersions.resAnimController,
resAttachable = monsteraConfig.minecraftFormatVersions.resAttachable,
resSoundDefs = monsteraConfig.minecraftFormatVersions.resSoundDefs,
resRendercontroller = monsteraConfig.minecraftFormatVersions.resRenderController,
),
langFileBuilder = Config.AddonLangFileBuilders(behPath, resPath)
)
monsteraConfig.packIcon?.let {
config.packIcon = File(System.getProperty("user.dir"), it)
}

return Result.success(config)
} catch (e: Exception) {
return Result.failure(e)
}
}

class MonsteraConfig(
var name: String,
var projectShort: String,
var description: String = "",
var namespace: String = "monstera",
var version: MutableList<Int> = mutableListOf(0, 1, 0),
var authors: MutableList<String> = mutableListOf(),
var world: String? = null,
var worldUUID: String = UUID.randomUUID().toString(),
var worldModuleUUID: String = UUID.randomUUID().toString(),
var behUUID: String = UUID.randomUUID().toString(),
var behModuleUUID: String = UUID.randomUUID().toString(),
var scriptUUID: String = UUID.randomUUID().toString(),
var resUUID: String = UUID.randomUUID().toString(),
var resModuleUUID: String = UUID.randomUUID().toString(),
var targetMcVersion: MutableList<Int> = mutableListOf(1, 20, 81),
var scriptingVersion: String? = null,
var scriptEntryFile: String? = null,
var packIcon: String? = null,
var hashFileNames: Boolean = false,
var minecraftPaths: MinecraftAddonPaths = MinecraftAddonPaths(),
var minecraftFormatVersions: MinecraftFormatVersions = MinecraftFormatVersions(),
)

class MinecraftAddonPaths(
var behEntity: String = "entities",
var behAnimController: String = "animation_controllers",
var behAnim: String = "animations",
var behBlocks: String = "blocks",
var behItems: String = "items",
var lootTableEntity: String = "loot_tables/entities",
var lootTableBlock: String = "loot_tables/blocks",
var behSpawnRules: String = "spawn_rules",
var behTrading: String = "trading/economy_trades",
var behRecipes: String = "recipes",
var behTexts: String = "texts",
var behScripts: String = "scripts",
var behMcFunction: String = "functions",
var resAnim: String = "animations",
var resAnimController: String = "animation_controllers",
var resEntity: String = "entity",
var resItem: String = "items",
var resModels: String = "models",
var resMaterials: String = "materials",
var resParticles: String = "particles",
var resRenderControllers: String = "render_controllers",
var resSounds: String = "sounds",
var resTextures: String = "textures",
var resAttachable: String = "attachables",
var resTexts: String = "texts"
)

class MinecraftFormatVersions(
var behEntity: String = "1.20.81",
var behItem: String = "1.10.0",
var behAnim: String = "1.8.0",
var behBlock: String = "1.20.81",
var behRecipe: String = "1.17.41",
var behSpawnRule: String = "1.8.0",
var behAnimController: String = "1.10.0",
var resEntity: String = "1.10.0",
var resItem: String = "1.10.0",
var resAnimController: String = "1.10.0",
var resAttachable: String = "1.10.0",
var resSoundDefs: String = "1.14.0",
var resRenderController: String = "1.10.0"
)

class MonsteraLocalConfig(
var projectPath: String? = null,
var buildPath: String = "build",
var minecraftDir: String? = Path(
System.getenv("LOCALAPPDATA") ?: "",
"Packages",
"Microsoft.MinecraftUWP_8wekyb3d8bbwe",
"LocalState",
"games",
"com.mojang"
).absolutePathString()
)

class Config(
val projectName: String,
var namespace: String = "monstera",
Expand Down Expand Up @@ -49,11 +260,16 @@ class Config(
"com.mojang"
),
var paths: AddonPaths = AddonPaths(behPath, resPath),
var targetMcVersion: ArrayList<Int> = arrayListOf(1, 19, 40),
var targetMcVersion: MutableList<Int> = arrayListOf(1, 20, 70),
var formatVersions: FormatVersions = FormatVersions(getVersionAsString(targetMcVersion)),
var langFileBuilder: AddonLangFileBuilders = AddonLangFileBuilders(behPath, resPath),
var scriptEntryFile: File = File(),
var scriptingVersion: String = "1.6.0"
var scriptingVersion: String = "1.8.0",
var hashFileNames: Boolean = false,
/**
* don't hash or modify these file names on build
*/
var vanillaFileNames: List<String> = listOf("player", "humanoid")
) {
init {
behPath.toFile().deleteRecursively()
Expand Down Expand Up @@ -139,7 +355,7 @@ class Config(
var behEntity: String = targetMcVersion,
var resEntity: String = "1.10.0",
var resItem: String = "1.10.0",
var behItem: String = targetMcVersion,
var behItem: String = "1.20.50",
var behAnimation: String = "1.8.0",
var behBlock: String = targetMcVersion,
var behRecipe: String = "1.17.41",
Expand All @@ -152,3 +368,35 @@ class Config(
)
}

private fun interactiveConfig(confFile: File) {
val projectName: String
var projectShort: String
val scanner = Scanner(System.`in`)
val gson = GsonBuilder().setPrettyPrinting().create()

print("Project Name: ")
projectName = scanner.nextLine().trim()
print("Project Short: ")
scanner.nextLine().trim().let {
projectShort = if (it == "") projectName.take(2) else it
}

val config = Config(projectName, projectShort = projectShort)

val monsteraConfig = MonsteraConfig(
config.projectName,
config.projectShort
)

if (!confFile.exists())
confFile.createNewFile()
confFile.writeText(gson.toJson(monsteraConfig))
}

fun createLocalFile(local: File) {
val gson = GsonBuilder().setPrettyPrinting().create()
if (!local.exists())
local.createNewFile()

local.writeText(gson.toJson(MonsteraLocalConfig()))
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.lop.devtools.monstera.addon.sound.Sound
import com.lop.devtools.monstera.addon.sound.SoundData
import com.lop.devtools.monstera.addon.sound.unsafeApplySoundData
import com.lop.devtools.monstera.files.beh.blocks.BehBlocks
import com.lop.devtools.monstera.files.beh.blocks.components.MaterialSettings
import com.lop.devtools.monstera.files.createWithDirs
import com.lop.devtools.monstera.files.getUniqueFileName
import com.lop.devtools.monstera.files.res.blocks.BlockDefs
Expand Down Expand Up @@ -55,7 +56,7 @@ open class Block(
/**
* set a texture that is applied on all sites
*/
fun texture(name: String, path: String, settings: BehBlocks.MaterialSettings.() -> Unit) {
fun texture(name: String, path: String, settings: MaterialSettings.() -> Unit) {
unsafeBehBlock.components {
materialInstance {
all {
Expand All @@ -70,7 +71,7 @@ open class Block(
/**
* set a texture that is applied on all sites
*/
fun texture(file: File, settings: BehBlocks.MaterialSettings.() -> Unit) {
fun texture(file: File, settings: MaterialSettings.() -> Unit) {
val uniqueFilename = getUniqueFileName(file)
val target = addon.config.paths.resTextures.resolve("monstera").resolve(uniqueFilename).toFile()
file.copyTo(target.createWithDirs(), true)
Expand Down
30 changes: 23 additions & 7 deletions src/main/kotlin/com/lop/devtools/monstera/addon/entity/Entity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,35 @@ package com.lop.devtools.monstera.addon.entity
import com.lop.devtools.monstera.addon.Addon
import com.lop.devtools.monstera.addon.entity.behaviour.BehaviourEntity
import com.lop.devtools.monstera.addon.entity.resource.ResourceEntity
import com.lop.devtools.monstera.addon.sound.Sound
import com.lop.devtools.monstera.addon.sound.SoundData
import com.lop.devtools.monstera.addon.sound.unsafeApplySoundData

class Entity(
open class Entity(
val addon: Addon,
var name: String = "undefined",
var displayName: String = name
) {
val unsafeBehaviourEntity: BehaviourEntity = BehaviourEntity(this)
val unsafeResourceEntity: ResourceEntity = ResourceEntity(this)
data class Data(
var addon: Addon,
var name: String,
var displayName: String,
var identifier: String,
var spawnAble: Boolean,
var sounds: MutableList<SoundData> = mutableListOf()
)

var unsafeSpawnAble: Boolean = false
val unsafeSoundData: MutableList<SoundData> = mutableListOf()
val data = Data(
addon = addon,
name = name,
displayName = displayName,
identifier = getIdentifier(),
spawnAble = false,
sounds = mutableListOf()
)

val unsafeBehaviourEntity: BehaviourEntity = BehaviourEntity(data)
val unsafeResourceEntity: ResourceEntity = ResourceEntity(data)

/**
* @return the identifier of the entity as it is defined in the final beh/res pack
Expand Down Expand Up @@ -80,8 +96,8 @@ class Entity(
unsafeBehaviourEntity.build()
unsafeResourceEntity.build()

if (unsafeSoundData.isNotEmpty()) {
addon.unsafeApplySoundData(unsafeSoundData, name)
if (data.sounds.isNotEmpty()) {
addon.unsafeApplySoundData(data.sounds, getIdentifier())
}
}
}
Loading

0 comments on commit e2556d9

Please sign in to comment.