diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/Ext.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/Ext.kt index df0bc7f95..fdb181a61 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/Ext.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/Ext.kt @@ -15,9 +15,9 @@ import ru.dbotthepony.mc.otm.compat.mekanism.getMekanismEnergySided import ru.dbotthepony.mc.otm.compat.mekanism.mekanismEnergy import ru.dbotthepony.mc.otm.container.iterator import ru.dbotthepony.mc.otm.container.stream -import ru.dbotthepony.mc.otm.core.iterator import ru.dbotthepony.mc.otm.core.ImpreciseFraction import ru.dbotthepony.mc.otm.core.orNull +import java.util.IdentityHashMap import java.util.stream.Stream val ICapabilityProvider.matteryPlayer: MatteryPlayerCapability? get() = getCapability(MatteryCapability.MATTERY_PLAYER).orNull() @@ -152,8 +152,10 @@ fun ICapabilityProvider.getMatteryEnergySided(side: Direction? = null): LazyOpti /** * DO NOT modify returned ItemStacks! + * + * Contains all items that player might carry */ -fun Player.itemStream(): Stream { +fun Player.itemsStream(): Stream { val streams = ArrayList>() streams.add(inventory.stream()) @@ -170,6 +172,27 @@ fun Player.itemStream(): Stream { return Streams.concat(*streams.toTypedArray()) } +/** + * DO NOT modify returned ItemStacks! + * + * Contains all items that player might see/access ([itemsStream] + open container's items) + */ +fun Player.allItemsStream(): Stream { + if (containerMenu == inventoryMenu) { + return itemsStream() + } + + val seen = IdentityHashMap(containerMenu.slots.size) + + for (slot in containerMenu.slots) { + seen[slot.item] = Unit + } + + return Streams.concat( + itemsStream().filter { it.isEmpty || it !in seen }, + containerMenu.slots.stream().map { it.item }) +} + fun Player.extendedItemIterator(): MutableIterator { return object : MutableIterator { private val regular = this@extendedItemIterator.inventory.iterator() diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/data/ItemInInventoryCondition.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/data/ItemInInventoryCondition.kt index 82dc80850..3cbaf2ddd 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/data/ItemInInventoryCondition.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/data/ItemInInventoryCondition.kt @@ -14,7 +14,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 net.minecraftforge.registries.ForgeRegistries -import ru.dbotthepony.mc.otm.capability.itemStream +import ru.dbotthepony.mc.otm.capability.itemsStream import ru.dbotthepony.mc.otm.core.registryName import ru.dbotthepony.mc.otm.core.set import ru.dbotthepony.mc.otm.registry.MLootItemConditions @@ -25,7 +25,7 @@ class ItemInInventoryCondition( val matchNBT: Boolean = false ) : LootItemCondition, LootItemCondition.Builder { override fun test(t: LootContext): Boolean { - val matches = t[LootContextParams.LAST_DAMAGE_PLAYER]?.itemStream()?.filter { + val matches = t[LootContextParams.LAST_DAMAGE_PLAYER]?.itemsStream()?.filter { if (it.isEmpty) { return@filter false } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/item/QuantumBatteryItem.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/item/QuantumBatteryItem.kt index e0528b2ab..c7092b8e7 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/item/QuantumBatteryItem.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/item/QuantumBatteryItem.kt @@ -380,19 +380,17 @@ class QuantumBatteryItem : Item { for (ply in event.server.playerList.players) { val networkedChannels = IntAVLTreeSet() - for (item in ply.containerMenu.itemStackIterator().nonEmpty()) { - if (item.item is QuantumBatteryItem) { - val power = item.getCapability(MatteryCapability.ENERGY).orThrow() as Power + for (item in ply.allItemsStream().filter { !it.isEmpty && it.item is QuantumBatteryItem }) { + val power = item.getCapability(MatteryCapability.ENERGY).orThrow() as Power - power.determineQuantumLinkWeak() + power.determineQuantumLinkWeak() - if (power.data.index < 0) { - continue - } + if (power.data.index < 0) { + continue + } - if (networkedChannels.add(power.data.index)) { - GenericNetworkChannel.send(ply, ChargePacket(item.item as QuantumBatteryItem, power.data.index, power.batteryLevel, power.data.passed, power.data.received)) - } + if (networkedChannels.add(power.data.index)) { + GenericNetworkChannel.send(ply, ChargePacket(item.item as QuantumBatteryItem, power.data.index, power.batteryLevel, power.data.passed, power.data.received)) } } }