mirror of
https://github.com/PaperMC/Paper.git
synced 2025-01-07 16:57:42 +01:00
Make Location ConfigurationSerializable.
By: Jofkos <JofkosDE@gmail.com>
This commit is contained in:
parent
6937ffacd4
commit
2fa0da6fd3
@ -1,13 +1,17 @@
|
||||
package org.bukkit;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||
import org.bukkit.util.NumberConversions;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
/**
|
||||
* Represents a 3-dimensional position in a world
|
||||
*/
|
||||
public class Location implements Cloneable {
|
||||
public class Location implements Cloneable, ConfigurationSerializable {
|
||||
private World world;
|
||||
private double x;
|
||||
private double y;
|
||||
@ -560,4 +564,36 @@ public class Location implements Cloneable {
|
||||
public static int locToBlock(double loc) {
|
||||
return NumberConversions.floor(loc);
|
||||
}
|
||||
|
||||
@Utility
|
||||
public Map<String, Object> serialize() {
|
||||
Map<String, Object> data = new HashMap<String, Object>();
|
||||
data.put("world", this.world.getName());
|
||||
|
||||
data.put("x", this.x);
|
||||
data.put("y", this.y);
|
||||
data.put("z", this.z);
|
||||
|
||||
data.put("yaw", this.yaw);
|
||||
data.put("pitch", this.pitch);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Required method for deserialization
|
||||
*
|
||||
* @param args map to deserialize
|
||||
* @return deserialized location
|
||||
* @throws IllegalArgumentException if the world don't exists
|
||||
* @see ConfigurationSerializable
|
||||
*/
|
||||
public static Location deserialize(Map<String, Object> args) {
|
||||
World world = Bukkit.getWorld((String) args.get("world"));
|
||||
if (world == null) {
|
||||
throw new IllegalArgumentException("unknown world");
|
||||
}
|
||||
|
||||
return new Location(world, NumberConversions.toDouble(args.get("x")), NumberConversions.toDouble(args.get("y")), NumberConversions.toDouble(args.get("z")), NumberConversions.toFloat(args.get("yaw")), NumberConversions.toFloat(args.get("pitch")));
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ import java.util.logging.Logger;
|
||||
import org.apache.commons.lang.Validate;
|
||||
import org.bukkit.Color;
|
||||
import org.bukkit.FireworkEffect;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.block.banner.Pattern;
|
||||
import org.bukkit.configuration.Configuration;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
@ -35,6 +36,7 @@ public class ConfigurationSerialization {
|
||||
registerClass(PotionEffect.class);
|
||||
registerClass(FireworkEffect.class);
|
||||
registerClass(Pattern.class);
|
||||
registerClass(Location.class);
|
||||
}
|
||||
|
||||
protected ConfigurationSerialization(Class<? extends ConfigurationSerializable> clazz) {
|
||||
|
Loading…
Reference in New Issue
Block a user