Skip to content

Commit

Permalink
Refined around content
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiiragi283 committed Nov 17, 2024
1 parent bba51c1 commit ba181b5
Show file tree
Hide file tree
Showing 21 changed files with 131 additions and 365 deletions.
4 changes: 2 additions & 2 deletions src/client/kotlin/hiiragi283/ragium/client/RagiumClient.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package hiiragi283.ragium.client

import hiiragi283.ragium.api.RagiumAPI
import hiiragi283.ragium.api.content.HTRegistryContent
import hiiragi283.ragium.api.content.HTContent
import hiiragi283.ragium.api.extension.getOrNull
import hiiragi283.ragium.api.machine.HTMachinePacket
import hiiragi283.ragium.api.machine.block.HTMachineBlockEntityBase
Expand Down Expand Up @@ -72,7 +72,7 @@ object RagiumClient : ClientModInitializer {
addAll(RagiumContents.Hulls.entries)
addAll(RagiumContents.Exporters.entries)
addAll(RagiumContents.Pipes.entries)
}.map(HTRegistryContent<Block>::value).forEach(::registerCutoutMipped)
}.map(HTContent<Block>::value).forEach(::registerCutoutMipped)

RagiumAPI
.getInstance()
Expand Down
65 changes: 53 additions & 12 deletions src/main/kotlin/hiiragi283/ragium/api/content/HTContent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,84 @@ package hiiragi283.ragium.api.content
import hiiragi283.ragium.api.machine.HTMachineTier
import hiiragi283.ragium.api.material.HTMaterialKey
import hiiragi283.ragium.api.material.HTTagPrefix
import net.minecraft.block.Block
import net.minecraft.item.Item
import net.minecraft.item.ItemConvertible
import net.minecraft.registry.Registries
import net.minecraft.registry.Registry
import net.minecraft.registry.RegistryKey
import net.minecraft.registry.RegistryKeys
import net.minecraft.registry.entry.RegistryEntry
import net.minecraft.registry.tag.TagKey
import net.minecraft.util.Identifier
import java.util.function.Supplier

interface HTContent<T : ItemConvertible> :
HTRegistryContent<T>,
ItemConvertible {
override fun asItem(): Item = value.asItem()
interface HTContent<T : Any> : Supplier<T> {
companion object {
@JvmStatic
fun <T : Any> of(key: RegistryKey<T>, entryGetter: (RegistryKey<T>) -> RegistryEntry<T>): HTContent<T> = object : HTContent<T> {
override val key: RegistryKey<T> = key
override val entry: RegistryEntry<T> by lazy { entryGetter(key) }
}

@JvmStatic
fun ofBlock(id: Identifier): HTContent<Block> = of(RegistryKey.of(RegistryKeys.BLOCK, id), Registries.BLOCK::entryOf)

@JvmStatic
fun ofItem(id: Identifier): HTContent<Item> = of(RegistryKey.of(RegistryKeys.ITEM, id), Registries.ITEM::entryOf)
}

val key: RegistryKey<T>
val entry: RegistryEntry<T>

val id: Identifier get() = key.value
val value: T get() = entry.value()

override fun get(): T = value

// Delegated //

interface Delegated<T : Any> : HTContent<T> {
val delegated: HTContent<T>

override val key: RegistryKey<T>
get() = delegated.key
override val entry: RegistryEntry<T>
get() = delegated.entry
}

// Material //

interface Material<T : ItemConvertible> : HTContent<T> {
interface Material<T : ItemConvertible> :
Delegated<T>,
ItemConvertible {
companion object {
@JvmStatic
fun wrapped(prefix: HTTagPrefix, key: HTMaterialKey, item: ItemConvertible): Material<Item> = object : Material<Item> {
fun ofWrapped(prefix: HTTagPrefix, key: HTMaterialKey, item: ItemConvertible): Material<Item> = object : Material<Item> {
override val material: HTMaterialKey = key
override val tagPrefix: HTTagPrefix = prefix
override val registry: Registry<Item> = Registries.ITEM
override val key: RegistryKey<Item>
get() = registry.getKey(item.asItem()).orElseThrow()
override val delegated: HTContent<Item> = object : HTContent<Item> {
override val key: RegistryKey<Item> by lazy { Registries.ITEM.getKey(item.asItem()).orElseThrow() }
override val entry: RegistryEntry<Item> by lazy { Registries.ITEM.entryOf(this.key) }
}
}
}

val material: HTMaterialKey

val tagPrefix: HTTagPrefix

val prefixedTagKey: TagKey<Item>
get() = tagPrefix.createTag(material)

override fun asItem(): Item = value.asItem()
}

// Tier //

interface Tier<T : ItemConvertible> : HTContent<T> {
interface Tier<T : ItemConvertible> :
Delegated<T>,
ItemConvertible {
val tier: HTMachineTier

override fun asItem(): Item = value.asItem()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ object HTHardModeContents {

@JvmField
val COPPER: HTHardModeContent = HTHardModeContent.of(
HTContent.Material.wrapped(
HTContent.Material.ofWrapped(
HTTagPrefix.INGOT,
RagiumMaterialKeys.COPPER,
Items.COPPER_INGOT,
Expand All @@ -30,7 +30,7 @@ object HTHardModeContents {

@JvmField
val GOLD: HTHardModeContent = HTHardModeContent.of(
HTContent.Material.wrapped(
HTContent.Material.ofWrapped(
HTTagPrefix.INGOT,
RagiumMaterialKeys.GOLD,
Items.GOLD_INGOT,
Expand All @@ -40,7 +40,7 @@ object HTHardModeContents {

@JvmField
val IRON: HTHardModeContent = HTHardModeContent.of(
HTContent.Material.wrapped(
HTContent.Material.ofWrapped(
HTTagPrefix.INGOT,
RagiumMaterialKeys.IRON,
Items.IRON_INGOT,
Expand Down
16 changes: 0 additions & 16 deletions src/main/kotlin/hiiragi283/ragium/api/content/HTRegistryContent.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,15 @@ fun <T : PersistentState> getState(
id: Identifier,
): T? = server.getWorld(key)?.let { getState(it, type, id) }

fun <T : PersistentState> getStateFromServer(server: MinecraftServer, type: PersistentState.Type<T>, id: Identifier): T =
getState(server.overworld, type, id)

fun <T : PersistentState> getStateFromServer(world: ServerWorld, type: PersistentState.Type<T>, id: Identifier): T =
getStateFromServer(world.server, type, id)

// Backpack
val MinecraftServer.backpackManager: HTBackpackManager
get() = getState(overworld, HTBackpackManager.TYPE, HTBackpackManager.ID)

val ServerWorld.backpackManager: HTBackpackManager
get() = server.backpackManager
get() = getStateFromServer(this, HTBackpackManager.TYPE, HTBackpackManager.ID)

val WorldAccess.backpackManager: HTBackpackManager?
get() = server?.backpackManager
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package hiiragi283.ragium.api.fluid

import hiiragi283.ragium.common.init.RagiumFluids
import net.minecraft.entity.LivingEntity
import net.minecraft.fluid.Fluid
import net.minecraft.item.ItemStack
import net.minecraft.world.World

object HTFluidDrinkingHandlerRegistry {
@JvmStatic
Expand All @@ -22,14 +19,4 @@ object HTFluidDrinkingHandlerRegistry {

@JvmStatic
fun get(fluid: Fluid): HTFluidDrinkingHandler? = registry[fluid]

@JvmStatic
fun onDrink(
fluid: Fluid,
stack: ItemStack,
world: World,
user: LivingEntity,
) {
get(fluid)?.onDrink(stack, world, user)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class HTFluidResult(val entry: RegistryEntry<Fluid>, val amount: Long = FluidCon
StoragePreconditions.notNegative(amount)
}

@Suppress("DEPRECATION")
constructor(fluid: Fluid, amount: Long = FluidConstants.BUCKET) : this(fluid.registryEntry, amount)

val fluid: Fluid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class HTItemResult(val entry: RegistryEntry<Item>, val count: Int = 1, val compo
check(count > 0) { "Non-Negative count required!" }
}

@Suppress("DEPRECATION")
constructor(item: ItemConvertible, count: Int = 1, components: ComponentChanges = ComponentChanges.EMPTY) : this(
item.asItem().registryEntry,
count,
Expand Down
Loading

0 comments on commit ba181b5

Please sign in to comment.