Added external celestial objects registraion

This is a temporary fix for MMOcore until a proper API is defined
This commit is contained in:
LemADEC 2017-05-07 13:20:14 +02:00
parent 4e292c214b
commit e7e774b88d

View file

@ -14,7 +14,7 @@ import net.minecraft.util.AxisAlignedBB;
public class CelestialObjectManager extends XmlFileManager {
private static CelestialObjectManager INSTANCE = new CelestialObjectManager();
private static final CelestialObjectManager INSTANCE = new CelestialObjectManager();
private static HashMap<String, HashMap<String, CelestialObject>> celestialObjectsByGroup = new HashMap<>();
public static CelestialObject[] celestialObjects = null;
@ -23,9 +23,29 @@ public class CelestialObjectManager extends XmlFileManager {
celestialObjectsByGroup = new HashMap<>();
}
public static void load(File dir) {
public static void load(final File dir) {
INSTANCE.load(dir, "celestialObjects", "celestialObject");
INSTANCE.rebuildAndValidate();
}
// @TODO add a proper API
public static void updateInRegistry(final CelestialObject celestialObject) {
INSTANCE.addOrUpdateInRegistry(celestialObject, true);
INSTANCE.rebuildAndValidate();
}
private void addOrUpdateInRegistry(final CelestialObject celestialObject, final boolean isUpdating) {
final HashMap<String, CelestialObject> celestialObjectByName = celestialObjectsByGroup.computeIfAbsent(celestialObject.group, k -> new HashMap<>());
final CelestialObject celestialObjectExisting = celestialObjectByName.get(celestialObject.name);
if (celestialObjectExisting == null || isUpdating) {
celestialObjectByName.put(celestialObject.name, celestialObject);
} else {
WarpDrive.logger.warn(String.format("Celestial object %s is already defined, keeping original definition", celestialObject.getFullName()));
}
}
private void rebuildAndValidate() {
// optimize execution speed by flattening the data structure
int count = 0;
for(HashMap<String, CelestialObject> mapCelestialObjectByName : celestialObjectsByGroup.values()) {
@ -48,8 +68,8 @@ public class CelestialObjectManager extends XmlFileManager {
}
// are they overlapping in a common parent dimension?
if (!celestialObject1.isHyperspace() && !celestialObject2.isHyperspace() && celestialObject1.parentDimensionId == celestialObject2.parentDimensionId) {
AxisAlignedBB areaInParent1 = celestialObject1.getAreaInParent();
AxisAlignedBB areaInParent2 = celestialObject2.getAreaInParent();
final AxisAlignedBB areaInParent1 = celestialObject1.getAreaInParent();
final AxisAlignedBB areaInParent2 = celestialObject2.getAreaInParent();
if (areaInParent1.intersectsWith(areaInParent2)) {
WarpDrive.logger.error("Overlapping parent areas detected " + celestialObject1.parentDimensionId);
WarpDrive.logger.error("Celestial object 1 is " + celestialObject1 + " with area " + areaInParent1);
@ -61,8 +81,8 @@ public class CelestialObjectManager extends XmlFileManager {
}
// are they in the same dimension?
if (!celestialObject1.isVirtual && !celestialObject2.isVirtual && celestialObject1.dimensionId == celestialObject2.dimensionId) {
AxisAlignedBB areaToReachParent1 = celestialObject1.getAreaToReachParent();
AxisAlignedBB areaToReachParent2 = celestialObject2.getAreaToReachParent();
final AxisAlignedBB areaToReachParent1 = celestialObject1.getAreaToReachParent();
final AxisAlignedBB areaToReachParent2 = celestialObject2.getAreaToReachParent();
if (areaToReachParent1.intersectsWith(areaToReachParent2)) {
WarpDrive.logger.error("Overlapping areas detected in dimension " + celestialObject1.dimensionId);
WarpDrive.logger.error("Celestial object 1 is " + celestialObject1 + " with area " + areaToReachParent1);
@ -83,31 +103,24 @@ public class CelestialObjectManager extends XmlFileManager {
parseCelestiaObjectElement(location, elementCelestialObject, "", "");
}
protected void parseCelestiaObjectElement(final String location, final Element elementCelestialObject, final String groupParent, final String nameParent) throws InvalidXmlException, SAXException, IOException {
CelestialObject celestialObjectRead = new CelestialObject(location, groupParent, nameParent, elementCelestialObject);
private void parseCelestiaObjectElement(final String location, final Element elementCelestialObject, final String groupParent, final String nameParent) throws InvalidXmlException, SAXException, IOException {
final CelestialObject celestialObjectRead = new CelestialObject(location, groupParent, nameParent, elementCelestialObject);
HashMap<String, CelestialObject> celestialObjectByName = celestialObjectsByGroup.computeIfAbsent(celestialObjectRead.group, k -> new HashMap<>());
CelestialObject celestialObjectExisting = celestialObjectByName.get(celestialObjectRead.name);
if (celestialObjectExisting == null) {
celestialObjectByName.put(celestialObjectRead.name, celestialObjectRead);
} else {
WarpDrive.logger.warn(String.format("Celestial object %s is already defined, keeping original definition", celestialObjectRead.getFullName()));
}
addOrUpdateInRegistry(celestialObjectRead, false);
// look for optional child element(s)
List<Element> listChildren = XmlFileManager.getChildrenElementByTagName(elementCelestialObject, "celestialObject");
final List<Element> listChildren = XmlFileManager.getChildrenElementByTagName(elementCelestialObject, "celestialObject");
if (!listChildren.isEmpty()) {
for (int indexElement = 0; indexElement < listChildren.size(); indexElement++) {
Element elementChild = listChildren.get(indexElement);
String locationChild = String.format("%s Celestial object %s > child %d/%d", location, celestialObjectRead.getFullName(), indexElement + 1, listChildren.size());
final Element elementChild = listChildren.get(indexElement);
final String locationChild = String.format("%s Celestial object %s > child %d/%d", location, celestialObjectRead.getFullName(), indexElement + 1, listChildren.size());
parseCelestiaObjectElement(locationChild, elementChild, celestialObjectRead.group, celestialObjectRead.name);
}
}
}
public static CelestialObject get(final String group, final String name) {
HashMap<String, CelestialObject> celestialObjectByName = celestialObjectsByGroup.get(group);
final HashMap<String, CelestialObject> celestialObjectByName = celestialObjectsByGroup.get(group);
if (celestialObjectByName == null) {
return null;
}