Skip to content

Commit ec8d702

Browse files
committed
Add focus holder emptying recipe
1 parent eb1b617 commit ec8d702

File tree

7 files changed

+89
-17
lines changed

7 files changed

+89
-17
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "hexdebug:focus_holder_emptying",
3+
"category": "misc"
4+
}

Common/src/main/kotlin/gay/object/hexdebug/items/FocusHolderBlockItem.kt

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ package gay.`object`.hexdebug.items
33
import at.petrak.hexcasting.api.casting.iota.Iota
44
import at.petrak.hexcasting.api.item.IotaHolderItem
55
import at.petrak.hexcasting.api.utils.asTranslatedComponent
6-
import at.petrak.hexcasting.api.utils.getCompound
76
import at.petrak.hexcasting.api.utils.getList
8-
import at.petrak.hexcasting.api.utils.putCompound
97
import gay.`object`.hexdebug.HexDebug
108
import gay.`object`.hexdebug.items.base.ItemPredicateProvider
119
import gay.`object`.hexdebug.items.base.ModelPredicateEntry
10+
import gay.`object`.hexdebug.registry.HexDebugBlockEntities
1211
import gay.`object`.hexdebug.utils.asItemPredicate
1312
import gay.`object`.hexdebug.utils.styledHoverName
1413
import net.minecraft.core.NonNullList
@@ -44,7 +43,7 @@ class FocusHolderBlockItem(block: Block, properties: Properties) :
4443
val (iotaStack, iotaHolder) = stack.getIotaStack()
4544
if (iotaHolder != null) {
4645
iotaHolder.writeDatum(iotaStack, iota)
47-
stack.putIotaStack(iotaStack)
46+
stack.setIotaStack(iotaStack)
4847
}
4948
}
5049

@@ -70,13 +69,13 @@ class FocusHolderBlockItem(block: Block, properties: Properties) :
7069
companion object {
7170
val HAS_ITEM = HexDebug.id("has_item")
7271

73-
val ItemStack.hasIotaStack get() = getCompound(BLOCK_ENTITY_TAG)
72+
val ItemStack.hasIotaStack get() = getBlockEntityData(this)
7473
?.getList("Items", Tag.TAG_COMPOUND)
7574
?.let { it.size > 0 }
7675
?: false
7776

7877
fun ItemStack.getIotaStack(): Pair<ItemStack, IotaHolderItem?> {
79-
val blockEntityTag = getCompound(BLOCK_ENTITY_TAG) ?: CompoundTag()
78+
val blockEntityTag = getBlockEntityData(this) ?: CompoundTag()
8079

8180
val containerStacks = NonNullList.withSize(1, ItemStack.EMPTY)
8281
ContainerHelper.loadAllItems(blockEntityTag, containerStacks)
@@ -85,13 +84,15 @@ class FocusHolderBlockItem(block: Block, properties: Properties) :
8584
return Pair(iotaStack, iotaStack.item as? IotaHolderItem)
8685
}
8786

88-
fun ItemStack.putIotaStack(iotaStack: ItemStack) {
89-
val blockEntityTag = getCompound(BLOCK_ENTITY_TAG) ?: CompoundTag()
87+
fun ItemStack.setIotaStack(iotaStack: ItemStack) {
88+
val tag = if (iotaStack.isEmpty) {
89+
CompoundTag()
90+
} else {
91+
val containerStacks = NonNullList.of(ItemStack.EMPTY, iotaStack)
92+
ContainerHelper.saveAllItems(getBlockEntityData(this) ?: CompoundTag(), containerStacks)
93+
}
9094

91-
val containerStacks = NonNullList.of(ItemStack.EMPTY, iotaStack)
92-
ContainerHelper.saveAllItems(blockEntityTag, containerStacks)
93-
94-
putCompound(BLOCK_ENTITY_TAG, blockEntityTag)
95+
setBlockEntityData(this, HexDebugBlockEntities.FOCUS_HOLDER.value, tag)
9596
}
9697
}
9798
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package gay.`object`.hexdebug.recipes
2+
3+
import gay.`object`.hexdebug.items.FocusHolderBlockItem.Companion.getIotaStack
4+
import gay.`object`.hexdebug.items.FocusHolderBlockItem.Companion.hasIotaStack
5+
import gay.`object`.hexdebug.items.FocusHolderBlockItem.Companion.setIotaStack
6+
import gay.`object`.hexdebug.registry.HexDebugBlocks
7+
import gay.`object`.hexdebug.registry.HexDebugRecipeSerializers
8+
import net.minecraft.core.NonNullList
9+
import net.minecraft.core.RegistryAccess
10+
import net.minecraft.resources.ResourceLocation
11+
import net.minecraft.world.inventory.CraftingContainer
12+
import net.minecraft.world.item.ItemStack
13+
import net.minecraft.world.item.Items
14+
import net.minecraft.world.item.crafting.CraftingBookCategory
15+
import net.minecraft.world.item.crafting.CustomRecipe
16+
import net.minecraft.world.level.Level
17+
18+
class FocusHolderEmptyingRecipe(id: ResourceLocation, category: CraftingBookCategory) : CustomRecipe(id, category) {
19+
override fun matches(container: CraftingContainer, level: Level): Boolean {
20+
return findItem(container) != null
21+
}
22+
23+
override fun assemble(container: CraftingContainer, registryAccess: RegistryAccess): ItemStack {
24+
val holder = findItem(container) ?: return ItemStack.EMPTY
25+
return holder.getIotaStack().first.copy()
26+
}
27+
28+
override fun getRemainingItems(container: CraftingContainer): NonNullList<ItemStack> {
29+
val remaining = NonNullList.withSize(container.containerSize, ItemStack.EMPTY)
30+
for (i in 0..remaining.size) {
31+
val itemStack = container.getItem(i)
32+
if (itemStack.`is`(HexDebugBlocks.FOCUS_HOLDER.item)) {
33+
remaining[i] = itemStack.copy().apply { setIotaStack(ItemStack.EMPTY) }
34+
}
35+
}
36+
return remaining
37+
}
38+
39+
override fun canCraftInDimensions(width: Int, height: Int): Boolean {
40+
return width * height >= 1
41+
}
42+
43+
override fun getSerializer() = HexDebugRecipeSerializers.FOCUS_HOLDER_EMPTYING.value
44+
45+
private fun findItem(container: CraftingContainer): ItemStack? {
46+
var holder: ItemStack? = null
47+
for (itemStack in container.items) {
48+
when (itemStack.item) {
49+
HexDebugBlocks.FOCUS_HOLDER.item -> {
50+
if (holder != null || !itemStack.hasIotaStack || itemStack.count > 1) return null
51+
holder = itemStack
52+
}
53+
Items.AIR -> {}
54+
else -> return null
55+
}
56+
}
57+
return holder
58+
}
59+
}

Common/src/main/kotlin/gay/object/hexdebug/recipes/FocusHolderFillingShapedRecipe.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package gay.`object`.hexdebug.recipes
33
import at.petrak.hexcasting.common.lib.HexItems
44
import com.google.gson.JsonObject
55
import gay.`object`.hexdebug.items.FocusHolderBlockItem.Companion.hasIotaStack
6-
import gay.`object`.hexdebug.items.FocusHolderBlockItem.Companion.putIotaStack
6+
import gay.`object`.hexdebug.items.FocusHolderBlockItem.Companion.setIotaStack
77
import gay.`object`.hexdebug.registry.HexDebugBlocks
88
import gay.`object`.hexdebug.registry.HexDebugRecipeSerializers
99
import net.minecraft.core.NonNullList
@@ -42,7 +42,7 @@ class FocusHolderFillingShapedRecipe(
4242
val result = super.assemble(container, registryAccess)
4343
for (ingredient in container.items) {
4444
if (ingredient.`is`(HexItems.FOCUS)) {
45-
result.putIotaStack(ingredient)
45+
result.setIotaStack(ingredient)
4646
break
4747
}
4848
}
@@ -63,7 +63,7 @@ class FocusHolderFillingShapedRecipe(
6363
height = height,
6464
recipeItems = ingredients,
6565
result = getResultItem(null).apply {
66-
putIotaStack(ItemStack(HexItems.FOCUS))
66+
setIotaStack(ItemStack(HexItems.FOCUS))
6767
},
6868
showNotification = showNotification(),
6969
)

Common/src/main/kotlin/gay/object/hexdebug/recipes/FocusHolderFillingShapelessRecipe.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package gay.`object`.hexdebug.recipes
22

33
import at.petrak.hexcasting.common.lib.HexItems
44
import gay.`object`.hexdebug.items.FocusHolderBlockItem.Companion.hasIotaStack
5-
import gay.`object`.hexdebug.items.FocusHolderBlockItem.Companion.putIotaStack
5+
import gay.`object`.hexdebug.items.FocusHolderBlockItem.Companion.setIotaStack
66
import gay.`object`.hexdebug.registry.HexDebugBlocks
77
import gay.`object`.hexdebug.registry.HexDebugRecipeSerializers
88
import net.minecraft.core.NonNullList
@@ -17,7 +17,7 @@ import net.minecraft.world.level.Level
1717

1818
class FocusHolderFillingShapelessRecipe(id: ResourceLocation, category: CraftingBookCategory) : CustomRecipe(id, category) {
1919
private val resultItem = ItemStack(HexDebugBlocks.FOCUS_HOLDER.item).apply {
20-
putIotaStack(ItemStack(HexItems.FOCUS))
20+
setIotaStack(ItemStack(HexItems.FOCUS))
2121
}
2222

2323
override fun matches(container: CraftingContainer, level: Level): Boolean {
@@ -27,7 +27,7 @@ class FocusHolderFillingShapelessRecipe(id: ResourceLocation, category: Crafting
2727
override fun assemble(container: CraftingContainer, registryAccess: RegistryAccess): ItemStack {
2828
val (holder, focus) = findItems(container) ?: return ItemStack.EMPTY
2929
return holder.copyWithCount(1).apply {
30-
putIotaStack(focus)
30+
setIotaStack(focus)
3131
}
3232
}
3333

Common/src/main/kotlin/gay/object/hexdebug/registry/HexDebugRecipeSerializers.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package gay.`object`.hexdebug.registry
22

3+
import gay.`object`.hexdebug.recipes.FocusHolderEmptyingRecipe
34
import gay.`object`.hexdebug.recipes.FocusHolderFillingShapedRecipe
45
import gay.`object`.hexdebug.recipes.FocusHolderFillingShapelessRecipe
56
import net.minecraft.core.registries.BuiltInRegistries
@@ -11,6 +12,10 @@ object HexDebugRecipeSerializers : HexDebugRegistrar<RecipeSerializer<*>>(
1112
Registries.RECIPE_SERIALIZER,
1213
{ BuiltInRegistries.RECIPE_SERIALIZER },
1314
) {
15+
val FOCUS_HOLDER_EMPTYING = register("focus_holder_emptying") {
16+
SimpleCraftingRecipeSerializer(::FocusHolderEmptyingRecipe)
17+
}
18+
1419
val FOCUS_HOLDER_FILLING_SHAPED = register("focus_holder_filling_shaped") {
1520
FocusHolderFillingShapedRecipe.Serializer()
1621
}

Forge/src/main/kotlin/gay/object/hexdebug/forge/datagen/HexDebugRecipes.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ class HexDebugRecipes(output: PackOutput) : PaucalRecipeProvider(output, HexDebu
8282

8383
// existing focus holder with existing focus
8484
specialRecipe(writer, HexDebugRecipeSerializers.FOCUS_HOLDER_FILLING_SHAPELESS)
85+
86+
// remove focus from holder
87+
specialRecipe(writer, HexDebugRecipeSerializers.FOCUS_HOLDER_EMPTYING)
8588
}
8689

8790
@Suppress("SameParameterValue")

0 commit comments

Comments
 (0)