package engine;

import java.awt.Graphics2D;

import _texture.loader.Textures;
import engine.Game.STATE;
import fileIO.Saving;
import fileIO.SettingsSaveFile;
import hud.constructionmenu.ConstructionMenu;
import hud.inventory.Inventory;
import utils.Stack;
import utils.Utils;
import worldGen.WorldGenerator;

/*  ------------- Nihilne ------------
    * Created by Mitch Weaver - 2016 *
      ~~~~ All Rights Reserved ~~~~ */

final class Settings {

	public boolean justCameFromPaused = false;

	public volatile Stack<MenuButton> mainButtons;
	public volatile Stack<MenuButton> resizeableContextMenu;
	public volatile Stack<MenuButton> displayFPSContextMenu;
	public volatile Stack<MenuButton> showEnemyHealthContextMenu;
	public volatile Stack<MenuButton> resolutionContextMenu;
	public volatile Stack<MenuButton> fpsContextMenu;

	// main buttons
	public static volatile MenuButton resolutionButton, resizeableButton, fpsButton, displayFPSButton, showHealthBarsButton, returnToMenuButton, saveButton;
	// True / False
	public static volatile MenuButton resizeableTrue, resizeableFalse, displayFPSTrue, displayFPSFalse, showHealthBarsTrue, showHealthBarsFalse;
	// Resolutions
	public static volatile MenuButton _fullscreen, _480x320, _640x480, _800x600, _1280x720, _1280x960, _1600x900, _1920x1080, _2560x1440, _3840x2160,
			_8192x4068;
	// FPS CAPS
	public static volatile MenuButton FPS30, FPS60, FPS90, FPS120, FPS144, FPS200, FPS_RIP_CPU;
	// booleans for button arrangement
	public static volatile boolean resizeableTrueFalseReverse, displayFPSTrueFalseReverse, showEnemyHealthBarsTrueFalseReverse;
	// count of all the buttons in all the arrays
	private static volatile int largestArraySize, max;

	// Save file to be loaded
	private static volatile SettingsSaveFile loadedSettings;

	public final synchronized void init() {
		try { /* attempt to load save and initialize variables to that save */
			loadedSettings = Saving.readSettingsSaveFile();

			mainButtons = loadedSettings.mainButtons;
			resizeableContextMenu = loadedSettings.resizeable;
			displayFPSContextMenu = loadedSettings.displayFPS;
			showEnemyHealthContextMenu = loadedSettings.healthBars;
			resolutionContextMenu = loadedSettings.resolution;
			fpsContextMenu = loadedSettings.fpsCap;

			// Load main buttons
			resolutionButton = mainButtons.get(0);
			resizeableButton = mainButtons.get(1);
			fpsButton = mainButtons.get(2);
			displayFPSButton = mainButtons.get(3);
			showHealthBarsButton = mainButtons.get(4);
			returnToMenuButton = mainButtons.get(5);
			saveButton = mainButtons.get(6);

			// Load True / False buttons
			resizeableTrue = resizeableContextMenu.get(0);
			resizeableFalse = resizeableContextMenu.get(1);
			displayFPSTrue = displayFPSContextMenu.get(0);
			displayFPSFalse = displayFPSContextMenu.get(1);
			showHealthBarsTrue = showEnemyHealthContextMenu.get(0);
			showHealthBarsFalse = showEnemyHealthContextMenu.get(1);

			// Load Resolution Buttons
			_fullscreen = resolutionContextMenu.get(0);
			_480x320 = resolutionContextMenu.get(1);
			_640x480 = resolutionContextMenu.get(2);
			_800x600 = resolutionContextMenu.get(3);
			_1280x720 = resolutionContextMenu.get(4);
			_1280x960 = resolutionContextMenu.get(5);
			_1600x900 = resolutionContextMenu.get(6);
			_1920x1080 = resolutionContextMenu.get(7);
			_2560x1440 = resolutionContextMenu.get(8);
			_3840x2160 = resolutionContextMenu.get(9);
			_8192x4068 = resolutionContextMenu.get(10);

			// Load FPS Cap buttons
			FPS30 = fpsContextMenu.get(0);
			FPS60 = fpsContextMenu.get(1);
			FPS90 = fpsContextMenu.get(2);
			FPS120 = fpsContextMenu.get(3);
			FPS144 = fpsContextMenu.get(4);
			FPS200 = fpsContextMenu.get(5);
			FPS_RIP_CPU = fpsContextMenu.get(6);

			// Load booleans
			resizeableTrueFalseReverse = loadedSettings.resizeableTF;
			displayFPSTrueFalseReverse = loadedSettings.displayFPSTF;
			showEnemyHealthBarsTrueFalseReverse = loadedSettings.showHealthTF;

			// Apply the loaded settings:
		} catch (Exception e) {
			System.err.println("WARNING: Save file either not found or corrupt.");
			System.out.println("--------  Creating new save file    --------");

			// initialize array lists:
			mainButtons = new Stack<MenuButton>();
			resizeableContextMenu = new Stack<MenuButton>();
			displayFPSContextMenu = new Stack<MenuButton>();
			showEnemyHealthContextMenu = new Stack<MenuButton>();
			resolutionContextMenu = new Stack<MenuButton>();
			fpsContextMenu = new Stack<MenuButton>();

			// initialize booleans
			resizeableTrueFalseReverse = false;
			displayFPSTrueFalseReverse = false;
			showEnemyHealthBarsTrueFalseReverse = false;

			// PARAMETERS: x, y, width, height, type, isHovering /* TYPE == Which Sprite to draw! */

			// Main Buttons
			resolutionButton = new MenuButton(0, 0, 155, 29, 0, false, true);
			resizeableButton = new MenuButton(0, 0, 155, 29, 2, false, true);
			fpsButton = new MenuButton(0, 0, 155, 29, 4, false, true);
			displayFPSButton = new MenuButton(0, 0, 155, 29, 6, false, true);
			showHealthBarsButton = new MenuButton(0, 0, 155, 29, 8, false, true);
			returnToMenuButton = new MenuButton(0, 0, 155, 29, 10, false, true);
			saveButton = new MenuButton(0, 0, 155, 29, 52, false, true);

			// True / False
			resizeableTrue = new MenuButton(0, 0, 155, 29, 12, false, true, false, false);
			resizeableFalse = new MenuButton(0, 0, 155, 29, 14, false, false, false, false);
			displayFPSTrue = new MenuButton(0, 0, 155, 29, 12, false, true, false, false);
			displayFPSFalse = new MenuButton(0, 0, 155, 29, 14, false, false, false, false);
			showHealthBarsTrue = new MenuButton(0, 0, 155, 29, 12, false, true, false, false);
			showHealthBarsFalse = new MenuButton(0, 0, 155, 29, 14, false, false, false, false);

			// Resolutions
			_fullscreen = new MenuButton(0, 0, 155, 29, 16, false, true, true, false);
			_480x320 = new MenuButton(0, 0, 155, 29, 18, false, false, false, false);
			_640x480 = new MenuButton(0, 0, 155, 29, 20, false, false, false, false);
			_800x600 = new MenuButton(0, 0, 155, 29, 22, false, false, false, false);
			_1280x720 = new MenuButton(0, 0, 155, 29, 24, false, false, false, false);
			_1280x960 = new MenuButton(0, 0, 155, 29, 26, false, false, false, false);
			_1600x900 = new MenuButton(0, 0, 155, 29, 28, false, false, false, false);
			_1920x1080 = new MenuButton(0, 0, 155, 29, 30, false, false, false, false);
			_2560x1440 = new MenuButton(0, 0, 155, 29, 32, false, false, false, false);
			_3840x2160 = new MenuButton(0, 0, 155, 29, 34, false, false, false, false);
			_8192x4068 = new MenuButton(0, 0, 155, 29, 36, false, false, false, false);

			// FPS caps
			FPS30 = new MenuButton(0, 0, 155, 29, 38, false, false, false, false);
			FPS60 = new MenuButton(0, 0, 155, 29, 40, false, true, true, false);
			FPS90 = new MenuButton(0, 0, 155, 29, 42, false, false, false, false);
			FPS120 = new MenuButton(0, 0, 155, 29, 44, false, false, false, false);
			FPS144 = new MenuButton(0, 0, 155, 29, 46, false, false, false, false);
			FPS200 = new MenuButton(0, 0, 155, 29, 48, false, false, false, false);
			FPS_RIP_CPU = new MenuButton(0, 0, 155, 29, 50, false, false, false, false);

			// Main Buttons
			mainButtons.add(resolutionButton);
			mainButtons.add(resizeableButton);
			mainButtons.add(fpsButton);
			mainButtons.add(displayFPSButton);
			mainButtons.add(showHealthBarsButton);
			mainButtons.add(returnToMenuButton);
			mainButtons.add(saveButton);

			// True / False
			resizeableContextMenu.add(resizeableTrue);
			resizeableContextMenu.add(resizeableFalse);
			displayFPSContextMenu.add(displayFPSTrue);
			displayFPSContextMenu.add(displayFPSFalse);
			showEnemyHealthContextMenu.add(showHealthBarsTrue);
			showEnemyHealthContextMenu.add(showHealthBarsFalse);

			// Resolutions
			resolutionContextMenu.add(_fullscreen);
			resolutionContextMenu.add(_480x320);
			resolutionContextMenu.add(_640x480);
			resolutionContextMenu.add(_800x600);
			resolutionContextMenu.add(_1280x720);
			resolutionContextMenu.add(_1280x960);
			resolutionContextMenu.add(_1600x900);
			resolutionContextMenu.add(_1920x1080);
			resolutionContextMenu.add(_2560x1440);
			resolutionContextMenu.add(_3840x2160);
			resolutionContextMenu.add(_8192x4068);

			// FPS CAPS
			fpsContextMenu.add(FPS30);
			fpsContextMenu.add(FPS60);
			fpsContextMenu.add(FPS90);
			fpsContextMenu.add(FPS120);
			fpsContextMenu.add(FPS144);
			fpsContextMenu.add(FPS200);
			fpsContextMenu.add(FPS_RIP_CPU);

			// find the largest of the arrays
			max = mainButtons.size();
			if (max < resizeableContextMenu.size()) max = resizeableContextMenu.size();
			else if (max < displayFPSContextMenu.size()) max = displayFPSContextMenu.size();
			else if (max < showEnemyHealthContextMenu.size()) max = showEnemyHealthContextMenu.size();
			else if (max < resolutionContextMenu.size()) max = resolutionContextMenu.size();
			else if (max < fpsContextMenu.size()) max = fpsContextMenu.size();
			largestArraySize = max;

			// save default settings
			createSettingsSaveFile();
		}
	}

	public static synchronized void applySettings() {

		try {

			// True / False Flipping:
			if (!resizeableTrueFalseReverse) {
				// do something
			} // else dont something;

			if (!displayFPSTrueFalseReverse) {
				Game.displayFPS = true;
			} else Game.displayFPS = false;

			if (!showEnemyHealthBarsTrueFalseReverse) {
				Game.showEnemyHealthBars = true;
			} else Game.showEnemyHealthBars = false;

			// Resolution Changing
			if (_fullscreen.isAtTop) Game.fullscreen = true;
			else Game.fullscreen = false;

			if (_fullscreen.isAtTop) Game.makeFullScreen();

			if (_480x320.isAtTop) {
				Game.WIDTH = 480;
				Game.HEIGHT = 320;
			} else if (_640x480.isAtTop) {
				Game.WIDTH = 640;
				Game.HEIGHT = 480;
			} else if (_800x600.isAtTop) {
				Game.WIDTH = 800;
				Game.HEIGHT = 600;
			} else if (_1280x720.isAtTop) {
				Game.WIDTH = 1280;
				Game.HEIGHT = 720;
			} else if (_1280x960.isAtTop) {
				Game.WIDTH = 1280;
				Game.HEIGHT = 960;
			} else if (_1600x900.isAtTop) {
				Game.WIDTH = 1600;
				Game.HEIGHT = 900;
			} else if (_1920x1080.isAtTop) {
				Game.WIDTH = 1920;
				Game.HEIGHT = 1080;
			} else if (_2560x1440.isAtTop) {
				Game.WIDTH = 2560;
				Game.HEIGHT = 1440;
			} else if (_3840x2160.isAtTop) {
				Game.WIDTH = 3840;
				Game.HEIGHT = 2160;
			} else if (_8192x4068.isAtTop) {
				Game.WIDTH = 8192;
				Game.HEIGHT = 4068;
			}

			// change the frame size
			if (!_fullscreen.isAtTop) {
				if(!Game.init) Game.undoFullScreen();
				Game.frame.setSize(Game.WIDTH, Game.HEIGHT);
			}

			// FPS Cap changing
			if (FPS30.isAtTop) Game.FPS_CAP = (30.0);
			else if (FPS60.isAtTop) Game.FPS_CAP = (60.0);
			else if (FPS90.isAtTop) Game.FPS_CAP = (90.0);
			else if (FPS120.isAtTop) Game.FPS_CAP = (120.0);
			else if (FPS144.isAtTop) Game.FPS_CAP = (144.0);
			else if (FPS200.isAtTop) Game.FPS_CAP = (200.0);
			else if (FPS_RIP_CPU.isAtTop) Game.FPS_CAP = 1500;

			Game.frameLimit = Utils.second / Game.FPS_CAP;
			
			Inventory.changeSettings();
			ConstructionMenu.changeSettings();
			
			Game.frame.setLocation(java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width / 2 - Game.frame.getWidth() / 2, 
			java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height / 2 - Game.frame.getHeight() / 2);
			
			WorldGenerator.regenWorldSectionsAndTiles(); /* NOTE: will take a while */
			
		} catch (NullPointerException npe) {
			if (nullCount > 5) {
				npe.printStackTrace();
			} else nullCount++;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public synchronized void onClick() {

		try { /* giant null pointer catch for issues when changing menus, ignore */

			// Main buttons
			for (int i = 0; i < mainButtons.size(); i++) {
				if (mainButtons.get(i).contains(MouseInput.mouseXY) && mainButtons.get(i).type == 0) {
					for (int j = 0; j < resolutionContextMenu.size(); j++) {
						if (!resolutionContextMenu.get(j).isAtTop) resolutionContextMenu.get(j).isVisible = !resolutionContextMenu.get(j).isVisible;
					}
					for (int j = 0; j < largestArraySize; j++) {
						if (j < fpsContextMenu.size()) {
							fpsContextMenu.get(j).otherContextMenuIsOpen = true;
						}
						if (j < resizeableContextMenu.size()) {
							resizeableContextMenu.get(j).otherContextMenuIsOpen = true;
						}
						if (j < displayFPSContextMenu.size()) {
							displayFPSContextMenu.get(j).otherContextMenuIsOpen = true;
						}
						if (j < showEnemyHealthContextMenu.size()) {
							showEnemyHealthContextMenu.get(j).otherContextMenuIsOpen = true;
						}

					}

				} else if (mainButtons.get(i).contains(MouseInput.mouseXY) && mainButtons.get(i).type == 2) {
					if (!resizeableTrueFalseReverse) resizeableFalse.isVisible = !resizeableFalse.isVisible;
					else resizeableTrue.isVisible = !resizeableTrue.isVisible;

				} else if (mainButtons.get(i).contains(MouseInput.mouseXY) && mainButtons.get(i).type == 4) {
					for (int j = 0; j < fpsContextMenu.size(); j++) {
						if (!fpsContextMenu.get(j).isAtTop) fpsContextMenu.get(j).isVisible = !fpsContextMenu.get(j).isVisible;
					}
					for (int j = 0; j < largestArraySize; j++) {
						if (j < resizeableContextMenu.size()) {
							resizeableContextMenu.get(j).otherContextMenuIsOpen = true;
						}
						if (j < displayFPSContextMenu.size()) {
							displayFPSContextMenu.get(j).otherContextMenuIsOpen = true;
						}
						if (j < showEnemyHealthContextMenu.size()) {
							showEnemyHealthContextMenu.get(j).otherContextMenuIsOpen = true;
						}
						if (j < resolutionContextMenu.size()) {
							resolutionContextMenu.get(j).otherContextMenuIsOpen = true;
						}
					}
				} else if (mainButtons.get(i).contains(MouseInput.mouseXY) && mainButtons.get(i).type == 6) {
					if (!displayFPSTrueFalseReverse) displayFPSFalse.isVisible = !displayFPSFalse.isVisible;
					else displayFPSTrue.isVisible = !displayFPSTrue.isVisible;
				} else if (mainButtons.get(i).contains(MouseInput.mouseXY) && mainButtons.get(i).type == 8) {
					if (!showEnemyHealthBarsTrueFalseReverse) showHealthBarsFalse.isVisible = !showHealthBarsFalse.isVisible;
					else showHealthBarsTrue.isVisible = !showHealthBarsTrue.isVisible;
				} else if (mainButtons.get(i).contains(MouseInput.mouseXY) && mainButtons.get(i).type == 10) {

					if (!justCameFromPaused) {
						Game.state = STATE.MENU;
						cleanUp();
					} else if (justCameFromPaused) {
						Game.justCameFromSettings = true;
						Game.state = STATE.PAUSED;
						cleanUp();
					}

				} else if (mainButtons.get(i).contains(MouseInput.mouseXY) && mainButtons.get(i).type == 52) {
					if (!justCameFromPaused) {
						createSettingsSaveFile();
						Game.state = STATE.MENU;
						cleanUp();
					} else if (justCameFromPaused) {
						createSettingsSaveFile();
						Game.state = STATE.PAUSED;
						cleanUp();
					}

				}
			}

			// Resizeable T/F flip
			if (resizeableTrue.contains(MouseInput.mouseXY) && resizeableTrueFalseReverse & resizeableFalse.isVisible & resizeableTrue.isVisible) {
				resizeableTrueFalseReverse = !resizeableTrueFalseReverse;
				if (resizeableTrue.isVisible) resizeableFalse.isVisible = !resizeableFalse.isVisible;

			} else if (resizeableFalse.contains(MouseInput.mouseXY) && !resizeableTrueFalseReverse & resizeableFalse.isVisible) {
				resizeableTrueFalseReverse = !resizeableTrueFalseReverse;
				if (resizeableFalse.isVisible) resizeableTrue.isVisible = !resizeableTrue.isVisible;

			}
			// DisplayFPS T/F flip
			else if (displayFPSTrue.contains(MouseInput.mouseXY) && displayFPSTrueFalseReverse & displayFPSFalse.isVisible & displayFPSTrue.isVisible) {
				displayFPSTrueFalseReverse = !displayFPSTrueFalseReverse;
				if (displayFPSTrue.isVisible) displayFPSFalse.isVisible = !displayFPSFalse.isVisible;

			} else if (displayFPSFalse.contains(MouseInput.mouseXY) && !displayFPSTrueFalseReverse & displayFPSFalse.isVisible) {
				displayFPSTrueFalseReverse = !displayFPSTrueFalseReverse;
				if (displayFPSFalse.isVisible) displayFPSTrue.isVisible = !displayFPSTrue.isVisible;

			}
			// Show Enemy Health Bars Flip
			else if (showHealthBarsTrue.contains(MouseInput.mouseXY)
					&& showEnemyHealthBarsTrueFalseReverse & showHealthBarsFalse.isVisible & showHealthBarsTrue.isVisible) {
				showEnemyHealthBarsTrueFalseReverse = !showEnemyHealthBarsTrueFalseReverse;
				if (showHealthBarsTrue.isVisible) showHealthBarsFalse.isVisible = !showHealthBarsFalse.isVisible;

			} else if (showHealthBarsFalse.contains(MouseInput.mouseXY) && !showEnemyHealthBarsTrueFalseReverse & showHealthBarsFalse.isVisible) {
				showEnemyHealthBarsTrueFalseReverse = !showEnemyHealthBarsTrueFalseReverse;
				if (showHealthBarsFalse.contains(MouseInput.mouseXY) && showHealthBarsFalse.isVisible)
					showHealthBarsTrue.isVisible = !showHealthBarsTrue.isVisible;

			}

			//// **** FPS CONTEXT MENU BUTTON CLICKS: ******////
			// if clicked and is visible, set it to top then set all others to not at top, and not visible
			if (FPS30.contains(MouseInput.mouseXY) && FPS30.isVisible) {
				FPS30.isAtTop = true;
				for (int i = 0; i < fpsContextMenu.size(); i++) {
					if (fpsContextMenu.get(i) != FPS30) {
						fpsContextMenu.get(i).isAtTop = false;
						fpsContextMenu.get(i).isVisible = false;
					}
				}
				for (int j = 0; j < largestArraySize; j++) {
					if (j < resizeableContextMenu.size()) {
						resizeableContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < displayFPSContextMenu.size()) {
						displayFPSContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < showEnemyHealthContextMenu.size()) {
						showEnemyHealthContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < resolutionContextMenu.size()) {
						resolutionContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < fpsContextMenu.size()) {
						fpsContextMenu.get(j).otherContextMenuIsOpen = false;
					}
				}
			} else if (FPS60.contains(MouseInput.mouseXY) && FPS60.isVisible) {
				FPS60.isAtTop = true;
				for (int i = 0; i < fpsContextMenu.size(); i++) {
					if (fpsContextMenu.get(i) != FPS60) {
						fpsContextMenu.get(i).isAtTop = false;
						fpsContextMenu.get(i).isVisible = false;
					}
				}
				for (int j = 0; j < largestArraySize; j++) {
					if (j < resizeableContextMenu.size()) {
						resizeableContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < displayFPSContextMenu.size()) {
						displayFPSContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < showEnemyHealthContextMenu.size()) {
						showEnemyHealthContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < resolutionContextMenu.size()) {
						resolutionContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < fpsContextMenu.size()) {
						fpsContextMenu.get(j).otherContextMenuIsOpen = false;
					}
				}
			} else if (FPS90.contains(MouseInput.mouseXY) && FPS90.isVisible) {
				FPS90.isAtTop = true;
				for (int i = 0; i < fpsContextMenu.size(); i++) {
					if (fpsContextMenu.get(i) != FPS90) {
						fpsContextMenu.get(i).isAtTop = false;
						fpsContextMenu.get(i).isVisible = false;
					}
				}
				for (int j = 0; j < largestArraySize; j++) {
					if (j < resizeableContextMenu.size()) {
						resizeableContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < displayFPSContextMenu.size()) {
						displayFPSContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < showEnemyHealthContextMenu.size()) {
						showEnemyHealthContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < resolutionContextMenu.size()) {
						resolutionContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < fpsContextMenu.size()) {
						fpsContextMenu.get(j).otherContextMenuIsOpen = false;
					}
				}
			} else if (FPS120.contains(MouseInput.mouseXY) && FPS120.isVisible) {
				FPS120.isAtTop = true;
				for (int i = 0; i < fpsContextMenu.size(); i++) {
					if (fpsContextMenu.get(i) != FPS120) {
						fpsContextMenu.get(i).isAtTop = false;
						fpsContextMenu.get(i).isVisible = false;
					}
				}
				for (int j = 0; j < largestArraySize; j++) {
					if (j < resizeableContextMenu.size()) {
						resizeableContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < displayFPSContextMenu.size()) {
						displayFPSContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < showEnemyHealthContextMenu.size()) {
						showEnemyHealthContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < resolutionContextMenu.size()) {
						resolutionContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < fpsContextMenu.size()) {
						fpsContextMenu.get(j).otherContextMenuIsOpen = false;
					}
				}
			} else if (FPS144.contains(MouseInput.mouseXY) && FPS144.isVisible) {
				FPS144.isAtTop = true;
				for (int i = 0; i < fpsContextMenu.size(); i++) {
					if (fpsContextMenu.get(i) != FPS144) {
						fpsContextMenu.get(i).isAtTop = false;
						fpsContextMenu.get(i).isVisible = false;
					}
				}
				for (int j = 0; j < largestArraySize; j++) {
					if (j < resizeableContextMenu.size()) {
						resizeableContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < displayFPSContextMenu.size()) {
						displayFPSContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < showEnemyHealthContextMenu.size()) {
						showEnemyHealthContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < resolutionContextMenu.size()) {
						resolutionContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < fpsContextMenu.size()) {
						fpsContextMenu.get(j).otherContextMenuIsOpen = false;
					}
				}
			} else if (FPS200.contains(MouseInput.mouseXY) && FPS200.isVisible) {
				FPS200.isAtTop = true;
				for (int i = 0; i < fpsContextMenu.size(); i++) {
					if (fpsContextMenu.get(i) != FPS200) {
						fpsContextMenu.get(i).isAtTop = false;
						fpsContextMenu.get(i).isVisible = false;
					}
				}
				for (int j = 0; j < largestArraySize; j++) {
					if (j < resizeableContextMenu.size()) {
						resizeableContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < displayFPSContextMenu.size()) {
						displayFPSContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < showEnemyHealthContextMenu.size()) {
						showEnemyHealthContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < resolutionContextMenu.size()) {
						resolutionContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < fpsContextMenu.size()) {
						fpsContextMenu.get(j).otherContextMenuIsOpen = false;
					}
				}
			} else if (FPS_RIP_CPU.contains(MouseInput.mouseXY) && FPS_RIP_CPU.isVisible) {
				FPS_RIP_CPU.isAtTop = true;
				for (int i = 0; i < fpsContextMenu.size(); i++) {
					if (fpsContextMenu.get(i) != FPS_RIP_CPU) {
						fpsContextMenu.get(i).isAtTop = false;
						fpsContextMenu.get(i).isVisible = false;
					}
				}
				for (int j = 0; j < largestArraySize; j++) {
					if (j < resizeableContextMenu.size()) {
						resizeableContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < displayFPSContextMenu.size()) {
						displayFPSContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < showEnemyHealthContextMenu.size()) {
						showEnemyHealthContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < resolutionContextMenu.size()) {
						resolutionContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < fpsContextMenu.size()) {
						fpsContextMenu.get(j).otherContextMenuIsOpen = false;
					}
				}

			}

			//// **** RESOLUTION CONTEXT MENU BUTTON CLICKS: ******////
			// if clicked and is visible, set it to top then set all others to not at top, and not visible
			if (_fullscreen.contains(MouseInput.mouseXY) && _fullscreen.isVisible) {
				_fullscreen.isAtTop = true;
				for (int i = 0; i < resolutionContextMenu.size(); i++) {
					if (resolutionContextMenu.get(i) != _fullscreen) resolutionContextMenu.get(i).isAtTop = false;
					if (resolutionContextMenu.get(i) != _fullscreen) resolutionContextMenu.get(i).isVisible = false;
				}

				for (int j = 0; j < largestArraySize; j++) {
					if (j < resizeableContextMenu.size()) {
						resizeableContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < displayFPSContextMenu.size()) {
						displayFPSContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < showEnemyHealthContextMenu.size()) {
						showEnemyHealthContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < resolutionContextMenu.size()) {
						resolutionContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < fpsContextMenu.size()) {
						fpsContextMenu.get(j).otherContextMenuIsOpen = false;
					}
				}

			} else if (_480x320.contains(MouseInput.mouseXY) && _480x320.isVisible) {
				_480x320.isAtTop = true;
				for (int i = 0; i < resolutionContextMenu.size(); i++) {
					if (resolutionContextMenu.get(i) != _480x320) resolutionContextMenu.get(i).isAtTop = false;
					if (resolutionContextMenu.get(i) != _480x320) resolutionContextMenu.get(i).isVisible = false;
				}

				for (int j = 0; j < largestArraySize; j++) {
					if (j < resizeableContextMenu.size()) {
						resizeableContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < displayFPSContextMenu.size()) {
						displayFPSContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < showEnemyHealthContextMenu.size()) {
						showEnemyHealthContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < resolutionContextMenu.size()) {
						resolutionContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < fpsContextMenu.size()) {
						fpsContextMenu.get(j).otherContextMenuIsOpen = false;
					}
				}

			} else if (_640x480.contains(MouseInput.mouseXY) && _640x480.isVisible) {
				_640x480.isAtTop = true;
				for (int i = 0; i < resolutionContextMenu.size(); i++) {
					if (resolutionContextMenu.get(i) != _640x480) resolutionContextMenu.get(i).isAtTop = false;
					if (resolutionContextMenu.get(i) != _640x480) resolutionContextMenu.get(i).isVisible = false;
				}

				for (int j = 0; j < largestArraySize; j++) {
					if (j < resizeableContextMenu.size()) {
						resizeableContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < displayFPSContextMenu.size()) {
						displayFPSContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < showEnemyHealthContextMenu.size()) {
						showEnemyHealthContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < resolutionContextMenu.size()) {
						resolutionContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < fpsContextMenu.size()) {
						fpsContextMenu.get(j).otherContextMenuIsOpen = false;
					}
				}

			} else if (_800x600.contains(MouseInput.mouseXY) && _800x600.isVisible) {
				_800x600.isAtTop = true;
				for (int i = 0; i < resolutionContextMenu.size(); i++) {
					if (resolutionContextMenu.get(i) != _800x600) resolutionContextMenu.get(i).isAtTop = false;
					if (resolutionContextMenu.get(i) != _800x600) resolutionContextMenu.get(i).isVisible = false;
				}

				for (int j = 0; j < largestArraySize; j++) {
					if (j < resizeableContextMenu.size()) {
						resizeableContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < displayFPSContextMenu.size()) {
						displayFPSContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < showEnemyHealthContextMenu.size()) {
						showEnemyHealthContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < resolutionContextMenu.size()) {
						resolutionContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < fpsContextMenu.size()) {
						fpsContextMenu.get(j).otherContextMenuIsOpen = false;
					}
				}

			} else if (_1280x720.contains(MouseInput.mouseXY) && _1280x720.isVisible) {
				_1280x720.isAtTop = true;
				for (int i = 0; i < resolutionContextMenu.size(); i++) {
					if (resolutionContextMenu.get(i) != _1280x720) resolutionContextMenu.get(i).isAtTop = false;
					if (resolutionContextMenu.get(i) != _1280x720) resolutionContextMenu.get(i).isVisible = false;
				}

				for (int j = 0; j < largestArraySize; j++) {
					if (j < resizeableContextMenu.size()) {
						resizeableContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < displayFPSContextMenu.size()) {
						displayFPSContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < showEnemyHealthContextMenu.size()) {
						showEnemyHealthContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < resolutionContextMenu.size()) {
						resolutionContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < fpsContextMenu.size()) {
						fpsContextMenu.get(j).otherContextMenuIsOpen = false;
					}
				}

			} else if (_1280x960.contains(MouseInput.mouseXY) && _1280x960.isVisible) {
				_1280x960.isAtTop = true;
				for (int i = 0; i < resolutionContextMenu.size(); i++) {
					if (resolutionContextMenu.get(i) != _1280x960) resolutionContextMenu.get(i).isAtTop = false;
					if (resolutionContextMenu.get(i) != _1280x960) resolutionContextMenu.get(i).isVisible = false;
				}

				for (int j = 0; j < largestArraySize; j++) {
					if (j < resizeableContextMenu.size()) {
						resizeableContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < displayFPSContextMenu.size()) {
						displayFPSContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < showEnemyHealthContextMenu.size()) {
						showEnemyHealthContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < resolutionContextMenu.size()) {
						resolutionContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < fpsContextMenu.size()) {
						fpsContextMenu.get(j).otherContextMenuIsOpen = false;
					}
				}

			} else if (_1600x900.contains(MouseInput.mouseXY) && _1600x900.isVisible) {
				_1600x900.isAtTop = true;
				for (int i = 0; i < resolutionContextMenu.size(); i++) {
					if (resolutionContextMenu.get(i) != _1600x900) resolutionContextMenu.get(i).isAtTop = false;
					if (resolutionContextMenu.get(i) != _1600x900) resolutionContextMenu.get(i).isVisible = false;
				}

				for (int j = 0; j < largestArraySize; j++) {
					if (j < resizeableContextMenu.size()) {
						resizeableContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < displayFPSContextMenu.size()) {
						displayFPSContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < showEnemyHealthContextMenu.size()) {
						showEnemyHealthContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < resolutionContextMenu.size()) {
						resolutionContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < fpsContextMenu.size()) {
						fpsContextMenu.get(j).otherContextMenuIsOpen = false;
					}
				}

			} else if (_1920x1080.contains(MouseInput.mouseXY) && _1920x1080.isVisible) {
				_1920x1080.isAtTop = true;
				for (int i = 0; i < resolutionContextMenu.size(); i++) {
					if (resolutionContextMenu.get(i) != _1920x1080) resolutionContextMenu.get(i).isAtTop = false;
					if (resolutionContextMenu.get(i) != _1920x1080) resolutionContextMenu.get(i).isVisible = false;
				}

				for (int j = 0; j < largestArraySize; j++) {
					if (j < resizeableContextMenu.size()) {
						resizeableContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < displayFPSContextMenu.size()) {
						displayFPSContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < showEnemyHealthContextMenu.size()) {
						showEnemyHealthContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < resolutionContextMenu.size()) {
						resolutionContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < fpsContextMenu.size()) {
						fpsContextMenu.get(j).otherContextMenuIsOpen = false;
					}
				}

			} else if (_2560x1440.contains(MouseInput.mouseXY) && _2560x1440.isVisible) {
				_2560x1440.isAtTop = true;
				for (int i = 0; i < resolutionContextMenu.size(); i++) {
					if (resolutionContextMenu.get(i) != _2560x1440) resolutionContextMenu.get(i).isAtTop = false;
					if (resolutionContextMenu.get(i) != _2560x1440) resolutionContextMenu.get(i).isVisible = false;
				}

				for (int j = 0; j < largestArraySize; j++) {
					if (j < resizeableContextMenu.size()) {
						resizeableContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < displayFPSContextMenu.size()) {
						displayFPSContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < showEnemyHealthContextMenu.size()) {
						showEnemyHealthContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < resolutionContextMenu.size()) {
						resolutionContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < fpsContextMenu.size()) {
						fpsContextMenu.get(j).otherContextMenuIsOpen = false;
					}
				}

			} else if (_3840x2160.contains(MouseInput.mouseXY) && _3840x2160.isVisible) {
				_3840x2160.isAtTop = true;
				for (int i = 0; i < resolutionContextMenu.size(); i++) {
					if (resolutionContextMenu.get(i) != _3840x2160) resolutionContextMenu.get(i).isAtTop = false;
					if (resolutionContextMenu.get(i) != _3840x2160) resolutionContextMenu.get(i).isVisible = false;
				}

				for (int j = 0; j < largestArraySize; j++) {
					if (j < resizeableContextMenu.size()) {
						resizeableContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < displayFPSContextMenu.size()) {
						displayFPSContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < showEnemyHealthContextMenu.size()) {
						showEnemyHealthContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < resolutionContextMenu.size()) {
						resolutionContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < fpsContextMenu.size()) {
						fpsContextMenu.get(j).otherContextMenuIsOpen = false;
					}
				}

			} else if (_8192x4068.contains(MouseInput.mouseXY) && _8192x4068.isVisible) {
				_8192x4068.isAtTop = true;
				for (int i = 0; i < resolutionContextMenu.size(); i++) {
					if (resolutionContextMenu.get(i) != _8192x4068) resolutionContextMenu.get(i).isAtTop = false;
					if (resolutionContextMenu.get(i) != _8192x4068) resolutionContextMenu.get(i).isVisible = false;
				}

				for (int j = 0; j < largestArraySize; j++) {
					if (j < resizeableContextMenu.size()) {
						resizeableContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < displayFPSContextMenu.size()) {
						displayFPSContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < showEnemyHealthContextMenu.size()) {
						showEnemyHealthContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < resolutionContextMenu.size()) {
						resolutionContextMenu.get(j).otherContextMenuIsOpen = false;
					}
					if (j < fpsContextMenu.size()) {
						fpsContextMenu.get(j).otherContextMenuIsOpen = false;
					}
				}
			}
		} catch (NullPointerException npe) {
			if (nullCount > 5) {
				npe.printStackTrace();
			} else nullCount++;
		}
	}

	public void tick() {
		
		if(Game.state != STATE.SETTINGS) return;

		try { /**** Giant null pointer check, helps solve odd problems occurring ****/

			// tick buttons x and y to adjust for screen resolution changes
			// Main buttons
			resolutionButton.x = Game.game.getWidth() - Game.game.getWidth() / 1.03f;
			resolutionButton.y = Game.game.getHeight() * 0.1f;
			fpsButton.x = Game.game.getWidth() - Game.game.getWidth() / 1.03f;
			fpsButton.y = Game.game.getHeight() * 0.2f;
			resizeableButton.x = Game.game.getWidth() - Game.game.getWidth() / 1.03f;
			resizeableButton.y = Game.game.getHeight() * 0.3f;
			displayFPSButton.x = Game.game.getWidth() - Game.game.getWidth() / 1.03f;
			displayFPSButton.y = Game.game.getHeight() * 0.4f;
			showHealthBarsButton.x = Game.game.getWidth() - Game.game.getWidth() / 1.03f;
			showHealthBarsButton.y = Game.game.getHeight() * 0.5f;
			returnToMenuButton.x = Game.game.getWidth() - Game.game.getWidth() / 4.5f;
			returnToMenuButton.y = Game.game.getHeight() * 0.85f;
			saveButton.x = Game.game.getWidth() - Game.game.getWidth() / 4.5f;
			saveButton.y = Game.game.getHeight() * 0.775f;
			// True / False buttons
			resizeableTrue.x = Game.game.getWidth() - Game.game.getWidth() / 1.4f;
			resizeableTrue.y = Game.game.getHeight() * 0.3f;
			resizeableFalse.x = Game.game.getWidth() - Game.game.getWidth() / 1.4f;
			resizeableFalse.y = Game.game.getHeight() * 0.35f;
			displayFPSTrue.x = Game.game.getWidth() - Game.game.getWidth() / 1.4f;
			displayFPSTrue.y = Game.game.getHeight() * 0.4f;
			displayFPSFalse.x = Game.game.getWidth() - Game.game.getWidth() / 1.4f;
			displayFPSFalse.y = Game.game.getHeight() * 0.45f;
			showHealthBarsTrue.x = Game.game.getWidth() - Game.game.getWidth() / 1.4f;
			showHealthBarsTrue.y = Game.game.getHeight() * 0.5f;
			showHealthBarsFalse.x = Game.game.getWidth() - Game.game.getWidth() / 1.4f;
			showHealthBarsFalse.y = Game.game.getHeight() * 0.55f;
			// Resolution Buttons
			_fullscreen.x = Game.game.getWidth() - Game.game.getWidth() / 1.4f;
			_fullscreen.y = Game.game.getHeight() * 0.1f;
			_480x320.x = Game.game.getWidth() - Game.game.getWidth() / 1.4f;
			_480x320.y = Game.game.getHeight() * 0.15f;
			_640x480.x = Game.game.getWidth() - Game.game.getWidth() / 1.4f;
			_640x480.y = Game.game.getHeight() * 0.2f;
			_800x600.x = Game.game.getWidth() - Game.game.getWidth() / 1.4f;
			_800x600.y = Game.game.getHeight() * 0.25f;
			_1280x720.x = Game.game.getWidth() - Game.game.getWidth() / 1.4f;
			_1280x720.y = Game.game.getHeight() * 0.3f;
			_1280x960.x = Game.game.getWidth() - Game.game.getWidth() / 1.4f;
			_1280x960.y = Game.game.getHeight() * 0.35f;
			_1600x900.x = Game.game.getWidth() - Game.game.getWidth() / 1.4f;
			_1600x900.y = Game.game.getHeight() * 0.4f;
			_1920x1080.x = Game.game.getWidth() - Game.game.getWidth() / 1.4f;
			_1920x1080.y = Game.game.getHeight() * 0.45f;
			_2560x1440.x = Game.game.getWidth() - Game.game.getWidth() / 1.4f;
			_2560x1440.y = Game.game.getHeight() * 0.5f;
			_3840x2160.x = Game.game.getWidth() - Game.game.getWidth() / 1.4f;
			_3840x2160.y = Game.game.getHeight() * 0.55f;
			_8192x4068.x = Game.game.getWidth() - Game.game.getWidth() / 1.4f;
			_8192x4068.y = Game.game.getHeight() * 0.6f;
			// FPS CAPS
			FPS30.x = Game.game.getWidth() - Game.game.getWidth() / 1.4f;
			FPS30.y = Game.game.getHeight() * 0.2f;
			FPS60.x = Game.game.getWidth() - Game.game.getWidth() / 1.4f;
			FPS60.y = Game.game.getHeight() * 0.25f;
			FPS90.x = Game.game.getWidth() - Game.game.getWidth() / 1.4f;
			FPS90.y = Game.game.getHeight() * 0.3f;
			FPS120.x = Game.game.getWidth() - Game.game.getWidth() / 1.4f;
			FPS120.y = Game.game.getHeight() * 0.35f;
			FPS144.x = Game.game.getWidth() - Game.game.getWidth() / 1.4f;
			FPS144.y = Game.game.getHeight() * 0.4f;
			FPS200.x = Game.game.getWidth() - Game.game.getWidth() / 1.4f;
			FPS200.y = Game.game.getHeight() * 0.45f;
			FPS_RIP_CPU.x = Game.game.getWidth() - Game.game.getWidth() / 1.4f;
			FPS_RIP_CPU.y = Game.game.getHeight() * 0.5f;

			// Move buttons around according to if they're supposed to be at the top
			/***** FPS ***/
			if (FPS30.isAtTop) FPS30.y = Game.game.getHeight() * 0.2f;
			else if (FPS60.isAtTop) FPS60.y = Game.game.getHeight() * 0.2f;
			else if (FPS90.isAtTop) FPS90.y = Game.game.getHeight() * 0.2f;
			else if (FPS120.isAtTop) FPS120.y = Game.game.getHeight() * 0.2f;
			else if (FPS144.isAtTop) FPS144.y = Game.game.getHeight() * 0.2f;
			else if (FPS200.isAtTop) FPS200.y = Game.game.getHeight() * 0.2f;
			else if (FPS_RIP_CPU.isAtTop) FPS_RIP_CPU.y = Game.game.getHeight() * 0.2f;
			/****** RESOLUTIONS *******/
			if (_fullscreen.isAtTop) _fullscreen.y = Game.game.getHeight() * 0.1f;
			else if (_480x320.isAtTop) _480x320.y = Game.game.getHeight() * 0.1f;
			else if (_640x480.isAtTop) _640x480.y = Game.game.getHeight() * 0.1f;
			else if (_800x600.isAtTop) _800x600.y = Game.game.getHeight() * 0.1f;
			else if (_1280x720.isAtTop) _1280x720.y = Game.game.getHeight() * 0.1f;
			else if (_1280x960.isAtTop) _1280x960.y = Game.game.getHeight() * 0.1f;
			else if (_1600x900.isAtTop) _1600x900.y = Game.game.getHeight() * 0.1f;
			else if (_1920x1080.isAtTop) _1920x1080.y = Game.game.getHeight() * 0.1f;
			else if (_2560x1440.isAtTop) _2560x1440.y = Game.game.getHeight() * 0.1f;
			else if (_3840x2160.isAtTop) _3840x2160.y = Game.game.getHeight() * 0.1f;
			else if (_8192x4068.isAtTop) _8192x4068.y = Game.game.getHeight() * 0.1f;

			// Move the rest of the context buttons in menu along accordingly
			for (int i = 1; i < fpsContextMenu.size() + 1; i++) {
				if (!fpsContextMenu.get(i - 1).isAtTop) {
					fpsContextMenu.get(i - 1).y = (Game.game.getHeight() * 0.2f) + (Game.game.getHeight() * 0.05f * i);
				}
			}
			for (int i = 1; i < resolutionContextMenu.size() + 1; i++) {
				if (!resolutionContextMenu.get(i - 1).isAtTop) {
					resolutionContextMenu.get(i - 1).y = (Game.game.getHeight() * 0.1f) + (Game.game.getHeight() * 0.05f * i);
				}
			}

			// Flip x/y's for True / False selection changes
			// Resizeable
			if (resizeableTrueFalseReverse) {
				resizeableTrue.y = (float) Utils.swap(resizeableFalse.y, resizeableFalse.y = resizeableTrue.y);
				resizeableTrue.x = (float) Utils.swap(resizeableFalse.x, resizeableFalse.x = resizeableTrue.x);
			}
			// DisplayFPS
			if (displayFPSTrueFalseReverse) {
				displayFPSTrue.y = (float) Utils.swap(displayFPSFalse.y, displayFPSFalse.y = displayFPSTrue.y);
				displayFPSFalse.x = (float) Utils.swap(displayFPSFalse.x, displayFPSFalse.x = displayFPSTrue.x);
			}
			// Show Enemy Health Bars
			if (showEnemyHealthBarsTrueFalseReverse) {
				showHealthBarsTrue.y = (float) Utils.swap(showHealthBarsFalse.y, showHealthBarsFalse.y = showHealthBarsTrue.y);
				showHealthBarsFalse.x = (float) Utils.swap(showHealthBarsFalse.x, showHealthBarsFalse.x = showHealthBarsTrue.x);
			}

			for (int i = 0; i < mainButtons.size(); i++) {
				mainButtons.get(i).calcIsHovering();
				mainButtons.get(i).setBounds((int) mainButtons.get(i).x, (int) mainButtons.get(i).y, mainButtons.get(i).width, mainButtons.get(i).height);
			}
			for (int i = 0; i < resizeableContextMenu.size(); i++) {
				resizeableContextMenu.get(i).calcIsHovering();
				resizeableContextMenu.get(i).setBounds((int) resizeableContextMenu.get(i).x, (int) resizeableContextMenu.get(i).y,
						resizeableContextMenu.get(i).width, resizeableContextMenu.get(i).height);
			}
			for (int i = 0; i < displayFPSContextMenu.size(); i++) {
				displayFPSContextMenu.get(i).calcIsHovering();
				displayFPSContextMenu.get(i).setBounds((int) displayFPSContextMenu.get(i).x, (int) displayFPSContextMenu.get(i).y,
						displayFPSContextMenu.get(i).width, displayFPSContextMenu.get(i).height);
			}
			for (int i = 0; i < showEnemyHealthContextMenu.size(); i++) {
				showEnemyHealthContextMenu.get(i).calcIsHovering();
				showEnemyHealthContextMenu.get(i).setBounds((int) showEnemyHealthContextMenu.get(i).x, (int) showEnemyHealthContextMenu.get(i).y,
						showEnemyHealthContextMenu.get(i).width, showEnemyHealthContextMenu.get(i).height);
			}
			for (int i = 0; i < resolutionContextMenu.size(); i++) {
				resolutionContextMenu.get(i).calcIsHovering();
				resolutionContextMenu.get(i).setBounds((int) resolutionContextMenu.get(i).x, (int) resolutionContextMenu.get(i).y,
						resolutionContextMenu.get(i).width, resolutionContextMenu.get(i).height);
			}
			for (int i = 0; i < fpsContextMenu.size(); i++) {
				fpsContextMenu.get(i).calcIsHovering();
				fpsContextMenu.get(i).setBounds((int) fpsContextMenu.get(i).x, (int) fpsContextMenu.get(i).y, fpsContextMenu.get(i).width,
						fpsContextMenu.get(i).height);
			}
		} catch (NullPointerException e) {
			if (nullCount > 5) {
				e.printStackTrace();
			} else nullCount++;
		}
	}

	public void render(Graphics2D g2d) {
		if(Game.state != STATE.SETTINGS) return;
		
		if (g2d == null) return;

		try { /* again, catch null pointers that happen occasionally when switching menus */

			// Main Buttons
			for (int i = 0; i < mainButtons.size(); i++) {
				if (!mainButtons.get(i).isHovering && mainButtons.get(i).isVisible)
					g2d.drawImage(Textures.settingsButtonSheet[mainButtons.get(i).type], (int) mainButtons.get(i).x, (int) mainButtons.get(i).y, null);
				else if (mainButtons.get(i).isVisible)
					g2d.drawImage(Textures.settingsButtonSheet[mainButtons.get(i).type + 1], (int) mainButtons.get(i).x, (int) mainButtons.get(i).y, null);
			}

			// Resizeable
			for (int i = 0; i < resizeableContextMenu.size(); i++) {
				if (!resizeableContextMenu.get(i).isHovering && resizeableContextMenu.get(i).isVisible & !resizeableContextMenu.get(i).otherContextMenuIsOpen)
					g2d.drawImage(Textures.settingsButtonSheet[resizeableContextMenu.get(i).type], (int) resizeableContextMenu.get(i).x,
							(int) resizeableContextMenu.get(i).y, null);
				else if (resizeableContextMenu.get(i).isVisible && !resizeableContextMenu.get(i).otherContextMenuIsOpen)
					g2d.drawImage(Textures.settingsButtonSheet[resizeableContextMenu.get(i).type + 1], (int) resizeableContextMenu.get(i).x,
							(int) resizeableContextMenu.get(i).y, null);
			}

			// DisplayFPS
			for (int i = 0; i < displayFPSContextMenu.size(); i++) {
				if (!displayFPSContextMenu.get(i).isHovering && displayFPSContextMenu.get(i).isVisible & !displayFPSContextMenu.get(i).otherContextMenuIsOpen)
					g2d.drawImage(Textures.settingsButtonSheet[displayFPSContextMenu.get(i).type], (int) displayFPSContextMenu.get(i).x,
							(int) displayFPSContextMenu.get(i).y, null);
				else if (displayFPSContextMenu.get(i).isVisible && !displayFPSContextMenu.get(i).otherContextMenuIsOpen)
					g2d.drawImage(Textures.settingsButtonSheet[displayFPSContextMenu.get(i).type + 1], (int) displayFPSContextMenu.get(i).x,
							(int) displayFPSContextMenu.get(i).y, null);
			}

			// Show Enemy Health Bars
			for (int i = 0; i < showEnemyHealthContextMenu.size(); i++) {
				if (!showEnemyHealthContextMenu.get(i).isHovering
						&& showEnemyHealthContextMenu.get(i).isVisible & !showEnemyHealthContextMenu.get(i).otherContextMenuIsOpen)
					g2d.drawImage(Textures.settingsButtonSheet[showEnemyHealthContextMenu.get(i).type], (int) showEnemyHealthContextMenu.get(i).x,
							(int) showEnemyHealthContextMenu.get(i).y, null);
				else if (showEnemyHealthContextMenu.get(i).isVisible && !showEnemyHealthContextMenu.get(i).otherContextMenuIsOpen)
					g2d.drawImage(Textures.settingsButtonSheet[showEnemyHealthContextMenu.get(i).type + 1], (int) showEnemyHealthContextMenu.get(i).x,
							(int) showEnemyHealthContextMenu.get(i).y, null);
			}

			// Resolutions
			for (int i = 0; i < resolutionContextMenu.size(); i++) {
				if (!resolutionContextMenu.get(i).isHovering && resolutionContextMenu.get(i).isVisible & !resolutionContextMenu.get(i).otherContextMenuIsOpen)
					g2d.drawImage(Textures.settingsButtonSheet[resolutionContextMenu.get(i).type], (int) resolutionContextMenu.get(i).x,
							(int) resolutionContextMenu.get(i).y, null);
				else if (resolutionContextMenu.get(i).isVisible && !resolutionContextMenu.get(i).otherContextMenuIsOpen)
					g2d.drawImage(Textures.settingsButtonSheet[resolutionContextMenu.get(i).type + 1], (int) resolutionContextMenu.get(i).x,
							(int) resolutionContextMenu.get(i).y, null);
			}

			// FPS's
			for (int i = 0; i < fpsContextMenu.size(); i++) {
				if (!fpsContextMenu.get(i).isHovering && fpsContextMenu.get(i).isVisible & !fpsContextMenu.get(i).otherContextMenuIsOpen)
					g2d.drawImage(Textures.settingsButtonSheet[fpsContextMenu.get(i).type], (int) fpsContextMenu.get(i).x, (int) fpsContextMenu.get(i).y, null);
				else if (fpsContextMenu.get(i).isVisible && !fpsContextMenu.get(i).otherContextMenuIsOpen)
					g2d.drawImage(Textures.settingsButtonSheet[fpsContextMenu.get(i).type + 1], (int) fpsContextMenu.get(i).x, (int) fpsContextMenu.get(i).y,
							null);
			}
		} catch (NullPointerException e) { // null pointer caught}
			if (nullCount > 5) {
				e.printStackTrace();
			} else nullCount++;
		}
	}

	public synchronized void createSettingsSaveFile() {
		try {
			SettingsSaveFile settings = new SettingsSaveFile(mainButtons, resizeableContextMenu, displayFPSContextMenu, showEnemyHealthContextMenu,
					resolutionContextMenu, fpsContextMenu, resizeableTrueFalseReverse, displayFPSTrueFalseReverse, showEnemyHealthBarsTrueFalseReverse);

			// Pass in the newly created save file
			Saving.saveSettingsSaveFile(settings);

			// Apply new settings to current game
			applySettings();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public synchronized void cleanUp() { /* is called when returning to main menu / quitting */
		try {
			mainButtons.clear();
			mainButtons = null;
			resizeableContextMenu.clear();
			resizeableContextMenu = null;
			displayFPSContextMenu.clear();
			displayFPSContextMenu = null;
			showEnemyHealthContextMenu.clear();
			showEnemyHealthContextMenu = null;
			resolutionContextMenu = null;
			fpsContextMenu.clear();
			fpsContextMenu = null;
			loadedSettings = null;
		} catch (Exception e) {}
	}

	private static int nullCount = 0;
}
