package com.ryandw11.structure.utils;

import java.util.Random;

import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;

public class HandleY {
	
	/**
	 * Calculate the y value for a structure.
	 * @param cs The configurationsection of the schematic
	 * @param height The current height at that area.
	 * @return the height. (Will never be null)
	 */
	public static int calculateY(ConfigurationSection cs, int height) {
		String value = cs.getString("SpawnY");
		
		if(value.equalsIgnoreCase("top")) {
			return height;
		}
		
		if(value.contains("[")) {
			//If +[num-num]
			if(value.startsWith("+")) {
				String v = value.replace("[", "").replace("]", "").replace("+", "");
				String[] out = v.split("-");
				try {
					int num1 = Integer.parseInt(out[0]);
					int num2 = Integer.parseInt(out[1]);
					
					Random r = new Random();
					
					int a = r.nextInt(num2) + (num1 + 1);
					return height + a;
					
				}catch(NumberFormatException | ArrayIndexOutOfBoundsException ex) {
					return height;
				}
			}
			// if -[num-num]
			else if(value.startsWith("-")) {
				String v = value.replace("[", "").replace("]", "").replace("-", "");
				String[] out = v.split("-");
				try {
					int num1 = Integer.parseInt(out[0]);
					int num2 = Integer.parseInt(out[1]);
					
					Random r = new Random();
					
					int a = r.nextInt(num2) + (num1 + 1);
					return height - a;
					
				}catch(NumberFormatException | ArrayIndexOutOfBoundsException ex) {
					return height;
				}
			}
			// if just [num-num]
			else {
				String v = value.replace("[", "").replace("]", "");
				String[] out = v.split("-");
				try {
					int num1 = Integer.parseInt(out[0]);
					int num2 = Integer.parseInt(out[1]);
					
					Random r = new Random();
					
					int a = r.nextInt(num2 + 1) + num1;
					return a;
					
				}catch(NumberFormatException | ArrayIndexOutOfBoundsException ex) {
					return height;
				}
			}
		}
		// if +num
		else if(value.startsWith("+")) {
			String v = value.replace("+", "");
			try {
				int num = Integer.parseInt(v);
				return height + num;
			}catch(NumberFormatException ex) {
				return height;
			}
		}
		// if -num
		else if(value.startsWith("-")) {
			String v = value.replace("-", "");
			
			try {
				int num = Integer.parseInt(v);
				return height - num;
			}catch(NumberFormatException ex) {
				return height;
			}
		}
		// if just num
		else {
			try {
				return Integer.parseInt(value);
			} catch(NumberFormatException ex) {
				return height;
			}
		}
	}

}

@@ -56,16 +56,63 @@
 .Random;
+%0Aimport java.util.concurrent.ThreadLocalRandom;
 %0A%0Aimport
@@ -1782,32 +1782,34 @@
 1%5D);%0A%09%09%09%09%09%0A%09%09%09%09%09
+//
 Random r = new R
@@ -1818,37 +1818,32 @@
 dom();%0A%09%09%09%09%09
-%0A%09%09%09%09%09int a =
+//return
  r.nextInt(n
@@ -1859,16 +1859,17 @@
 + num1;%0A
+%0A
 %09%09%09%09%09ret
@@ -1872,17 +1872,67 @@
 %09return 
-a
+ThreadLocalRandom.current().nextInt(num1, num2 + 1)
 ;%0A%09%09%09%09%09%0A

Fix structures attempting to spawn above y=255