aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/misc/HideArmor.kt
blob: aab1db9adc3cff17b5a11d88b119a7a1f7cc8a5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package at.hannibal2.skyhanni.features.misc

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.events.PacketEvent
import at.hannibal2.skyhanni.utils.LorenzUtils
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.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.minecraftforge.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.fml.common.gameevent.TickEvent

class HideArmor {

    private var invOpen = false
    private val laterCheck = mutableListOf<Int>()

    @SubscribeEvent
    fun onGuiInventoryToggle(event: TickEvent.ClientTickEvent) {
        if (!LorenzUtils.inSkyBlock) return // TODO test this

        fixOtherArmor()

        if (!SkyHanniMod.feature.misc.hideArmorEnabled) return

        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))
            }
        }
    }

    // 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) {
                val armorInventory = entity.inventory.armorInventory
                for ((equipmentSlot, _) in armorInventory.withIndex()) {
                    if (!SkyHanniMod.feature.misc.hideArmorOnlyHelmet || equipmentSlot == 3) {
                        armorInventory[equipmentSlot] = null
                    }
                }
            }
        }
    }

    @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()) {

                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) {
                        if (SkyHanniMod.feature.misc.hideArmorOwn) {
                            if (!SkyHanniMod.feature.misc.hideArmorOnlyHelmet || armorSlot == 3) {
                                packet.itemStacks[slot] = null
                            }
                        }
                    }
                }
            }
            return
        }


        //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 armor = getCachedArmor(Minecraft.getMinecraft().thePlayer.entityId)
            // 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) {
                    if (SkyHanniMod.feature.misc.hideArmorOwn) {
                        if (!SkyHanniMod.feature.misc.hideArmorOnlyHelmet || 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) {
                if (!SkyHanniMod.feature.misc.hideArmorOnlyHelmet || 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) {
                    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) {
                if (entity is EntityPlayerSP) {
                    return
                }
            }

            if (!SkyHanniMod.feature.misc.hideArmorOnlyHelmet) {
                current[0] = null
                current[1] = null
                current[2] = null
            }
            current[3] = null
        }
    }

    @SubscribeEvent
    fun onWorldChange(event: WorldEvent.Load) {
        armorCache.clear()
        laterCheck.clear()
    }
}