From b2e14cb247eb2a8c79ff1e56f932ca6e713e9dfd Mon Sep 17 00:00:00 2001 From: Prototik Date: Sun, 6 Sep 2015 18:54:03 +0700 Subject: [PATCH] Return loop iterating over entites on overloaded server --- .../network/NetHandlerPlayServer.java.patch | 3 +- patches/net/minecraft/world/World.java.patch | 8 +- .../kcauldron/wrapper/CollectionWrapper.java | 8 +- .../kcauldron/wrapper/ProcessingQueue.java | 132 ++++++++++++++++++ .../java/kcauldron/wrapper/QueueToList.java | 2 +- .../java/kcauldron/wrapper/QueueWrapper.java | 34 +++++ 6 files changed, 178 insertions(+), 9 deletions(-) create mode 100644 src/main/java/kcauldron/wrapper/ProcessingQueue.java create mode 100644 src/main/java/kcauldron/wrapper/QueueWrapper.java diff --git a/patches/net/minecraft/network/NetHandlerPlayServer.java.patch b/patches/net/minecraft/network/NetHandlerPlayServer.java.patch index 2e44db9..0d71fbc 100644 --- a/patches/net/minecraft/network/NetHandlerPlayServer.java.patch +++ b/patches/net/minecraft/network/NetHandlerPlayServer.java.patch @@ -321,8 +321,7 @@ - if (this.playerEntity.isPlayerSleeping()) + /*if (this.playerEntity.isPlayerSleeping()) // KCauldron - moved up { -- this.playerEntity.onUpdateEntity(); -+ this.playerEntity.onUpdateEntity(); + this.playerEntity.onUpdateEntity(); this.playerEntity.setPositionAndRotation(this.lastPosX, this.lastPosY, this.lastPosZ, this.playerEntity.rotationYaw, this.playerEntity.rotationPitch); worldserver.updateEntity(this.playerEntity); return; diff --git a/patches/net/minecraft/world/World.java.patch b/patches/net/minecraft/world/World.java.patch index 8b858d4..646d5b9 100644 --- a/patches/net/minecraft/world/World.java.patch +++ b/patches/net/minecraft/world/World.java.patch @@ -107,11 +107,11 @@ - public List loadedTileEntityList = new ArrayList(); - private List addedTileEntityList = new ArrayList(); - private List field_147483_b = new ArrayList(); -+ public Queue loadedEntityList_KC = new ConcurrentLinkedQueue(); // KCauldron -+ public List loadedEntityList = new kcauldron.wrapper.QueueToList(loadedEntityList_KC); // KCauldron ++ public Queue loadedEntityList_KC = new kcauldron.wrapper.ProcessingQueue(new ConcurrentLinkedQueue()); // KCauldron ++ public List loadedEntityList = new kcauldron.wrapper.QueueToList(((kcauldron.wrapper.ProcessingQueue)loadedEntityList_KC).unwrap()); // KCauldron + public List unloadedEntityList = com.google.common.collect.ImmutableList.of(); // KCauldron -+ public Queue loadedTileEntityList_KC = new ConcurrentLinkedQueue(); // KCauldron -+ public List loadedTileEntityList = new kcauldron.wrapper.QueueToList(loadedTileEntityList_KC); // KCauldron ++ public Queue loadedTileEntityList_KC = new kcauldron.wrapper.ProcessingQueue(new ConcurrentLinkedQueue()); // KCauldron ++ public List loadedTileEntityList = new kcauldron.wrapper.QueueToList(((kcauldron.wrapper.ProcessingQueue)loadedTileEntityList_KC).unwrap()); // KCauldron + private List addedTileEntityList = new ArrayList(); // KCauldron + public List field_147483_b = com.google.common.collect.ImmutableList.of(); // KCauldron public List playerEntities = new ArrayList(); diff --git a/src/main/java/kcauldron/wrapper/CollectionWrapper.java b/src/main/java/kcauldron/wrapper/CollectionWrapper.java index b42d408..17a1cf3 100644 --- a/src/main/java/kcauldron/wrapper/CollectionWrapper.java +++ b/src/main/java/kcauldron/wrapper/CollectionWrapper.java @@ -4,7 +4,7 @@ import java.util.Collection; import java.util.Iterator; public class CollectionWrapper> implements Collection { - private C mCollection; + protected C mCollection; public CollectionWrapper(C collection) { mCollection = collection; @@ -22,7 +22,7 @@ public class CollectionWrapper> implements Collection @Override public void clear() { - mCollection.clear(); + mCollection.clear(); } @Override @@ -74,4 +74,8 @@ public class CollectionWrapper> implements Collection public T[] toArray(T[] a) { return mCollection.toArray(a); } + + public C unwrap() { + return mCollection; + } } diff --git a/src/main/java/kcauldron/wrapper/ProcessingQueue.java b/src/main/java/kcauldron/wrapper/ProcessingQueue.java new file mode 100644 index 0000000..0c8fe3e --- /dev/null +++ b/src/main/java/kcauldron/wrapper/ProcessingQueue.java @@ -0,0 +1,132 @@ +package kcauldron.wrapper; + +import java.util.Collection; +import java.util.Iterator; +import java.util.Queue; +import java.util.concurrent.atomic.AtomicInteger; + +public class ProcessingQueue extends QueueWrapper { + public class ProcessingIterator implements Iterator { + private Iterator mRealIterator = mCollection.iterator(); + private AtomicInteger mPosition = new AtomicInteger(0); + + @Override + public boolean hasNext() { + final int size = mSize.get(); + final int position = mPosition.get(); + if (size == 0 || position >= size) { + mPosition.compareAndSet(position, 0); + mRealIterator = mCollection.iterator(); + return false; + } + boolean value = mRealIterator.hasNext(); + if (!value) { + mRealIterator = mCollection.iterator(); + value = mRealIterator.hasNext(); + } + return value; + } + + @Override + public T next() { + T value = mRealIterator.next(); + mPosition.incrementAndGet(); + return value; + } + + @Override + public void remove() { + mRealIterator.remove(); + mSize.decrementAndGet(); + mPosition.decrementAndGet(); + } + + public void reset() { + mPosition.set(0); + } + } + + private final AtomicInteger mSize; + private final ProcessingIterator mIterator = new ProcessingIterator(); + + @Override + public boolean add(T e) { + boolean result = super.add(e); + if (result) { + mSize.incrementAndGet(); + } + return result; + }; + + @Override + public boolean offer(T e) { + boolean result = super.offer(e); + if (result) { + mSize.incrementAndGet(); + } + return result; + } + + @Override + public boolean addAll(Collection c) { + boolean result = false; + for (T t : c) { + result |= add(t); + } + return result; + } + + @Override + public T remove() { + T result = super.remove(); + mSize.decrementAndGet(); + return result; + } + + @Override + public boolean remove(Object o) { + boolean result = super.remove(o); + if (result) { + mSize.decrementAndGet(); + } + return result; + } + + @Override + public boolean removeAll(Collection c) { + boolean result = false; + for (Object t : c) { + result |= remove(t); + } + return result; + } + + @Override + public boolean retainAll(Collection c) { + boolean result = super.retainAll(c); + if (result) { + mSize.set(mCollection.size()); + } + return result; + } + + @Override + public T poll() { + T result = super.remove(); + if (result != null) { + mSize.decrementAndGet(); + } + return result; + } + + public ProcessingQueue(Queue collection) { + super(collection); + mSize = new AtomicInteger(collection.size()); + } + + @Override + public ProcessingIterator iterator() { + mIterator.reset(); + return mIterator; + } +} diff --git a/src/main/java/kcauldron/wrapper/QueueToList.java b/src/main/java/kcauldron/wrapper/QueueToList.java index 9c94358..6dd9332 100644 --- a/src/main/java/kcauldron/wrapper/QueueToList.java +++ b/src/main/java/kcauldron/wrapper/QueueToList.java @@ -5,7 +5,7 @@ import java.util.List; import java.util.ListIterator; import java.util.Queue; -public class QueueToList extends CollectionWrapper>implements List { +public class QueueToList extends CollectionWrapper> implements List { private final Queue mQueue; private final LinkedHelper mHelper; diff --git a/src/main/java/kcauldron/wrapper/QueueWrapper.java b/src/main/java/kcauldron/wrapper/QueueWrapper.java new file mode 100644 index 0000000..b65ef73 --- /dev/null +++ b/src/main/java/kcauldron/wrapper/QueueWrapper.java @@ -0,0 +1,34 @@ +package kcauldron.wrapper; + +import java.util.Queue; + +public class QueueWrapper extends CollectionWrapper> implements Queue{ + public QueueWrapper(Queue collection) { + super(collection); + } + + @Override + public T element() { + return mCollection.element(); + } + + @Override + public boolean offer(T e) { + return mCollection.offer(e); + } + + @Override + public T peek() { + return mCollection.peek(); + } + + @Override + public T poll() { + return mCollection.poll(); + } + + @Override + public T remove() { + return mCollection.remove(); + } +}