From 405fa915e2d04a837b8598553ba7e64fe6c8383d Mon Sep 17 00:00:00 2001 From: babbaj Date: Wed, 15 Aug 2018 18:59:35 -0400 Subject: [PATCH] Fix npe caused by null entities --- .../zeroeightsix/kami/gui/kami/KamiGUI.java | 95 ++++++++++--------- .../rgui/component/listen/TickListener.java | 2 +- 2 files changed, 50 insertions(+), 47 deletions(-) diff --git a/src/main/java/me/zeroeightsix/kami/gui/kami/KamiGUI.java b/src/main/java/me/zeroeightsix/kami/gui/kami/KamiGUI.java index 4e6856e9..a58f6104 100644 --- a/src/main/java/me/zeroeightsix/kami/gui/kami/KamiGUI.java +++ b/src/main/java/me/zeroeightsix/kami/gui/kami/KamiGUI.java @@ -29,8 +29,11 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.projectile.EntityEgg; import net.minecraft.entity.projectile.EntitySnowball; import net.minecraft.entity.projectile.EntityWitherSkull; +import net.minecraft.util.text.TextFormatting; +import javax.annotation.Nonnull; import java.util.*; +import java.util.stream.Collectors; /** * Created by 086 on 25/06/2017. @@ -250,59 +253,30 @@ public class KamiGUI extends GUI { @Override public void onTick() { if (mc.player == null || !entityLabel.isVisible()) return; - List entityList = mc.world.loadedEntityList; + + final List entityList = new ArrayList<>(mc.world.loadedEntityList); if (entityList.size() <= 1) { entityLabel.setText(""); return; } - - Map entityMap = new HashMap<>(); - List copy = new ArrayList<>(entityList); - for (Entity e : copy) { - if (e instanceof EntityPlayer) continue; - - String name = e.getName(); - - int add = 1; - - if (e instanceof EntityItem) { - name = Command.SECTIONSIGN() + "3" + ((EntityItem) e).getItem().getItem().getItemStackDisplayName(((EntityItem) e).getItem()); - add = ((EntityItem) e).getItem().getCount(); - } - if (e instanceof EntityWitherSkull) { - name = Command.SECTIONSIGN() + "8" + "Wither skull"; - } - if (e instanceof EntityEnderCrystal) { - name = Command.SECTIONSIGN() + "d" + "End crystal"; - } - if (e instanceof EntityEnderPearl) { - name = "Thrown ender pearl"; - } - if (e instanceof EntityMinecart) { - name = "Minecart"; - } - if (e instanceof EntityItemFrame) { - name = "Item frame"; - } - if (e instanceof EntityEgg) { - name = "Thrown egg"; - } - if (e instanceof EntitySnowball) { - name = "Thrown snowball"; - } - - int count = entityMap.containsKey(name) ? entityMap.get(name) : 0; - entityMap.put(name, count + add); - } - - entityMap = sortByValue(entityMap); + final Map entityCounts = entityList.stream() + .filter(Objects::nonNull) + .filter(e -> !(e instanceof EntityPlayer)) + .collect(Collectors.groupingBy(KamiGUI::getEntityName, + Collectors.reducing(0, ent -> { + if (ent instanceof EntityItem) + return ((EntityItem)ent).getItem().getCount(); + return 1; + }, Integer::sum) + )); entityLabel.setText(""); - for (Map.Entry entry : entityMap.entrySet()) { - entityLabel.addLine(Command.SECTIONSIGN() + "7" + entry.getKey() + " " + Command.SECTIONSIGN() + "8x" + entry.getValue()); - } + entityCounts.entrySet().stream() + .sorted(Map.Entry.comparingByValue()) + .map(entry -> TextFormatting.GRAY + entry.getKey() + " " + TextFormatting.DARK_GRAY + "x" + entry.getValue()) + .forEach(entityLabel::addLine); -// entityLabel.getParent().setHeight(entityLabel.getLines().length * (entityLabel.getTheme().getFontRenderer().getFontHeight()+1) + 3); + //entityLabel.getParent().setHeight(entityLabel.getLines().length * (entityLabel.getTheme().getFontRenderer().getFontHeight()+1) + 3); } }); frame.addChild(entityLabel); @@ -383,6 +357,35 @@ public class KamiGUI extends GUI { } } + private static String getEntityName(@Nonnull Entity entity) { + if (entity instanceof EntityItem) { + return TextFormatting.DARK_AQUA + ((EntityItem) entity).getItem().getItem().getItemStackDisplayName(((EntityItem) entity).getItem()); + } + if (entity instanceof EntityWitherSkull) { + return TextFormatting.DARK_GRAY + "Wither skull"; + } + if (entity instanceof EntityEnderCrystal) { + return TextFormatting.LIGHT_PURPLE + "End crystal"; + } + if (entity instanceof EntityEnderPearl) { + return "Thrown ender pearl"; + } + if (entity instanceof EntityMinecart) { + return "Minecart"; + } + if (entity instanceof EntityItemFrame) { + return "Item frame"; + } + if (entity instanceof EntityEgg) { + return "Thrown egg"; + } + if (entity instanceof EntitySnowball) { + return "Thrown snowball"; + } + + return entity.getName(); + } + public static > Map sortByValue(Map map) { List> list = new LinkedList<>(map.entrySet()); diff --git a/src/main/java/me/zeroeightsix/kami/gui/rgui/component/listen/TickListener.java b/src/main/java/me/zeroeightsix/kami/gui/rgui/component/listen/TickListener.java index e6507120..a5573112 100644 --- a/src/main/java/me/zeroeightsix/kami/gui/rgui/component/listen/TickListener.java +++ b/src/main/java/me/zeroeightsix/kami/gui/rgui/component/listen/TickListener.java @@ -4,5 +4,5 @@ package me.zeroeightsix.kami.gui.rgui.component.listen; * Created by 086 on 5/08/2017. */ public interface TickListener { - public void onTick(); + void onTick(); }