Skip to content

Commit

Permalink
added a recipe api + integrated into Item and Entity
Browse files Browse the repository at this point in the history
  • Loading branch information
12rcu committed Jan 11, 2024
1 parent 5964303 commit ced6d1b
Show file tree
Hide file tree
Showing 8 changed files with 304 additions and 3 deletions.
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
rootProject.name = "monstera"
rootProject.name = "Monstera"
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.lop.devtools.monstera.addon.entity.Entity
import com.lop.devtools.monstera.addon.entity.behaviour.components.OverwriteComponents
import com.lop.devtools.monstera.addon.molang.Molang
import com.lop.devtools.monstera.addon.molang.Query
import com.lop.devtools.monstera.addon.recipes.CraftingRecipe
import com.lop.devtools.monstera.files.animcontroller.AnimController
import com.lop.devtools.monstera.files.animcontroller.AnimationControllers
import com.lop.devtools.monstera.files.beh.animations.BehAnimation
Expand All @@ -19,6 +20,7 @@ interface BehaviourEntity: OverwriteComponents, AnimationControllerExtensions {
val unsafeRawEntity: BehEntity
val unsafeRawAnimations: BehAnimations
val unsafeRawControllers: AnimationControllers
val unsafeRawCraftingRecipe: CraftingRecipe

/**
* add a runtimeIdentifier like guardian
Expand Down Expand Up @@ -163,6 +165,25 @@ interface BehaviourEntity: OverwriteComponents, AnimationControllerExtensions {
*/
fun properties(data: EntityProperties.() -> Unit)

/**
* creates a recipe for the crafting table
*
* ```
* craftingRecipe {
* craftingPattern(
* t("","minecraft:diamond","minecraft:diamond"),
* t("","minecraft:diamond",""),
* t("","minecraft:stick","")
* )
* unlock {
* item("minecraft:wood", count = 3, data = 2)
* context()
* }
* }
* ```
*/
fun craftingRecipe(data: CraftingRecipe.() -> Unit)

/**
* internal function for building the beh files
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.lop.devtools.monstera.addon.entity.Entity
import com.lop.devtools.monstera.addon.entity.behaviour.components.OverwriteComponentsImpl
import com.lop.devtools.monstera.addon.molang.Molang
import com.lop.devtools.monstera.addon.molang.Query
import com.lop.devtools.monstera.addon.recipes.CraftingRecipe
import com.lop.devtools.monstera.files.animcontroller.AnimController
import com.lop.devtools.monstera.files.animcontroller.AnimStateComponent
import com.lop.devtools.monstera.files.beh.animations.BehAnimation
Expand Down Expand Up @@ -108,6 +109,10 @@ abstract class BehaviourEntityImpl(
}
}

override fun craftingRecipe(data: CraftingRecipe.() -> Unit) {
unsafeRawCraftingRecipe.apply(data)
}

override fun AnimStateComponent.controller(name: String, query: Query, data: AnimController.() -> Unit) {
controller("${unsafeParent.name}.$name", query)
animController(name, Query.False, data)
Expand Down Expand Up @@ -141,5 +146,13 @@ abstract class BehaviourEntityImpl(
unsafeParent.name,
unsafeParent.addon.config.paths.behAnimController
)

if(!unsafeRawCraftingRecipe.unsafe.isEmpty()) {
unsafeRawCraftingRecipe.unsafe.build(
unsafeParent.name,
unsafeParent.getIdentifier() + "_spawn_egg",
unsafeParent.addon
)
}
}
}
26 changes: 26 additions & 0 deletions src/main/kotlin/com/lop/devtools/monstera/addon/item/Item.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.lop.devtools.monstera.addon.item

import com.lop.devtools.monstera.addon.Addon
import com.lop.devtools.monstera.addon.recipes.CraftingRecipe
import com.lop.devtools.monstera.files.beh.item.BehItem
import com.lop.devtools.monstera.files.beh.item.BehItemComponents
import com.lop.devtools.monstera.files.getUniqueFileName
Expand All @@ -11,6 +12,7 @@ class Item(val name: String, val displayName: String, private val addon: Addon)
private val behItem = BehItem()
private val resItem = ResItem()
private var category: String = "equipment"
private val craftingRecipe: CraftingRecipe = CraftingRecipe()

fun category(category: String = "Equipment") {
this.category = category
Expand Down Expand Up @@ -53,11 +55,35 @@ class Item(val name: String, val displayName: String, private val addon: Addon)
behItem.components(components)
}

/**
* creates a recipe for the crafting table
*
* ```
* craftingRecipe {
* craftingPattern(
* t("","minecraft:diamond","minecraft:diamond"),
* t("","minecraft:diamond",""),
* t("","minecraft:stick","")
* )
* unlock {
* item("minecraft:wood", count = 3, data = 2)
* context()
* }
* }
* ```
*/
fun craftingRecipe(data: CraftingRecipe.() -> Unit) {
craftingRecipe.apply(data)
}

fun build() {
behItem.description(identifier(), category)
behItem.unsafe.build(name, addon.config.paths.behItems)

resItem.description(identifier(), category, displayName)
resItem.unsafe.build(name, addon.config.paths.resItem)

if(!craftingRecipe.unsafe.isEmpty())
craftingRecipe.unsafe.build(name, identifier(), addon)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.lop.devtools.monstera.addon.recipes

import com.lop.devtools.monstera.addon.Addon
import com.lop.devtools.monstera.addon.api.MonsteraFile
import com.lop.devtools.monstera.addon.api.MonsteraUnsafe
import com.lop.devtools.monstera.files.beh.recipes.BehRecipe
import com.lop.devtools.monstera.files.beh.recipes.BehRecipeUnlock
import com.lop.devtools.monstera.files.beh.recipes.RecipeTags

class CraftingRecipe: MonsteraFile {
override val unsafe = Unsafe()

inner class Unsafe: MonsteraUnsafe {
val rawRecipe = BehRecipe()
val keyPattern = mutableMapOf<String, String>()

fun isEmpty() = keyPattern.isEmpty()

fun build(name: String, identifier: String, addon: Addon, resultItem: String = identifier) {
rawRecipe.identifier = identifier
rawRecipe.result(resultItem)
rawRecipe.keys {
keyPattern.forEach { (id, key) ->
key(key, id)
}
}
rawRecipe.unsafe.build(name, addon.config.paths.behRecipe)
}
}


/**
* add a pattern for the crafting table to the recipe
*
* ```
* craftingPattern(
* t("","minecraft:diamond","minecraft:diamond"),
* t("","minecraft:diamond",""),
* t("","minecraft:stick","")
* )
* ```
*/
fun craftingPattern(
t1: Triple<String, String, String>,
t2: Triple<String, String, String>,
t3: Triple<String, String, String>,
) {
unsafe.rawRecipe.tags = arrayListOf(RecipeTags.CRAFTING_TABLE)
unsafe.rawRecipe.pattern = arrayListOf(
tripleToPattern(t1),
tripleToPattern(t2),
tripleToPattern(t3)
)
}

/**
* @param data define how this item will be unlocked in the recipe book
*
* ```
* unlock {
* item("minecraft:wood", count = 3, data = 2)
* context()
* }
* ```
*/
fun unlock(data: BehRecipeUnlock.() -> Unit) {
unsafe.rawRecipe.unlock(data)
}

fun t(i1: String, i2: String, i3: String) = Triple(i1, i2, i3)

private fun tripleToPattern(t: Triple<String, String, String>): String {
var patternString = ""
patternString += compilePattern(t.first)
patternString += compilePattern(t.second)
patternString += compilePattern(t.third)
return patternString
}

private fun compilePattern(t: String): String {
if(unsafe.keyPattern.containsKey(t))
return unsafe.keyPattern[t]!!

val ret = when(unsafe.keyPattern.size) {
0 -> "F"
1 -> "G"
2 -> "H"
3 -> "I"
4 -> "J"
5 -> "K"
6 -> "L"
7 -> "M"
8 -> "N"
else -> "O"
}

unsafe.keyPattern[t] = ret

return ret
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package com.lop.devtools.monstera.files.beh.recipes

import com.lop.devtools.monstera.addon.api.MonsteraFile
import com.lop.devtools.monstera.addon.api.MonsteraUnsafeMap
import com.lop.devtools.monstera.files.MonsteraBuilder
import java.nio.file.Path

class BehRecipe: MonsteraFile {
class BehRecipe : MonsteraFile {
/**
* unsafe to use variables, used for plugins/ libraries
*/
override val unsafe = Unsafe()

inner class Unsafe: MonsteraUnsafeMap {
inner class Unsafe : MonsteraUnsafeMap {
/**
* access to all defined data
*/
Expand All @@ -27,10 +29,40 @@ class BehRecipe: MonsteraFile {
reagent?.let { unsafe.general["reagent"] = it }
return unsafe.general
}

fun build(
filename: String,
path: Path,
version: String = "1.17.41"
) {
val sanFile = filename
.removeSuffix(".json")
.replace("-", "_")
.replace(" ", "_")
MonsteraBuilder.buildTo(
path, "$sanFile.json", mutableMapOf(
"format_version" to version,
"minecraft:recipe_shaped" to getData()
)
)
}
}

var identifier: String? = null
var tags: ArrayList<RecipeTags>? = null
set(value) {
if (field == null || value == null)
field = value
else
field?.let { field!!.addAll(it) }
}
var pattern: ArrayList<String>? = null
set(value) {
if (field == null || value == null)
field = value
else
field?.let { field!!.addAll(it) }
}
var inputBrewingStand: String? = null

var reagent: String? = null
Expand Down Expand Up @@ -68,6 +100,13 @@ class BehRecipe: MonsteraFile {
}
}

/**
* @param data define how this item will be unlocked in the recipe book
*/
fun unlock(data: BehRecipeUnlock.() -> Unit) {
unsafe.general["unlock"] = BehRecipeUnlock().apply(data).unsafe.getData()
}

/**
* 1
*
Expand Down
Loading

0 comments on commit ced6d1b

Please sign in to comment.