Skip to content

Commit

Permalink
Fix crash with EntityInfoRenderer (#91
Browse files Browse the repository at this point in the history
Signed-off-by: Hendrix-Shen <[email protected]>
  • Loading branch information
Hendrix-Shen committed Jul 6, 2023
1 parent ebfd02c commit aae0554
Showing 1 changed file with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
Expand All @@ -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) {
Expand All @@ -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;
}
}

0 comments on commit aae0554

Please sign in to comment.