From 572e159de71827c5c8a6433de90adba2ebec5c4a Mon Sep 17 00:00:00 2001 From: Thunderblade73 <85900443+Thunderblade73@users.noreply.github.com> Date: Mon, 30 Oct 2023 20:02:47 +0100 Subject: NullPointerException fix for getEntityByID (#652) Fixed rare error message while disconnecting. #652 --- src/main/java/at/hannibal2/skyhanni/data/EntityData.kt | 7 +++---- .../skyhanni/features/event/diana/InquisitorWaypointShare.kt | 4 ++-- .../at/hannibal2/skyhanni/features/garden/visitor/VisitorAPI.kt | 8 ++++---- .../at/hannibal2/skyhanni/features/misc/trevor/TrevorFeatures.kt | 5 +++-- .../hannibal2/skyhanni/features/slayer/SlayerItemProfitTracker.kt | 3 ++- src/main/java/at/hannibal2/skyhanni/test/PacketTest.kt | 5 +++-- src/main/java/at/hannibal2/skyhanni/utils/EntityUtils.kt | 7 +++++++ 7 files changed, 24 insertions(+), 15 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/at/hannibal2/skyhanni/data/EntityData.kt b/src/main/java/at/hannibal2/skyhanni/data/EntityData.kt index b26312b51..1ffa5a70b 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/EntityData.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/EntityData.kt @@ -6,9 +6,9 @@ import at.hannibal2.skyhanni.events.LorenzTickEvent import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent import at.hannibal2.skyhanni.events.PacketEvent import at.hannibal2.skyhanni.utils.EntityUtils +import at.hannibal2.skyhanni.utils.EntityUtils.getEntityByID import at.hannibal2.skyhanni.utils.LorenzUtils.baseMaxHealth import at.hannibal2.skyhanni.utils.LorenzUtils.derpy -import net.minecraft.client.Minecraft import net.minecraft.client.entity.EntityOtherPlayerMP import net.minecraft.client.entity.EntityPlayerSP import net.minecraft.entity.EntityLivingBase @@ -50,8 +50,7 @@ class EntityData { val watchableObjects = packet.func_149376_c() ?: return val entityId = packet.entityId - val theWorld = Minecraft.getMinecraft().theWorld ?: return - val entity = theWorld.getEntityByID(entityId) ?: return + val entity = getEntityByID(entityId) ?: return if (entity is EntityArmorStand) return if (entity is EntityXPOrb) return if (entity is EntityItem) return @@ -77,4 +76,4 @@ class EntityData { } } } -} \ No newline at end of file +} diff --git a/src/main/java/at/hannibal2/skyhanni/features/event/diana/InquisitorWaypointShare.kt b/src/main/java/at/hannibal2/skyhanni/features/event/diana/InquisitorWaypointShare.kt index ae8b1a892..7485d38ae 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/event/diana/InquisitorWaypointShare.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/event/diana/InquisitorWaypointShare.kt @@ -8,6 +8,7 @@ import at.hannibal2.skyhanni.events.LorenzKeyPressEvent import at.hannibal2.skyhanni.events.LorenzTickEvent import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent import at.hannibal2.skyhanni.events.PacketEvent +import at.hannibal2.skyhanni.utils.EntityUtils.getEntityByID import at.hannibal2.skyhanni.utils.KeyboardManager import at.hannibal2.skyhanni.utils.LocationUtils import at.hannibal2.skyhanni.utils.LorenzLogger @@ -19,7 +20,6 @@ import at.hannibal2.skyhanni.utils.SoundUtils import at.hannibal2.skyhanni.utils.StringUtils.cleanPlayerName import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import at.hannibal2.skyhanni.utils.getLorenzVec -import net.minecraft.client.Minecraft import net.minecraft.client.entity.EntityOtherPlayerMP import net.minecraft.network.play.server.S02PacketChat import net.minecraftforge.event.entity.EntityJoinWorldEvent @@ -191,7 +191,7 @@ object InquisitorWaypointShare { return } - val inquisitor = Minecraft.getMinecraft().theWorld.getEntityByID(inquisitor) + val inquisitor = getEntityByID(inquisitor) if (inquisitor == null) { LorenzUtils.chat("§cInquisitor out of range!") return diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/VisitorAPI.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/VisitorAPI.kt index a1a7b5110..d45088171 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/VisitorAPI.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/visitor/VisitorAPI.kt @@ -7,12 +7,12 @@ import at.hannibal2.skyhanni.events.garden.visitor.VisitorLeftEvent import at.hannibal2.skyhanni.events.garden.visitor.VisitorRefusedEvent import at.hannibal2.skyhanni.events.withAlpha import at.hannibal2.skyhanni.test.command.ErrorManager +import at.hannibal2.skyhanni.utils.EntityUtils.getEntityByID import at.hannibal2.skyhanni.utils.LorenzColor import at.hannibal2.skyhanni.utils.LorenzLogger import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.LorenzUtils.editCopy import at.hannibal2.skyhanni.utils.NEUInternalName -import net.minecraft.client.Minecraft import net.minecraft.entity.Entity import net.minecraft.item.ItemStack @@ -116,8 +116,8 @@ object VisitorAPI { var allRewards = listOf() var lastLore = listOf() - fun getEntity(): Entity? = Minecraft.getMinecraft().theWorld.getEntityByID(entityId) - fun getNameTagEntity(): Entity? = Minecraft.getMinecraft().theWorld.getEntityByID(nameTagEntityId) + fun getEntity(): Entity? = getEntityByID(entityId) + fun getNameTagEntity(): Entity? = getEntityByID(nameTagEntityId) fun hasReward(): VisitorReward? { for (internalName in allRewards) { @@ -139,4 +139,4 @@ object VisitorAPI { ACCEPTED("§7Accepted", LorenzColor.DARK_GRAY.toColor().withAlpha(80)), REFUSED("§cRefused", LorenzColor.RED.toColor().withAlpha(60)), } -} \ No newline at end of file +} diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/trevor/TrevorFeatures.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/trevor/TrevorFeatures.kt index 83fd1ac52..3475802dd 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/misc/trevor/TrevorFeatures.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/trevor/TrevorFeatures.kt @@ -14,6 +14,7 @@ import at.hannibal2.skyhanni.features.garden.farming.GardenCropSpeed import at.hannibal2.skyhanni.mixins.hooks.RenderLivingEntityHelper import at.hannibal2.skyhanni.test.GriffinUtils.drawWaypointFilled import at.hannibal2.skyhanni.test.command.ErrorManager +import at.hannibal2.skyhanni.utils.EntityUtils.getEntityByID import at.hannibal2.skyhanni.utils.LocationUtils.distanceToPlayer import at.hannibal2.skyhanni.utils.LorenzColor import at.hannibal2.skyhanni.utils.LorenzUtils @@ -211,9 +212,9 @@ object TrevorFeatures { @SubscribeEvent fun onRenderWorld(event: LorenzRenderWorldEvent) { if (!onFarmingIsland()) return - var entityTrapper = Minecraft.getMinecraft().theWorld.getEntityByID(trapperID) + var entityTrapper = getEntityByID(trapperID) if (entityTrapper !is EntityLivingBase) entityTrapper = - Minecraft.getMinecraft().theWorld.getEntityByID(backupTrapperID) + getEntityByID(backupTrapperID) if (entityTrapper is EntityLivingBase && config.trapperTalkCooldown) { RenderLivingEntityHelper.setEntityColor(entityTrapper, currentStatus.color) { config.trapperTalkCooldown } diff --git a/src/main/java/at/hannibal2/skyhanni/features/slayer/SlayerItemProfitTracker.kt b/src/main/java/at/hannibal2/skyhanni/features/slayer/SlayerItemProfitTracker.kt index 67b5f595f..aec856797 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/slayer/SlayerItemProfitTracker.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/slayer/SlayerItemProfitTracker.kt @@ -14,6 +14,7 @@ import at.hannibal2.skyhanni.events.SlayerChangeEvent import at.hannibal2.skyhanni.events.SlayerQuestCompleteEvent import at.hannibal2.skyhanni.features.bazaar.BazaarApi.Companion.getBazaarData import at.hannibal2.skyhanni.test.PriceSource +import at.hannibal2.skyhanni.utils.EntityUtils.getEntityByID import at.hannibal2.skyhanni.utils.ItemUtils.getInternalNameOrNull import at.hannibal2.skyhanni.utils.ItemUtils.name import at.hannibal2.skyhanni.utils.KeyboardManager @@ -161,7 +162,7 @@ object SlayerItemProfitTracker { if (packet !is S0DPacketCollectItem) return val entityID = packet.collectedItemEntityID - val item = Minecraft.getMinecraft().theWorld.getEntityByID(entityID) ?: return + val item = getEntityByID(entityID) ?: return if (item !is EntityItem) return if (collectedCache.getIfPresent(entityID) != null) return diff --git a/src/main/java/at/hannibal2/skyhanni/test/PacketTest.kt b/src/main/java/at/hannibal2/skyhanni/test/PacketTest.kt index 175e3f65b..e9a683dae 100644 --- a/src/main/java/at/hannibal2/skyhanni/test/PacketTest.kt +++ b/src/main/java/at/hannibal2/skyhanni/test/PacketTest.kt @@ -1,6 +1,7 @@ package at.hannibal2.skyhanni.test import at.hannibal2.skyhanni.events.PacketEvent +import at.hannibal2.skyhanni.utils.EntityUtils.getEntityByID import at.hannibal2.skyhanni.utils.LocationUtils.distanceToPlayer import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.LorenzUtils.round @@ -189,7 +190,7 @@ class PacketTest { return packet.getEntity(world) } if (id != null) { - return world.getEntityByID(id) + return getEntityByID(id) } return null @@ -207,4 +208,4 @@ class PacketTest { else -> null } -} \ No newline at end of file +} diff --git a/src/main/java/at/hannibal2/skyhanni/utils/EntityUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/EntityUtils.kt index 8e0cd9fdb..f1d0544a2 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/EntityUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/EntityUtils.kt @@ -155,4 +155,11 @@ object EntityUtils { }?.asSequence()?.filterNotNull() ?: emptySequence() fun Entity.canBeSeen(radius: Double = 150.0) = getLorenzVec().add(y = 0.5).canBeSeen(radius) + + fun getEntityByID(id: Int): Entity? { + val minecraft = Minecraft.getMinecraft() ?: return null + val world = minecraft.theWorld ?: return null + if (minecraft.thePlayer == null) return null + return world.getEntityByID(id) + } } -- cgit