2016-02-18 17:25:11 +01:00
|
|
|
package WayofTime.bloodmagic.ritual.portal;
|
|
|
|
|
2017-08-14 20:53:42 -07:00
|
|
|
import WayofTime.bloodmagic.BloodMagic;
|
2018-02-15 18:49:01 -08:00
|
|
|
import WayofTime.bloodmagic.teleport.PortalLocation;
|
2018-02-16 23:48:28 -08:00
|
|
|
import WayofTime.bloodmagic.util.BMLog;
|
2016-03-17 13:00:44 -07:00
|
|
|
import net.minecraftforge.common.DimensionManager;
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
2016-02-18 17:25:11 +01:00
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public class LocationsHandler implements Serializable {
|
2016-02-18 17:25:11 +01:00
|
|
|
|
|
|
|
public static final long serialVersionUID = 10102001;
|
2017-08-14 20:53:42 -07:00
|
|
|
private static final String fileName = String.valueOf(DimensionManager.getCurrentSaveRootDirectory()) + "/" + BloodMagic.MODID + "/PortalLocations.dat";
|
2016-02-18 17:25:11 +01:00
|
|
|
private static HashMap<String, ArrayList<PortalLocation>> portals;
|
|
|
|
private static LocationsHandler locationsHandler;
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
private LocationsHandler() {
|
2018-03-01 19:27:38 -08:00
|
|
|
portals = new HashMap<>();
|
2016-02-18 17:25:11 +01:00
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public boolean addLocation(String name, PortalLocation location) {
|
|
|
|
ArrayList<PortalLocation> portalLocations = portals.get(name);
|
|
|
|
if (portalLocations == null) {
|
2018-03-01 19:27:38 -08:00
|
|
|
portals.put(name, new ArrayList<>());
|
2017-08-15 21:30:48 -07:00
|
|
|
updateFile(fileName, portals);
|
|
|
|
}
|
|
|
|
if (!portals.get(name).isEmpty() && portals.get(name).size() >= 2) {
|
2018-02-16 23:48:28 -08:00
|
|
|
BMLog.DEBUG.info("Location {} already exists.", name);
|
2017-08-15 21:30:48 -07:00
|
|
|
updateFile(fileName, portals);
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
portals.get(name).add(location);
|
2018-02-16 23:48:28 -08:00
|
|
|
BMLog.DEBUG.info("Adding {}", name);
|
2017-08-15 21:30:48 -07:00
|
|
|
updateFile(fileName, portals);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean removeLocation(String name, PortalLocation location) {
|
|
|
|
if (portals.get(name) != null && !portals.get(name).isEmpty()) {
|
|
|
|
if (portals.get(name).contains(location)) {
|
|
|
|
portals.get(name).remove(location);
|
2018-02-16 23:48:28 -08:00
|
|
|
BMLog.DEBUG.info("Removing {}", name);
|
2017-08-15 21:30:48 -07:00
|
|
|
updateFile(fileName, portals);
|
|
|
|
return true;
|
|
|
|
} else {
|
2018-02-16 23:48:28 -08:00
|
|
|
BMLog.DEBUG.info("No location matching {}", name);
|
2017-08-15 21:30:48 -07:00
|
|
|
updateFile(fileName, portals);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ArrayList<PortalLocation> getLinkedLocations(String name) {
|
|
|
|
return portals.get(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static LocationsHandler getLocationsHandler() {
|
|
|
|
if (locationsHandler == null || loadFile() == null) {
|
2016-02-18 17:25:11 +01:00
|
|
|
locationsHandler = new LocationsHandler();
|
|
|
|
return locationsHandler;
|
2017-08-15 21:30:48 -07:00
|
|
|
} else {
|
2016-02-18 17:25:11 +01:00
|
|
|
portals = loadFile();
|
|
|
|
return locationsHandler;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
private static HashMap<String, ArrayList<PortalLocation>> loadFile() {
|
2016-02-18 17:25:11 +01:00
|
|
|
HashMap<String, ArrayList<PortalLocation>> map;
|
|
|
|
File file = new File(fileName);
|
2017-08-15 21:30:48 -07:00
|
|
|
try {
|
|
|
|
if (!file.exists()) {
|
|
|
|
if (file.getParentFile().mkdir()) {
|
|
|
|
if (file.createNewFile()) {
|
2018-02-16 23:48:28 -08:00
|
|
|
BMLog.DEBUG.info("Creating {} in {}", fileName, DimensionManager.getCurrentSaveRootDirectory());
|
2016-02-18 17:25:11 +01:00
|
|
|
}
|
2017-08-15 21:30:48 -07:00
|
|
|
} else if (file.createNewFile()) {
|
2018-02-16 23:48:28 -08:00
|
|
|
BMLog.DEBUG.info("Creating {} in {}", fileName, DimensionManager.getCurrentSaveRootDirectory());
|
2016-02-18 17:25:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
FileInputStream fileIn = new FileInputStream(file);
|
|
|
|
ObjectInputStream in = new ObjectInputStream(fileIn);
|
|
|
|
map = (HashMap<String, ArrayList<PortalLocation>>) in.readObject();
|
|
|
|
in.close();
|
|
|
|
fileIn.close();
|
|
|
|
return map;
|
2017-08-15 21:30:48 -07:00
|
|
|
} catch (IOException e) {
|
2016-02-18 17:25:11 +01:00
|
|
|
return null;
|
2017-08-15 21:30:48 -07:00
|
|
|
} catch (ClassNotFoundException e) {
|
2018-02-16 23:48:28 -08:00
|
|
|
BMLog.DEFAULT.error("{} was not found in {}", file, DimensionManager.getCurrentSaveRootDirectory());
|
2016-02-18 17:25:11 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
private static void updateFile(String file, HashMap<String, ArrayList<PortalLocation>> object) {
|
|
|
|
try {
|
2016-02-18 17:25:11 +01:00
|
|
|
FileOutputStream fos = new FileOutputStream(file);
|
|
|
|
ObjectOutputStream oos = new ObjectOutputStream(fos);
|
|
|
|
oos.writeObject(object);
|
|
|
|
oos.close();
|
2017-08-15 21:30:48 -07:00
|
|
|
} catch (IOException e) {
|
2016-02-18 17:25:11 +01:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|