Files
Opis/mcp/mobius/opis/data/managers/EntityManager.java
T
ProfMobius 8469bd29bf Added a kill all stacks command to the purge buttons.
Added confirmation windows for purge commands.
Display itemstacks instead of total entities.
2014-04-04 20:41:17 +02:00

344 lines
11 KiB
Java

package mcp.mobius.opis.data.managers;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
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.DataTiming;
import mcp.mobius.opis.data.profilers.ProfilerEntityUpdate;
import mcp.mobius.opis.helpers.Teleport;
public enum EntityManager {
INSTANCE;
public ArrayList<DataEntity> getWorses(int amount){
ArrayList<DataEntity> sorted = new ArrayList<DataEntity>();
ArrayList<DataEntity> topEntities = new ArrayList<DataEntity>();
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<DataEntity> getAllEntities(){
ArrayList<DataEntity> entities = new ArrayList<DataEntity>();
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<DataEntity> getEntitiesInDim(int dim){
ArrayList<DataEntity> entities = new ArrayList<DataEntity>();
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<CoordinatesChunk, ArrayList<DataEntity>> getAllEntitiesPerChunk(){
HashMap<CoordinatesChunk, ArrayList<DataEntity>> entities = new HashMap<CoordinatesChunk, ArrayList<DataEntity>>();
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<CoordinatesChunk, ArrayList<DataEntity>> getEntitiesPerChunkInDim(int dim){
HashMap<CoordinatesChunk, ArrayList<DataEntity>> entities = new HashMap<CoordinatesChunk, ArrayList<DataEntity>>();
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<DataEntity>());
//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<DataEntity> getEntitiesInChunk(CoordinatesChunk coord){
ArrayList<DataEntity> entities = new ArrayList<DataEntity>();
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<AmountHolder> getCumulativeEntities(boolean filtered){
ArrayList<AmountHolder> cumData = new ArrayList<AmountHolder>();
HashMap<String, Integer> entities = new HashMap<String, Integer>();
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 "<Unknown dropped item>";
}
}
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<DataEntity> getAllPlayers(){
List players = MinecraftServer.getServerConfigurationManager(MinecraftServer.getServer()).playerEntityList;
ArrayList<DataEntity> outList = new ArrayList<DataEntity>();
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 killAllHostiles(int dim){
WorldServer world = DimensionManager.getWorld(dim);
if (world == null) return -1;
int killedEnts = 0;
for (Entity entity : (ArrayList<Entity>)world.loadedEntityList){
if (entity instanceof EntityMob){
entity.setDead();
killedEnts += 1;
}
}
return killedEnts;
}
public int killAllStacks(int dim){
WorldServer world = DimensionManager.getWorld(dim);
if (world == null) return -1;
int killedEnts = 0;
for (Entity entity : (ArrayList<Entity>)world.loadedEntityList){
if (entity instanceof EntityItem){
entity.setDead();
killedEnts += 1;
}
}
return killedEnts;
}
}