Fix npe caused by null entities

This commit is contained in:
babbaj 2018-08-15 18:59:35 -04:00
parent c8d8c91fe0
commit 405fa915e2
2 changed files with 50 additions and 47 deletions

View File

@ -29,8 +29,11 @@ import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityEgg; import net.minecraft.entity.projectile.EntityEgg;
import net.minecraft.entity.projectile.EntitySnowball; import net.minecraft.entity.projectile.EntitySnowball;
import net.minecraft.entity.projectile.EntityWitherSkull; import net.minecraft.entity.projectile.EntityWitherSkull;
import net.minecraft.util.text.TextFormatting;
import javax.annotation.Nonnull;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
/** /**
* Created by 086 on 25/06/2017. * Created by 086 on 25/06/2017.
@ -250,59 +253,30 @@ public class KamiGUI extends GUI {
@Override @Override
public void onTick() { public void onTick() {
if (mc.player == null || !entityLabel.isVisible()) return; if (mc.player == null || !entityLabel.isVisible()) return;
List<Entity> entityList = mc.world.loadedEntityList;
final List<Entity> entityList = new ArrayList<>(mc.world.loadedEntityList);
if (entityList.size() <= 1) { if (entityList.size() <= 1) {
entityLabel.setText(""); entityLabel.setText("");
return; return;
} }
final Map<String, Integer> entityCounts = entityList.stream()
Map<String, Integer> entityMap = new HashMap<>(); .filter(Objects::nonNull)
List<Entity> copy = new ArrayList<>(entityList); .filter(e -> !(e instanceof EntityPlayer))
for (Entity e : copy) { .collect(Collectors.groupingBy(KamiGUI::getEntityName,
if (e instanceof EntityPlayer) continue; Collectors.reducing(0, ent -> {
if (ent instanceof EntityItem)
String name = e.getName(); return ((EntityItem)ent).getItem().getCount();
return 1;
int add = 1; }, Integer::sum)
));
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);
entityLabel.setText(""); entityLabel.setText("");
for (Map.Entry<String, Integer> entry : entityMap.entrySet()) { entityCounts.entrySet().stream()
entityLabel.addLine(Command.SECTIONSIGN() + "7" + entry.getKey() + " " + Command.SECTIONSIGN() + "8x" + entry.getValue()); .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); 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 <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) { public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
List<Map.Entry<K, V>> list = List<Map.Entry<K, V>> list =
new LinkedList<>(map.entrySet()); new LinkedList<>(map.entrySet());

View File

@ -4,5 +4,5 @@ package me.zeroeightsix.kami.gui.rgui.component.listen;
* Created by 086 on 5/08/2017. * Created by 086 on 5/08/2017.
*/ */
public interface TickListener { public interface TickListener {
public void onTick(); void onTick();
} }