-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Rudimentary system for ingredient options in crafting recipes (…
…ex. keep ingredient, or damage ingredient)
- Loading branch information
Showing
11 changed files
with
199 additions
and
53 deletions.
There are no files selected for viewing
11 changes: 8 additions & 3 deletions
11
idofront-catalog-shaded/src/main/java/com/mineinabyss/idofront/IdofrontPlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,14 @@ | ||
package com.mineinabyss.idofront | ||
|
||
import com.mineinabyss.idofront.resourcepacks.MinecraftAssetExtractor | ||
import org.bukkit.Bukkit | ||
import com.mineinabyss.idofront.di.DI | ||
import com.mineinabyss.idofront.plugin.listeners | ||
import com.mineinabyss.idofront.serialization.recipes.options.IngredientOptionsListener | ||
import org.bukkit.plugin.java.JavaPlugin | ||
|
||
class IdofrontPlugin : JavaPlugin() { | ||
|
||
override fun onEnable() { | ||
val recipeOptionsListener = IngredientOptionsListener(this) | ||
DI.add(recipeOptionsListener) | ||
listeners(recipeOptionsListener) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...serializers/src/main/kotlin/com/mineinabyss/idofront/serialization/recipes/RecipeUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.mineinabyss.idofront.serialization.recipes | ||
|
||
import org.bukkit.Bukkit | ||
import org.bukkit.Material | ||
import org.bukkit.NamespacedKey | ||
import org.bukkit.Tag | ||
import org.bukkit.inventory.RecipeChoice | ||
|
||
object RecipeUtils { | ||
fun getMaterialChoiceForTag(key: NamespacedKey) = RecipeChoice.MaterialChoice( | ||
Bukkit.getTag( | ||
Tag.REGISTRY_BLOCKS, | ||
key, | ||
Material::class.java | ||
) ?: Bukkit.getTag( | ||
Tag.REGISTRY_ITEMS, | ||
key, | ||
Material::class.java | ||
) ?: Tag.DIRT | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
.../src/main/kotlin/com/mineinabyss/idofront/serialization/recipes/options/IngredientInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.mineinabyss.idofront.serialization.recipes.options | ||
|
||
import org.bukkit.inventory.ItemStack | ||
|
||
internal data class IngredientInfo( | ||
val item: ItemStack, | ||
val slot: Int, | ||
val options: List<IngredientOption>, | ||
) |
30 changes: 30 additions & 0 deletions
30
...rc/main/kotlin/com/mineinabyss/idofront/serialization/recipes/options/IngredientOption.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.mineinabyss.idofront.serialization.recipes.options | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
import org.bukkit.inventory.ItemStack | ||
import org.bukkit.inventory.meta.Damageable | ||
|
||
@Serializable | ||
sealed class IngredientOption { | ||
abstract fun onCrafted(item: ItemStack, setItemInSlot: (ItemStack?) -> Unit) | ||
|
||
@Serializable | ||
@SerialName("keep") | ||
data object Keep : IngredientOption() { | ||
override fun onCrafted(item: ItemStack, setItemInSlot: (ItemStack?) -> Unit) { | ||
setItemInSlot(item) | ||
} | ||
} | ||
|
||
@Serializable | ||
@SerialName("damage") | ||
data class Damage(val amount: Int) : IngredientOption() { | ||
override fun onCrafted(item: ItemStack, setItemInSlot: (ItemStack?) -> Unit) { | ||
item.editMeta { | ||
if (it is Damageable) it.damage += amount | ||
} | ||
setItemInSlot(item) | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...c/main/kotlin/com/mineinabyss/idofront/serialization/recipes/options/IngredientOptions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.mineinabyss.idofront.serialization.recipes.options | ||
|
||
import org.bukkit.inventory.ItemStack | ||
import org.bukkit.inventory.RecipeChoice | ||
|
||
data class IngredientOptions( | ||
val options: Map<RecipeChoice, List<IngredientOption>> = mapOf(), | ||
) { | ||
fun getOptionsFor(item: ItemStack): List<IngredientOption> { | ||
val choice = options.entries.firstOrNull { it.key.test(item) } ?: return emptyList() | ||
return choice.value | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...otlin/com/mineinabyss/idofront/serialization/recipes/options/IngredientOptionsListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.mineinabyss.idofront.serialization.recipes.options | ||
|
||
import com.mineinabyss.idofront.di.DI | ||
import org.bukkit.Bukkit | ||
import org.bukkit.Keyed | ||
import org.bukkit.event.EventHandler | ||
import org.bukkit.event.Listener | ||
import org.bukkit.event.inventory.CraftItemEvent | ||
import org.bukkit.plugin.Plugin | ||
|
||
val ingredientOptionsListener by DI.observe<IngredientOptionsListener>() | ||
|
||
class IngredientOptionsListener( | ||
val plugin: Plugin, | ||
) : Listener { | ||
val keyToOptions = mutableMapOf<String, IngredientOptions>() | ||
|
||
@EventHandler | ||
fun CraftItemEvent.onCraft() { | ||
val recipe = recipe as? Keyed ?: return | ||
val options = keyToOptions[recipe.key.asString()] ?: return | ||
val onCrafted = inventory.matrix.mapIndexedNotNull { index, itemStack -> | ||
if (itemStack == null) return@mapIndexedNotNull null | ||
val itemOptions = options.getOptionsFor(itemStack) | ||
IngredientInfo(itemStack.clone(), index, itemOptions) | ||
} | ||
|
||
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin) { | ||
val matrix = inventory.matrix | ||
onCrafted.forEach { (item, slot, options) -> | ||
options.forEach { it.onCrafted(item) { matrix[slot] = it } } | ||
} | ||
inventory.matrix = matrix | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...c/main/kotlin/com/mineinabyss/idofront/serialization/recipes/options/RecipeWithOptions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.mineinabyss.idofront.serialization.recipes.options | ||
|
||
import org.bukkit.inventory.Recipe | ||
|
||
data class RecipeWithOptions( | ||
val recipe: Recipe, | ||
val options: IngredientOptions, | ||
) |