package protocolsupport.utils;

import java.util.ArrayList;
import java.util.Arrays;

public class MapTransformer {

	private int[] colors = new int[16384];

	{
		Arrays.fill(colors, 1337);
	}

	public void loadFromNewMapData(int columns, int rows, int xstart, int ystart, byte[] data) {
		for (int column = 0; column < columns; ++column) {
			for (int row = 0; row < rows; ++row) {
				colors[xstart + column + (ystart + row) * 128] = data[column + row * columns];
			}
		}
	}

	public ArrayList<ColumnEntry> transformToOldMapData() {
		ArrayList<ColumnEntry> entries = new ArrayList<ColumnEntry>();
		for (int column = 0; column < 128; column++) {
			ColumnEntry entry = new ColumnEntry();
			entry.x = column;
			boolean foundstart = false;
			for (int row = 0; row < 128; row++) {
				int value = colors[row * 128 + column];
				if (value != 1337) {
					if (!foundstart) {
						foundstart = true;
						entry.y = row;
						entry.colors = new byte[128 - row];
					}
					entry.colors[row - entry.y] = (byte) value;
				} else if (foundstart) {
					byte[] colors = new byte[row - entry.y];
					System.arraycopy(entry.colors, 0, colors, 0, colors.length);
					entry.colors = colors;
					break;
				}
			}
			if (entry.colors != null) {
				entries.add(entry);
			}
		}
		return entries;
	}

	public static class ColumnEntry {

		private int x = -1;
		private int y = -1;

		private byte[] colors;

		public int getX() {
			return x;
		}

		public int getY() {
			return y;
		}

		public byte[] getColors() {
			return colors;
		}

	}

}

@@ -56,33 +56,8 @@
 ist;
-%0Aimport java.util.Arrays;
 %0A%0Apu
@@ -94,19 +94,20 @@
 private 
-int
+byte
 %5B%5D color
@@ -114,19 +114,20 @@
 s = new 
-int
+byte
 %5B16384%5D;
@@ -133,41 +133,101 @@
 ;%0A%0A%09
-%7B%0A%09%09Arrays.fill(colors, 1337);%0A%09%7D
+private int columnStart;%0A%09private int columnEnd;%0A%0A%09private int rowStart;%0A%09private int rowEnd;
 %0A%0A%09p
@@ -505,16 +505,119 @@
 %09%09%7D%0A%09%09%7D%0A
+%09%09columnStart = xstart;%0A%09%09columnEnd = xstart + columns;%0A%09%09rowStart = ystart;%0A%09%09rowEnd = ystart + rows;%0A
 %09%7D%0A%0A%09pub
@@ -746,33 +746,43 @@
 r (int column = 
-0
+columnStart
 ; column %3C 128; 
@@ -776,19 +776,25 @@
 olumn %3C 
-128
+columnEnd
 ; column
@@ -842,61 +842,44 @@
 try(
-);%0A%09%09%09entry.x = column;%0A%09%09%09boolean foundstart = false
+column, rowStart, rowEnd - rowStart)
 ;%0A%09%09
@@ -894,25 +894,32 @@
 t row = 
-0
+rowStart
 ; row %3C 
 128; row
@@ -910,19 +910,22 @@
 ; row %3C 
-128
+rowEnd
 ; row++)
@@ -935,17 +935,36 @@
 %09%09%09%09
-int value
+entry.colors%5Brow - rowStart%5D
  = c
@@ -997,410 +997,187 @@
 %0A%09%09%09
-%09if (value != 1337) %7B%0A%09%09%09%09%09if (!foundstart) %7B%0A%09%09%09%09%09%09foundstart = true;%0A%09%09%09%09%09%09entry.y = row;%0A%09%09%09%09%09%09entry.colors = new byte%5B128 - row%5D;%0A%09%09%09%09%09%7D%0A%09%09%09%09%09entry.colors%5Brow - entry.y%5D = (byte) value;%0A%09%09%09%09%7D else if (foundstart)
+%7D%0A%09%09%09entries.add(entry);%0A%09%09%7D%0A%09%09return entries;%0A%09%7D%0A%0A%09public static class ColumnEntry
  %7B%0A
+%0A
 %09%09
-%09%09%09byte%5B%5D colors = new byte%5Brow - entry.y%5D;%0A%09%09%09%09%09System.arraycopy(entry.colors, 0, colors, 0, colors.length);%0A%09%09%09%09%09entry.colors = colors
+public ColumnEntry(int x, int y, int rows) %7B%0A%09%09%09this.x = x
 ;%0A%09%09%09
-%09%09break;%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09if (entry
+this.y = y;%0A%09%09%09this
 .colors 
 != n
@@ -1176,108 +1176,29 @@
 ors 
-!
 = n
-ull) %7B%0A%09%09%09%09entries.add(entry);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return entries;%0A%09%7D%0A%0A%09public static class ColumnEntry %7B
+ew byte%5Brows%5D;%0A%09%09%7D
 %0A%0A%09%09
@@ -1210,21 +1210,16 @@
 te int x
- = -1
 ;%0A%09%09priv
@@ -1231,13 +1231,8 @@
 nt y
- = -1
 ;%0A%0A%09

Optimize map transformer