Guest User

Untitled

a guest
Jun 30th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.11 KB | None | 0 0
  1. package me.sekabo.advancementbroadcast.api;
  2.  
  3. import com.google.gson.Gson;
  4. import com.google.gson.GsonBuilder;
  5.  
  6. import me.sekabo.advancementbroadcast.Main;
  7. import net.minecraft.server.v1_12_R1.Item;
  8. import net.minecraft.server.v1_12_R1.MinecraftKey;
  9.  
  10. import org.bukkit.Bukkit;
  11. import org.bukkit.Material;
  12. import org.bukkit.NamespacedKey;
  13. import org.bukkit.World;
  14. import org.bukkit.advancement.Advancement;
  15. import org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack;
  16. import org.bukkit.craftbukkit.v1_12_R1.util.CraftMagicNumbers;
  17. import org.bukkit.entity.Player;
  18. import org.bukkit.inventory.ItemStack;
  19. import org.bukkit.material.MaterialData;
  20. import org.json.simple.JSONArray;
  21. import org.json.simple.JSONObject;
  22.  
  23. import java.io.File;
  24. import java.io.FileWriter;
  25. import java.io.IOException;
  26. import java.nio.file.Path;
  27. import java.nio.file.Paths;
  28. import java.util.List;
  29. import java.util.Objects;
  30. import java.util.Date;
  31.  
  32. import com.google.common.collect.Lists;
  33.  
  34. /**
  35.  * Created by charliej on 14/05/2017.
  36.  * Edited by GiansCode
  37.  */
  38. public class AdvancementAPI {
  39.  
  40.     private NamespacedKey id;
  41.     private String title, parent, trigger, icon, description, background, frame;
  42.     private Integer subID = 0, amount = 0;
  43.     private boolean announce, toast = true;
  44.     private List<ItemStack> items;
  45.  
  46.     public static enum FrameType{
  47.         CHALLANGE("challenge"),
  48.         GOAL("goal"),
  49.         DEFAULT("task");
  50.        
  51.         private String str;
  52.        
  53.         FrameType(String str){
  54.             this.str = str;
  55.         }
  56.        
  57.         public String getName(){return this.str;}
  58.     }
  59.    
  60.     public static enum AdvancementBackground{
  61.         ADVENTURE("minecraft:textures/gui/advancements/backgrounds/adventure.png"),
  62.         END("minecraft:textures/gui/advancements/backgrounds/end.png"),
  63.         HUSBANDRY("minecraft:textures/gui/advancements/backgrounds/husbandry.png"),
  64.         NETHER("minecraft:textures/gui/advancements/backgrounds/nether.png"),
  65.         STONE("minecraft:textures/gui/advancements/backgrounds/stone.png"),
  66.         fromNamespace(null);
  67.        
  68.         public String str;
  69.        
  70.         AdvancementBackground(String str){
  71.             this.str = str;
  72.         }
  73.  
  74.         public void fromNamespace(String string) {
  75.             str = string;
  76.         }
  77.     }
  78.    
  79.     public AdvancementAPI(NamespacedKey id) {
  80.         this.id = id;
  81.         this.items = Lists.newArrayList();
  82.         this.announce = true;
  83.     }
  84.  
  85.     public NamespacedKey getID() {
  86.         return id;
  87.     }
  88.  
  89.     public String getIcon() {
  90.         return icon;
  91.     }
  92.  
  93.     public AdvancementAPI withIcon(String icon) {
  94.         this.icon = icon;
  95.         return this;
  96.     }
  97.    
  98.     public AdvancementAPI withIcon(Material material){
  99.         this.icon = getMinecraftIDFrom(new ItemStack(material));
  100.         return this;
  101.     }
  102.    
  103.     @SuppressWarnings("deprecation")
  104.     public AdvancementAPI withIcon(MaterialData material){
  105.         this.icon = getMinecraftIDFrom(new ItemStack(material.getItemType()));
  106.         this.subID = (int) material.getData();
  107.         return this;
  108.     }
  109.    
  110.     public AdvancementAPI withIconData(int subID){
  111.         this.subID = subID;
  112.         return this;
  113.     }
  114.  
  115.     public String getDescription() {
  116.         return description;
  117.     }
  118.  
  119.     public AdvancementAPI withDescription(String description) {
  120.         this.description = description;
  121.         return this;
  122.     }
  123.  
  124.     public String getBackground() {
  125.         return background;
  126.     }
  127.  
  128.     public AdvancementAPI withBackground(String url) {
  129.         this.background = url;
  130.         return this;
  131.     }
  132.    
  133.     public AdvancementAPI withAmount(int i){
  134.         this.amount = i;
  135.         return this;
  136.     }
  137.  
  138.     public String getTitle() {
  139.         return title;
  140.     }
  141.  
  142.     public AdvancementAPI withTitle(String title) {
  143.         this.title = title;
  144.         return this;
  145.     }
  146.  
  147.     public String getParent() {
  148.         return parent;
  149.     }
  150.  
  151.     public AdvancementAPI withParent(String parent) {
  152.         this.parent = parent;
  153.         return this;
  154.     }
  155.    
  156.     public AdvancementAPI withToast(boolean bool){
  157.         this.toast = bool;
  158.         return this;
  159.     }
  160.  
  161.     public String getTrigger() {
  162.         return trigger;
  163.     }
  164.  
  165.     public AdvancementAPI withTrigger(String trigger) {
  166.         this.trigger = trigger;
  167.         return this;
  168.     }
  169.  
  170.     public List<ItemStack> getItems() {
  171.         return items;
  172.     }
  173.  
  174.     public AdvancementAPI withItem(ItemStack is) {
  175.         items.add(is);
  176.         return this;
  177.     }
  178.  
  179.     public String getFrame() {
  180.         return frame;
  181.     }
  182.  
  183.     public AdvancementAPI withFrame(FrameType frame) {
  184.         this.frame = frame.getName();
  185.         return this;
  186.     }
  187.  
  188.     public boolean getAnnouncement(){
  189.         return announce;
  190.     }
  191.     public AdvancementAPI withAnnouncement(boolean announce){
  192.         this.announce = announce;
  193.         return this;
  194.     }
  195.  
  196.     @SuppressWarnings("unchecked")
  197.     public String getJSON() {
  198.         if(this.amount > 0){
  199.             return getJson(this.amount);
  200.         }
  201.         JSONObject json = new JSONObject();
  202.  
  203.  
  204.         JSONObject icon = new JSONObject();
  205.         icon.put("item", getIcon());
  206.         icon.put("data", getIconSubID());
  207.  
  208.         JSONObject display = new JSONObject();
  209.         display.put("icon", icon);
  210.         display.put("title", getTitle());
  211.         display.put("description", getDescription());
  212.         display.put("background", getBackground());
  213.         display.put("frame", getFrame());
  214.         display.put("announce_to_chat", getAnnouncement());
  215.         display.put("show_toast", getToast());
  216.  
  217.         json.put("parent", getParent());
  218.  
  219.         JSONObject criteria = new JSONObject();
  220.         JSONObject conditions = new JSONObject();
  221.         JSONObject elytra = new JSONObject();
  222.  
  223.         JSONArray itemArray = new JSONArray();
  224.         JSONObject itemJSON = new JSONObject();
  225.  
  226.         for(ItemStack i : getItems()) {
  227.             itemJSON.put("item", "minecraft:"+ i.getType().name().toLowerCase());
  228.             itemJSON.put("amount", i.getAmount());
  229.             itemArray.add(itemJSON);
  230.         }
  231.  
  232.         conditions.put("items", itemArray);
  233.         elytra.put("trigger", getTrigger());
  234.         elytra.put("conditions", conditions);
  235.  
  236.         criteria.put("elytra", elytra);
  237.  
  238.         json.put("criteria", criteria);
  239.         json.put("display", display);
  240.  
  241.  
  242.         Gson gson = new GsonBuilder().setPrettyPrinting().create();
  243.         String prettyJson = gson.toJson(json);
  244.  
  245.         return prettyJson;
  246.     }
  247.    
  248.     @SuppressWarnings("unchecked")
  249.     public String getJson(int amaunt){
  250.         if(!getFrame().equalsIgnoreCase("challenge")){
  251.             return getJSON();
  252.         }
  253.         JSONObject json = new JSONObject();
  254.  
  255.         JSONObject icon = new JSONObject();
  256.         icon.put("item", getIcon());
  257.         icon.put("data", getIconSubID());
  258.  
  259.         JSONObject display = new JSONObject();
  260.         display.put("icon", icon);
  261.         display.put("title", getTitle());
  262.         display.put("description", getDescription());
  263.         display.put("background", getBackground());
  264.         display.put("frame", getFrame());
  265.         display.put("announce_to_chat", getAnnouncement());
  266.         display.put("show_toast", getToast());
  267.  
  268.         json.put("parent", getParent());
  269.  
  270.         JSONObject criteria = new JSONObject();
  271.         JSONObject conditions = new JSONObject();
  272.        
  273.  
  274.         JSONArray itemArray = new JSONArray();
  275.         JSONObject itemJSON = new JSONObject();
  276.  
  277.         for(ItemStack i : getItems()) {
  278.             itemJSON.put("item", "minecraft:"+ i.getType().name().toLowerCase());
  279.             itemJSON.put("amount", i.getAmount());
  280.             itemArray.add(itemJSON);
  281.         }
  282.  
  283.         for(int i = 0; i<=amaunt; i++){
  284.              JSONObject elytra = new JSONObject();
  285.              elytra.put("trigger", "minecraft:impossible");
  286.              conditions.put("items", itemArray);
  287.              elytra.put("conditions", conditions);
  288.              criteria.put("key" + i, elytra);
  289.         }
  290.  
  291.         json.put("criteria", criteria);
  292.         json.put("display", display);
  293.  
  294.  
  295.         Gson gson = new GsonBuilder().setPrettyPrinting().create();
  296.         String prettyJson = gson.toJson(json);
  297.  
  298.         return prettyJson;
  299.     }
  300.    
  301.     private boolean getToast() {
  302.         return this.toast;
  303.     }
  304.  
  305.     private int getIconSubID(){
  306.         return this.subID;
  307.     }
  308.  
  309.     @SuppressWarnings("deprecation")
  310.     public void loadAdvancement(){
  311.         for(World world : Bukkit.getWorlds()){
  312.             Path path = Paths.get(world.getWorldFolder() + File.separator + "data"
  313.             + File.separator + "advancements" + File.separator + id.getNamespace() + File.separator + getID().getKey().split("/")[0]);
  314.            
  315.             Path path2 = Paths.get(world.getWorldFolder() + File.separator + "data"
  316.             + File.separator + "advancements" + File.separator + id.getNamespace()
  317.             + File.separator + getID().getKey().split("/")[0] + File.separator + getID().getKey().split("/")[1] + ".json");
  318.            
  319.             if(!path.toFile().exists()){
  320.                 path.toFile().mkdirs();
  321.             }
  322.            
  323.             if(!path2.toFile().exists()){
  324.                 File file = path2.toFile();
  325.                 try {
  326.                     file.createNewFile();
  327.                     FileWriter writer = new FileWriter(file);
  328.                     writer.write(getJSON());
  329.                     writer.flush();
  330.                     writer.close();
  331.                 } catch (IOException e) {
  332.                     e.printStackTrace();
  333.                 }
  334.             }
  335.         }
  336.        
  337.         if(Bukkit.getAdvancement(getID()) == null){
  338.             Bukkit.getUnsafe().loadAdvancement(getID(), getJSON());
  339.         }
  340.        
  341.         Main.getInstance().AddAdvancment(this);
  342.     }
  343.    
  344.     @SuppressWarnings("deprecation")
  345.     public void delete(){
  346.         Bukkit.getUnsafe().removeAdvancement(getID());
  347.     }
  348.    
  349.     @SuppressWarnings("deprecation")
  350.     public void delete(Player ... player){
  351.         for(Player p : player){
  352.             if(p.getAdvancementProgress(getAdvancement()).isDone()){
  353.                 p.getAdvancementProgress(getAdvancement()).revokeCriteria("elytra");
  354.             }
  355.         }
  356.        
  357.         Bukkit.getScheduler().runTaskLater(Main.getInstance(), new Runnable() {
  358.             @Override
  359.             public void run() {
  360.                 CraftMagicNumbers.INSTANCE.removeAdvancement(getID());
  361.             }
  362.         }, 5);
  363.     }
  364.    
  365.     public static String getMinecraftIDFrom(ItemStack stack) {
  366.         final int check = Item.getId(CraftItemStack.asNMSCopy(stack).getItem());
  367.         final MinecraftKey matching = Item.REGISTRY.keySet()
  368.             .stream()
  369.             .filter(key -> Item.getId(Item.REGISTRY.get(key)) == check)
  370.             .findFirst().orElse(null);
  371.         return Objects.toString(matching, null);
  372.     }
  373.    
  374.     public void sendPlayer(Player ... player){
  375.         for(Player p : player){
  376.             if(!p.getAdvancementProgress(getAdvancement()).isDone()){
  377.                 p.getAdvancementProgress(getAdvancement()).awardCriteria("elytra");
  378.             }
  379.         }
  380.     }
  381.    
  382.     public void sendPlayer(String criteria, Player ... player){
  383.         for(Player p : player){
  384.             if(!p.getAdvancementProgress(getAdvancement()).isDone()){
  385.                 p.getAdvancementProgress(getAdvancement()).awardCriteria(criteria);
  386.             }
  387.         }
  388.     }
  389.    
  390.     public boolean next(Player p){
  391.         if(!p.getAdvancementProgress(getAdvancement()).isDone()){
  392.             for(String criteria : getAdvancement().getCriteria()){
  393.                 if(p.getAdvancementProgress(getAdvancement()).getDateAwarded(criteria) == null){
  394.                     p.getAdvancementProgress(getAdvancement()).awardCriteria(criteria);
  395.                     return true;
  396.                 }
  397.             }
  398.         }
  399.         return false;
  400.     }
  401.    
  402.     public boolean next(Player p, long diff, boolean onlyLast){
  403.         if(!p.getAdvancementProgress(getAdvancement()).isDone()){
  404.             Date oldData = null;
  405.             String str = "";
  406.             for(String criteria : getAdvancement().getCriteria()){
  407.                 if(p.getAdvancementProgress(getAdvancement()).getDateAwarded(criteria) != null){
  408.                     oldData = p.getAdvancementProgress(getAdvancement()).getDateAwarded(criteria);
  409.                     str = criteria;
  410.                     continue;
  411.                 }else{
  412.                     if(oldData == null){
  413.                         p.getAdvancementProgress(getAdvancement()).awardCriteria(criteria);
  414.                         return true;
  415.                     }else{
  416.                         long oldTime = oldData.getTime();
  417.                         long current = System.currentTimeMillis();
  418.                         if((current - diff) > oldTime){
  419.                             if(onlyLast){
  420.                                 p.getAdvancementProgress(getAdvancement()).revokeCriteria(str);
  421.                                 return false;
  422.                             }else{
  423.                                 for(String string : getAdvancement().getCriteria()){
  424.                                     p.getAdvancementProgress(getAdvancement()).revokeCriteria(string);
  425.                                 }
  426.                                 p.getAdvancementProgress(getAdvancement()).awardCriteria(getAdvancement().getCriteria().stream().findFirst().get());
  427.                                 return false;
  428.                             }
  429.                         }else{
  430.                             p.getAdvancementProgress(getAdvancement()).awardCriteria(criteria);
  431.                             return true;
  432.                         }
  433.                     }
  434.                 }
  435.             }
  436.         }
  437.         return false;
  438.     }
  439.    
  440.     public Date getLastAwardTime(Player p){
  441.         if(!p.getAdvancementProgress(getAdvancement()).isDone()){
  442.             Date oldData = null;
  443.             for(String criteria : getAdvancement().getCriteria()){
  444.                 if(p.getAdvancementProgress(getAdvancement()).getDateAwarded(criteria) != null){
  445.                     oldData = p.getAdvancementProgress(getAdvancement()).getDateAwarded(criteria);
  446.                     continue;
  447.                 }else{
  448.                     return oldData;
  449.                 }
  450.             }
  451.         }
  452.         return null;
  453.     }
  454.    
  455.     public Advancement getAdvancement(){
  456.         return Bukkit.getAdvancement(getID());
  457.     }
  458. }
Advertisement
Add Comment
Please, Sign In to add comment