diff options
author | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-05-24 23:20:53 +0200 |
---|---|---|
committer | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-05-24 23:20:53 +0200 |
commit | 617130c7f52d35335cd6f4f7f14e7fd3dc3af3af (patch) | |
tree | ff16fe3469759c85be289c3e88fa48b65e98f1f2 /src/main/java/at/hannibal2/skyhanni/features/misc | |
parent | 1ab8930cee2d6740b9b9dbf0a496c5e6e0f195fe (diff) | |
download | skyhanni-617130c7f52d35335cd6f4f7f14e7fd3dc3af3af.tar.gz skyhanni-617130c7f52d35335cd6f4f7f14e7fd3dc3af3af.tar.bz2 skyhanni-617130c7f52d35335cd6f4f7f14e7fd3dc3af3af.zip |
Reworked Hide Armor
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/features/misc')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/features/misc/HideArmor.kt | 233 |
1 files changed, 35 insertions, 198 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/HideArmor.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/HideArmor.kt index 6c081f243..96301ee7c 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/misc/HideArmor.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/HideArmor.kt @@ -1,73 +1,50 @@ package at.hannibal2.skyhanni.features.misc import at.hannibal2.skyhanni.SkyHanniMod -import at.hannibal2.skyhanni.events.ConfigLoadEvent -import at.hannibal2.skyhanni.events.PacketEvent +import at.hannibal2.skyhanni.utils.EntityUtils.getArmorInventory +import at.hannibal2.skyhanni.utils.EntityUtils.hasPotionEffect +import at.hannibal2.skyhanni.utils.EntityUtils.isNPC import at.hannibal2.skyhanni.utils.LorenzUtils -import at.hannibal2.skyhanni.utils.LorenzUtils.onToggle -import net.minecraft.client.Minecraft -import net.minecraft.client.entity.EntityOtherPlayerMP import net.minecraft.client.entity.EntityPlayerSP -import net.minecraft.client.gui.inventory.GuiInventory +import net.minecraft.entity.EntityLivingBase import net.minecraft.entity.player.EntityPlayer import net.minecraft.item.ItemStack -import net.minecraft.network.play.server.S04PacketEntityEquipment -import net.minecraft.network.play.server.S2FPacketSetSlot -import net.minecraft.network.play.server.S30PacketWindowItems -import net.minecraftforge.event.world.WorldEvent +import net.minecraft.potion.Potion +import net.minecraftforge.client.event.RenderLivingEvent import net.minecraftforge.fml.common.eventhandler.SubscribeEvent -import net.minecraftforge.fml.common.gameevent.TickEvent class HideArmor { + private val config get() = SkyHanniMod.feature.misc.hideArmor2 + private var armor = mapOf<Int, ItemStack>() - private var invOpen = false - private val laterCheck = mutableListOf<Int>() + private fun shouldHideArmor(entity: EntityLivingBase): Boolean { + if (!LorenzUtils.inSkyBlock) return false + if (entity !is EntityPlayer) return false + if (entity.hasPotionEffect(Potion.invisibility)) return false + if (entity.isNPC()) return false - @SubscribeEvent - fun onGuiInventoryToggle(event: TickEvent.ClientTickEvent) { - if (!LorenzUtils.inSkyBlock) return // TODO test this - - fixOtherArmor() + return when (config.mode) { + 0 -> true - if (!SkyHanniMod.feature.misc.hideArmorEnabled.get()) return + 1 -> entity is EntityPlayerSP + 2 -> entity !is EntityPlayerSP - val currentScreen = Minecraft.getMinecraft().currentScreen - if (currentScreen == null || currentScreen !is GuiInventory) { - if (invOpen) { - invOpen = false - changeArmor(Minecraft.getMinecraft().thePlayer, null) - } - } else { - if (!invOpen) { - invOpen = true - val thePlayer = Minecraft.getMinecraft().thePlayer - val entityId = thePlayer.entityId - changeArmor(thePlayer, getCachedArmor(entityId)) - } + else -> false } } @SubscribeEvent - fun onConfigLoaded(event: ConfigLoadEvent) { - val misc = SkyHanniMod.feature.misc - onToggle(misc.hideArmorEnabled, misc.hideArmorOwn, misc.hideArmorOnlyHelmet) { updateArmor() } - } - - - // Since S04PacketEntityEquipment gets sent before the entity is fully loaded, I need to remove the armor later - private fun fixOtherArmor() { - for (entity in Minecraft.getMinecraft().theWorld.loadedEntityList) { - if (entity !is EntityOtherPlayerMP) continue - - val entityId = entity.entityId - if (entityId !in laterCheck) continue - - laterCheck.remove(entityId) - if (SkyHanniMod.feature.misc.hideArmorEnabled.get()) { - val armorInventory = entity.inventory.armorInventory - for ((equipmentSlot, _) in armorInventory.withIndex()) { - if (!SkyHanniMod.feature.misc.hideArmorOnlyHelmet.get() || equipmentSlot == 3) { - armorInventory[equipmentSlot] = null + fun onRenderLivingPre(event: RenderLivingEvent.Pre<EntityLivingBase>) { + val entity = event.entity + if (!shouldHideArmor(entity)) return + val armorInventory = entity.getArmorInventory() ?: return + + armor = buildMap { + for ((i, stack) in armorInventory.withIndex()) { + stack?.let { + if (!config.onlyHelmet || i == 3) { + this[i] = it.copy() + armorInventory[i] = null } } } @@ -75,153 +52,13 @@ class HideArmor { } @SubscribeEvent - fun onPacketReceive(event: PacketEvent.ReceiveEvent) { - val packet = event.packet - - //own player world switch - if (packet is S30PacketWindowItems) { - - // check window id - if (packet.func_148911_c() != 0) return - - for ((slot, itemStack) in packet.itemStacks.withIndex()) { + fun onRenderLivingPost(event: RenderLivingEvent.Post<EntityLivingBase>) { + val entity = event.entity + if (!shouldHideArmor(entity)) return + val armorInventory = entity.getArmorInventory() ?: return - if (slot !in 5..8) continue - - val armorSlot = (slot - 5) * -1 + 3 - val entityId = Minecraft.getMinecraft()?.thePlayer?.entityId ?: return - val armor = getCachedArmor(entityId) - armor[armorSlot] = itemStack - - val currentScreen = Minecraft.getMinecraft().currentScreen - if (currentScreen == null || currentScreen !is GuiInventory) { - if (SkyHanniMod.feature.misc.hideArmorEnabled.get()) { - if (SkyHanniMod.feature.misc.hideArmorOwn.get()) { - if (!SkyHanniMod.feature.misc.hideArmorOnlyHelmet.get() || armorSlot == 3) { - packet.itemStacks[slot] = null - } - } - } - } - } - return + for ((index, stack) in armor) { + armorInventory[index] = stack } - - - //own player armor change - if (packet is S2FPacketSetSlot) { - val slot = packet.func_149173_d() - - // check window id - if (packet.func_149175_c() != 0) return - if (slot !in 5..8) return - - val armorSlot = (slot - 5) * -1 + 3 - val ownEntityId = Minecraft.getMinecraft()?.thePlayer?.entityId ?: return - val armor = getCachedArmor(ownEntityId) - // set item in cache - armor[armorSlot] = packet.func_149174_e() - - val currentScreen = Minecraft.getMinecraft().currentScreen - if (currentScreen == null || currentScreen !is GuiInventory) { - if (SkyHanniMod.feature.misc.hideArmorEnabled.get()) { - if (SkyHanniMod.feature.misc.hideArmorOwn.get()) { - if (!SkyHanniMod.feature.misc.hideArmorOnlyHelmet.get() || armorSlot == 3) { - event.isCanceled = true - } - } - } - } - return - } - - - //other player armor switch - if (packet is S04PacketEntityEquipment) { - val entityID = packet.entityID - val equipmentSlot = packet.equipmentSlot - 1 - if (equipmentSlot == -1) return - - val entity = Minecraft.getMinecraft().theWorld?.getEntityByID(entityID) - if (entity == null) { - laterCheck.add(entityID) - return - } - - if (entity !is EntityOtherPlayerMP) return - - val armor = getCachedArmor(entityID) - - // set item in cache - armor[equipmentSlot] = packet.itemStack - - if (SkyHanniMod.feature.misc.hideArmorEnabled.get()) { - if (!SkyHanniMod.feature.misc.hideArmorOnlyHelmet.get() || equipmentSlot == 3) { - event.isCanceled = true - } - } - } - } - - private fun getCachedArmor(entityID: Int): Array<ItemStack?> { - val armor: Array<ItemStack?> = if (armorCache.containsKey(entityID)) { - armorCache[entityID]!! - } else { - val new = arrayOf<ItemStack?>(null, null, null, null) - armorCache[entityID] = new - new - } - return armor - } - - companion object { - var armorCache: MutableMap<Int, Array<ItemStack?>> = mutableMapOf() - - fun updateArmor() { - for (entity in Minecraft.getMinecraft().theWorld.loadedEntityList) { - if (entity !is EntityPlayer) continue - - val entityId = entity.entityId - armorCache[entityId]?.let { - changeArmor(entity, it) - } - - if (SkyHanniMod.feature.misc.hideArmorEnabled.get()) { - changeArmor(entity, null) - } - } - } - - private fun changeArmor(entity: EntityPlayer, new: Array<ItemStack?>?) { - if (!LorenzUtils.inSkyBlock) return - - val current = entity.inventory.armorInventory - if (new != null) { - current[0] = new[0] - current[1] = new[1] - current[2] = new[2] - current[3] = new[3] - return - } - - if (!SkyHanniMod.feature.misc.hideArmorOwn.get()) { - if (entity is EntityPlayerSP) { - return - } - } - - if (!SkyHanniMod.feature.misc.hideArmorOnlyHelmet.get()) { - current[0] = null - current[1] = null - current[2] = null - } - current[3] = null - } - } - - @SubscribeEvent - fun onWorldChange(event: WorldEvent.Load) { - armorCache.clear() - laterCheck.clear() } }
\ No newline at end of file |