Last active
August 29, 2015 14:05
-
-
Save jmhertlein/f8fbf66f1109f9ad4cbc to your computer and use it in GitHub Desktop.
Orders using a Map
This file contains 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
public class SomePlugin extends JavaPlugin { | |
private Map<String, Order> orders; | |
... | |
public void onEnable() { | |
//you'll need to remember to load the config from disk | |
//something like | |
//this.orders = getConfig().getMap("Amazon.Players"); | |
this.orders = new HashMap<String, Order>; | |
... | |
} | |
//call this method whenever you need to add orders | |
public void addOrder(String playerName, Material item, int quantity) { | |
orders.put(playerName, new Order(item, quantity)); | |
} | |
public void onDisable() { | |
getConfig().set("Amazon.Players", orders); | |
saveConfig(); | |
} | |
} | |
public class Order implements ConfigurationSerializable { | |
private int count; | |
private Material item; | |
public Order(Material i, int c) { | |
this.count = c; | |
this.item = i; | |
} | |
//add getters and setters if you need them | |
public Map<String, Object> serialize() { | |
Map ret = new HashMap<String, Object>(); | |
ret.put("item", item.toString()); | |
ret.put("count", count); | |
return ret; | |
} | |
public static Order deserialize(Map<String, Object> input) { | |
return new Order( | |
Material.matchMaterial((String) input.get("item")), | |
(Integer) input.get("count") | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment