From c563a301dc75cf60849807f3a2317d75326dab61 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Mon, 10 Oct 2022 16:08:02 +0700 Subject: [PATCH] Rename lazymap/list to correct suppliermap/list --- .../dbotthepony/mc/otm/core/ConditionalSet.kt | 62 ++++++ .../mc/otm/core/ConditionalSupplierSet.kt | 62 ++++++ .../dbotthepony/mc/otm/core/SupplierList.kt | 24 +++ .../ru/dbotthepony/mc/otm/core/SupplierMap.kt | 27 +++ .../ru/dbotthepony/mc/otm/registry/Ext.kt | 6 +- .../dbotthepony/mc/otm/registry/LazyList.kt | 186 ------------------ .../ru/dbotthepony/mc/otm/registry/MItems.kt | 19 +- .../objects/ColoredDecorativeBlock.kt | 9 +- .../otm/registry/objects/DecorativeBlock.kt | 8 +- .../objects/StripedColoredDecorativeBlock.kt | 16 +- 10 files changed, 201 insertions(+), 218 deletions(-) create mode 100644 src/main/kotlin/ru/dbotthepony/mc/otm/core/ConditionalSet.kt create mode 100644 src/main/kotlin/ru/dbotthepony/mc/otm/core/ConditionalSupplierSet.kt create mode 100644 src/main/kotlin/ru/dbotthepony/mc/otm/core/SupplierList.kt create mode 100644 src/main/kotlin/ru/dbotthepony/mc/otm/core/SupplierMap.kt delete mode 100644 src/main/kotlin/ru/dbotthepony/mc/otm/registry/LazyList.kt diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/ConditionalSet.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/ConditionalSet.kt new file mode 100644 index 000000000..0b75642e9 --- /dev/null +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/ConditionalSet.kt @@ -0,0 +1,62 @@ +package ru.dbotthepony.mc.otm.core + +class ConditionalSet : AbstractSet { + // method without boxing + fun interface Condition { + fun check(): Boolean + } + + private val getters: Array> + + constructor(vararg getters: Pair) : super() { + this.getters = Array(getters.size) { getters[it] } + } + + constructor(getters: List>) : super() { + this.getters = Array(getters.size) { getters[it] } + } + + override val size: Int get() { + var i = 0 + + for (pair in getters) { + if (pair.first.check()) { + i++ + } + } + + return i + } + + override fun iterator(): Iterator { + return object : Iterator { + val parent = getters.iterator() + private var pair: Pair? = null + + private fun search() { + for (pair in parent) { + if (pair.first.check()) { + this.pair = pair + return + } + } + + this.pair = null + } + + init { + search() + } + + override fun hasNext(): Boolean { + return pair != null + } + + override fun next(): T { + val pair = pair ?: throw NoSuchElementException() + search() + return pair.second + } + } + } +} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/ConditionalSupplierSet.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/ConditionalSupplierSet.kt new file mode 100644 index 000000000..f3454ceb0 --- /dev/null +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/ConditionalSupplierSet.kt @@ -0,0 +1,62 @@ +package ru.dbotthepony.mc.otm.core + +class ConditionalSupplierSet : AbstractSet { + // method without boxing + fun interface Condition { + fun check(): Boolean + } + + private val getters: Array T>> + + constructor(vararg getters: Pair T>) : super() { + this.getters = Array(getters.size) { getters[it] } + } + + constructor(getters: List T>>) : super() { + this.getters = Array(getters.size) { getters[it] } + } + + override val size: Int get() { + var i = 0 + + for (pair in getters) { + if (pair.first.check()) { + i++ + } + } + + return i + } + + override fun iterator(): Iterator { + return object : Iterator { + val parent = getters.iterator() + private var pair: Pair T>? = null + + private fun search() { + for (pair in parent) { + if (pair.first.check()) { + this.pair = pair + return + } + } + + this.pair = null + } + + init { + search() + } + + override fun hasNext(): Boolean { + return pair != null + } + + override fun next(): T { + val pair = pair ?: throw NoSuchElementException() + search() + return pair.second.invoke() + } + } + } +} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/SupplierList.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/SupplierList.kt new file mode 100644 index 000000000..603b899d7 --- /dev/null +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/SupplierList.kt @@ -0,0 +1,24 @@ +package ru.dbotthepony.mc.otm.core + +class SupplierList : AbstractList { + private val getters: Array<() -> T> + + constructor(vararg getters: () -> T) : super() { + this.getters = Array(getters.size) { getters[it] } + } + + constructor(getters: List<() -> T>) : super() { + this.getters = Array(getters.size) { getters[it] } + } + + constructor(size: Int, provider: (Int) -> () -> T) { + this.getters = Array(size, provider) + } + + override val size: Int + get() = getters.size + + override fun get(index: Int): T { + return getters[index].invoke() + } +} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/SupplierMap.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/SupplierMap.kt new file mode 100644 index 000000000..744459899 --- /dev/null +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/SupplierMap.kt @@ -0,0 +1,27 @@ +package ru.dbotthepony.mc.otm.core + +import com.google.common.collect.ImmutableSet + +class SupplierMap : AbstractMap { + override val entries: Set> + + constructor(vararg mValues: Pair T>) : super() { + entries = ImmutableSet.copyOf(mValues.map { Entry(it.first, it.second) }) + } + + constructor(mValues: Collection T>>) : super() { + entries = ImmutableSet.copyOf(mValues.map { Entry(it.first, it.second) }) + } + + constructor(mValues: Map T>) : super() { + entries = ImmutableSet.copyOf(mValues.map { Entry(it.key, it.value) }) + } + + private inner class Entry( + override val key: K, + private val getter: () -> T + ) : Map.Entry { + override val value: T + get() = getter.invoke() + } +} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/Ext.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/Ext.kt index a43f5f3d4..8819eecbb 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/Ext.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/Ext.kt @@ -2,7 +2,7 @@ package ru.dbotthepony.mc.otm.registry import net.minecraft.world.item.DyeColor import net.minecraftforge.registries.DeferredRegister -import ru.dbotthepony.mc.otm.item.MinecartCargoCrateItem +import ru.dbotthepony.mc.otm.core.SupplierMap private fun DeferredRegister.doColored(prefix: String, factory: (color: DyeColor, name: String) -> T): MutableCollection T>> { return mutableListOf( @@ -26,10 +26,10 @@ private fun DeferredRegister.doColored(prefix: String, factory: (color: D } fun DeferredRegister.colored(prefix: String, factory: (color: DyeColor, name: String) -> T): Map { - return LazyMap(doColored(prefix, factory)) + return SupplierMap(doColored(prefix, factory)) } @Suppress("unchecked_cast") fun DeferredRegister.allColored(prefix: String, factory: (color: DyeColor?, name: String) -> T): Map { - return LazyMap(doColored(prefix, factory).also { (it as MutableCollection T>>).add((null as DyeColor?) to register(prefix) { factory.invoke(null, prefix) }::get) }) + return SupplierMap(doColored(prefix, factory).also { (it as MutableCollection T>>).add((null as DyeColor?) to register(prefix) { factory.invoke(null, prefix) }::get) }) } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/LazyList.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/LazyList.kt deleted file mode 100644 index 94c869bfd..000000000 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/LazyList.kt +++ /dev/null @@ -1,186 +0,0 @@ -package ru.dbotthepony.mc.otm.registry - -import com.google.common.collect.ImmutableList -import com.google.common.collect.ImmutableSet -import net.minecraft.world.item.DyeColor -import net.minecraftforge.registries.RegistryObject - -class LazyList : AbstractList { - private val getters: Array<() -> T> - - constructor(vararg getters: () -> T) : super() { - this.getters = Array(getters.size) { getters[it] } - } - - constructor(getters: List<() -> T>) : super() { - this.getters = Array(getters.size) { getters[it] } - } - - constructor(size: Int, provider: (Int) -> () -> T) { - this.getters = Array(size, provider) - } - - override val size: Int - get() = getters.size - - override fun get(index: Int): T { - return getters[index].invoke() - } -} - -class ConditionalSet : AbstractSet { - // method without boxing - fun interface Condition { - fun check(): Boolean - } - - private val getters: Array> - - constructor(vararg getters: Pair) : super() { - this.getters = Array(getters.size) { getters[it] } - } - - constructor(getters: List>) : super() { - this.getters = Array(getters.size) { getters[it] } - } - - override val size: Int get() { - var i = 0 - - for (pair in getters) { - if (pair.first.check()) { - i++ - } - } - - return i - } - - override fun iterator(): Iterator { - return object : Iterator { - val parent = getters.iterator() - private var pair: Pair? = null - - private fun search() { - for (pair in parent) { - if (pair.first.check()) { - this.pair = pair - return - } - } - - this.pair = null - } - - init { - search() - } - - override fun hasNext(): Boolean { - return pair != null - } - - override fun next(): T { - val pair = pair ?: throw NoSuchElementException() - search() - return pair.second - } - } - } -} - -class ConditionalLazySet : AbstractSet { - // method without boxing - fun interface Condition { - fun check(): Boolean - } - - private val getters: Array T>> - - constructor(vararg getters: Pair T>) : super() { - this.getters = Array(getters.size) { getters[it] } - } - - constructor(getters: List T>>) : super() { - this.getters = Array(getters.size) { getters[it] } - } - - override val size: Int get() { - var i = 0 - - for (pair in getters) { - if (pair.first.check()) { - i++ - } - } - - return i - } - - override fun iterator(): Iterator { - return object : Iterator { - val parent = getters.iterator() - private var pair: Pair T>? = null - - private fun search() { - for (pair in parent) { - if (pair.first.check()) { - this.pair = pair - return - } - } - - this.pair = null - } - - init { - search() - } - - override fun hasNext(): Boolean { - return pair != null - } - - override fun next(): T { - val pair = pair ?: throw NoSuchElementException() - search() - return pair.second.invoke() - } - } - } -} - -class LazyMap : AbstractMap { - override val entries: Set> - - constructor(vararg mValues: Pair T>) : super() { - entries = ImmutableSet.copyOf(mValues.map { Entry(it.first, it.second) }) - } - - constructor(mValues: Collection T>>) : super() { - entries = ImmutableSet.copyOf(mValues.map { Entry(it.first, it.second) }) - } - - constructor(mValues: Map T>) : super() { - entries = ImmutableSet.copyOf(mValues.map { Entry(it.key, it.value) }) - } - - private inner class Entry( - override val key: K, - private val getter: () -> T - ) : Map.Entry { - override val value: T - get() = getter.invoke() - } -} - -class RegistryObjectList(private vararg val getters: RegistryObject) : AbstractList() { - constructor(getters: Collection>) : this(*getters.toTypedArray()) - - override val size: Int - get() = getters.size - - override fun get(index: Int): T { - return getters[index].get() - } -} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MItems.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MItems.kt index 45f021d19..b5fe6dee1 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MItems.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MItems.kt @@ -1,7 +1,6 @@ package ru.dbotthepony.mc.otm.registry -import com.google.common.collect.Streams import net.minecraft.ChatFormatting import net.minecraft.network.chat.Component import net.minecraft.tags.BlockTags @@ -16,14 +15,10 @@ import net.minecraftforge.registries.DeferredRegister import net.minecraftforge.registries.ForgeRegistries import ru.dbotthepony.mc.otm.OverdriveThatMatters import ru.dbotthepony.mc.otm.ServerConfig +import ru.dbotthepony.mc.otm.core.SupplierList import ru.dbotthepony.mc.otm.core.TranslatableComponent -import ru.dbotthepony.mc.otm.core.ImpreciseFraction -import ru.dbotthepony.mc.otm.core.toUUID import ru.dbotthepony.mc.otm.item.* import ru.dbotthepony.mc.otm.item.weapon.PlasmaRifleItem -import java.util.* -import java.util.stream.Stream -import kotlin.collections.ArrayList object MItems { private val DEFAULT_PROPERTIES = Item.Properties().stacksTo(64).tab(OverdriveThatMatters.INSTANCE.CREATIVE_TAB) @@ -111,7 +106,7 @@ object MItems { } } - val MACHINES = LazyList( + val MACHINES = SupplierList( ::ANDROID_STATION, ::BATTERY_BANK, ::MATTER_DECOMPOSER, ::MATTER_CAPACITOR_BANK, ::MATTER_CABLE, ::PATTERN_STORAGE, ::MATTER_SCANNER, ::MATTER_PANEL, ::MATTER_REPLICATOR, ::MATTER_BOTTLER, ::ENERGY_COUNTER, ::CHEMICAL_GENERATOR, ::PLATE_PRESS, ::MATTER_RECYCLER, ::STORAGE_BUS, ::STORAGE_IMPORTER, ::STORAGE_EXPORTER, ::DRIVE_VIEWER, @@ -146,7 +141,7 @@ object MItems { val TRITANIUM_PICKAXE: Item by registry.register(MNames.TRITANIUM_PICKAXE) { PickaxeItem(TRITANIUM_COMPONENT, 2, -2.8f, TOOLS_PROPRTIES) } val TRITANIUM_HOE: Item by registry.register(MNames.TRITANIUM_HOE) { HoeItem(TRITANIUM_COMPONENT, 0, -3.4f, TOOLS_PROPRTIES) } - val TRITANIUM_TOOLS = LazyList( + val TRITANIUM_TOOLS = SupplierList( { TRITANIUM_SWORD }, { TRITANIUM_SHOVEL }, { TRITANIUM_AXE }, @@ -159,7 +154,7 @@ object MItems { val TRITANIUM_PANTS: Item by registry.register(MNames.TRITANIUM_PANTS) { ItemTritaniumArmor(EquipmentSlot.LEGS) } val TRITANIUM_BOOTS: Item by registry.register(MNames.TRITANIUM_BOOTS) { ItemTritaniumArmor(EquipmentSlot.FEET) } - val TRITANIUM_ARMOR = LazyList( + val TRITANIUM_ARMOR = SupplierList( { TRITANIUM_HELMET }, { TRITANIUM_CHESTPLATE }, { TRITANIUM_PANTS }, @@ -209,7 +204,7 @@ object MItems { val QUANTUM_BATTERY_CREATIVE: Item by registry.register(MNames.QUANTUM_BATTERY_CREATIVE) { QuantumBatteryItem(MNames.QUANTUM_BATTERY_CREATIVE) } val ZPM_BATTERY: Item by registry.register(MNames.ZPM_BATTERY) { ZPMItem() } - val BATTERIES = LazyList( + val BATTERIES = SupplierList( { BATTERY_CRUDE }, { BATTERY_BASIC }, { BATTERY_NORMAL }, @@ -277,7 +272,7 @@ object MItems { } } } - val DATAGEN_COMPONENTS = LazyList( + val DATAGEN_COMPONENTS = SupplierList( { ENERGY_BUS }, { ELECTRIC_PARTS }, { TRITANIUM_PLATE }, @@ -305,7 +300,7 @@ object MItems { val INVENTORY_UPGRADE_CREATIVE: Item by registry.register("exosuit_inventory_upgrade_creative") { ExoSuitSlotUpgradeItem(null, 27, Rarity.EPIC) } val CRAFTING_UPGRADE: Item by registry.register("exosuit_crafting_upgrade", ::ExoSuitCraftingUpgradeItem) - val INVENTORY_UPGRADES = LazyList(8) { + val INVENTORY_UPGRADES = SupplierList(8) { registry.register("exosuit_inventory_upgrade_$it") { ExoSuitSlotUpgradeItem(18, Rarity.COMMON) }::get } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/objects/ColoredDecorativeBlock.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/objects/ColoredDecorativeBlock.kt index 066def2b7..81416befa 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/objects/ColoredDecorativeBlock.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/objects/ColoredDecorativeBlock.kt @@ -9,8 +9,7 @@ import net.minecraft.world.level.block.state.BlockBehaviour import net.minecraftforge.registries.DeferredRegister import net.minecraftforge.registries.RegistryObject import ru.dbotthepony.mc.otm.OverdriveThatMatters -import ru.dbotthepony.mc.otm.registry.LazyMap -import ru.dbotthepony.mc.otm.registry.MRegistry +import ru.dbotthepony.mc.otm.core.SupplierMap import java.util.EnumMap /** @@ -81,12 +80,12 @@ open class ColoredDecorativeBlock( val blocks: Map by lazy { check(registeredBlocks) { "Didn't register blocks yet" } - LazyMap(blockMap.map { it.key to it.value::get }) + SupplierMap(blockMap.map { it.key to it.value::get }) } val items: Map by lazy { check(registeredItems) { "Didn't register items yet" } - LazyMap(itemMap.map { it.key to it.value::get }) + SupplierMap(itemMap.map { it.key to it.value::get }) } open fun registerBlocks(registry: DeferredRegister) { @@ -145,4 +144,4 @@ open class ColoredDecorativeBlock( }, itemGroup) } } -} \ No newline at end of file +} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/objects/DecorativeBlock.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/objects/DecorativeBlock.kt index 6407e77fb..4f7f8dd39 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/objects/DecorativeBlock.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/objects/DecorativeBlock.kt @@ -9,7 +9,7 @@ import net.minecraft.world.level.block.state.BlockBehaviour import net.minecraftforge.registries.DeferredRegister import net.minecraftforge.registries.RegistryObject import ru.dbotthepony.mc.otm.OverdriveThatMatters -import ru.dbotthepony.mc.otm.registry.LazyMap +import ru.dbotthepony.mc.otm.core.SupplierMap import ru.dbotthepony.mc.otm.registry.WriteOnce /** @@ -29,12 +29,12 @@ class DecorativeBlock( val allBlocks: Map by lazy { check(registeredBlocks) { "Didn't register items yet" } - LazyMap(blockMap.map { it.key to it.value::get }.toMutableList().also { it.add(null to _block::get) }) + SupplierMap(blockMap.map { it.key to it.value::get }.toMutableList().also { it.add(null to _block::get) }) } val allItems: Map by lazy { check(registeredItems) { "Didn't register items yet" } - LazyMap(itemMap.map { it.key to it.value::get }.toMutableList().also { it.add(null to _item::get) }) + SupplierMap(itemMap.map { it.key to it.value::get }.toMutableList().also { it.add(null to _item::get) }) } override fun registerBlocks(registry: DeferredRegister) { @@ -54,4 +54,4 @@ class DecorativeBlock( }, itemGroup) } } -} \ No newline at end of file +} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/objects/StripedColoredDecorativeBlock.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/objects/StripedColoredDecorativeBlock.kt index 7c55341df..ba916e079 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/objects/StripedColoredDecorativeBlock.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/objects/StripedColoredDecorativeBlock.kt @@ -9,8 +9,8 @@ import net.minecraft.world.level.block.Block import net.minecraftforge.registries.DeferredRegister import net.minecraftforge.registries.RegistryObject import ru.dbotthepony.mc.otm.OverdriveThatMatters -import ru.dbotthepony.mc.otm.registry.LazyList -import ru.dbotthepony.mc.otm.registry.LazyMap +import ru.dbotthepony.mc.otm.core.SupplierList +import ru.dbotthepony.mc.otm.core.SupplierMap import java.util.EnumMap @Suppress("PropertyName", "unused", "ReplaceGetOrSet", "ReplacePutWithAssignment") @@ -55,7 +55,7 @@ class StripedColoredDecorativeBlock( val builder = ImmutableMap.Builder>() for ((base, children) in mapItems) { - builder.put(base, LazyMap(children.map { it.key to it.value::get })) + builder.put(base, SupplierMap(children.map { it.key to it.value::get })) } builder.build() @@ -66,7 +66,7 @@ class StripedColoredDecorativeBlock( val builder = ImmutableMap.Builder>() for ((base, children) in mapBlocks) { - builder.put(base, LazyMap(children.map { it.key to it.value::get })) + builder.put(base, SupplierMap(children.map { it.key to it.value::get })) } builder.build() @@ -74,12 +74,12 @@ class StripedColoredDecorativeBlock( val flatItems: List by lazy { check(registeredItems) { "Didn't register items yet" } - LazyList(mapItems.flatMap { it.value.values }.map { it::get }) + SupplierList(mapItems.flatMap { it.value.values }.map { it::get }) } val flatBlocks: List by lazy { check(registeredBlocks) { "Didn't register items yet" } - LazyList(mapBlocks.flatMap { it.value.values }.map { it::get }) + SupplierList(mapBlocks.flatMap { it.value.values }.map { it::get }) } fun registerItems(registry: DeferredRegister) { @@ -125,7 +125,7 @@ class StripedColoredDecorativeBlock( } } - LazyList(build.build()) + SupplierList(build.build()) } val itemsWithColor: List>> by lazy { @@ -141,6 +141,6 @@ class StripedColoredDecorativeBlock( } } - LazyList(build.build()) + SupplierList(build.build()) } }