package me.nallar.patched.world.tracking;

import java.util.ArrayList;
import java.util.List;

import me.nallar.tickthreading.patcher.Declare;
import net.minecraft.server.management.PlayerInstance;
import net.minecraft.server.management.PlayerManager;
import net.minecraft.tileentity.TileEntity;

public abstract class PatchPlayerInstance extends PlayerInstance {
	private List<TileEntity> tilesToUpdate;
	private static java.lang.reflect.Method getChunkWatcherWithPlayers;

	public PatchPlayerInstance(PlayerManager par1PlayerManager, int par2, int par3) {
		super(par1PlayerManager, par2, par3);
	}

	public void construct() {
		tilesToUpdate = new ArrayList<TileEntity>();
	}

	public void sendTiles() {
		for (TileEntity tileEntity : tilesToUpdate) {
			sendTileToAllPlayersWatchingChunk(tileEntity);
		}
		tilesToUpdate.clear();
	}

	@Override
	@Declare
	public void updateTile(TileEntity tileEntity) {
		if (numberOfTilesToUpdate == 0) {
			this.myManager.playerUpdateLock.lock();
			try {
				this.myManager.getChunkWatcherWithPlayers().add(this);
			} finally {
				this.myManager.playerUpdateLock.unlock();
			}
			numberOfTilesToUpdate++;
		}
		if (!tilesToUpdate.contains(tileEntity)) {
			tilesToUpdate.add(tileEntity);
		}
	}
}

@@ -55,24 +55,22 @@
 va.util.
-ArrayLis
+HashSe
 t;%0D%0Aimpo
@@ -82,20 +82,48 @@
 va.util.
-List
+concurrent.ConcurrentLinkedQueue
 ;%0D%0A%0D%0Aimp
@@ -403,20 +403,37 @@
 private 
-List
+ConcurrentLinkedQueue
 %3CTileEnt
@@ -710,17 +710,29 @@
 new 
-ArrayList
+ConcurrentLinkedQueue
 %3CTil
@@ -778,16 +778,81 @@
 es() %7B%0D%0A
+%09%09HashSet%3CTileEntity%3E tileEntities = new HashSet%3CTileEntity%3E();%0D%0A
 %09%09for (T
@@ -876,9 +876,9 @@
 ity 
-:
+=
  til
@@ -887,24 +887,178 @@
 ToUpdate
-) %7B%0D%0A%09%09%09
+.poll(); tileEntity != null; tileEntity = tilesToUpdate.poll()) %7B%0D%0A%09%09%09tileEntities.add(tileEntity);%0D%0A%09%09%7D%0D%0A%09%09for (TileEntity tileEntity : tileEntities) %7B%0D%0A%09%09%09this.
 sendTile
@@ -1108,25 +1108,24 @@
 %0D%0A%09%09tile
-sToUpdate
+Entities
 .clear()
@@ -1464,55 +1464,8 @@
 %09%7D%0D%0A
-%09%09if (!tilesToUpdate.contains(tileEntity)) %7B%0D%0A%09
 %09%09ti
@@ -1494,20 +1494,15 @@
 tity);%0D%0A
-%09%09%7D%0D%0A
 %09%7D%0D%0A%7D%0D%0A

Fix comodification exception in sendTiles