Created
September 5, 2015 15:13
-
-
Save NeatMonster/d68ee30f7a306571478f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fr.neatmonster.plugin; | |
import java.util.HashMap; | |
import java.util.Map; | |
import org.bukkit.Bukkit; | |
import org.bukkit.Location; | |
import org.bukkit.World; | |
import org.bukkit.configuration.serialization.ConfigurationSerializable; | |
import org.bukkit.configuration.serialization.ConfigurationSerialization; | |
import org.bukkit.configuration.serialization.SerializableAs; | |
import org.bukkit.plugin.java.JavaPlugin; | |
import fr.neatmonster.plugin.Plugin.SSelection.SLocation; | |
public class Plugin extends JavaPlugin { | |
@SerializableAs("Selection") | |
public static class SSelection implements ConfigurationSerializable { | |
@SerializableAs("Location") | |
public static class SLocation extends Location implements ConfigurationSerializable { | |
public static SLocation deserialize(final Map<String, Object> map) { | |
final int x = (int) map.get("x"); | |
final int y = (int) map.get("y"); | |
final int z = (int) map.get("z"); | |
final World w = Bukkit.getWorld((String) map.get("w")); | |
return new SLocation(w, x, y, z); | |
} | |
public SLocation(final World world, final int x, final int y, final int z) { | |
super(world, x, y, z); | |
} | |
@Override | |
public Map<String, Object> serialize() { | |
final Map<String, Object> map = new HashMap<>(); | |
map.put("x", getBlockX()); | |
map.put("y", getBlockY()); | |
map.put("z", getBlockZ()); | |
map.put("w", getWorld().getName()); | |
return map; | |
} | |
} | |
public static SSelection deserialize(final Map<String, Object> map) { | |
final SLocation min = (SLocation) map.get("min"); | |
final SLocation max = (SLocation) map.get("max"); | |
return new SSelection(min, max); | |
} | |
private final SLocation min; | |
private final SLocation max; | |
public SSelection(final SLocation min, final SLocation max) { | |
this.min = min; | |
this.max = max; | |
} | |
public SLocation getMinimum() { | |
return min; | |
} | |
public SLocation getMaximum() { | |
return max; | |
} | |
@Override | |
public Map<String, Object> serialize() { | |
final Map<String, Object> map = new HashMap<>(); | |
map.put("min", min); | |
map.put("max", max); | |
return map; | |
} | |
} | |
@Override | |
public void onEnable() { | |
SSelection sel; | |
if (getConfig().contains("sel")) | |
sel = (SSelection) getConfig().get("sel"); | |
else { | |
final World w = Bukkit.getWorlds().get(0); | |
final SLocation min = new SLocation(w, 1, 2, 3); | |
final SLocation max = new SLocation(w, 4, 5, 6); | |
getConfig().set("sel", sel = new SSelection(min, max)); | |
saveConfig(); | |
} | |
System.out.println("min = " + sel.getMinimum()); | |
System.out.println("max = " + sel.getMaximum()); | |
} | |
@Override | |
public void onLoad() { | |
ConfigurationSerialization.registerClass(SLocation.class, "Location"); | |
ConfigurationSerialization.registerClass(SSelection.class, "Selection"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment