From aae05541e19b00d9ddb8fc33c64ae6c8014364b5 Mon Sep 17 00:00:00 2001 From: Hendrix-Shen Date: Thu, 6 Jul 2023 12:58:47 +0800 Subject: [PATCH] Fix crash with EntityInfoRenderer (#91 Signed-off-by: Hendrix-Shen --- .../entityInfo/EntityInfoRenderer.java | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/plusls/MasaGadget/generic/entityInfo/EntityInfoRenderer.java b/src/main/java/com/plusls/MasaGadget/generic/entityInfo/EntityInfoRenderer.java index edeaf2d8..9c9f2b3a 100644 --- a/src/main/java/com/plusls/MasaGadget/generic/entityInfo/EntityInfoRenderer.java +++ b/src/main/java/com/plusls/MasaGadget/generic/entityInfo/EntityInfoRenderer.java @@ -2,6 +2,7 @@ import com.google.common.collect.Lists; import com.plusls.MasaGadget.config.Configs; +import com.plusls.MasaGadget.util.MiscUtil; import net.minecraft.client.Minecraft; import net.minecraft.client.server.IntegratedServer; import net.minecraft.core.Position; @@ -37,7 +38,7 @@ private static void collect(Entity entity, RenderContext context, float tickDelt public static void render(Level level, RenderContext context, float tickDelta) { for (Entity entity : EntityInfoRenderer.list) { if (entity instanceof Villager) { - Villager villager = (Villager) EntityInfoRenderer.getEntityDataFromIntegratedServer(entity); + Villager villager = MiscUtil.cast(EntityInfoRenderer.syncEntityDataFromIntegratedServer(entity)); TextRenderer renderer = TextRenderer.create(); if (Configs.renderNextRestockTime) { @@ -48,13 +49,13 @@ public static void render(Level level, RenderContext context, float tickDelta) { VillageTradeEnchantedBookInfo.getInfo(villager).forEach(renderer::addLine); } - EntityInfoRenderer.rotationAround(renderer, villager.getEyePosition(tickDelta), 0.7) + EntityInfoRenderer.rotationAround(renderer, entity.getEyePosition(tickDelta), 0.7) .bgColor((int) (Minecraft.getInstance().options.getBackgroundOpacity(0.25F) * 255.0F) << 24) .fontScale(0.015F) .render(context); } else if (entity instanceof ZombieVillager) { - ZombieVillager zombieVillager = (ZombieVillager) EntityInfoRenderer.getEntityDataFromIntegratedServer(entity); - EntityInfoRenderer.rotationAround(TextRenderer.create(), zombieVillager.getEyePosition(tickDelta), 0.6) + ZombieVillager zombieVillager = MiscUtil.cast(EntityInfoRenderer.syncEntityDataFromIntegratedServer(entity)); + EntityInfoRenderer.rotationAround(TextRenderer.create(), entity.getEyePosition(tickDelta), 0.6) .text(ZombieVillagerConvertTimeInfo.getInfo(zombieVillager)) .bgColor((int) (Minecraft.getInstance().options.getBackgroundOpacity(0.25F) * 255.0F) << 24) .fontScale(0.015F) @@ -72,7 +73,12 @@ private static TextRenderer rotationAround(@NotNull TextRenderer renderer, @NotN return renderer.pos(range * Mth.cos(xAngle) + centerPos.x(), centerPos.y(), range * Mth.cos(yAngle) + centerPos.z()); } - private static Entity getEntityDataFromIntegratedServer(Entity entity) { + /** + * Try to get entity data from the integrated server. + * + * @return Return synced data if the access is successful, otherwise return input entity. + */ + private static Entity syncEntityDataFromIntegratedServer(Entity entity) { IntegratedServer server = Minecraft.getInstance().getSingleplayerServer(); if (server == null) { @@ -84,6 +90,12 @@ private static Entity getEntityDataFromIntegratedServer(Entity entity) { //#else //$$ ServerLevel level = server.getLevel(entity.dimension); //#endif - return level == null ? entity : level.getEntity(entity.getId()); + + if (level == null) { + return entity; + } + + Entity localEntity = level.getEntity(entity.getId()); + return localEntity == null ? entity : localEntity; } }