Skip to content

Commit

Permalink
feat: fix strainer filtering recipe not work
Browse files Browse the repository at this point in the history
rft: IHydrationUsable
  • Loading branch information
gloridifice committed May 31, 2024
1 parent 4f16c76 commit 11bd6fc
Show file tree
Hide file tree
Showing 14 changed files with 140 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import xyz.koiro.watersource.datagen.ModChineseLangGenerator
import xyz.koiro.watersource.datagen.ModEnglishLangGenerator
import xyz.koiro.watersource.datagen.ModModelGenerator
import xyz.koiro.watersource.datagen.ModItemTagGenerator
import xyz.koiro.watersource.datagen.ModFluidTagGenerator
import xyz.koiro.watersource.datagen.ModRecipeGenerator

object WaterSourceDataGenerator : DataGeneratorEntrypoint {
Expand All @@ -17,6 +18,7 @@ object WaterSourceDataGenerator : DataGeneratorEntrypoint {
pack.addProvider(::ModChineseLangGenerator)
pack.addProvider(::ModEnglishLangGenerator)
pack.addProvider(::ModItemTagGenerator)
pack.addProvider(::ModFluidTagGenerator)
pack.addProvider(::ModRecipeGenerator)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@ import net.minecraft.item.Items
import xyz.koiro.watersource.WaterSource
import xyz.koiro.watersource.datagen.provider.HydrationDataProvider
import xyz.koiro.watersource.world.effect.ModStatusEffects
import xyz.koiro.watersource.world.fluid.ModFluids

class HydrationDataGenerator(output: DataOutput): HydrationDataProvider(output) {
class HydrationDataGenerator(output: DataOutput) : HydrationDataProvider(output) {
override fun addData(adder: HydrationDataAdder) {
adder.add(WaterSource.identifier("item_apple"), item(Items.APPLE, 1, 1))
adder.add(WaterSource.identifier("fluid_water"), fluid(Fluids.WATER, 2, 0, StatusEffectInstance(ModStatusEffects.THIRSTY, 1200)))
adder.add(WaterSource.identifier("item_apple"), item(Items.APPLE, 1, 2))
adder.add(
WaterSource.identifier("fluid_water"),
fluid(Fluids.WATER, 2, 0, StatusEffectInstance(ModStatusEffects.THIRSTY, 1200))
)
adder.add(
WaterSource.identifier("fluid_purified_water"),
fluid(ModFluids.PURIFIED_WATER, 4, 6)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ package xyz.koiro.watersource.datagen

import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput
import net.fabricmc.fabric.api.datagen.v1.provider.FabricTagProvider.FluidTagProvider
import net.fabricmc.fabric.api.datagen.v1.provider.FabricTagProvider.ItemTagProvider
import net.minecraft.registry.RegistryWrapper
import net.minecraft.registry.tag.FluidTags
import xyz.koiro.watersource.world.fluid.ModFluids
import xyz.koiro.watersource.world.item.ModItems
import xyz.koiro.watersource.world.tag.ModTags
import java.util.concurrent.CompletableFuture

class ModFluidTagProvider(
class ModFluidTagGenerator(
output: FabricDataOutput?,
completableFuture: CompletableFuture<RegistryWrapper.WrapperLookup>?
) : FluidTagProvider(output, completableFuture) {
override fun configure(arg: RegistryWrapper.WrapperLookup?) {
getOrCreateTagBuilder(FluidTags.WATER).add(ModFluids.PURIFIED_WATER)
getOrCreateTagBuilder(FluidTags.WATER).add(ModFluids.FLOWING_PURIFIED_WATER)
getOrCreateTagBuilder(FluidTags.WATER)
.add(ModFluids.PURIFIED_WATER)
.add(ModFluids.FLOWING_PURIFIED_WATER)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import net.minecraft.registry.RegistryWrapper
import net.minecraft.registry.tag.TagKey
import net.minecraft.util.Identifier
import xyz.koiro.watersource.world.item.ModItems
import xyz.koiro.watersource.world.tag.ModTags
import java.util.concurrent.CompletableFuture

class ModItemTagGenerator(
Expand All @@ -21,5 +22,9 @@ class ModItemTagGenerator(
getOrCreateTagBuilder(TagKey.of(RegistryKeys.ITEM, Identifier(id))).add(it.first)
}
}

getOrCreateTagBuilder(ModTags.Item.PURIFICATION_STRAINER)
.add(ModItems.PAPER_STRAINER)
.add(ModItems.NATURAL_STRAINER)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ class ModRecipeGenerator(output: FabricDataOutput?) : FabricRecipeProvider(outpu
outFluid: Fluid,
strainer: Ingredient
) {
StrainerFilteringFluidRecipeJsonBuilder(id, outFluid, inFluid, strainer).offerTo(exporter, id)
StrainerFilteringFluidRecipeJsonBuilder(id, inFluid, outFluid, strainer).offerTo(exporter, id)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class StrainerFilteringFluidRecipeJsonBuilder(
val outFluidId: String,
val strainer: Ingredient,
val advancementBuilder: Advancement.Builder?,
val advancementId: Identifier?,
val advancementIdentifier: Identifier?,
val group: String?,
) : RecipeJsonProvider {
override fun serialize(json: JsonObject) {
Expand All @@ -87,6 +87,6 @@ class StrainerFilteringFluidRecipeJsonBuilder(

override fun toAdvancementJson(): JsonObject? = this.advancementBuilder?.toJson()

override fun getAdvancementId(): Identifier? = this.advancementId
override fun getAdvancementId(): Identifier? = this.advancementIdentifier
}
}
26 changes: 10 additions & 16 deletions src/main/kotlin/xyz/koiro/watersource/event/ModEventsRegistries.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import xyz.koiro.watersource.api.ifInSurvivalAndGetWaterData
import xyz.koiro.watersource.data.HydrationData
import xyz.koiro.watersource.world.effect.ModStatusEffects
import xyz.koiro.watersource.world.item.DrinkableContainer
import xyz.koiro.watersource.world.item.IHydrationUsable
import java.util.*

object ModEventsRegistries {
Expand Down Expand Up @@ -149,24 +150,17 @@ object ModEventsRegistries {
player.ifInSurvivalAndGetWaterData { waterLevelData ->
WaterSource.LOGGER.info("Player ${player.name} finishes using ${stack.name}")

val isItemDrinkable = stack.item is DrinkableContainer
val data: HydrationData? = if (isItemDrinkable) {
stack.getOrCreateFluidStorageData()?.let { storage ->
HydrationDataManager.SERVER.findByFluid(storage.fluid)
val item = stack.item
if (item is IHydrationUsable){
val data = item.findHydrationData(stack, HydrationDataManager.SERVER)
data?.let {
item.hydrationUse(stack, it, waterLevelData, player)
item.onHydrationUsingFinished(stack)
}
} else {
HydrationDataManager.SERVER.findByItemStack(stack)
}

data?.let { hydrationData ->
val multiplier = if (isItemDrinkable) (stack.item as DrinkableContainer).drinkVolumeMultiplier else 1
val level = hydrationData.level
val saturation = hydrationData.saturation
waterLevelData.restoreWater(level, saturation, multiplier)
waterLevelData.updateToClient(player)
hydrationData.applyEffectsToPlayer(player, multiplier)
if (isItemDrinkable){
stack.extractFluid(WSConfig.UNIT_DRINK_VOLUME)
val data = HydrationDataManager.SERVER.findByItemStack(stack)
data?.let {
IHydrationUsable.restoreWaterFromHydrationData(it, waterLevelData, player)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ object ModBlocks {
}

fun reflectAutoGenDataBlocks(): Iterable<Pair<Block, AutoGenBlockData>> =
ModItems::class.java.getDeclaredFields().filter {
ModBlocks::class.java.getDeclaredFields().filter {
val block = it.get(null) as? Block
val isBlock = block != null
val isAutoGenData = it.getAnnotation(AutoGenBlockData::class.java) != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,25 @@ package xyz.koiro.watersource.world.item

import net.minecraft.entity.player.PlayerEntity
import net.minecraft.item.ItemStack
import net.minecraft.item.ItemUsage
import net.minecraft.server.network.ServerPlayerEntity
import net.minecraft.util.ActionResult
import net.minecraft.util.Hand
import net.minecraft.util.TypedActionResult
import net.minecraft.util.UseAction
import net.minecraft.world.World
import xyz.koiro.watersource.WSConfig
import xyz.koiro.watersource.WaterSource
import xyz.koiro.watersource.api.extractFluid
import xyz.koiro.watersource.api.getOrCreateFluidStorageData
import xyz.koiro.watersource.data.HydrationData
import xyz.koiro.watersource.data.HydrationDataManager
import xyz.koiro.watersource.world.attachment.WaterLevelData

open class DrinkableContainer(
settings: Settings, capacity: Long,
val useDuration: Int = 32,
val drinkVolumeMultiplier: Int = 1
) : FluidContainer(settings, capacity) {
) : FluidContainer(settings, capacity), IHydrationUsable {
override fun use(world: World, user: PlayerEntity, hand: Hand): TypedActionResult<ItemStack> {
val result = super.use(world, user, hand)
if (result.result != ActionResult.PASS) return result
Expand All @@ -33,9 +37,7 @@ open class DrinkableContainer(
val fluid = storageData.fluid
HydrationDataManager.SERVER.findByFluid(fluid)?.let { hydration ->
if (storageData.amount >= WSConfig.UNIT_DRINK_VOLUME) {
WaterSource.LOGGER.info("Debug use drinkable")
//restore player water level
return TypedActionResult.consume(user.getStackInHand(hand))
return ItemUsage.consumeHeldItem(world, user, hand)
}
}
}
Expand All @@ -49,4 +51,25 @@ open class DrinkableContainer(
override fun getUseAction(stack: ItemStack?): UseAction {
return UseAction.DRINK
}

override fun onHydrationUsingFinished(stack: ItemStack) {
updateDamage(stack)
}

override fun hydrationUse(
stack: ItemStack,
hydrationData: HydrationData,
waterLevelData: WaterLevelData,
player: ServerPlayerEntity
) {
val multiplier = drinkVolumeMultiplier
IHydrationUsable.restoreWaterFromHydrationData(hydrationData, waterLevelData, player, multiplier)
stack.extractFluid(WSConfig.UNIT_DRINK_VOLUME)
}

override fun findHydrationData(stack: ItemStack, manager: HydrationDataManager): HydrationData? {
return stack.getOrCreateFluidStorageData()?.let {
manager.findByFluid(it.fluid)
}
}
}
16 changes: 11 additions & 5 deletions src/main/kotlin/xyz/koiro/watersource/world/item/FluidContainer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import xyz.koiro.watersource.*
import xyz.koiro.watersource.api.FluidStorageData
import xyz.koiro.watersource.api.getOrCreateFluidStorageData
import xyz.koiro.watersource.api.insertFluid
import kotlin.math.round

open class FluidContainer(
settings: Settings,
Expand All @@ -37,6 +38,14 @@ open class FluidContainer(
return fluid == currentData.fluid || currentData.isBlank()
}

fun updateDamage(stack: ItemStack) {
stack.getOrCreateFluidStorageData()?.let {
val amount = it.amount
val damage = round((1 - amount.toDouble() / capacity.toDouble()) * maxDamage).toInt()
stack.damage = damage
}
}

override fun use(world: World, user: PlayerEntity, hand: Hand): TypedActionResult<ItemStack> {
if (!world.isClient()) {
//todo get delta
Expand All @@ -46,6 +55,7 @@ open class FluidContainer(
val blockState = world.getBlockState(hit.blockPos)
if (blockState.fluidState.fluid == Fluids.WATER) {
handItem.insertFluid(Fluids.WATER) { it.capacity }
updateDamage(handItem)
return TypedActionResult.success(handItem)
}
}
Expand All @@ -65,7 +75,7 @@ open class FluidContainer(
val nbtCompound = NbtCompound()
this.writeNbt(nbtCompound)
stack.setSubNbt("FluidStorage", nbtCompound)
updateDamageFromAmount(stack, this.getAmount())
updateDamage(stack)
}
}
if (nbt != null) {
Expand All @@ -74,10 +84,6 @@ open class FluidContainer(
return ret
}

open fun updateDamageFromAmount(stack: ItemStack, amount: Long) {
stack.damage = stack.maxDamage - (amount / WSConfig.UNIT_DRINK_VOLUME).toInt()
}

override fun appendTooltip(
stack: ItemStack,
world: World?,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package xyz.koiro.watersource.world.item

import net.minecraft.item.ItemStack
import net.minecraft.server.network.ServerPlayerEntity
import xyz.koiro.watersource.data.HydrationData
import xyz.koiro.watersource.data.HydrationDataManager
import xyz.koiro.watersource.world.attachment.WaterLevelData

interface IHydrationUsable {
fun onHydrationUsingFinished(stack: ItemStack)
fun hydrationUse(
stack: ItemStack,
hydrationData: HydrationData,
waterLevelData: WaterLevelData,
player: ServerPlayerEntity
)
fun findHydrationData(stack: ItemStack, manager: HydrationDataManager): HydrationData?

companion object{
fun restoreWaterFromHydrationData(
hydrationData: HydrationData,
waterLevelData: WaterLevelData,
player: ServerPlayerEntity,
multiplier: Int = 1,
){
val level = hydrationData.level
val saturation = hydrationData.saturation
waterLevelData.restoreWater(level, saturation, multiplier)
waterLevelData.updateToClient(player)
hydrationData.applyEffectsToPlayer(player, multiplier)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ object ModItems {

@AutoGenItemData(enLang = "Purified Water Bottle", cnLang = "净水瓶")
val PURIFIED_WATTER_BOTTLE = registerItem("purified_water_bottle", DrinkOnceItem(FabricItemSettings().maxCount(0), Items.GLASS_BOTTLE))

fun active() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ object ModRecipes {
fun initialize(){
}

val STRAINER_FILTERING_FLUID_TYPE = registerRecipeType("strainer_filtering_fluid_type", StrainerFilteringFluidRecipe.Type())
val STRAINER_FILTERING_FLUID_SERIALIZER = registerRecipeSerializer("strainer_filtering_fluid_serializer", StrainerFilteringFluidRecipe.Serializer())
val STRAINER_FILTERING_FLUID_SERIALIZER = registerRecipeSerializer("strainer_filtering_fluid", StrainerFilteringFluidRecipe.Serializer())

private fun registerRecipeType(registryName: String, recipeType: RecipeType<*>): RecipeType<*> {
return Registry.register(Registries.RECIPE_TYPE, Identifier(WaterSource.MODID, registryName), recipeType)
Expand Down
Loading

0 comments on commit 11bd6fc

Please sign in to comment.