From 2e7e676739fad0e0613e3b5e28c56143b35d9956 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Sun, 4 Sep 2022 21:04:40 +0700 Subject: [PATCH] Make exosuit inventory contents appear in LivingDropsEvent when possible --- .../mc/otm/OverdriveThatMatters.java | 1 + .../otm/capability/MatteryPlayerCapability.kt | 58 ++++++++++++++++--- 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/src/main/java/ru/dbotthepony/mc/otm/OverdriveThatMatters.java b/src/main/java/ru/dbotthepony/mc/otm/OverdriveThatMatters.java index 8be14da45..9b3eb9588 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/OverdriveThatMatters.java +++ b/src/main/java/ru/dbotthepony/mc/otm/OverdriveThatMatters.java @@ -107,6 +107,7 @@ public final class OverdriveThatMatters { EVENT_BUS.addListener(EventPriority.LOWEST, MatteryPlayerCapability.Companion::onPlayerDeath); EVENT_BUS.addListener(EventPriority.NORMAL, MatteryPlayerCapability.Companion::onPlayerCloneEvent); EVENT_BUS.addListener(EventPriority.LOW, MatteryPlayerCapability.Companion::onPickupEvent); + EVENT_BUS.addListener(EventPriority.HIGHEST, MatteryPlayerCapability.Companion::onLivingDrops); EVENT_BUS.addListener(EventPriority.LOW, MatterDataKt::serverStartData); EVENT_BUS.addListener(EventPriority.NORMAL, MatterRegistryKt::onPlayerJoin); 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 453db529b..4736e9ee3 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/MatteryPlayerCapability.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/MatteryPlayerCapability.kt @@ -13,6 +13,7 @@ import net.minecraft.server.level.ServerPlayer import net.minecraft.world.effect.MobEffect import net.minecraft.world.effect.MobEffects import net.minecraft.world.entity.Entity +import net.minecraft.world.entity.LivingEntity import net.minecraft.world.entity.item.ItemEntity import net.minecraft.world.entity.player.Player import net.minecraft.world.item.ItemStack @@ -25,6 +26,7 @@ import net.minecraftforge.common.util.INBTSerializable import net.minecraftforge.common.util.LazyOptional import net.minecraftforge.event.AttachCapabilitiesEvent import net.minecraftforge.event.entity.living.LivingDeathEvent +import net.minecraftforge.event.entity.living.LivingDropsEvent import net.minecraftforge.event.entity.living.LivingEvent.LivingTickEvent import net.minecraftforge.event.entity.living.LivingHurtEvent import net.minecraftforge.event.entity.player.EntityItemPickupEvent @@ -33,6 +35,7 @@ import net.minecraftforge.eventbus.api.EventPriority import net.minecraftforge.eventbus.api.SubscribeEvent import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent import org.apache.commons.lang3.mutable.MutableInt +import org.apache.logging.log4j.LogManager import ru.dbotthepony.mc.otm.* import ru.dbotthepony.mc.otm.android.AndroidFeature import ru.dbotthepony.mc.otm.android.AndroidFeatureType @@ -47,6 +50,7 @@ import ru.dbotthepony.mc.otm.registry.MRegistry import ru.dbotthepony.mc.otm.registry.StatNames import java.util.* import kotlin.collections.ArrayDeque +import kotlin.collections.ArrayList @Suppress("unused") class MatteryPlayerCapability(val ply: Player) : ICapabilityProvider, IMatteryEnergyStorage, INBTSerializable { @@ -96,18 +100,24 @@ class MatteryPlayerCapability(val ply: Player) : ICapabilityProvider, IMatteryEn field = value } - fun dropExoSuitInventory() { - val iterator = exoSuitContainer.iterator() + fun dropExoSuitInventory(): List { + val result = ArrayList() + val oldCaptureDrops = ply.captureDrops(result) as MutableCollection? + + val iterator = exoSuitContainer.iterator().nonEmpty() for (item in iterator) { - if (!item.isEmpty) { - if (!hasVanishingCurse(item)) { - ply.drop(item, true, false) - } - - iterator.remove() + if (!hasVanishingCurse(item)) { + ply.drop(item, true, false)?.also(result::add) } + + iterator.remove() } + + ply.captureDrops(oldCaptureDrops) + oldCaptureDrops?.addAll(result) + + return result } var isExoSuitCraftingUpgraded by synchronizer.bool(setter = setter@{ value, access, _ -> @@ -822,6 +832,23 @@ class MatteryPlayerCapability(val ply: Player) : ICapabilityProvider, IMatteryEn .ifPresentK { it.invalidateNetworkState() } } + private val waitingToDie = ArrayList>() + + fun onLivingDrops(event: LivingDropsEvent) { + val indexOf = waitingToDie.indexOfFirst { event.entity == it.first } + + if (indexOf == -1) + return + + val drops = waitingToDie[indexOf].second.dropExoSuitInventory() + + if (event.entity.captureDrops() == null) { + event.drops.addAll(drops) + } + + waitingToDie.removeAt(indexOf) + } + fun onPlayerDeath(event: LivingDeathEvent) { val ply = event.entity as? Player ?: return val mattery = ply.matteryPlayer ?: return @@ -833,7 +860,18 @@ class MatteryPlayerCapability(val ply: Player) : ICapabilityProvider, IMatteryEn } if (mattery.hasExoSuit && !ply.level.gameRules.getBoolean(GameRules.RULE_KEEPINVENTORY)) { - mattery.dropExoSuitInventory() + waitingToDie.add(ply to mattery) + + addPostServerTickerOnce { + val index = waitingToDie.indexOfFirst { it.first == ply } + + if (index != -1) { + waitingToDie.removeAt(index) + mattery.dropExoSuitInventory().forEach { ply.level.addFreshEntity(it) } + LOGGER.warn("Unable to 'properly' drop $ply's ExoSuit inventory contents") + LOGGER.warn("I blame Forge.") + } + } } if (!mattery.isAndroid) { @@ -881,6 +919,8 @@ class MatteryPlayerCapability(val ply: Player) : ICapabilityProvider, IMatteryEn private val itemPickupTicks = WeakHashMap>() + private val LOGGER = LogManager.getLogger() + fun onPickupEvent(event: EntityItemPickupEvent) { if (event.item.owner != null && event.item.owner != event.entity.uuid && event.item.age < 200 || event.item.item.isEmpty) { return