Switch more code to use kommons

This commit is contained in:
DBotThePony 2025-03-30 23:56:05 +07:00
parent c5c7e1bac4
commit 4b38528fe0
Signed by: DBot
GPG Key ID: DCC23B5715498507
60 changed files with 122 additions and 616 deletions

View File

@ -27,7 +27,7 @@ import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets
import net.minecraft.world.level.storage.loot.predicates.LootItemBlockStatePropertyCondition
import net.minecraft.world.level.storage.loot.providers.number.ConstantValue
import net.neoforged.neoforge.data.event.GatherDataEvent
import ru.dbotthepony.mc.otm.util.stream
import ru.dbotthepony.kommons.collect.stream
import ru.dbotthepony.mc.otm.data.loot.CopyTileNbtFunction
import java.util.concurrent.CompletableFuture

View File

@ -21,6 +21,8 @@ import net.minecraft.world.level.levelgen.structure.BoundingBox
import net.minecraft.world.phys.AABB
import net.minecraft.world.phys.Vec3
import net.neoforged.neoforge.common.Tags
import ru.dbotthepony.kommons.collect.addAll
import ru.dbotthepony.kommons.collect.map
import ru.dbotthepony.kommons.util.getValue
import ru.dbotthepony.kommons.util.setValue
import ru.dbotthepony.mc.otm.block.BlackHoleBlock
@ -29,8 +31,6 @@ import ru.dbotthepony.mc.otm.block.entity.MatteryBlockEntity
import ru.dbotthepony.mc.otm.client.minecraft
import ru.dbotthepony.mc.otm.client.sound.BlackHoleAmbientSoundInstance
import ru.dbotthepony.mc.otm.config.ServerConfig
import ru.dbotthepony.mc.otm.util.addAll
import ru.dbotthepony.mc.otm.util.collect.map
import ru.dbotthepony.mc.otm.util.damageType
import ru.dbotthepony.mc.otm.util.getExplosionResistance
import ru.dbotthepony.mc.otm.util.gracefulBlockBreak

View File

@ -9,6 +9,7 @@ import net.minecraft.world.entity.player.Player
import net.minecraft.world.inventory.AbstractContainerMenu
import net.minecraft.world.level.block.state.BlockState
import net.neoforged.neoforge.capabilities.Capabilities
import ru.dbotthepony.kommons.collect.map
import ru.dbotthepony.kommons.collect.toList
import ru.dbotthepony.kommons.util.DelegateSetter
import ru.dbotthepony.kommons.util.getValue
@ -24,7 +25,6 @@ import ru.dbotthepony.mc.otm.capability.matter.CombinedProfiledMatterStorage
import ru.dbotthepony.mc.otm.config.MachinesConfig
import ru.dbotthepony.mc.otm.config.ServerConfig
import ru.dbotthepony.mc.otm.util.TranslatableComponent
import ru.dbotthepony.mc.otm.util.collect.map
import ru.dbotthepony.mc.otm.multiblock.ShapedMultiblock
import ru.dbotthepony.mc.otm.util.getBlockStateNow
import ru.dbotthepony.kommons.guava.immutableList

View File

@ -9,8 +9,8 @@ import ru.dbotthepony.mc.otm.SERVER_IS_LIVE
import ru.dbotthepony.mc.otm.UNIVERSE_TICKS
import ru.dbotthepony.mc.otm.capability.receiveEnergy
import ru.dbotthepony.mc.otm.config.CablesConfig
import ru.dbotthepony.mc.otm.util.collect.map
import ru.dbotthepony.mc.otm.util.collect.reduce
import ru.dbotthepony.kommons.collect.map
import ru.dbotthepony.kommons.collect.reduce
import ru.dbotthepony.mc.otm.util.math.Decimal
import ru.dbotthepony.mc.otm.util.math.RelativeSide
import ru.dbotthepony.mc.otm.util.otmRandom

View File

@ -30,10 +30,10 @@ import ru.dbotthepony.mc.otm.container.slotted.AutomationFilters
import ru.dbotthepony.mc.otm.container.slotted.FilteredContainerSlot
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
import ru.dbotthepony.mc.otm.util.SimpleCache
import ru.dbotthepony.mc.otm.util.collect.filter
import ru.dbotthepony.mc.otm.util.collect.forEach
import ru.dbotthepony.mc.otm.util.collect.map
import ru.dbotthepony.mc.otm.util.collect.toList
import ru.dbotthepony.kommons.collect.filter
import ru.dbotthepony.kommons.collect.forEach
import ru.dbotthepony.kommons.collect.map
import ru.dbotthepony.kommons.collect.toList
import ru.dbotthepony.mc.otm.util.math.Decimal
import ru.dbotthepony.mc.otm.container.ItemStackKey
import ru.dbotthepony.mc.otm.container.asKey

View File

@ -17,7 +17,7 @@ import ru.dbotthepony.mc.otm.block.entity.MatteryDeviceBlockEntity
import ru.dbotthepony.mc.otm.capability.matter.*
import ru.dbotthepony.mc.otm.container.slotted.ContainerSlot
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
import ru.dbotthepony.mc.otm.util.collect.map
import ru.dbotthepony.kommons.collect.map
import ru.dbotthepony.mc.otm.util.isNotEmpty
import ru.dbotthepony.mc.otm.graph.matter.SimpleMatterNode
import ru.dbotthepony.mc.otm.registry.game.MBlockEntities

View File

@ -26,9 +26,9 @@ import ru.dbotthepony.mc.otm.container.util.awareStream
import ru.dbotthepony.mc.otm.container.util.iterator
import ru.dbotthepony.mc.otm.util.collect.AwareItemStack
import ru.dbotthepony.mc.otm.util.collect.ContainerItemStackEntry
import ru.dbotthepony.mc.otm.util.collect.concatIterators
import ru.dbotthepony.mc.otm.util.collect.filter
import ru.dbotthepony.mc.otm.util.collect.map
import ru.dbotthepony.kommons.collect.concatIterators
import ru.dbotthepony.kommons.collect.filter
import ru.dbotthepony.kommons.collect.map
import ru.dbotthepony.mc.otm.util.isNotEmpty
import ru.dbotthepony.mc.otm.util.math.Decimal
import ru.dbotthepony.mc.otm.util.formatFluidLevel

View File

@ -2,8 +2,8 @@ package ru.dbotthepony.mc.otm.capability.energy
import net.minecraft.util.RandomSource
import ru.dbotthepony.mc.otm.capability.FlowDirection
import ru.dbotthepony.mc.otm.util.collect.map
import ru.dbotthepony.mc.otm.util.collect.reduce
import ru.dbotthepony.kommons.collect.map
import ru.dbotthepony.kommons.collect.reduce
import ru.dbotthepony.mc.otm.util.math.Decimal
import ru.dbotthepony.mc.otm.util.CapabilityListIterator
import java.util.function.Supplier

View File

@ -5,8 +5,8 @@ import ru.dbotthepony.mc.otm.capability.AbstractProfiledStorage
import ru.dbotthepony.mc.otm.capability.FlowDirection
import ru.dbotthepony.mc.otm.capability.IProfiledStorage
import ru.dbotthepony.mc.otm.util.chart.CombinedDecimalHistoryChart
import ru.dbotthepony.mc.otm.util.collect.map
import ru.dbotthepony.mc.otm.util.collect.reduce
import ru.dbotthepony.kommons.collect.map
import ru.dbotthepony.kommons.collect.reduce
import ru.dbotthepony.mc.otm.util.math.Decimal
import ru.dbotthepony.mc.otm.util.ITickable
import java.util.function.Supplier

View File

@ -2,8 +2,8 @@ package ru.dbotthepony.mc.otm.capability.matter
import net.minecraft.util.RandomSource
import ru.dbotthepony.mc.otm.capability.FlowDirection
import ru.dbotthepony.mc.otm.util.collect.map
import ru.dbotthepony.mc.otm.util.collect.reduce
import ru.dbotthepony.kommons.collect.map
import ru.dbotthepony.kommons.collect.reduce
import ru.dbotthepony.mc.otm.util.math.Decimal
import ru.dbotthepony.mc.otm.util.CapabilityListIterator
import java.util.function.Supplier

View File

@ -5,8 +5,8 @@ import ru.dbotthepony.mc.otm.capability.AbstractProfiledStorage
import ru.dbotthepony.mc.otm.capability.FlowDirection
import ru.dbotthepony.mc.otm.capability.IProfiledStorage
import ru.dbotthepony.mc.otm.util.chart.CombinedDecimalHistoryChart
import ru.dbotthepony.mc.otm.util.collect.map
import ru.dbotthepony.mc.otm.util.collect.reduce
import ru.dbotthepony.kommons.collect.map
import ru.dbotthepony.kommons.collect.reduce
import ru.dbotthepony.mc.otm.util.math.Decimal
import ru.dbotthepony.mc.otm.util.ITickable
import java.util.function.Supplier

View File

@ -17,6 +17,7 @@ import net.neoforged.neoforge.client.event.MovementInputUpdateEvent
import net.neoforged.neoforge.client.event.ScreenEvent
import net.neoforged.neoforge.event.entity.player.ItemTooltipEvent
import net.neoforged.neoforge.network.PacketDistributor
import ru.dbotthepony.kommons.collect.addAll
import ru.dbotthepony.mc.otm.config.ClientConfig
import ru.dbotthepony.mc.otm.player.android.feature.JumpBoostFeature
import ru.dbotthepony.mc.otm.player.MatteryPlayer
@ -32,7 +33,6 @@ import ru.dbotthepony.mc.otm.client.screen.panels.button.ButtonPanel
import ru.dbotthepony.mc.otm.compat.vanilla.InventoryScrollPacket
import ru.dbotthepony.mc.otm.compat.cos.isCosmeticArmorScreen
import ru.dbotthepony.mc.otm.util.TranslatableComponent
import ru.dbotthepony.mc.otm.util.addAll
import ru.dbotthepony.mc.otm.util.math.integerDivisionUp
import ru.dbotthepony.mc.otm.menu.MatteryMenu
import ru.dbotthepony.mc.otm.registry.game.AndroidFeatures

View File

@ -5,8 +5,8 @@ import com.google.gson.JsonPrimitive
import com.google.gson.JsonSyntaxException
import net.minecraft.network.FriendlyByteBuf
import net.minecraft.resources.ResourceLocation
import ru.dbotthepony.kommons.gson.set
import ru.dbotthepony.mc.otm.client.render.UVWindingOrder
import ru.dbotthepony.mc.otm.util.set
enum class SpriteType {
SINGLE {

View File

@ -25,8 +25,8 @@ import ru.dbotthepony.mc.otm.client.render.popScissorRect
import ru.dbotthepony.mc.otm.client.render.pushScissorRect
import ru.dbotthepony.mc.otm.client.screen.MatteryScreen
import ru.dbotthepony.mc.otm.client.screen.panels.input.QueryUserPanel
import ru.dbotthepony.mc.otm.util.collect.concatIterators
import ru.dbotthepony.mc.otm.util.collect.flatMap
import ru.dbotthepony.kommons.collect.concatIterators
import ru.dbotthepony.kommons.collect.flatMap
import ru.dbotthepony.mc.otm.util.GJRAND64RandomSource
import java.util.*
import java.util.concurrent.CopyOnWriteArrayList

View File

@ -14,6 +14,7 @@ import net.minecraft.client.gui.screens.Screen
import net.minecraft.client.renderer.GameRenderer
import net.minecraft.network.chat.Component
import org.joml.Vector2i
import ru.dbotthepony.kommons.collect.addAll
import ru.dbotthepony.mc.otm.client.CursorType
import ru.dbotthepony.mc.otm.client.render.MGUIGraphics
import ru.dbotthepony.mc.otm.client.isCtrlDown
@ -26,9 +27,6 @@ import ru.dbotthepony.mc.otm.client.render.tesselator
import ru.dbotthepony.mc.otm.client.screen.panels.DockProperty
import ru.dbotthepony.mc.otm.client.screen.panels.EditablePanel
import ru.dbotthepony.mc.otm.util.TextComponent
import ru.dbotthepony.mc.otm.util.addAll
import ru.dbotthepony.mc.otm.util.collect.map
import ru.dbotthepony.mc.otm.util.collect.reduce
import ru.dbotthepony.kommons.math.RGBAColor
import ru.dbotthepony.mc.otm.client.render.vertex
import ru.dbotthepony.mc.otm.milliTime
@ -957,7 +955,7 @@ open class TextInputPanel<out S : Screen>(
return true
}
var index = cursorRow + (0 until cursorLine).iterator().map { this[it]?.length ?: 0 }.reduce(0, Int::plus)
var index = cursorRow + (0 until cursorLine).sumOf { this[it]?.length ?: 0 }
val insert = minecraft.keyboardHandler.clipboard.replace("\t", " ").filter { acceptsCharacter(it, 0, index++) }.split(NEWLINES).toMutableList()
val actualLastSize = insert.lastOrNull()?.length ?: 0
val line = this[cursorLine]
@ -1009,7 +1007,7 @@ open class TextInputPanel<out S : Screen>(
}
}
} else {
var index = cursorRow + (0 until cursorLine).iterator().map { this[it]?.length ?: 0 }.reduce(0, Int::plus)
var index = cursorRow + (0 until cursorLine).sumOf { this[it]?.length ?: 0 }
val insert = minecraft.keyboardHandler.clipboard.replace("\t", " ").replace(NEWLINES, "").filter { acceptsCharacter(it, 0, index++) }
val line = this[cursorLine]

View File

@ -5,10 +5,6 @@ import ru.dbotthepony.mc.otm.client.render.RenderGravity
import ru.dbotthepony.mc.otm.client.screen.panels.Dock
import ru.dbotthepony.mc.otm.client.screen.panels.EditablePanel
import ru.dbotthepony.mc.otm.client.screen.panels.slot.AbstractSlotPanel
import ru.dbotthepony.mc.otm.util.collect.filter
import ru.dbotthepony.mc.otm.util.collect.maybe
import java.util.stream.IntStream
import java.util.stream.Stream
import kotlin.math.max
open class GridPanel<out S : Screen>(

View File

@ -27,8 +27,8 @@ import ru.dbotthepony.mc.otm.compat.cos.CosmeticToggleButton.Companion.BUTTON_IN
import ru.dbotthepony.mc.otm.container.util.awareStream
import ru.dbotthepony.mc.otm.container.util.iterator
import ru.dbotthepony.mc.otm.util.collect.AwareItemStack
import ru.dbotthepony.mc.otm.util.collect.emptyIterator
import ru.dbotthepony.mc.otm.menu.MatteryMenuSlot
import java.util.Collections.emptyIterator
import java.util.stream.Stream
val isCosmeticArmorLoaded by lazy {

View File

@ -23,16 +23,15 @@ import ru.dbotthepony.mc.otm.client.screen.panels.slot.SlotPanel
import ru.dbotthepony.mc.otm.container.util.awareStream
import ru.dbotthepony.mc.otm.container.util.iterator
import ru.dbotthepony.mc.otm.util.collect.AwareItemStack
import ru.dbotthepony.mc.otm.util.collect.concatIterators
import ru.dbotthepony.mc.otm.util.collect.emptyIterator
import ru.dbotthepony.kommons.collect.concatIterators
import ru.dbotthepony.mc.otm.menu.PlayerSlot
import top.theillusivec4.curios.api.CuriosApi
import top.theillusivec4.curios.api.CuriosCapability
import top.theillusivec4.curios.api.event.SlotModifiersUpdatedEvent
import top.theillusivec4.curios.common.inventory.CosmeticCurioSlot
import top.theillusivec4.curios.common.inventory.CurioSlot
import top.theillusivec4.curios.common.network.client.CPacketOpenCurios
import top.theillusivec4.curios.common.network.client.CPacketToggleRender
import java.util.Collections.emptyIterator
import java.util.stream.Stream
import kotlin.collections.ArrayList

View File

@ -24,10 +24,10 @@ import ru.dbotthepony.mc.otm.client.screen.MatteryScreen
import ru.dbotthepony.mc.otm.client.screen.panels.slot.AbstractSlotPanel
import ru.dbotthepony.mc.otm.client.screen.widget.FluidGaugePanel
import ru.dbotthepony.mc.otm.util.ResourceLocation
import ru.dbotthepony.mc.otm.util.collect.filter
import ru.dbotthepony.mc.otm.util.collect.filterIsInstance
import ru.dbotthepony.mc.otm.util.collect.map
import ru.dbotthepony.mc.otm.util.collect.toList
import ru.dbotthepony.kommons.collect.filter
import ru.dbotthepony.kommons.collect.filterIsInstance
import ru.dbotthepony.kommons.collect.map
import ru.dbotthepony.kommons.collect.toList
import ru.dbotthepony.mc.otm.menu.decorative.PainterMenu
import ru.dbotthepony.mc.otm.menu.matter.MatterEntanglerMenu
import ru.dbotthepony.mc.otm.menu.tech.PlatePressMenu

View File

@ -5,7 +5,7 @@ import net.minecraft.world.item.ItemStack
import ru.dbotthepony.kommons.collect.iterateClearBits
import ru.dbotthepony.kommons.collect.iterateSetBits
import ru.dbotthepony.mc.otm.util.collect.IntRange2Set
import ru.dbotthepony.mc.otm.util.collect.map
import ru.dbotthepony.kommons.collect.map
import java.util.*
abstract class BitmapTrackingContainer<out S : IContainerSlot> : IEnhancedContainer<S> {

View File

@ -11,13 +11,13 @@ import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap
import net.minecraft.world.Container
import net.minecraft.world.entity.player.Player
import net.minecraft.world.item.ItemStack
import ru.dbotthepony.kommons.collect.concatIterators
import ru.dbotthepony.kommons.collect.filter
import ru.dbotthepony.kommons.collect.flatMap
import ru.dbotthepony.kommons.collect.map
import ru.dbotthepony.kommons.collect.stream
import ru.dbotthepony.mc.otm.container.util.iterator
import ru.dbotthepony.mc.otm.util.collect.concatIterators
import ru.dbotthepony.mc.otm.util.collect.filter
import ru.dbotthepony.mc.otm.util.collect.flatMap
import ru.dbotthepony.mc.otm.util.collect.map
import ru.dbotthepony.mc.otm.util.isNotEmpty
import ru.dbotthepony.mc.otm.util.stream
import java.util.stream.Stream
class CombinedContainer<S : IContainerSlot>(containers: Stream<Pair<IEnhancedContainer<S>, Iterable<Int>>>) : ISlottedContainer<S> {

View File

@ -16,16 +16,15 @@ import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.enchantment.EnchantmentEffectComponents
import net.minecraft.world.item.enchantment.EnchantmentHelper
import net.neoforged.neoforge.fluids.capability.IFluidHandler
import ru.dbotthepony.kommons.collect.addAll
import ru.dbotthepony.kommons.collect.filter
import ru.dbotthepony.kommons.collect.toList
import ru.dbotthepony.kommons.util.map
import ru.dbotthepony.mc.otm.container.util.ItemStackHashStrategy
import ru.dbotthepony.mc.otm.container.util.containerSlot
import ru.dbotthepony.mc.otm.container.util.slotIterator
import ru.dbotthepony.mc.otm.util.addAll
import ru.dbotthepony.mc.otm.util.collect.IntRange2Set
import ru.dbotthepony.mc.otm.util.collect.filter
import ru.dbotthepony.mc.otm.util.collect.toList
import ru.dbotthepony.mc.otm.util.isNotEmpty
import ru.dbotthepony.mc.otm.util.map
import ru.dbotthepony.mc.otm.util.ItemStackSorter
import kotlin.math.roundToInt

View File

@ -9,9 +9,8 @@ import net.minecraft.world.item.Item
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.Items
import net.minecraft.world.item.crafting.RecipeInput
import ru.dbotthepony.mc.otm.util.collect.any
import ru.dbotthepony.mc.otm.util.collect.filter
import ru.dbotthepony.mc.otm.util.collect.map
import ru.dbotthepony.kommons.collect.filter
import ru.dbotthepony.kommons.collect.map
import ru.dbotthepony.mc.otm.util.isNotEmpty
import java.util.function.Predicate
import java.util.stream.Stream
@ -243,14 +242,14 @@ interface IEnhancedContainer<out S : IContainerSlot> : Container, RecipeInput, I
if (Items.AIR in items && hasEmptySlots)
return true
return iterator().any { it.item in items }
return any { it.item in items }
}
override fun hasAnyMatching(predicate: Predicate<ItemStack>): Boolean {
if (predicate.test(ItemStack.EMPTY) && hasEmptySlots)
return true
return iterator().any(predicate)
return any(predicate::test)
}
fun toList(): MutableList<ItemStack> {

View File

@ -6,8 +6,8 @@ import ru.dbotthepony.mc.otm.capability.MatteryCapability
import ru.dbotthepony.mc.otm.capability.UpgradeType
import ru.dbotthepony.mc.otm.config.EnergyBalanceValues
import ru.dbotthepony.mc.otm.config.VerboseEnergyBalanceValues
import ru.dbotthepony.mc.otm.util.collect.map
import ru.dbotthepony.mc.otm.util.collect.reduce
import ru.dbotthepony.kommons.collect.map
import ru.dbotthepony.kommons.collect.reduce
import ru.dbotthepony.mc.otm.util.math.Decimal
import java.util.function.BooleanSupplier
import kotlin.math.pow

View File

@ -6,7 +6,7 @@ import net.minecraft.world.item.ItemStack
import net.neoforged.neoforge.items.IItemHandler
import ru.dbotthepony.mc.otm.util.collect.AwareItemStack
import ru.dbotthepony.mc.otm.util.collect.ItemHandlerItemStackEntry
import ru.dbotthepony.mc.otm.util.collect.filter
import ru.dbotthepony.kommons.collect.filter
import ru.dbotthepony.mc.otm.util.isNotEmpty
import java.util.*
import java.util.stream.Stream

View File

@ -6,8 +6,8 @@ import net.minecraft.world.item.ItemStack
import ru.dbotthepony.mc.otm.container.IContainerSlot
import ru.dbotthepony.mc.otm.container.IEnhancedContainer
import ru.dbotthepony.mc.otm.container.get
import ru.dbotthepony.mc.otm.util.collect.filter
import ru.dbotthepony.mc.otm.util.collect.map
import ru.dbotthepony.kommons.collect.filter
import ru.dbotthepony.kommons.collect.map
import ru.dbotthepony.mc.otm.util.isNotEmpty
fun Container.containerSlot(slot: Int): IContainerSlot {

View File

@ -5,7 +5,7 @@ import com.mojang.datafixers.util.Pair
import com.mojang.serialization.Codec
import com.mojang.serialization.DataResult
import com.mojang.serialization.DynamicOps
import ru.dbotthepony.mc.otm.util.stream
import ru.dbotthepony.kommons.collect.stream
import java.util.stream.Stream
class CodecList<S : Any>(codecs: Stream<Codec<S>>) : Codec<S> {

View File

@ -5,7 +5,7 @@ import com.mojang.datafixers.util.Pair
import com.mojang.serialization.Codec
import com.mojang.serialization.DataResult
import com.mojang.serialization.DynamicOps
import ru.dbotthepony.mc.otm.util.stream
import ru.dbotthepony.kommons.collect.stream
import java.util.function.Predicate
import java.util.stream.Stream

View File

@ -9,7 +9,7 @@ import net.minecraft.world.level.storage.loot.parameters.LootContextParams
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition
import net.minecraft.world.level.storage.loot.predicates.LootItemConditionType
import ru.dbotthepony.mc.otm.capability.items
import ru.dbotthepony.mc.otm.util.collect.filter
import ru.dbotthepony.kommons.collect.filter
import ru.dbotthepony.mc.otm.data.get
import ru.dbotthepony.mc.otm.registry.data.MLootItemConditions

View File

@ -14,10 +14,10 @@ import net.minecraft.world.level.storage.loot.LootContext
import net.minecraft.world.level.storage.loot.functions.LootItemFunction
import net.minecraft.world.level.storage.loot.functions.LootItemFunctionType
import net.minecraft.world.level.storage.loot.parameters.LootContextParams
import ru.dbotthepony.kommons.collect.stream
import ru.dbotthepony.mc.otm.block.entity.MatteryBlockEntity
import ru.dbotthepony.mc.otm.util.fromNbt
import ru.dbotthepony.mc.otm.util.set
import ru.dbotthepony.mc.otm.util.stream
import ru.dbotthepony.mc.otm.registry.data.MItemFunctionTypes
import java.util.*
import java.util.stream.Stream

View File

@ -29,12 +29,12 @@ import net.neoforged.neoforge.common.SoundActions
import net.neoforged.neoforge.fluids.FluidStack
import net.neoforged.neoforge.fluids.FluidUtil
import net.neoforged.neoforge.fluids.capability.IFluidHandler
import ru.dbotthepony.kommons.collect.any
import ru.dbotthepony.mc.otm.capability.fluid.ItemMatteryFluidHandler
import ru.dbotthepony.mc.otm.capability.fluid.iterator
import ru.dbotthepony.mc.otm.capability.moveFluid
import ru.dbotthepony.mc.otm.container.get
import ru.dbotthepony.mc.otm.util.TranslatableComponent
import ru.dbotthepony.mc.otm.util.collect.any
import ru.dbotthepony.kommons.guava.immutableList
import ru.dbotthepony.kommons.guava.immutableMap
import ru.dbotthepony.mc.otm.util.isNotEmpty

View File

@ -35,7 +35,7 @@ import ru.dbotthepony.mc.otm.capability.matteryEnergy
import ru.dbotthepony.mc.otm.config.EnergyBalanceValues
import ru.dbotthepony.mc.otm.util.ResourceLocation
import ru.dbotthepony.mc.otm.util.TranslatableComponent
import ru.dbotthepony.mc.otm.util.collect.filter
import ru.dbotthepony.kommons.collect.filter
import ru.dbotthepony.mc.otm.util.isNotEmpty
import ru.dbotthepony.mc.otm.util.math.Decimal
import ru.dbotthepony.mc.otm.util.getDecimal

View File

@ -10,6 +10,7 @@ import net.minecraft.world.item.TooltipFlag
import net.minecraft.world.level.storage.loot.functions.LootItemConditionalFunction
import net.minecraft.world.level.storage.loot.functions.SetComponentsFunction
import net.neoforged.neoforge.capabilities.RegisterCapabilitiesEvent
import ru.dbotthepony.kommons.collect.stream
import ru.dbotthepony.mc.otm.capability.MatteryCapability
import ru.dbotthepony.mc.otm.capability.matter.IPatternStorage
import ru.dbotthepony.mc.otm.capability.matter.PatternInsertFailure
@ -18,7 +19,6 @@ import ru.dbotthepony.mc.otm.capability.matter.PatternState
import ru.dbotthepony.mc.otm.capability.matter.gatherTooltip
import ru.dbotthepony.mc.otm.util.TextComponent
import ru.dbotthepony.mc.otm.util.TranslatableComponent
import ru.dbotthepony.mc.otm.util.stream
import ru.dbotthepony.mc.otm.item.MatteryItem
import ru.dbotthepony.mc.otm.registry.CapabilitiesRegisterListener
import ru.dbotthepony.mc.otm.registry.game.MDataComponentTypes

View File

@ -82,9 +82,8 @@ import ru.dbotthepony.mc.otm.container.util.stream
import ru.dbotthepony.mc.otm.util.ResourceLocation
import ru.dbotthepony.mc.otm.util.TextComponent
import ru.dbotthepony.mc.otm.util.TranslatableComponent
import ru.dbotthepony.mc.otm.util.collect.any
import ru.dbotthepony.mc.otm.util.collect.filter
import ru.dbotthepony.kommons.collect.filterNotNull
import ru.dbotthepony.kommons.collect.stream
import ru.dbotthepony.mc.otm.util.getReverseTag
import ru.dbotthepony.mc.otm.util.isNotEmpty
import ru.dbotthepony.mc.otm.util.math.Decimal
@ -92,7 +91,6 @@ import ru.dbotthepony.mc.otm.util.math.isZero
import ru.dbotthepony.mc.otm.util.readComponent
import ru.dbotthepony.mc.otm.util.readItemType
import ru.dbotthepony.mc.otm.util.registryName
import ru.dbotthepony.mc.otm.util.stream
import ru.dbotthepony.mc.otm.util.formatMatter
import ru.dbotthepony.mc.otm.util.formatMatterFull
import ru.dbotthepony.mc.otm.util.formatSiComponent
@ -1633,11 +1631,11 @@ object MatterManager {
.executes { dumpRegistry(it, it.getArgument("mode", DumpFilter::class.java), StringArgumentType.getString(it, "mod")) }
.suggests { context, builder ->
val startingWith = builder.input.substring(builder.start).lowercase()
ModList.get().mods.iterator()
ModList.get().mods.stream()
.filter { it.modId.startsWith(startingWith) }
.filter { key -> BuiltInRegistries.ITEM.entrySet().iterator()
.filter { key -> BuiltInRegistries.ITEM.entrySet().stream()
.filter { it.key.location().namespace == key.namespace }
.any { !Registry.isBlacklisted(it.value) } }
.anyMatch { !Registry.isBlacklisted(it.value) } }
.forEach { builder.suggest(it.modId, TextComponent(it.displayName)) }
builder.buildFuture()
})

View File

@ -9,6 +9,9 @@ import net.minecraft.world.level.material.Fluids
import net.neoforged.neoforge.capabilities.Capabilities
import net.neoforged.neoforge.fluids.FluidStack
import net.neoforged.neoforge.fluids.capability.IFluidHandler
import ru.dbotthepony.kommons.collect.addAll
import ru.dbotthepony.kommons.collect.filter
import ru.dbotthepony.kommons.collect.maybe
import ru.dbotthepony.kommons.util.Listenable
import ru.dbotthepony.kommons.util.ListenableDelegate
import ru.dbotthepony.kommons.util.getValue
@ -16,10 +19,7 @@ import ru.dbotthepony.kommons.util.setValue
import ru.dbotthepony.mc.otm.block.entity.decorative.PainterBlockEntity
import ru.dbotthepony.mc.otm.player.matteryPlayer
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
import ru.dbotthepony.mc.otm.util.addAll
import ru.dbotthepony.mc.otm.util.collect.SupplierMap
import ru.dbotthepony.mc.otm.util.collect.filter
import ru.dbotthepony.mc.otm.util.collect.maybe
import ru.dbotthepony.mc.otm.util.isNotEmpty
import ru.dbotthepony.mc.otm.menu.MatteryMenu
import ru.dbotthepony.mc.otm.menu.MatteryMenuSlot

View File

@ -12,8 +12,8 @@ import ru.dbotthepony.mc.otm.block.entity.storage.IItemMonitorPlayerSettings
import ru.dbotthepony.mc.otm.block.entity.storage.ItemMonitorBlockEntity
import ru.dbotthepony.mc.otm.block.entity.storage.ItemMonitorPlayerSettings
import ru.dbotthepony.mc.otm.container.get
import ru.dbotthepony.mc.otm.util.collect.map
import ru.dbotthepony.mc.otm.util.collect.reduce
import ru.dbotthepony.kommons.collect.map
import ru.dbotthepony.kommons.collect.reduce
import ru.dbotthepony.mc.otm.util.isNotEmpty
import ru.dbotthepony.mc.otm.menu.MatteryPoweredMenu
import ru.dbotthepony.mc.otm.menu.MatteryMenuSlot

View File

@ -16,8 +16,6 @@ import net.minecraft.world.level.block.state.BlockState
import net.minecraft.world.level.chunk.LevelChunk
import net.minecraft.world.phys.AABB
import ru.dbotthepony.kommons.util.Listenable
import ru.dbotthepony.mc.otm.util.collect.collect
import ru.dbotthepony.mc.otm.util.collect.map
import ru.dbotthepony.mc.otm.util.getBlockEntityNow
import ru.dbotthepony.mc.otm.util.getBlockStateNowOrNull
import ru.dbotthepony.mc.otm.util.math.Vector
@ -83,7 +81,7 @@ class ShapedMultiblock(pos: BlockPos, factory: ShapedMultiblockFactory) : IMulti
private val assignedBlockEntityLists = ArrayList<BlockEntitySet<*>>(prototype.blockEntityTags.size)
private val assignedBlockStateLists = ArrayList<Reference2IntMap<BlockState>>()
private val assignedBlockLists = ArrayList<Reference2IntMap<Block>>()
private val children: ImmutableList<Part> by lazy(LazyThreadSafetyMode.NONE) { prototype.children.iterator().map { Part(pos, it, chunkPos) }.collect(ImmutableList.toImmutableList()) }
private val children: ImmutableList<Part> by lazy(LazyThreadSafetyMode.NONE) { prototype.children.stream().map { Part(pos, it, chunkPos) }.collect(ImmutableList.toImmutableList()) }
override fun compareTo(other: Part): Int {
return chunkPos.compareTo(other.chunkPos)

View File

@ -10,8 +10,7 @@ import net.minecraft.core.Vec3i
import net.minecraft.tags.TagKey
import net.minecraft.world.level.block.Block
import net.minecraft.world.level.block.state.BlockState
import ru.dbotthepony.mc.otm.util.collect.collect
import ru.dbotthepony.mc.otm.util.collect.map
import ru.dbotthepony.kommons.collect.map
import ru.dbotthepony.mc.otm.util.math.RelativeSide
import ru.dbotthepony.mc.otm.util.math.plus
import java.util.function.Predicate
@ -359,6 +358,6 @@ class ShapedMultiblockBuilder {
* Created factory does not share reference(s) to this builder, and this builder can be mutated further without consequences.
*/
fun build(): ShapedMultiblockFactory {
return ShapedMultiblockFactory(nodes.values.iterator().map { it.build() }.collect(ImmutableSet.toImmutableSet()), ImmutableList.copyOf(customChecks))
return ShapedMultiblockFactory(nodes.values.stream().map { it.build() }.collect(ImmutableSet.toImmutableSet()), ImmutableList.copyOf(customChecks))
}
}

View File

@ -5,8 +5,7 @@ import com.google.common.collect.ImmutableSet
import net.minecraft.core.BlockPos
import net.minecraft.world.level.block.Rotation
import net.minecraft.world.level.block.state.BlockState
import ru.dbotthepony.mc.otm.util.collect.collect
import ru.dbotthepony.mc.otm.util.collect.map
import ru.dbotthepony.kommons.collect.map
import java.util.function.Predicate
class ShapedMultiblockFactory(val north: ImmutableSet<Part>, val customChecks: ImmutableList<Predicate<IMultiblockAccess>>) {
@ -28,7 +27,7 @@ class ShapedMultiblockFactory(val north: ImmutableSet<Part>, val customChecks: I
return ShapedMultiblock(pos, this)
}
val south: ImmutableSet<Part> = north.iterator().map { it.copy(pos = it.pos.rotate(Rotation.CLOCKWISE_180)) }.collect(ImmutableSet.toImmutableSet())
val west: ImmutableSet<Part> = north.iterator().map { it.copy(pos = it.pos.rotate(Rotation.COUNTERCLOCKWISE_90)) }.collect(ImmutableSet.toImmutableSet())
val east: ImmutableSet<Part> = north.iterator().map { it.copy(pos = it.pos.rotate(Rotation.CLOCKWISE_90)) }.collect(ImmutableSet.toImmutableSet())
val south: ImmutableSet<Part> = north.stream().map { it.copy(pos = it.pos.rotate(Rotation.CLOCKWISE_180)) }.collect(ImmutableSet.toImmutableSet())
val west: ImmutableSet<Part> = north.stream().map { it.copy(pos = it.pos.rotate(Rotation.COUNTERCLOCKWISE_90)) }.collect(ImmutableSet.toImmutableSet())
val east: ImmutableSet<Part> = north.stream().map { it.copy(pos = it.pos.rotate(Rotation.CLOCKWISE_90)) }.collect(ImmutableSet.toImmutableSet())
}

View File

@ -16,8 +16,8 @@ import ru.dbotthepony.mc.otm.block.entity.MatteryBlockEntity
import ru.dbotthepony.mc.otm.client.minecraft
import ru.dbotthepony.mc.otm.client.onceClient
import ru.dbotthepony.mc.otm.util.ResourceLocation
import ru.dbotthepony.mc.otm.util.collect.map
import ru.dbotthepony.mc.otm.util.collect.reduce
import ru.dbotthepony.kommons.collect.map
import ru.dbotthepony.kommons.collect.reduce
import java.util.*
import kotlin.collections.ArrayList

View File

@ -18,8 +18,8 @@ import ru.dbotthepony.kommons.util.Listenable
import ru.dbotthepony.kommons.util.ListenableDelegate
import ru.dbotthepony.kommons.util.Observer
import ru.dbotthepony.mc.otm.OTM_CLEANER
import ru.dbotthepony.mc.otm.util.collect.filterNotNull
import ru.dbotthepony.mc.otm.util.collect.map
import ru.dbotthepony.kommons.collect.filterNotNull
import ru.dbotthepony.kommons.collect.map
import ru.dbotthepony.mc.otm.util.math.Decimal
import ru.dbotthepony.mc.otm.network.ItemStackCodec
import ru.dbotthepony.mc.otm.network.MatteryStreamCodec

View File

@ -83,7 +83,7 @@ import ru.dbotthepony.mc.otm.container.ItemFilter
import ru.dbotthepony.mc.otm.container.util.slotIterator
import ru.dbotthepony.mc.otm.container.vanishCursedItems
import ru.dbotthepony.mc.otm.util.collect.UUIDIntModifiersMap
import ru.dbotthepony.mc.otm.util.collect.filter
import ru.dbotthepony.kommons.collect.filter
import ru.dbotthepony.mc.otm.util.math.Decimal
import ru.dbotthepony.mc.otm.util.math.RGBColorDFUCodec
import ru.dbotthepony.mc.otm.util.math.minus

View File

@ -19,6 +19,7 @@ import net.neoforged.neoforge.event.OnDatapackSyncEvent
import net.neoforged.neoforge.network.PacketDistributor
import net.neoforged.neoforge.network.handling.IPayloadContext
import org.apache.logging.log4j.LogManager
import ru.dbotthepony.kommons.gson.set
import ru.dbotthepony.mc.otm.MINECRAFT_SERVER
import ru.dbotthepony.mc.otm.NULLABLE_MINECRAFT_SERVER
import ru.dbotthepony.mc.otm.OverdriveThatMatters

View File

@ -18,8 +18,8 @@ import net.minecraft.world.item.crafting.ShapedRecipePattern
import net.minecraft.world.level.Level
import ru.dbotthepony.mc.otm.capability.MatteryCapability
import ru.dbotthepony.mc.otm.capability.matteryEnergy
import ru.dbotthepony.mc.otm.util.collect.filterNotNull
import ru.dbotthepony.mc.otm.util.collect.map
import ru.dbotthepony.kommons.collect.filterNotNull
import ru.dbotthepony.kommons.collect.map
import ru.dbotthepony.mc.otm.util.math.Decimal
import ru.dbotthepony.mc.otm.data.codec.DecimalCodec
import ru.dbotthepony.mc.otm.data.codec.minRange

View File

@ -13,8 +13,7 @@ import net.minecraft.advancements.critereon.EntityPredicate
import net.minecraft.resources.ResourceLocation
import net.minecraft.server.PlayerAdvancements
import net.minecraft.server.level.ServerPlayer
import ru.dbotthepony.mc.otm.util.collect.filter
import ru.dbotthepony.mc.otm.util.collect.toImmutableList
import ru.dbotthepony.kommons.collect.filter
import java.util.*
import java.util.function.Predicate
@ -46,9 +45,9 @@ abstract class MCriterionTrigger<T : MCriterionTrigger<T>.AbstractInstance>(val
val context = EntityPredicate.createContext(player, player)
listeners.iterator()
listeners.stream()
.filter { predicate.test(it.trigger) && it.trigger.playerPredicate.map { it.matches(context) }.orElse(true) }
.toImmutableList()
.toList()
.forEach { it.run(advancements) }
}

View File

@ -321,3 +321,9 @@ fun RegistryFriendlyByteBuf.readComponent(): Component {
fun InputStream.readItemType(): Item {
return BuiltInRegistries.ITEM.byId(readVarIntLE())
}
fun FriendlyByteBuf.writeBigInteger(value: BigInteger) {
writeByteArray(value.toByteArray())
}
fun FriendlyByteBuf.readBigInteger(byteLimit: Int = 128) = BigInteger(readByteArray(byteLimit))

View File

@ -6,6 +6,7 @@ import net.minecraft.util.RandomSource
import net.minecraft.world.level.ChunkPos
import net.minecraft.world.level.Level
import net.minecraft.world.level.LevelAccessor
import net.minecraft.world.level.block.Block
import net.minecraft.world.level.block.Blocks
import net.minecraft.world.level.block.entity.BlockEntity
import net.minecraft.world.level.block.state.BlockState
@ -61,3 +62,12 @@ interface IMatteryLevel {
* because RNG is not sampled only once per tick, obviously)
*/
val Level.otmRandom: RandomSource get() = (this as IMatteryLevel).otmRandom ?: random
fun Level.gracefulBlockBreak(blockPos: BlockPos, block: BlockState = getBlockState(blockPos)) {
val tile = getBlockEntity(blockPos)
val state = block.fluidState.createLegacyBlock()
block.onRemove(this, blockPos, state, false)
Block.dropResources(block, this, blockPos, tile)
block.block.destroy(this, blockPos, block)
setBlock(blockPos, state, Block.UPDATE_ALL)
}

View File

@ -63,34 +63,6 @@ operator fun CraftingInput.Positioned.get(x: Int, y: Int): ItemStack {
return input.getItem(actualX, actualY)
}
fun FriendlyByteBuf.writeBigInteger(value: BigInteger) {
writeByteArray(value.toByteArray())
}
fun FriendlyByteBuf.readBigInteger(byteLimit: Int = 128) = BigInteger(readByteArray(byteLimit))
fun <V> Map<*, V>.asLambdaSupplierArray(): Array<() -> V> {
val result = arrayOfNulls<() -> V>(size)
var i = 0
for (k in keys) {
result[i++] = { this[k] ?: throw ConcurrentModificationException("Key $k is no longer present in map $this") }
}
return result as Array<() -> V>
}
fun <V> Map<*, V>.asSupplierArray(): Array<Supplier<V>> {
val result = arrayOfNulls<Supplier<V>>(size)
var i = 0
for (k in keys) {
result[i++] = { this[k] ?: throw ConcurrentModificationException("Key $k is no longer present in map $this") }
}
return result as Array<Supplier<V>>
}
fun <V> Future<V>.asSupplier(): Supplier<V> {
return Supplier { get() }
}
@ -101,11 +73,6 @@ fun <V> runInBackground(block: Callable<V>): Future<V> {
operator fun IItemHandler.get(index: Int): ItemStack = getStackInSlot(index)
operator fun JsonObject.set(s: String, value: JsonElement) = add(s, value)
operator fun JsonObject.set(s: String, value: String) = add(s, JsonPrimitive(value))
operator fun JsonObject.set(s: String, value: Number) = add(s, JsonPrimitive(value))
operator fun JsonObject.set(s: String, value: Boolean) = add(s, JsonPrimitive(value))
inline val FluidStack.isNotEmpty get() = !isEmpty
inline val ItemStack.isNotEmpty get() = !isEmpty
@ -149,35 +116,6 @@ fun BlockState.getExplosionResistance(level: BlockGetter, pos: BlockPos): Float
}
}
fun Level.gracefulBlockBreak(blockPos: BlockPos, block: BlockState = getBlockState(blockPos)) {
val tile = getBlockEntity(blockPos)
val state = block.fluidState.createLegacyBlock()
block.onRemove(this, blockPos, state, false)
Block.dropResources(block, this, blockPos, tile)
block.block.destroy(this, blockPos, block)
setBlock(blockPos, state, Block.UPDATE_ALL)
}
fun <E> MutableCollection<E>.addAll(elements: Iterator<E>) {
for (item in elements) {
add(item)
}
}
fun <E> MutableCollection<E>.addAll(elements: Stream<out E>) {
for (item in elements) {
add(item)
}
}
fun <E> Iterable<E>.stream(): Stream<out E> {
return StreamSupport.stream(this.spliterator(), false)
}
fun <E> Iterator<E>.stream(): Stream<out E> {
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(this, 0), false)
}
fun Entity.genericPositions(): Collection<Vector> {
return listOf(
position,
@ -194,38 +132,6 @@ fun String.toUUID(): UUID {
return UUID.nameUUIDFromBytes(toByteArray(Charsets.UTF_8))
}
fun <T> Array<T>.stream(): Stream<T> = Arrays.stream(this)
inline fun <T> MutableList<out Reference<out T>>.forValidRefs(fn: (T) -> Unit) {
val iterator = listIterator()
for (value in iterator) {
val get = value.get()
if (get == null) {
iterator.remove()
} else {
fn.invoke(get)
}
}
}
inline fun <T> MutableList<out Reference<out T>>.forValidRefsBreak(fn: (T) -> Boolean) {
val iterator = listIterator()
for (value in iterator) {
val get = value.get()
if (get == null) {
iterator.remove()
} else {
if (fn.invoke(get)) {
break
}
}
}
}
val ComponentContents.key: String
get() = (this as? TranslatableContents ?: throw ClassCastException("$this is not a TranslatableContents")).key

View File

@ -3,9 +3,9 @@ package ru.dbotthepony.mc.otm.util.chart
import it.unimi.dsi.fastutil.objects.ObjectIterators
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet
import net.minecraft.network.RegistryFriendlyByteBuf
import ru.dbotthepony.mc.otm.util.collect.filter
import ru.dbotthepony.mc.otm.util.collect.map
import ru.dbotthepony.mc.otm.util.collect.toList
import ru.dbotthepony.kommons.collect.filter
import ru.dbotthepony.kommons.collect.map
import ru.dbotthepony.kommons.collect.toList
import ru.dbotthepony.mc.otm.util.ITickable
import ru.dbotthepony.mc.otm.network.syncher.DynamicSynchableGroup
import ru.dbotthepony.mc.otm.network.syncher.IRemoteState

View File

@ -1,7 +1,7 @@
package ru.dbotthepony.mc.otm.util.chart
import net.minecraft.network.RegistryFriendlyByteBuf
import ru.dbotthepony.mc.otm.util.collect.reduce
import ru.dbotthepony.kommons.collect.reduce
import ru.dbotthepony.mc.otm.util.math.Decimal
import java.util.function.Supplier

View File

@ -2,7 +2,7 @@ package ru.dbotthepony.mc.otm.util.chart
import com.mojang.serialization.Codec
import net.minecraft.network.RegistryFriendlyByteBuf
import ru.dbotthepony.mc.otm.util.collect.reduce
import ru.dbotthepony.kommons.collect.reduce
import ru.dbotthepony.mc.otm.util.math.Decimal
import ru.dbotthepony.mc.otm.data.codec.DecimalCodec
import ru.dbotthepony.mc.otm.network.MatteryStreamCodec

View File

@ -1,5 +1,7 @@
package ru.dbotthepony.mc.otm.util.collect
import ru.dbotthepony.kommons.collect.filter
import ru.dbotthepony.kommons.collect.map
import java.util.EnumMap
import java.util.function.BooleanSupplier
import java.util.stream.Stream

View File

@ -1,6 +1,8 @@
package ru.dbotthepony.mc.otm.util.collect
import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap
import ru.dbotthepony.kommons.collect.filter
import ru.dbotthepony.kommons.collect.map
import java.util.function.BooleanSupplier
import java.util.stream.Stream

View File

@ -7,6 +7,7 @@ import it.unimi.dsi.fastutil.ints.IntComparator
import it.unimi.dsi.fastutil.ints.IntIterators
import it.unimi.dsi.fastutil.ints.IntSet
import it.unimi.dsi.fastutil.ints.IntSortedSet
import ru.dbotthepony.kommons.collect.all
class IntRange2Set private constructor(private val first: Int, private val last: Int) : IntSortedSet {
constructor(range: IntRange) : this(range.first, range.last) {

View File

@ -1,408 +0,0 @@
package ru.dbotthepony.mc.otm.util.collect
import it.unimi.dsi.fastutil.objects.ObjectIterators
import ru.dbotthepony.mc.otm.util.addAll
import java.util.Optional
import java.util.Spliterators
import java.util.function.Predicate
import java.util.stream.Collector
import java.util.stream.Stream
import java.util.stream.StreamSupport
// Purpose of Stream API over Iterators is that it is simple enough for JIT to inline most of it,
// unlike actual Streams.
// We lose only one (actual) element of Stream API here: ability to parallelize work,
// except it doesn't (properly) work in Minecraft (Forge) either, due to classloader bug:
// https://github.com/MinecraftForge/EventBus/issues/44
// Aside parallel work, unimplemented Stream API elements can be easily implemented when required
private class FilteringIterator<T>(private val parent: Iterator<T>, private val predicate: Predicate<in T>, private var value: T) : MutableIterator<T> {
private var hasValue = true
private var returned = false
override fun hasNext(): Boolean {
return hasValue
}
override fun next(): T {
if (!hasValue) throw NoSuchElementException()
hasValue = false
returned = true
val value = this.value
while (parent.hasNext()) {
val next = parent.next()
if (predicate.test(next)) {
hasValue = true
this.value = next
break
}
}
return value
}
override fun remove() {
if (!returned) throw NoSuchElementException()
returned = false
(parent as MutableIterator<T>).remove()
}
}
private class MappingIterator<T, R>(private val parent: Iterator<T>, private val mapper: (T) -> R) : MutableIterator<R> {
override fun hasNext(): Boolean {
return parent.hasNext()
}
override fun next(): R {
return mapper.invoke(parent.next())
}
override fun remove() {
(parent as MutableIterator<T>).remove()
}
}
private class FlatMappingIterator<T, R>(private val parent: Iterator<T>, private val mapper: (T) -> Iterator<R>) : MutableIterator<R> {
private var current: Iterator<R> = mapper.invoke(parent.next())
private var last: Iterator<R>? = null
init {
while (!current.hasNext() && parent.hasNext()) {
current = mapper.invoke(parent.next())
}
}
override fun hasNext(): Boolean {
return current.hasNext()
}
override fun next(): R {
if (!current.hasNext())
throw NoSuchElementException()
val v = current.next()
last = current
while (!current.hasNext() && parent.hasNext()) {
current = mapper.invoke(parent.next())
}
return v
}
override fun remove() {
(last as MutableIterator<R>? ?: throw NoSuchElementException()).remove()
last = null
}
}
private class LimitingIterator<T>(private val parent: Iterator<T>, private val limit: Long) : Iterator<T> {
init {
require(limit > 0) { "Invalid limit $limit" }
}
private var found = 0L
override fun hasNext(): Boolean {
return found < limit && parent.hasNext()
}
override fun next(): T {
if (found >= limit)
throw NoSuchElementException()
return parent.next()
}
}
private class SkippingIterator<T>(private val parent: Iterator<T>, skip: Long) : MutableIterator<T> {
init {
require(skip >= 0) { "Invalid skip amount $skip" }
}
private var found = skip
private var returned = false
override fun hasNext(): Boolean {
while (parent.hasNext() && found > 0L) {
found--
parent.next()
}
return parent.hasNext()
}
override fun next(): T {
if (!hasNext())
throw NoSuchElementException()
val v = parent.next()
returned = true
return v
}
override fun remove() {
if (!returned) {
throw NoSuchElementException()
}
returned = false
(parent as MutableIterator<T>).remove()
}
}
fun <T> concatIterators(): MutableIterator<T> {
return ObjectIterators.EMPTY_ITERATOR as MutableIterator<T>
}
fun <T> concatIterators(a: Iterator<T>): MutableIterator<T> {
return a as MutableIterator<T>
}
fun <T> concatIterators(iterators: Iterable<Iterator<T>>): MutableIterator<T> {
return iterators.iterator().flatMap { it }
}
fun <T> concatIterators(vararg iterators: Iterator<T>): MutableIterator<T> {
return iterators.iterator().flatMap { it }
}
/**
* Filters elements of [this] iterator
*
* Resulting [Iterator] is [MutableIterator] if [this] is
*/
fun <T> Iterator<T>.filter(condition: Predicate<in T>): MutableIterator<T> {
while (hasNext()) {
val v = next()
if (condition.test(v)) {
return FilteringIterator(this, condition, v)
}
}
return emptyIterator()
}
/**
* Maps elements of [this] iterator from values of [T] to [R] using function [mapper]
*
* Resulting [Iterator] is [MutableIterator] if [this] is
*/
fun <T, R> Iterator<T>.map(mapper: (T) -> R): MutableIterator<R> {
if (!hasNext()) {
return emptyIterator()
}
return MappingIterator(this, mapper)
}
/**
* Maps elements of [this] iterator from type [T] to other iterators of type [R] using function [mapper]
*
* Resulting [Iterator] is [MutableIterator] if [this] is
*/
fun <T, R> Iterator<T>.flatMap(mapper: (T) -> Iterator<R>): MutableIterator<R> {
if (!hasNext()) {
return emptyIterator()
}
return FlatMappingIterator(this, mapper)
}
inline fun <T> Iterator<T>.reduce(identity: T, reducer: (T, T) -> T): T {
var result = identity
while (hasNext()) result = reducer.invoke(result, next())
return result
}
fun <T> Iterator<T?>.filterNotNull(): MutableIterator<T> = filter { it != null } as MutableIterator<T>
inline fun <reified T> Iterator<*>.filterIsInstance(): MutableIterator<T> = filter { it is T } as MutableIterator<T>
fun <T> Iterator<T>.any(predicate: Predicate<in T>): Boolean {
while (hasNext())
if (predicate.test(next()))
return true
return false
}
inline fun <T> Iterator<T>.any(predicate: (T) -> Boolean): Boolean {
while (hasNext())
if (predicate.invoke(next()))
return true
return false
}
fun <T> Iterator<T>.all(predicate: Predicate<in T>): Boolean {
while (hasNext())
if (!predicate.test(next()))
return false
return true
}
inline fun <T> Iterator<T>.all(predicate: (T) -> Boolean): Boolean {
while (hasNext())
if (!predicate.invoke(next()))
return false
return true
}
fun <T> Iterator<T>.none(predicate: Predicate<in T>): Boolean {
while (hasNext())
if (predicate.test(next()))
return false
return true
}
inline fun <T> Iterator<T>.none(predicate: (T) -> Boolean): Boolean {
while (hasNext())
if (predicate.invoke(next()))
return false
return true
}
fun <T, A, R> Iterator<T>.collect(collector: Collector<T, A, R>): R {
val accumulator = collector.accumulator()
val instance = collector.supplier().get()
for (value in this) {
accumulator.accept(instance, value)
}
return collector.finisher().apply(instance)
}
fun <T> Iterator<T>.toList(expectedSize: Int = 16): MutableList<T> {
val result = ArrayList<T>(expectedSize)
result.addAll(this)
return result
}
fun <T> Iterator<T>.toImmutableList(expectedSize: Int = 16): List<T> {
if (!hasNext())
return emptyList()
return toList(expectedSize)
}
fun <T : Any> Iterator<T>.find(): Optional<T> {
if (hasNext()) {
return Optional.of(next())
}
return Optional.empty<T>()
}
fun <T> Iterator<T>.limit(limit: Long): Iterator<T> = LimitingIterator(this, limit)
fun <T> Iterator<T>.skip(skip: Long): Iterator<T> = if (skip == 0L) this else SkippingIterator(this, skip)
inline fun <T> Iterator<T>.forEach(action: (T) -> Unit) {
for (value in this) {
action.invoke(value)
}
}
fun <T : Any> Iterator<T>.min(comparator: Comparator<T>): Optional<T> {
if (!hasNext()) {
return Optional.empty<T>()
}
var min = next()
for (value in this) {
if (comparator.compare(min, value) > 0) {
min = value
}
}
return Optional.of(min)
}
fun <T : Any> Iterator<T>.max(comparator: Comparator<T>): Optional<T> {
if (!hasNext()) {
return Optional.empty<T>()
}
var max = next()
for (value in this) {
if (comparator.compare(max, value) < 0) {
max = value
}
}
return Optional.of(max)
}
fun <T> Iterator<T>.peek(peeker: (T) -> Unit): MutableIterator<T> {
return object : MutableIterator<T> {
override fun hasNext(): Boolean {
return this@peek.hasNext()
}
override fun next(): T {
return this@peek.next().also(peeker)
}
override fun remove() {
(this@peek as MutableIterator<T>).remove()
}
}
}
fun <T> Iterator<T>.maybe(): T? {
return if (hasNext())
next()
else
null
}
fun <T> emptyIterator(): MutableIterator<T> {
return ObjectIterators.emptyIterator()
}
fun <T> iteratorOf(value: T): MutableIterator<T> {
return ObjectIterators.singleton(value)
}
fun <T> iteratorOf(vararg value: T): MutableIterator<T> {
return ObjectIterators.wrap(value)
}
fun <T> Iterator<T>.toStream(): Stream<T> {
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(this, 0), false)
}
fun <T> Iterator<T>.allEqual(): Boolean {
if (hasNext()) {
val v = next()
while (hasNext()) {
if (v != next()) {
return false
}
}
return true
}
return false
}
fun <T> Iterator<T>.count(): Long {
var count = 0L
while (hasNext()) count++
return count
}

View File

@ -1,6 +1,6 @@
package ru.dbotthepony.mc.otm.util.collect
import ru.dbotthepony.mc.otm.util.stream
import ru.dbotthepony.kommons.collect.stream
import java.util.concurrent.Future
import java.util.function.Supplier
import java.util.stream.Stream

View File

@ -2,7 +2,7 @@ package ru.dbotthepony.mc.otm.util.collect
import com.google.common.collect.ImmutableSet
import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap
import ru.dbotthepony.mc.otm.util.stream
import ru.dbotthepony.kommons.collect.stream
import java.util.function.Supplier
import java.util.stream.Stream

View File

@ -3,6 +3,8 @@ package ru.dbotthepony.mc.otm.util.collect
import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenCustomHashSet
import it.unimi.dsi.fastutil.objects.ObjectOpenCustomHashSet
import it.unimi.dsi.fastutil.objects.ObjectSet
import ru.dbotthepony.kommons.collect.filterNotNull
import ru.dbotthepony.kommons.collect.map
import ru.dbotthepony.mc.otm.util.HashedWeakReference
import java.lang.ref.ReferenceQueue

View File

@ -13,9 +13,9 @@ import org.joml.Matrix3f
import org.joml.Matrix4f
import org.joml.Quaternionf
import org.joml.Vector3f
import ru.dbotthepony.mc.otm.util.collect.filter
import ru.dbotthepony.mc.otm.util.collect.flatMap
import ru.dbotthepony.mc.otm.util.collect.map
import ru.dbotthepony.kommons.collect.filter
import ru.dbotthepony.kommons.collect.flatMap
import ru.dbotthepony.kommons.collect.map
import java.lang.ref.SoftReference
import java.util.stream.Stream
import java.util.stream.StreamSupport

View File

@ -3,7 +3,7 @@ package ru.dbotthepony.mc.otm.tests
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import ru.dbotthepony.mc.otm.util.collect.WeakHashSet
import ru.dbotthepony.mc.otm.util.collect.forEach
import ru.dbotthepony.kommons.collect.forEach
object WeakHashSetTests {
@Test