Skip to content

Commit

Permalink
Split HTIngredient to HTItemIngredient and HTFluidIngredient
Browse files Browse the repository at this point in the history
Added new item Fluid/Item Filter for Exporter
  • Loading branch information
Hiiragi283 committed Nov 18, 2024
1 parent 21e1b2a commit d2a4224
Show file tree
Hide file tree
Showing 35 changed files with 490 additions and 341 deletions.
2 changes: 1 addition & 1 deletion src/client/kotlin/hiiragi283/ragium/client/RagiumClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ object RagiumClient : ClientModInitializer {
registerEvents()
registerNetworks()

RagiumAPI.log { info("Ragium-Client initialized!") }
RagiumAPI.LOGGER.info("Ragium-Client initialized!")
}

// Blocks //
Expand Down
2 changes: 2 additions & 0 deletions src/main/generated/assets/ragium/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@
"material.ragium.tungsten": "Tungsten",
"material.ragium.wood": "Wood",
"material.ragium.zinc": "Zinc",
"message.ragium.block.exporter.fluid_filter": "Current Fluid Filter: %s",
"message.ragium.block.exporter.item_filter": "Current Item Filter: %s",
"message.ragium.multi_shape.error": "Not matching condition; %s at %ss",
"message.ragium.multi_shape.success": "The structure is valid!",
"tag_prefix.ragium.deep_ore": "Deepslate %s Ore",
Expand Down
2 changes: 2 additions & 0 deletions src/main/generated/assets/ragium/lang/ja_jp.json
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@
"material.ragium.tungsten": "タングステン",
"material.ragium.wood": "木材",
"material.ragium.zinc": "亜鉛",
"message.ragium.block.exporter.fluid_filter": "現在の液体フィルタ: %s",
"message.ragium.block.exporter.item_filter": "現在のアイテムフィルタ: %s",
"message.ragium.multi_shape.error": "次の条件を満たしていません; %s (座標 %s)",
"message.ragium.multi_shape.success": "構造物は有効です!",
"tag_prefix.ragium.deep_ore": "深層%s鉱石",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/generated",
"textures": {
"layer0": "ragium:item/fluid_filter"
}
}
6 changes: 6 additions & 0 deletions src/main/generated/assets/ragium/models/item/item_filter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/generated",
"textures": {
"layer0": "ragium:item/item_filter"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"values": [
"ragium:fluid_filter"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"values": [
"ragium:item_filter"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private <T> void addTag(Map<Identifier, List<TagGroupLoader.TrackedEntry>> map,
// add item entries
items.forEach((Item item) -> add(map, tagKey, item, Registries.ITEM));
}));
RagiumAPI.getLogger().info("Registered runtime item tags!");
RagiumAPI.getLOGGER().info("Registered runtime item tags!");
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/hiiragi283/ragium/api/RagiumAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ interface RagiumAPI {
fun id(path: String): Identifier = Identifier.of(MOD_ID, path)

@JvmStatic
val logger: Logger = LoggerFactory.getLogger(MOD_NAME)
val LOGGER: Logger = LoggerFactory.getLogger(MOD_NAME)

@JvmStatic
inline fun log(action: Logger.() -> Unit) {
logger.action()
LOGGER.action()
}

@JvmStatic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import hiiragi283.ragium.api.machine.HTMachineKey
import hiiragi283.ragium.api.machine.HTMachineTier
import hiiragi283.ragium.api.material.HTMaterialKey
import hiiragi283.ragium.api.material.HTTagPrefix
import hiiragi283.ragium.api.recipe.HTFluidIngredient
import hiiragi283.ragium.api.recipe.HTFluidResult
import hiiragi283.ragium.api.recipe.HTIngredient
import hiiragi283.ragium.api.recipe.HTItemIngredient
import hiiragi283.ragium.api.recipe.HTItemResult
import hiiragi283.ragium.api.recipe.HTMachineRecipe
import hiiragi283.ragium.common.init.RagiumFluids
Expand Down Expand Up @@ -49,53 +50,53 @@ class HTMachineRecipeJsonBuilder private constructor(
fun createRecipeId(fluid: RagiumFluids): Identifier = createRecipeId(fluid.value)
}

private val itemInputs: MutableList<HTIngredient.Item> = mutableListOf()
private val fluidInputs: MutableList<HTIngredient.Fluid> = mutableListOf()
private val itemInputs: MutableList<HTItemIngredient> = mutableListOf()
private val fluidInputs: MutableList<HTFluidIngredient> = mutableListOf()
private val itemOutputs: MutableList<HTItemResult> = mutableListOf()
private val fluidOutputs: MutableList<HTFluidResult> = mutableListOf()
private var catalyst: HTIngredient.Item? = null
private var catalyst: HTItemIngredient? = null

// Input //

fun itemInput(ingredient: HTIngredient.Item): HTMachineRecipeJsonBuilder = apply {
fun itemInput(ingredient: HTItemIngredient): HTMachineRecipeJsonBuilder = apply {
itemInputs.add(ingredient)
}

fun itemInput(
prefix: HTTagPrefix,
material: HTMaterialKey,
count: Int = 1,
consumeType: HTIngredient.ConsumeType = HTIngredient.ConsumeType.DECREMENT,
consumeType: HTItemIngredient.ConsumeType = HTItemIngredient.ConsumeType.DECREMENT,
): HTMachineRecipeJsonBuilder = itemInput(prefix.createTag(material), count, consumeType)

fun itemInput(
content: HTContent.Material<*>,
count: Int = 1,
consumeType: HTIngredient.ConsumeType = HTIngredient.ConsumeType.DECREMENT,
consumeType: HTItemIngredient.ConsumeType = HTItemIngredient.ConsumeType.DECREMENT,
): HTMachineRecipeJsonBuilder = itemInput(content.prefixedTagKey, count, consumeType)

fun itemInput(
item: ItemConvertible,
count: Int = 1,
consumeType: HTIngredient.ConsumeType = HTIngredient.ConsumeType.DECREMENT,
): HTMachineRecipeJsonBuilder = itemInput(HTIngredient.ofItem(item, count, consumeType))
consumeType: HTItemIngredient.ConsumeType = HTItemIngredient.ConsumeType.DECREMENT,
): HTMachineRecipeJsonBuilder = itemInput(HTItemIngredient.of(item, count, consumeType))

fun itemInput(
tagKey: TagKey<Item>,
count: Int = 1,
consumeType: HTIngredient.ConsumeType = HTIngredient.ConsumeType.DECREMENT,
): HTMachineRecipeJsonBuilder = itemInput(HTIngredient.ofItem(tagKey, count, consumeType))
consumeType: HTItemIngredient.ConsumeType = HTItemIngredient.ConsumeType.DECREMENT,
): HTMachineRecipeJsonBuilder = itemInput(HTItemIngredient.of(tagKey, count, consumeType))

fun fluidInput(fluid: Fluid, amount: Long = FluidConstants.BUCKET): HTMachineRecipeJsonBuilder = apply {
fluidInputs.add(HTIngredient.ofFluid(fluid, amount))
fluidInputs.add(HTFluidIngredient.of(fluid, amount))
}

fun fluidInput(fluid: RagiumFluids, amount: Long = FluidConstants.BUCKET): HTMachineRecipeJsonBuilder = apply {
fluidInputs.add(HTIngredient.ofFluid(fluid.value, amount))
fluidInputs.add(HTFluidIngredient.of(fluid.value, amount))
}

fun fluidInput(tagKey: TagKey<Fluid>, amount: Long = FluidConstants.BUCKET): HTMachineRecipeJsonBuilder = apply {
fluidInputs.add(HTIngredient.ofFluid(tagKey, amount))
fluidInputs.add(HTFluidIngredient.of(tagKey, amount))
}

// Output //
Expand All @@ -121,11 +122,11 @@ class HTMachineRecipeJsonBuilder private constructor(
// Catalyst //

fun catalyst(item: ItemConvertible): HTMachineRecipeJsonBuilder = apply {
catalyst = HTIngredient.ofItem(item)
catalyst = HTItemIngredient.of(item)
}

fun catalyst(tagKey: TagKey<Item>): HTMachineRecipeJsonBuilder = apply {
catalyst = HTIngredient.ofItem(tagKey)
catalyst = HTItemIngredient.of(tagKey)
}

@Deprecated(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,13 @@ fun Inventory.writeNbt(nbt: NbtCompound, wrapperLookup: RegistryWrapper.WrapperL
ItemStack.OPTIONAL_CODEC
.listOf()
.encodeStart(wrapperLookup.getOps(NbtOps.INSTANCE), iterateStacks())
.result()
.orElse(NbtList())
.let { nbt.put("Inventory", it) }
.ifSuccess { nbt.put("Inventory", it) }
.ifError { nbt.put("Inventory", NbtList()) }
}

fun Inventory.readNbt(nbt: NbtCompound, wrapperLookup: RegistryWrapper.WrapperLookup) {
ItemStack.OPTIONAL_CODEC
.listOf()
.parse(wrapperLookup.getOps(NbtOps.INSTANCE), nbt.get("Inventory"))
.result()
.orElse(listOf())
.forEachIndexed(::setStack)
.ifSuccess { it.forEachIndexed(::setStack) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ operator fun <T : Recipe<*>> RecipeEntry<T>.component2(): T = this.value

// Registry //

val <T : Any> RegistryEntryList<T>.isEmpty: Boolean
get() = size() == 0

fun createWrapperLookup(): RegistryWrapper.WrapperLookup = BuiltinRegistries.createWrapperLookup()

fun <T : Any> idComparator(registry: Registry<T>): Comparator<T> = compareBy(registry::getId)
Expand Down
104 changes: 104 additions & 0 deletions src/main/kotlin/hiiragi283/ragium/api/recipe/HTFluidIngredient.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package hiiragi283.ragium.api.recipe

import com.mojang.serialization.Codec
import com.mojang.serialization.codecs.RecordCodecBuilder
import hiiragi283.ragium.api.extension.contains
import hiiragi283.ragium.api.extension.isOf
import hiiragi283.ragium.api.extension.longRangeCodec
import hiiragi283.ragium.api.extension.useTransaction
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidConstants
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant
import net.fabricmc.fabric.api.transfer.v1.storage.StorageUtil
import net.fabricmc.fabric.api.transfer.v1.storage.base.ResourceAmount
import net.fabricmc.fabric.api.transfer.v1.storage.base.SingleSlotStorage
import net.fabricmc.fabric.api.transfer.v1.transaction.Transaction
import net.minecraft.fluid.Fluid
import net.minecraft.fluid.Fluids
import net.minecraft.network.RegistryByteBuf
import net.minecraft.network.codec.PacketCodec
import net.minecraft.network.codec.PacketCodecs
import net.minecraft.registry.Registries
import net.minecraft.registry.RegistryCodecs
import net.minecraft.registry.RegistryKeys
import net.minecraft.registry.entry.RegistryEntry
import net.minecraft.registry.entry.RegistryEntryList
import net.minecraft.registry.tag.TagKey
import java.util.function.BiPredicate

class HTFluidIngredient private constructor(private val entryList: RegistryEntryList<Fluid>, val amount: Long) : BiPredicate<Fluid, Long> {
companion object {
@JvmField
val CODEC: Codec<HTFluidIngredient> = RecordCodecBuilder.create { instance ->
instance
.group(
RegistryCodecs
.entryList(RegistryKeys.FLUID)
.fieldOf("fluids")
.forGetter(HTFluidIngredient::entryList),
longRangeCodec(1, Long.MAX_VALUE)
.optionalFieldOf("amount", FluidConstants.BUCKET)
.forGetter(HTFluidIngredient::amount),
).apply(instance, ::HTFluidIngredient)
}

@JvmField
val PACKET_CODEC: PacketCodec<RegistryByteBuf, HTFluidIngredient> = PacketCodec
.tuple(
PacketCodecs.registryEntryList(RegistryKeys.FLUID),
HTFluidIngredient::entryList,
PacketCodecs.VAR_LONG,
HTFluidIngredient::amount,
::HTFluidIngredient,
)

@JvmStatic
fun of(fluid: Fluid, amount: Long = FluidConstants.BUCKET): HTFluidIngredient =
HTFluidIngredient(RegistryEntryList.of(fluid.registryEntry), amount)

@JvmStatic
fun of(tagKey: TagKey<Fluid>, amount: Long = FluidConstants.BUCKET): HTFluidIngredient =
HTFluidIngredient(Registries.FLUID.getOrCreateEntryList(tagKey), amount)
}

val isEmpty: Boolean
get() = entryList.storage.map(
{ false },
{ it.isEmpty() || it.any { entry: RegistryEntry<Fluid> -> entry.isOf(Fluids.EMPTY) } },
) ||
amount <= 0

fun <T : Any> map(transform: (RegistryEntry<Fluid>, Long) -> T): List<T> = entryList.map { transform(it, amount) }

val firstEntry: RegistryEntry<Fluid>?
get() = entryList.firstOrNull()

val entryMap: Map<RegistryEntry<Fluid>, Long> = entryList.associateWith { amount }

val valueMap: Map<Fluid, Long>
get() = entryMap.mapKeys { it.key.value() }

fun test(resource: ResourceAmount<FluidVariant>): Boolean = test(resource.resource.fluid, resource.amount)

override fun test(fluid: Fluid, amount: Long): Boolean = when {
fluid == Fluids.EMPTY || amount <= 0 -> this.isEmpty
else -> fluid in entryList && amount >= this.amount
}

fun onConsume(storage: SingleSlotStorage<FluidVariant>) {
useTransaction { transaction: Transaction ->
val foundVariant: FluidVariant = StorageUtil.findExtractableResource(
storage,
{ test(it.fluid, storage.amount) },
transaction,
) ?: return
if (test(foundVariant.fluid, amount)) {
val extracted: Long = storage.extract(foundVariant, amount, transaction)
if (extracted == amount) {
transaction.commit()
} else {
transaction.abort()
}
}
}
}
}
Loading

0 comments on commit d2a4224

Please sign in to comment.