Merge pull request #37 from craftinc/refactor/serialization_cleanup

Serialization cleanup
This commit is contained in:
Tobias Ottenweller 2017-01-01 15:12:08 +01:00 committed by GitHub
commit ed5222e5fb
4 changed files with 12 additions and 171 deletions

View File

@ -41,7 +41,7 @@ public class GatesManager {
private static final String gatesPath = "gates"; // path to gates inside the yaml file private static final String gatesPath = "gates"; // path to gates inside the yaml file
private static final String storageVersionPath = "version"; private static final String storageVersionPath = "version";
private static final int storageVersion = 2; private static final int storageVersion = 3;
private File gatesConfigFile; private File gatesConfigFile;
private FileConfiguration gatesConfig; private FileConfiguration gatesConfig;

View File

@ -19,7 +19,6 @@ package de.craftinc.gates.models;
import de.craftinc.gates.Plugin; import de.craftinc.gates.Plugin;
import de.craftinc.gates.util.ConfigurationUtil; import de.craftinc.gates.util.ConfigurationUtil;
import de.craftinc.gates.util.FloodUtil; import de.craftinc.gates.util.FloodUtil;
import de.craftinc.gates.persistence.LocationUtil;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
@ -232,7 +231,6 @@ public class Gate implements ConfigurationSerializable {
} }
} }
/* /*
* INTERFACE: ConfigurationSerializable * INTERFACE: ConfigurationSerializable
*/ */
@ -242,44 +240,18 @@ public class Gate implements ConfigurationSerializable {
static private String exitKey = "exit"; static private String exitKey = "exit";
static private String isHiddenKey = "hidden"; static private String isHiddenKey = "hidden";
static private String isOpenKey = "open"; static private String isOpenKey = "open";
static private String locationYawKey = "locationYaw";
static private String locationPitchKey = "locationPitch";
static private String exitYawKey = "exitYaw";
static private String exitPitchKey = "exitPitch";
static private String allowsVehiclesKey = "allowsVehiclesKey"; static private String allowsVehiclesKey = "allowsVehiclesKey";
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Gate(Map<String, Object> map) { public Gate(Map<String, Object> map) {
try { try {
id = map.get(idKey).toString().toLowerCase(); id = map.get(idKey).toString().toLowerCase();
location = (Location) map.get(locationKey);
exit = (Location) map.get(exitKey);
isHidden = (Boolean) map.get(isHiddenKey); isHidden = (Boolean) map.get(isHiddenKey);
isOpen = (Boolean) map.get(isOpenKey); isOpen = (Boolean) map.get(isOpenKey);
allowsVehicles = (Boolean) map.get(allowsVehiclesKey);
location = LocationUtil.deserializeLocation((Map<String, Object>) map.get(locationKey)); gateBlockLocations = (Set<Location>) map.get(gateBlocksKey);
exit = LocationUtil.deserializeLocation((Map<String, Object>) map.get(exitKey));
if (map.containsKey(exitPitchKey)) {
exit.setPitch(((Number) map.get(exitPitchKey)).floatValue());
exit.setYaw(((Number) map.get(exitYawKey)).floatValue());
}
if (map.containsKey(locationPitchKey)) {
location.setPitch(((Number) map.get(locationPitchKey)).floatValue());
location.setYaw(((Number) map.get(locationYawKey)).floatValue());
}
if (map.containsKey(allowsVehiclesKey)) {
allowsVehicles = (Boolean) map.get(allowsVehiclesKey);
}
gateBlockLocations = new HashSet<>();
List<Map<String, Object>> serializedGateBlocks = (List<Map<String, Object>>) map.get(gateBlocksKey);
for (Map<String, Object> sgb : serializedGateBlocks) {
gateBlockLocations.add(LocationUtil.deserializeLocation(sgb));
}
gateFrameBlocks = FloodUtil.getFrameWithLocations(gateBlockLocations); gateFrameBlocks = FloodUtil.getFrameWithLocations(gateBlockLocations);
} catch (Exception e) { } catch (Exception e) {
@ -296,35 +268,23 @@ public class Gate implements ConfigurationSerializable {
} }
} }
public Map<String, Object> serialize() { public Map<String, Object> serialize() {
Map<String, Object> retVal = new HashMap<>(); Map<String, Object> retVal = new HashMap<>();
retVal.put(idKey, id); retVal.put(idKey, id);
retVal.put(locationKey, LocationUtil.serializeLocation(location));
retVal.put(exitKey, LocationUtil.serializeLocation(exit));
retVal.put(isHiddenKey, isHidden); retVal.put(isHiddenKey, isHidden);
retVal.put(isOpenKey, isOpen); retVal.put(isOpenKey, isOpen);
retVal.put(allowsVehiclesKey, allowsVehicles); retVal.put(allowsVehiclesKey, allowsVehicles);
retVal.put(gateBlocksKey, gateBlockLocations);
if (exit != null) { if (exit != null) {
retVal.put(exitPitchKey, exit.getPitch()); retVal.put(exitKey, exit.serialize());
retVal.put(exitYawKey, exit.getYaw());
} }
if (location != null) { if (location != null) {
retVal.put(locationPitchKey, location.getPitch()); retVal.put(locationKey, location.serialize());
retVal.put(locationYawKey, location.getYaw());
} }
List<Map<String, Object>> serializedGateBlocks = new ArrayList<>();
for (Location l : gateBlockLocations) {
serializedGateBlocks.add(LocationUtil.serializeLocation(l));
}
retVal.put(gateBlocksKey, serializedGateBlocks);
return retVal; return retVal;
} }
} }

View File

@ -1,93 +0,0 @@
/* Craft Inc. Gates
Copyright (C) 2011-2013 Craft Inc. Gates Team (see AUTHORS.txt)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program (LGPLv3). If not, see <http://www.gnu.org/licenses/>.
*/
package de.craftinc.gates.persistence;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.Location;
import org.bukkit.World;
import de.craftinc.gates.Plugin;
public class LocationUtil {
private final static String worldKey = "world";
private final static String xKey = "x";
private final static String yKey = "y";
private final static String zKey = "z";
private static World getWorld(final String name) throws Exception {
if (name == null) {
throw new IllegalArgumentException("The name of the world must not be 'null");
}
World world = Plugin.getPlugin().getServer().getWorld(name);
if (world == null) {
throw new Exception("World '" + name + "' does not exists anymore! Cannot get instance!");
}
return world;
}
/**
* Serializes a location. Helps storing locations inside yaml files. NOTE: We do not care about yaw
* and pitch for gate locations. So we won't serialize them.
*
* @param l The location to serialize. Supplying 'null' is ok..
* @return A Map object ready for storing inside a yaml file. Will return 'null' if 'l' is null.
*/
public static Map<String, Object> serializeLocation(final Location l) {
if (l == null) {
return null;
}
Map<String, Object> serializedLocation = new HashMap<>();
serializedLocation.put(worldKey, l.getWorld().getName());
serializedLocation.put(xKey, l.getX());
serializedLocation.put(yKey, l.getY());
serializedLocation.put(zKey, l.getZ());
return serializedLocation;
}
/**
* @param map A map generated with the 'serializeLocation' method. Supplying 'null' is ok.
* @return A deserialized location. This method will return 'null' if 'map' is null!
* @throws Exception This method will throw an exception if the world of the supplied serialized location
* does not exist or if 'map' does not contain all necessary keys!
*/
public static Location deserializeLocation(final Map<String, Object> map) throws Exception {
if (map == null) {
return null;
}
World w = getWorld((String) map.get(worldKey));
Number x = (Number) map.get(xKey);
Number y = (Number) map.get(yKey);
Number z = (Number) map.get(zKey);
if (x == null || y == null || z == null) {
throw new IllegalArgumentException("Supplied map is invalid x, y or z coordinate was not supplied");
}
return new Location(w, x.doubleValue(), y.doubleValue(), z.doubleValue());
}
}

View File

@ -30,37 +30,11 @@ import java.util.logging.Level;
public class MigrationUtil { public class MigrationUtil {
public static boolean performMigration(int storageVersion, int currentVersion, List<Gate> gates) { public static boolean performMigration(int storageVersion, int currentVersion, List<Gate> gates) {
if (storageVersion == 0 && currentVersion >= 2) { if (storageVersion != currentVersion) {
removePortalBlocks(gates); Plugin.log(Level.SEVERE, "Supplied storage version is currently not supported!" +
updateAllowVehicles(gates); "Make sure you have the latest version of Craft Inc. Gates installed. Plugin will be disabled!");
return true;
} else if (storageVersion == 1 && currentVersion >= 2) {
updateAllowVehicles(gates);
return true;
} else {
Plugin.log(Level.SEVERE, "Supplied storage version is currently not supported! Make sure you have the latest version of Craft Inc. Gates installed. Plugin will be disabled!");
return false; return false;
} }
} return true;
private static void removePortalBlocks(List<Gate> gates) {
for (Gate g : gates) {
for (Location l : g.getGateBlockLocations()) {
Block b = l.getBlock();
if (b.getType() == Material.PORTAL) {
b.setType(Material.AIR);
}
}
}
}
private static void updateAllowVehicles(List<Gate> gates) {
for (Gate g : gates) {
g.setAllowsVehicles(true);
}
} }
} }