package mcp.mobius.opis.data.managers; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import cpw.mods.fml.common.network.PacketDispatcher; import cpw.mods.fml.common.network.Player; import cpw.mods.fml.common.registry.GameRegistry; import net.minecraft.block.Block; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityList; import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.monster.EntityMob; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.item.Item; import net.minecraft.network.packet.Packet3Chat; import net.minecraft.network.packet.Packet41EntityEffect; import net.minecraft.network.packet.Packet9Respawn; import net.minecraft.potion.PotionEffect; import net.minecraft.server.MinecraftServer; import net.minecraft.util.ChatMessageComponent; import net.minecraft.util.ChunkCoordinates; import net.minecraft.util.MathHelper; import net.minecraft.world.Teleporter; import net.minecraft.world.World; import net.minecraft.world.WorldProvider; import net.minecraft.world.WorldServer; import net.minecraftforge.common.DimensionManager; import mcp.mobius.mobiuscore.profiler.ProfilerSection; import mcp.mobius.opis.data.holders.basetypes.AmountHolder; import mcp.mobius.opis.data.holders.basetypes.CoordinatesBlock; import mcp.mobius.opis.data.holders.basetypes.CoordinatesChunk; import mcp.mobius.opis.data.holders.newtypes.DataEntity; import mcp.mobius.opis.data.holders.newtypes.DataEntityPerClass; import mcp.mobius.opis.data.holders.newtypes.DataTiming; import mcp.mobius.opis.data.profilers.ProfilerEntityUpdate; import mcp.mobius.opis.helpers.Teleport; public enum EntityManager { INSTANCE; public ArrayList getWorses(int amount){ ArrayList sorted = new ArrayList(); ArrayList topEntities = new ArrayList(); for (Entity entity : ((ProfilerEntityUpdate)ProfilerSection.ENTITY_UPDATETIME.getProfiler()).data.keySet()) sorted.add(new DataEntity().fill(entity)); Collections.sort(sorted); int i = 0; while (topEntities.size() < Math.min(amount, sorted.size()) && (i < sorted.size()) ){ DataEntity testats = sorted.get(i); if (testats.npoints < 40){ i++; continue; } if (DimensionManager.getWorld(testats.pos.dim) == null) { i++; continue; } topEntities.add(testats); i++; } return topEntities; } /* Returns all the entities in all dimensions (without timing data) */ public ArrayList getAllEntities(){ ArrayList entities = new ArrayList(); for (int i : DimensionManager.getIDs()){ entities.addAll(this.getEntitiesInDim(i)); } return entities; } /* Returns all the entities in the given dimension (without timing data) */ public ArrayList getEntitiesInDim(int dim){ ArrayList entities = new ArrayList(); World world = DimensionManager.getWorld(dim); if (world == null) return entities; ArrayList copyList = new ArrayList(world.loadedEntityList); for (int i = 0; i < copyList.size(); i++){ Entity ent = (Entity)copyList.get(i); entities.add(new DataEntity().fill(ent)); } return entities; } /* Returns a hashmap of all entities per chunk (not timing) */ public HashMap> getAllEntitiesPerChunk(){ HashMap> entities = new HashMap>(); for (int i : DimensionManager.getIDs()){ entities.putAll(this.getEntitiesPerChunkInDim(i)); } return entities; } /* Returns a hashmap of entities in the given dimension (not timing) */ public HashMap> getEntitiesPerChunkInDim(int dim){ HashMap> entities = new HashMap>(); World world = DimensionManager.getWorld(dim); if (world == null) return entities; ArrayList copyList = new ArrayList(world.loadedEntityList); for (int i = 0; i < copyList.size(); i++){ Entity ent = (Entity)copyList.get(i); CoordinatesChunk chunk = new CoordinatesBlock(ent.dimension, (int)ent.posX, (int)ent.posY, (int)ent.posZ).asCoordinatesChunk(); if (!entities.containsKey(chunk)) entities.put(chunk, new ArrayList()); //entities.get(chunk).add(new EntityStats(ent.entityId, ent.getClass().getName(), ent.dimension, ent.posX, ent.posY, ent.posZ)); entities.get(chunk).add(new DataEntity().fill(ent)); } return entities; } /* Returns an array of all entities in a given chunk */ public ArrayList getEntitiesInChunk(CoordinatesChunk coord){ ArrayList entities = new ArrayList(); World world = DimensionManager.getWorld(coord.dim); if (world == null) return entities; ArrayList copyList = new ArrayList(world.loadedEntityList); for (int i = 0; i < copyList.size(); i++){ Entity ent = (Entity)copyList.get(i); CoordinatesChunk chunk = new CoordinatesBlock(ent.dimension, (int)ent.posX, (int)ent.posY, (int)ent.posZ).asCoordinatesChunk(); if (chunk.equals(coord)) //entities.add(new EntityStats(ent.entityId, ent.getClass().getName(), ent.dimension, ent.posX, ent.posY, ent.posZ)); entities.add(new DataEntity().fill(ent)); } return entities; } /* Returns a hashmap with the entity name and amount of it on the server */ public ArrayList getCumulativeEntities(boolean filtered){ ArrayList cumData = new ArrayList(); HashMap entities = new HashMap(); for (int dim : DimensionManager.getIDs()){ World world = DimensionManager.getWorld(dim); if (world == null) continue; ArrayList copyList = new ArrayList(world.loadedEntityList); for (int i = 0; i < copyList.size(); i++){ Entity ent = (Entity)copyList.get(i); //String name = ent.getClass().getName(); String name = getEntityName(ent, filtered); if (!entities.containsKey(name)) entities.put(name, 0); entities.put(name, entities.get(name) + 1); } } for (String key : entities.keySet()) cumData.add(new AmountHolder(key, entities.get(key))); return cumData; } public boolean teleportPlayer(CoordinatesBlock coord, EntityPlayerMP player){ //System.out.printf("%s %s\n", coord, getTeleportTarget(coord)); CoordinatesBlock target = Teleport.instance().getTeleportTarget(coord); if (target == null) return false; target = Teleport.instance().fixNetherTP(target); if (target == null) return false; if (Teleport.instance().movePlayerToDimension(player, coord.dim)) player.setPositionAndUpdate(target.x + 0.5, target.y, target.z + 0.5); else return false; return true; } public boolean teleportEntity(Entity src, Entity trg, Player msgtrg){ if ((src == null) && (msgtrg != null)) { PacketDispatcher.sendPacketToPlayer(new Packet3Chat(ChatMessageComponent.createFromText(String.format("\u00A7oCannot find source entity %s", src))), (Player)msgtrg); return false; } if ((trg == null) && (msgtrg != null)) { PacketDispatcher.sendPacketToPlayer(new Packet3Chat(ChatMessageComponent.createFromText(String.format("\u00A7oCannot find target entity %s", trg))), (Player)msgtrg); return false; } if (src instanceof EntityPlayerMP){ if (Teleport.instance().movePlayerToDimension((EntityPlayerMP)src, trg.worldObj.provider.dimensionId)) src.setLocationAndAngles(trg.posX, trg.posY, trg.posZ, src.rotationYaw, src.rotationPitch); else return false; } else{ if (Teleport.instance().moveEntityToDimension(src, trg.worldObj.provider.dimensionId)) src.setLocationAndAngles(trg.posX, trg.posY, trg.posZ, src.rotationYaw, src.rotationPitch); else return false; } return true; } public Entity getEntity(int eid, int dim){ World world = DimensionManager.getWorld(dim); if (world == null) return null; Entity entity = world.getEntityByID(eid); return entity; } public String getEntityName(Entity ent){ return getEntityName(ent, false); } public String getEntityName(Entity ent, boolean filtered){ if (ent instanceof EntityItem && filtered){ return "Dropped Item"; } else if (ent instanceof EntityItem && !filtered){ try { return "[Stack] " + ((EntityItem)ent).getEntityItem().getDisplayName(); } catch (Exception e) { return ""; } } if (ent instanceof EntityPlayerMP && filtered) return "Player"; else if (ent instanceof EntityPlayerMP && !filtered) return "[ Player ] " + ((EntityPlayerMP)ent).getDisplayName(); String name = ent.getTranslatedEntityName(); if (name.contains(".")){ String[] namelst = ent.getClass().getName().split("\\."); return namelst[namelst.length - 1]; } return name; } public DataTiming getTotalUpdateTime(){ double updateTime = 0D; for (Entity entity : ((ProfilerEntityUpdate)ProfilerSection.ENTITY_UPDATETIME.getProfiler()).data.keySet()){ updateTime += ((ProfilerEntityUpdate)ProfilerSection.ENTITY_UPDATETIME.getProfiler()).data.get(entity).getGeometricMean(); } return new DataTiming(updateTime); } public int killAll(String entName){ int nkilled = 0; if (entName.contains("Player")){ return -1; //Error msg for when trying to kill a player } for (int dim : DimensionManager.getIDs()){ World world = DimensionManager.getWorld(dim); if (world == null) continue; ArrayList copyList = new ArrayList(world.loadedEntityList); for (Object o : copyList){ Entity ent = (Entity)o; String nameFiltered = this.getEntityName(ent, true).toLowerCase(); String nameUnfiltered = this.getEntityName(ent, false).toLowerCase(); if (nameFiltered.equals(entName.toLowerCase()) || nameUnfiltered.equals(entName.toLowerCase())){ ent.setDead(); nkilled += 1; } } } System.out.printf("Killed %d %s\n", nkilled, entName); return nkilled; } public ArrayList getAllPlayers(){ List players = MinecraftServer.getServerConfigurationManager(MinecraftServer.getServer()).playerEntityList; ArrayList outList = new ArrayList(); for (Object p : players) outList.add(new DataEntity().fill((EntityPlayer) p)); return outList; } public int getAmountEntities(){ int amountEntities = 0; for (WorldServer world : DimensionManager.getWorlds()){ amountEntities += world.loadedEntityList.size(); } return amountEntities; } public int killAllPerClass(int dim, Class clazz){ WorldServer world = DimensionManager.getWorld(dim); if (world == null) return -1; int killedEnts = 0; ArrayList copyList = new ArrayList(world.loadedEntityList); for (Entity entity : (ArrayList)copyList){ if (clazz.isInstance(entity)){ entity.setDead(); killedEnts += 1; } } return killedEnts; } public ArrayList getTotalPerClass(){ HashMap data = new HashMap(); for (Entity entity : ((ProfilerEntityUpdate)ProfilerSection.ENTITY_UPDATETIME.getProfiler()).data.keySet()){ String name = this.getEntityName(entity, true); if (!data.containsKey(name)) data.put(name, new DataEntityPerClass(name)); data.get(name).add(((ProfilerEntityUpdate)ProfilerSection.ENTITY_UPDATETIME.getProfiler()).data.get(entity).getGeometricMean()); } return new ArrayList(data.values()); } }