Compare commits

..

No commits in common. "0efc5207826d19df901e45bc444acfe87ab94653" and "70c5382e9d7a62580690bdc246e33d36c0eb0375" have entirely different histories.

6 changed files with 33 additions and 131 deletions

View File

@ -22,7 +22,7 @@ mixin_version=0.8.5
neogradle.subsystems.parchment.minecraftVersion=1.21.1
neogradle.subsystems.parchment.mappingsVersion=2024.11.17
kommons_version=3.6.0
kommons_version=3.5.2
caffeine_cache_version=3.1.5
jei_version=19.16.4.171

View File

@ -1,61 +0,0 @@
package ru.dbotthepony.mc.otm.container
import it.unimi.dsi.fastutil.ints.IntCollection
import net.minecraft.world.item.ItemStack
import ru.dbotthepony.kommons.collect.iterateClearBits
import ru.dbotthepony.kommons.collect.iterateSetBits
import ru.dbotthepony.mc.otm.core.collect.IntRange2Set
import ru.dbotthepony.mc.otm.core.collect.map
import java.util.*
abstract class BitmapTrackingContainer<out S : IContainerSlot> : IEnhancedContainer<S> {
protected val bitmap = BitSet()
final override fun isEmpty(): Boolean {
return bitmap.isEmpty
}
final override fun nextEmptySlot(startIndex: Int): Int {
if (startIndex >= containerSize)
return -1
else if (startIndex < 0)
return bitmap.nextClearBit(0)
else
return bitmap.nextClearBit(startIndex)
}
final override fun nextNonEmptySlot(startIndex: Int): Int {
if (startIndex >= containerSize)
return -1
else if (startIndex < 0)
return bitmap.nextSetBit(0)
else
return bitmap.nextSetBit(startIndex)
}
final override fun iterator(): Iterator<ItemStack> {
return bitmap.iterateSetBits(containerSize).map { this[it] }
}
final override fun nonEmptySlotIndexIterator(): IntIterator {
return bitmap.iterateSetBits(containerSize)
}
final override fun emptySlotIndexIterator(): IntIterator {
return bitmap.iterateClearBits(containerSize)
}
final override fun emptySlotIndexIterator(allowedSlots: IntCollection): IntIterator {
if (allowedSlots is IntRange2Set && allowedSlots.isNotEmpty())
return bitmap.iterateClearBits(allowedSlots.firstInt(), allowedSlots.lastInt() + 1)
return super.emptySlotIndexIterator(allowedSlots)
}
final override fun nonEmptySlotIndexIterator(allowedSlots: IntCollection): IntIterator {
if (allowedSlots is IntRange2Set && allowedSlots.isNotEmpty())
return bitmap.iterateSetBits(allowedSlots.firstInt(), allowedSlots.lastInt() + 1)
return super.nonEmptySlotIndexIterator(allowedSlots)
}
}

View File

@ -1,6 +1,5 @@
package ru.dbotthepony.mc.otm.container
import it.unimi.dsi.fastutil.ints.IntCollection
import it.unimi.dsi.fastutil.ints.IntOpenHashSet
import net.minecraft.core.HolderLookup.Provider
import net.minecraft.nbt.CompoundTag
@ -13,14 +12,9 @@ import net.minecraft.world.entity.player.Player
import net.minecraft.world.item.ItemStack
import net.neoforged.neoforge.common.util.INBTSerializable
import org.apache.logging.log4j.LogManager
import ru.dbotthepony.kommons.collect.iterateClearBits
import ru.dbotthepony.kommons.collect.iterateSetBits
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
import ru.dbotthepony.mc.otm.core.collect.IntRange2Set
import ru.dbotthepony.mc.otm.core.collect.map
import ru.dbotthepony.mc.otm.core.isNotEmpty
import ru.dbotthepony.mc.otm.core.nbt.set
import java.util.BitSet
/**
* Flexible base implementation of [IEnhancedContainer], designed to be inherited, or used as-is
@ -29,7 +23,7 @@ import java.util.BitSet
* This is supposed to be counterpart to [SimpleContainer] of Minecraft itself, with more features
* and improved performance (inside [IEnhancedContainer] defined methods).
*/
abstract class EnhancedContainer<out S : IContainerSlot>(private val size: Int) : BitmapTrackingContainer<S>(), INBTSerializable<CompoundTag> {
abstract class EnhancedContainer<out S : IContainerSlot>(private val size: Int) : IEnhancedContainer<S>, INBTSerializable<CompoundTag> {
private val items = Array(size) { ItemStack.EMPTY }
private val observedItems = Array(size) { ItemStack.EMPTY }
@ -42,11 +36,9 @@ abstract class EnhancedContainer<out S : IContainerSlot>(private val size: Int)
if (items[slot].isEmpty) {
items[slot] = ItemStack.EMPTY
observedItems[slot] = ItemStack.EMPTY
bitmap[slot] = false
} else {
notifySlotChanged(slot, observedItems[slot])
observedItems[slot] = items[slot].copy()
bitmap[slot] = true
}
}
}
@ -58,7 +50,6 @@ abstract class EnhancedContainer<out S : IContainerSlot>(private val size: Int)
override fun clearContent() {
items.fill(ItemStack.EMPTY)
observedItems.fill(ItemStack.EMPTY)
bitmap.clear()
}
override fun setChanged() {
@ -155,7 +146,6 @@ abstract class EnhancedContainer<out S : IContainerSlot>(private val size: Int)
val copy = observedItems.copyOf()
items.fill(ItemStack.EMPTY)
observedItems.fill(ItemStack.EMPTY)
bitmap.clear()
val seenSlots = IntOpenHashSet()
val ops = provider.createSerializationContext(NbtOps.INSTANCE)
@ -176,7 +166,6 @@ abstract class EnhancedContainer<out S : IContainerSlot>(private val size: Int)
if (it.isNotEmpty) {
items[slot] = it
observedItems[slot] = it.copy()
bitmap[slot] = true
if (it.count != copy[slot].count || !ItemStack.isSameItemSameComponents(it, copy[slot]))
notifySlotChanged(slot, copy[slot])

View File

@ -76,22 +76,10 @@ interface IEnhancedContainer<out S : IContainerSlot> : Container, RecipeInput, I
}
fun nonEmptySlotIterator(): Iterator<S> {
return nonEmptySlotIndexIterator().map { containerSlot(it) }
return slotIterator().filter { it.isNotEmpty }
}
fun emptySlotIterator(): Iterator<S> {
return emptySlotIndexIterator().map { containerSlot(it) }
}
fun nonEmptySlotIterator(allowedSlots: IntCollection): Iterator<S> {
return nonEmptySlotIndexIterator(allowedSlots).map { containerSlot(it) }
}
fun emptySlotIterator(allowedSlots: IntCollection): Iterator<S> {
return emptySlotIndexIterator(allowedSlots).map { containerSlot(it) }
}
private fun slotIndexWalker(allowedSlots: IntCollection, predicate: Predicate<ItemStack>): IntIterator {
private fun slotIterator(allowedSlots: IntCollection, predicate: Predicate<ItemStack>): IntIterator {
return object : IntIterator() {
private val parent = allowedSlots.intIterator()
private var foundNext = false
@ -132,14 +120,6 @@ interface IEnhancedContainer<out S : IContainerSlot> : Container, RecipeInput, I
}
}
fun emptySlotIndexIterator(allowedSlots: IntCollection): IntIterator {
return slotIndexWalker(allowedSlots) { it.isEmpty }
}
fun nonEmptySlotIndexIterator(allowedSlots: IntCollection): IntIterator {
return slotIndexWalker(allowedSlots) { it.isNotEmpty }
}
fun emptySlotIndexIterator(): IntIterator {
return emptySlotIndexIterator(slotRange)
}
@ -148,30 +128,20 @@ interface IEnhancedContainer<out S : IContainerSlot> : Container, RecipeInput, I
return nonEmptySlotIndexIterator(slotRange)
}
fun slotIndexWithItemIterator(item: Item): IntIterator {
return slotIndexWithItemIterator(item, slotRange)
fun emptySlotIndexIterator(allowedSlots: IntCollection): IntIterator {
return slotIterator(allowedSlots) { it.isEmpty }
}
fun slotIndexWithItemIterator(item: Item, allowedSlots: IntCollection): IntIterator {
val parent = nonEmptySlotIndexIterator(allowedSlots).filter { this[it].isNotEmpty && this[it].item === item }
return object : IntIterator() {
override fun nextInt(): Int {
return parent.next()
}
override fun hasNext(): Boolean {
return parent.hasNext()
}
}
fun nonEmptySlotIndexIterator(allowedSlots: IntCollection): IntIterator {
return slotIterator(allowedSlots) { it.isNotEmpty }
}
fun slotWithItemIterator(item: Item): Iterator<S> {
fun slotWithItemIterator(item: Item): IntIterator {
return slotWithItemIterator(item, slotRange)
}
fun slotWithItemIterator(item: Item, allowedSlots: IntCollection): Iterator<S> {
return slotIndexWithItemIterator(item, allowedSlots).map { containerSlot(it) }
fun slotWithItemIterator(item: Item, allowedSlots: IntCollection): IntIterator {
return slotIterator(allowedSlots) { it.isNotEmpty && it.item === item }
}
fun nextEmptySlot(startIndex: Int): Int {
@ -219,11 +189,17 @@ interface IEnhancedContainer<out S : IContainerSlot> : Container, RecipeInput, I
}
override fun isEmpty(): Boolean {
return nextNonEmptySlot(0) != -1
return super.isEmpty()
}
val hasEmptySlots: Boolean get() {
return nextEmptySlot(0) != -1
for (i in 0 until containerSize) {
if (this[i].isEmpty) {
return true
}
}
return false
}
override fun size(): Int {
@ -233,8 +209,11 @@ interface IEnhancedContainer<out S : IContainerSlot> : Container, RecipeInput, I
override fun countItem(item: Item): Int {
var count = 0
for (slot in slotWithItemIterator(item))
count += slot.item.count
for (stack in this) {
if (stack.item === item) {
count += stack.count
}
}
return count
}
@ -278,7 +257,9 @@ interface IEnhancedContainer<out S : IContainerSlot> : Container, RecipeInput, I
return stack
// двигаем в одинаковые слоты
for (slot in slotWithItemIterator(stack.item, slots)) {
for (i in slotWithItemIterator(stack.item, slots)) {
val slot = containerSlot(i)
val condition: Boolean
if (slot is IFilteredContainerSlot) {
@ -314,7 +295,9 @@ interface IEnhancedContainer<out S : IContainerSlot> : Container, RecipeInput, I
}
if (!onlyIntoExisting) {
for (slot in emptySlotIterator(slots)) {
for (i in emptySlotIndexIterator(slots)) {
val slot = containerSlot(i)
val condition: Boolean
if (slot is IFilteredContainerSlot) {

View File

@ -56,12 +56,12 @@ open class ContainerSlot(
notifyChanged(observedItem)
observedItem = ItemStack.EMPTY
_item = ItemStack.EMPTY
container.notifyChanged(slot)
container.notifyChanged()
return true
} else if (observedItem.count != item.count || !ItemStack.isSameItemSameComponents(item, observedItem)) {
notifyChanged(observedItem)
observedItem = item.copy()
container.notifyChanged(slot)
container.notifyChanged()
return true
}

View File

@ -17,7 +17,6 @@ import net.minecraft.world.item.ItemStack
import net.neoforged.neoforge.common.util.INBTSerializable
import org.apache.logging.log4j.LogManager
import ru.dbotthepony.kommons.util.Either
import ru.dbotthepony.mc.otm.container.BitmapTrackingContainer
import ru.dbotthepony.mc.otm.container.EnhancedContainer
import ru.dbotthepony.mc.otm.container.IAutomatedContainer
import ru.dbotthepony.mc.otm.container.IAutomatedContainerSlot
@ -26,7 +25,6 @@ import ru.dbotthepony.mc.otm.container.balance
import ru.dbotthepony.mc.otm.core.isNotEmpty
import ru.dbotthepony.mc.otm.core.nbt.set
import ru.dbotthepony.mc.otm.data.codec.minRange
import java.util.BitSet
import java.util.function.Predicate
import kotlin.reflect.KClass
@ -42,7 +40,7 @@ class SlottedContainer(
slots: Collection<MarkedSlotProvider<*>>,
private val stillValid: Predicate<Player>,
private val globalChangeListeners: Array<Runnable>
) : BitmapTrackingContainer<ContainerSlot>(), IAutomatedContainer<ContainerSlot>, INBTSerializable<Tag> {
) : IAutomatedContainer<ContainerSlot>, INBTSerializable<Tag> {
interface ISlotGroup<T : ContainerSlot> : List<T> {
/**
* @see IAutomatedContainer.addItem
@ -132,7 +130,6 @@ class SlottedContainer(
private var suppressListeners = false
override fun clearContent() {
bitmap.clear()
suppressListeners = true
try {
@ -152,11 +149,6 @@ class SlottedContainer(
globalChangeListeners.forEach { it.run() }
}
fun notifyChanged(slot: Int) {
notifyChanged()
bitmap[slot] = slots[slot].isNotEmpty
}
// called by outside code (vanilla and other unaware mods)
override fun setChanged() {
suppressListeners = true
@ -261,7 +253,6 @@ class SlottedContainer(
override fun deserializeNBT(provider: HolderLookup.Provider, nbt: Tag) {
lostItems.clear()
slots.forEach { it.clear() }
bitmap.clear()
if (nbt is CompoundTag) {
// legacy container