Created
January 21, 2015 13:40
-
-
Save Ivorforce/e58a8a80c2b47e38af0b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2014, Lukas Tenbrink. | |
* * http://lukas.axxim.net | |
*/ | |
package ivorius.reccomplex.dimensions; | |
import gnu.trove.map.TIntObjectMap; | |
import gnu.trove.map.hash.TIntObjectHashMap; | |
import java.util.*; | |
/** | |
* Handles tags for dimensions, in a way similar to {@link net.minecraftforge.common.BiomeDictionary}. | |
* | |
* A type can have subtypes and super-types. | |
* A dimension matches a type when it is associated with either the type or any of its sub-types. | |
*/ | |
public class DimensionDictionary | |
{ | |
private static final TIntObjectMap<Set<String>> dimensionTypes = new TIntObjectHashMap<>(); | |
private static final Map<String, Type> types = new HashMap<>(); | |
public static final String MC_DEFAULT = "MC_DEFAULT"; | |
public static final String REAL = "REAL"; | |
public static final String UNREAL = "UNREAL"; | |
public static final String IMAGINARY = "IMAGINARY"; | |
public static final String SIMULATED = "SIMULATED"; | |
public static final String ABSTRACT = "ABSTRACT"; | |
public static final String OVERWORLD = "OVERWORLD"; | |
public static final String SKY = "SKY"; | |
public static final String EARTH = "EARTH"; | |
public static final String HELL = "HELL"; | |
public static final String ENDER = "ENDER"; | |
static | |
{ | |
registerSubtypes(UNREAL, Arrays.asList(IMAGINARY, SIMULATED, ABSTRACT)); | |
registerSubtypes(OVERWORLD, Arrays.asList(EARTH)); | |
registerSubtypes(SKY, Arrays.asList(ENDER)); | |
registerDimensionTypes(0, Arrays.asList(EARTH, REAL, MC_DEFAULT)); | |
registerDimensionTypes(-1, Arrays.asList(HELL, REAL, MC_DEFAULT)); | |
registerDimensionTypes(1, Arrays.asList(ENDER, REAL, MC_DEFAULT)); | |
} | |
/** | |
* Registers a dimension for dimension types. | |
* This will also register each of the used types, if they weren't already. | |
* | |
* @param dimensionID The ID of the dimension. | |
* @param types The dimension types. | |
*/ | |
public static void registerDimensionTypes(int dimensionID, Collection<String> types) | |
{ | |
Set<String> dTypes = dimensionTypes.get(dimensionID); | |
if (dTypes == null) | |
{ | |
dTypes = new HashSet<>(); | |
dimensionTypes.put(dimensionID, dTypes); | |
} | |
dTypes.addAll(types); | |
for (String type : types) | |
registerType(type); | |
} | |
/** | |
* Registers a dimension type. This is required for the GUIs to suggest the type to the user. | |
* It is recommended that you make all your types uppercase, although all checks will be done ignoring the case. | |
* | |
* @param type The type to register. | |
*/ | |
public static void registerType(String type) | |
{ | |
if (!types.containsKey(type)) | |
types.put(type, new Type()); | |
} | |
/** | |
* Adds subtypes to a specific type. | |
* This will also register any used types, if they weren't already. | |
* | |
* @param type The type. | |
* @param subTypes The subtypes. | |
*/ | |
public static void registerSubtypes(String type, Collection<String> subTypes) | |
{ | |
registerGetType(type).subTypes.addAll(subTypes); | |
for (String sub : subTypes) | |
registerGetType(sub).superTypes.add(type); | |
} | |
/** | |
* Adds the specified type as a subtype to each of the collection's type. | |
* This will also register any used types, if they weren't already. | |
* | |
* @param type The type. | |
* @param subTypes The supertypes. | |
*/ | |
public static void registerSupertypes(String type, Collection<String> subTypes) | |
{ | |
registerGetType(type).superTypes.addAll(subTypes); | |
for (String sub : subTypes) | |
registerGetType(sub).subTypes.add(type); | |
} | |
/** | |
* Returns a set of types a specific dimension was registered for. | |
* | |
* @param dimensionID The dimension's ID. | |
* @return The dimension's types. | |
*/ | |
public static Set<String> getDimensionTypes(int dimensionID) | |
{ | |
Set<String> types = dimensionTypes.get(dimensionID); | |
return types != null ? Collections.unmodifiableSet(types) : Collections.<String>emptySet(); | |
} | |
/** | |
* Determines if a dimension matches all types from a collection of types. | |
* | |
* @param dimensionID The dimension's ID. | |
* @param types The types. | |
* @return True if all types were matched, otherwise false. | |
*/ | |
public static boolean doesDimensionMatchTypes(int dimensionID, Collection<String> types) | |
{ | |
for (String type : types) | |
{ | |
if (!doesDimensionMatchType(dimensionID, type)) | |
return false; | |
} | |
return true; | |
} | |
/** | |
* Determines if a dimension matches a specific type. | |
* This is the case exactly when the dimension is associated with either the type or any of its subtypes. | |
* | |
* @param dimensionID The dimension's ID. | |
* @param type The type. | |
* @return True if the dimension matches the type, otherwise false. | |
*/ | |
public static boolean doesDimensionMatchType(int dimensionID, String type) | |
{ | |
Set<String> dimTypes = dimensionTypes.get(dimensionID); | |
if (dimTypes == null) | |
return false; | |
Queue<String> curTypes = new ArrayDeque<>(); | |
curTypes.add(type); | |
String curType; | |
while ((curType = curTypes.poll()) != null) | |
{ | |
if (dimTypes.contains(curType.toUpperCase())) | |
return true; | |
Type curT = types.get(curType); | |
if (curT != null) | |
curTypes.addAll(curT.subTypes); | |
} | |
return false; | |
} | |
private static Type registerGetType(String type) | |
{ | |
registerType(type); | |
return types.get(type); | |
} | |
private static class Type | |
{ | |
public final Set<String> superTypes = new HashSet<>(); | |
public final Set<String> subTypes = new HashSet<>(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment