package com.ezardlabs.dethsquare.prefabs;

import com.ezardlabs.dethsquare.GameObject;

import java.util.HashMap;

public class PrefabManager {
	private static HashMap<String, PrefabCreator> prefabs = new HashMap<>();


	/**
	 * Register a prefabricated {@link GameObject}
	 *
	 * @param name   The name to identify the prefab with
	 * @param prefab The {@link PrefabCreator} used to create the GameObject
	 */
	public static void addPrefab(String name, PrefabCreator prefab) {
		if (name.endsWith("_other")) {
			throw new Error("Prefab name cannot end with \"_other\"");
		} else {
			prefabs.put(name, prefab);
		}
	}

	/**
	 * Register a prefabricated {@link GameObject} that can be instantiated across clients
	 *
	 * @param name        The name to identify the prefab with
	 * @param prefab      The {@link PrefabCreator} used to create the GameObject
	 * @param otherPrefab An alternate {@link PrefabCreator} to be used for instantiation on
	 *                    every client except the caller - for example, you could have a
	 *                    prefab for a player object that has a script that accepts input on
	 *                    one client, but not on other clients. Can be null, in which case the
	 *                    same {@link PrefabCreator} will be used on all clients
	 */
	public static void addPrefab(String name, PrefabCreator prefab, PrefabCreator otherPrefab) {
		if (name.endsWith("_other")) {
			throw new IllegalArgumentException("Prefab name cannot end with '_other'");
		} else {
			prefabs.put(name, prefab);
			prefabs.put(name + "_other", otherPrefab);
		}
	}

	/**
	 * Check whether or not a prefab exists
	 *
	 * @param name The name of the prefab
	 * @return Whether or not a prefab with the given name exists
	 */
	public static boolean prefabExists(String name) {
		return prefabs.containsKey(name);
	}

	/**
	 * Load the prefab with the given name
	 *
	 * @param name The name of the prefab to load
	 * @return The {@link GameObject} prefab with the given name
	 */
	public static GameObject loadPrefab(String name) {
		return prefabs.get(name).create();
	}

	/**
	 * Unregisters the prefab with the given name
	 *
	 * @param name The name of the prefab to unregister
	 */
	public static void removePrefab(String name) {
		prefabs.remove(name);
	}
}

@@ -2050,34 +2050,180 @@
 tring name) %7B%0A%09%09
+P
 re
+fabCreator prefab = prefabs.get(name);%0A%09%09if (prefab == null) %7B%0A%09%09%09throw new RuntimeException(%22Prefab '%22 + name + %22' not found%22);%0A%09%09%7D else %7B%0A%09%09%09re
 turn prefabs.get
@@ -2217,27 +2217,16 @@
 n prefab
-s.get(name)
 .create(
@@ -2228,16 +2228,20 @@
 eate();%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09/**

Check that a prefab actually exists before trying to create it