Core/src/main/java/eu/univento/core/api/advancement/Advancement.java

180 lines
4.6 KiB
Java

package eu.univento.core.api.advancement;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.Getter;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import java.util.List;
/**
* Created by charliej on 14/05/2017.
* Last modification DiscowZombie on 5/06/2017.
*/
@Getter
public class Advancement {
private NamespacedKey id;
private String title, parent, trigger, icon, description, background;
private boolean announce, toast;
private FrameType frame;
private List<ItemStack> items;
public Advancement(NamespacedKey id) {
this.id = id;
this.items = Lists.newArrayList();
this.announce = true;
this.toast = true;
}
public Advancement withIcon(String icon) {
this.icon = icon;
return this;
}
public String getDescription() {
return description;
}
public Advancement withDescription(String description) {
this.description = description;
return this;
}
public Advancement withBackground(String url) {
this.background = url;
return this;
}
public Advancement withBackground(AdvancementBackground background) {
this.background = background.getUrl();
return this;
}
public Advancement withTitle(String title) {
this.title = title;
return this;
}
public Advancement withParent(String parent) {
this.parent = parent;
return this;
}
public Advancement withTrigger(String trigger) {
this.trigger = trigger;
return this;
}
public Advancement withItem(ItemStack is) {
items.add(is);
return this;
}
public Advancement withFrame(FrameType frame) {
this.frame = frame;
return this;
}
public Advancement withAnnouncement(boolean announce){
this.announce = announce;
return this;
}
public Advancement withToast(boolean toast){
this.toast = toast;
return this;
}
public String getJSON() {
JSONObject json = new JSONObject();
JSONObject icon = new JSONObject();
icon.put("item", getIcon());
JSONObject display = new JSONObject();
display.put("icon", icon);
display.put("title", getTitle());
display.put("description", getDescription());
display.put("background", getBackground());
display.put("frame", getFrame().toString());
display.put("announce_to_chat", isAnnounce());
display.put("show_toast", isToast());
json.put("parent", getParent());
JSONObject criteria = new JSONObject();
JSONObject conditions = new JSONObject();
JSONObject elytra = new JSONObject();
JSONArray itemArray = new JSONArray();
JSONObject itemJSON = new JSONObject();
for(ItemStack i : getItems()) {
itemJSON.put("item", "minecraft:"+ i.getType().name().toLowerCase());
itemJSON.put("amount", i.getAmount());
itemArray.add(itemJSON);
}
/**
* Define each criteria, for each criteria in list,
* add items, trigger and conditions
*/
conditions.put("items", itemArray);
elytra.put("trigger", getTrigger());
elytra.put("conditions", conditions);
criteria.put("elytra", elytra);
json.put("criteria", criteria);
json.put("display", display);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
return gson.toJson(json);
}
public enum FrameType {
TASK("task"),
GOAL("goal"),
CHALLENGE("challenge");
private String name = "task";
FrameType(String name){
this.name = name;
}
public String toString(){
return name;
}
}
public enum AdvancementBackground{
ADVENTURE("minecraft:textures/gui/advancements/backgrounds/adventure.png"),
END("minecraft:textures/gui/advancements/backgrounds/end.png"),
HUSBANDRY("minecraft:textures/gui/advancements/backgrounds/husbandry.png"),
NETHER("minecraft:textures/gui/advancements/backgrounds/nether.png"),
STONE("minecraft:textures/gui/advancements/backgrounds/stone.png"),
fromNamespace(null);
@Getter private String url;
AdvancementBackground(String url){
this.url = url;
}
public void fromNamespace(String string) {
url = string;
}
}
}