From c3a969a16a0f7b84970b684fcc4c83c567817f8f Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Mon, 29 Aug 2022 12:56:16 +0700 Subject: [PATCH] Inspiring stuff. --- .../otm/capability/MatteryPlayerCapability.kt | 33 +++++++++++++++++++ .../mc/otm/container/MatteryContainer.kt | 5 +-- .../otm/item/PortableCondensationDriveItem.kt | 5 +-- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/MatteryPlayerCapability.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/MatteryPlayerCapability.kt index afc71d748..92bba202f 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/MatteryPlayerCapability.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/MatteryPlayerCapability.kt @@ -23,6 +23,7 @@ import net.minecraftforge.common.util.LazyOptional import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent import net.minecraft.world.effect.MobEffects import net.minecraft.world.entity.Entity +import net.minecraft.world.entity.item.ItemEntity import net.minecraft.world.entity.player.Player import net.minecraftforge.common.capabilities.Capability import net.minecraftforge.common.capabilities.ForgeCapabilities @@ -30,11 +31,14 @@ import net.minecraftforge.event.AttachCapabilitiesEvent import net.minecraftforge.event.entity.living.LivingDeathEvent import net.minecraftforge.eventbus.api.SubscribeEvent import net.minecraftforge.event.entity.living.LivingEvent.LivingTickEvent +import net.minecraftforge.event.entity.player.EntityItemPickupEvent import net.minecraftforge.event.entity.player.PlayerEvent import net.minecraftforge.eventbus.api.EventPriority +import org.apache.commons.lang3.mutable.MutableInt import ru.dbotthepony.mc.otm.* import ru.dbotthepony.mc.otm.android.AndroidResearch import ru.dbotthepony.mc.otm.android.AndroidResearchType +import ru.dbotthepony.mc.otm.container.MatteryContainer import ru.dbotthepony.mc.otm.core.ImpreciseFraction import ru.dbotthepony.mc.otm.network.* import ru.dbotthepony.mc.otm.registry.AndroidFeatures @@ -46,6 +50,8 @@ import kotlin.collections.ArrayDeque @Suppress("unused") class MatteryPlayerCapability(val ply: Player) : ICapabilityProvider, IMatteryEnergyStorage, INBTSerializable { + var exoSuitItems = MatteryContainer(20) + private var shouldSendIteration = false var iteration = 0 private set @@ -292,6 +298,8 @@ class MatteryPlayerCapability(val ply: Player) : ICapabilityProvider, IMatteryEn } } + tag["exo_suit_inventory"] = exoSuitItems.serializeNBT() + return tag } @@ -360,6 +368,8 @@ class MatteryPlayerCapability(val ply: Player) : ICapabilityProvider, IMatteryEn } } } + + exoSuitItems.deserializeNBT(compound["exo_suit_inventory"]) } fun dropBattery() { @@ -803,6 +813,29 @@ class MatteryPlayerCapability(val ply: Player) : ICapabilityProvider, IMatteryEn val ENERGY_FOR_HUNGER_POINT = ImpreciseFraction(1000) const val SLEEP_TICKS_LIMIT = 80 + + /* + private val itemPickupTicks = WeakHashMap>() + + @SubscribeEvent(priority = EventPriority.LOW) + fun onPickupEvent(event: EntityItemPickupEvent) { + if (event.item.owner != null && event.item.owner != event.entity.uuid && event.item.age < 200 || event.item.item.isEmpty) { + return + } + + val it = event.entity.matteryPlayer ?: return + + val ticks = itemPickupTicks.computeIfAbsent(event.entity) { WeakHashMap() }.computeIfAbsent(event.item) { MutableInt() } + ticks.increment() + + if (ticks.value < 10) { + return + } + + val leftover = it.exoSuitItems.addItem(event.item.item, false) + event.item.item.count = leftover.count + } + */ } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/container/MatteryContainer.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/container/MatteryContainer.kt index 22e21768c..9ec6ce863 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/container/MatteryContainer.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/container/MatteryContainer.kt @@ -329,9 +329,10 @@ open class MatteryContainer(val watcher: Runnable, private val size: Int) : Cont } } -@JvmOverloads fun Container.addItem(stack: ItemStack, range: IntRange, simulate: Boolean = false): ItemStack { - check(this !is MatteryContainer) { "call MatteryContainer#addItem instead" } + if (this is MatteryContainer) { + return this.addItem(stack, range, simulate) + } if (range.last >= containerSize || range.first < 0) throw IllegalArgumentException("Invalid range: $range") diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/item/PortableCondensationDriveItem.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/item/PortableCondensationDriveItem.kt index 20ecc6f35..746d2b61a 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/item/PortableCondensationDriveItem.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/item/PortableCondensationDriveItem.kt @@ -20,6 +20,7 @@ import net.minecraftforge.common.capabilities.Capability import net.minecraftforge.event.ForgeEventFactory import net.minecraftforge.eventbus.api.SubscribeEvent import net.minecraftforge.event.entity.player.EntityItemPickupEvent +import net.minecraftforge.eventbus.api.EventPriority import ru.dbotthepony.mc.otm.TextComponent import ru.dbotthepony.mc.otm.capability.drive.DrivePool import ru.dbotthepony.mc.otm.container.ItemFilter @@ -101,9 +102,9 @@ class PortableCondensationDriveItem(capacity: Int) : const val MAX_FILTERS = 4 * 3 const val FILTER_PATH = "filter" - @SubscribeEvent + @SubscribeEvent(priority = EventPriority.LOWEST) fun onPickupEvent(event: EntityItemPickupEvent) { - if (event.item.owner != null && event.item.owner != event.entity.uuid && event.item.age < 200) { + if (event.item.owner != null && event.item.owner != event.entity.uuid && event.item.age < 200 || event.item.item.isEmpty) { return }