3
0

Use iterator for entities loop

This commit is contained in:
Prototik
2015-08-29 17:37:03 +07:00
parent 4a63dcacca
commit 9ba84893f3
9 changed files with 439 additions and 110 deletions

View File

@ -0,0 +1,77 @@
package kcauldron.wrapper;
import java.util.Collection;
import java.util.Iterator;
public class CollectionWrapper<T, C extends Collection<T>> implements Collection<T> {
private C mCollection;
public CollectionWrapper(C collection) {
mCollection = collection;
}
@Override
public boolean add(T e) {
return mCollection.add(e);
}
@Override
public boolean addAll(Collection<? extends T> c) {
return mCollection.addAll(c);
}
@Override
public void clear() {
mCollection.clear();
}
@Override
public boolean contains(Object o) {
return mCollection.contains(o);
}
@Override
public boolean containsAll(Collection<?> c) {
return mCollection.containsAll(c);
}
@Override
public boolean isEmpty() {
return mCollection.isEmpty();
}
@Override
public Iterator<T> iterator() {
return mCollection.iterator();
}
@Override
public boolean remove(Object o) {
return mCollection.remove(o);
}
@Override
public boolean removeAll(Collection<?> c) {
return mCollection.removeAll(c);
}
@Override
public boolean retainAll(Collection<?> c) {
return mCollection.retainAll(c);
}
@Override
public int size() {
return mCollection.size();
}
@Override
public Object[] toArray() {
return mCollection.toArray();
}
@Override
public <T> T[] toArray(T[] a) {
return mCollection.toArray(a);
}
}

View File

@ -0,0 +1,144 @@
package kcauldron.wrapper;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class LinkedHelper<T> {
private Iterable<T> mIterable;
private Iterator<T> mIterator;
private int iteratorIndex;
private T indexValue;
public LinkedHelper(Iterable<T> iterable) {
mIterable = iterable;
}
public T get(int index) {
if (iteratorIndex > index || mIterator == null) {
mIterator = mIterable.iterator();
iteratorIndex = -1;
}
while (iteratorIndex < index) {
if (mIterator.hasNext()) {
indexValue = mIterator.next();
iteratorIndex++;
} else {
throw new IndexOutOfBoundsException();
}
}
return indexValue;
}
public int indexOf(Object o) {
int i = 0;
for (T value : mIterable) {
if (o == null ? value == null : o.equals(value))
return i;
i++;
}
return -1;
}
public int lastIndexOf(Object o) {
if (!(mIterable instanceof List)) {
throw new UnsupportedOperationException();
}
ListIterator<T> iterator = ((List<T>) mIterable).listIterator();
int i = 0;
while (iterator.hasPrevious()) {
T value = iterator.previous();
if (o == null ? value == null : o.equals(value))
return i;
i++;
}
return -1;
}
public ListIterator<T> listIterator() {
if (mIterable instanceof List) {
return ((List<T>) mIterable).listIterator();
}
return new LinkedListIterator<T>(mIterable.iterator(), 0);
}
public ListIterator<T> listIterator(int index) {
if (mIterable instanceof List) {
return ((List<T>) mIterable).listIterator(index);
}
Iterator<T> iterator = mIterable.iterator();
for (int i = 0; i < index; i++)
iterator.next();
return new LinkedListIterator<T>(iterator, index);
}
private static final class LinkedListIterator<T> implements ListIterator<T> {
private final Iterator<T> mIterator;
private final ListIterator<T> mListIterator;
private int mNextIndex;
public LinkedListIterator(Iterator<T> iterator, int index) {
mIterator = iterator;
mListIterator = iterator instanceof ListIterator ? (ListIterator<T>) iterator : null;
mNextIndex = index;
}
private void ensureListIterator() {
if (mListIterator == null)
throw new UnsupportedOperationException();
}
@Override
public void add(T e) {
ensureListIterator();
mListIterator.add(e);
}
@Override
public boolean hasNext() {
return mIterator.hasNext();
}
@Override
public boolean hasPrevious() {
ensureListIterator();
return mListIterator.hasPrevious();
}
@Override
public T next() {
mNextIndex++;
return mIterator.next();
}
@Override
public int nextIndex() {
return mNextIndex;
}
@Override
public T previous() {
ensureListIterator();
return mListIterator.previous();
}
@Override
public int previousIndex() {
ensureListIterator();
return mListIterator.previousIndex();
}
@Override
public void remove() {
ensureListIterator();
mListIterator.remove();
}
@Override
public void set(T e) {
ensureListIterator();
mListIterator.set(e);
}
}
}

View File

@ -0,0 +1,68 @@
package kcauldron.wrapper;
import java.util.Collection;
import java.util.List;
import java.util.ListIterator;
import java.util.Queue;
public class QueueToList<T> extends CollectionWrapper<T, Queue<T>>implements List<T> {
private final Queue<T> mQueue;
private final LinkedHelper<T> mHelper;
public QueueToList(Queue<T> queue) {
super(queue);
mQueue = queue;
mHelper = new LinkedHelper<T>(mQueue);
}
@Override
public void add(int index, T element) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(int index, Collection<? extends T> c) {
throw new UnsupportedOperationException();
}
@Override
public T get(int index) {
return mHelper.get(index);
}
@Override
public int indexOf(Object o) {
return mHelper.indexOf(o);
}
@Override
public int lastIndexOf(Object o) {
return mHelper.lastIndexOf(o);
}
@Override
public ListIterator<T> listIterator() {
return mHelper.listIterator();
}
@Override
public ListIterator<T> listIterator(int index) {
return mHelper.listIterator(index);
}
@Override
public T remove(int index) {
T object = mHelper.get(index);
return mQueue.remove(object) ? object : null;
}
@Override
public T set(int index, T element) {
throw new UnsupportedOperationException();
}
@Override
public List<T> subList(int fromIndex, int toIndex) {
throw new UnsupportedOperationException();
}
}

View File

@ -334,16 +334,15 @@ public class CauldronHooks
writer.name("players").value(world.playerEntities.size());
writer.name("loadedChunks").value(world.theChunkProviderServer.loadedChunkHashMap_KC.size());
writer.name("activeChunks").value(world.activeChunkSet.size());
writer.name("entities").value(world.loadedEntityList.size());
writer.name("entities").value(world.loadedEntityList_KC.size());
writer.name("tiles").value(world.loadedTileEntityList.size());
TObjectIntHashMap<ChunkCoordIntPair> chunkEntityCounts = new TObjectIntHashMap<ChunkCoordIntPair>();
TObjectIntHashMap<Class> classEntityCounts = new TObjectIntHashMap<Class>();
TObjectIntHashMap<Entity> entityCollisionCounts = new TObjectIntHashMap<Entity>();
Set<ChunkCoordinates> collidingCoords = new HashSet<ChunkCoordinates>();
for (int i = 0; i < world.loadedEntityList.size(); i++)
for (Entity entity : world.loadedEntityList_KC)
{
Entity entity = (Entity) world.loadedEntityList.get(i);
ChunkCoordIntPair chunkCoords = new ChunkCoordIntPair((int) entity.posX >> 4, (int) entity.posZ >> 4);
chunkEntityCounts.adjustOrPutValue(chunkCoords, 1, 1);
classEntityCounts.adjustOrPutValue(entity.getClass(), 1, 1);

View File

@ -114,7 +114,7 @@ public class CauldronCommand extends Command
sender.sendMessage(ChatColor.GOLD + "Dimension: " + ChatColor.GRAY + world.provider.dimensionId +
ChatColor.GOLD + " Loaded Chunks: " + ChatColor.GRAY + world.theChunkProviderServer.loadedChunkHashMap_KC.size() +
ChatColor.GOLD + " Active Chunks: " + ChatColor.GRAY + world.activeChunkSet.size() +
ChatColor.GOLD + " Entities: " + ChatColor.GRAY + world.loadedEntityList.size() +
ChatColor.GOLD + " Entities: " + ChatColor.GRAY + world.loadedEntityList_KC.size() +
ChatColor.GOLD + " Tile Entities: " + ChatColor.GRAY + world.loadedTileEntityList.size()
);
sender.sendMessage(ChatColor.GOLD + " Entities Last Tick: " + ChatColor.GRAY + world.entitiesTicked +

View File

@ -683,16 +683,16 @@ public class CraftWorld implements World {
public List<Entity> getEntities() {
List<Entity> list = new ArrayList<Entity>();
for (Object o : world.loadedEntityList) {
if (o instanceof net.minecraft.entity.Entity) {
net.minecraft.entity.Entity mcEnt = (net.minecraft.entity.Entity) o;
for (net.minecraft.entity.Entity mcEnt : world.loadedEntityList_KC) {
//if (o instanceof net.minecraft.entity.Entity) {
//net.minecraft.entity.Entity mcEnt = (net.minecraft.entity.Entity) o;
Entity bukkitEntity = mcEnt.getBukkitEntity();
// Assuming that bukkitEntity isn't null
if (bukkitEntity != null) {
list.add(bukkitEntity);
}
}
//}
}
return list;
@ -701,16 +701,16 @@ public class CraftWorld implements World {
public List<LivingEntity> getLivingEntities() {
List<LivingEntity> list = new ArrayList<LivingEntity>();
for (Object o : world.loadedEntityList) {
if (o instanceof net.minecraft.entity.Entity) {
net.minecraft.entity.Entity mcEnt = (net.minecraft.entity.Entity) o;
for (net.minecraft.entity.Entity mcEnt : world.loadedEntityList_KC) {
//if (o instanceof net.minecraft.entity.Entity) {
//net.minecraft.entity.Entity mcEnt = (net.minecraft.entity.Entity) o;
Entity bukkitEntity = mcEnt.getBukkitEntity();
// Assuming that bukkitEntity isn't null
if (bukkitEntity != null && bukkitEntity instanceof LivingEntity) {
list.add((LivingEntity) bukkitEntity);
}
}
//}
}
return list;
@ -726,9 +726,9 @@ public class CraftWorld implements World {
public <T extends Entity> Collection<T> getEntitiesByClass(Class<T> clazz) {
Collection<T> list = new ArrayList<T>();
for (Object entity: world.loadedEntityList) {
if (entity instanceof net.minecraft.entity.Entity) {
Entity bukkitEntity = ((net.minecraft.entity.Entity) entity).getBukkitEntity();
for (net.minecraft.entity.Entity entity : world.loadedEntityList_KC) {
//if (entity instanceof net.minecraft.entity.Entity) {
Entity bukkitEntity = entity.getBukkitEntity();
if (bukkitEntity == null) {
continue;
@ -739,7 +739,7 @@ public class CraftWorld implements World {
if (clazz.isAssignableFrom(bukkitClass)) {
list.add((T) bukkitEntity);
}
}
//}
}
return list;
@ -748,9 +748,9 @@ public class CraftWorld implements World {
public Collection<Entity> getEntitiesByClasses(Class<?>... classes) {
Collection<Entity> list = new ArrayList<Entity>();
for (Object entity: world.loadedEntityList) {
if (entity instanceof net.minecraft.entity.Entity) {
Entity bukkitEntity = ((net.minecraft.entity.Entity) entity).getBukkitEntity();
for (net.minecraft.entity.Entity entity: world.loadedEntityList_KC) {
//if (entity instanceof net.minecraft.entity.Entity) {
Entity bukkitEntity = entity.getBukkitEntity();
if (bukkitEntity == null) {
continue;
@ -764,7 +764,7 @@ public class CraftWorld implements World {
break;
}
}
}
//}
}
return list;
@ -773,15 +773,15 @@ public class CraftWorld implements World {
public List<Player> getPlayers() {
List<Player> list = new ArrayList<Player>();
for (Object o : world.loadedEntityList) {
if (o instanceof net.minecraft.entity.Entity) {
net.minecraft.entity.Entity mcEnt = (net.minecraft.entity.Entity) o;
for (net.minecraft.entity.Entity mcEnt : world.loadedEntityList_KC) {
//if (o instanceof net.minecraft.entity.Entity) {
//net.minecraft.entity.Entity mcEnt = (net.minecraft.entity.Entity) o;
Entity bukkitEntity = mcEnt.getBukkitEntity();
if ((bukkitEntity != null) && (bukkitEntity instanceof Player)) {
list.add((Player) bukkitEntity);
}
}
//}
}
return list;

View File

@ -90,7 +90,7 @@ public class WatchdogThread extends Thread
log.log(Level.SEVERE, " Dimension:" + world.provider.dimensionId);
log.log(Level.SEVERE,
" Loaded Chunks: " + world.theChunkProviderServer.loadedChunkHashMap_KC.size() + " Active Chunks: " + world.activeChunkSet.size()
+ " Entities: " + world.loadedEntityList.size() + " Tile Entities: " + world.loadedTileEntityList.size());
+ " Entities: " + world.loadedEntityList_KC.size() + " Tile Entities: " + world.loadedTileEntityList.size());
log.log(Level.SEVERE, " Entities Last Tick: " + world.entitiesTicked);
log.log(Level.SEVERE, " Tiles Last Tick: " + world.tilesTicked);
}
@ -155,7 +155,7 @@ public class WatchdogThread extends Thread
log.log(Level.WARNING, " Dimension:" + world.provider.dimensionId);
log.log(Level.WARNING, " Loaded Chunks: " + world.theChunkProviderServer.loadedChunkHashMap_KC.size() +
" Active Chunks: " + world.activeChunkSet.size() +
" Entities: " + world.loadedEntityList.size() +
" Entities: " + world.loadedEntityList_KC.size() +
" Tile Entities: " + world.loadedTileEntityList.size());
log.log(Level.WARNING, " Entities Last Tick: " + world.entitiesTicked);
log.log(Level.WARNING, " Tiles Last Tick: " + world.tilesTicked);