Skip to content

Instantly share code, notes, and snippets.

@Zren
Created July 1, 2012 01:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zren/3026337 to your computer and use it in GitHub Desktop.
Save Zren/3026337 to your computer and use it in GitHub Desktop.
package ca.xshade.bukkit.util.config;
import ca.xshade.bukkit.util.BukkitUtil;
import com.pridemc.games.arena.CuboidRegion;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.configuration.serialization.ConfigurationSerialization;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Author: Chris H (Zren / Shade)
* Date: 6/18/12
*/
public class Config extends YamlConfiguration {
static {
ConfigurationSerialization.registerClass(CuboidRegion.class);
ConfigurationSerialization.registerClass(LocationSerializable.class);
ConfigurationSerialization.registerClass(WorldVector.class);
}
private File file = new File("");
public Config(File file) {
if (!file.isFile())
throw new IllegalArgumentException("Path must be a file.");
this.file = file;
}
public void save() {
try {
save(file);
} catch (IOException e) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE,
String.format("Could not save config file '%s'.", file.getPath()),
e);
}
}
public void load() {
try {
load(file);
} catch (IOException e) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE,
String.format("Could not load config file '%s'.", file.getPath()),
e);
} catch (InvalidConfigurationException e) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE,
e.getMessage(),
e);
}
}
public Location getLocation(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object def = getDefault(path);
return getLocation(path, (def instanceof LocationSerializable) ? ((LocationSerializable)def).toLocation() : null);
}
public Location getLocation(String path, Location def) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object val = get(path, def);
return (val instanceof LocationSerializable) ? ((LocationSerializable)val).toLocation() : def;
}
public boolean isLocation(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object val = get(path);
return val instanceof LocationSerializable;
}
public World getWorld(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object def = getDefault(path);
return getWorld(path, (def instanceof World) ? (World) def : null);
}
public World getWorld(String path, World def) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object val = get(path, def);
return (val instanceof String) ? BukkitUtil.getWorldFromName((String)val) : def;
}
@Override
public void set(String path, Object value) {
set(path, value, true);
}
public void set(String path, Object value, boolean save) {
if (value instanceof Location) {
super.set(path, new LocationSerializable((Location)value));
} else if (value instanceof World) {
super.set(path, ((World)value).getName());
} else {
super.set(path, value);
}
if (save)
save();
}
public WorldVector getWorldVector(String path) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object def = getDefault(path);
return getWorldVector(path, (def instanceof WorldVector) ? (WorldVector) def : null);
}
public WorldVector getWorldVector(String path, WorldVector def) {
if (path == null) {
throw new IllegalArgumentException("Path cannot be null");
}
Object val = get(path, def);
return (val instanceof WorldVector) ? (WorldVector)val : def;
}
public List<WorldVector> getWorldVectorList(String path) {
List<?> list = getList(path);
if (list == null) {
return new ArrayList<WorldVector>(0);
}
List<WorldVector> result = new ArrayList<WorldVector>();
for (Object object : list) {
if (object instanceof WorldVector) {
result.add((WorldVector)object);
}
}
return result;
}
public List<String> getStringList(String path) {
List<?> list = getList(path);
if (list == null) {
return new ArrayList<String>(0);
}
List<String> result = new ArrayList<String>();
for (Object object : list) {
if ((object instanceof String) || (isPrimitiveWrapper(object))) {
result.add(String.valueOf(object));
}
}
return result;
}
}
package ca.xshade.bukkit.util.config;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.serialization.SerializableAs;
import org.bukkit.util.Vector;
import java.util.Map;
/**
* Author: Chris H (Zren / Shade)
* Date: 6/18/12
*/
@SerializableAs("Location")
public class LocationSerializable extends WorldVector {
private float yaw;
private float pitch;
public LocationSerializable(WorldVector worldVector) {
this(worldVector, 0, 0);
}
public LocationSerializable(WorldVector worldVector, float yaw, float pitch) {
this(worldVector.getWorld(), worldVector.getX(), worldVector.getY(), worldVector.getZ(), yaw, pitch);
}
public LocationSerializable(Location location) {
this(location.getWorld(), location.toVector(), location.getYaw(), location.getPitch());
}
public LocationSerializable(World world, Vector vector, float yaw, float pitch) {
this(world, vector.getX(), vector.getY(), vector.getZ(), yaw, pitch);
}
public LocationSerializable(World world, double x, double y, double z, float yaw, float pitch) {
super(world, x, y, z);
setYaw(yaw);
setPitch(pitch);
}
public float getYaw() {
return yaw;
}
public void setYaw(float yaw) {
this.yaw = yaw;
}
public float getPitch() {
return pitch;
}
public void setPitch(float pitch) {
this.pitch = pitch;
}
public Location toLocation() {
return toLocation(getWorld(), getYaw(), getPitch());
}
@Override
public Map<String, Object> serialize() {
Map<String, Object> result = super.serialize();
result.put("yaw", getYaw());
result.put("pitch", getPitch());
return result;
}
public static LocationSerializable deserialize(Map<String, Object> args) {
float yaw = 0;
float pitch = 0;
if (args.containsKey("yaw")) {
yaw = ((Double)args.get("yaw")).floatValue();
}
if (args.containsKey("pitch")) {
pitch = ((Double)args.get("pitch")).floatValue();
}
return new LocationSerializable(WorldVector.deserialize(args), yaw, pitch);
}
}
package ca.xshade.bukkit.util.config;
/**
* Author: Chris H (Zren / Shade)
* Date: 6/18/12
*/
import ca.xshade.bukkit.util.BukkitUtil;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.serialization.SerializableAs;
import org.bukkit.util.Vector;
import java.util.Map;
@SerializableAs("WorldVector")
public class WorldVector extends Vector {
private World world;
public WorldVector(Location location) {
this(location.getWorld(), location.getX(), location.getY(), location.getZ());
}
public WorldVector(World world, Vector vector) {
this(world, vector.getX(), vector.getY(), vector.getZ());
}
public WorldVector(World world, double x, double y, double z) {
super(x, y, z);
if (world == null)
throw new IllegalArgumentException();
this.world = world;
}
public World getWorld() {
return world;
}
public void setWorld(World world) {
this.world = world;
}
public Vector toVector() {
return super.copy(this);
}
public WorldVector floorVector() {
return new WorldVector(getWorld(), getBlockX(), getBlockY(), getBlockZ());
}
@Override
public Map<String, Object> serialize() {
Map<String, Object> result = super.serialize();
result.put("world", getWorld().getName());
return result;
}
public static WorldVector deserialize(Map<String, Object> args) {
World world = null;
Vector vector = null;
if (args.containsKey("world")) {
world = BukkitUtil.getWorldFromName((String) args.get("world"));
}
vector = Vector.deserialize(args);
return new WorldVector(world, vector);
}
public Location toLocation() {
return toLocation(getWorld());
}
@Override
public String toString() {
return getWorld().getName() + "," + super.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment