Skip to content

Commit

Permalink
fix: serialize name
Browse files Browse the repository at this point in the history
  • Loading branch information
gloridifice committed Jun 15, 2024
1 parent c0e7a69 commit ac162c9
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 30 deletions.
33 changes: 26 additions & 7 deletions src/main/kotlin/xyz/koiro/watersource/WSConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@ import kotlinx.serialization.Serializable
import net.minecraft.entity.effect.StatusEffect
import net.minecraft.entity.effect.StatusEffectInstance
import net.minecraft.entity.effect.StatusEffects
import net.minecraft.item.ItemStack
import net.minecraft.registry.Registries
import net.minecraft.util.Identifier
import xyz.koiro.watersource.data.HydrationData
import xyz.koiro.watersource.datagen.provider.HydrationDataProvider
import xyz.koiro.watersource.world.effect.ModStatusEffects
import kotlin.math.floor

object WSConfig {
@Serializable
class Format(
val exhaustion: ExhaustionFormat = ExhaustionFormat(),
val punishment: PunishmentFormat = PunishmentFormat(),
val filtering: FilteringFormat = FilteringFormat()
val filtering: FilteringFormat = FilteringFormat(),
val highWaterPlayerHealingIntervalSecond: Float = 2.5f,
val enableDryFeature: Boolean = true,
val enableDefaultDryData: Boolean = true,
)

@Serializable
Expand Down Expand Up @@ -48,28 +54,28 @@ object WSConfig {
effectsInHard = arrayOf(
EffectObject(StatusEffects.SLOWNESS, 1),
EffectObject(StatusEffects.WEAKNESS, 1)
).map {it.toString() }
).map { it.toString() }
},
val heavyPunishmentInLowWater: LowWaterPunishmentObject = LowWaterPunishmentObject().apply {
effectsInEasy = arrayOf(
EffectObject(StatusEffects.SLOWNESS),
EffectObject(StatusEffects.WEAKNESS, 2)
).map {it.toString() }
).map { it.toString() }
effectsInNormal = arrayOf(
EffectObject(StatusEffects.SLOWNESS, 1),
EffectObject(StatusEffects.WEAKNESS, 2)
).map {it.toString() }
).map { it.toString() }
effectsInHard = arrayOf(
EffectObject(StatusEffects.SLOWNESS, 2),
EffectObject(StatusEffects.WEAKNESS, 2)
).map {it.toString() }
).map { it.toString() }
}
)

@Serializable
class LowWaterPunishmentObject(
val enable: Boolean = true,

var effectsInEasy: List<String> = emptyList(),
var effectsInNormal: List<String> = emptyList(),
var effectsInHard: List<String> = emptyList(),
Expand Down Expand Up @@ -104,13 +110,15 @@ object WSConfig {
fun fromInstance(instance: StatusEffectInstance): EffectObject {
return EffectObject(instance.effectType.identifier().toString(), instance.amplifier)
}
fun fromString(str: String): EffectObject{

fun fromString(str: String): EffectObject {
val list = str.split("::")
val id = list[0]
val amplifier = list[1].toInt()
return EffectObject(id, amplifier)
}
}

override fun toString(): String {
return "$id::$amplifier"
}
Expand Down Expand Up @@ -171,5 +179,16 @@ object WSConfig {
val config
get() = format.filtering
}

fun getDefaultFoodDryData(itemStack: ItemStack): HydrationData? {
if (format.enableDefaultDryData && itemStack.isFood) {
itemStack.item.foodComponent?.hunger?.let {
if (it > 1) {
return HydrationDataProvider.dryItem(itemStack.item, floor(it / 2f).toInt())
}
}
}
return null
}
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package xyz.koiro.watersource.api.serialize

import kotlinx.serialization.SerialInfo
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.Serializer
import net.minecraft.item.Item
import net.minecraft.recipe.Ingredient
import net.minecraft.registry.Registries
Expand All @@ -14,7 +17,9 @@ import kotlin.jvm.optionals.getOrNull

@Serializable
class IngredientFormat(
@SerialName("item")
val itemId: String? = null,
@SerialName("tag")
val tagId: String? = null
) {
fun asItem(): Item?{
Expand Down
21 changes: 12 additions & 9 deletions src/main/kotlin/xyz/koiro/watersource/data/FilterRecipeData.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package xyz.koiro.watersource.data

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import net.minecraft.fluid.Fluid
import net.minecraft.item.ItemStack
Expand All @@ -10,34 +11,36 @@ import xyz.koiro.watersource.api.serialize.IngredientFormat
import xyz.koiro.watersource.identifier

class FilterRecipeData(
val inFluid: Fluid,
val outFluid: Fluid,
val inputFluid: Fluid,
val outputFluid: Fluid,
val strainer: Ingredient,
) {

fun match(fluidIn: Fluid, strainerStack: ItemStack): Boolean {
return fluidIn == this.inFluid && strainer.test(strainerStack)
return fluidIn == this.inputFluid && strainer.test(strainerStack)
}

fun format(): Format {
return Format(
IngredientFormat.fromIngredient(strainer),
inFluid.identifier().toString(),
outFluid.identifier().toString()
inputFluid.identifier().toString(),
outputFluid.identifier().toString()
)
}

constructor(format: Format) : this(
Registries.FLUID.get(Identifier.tryParse(format.fluidIn)),
Registries.FLUID.get(Identifier.tryParse(format.fluidOut)),
Registries.FLUID.get(Identifier.tryParse(format.inputFluid)),
Registries.FLUID.get(Identifier.tryParse(format.outputFluid)),
format.strainer.toIngredient()
)


@Serializable
class Format(
val strainer: IngredientFormat,
val fluidIn: String,
val fluidOut: String
@SerialName("input_fluid")
val inputFluid: String,
@SerialName("output_fluid")
val outputFluid: String
)
}
14 changes: 3 additions & 11 deletions src/main/kotlin/xyz/koiro/watersource/data/HydrationDataManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,19 @@ import net.minecraft.fluid.Fluid
import net.minecraft.item.ItemStack
import net.minecraft.resource.Resource
import net.minecraft.util.Identifier
import xyz.koiro.watersource.WSConfig
import xyz.koiro.watersource.WaterSource
import xyz.koiro.watersource.api.storage.getOrCreateFluidStorageData
import xyz.koiro.watersource.datagen.provider.HydrationDataProvider
import kotlin.math.floor

object HydrationDataManager :
DataManager<HydrationData>(ModResourceRegistries.HYDRATION_KEY, WaterSource.identifier(ModResourceRegistries.HYDRATION_KEY)) {


fun findByItemStack(itemStack: ItemStack): HydrationData? {
val data = sequence?.find {
it.match(itemStack)
} ?: itemStack.getOrCreateFluidStorageData()?.let { data -> sequence?.find { it.match(data.fluid) } }
return data
?: if (itemStack.isFood) {
itemStack.item.foodComponent?.hunger?.let {
if (it > 1) {
HydrationDataProvider.dryItem(itemStack.item, floor(it / 2f).toInt())
} else null
}
} else null
if (!WSConfig.format.enableDryFeature && data?.isDry() == true) return null
return data ?: WSConfig.getDefaultFoodDryData(itemStack)
}

fun findByFluid(fluid: Fluid): HydrationData? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import net.minecraft.entity.effect.StatusEffectInstance
import net.minecraft.server.network.ServerPlayerEntity
import net.minecraft.util.ActionResult
import net.minecraft.world.World
import xyz.koiro.watersource.WSConfig
import xyz.koiro.watersource.WSConfig.Exhaustion
import xyz.koiro.watersource.WSConfig.Punishment
import xyz.koiro.watersource.WaterSource
Expand Down Expand Up @@ -55,11 +56,12 @@ object ModEventsHandlers {
}

private fun highWaterLevelReward(player: ServerPlayerEntity, world: World) {
if (world.isClient()) return
player.ifInSurvivalAndGetWaterData { waterLevelData ->
if (waterLevelData.level > 16 && !player.hasStatusEffect(ModStatusEffects.THIRSTY)) {
val tick = player.getAttachedOrCreate(ModAttachmentTypes.WATER_REWARD_HEAL_TICKER)
tick.add(1)
if (tick.value > 125) {
if (tick.value > (WSConfig.format.highWaterPlayerHealingIntervalSecond * 50).toInt()) {
player.heal(1f)
waterLevelData.addExhaustion(Exhaustion.healthReward, player)
waterLevelData.updateToClient(player)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class FilterBlockEntity(pos: BlockPos, state: BlockState, var capacity: Long, va
FilterRecipeDataManager.findRecipe(upFluidStorageData.fluid, strainerStorage.stack)

val isWorking = recipe != null
&& (downFluidStorageData.fluid == recipe.outFluid || downFluidStorageData.isBlank())
&& (downFluidStorageData.fluid == recipe.outputFluid || downFluidStorageData.isBlank())
&& downFluidStorageData.amount < downFluidStorageData.capacity
&& upFluidStorageData.amount > 0
entity.isWorking = isWorking
Expand All @@ -168,7 +168,7 @@ class FilterBlockEntity(pos: BlockPos, state: BlockState, var capacity: Long, va

// 0.05 * 20 = 1s
if (entity.filterTicks % tickAmount == 0) {
val outFluid = recipe.outFluid
val outFluid = recipe.outputFluid
val shouldDamageStrainer =
entity.filterTicks % (WSConfig.UNIT_DRINK_VOLUME / volumePerSecond * tickAmount).toInt() == 0

Expand Down

0 comments on commit ac162c9

Please sign in to comment.