Advertisement
Guest User

itemDict.java

a guest
Jul 21st, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.14 KB | None | 0 0
  1. package org.styxl.slimefuncalculator;
  2.  
  3.  
  4. import org.yaml.snakeyaml.Yaml;
  5.  
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileWriter;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.io.Writer;
  12. import java.nio.file.Files;
  13. import java.nio.file.Paths;
  14. import java.nio.file.StandardCopyOption;
  15.  
  16. import java.net.URL;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import java.util.Map;
  20.  
  21.  
  22. public final class ItemDict {
  23.    
  24.    
  25.     static String url = "https://gist.githack.com/NihilistBrew/16cb075dc74a042cd539497085294f5c/raw/fdfb0ae796c29e091ee06a29e868d37d1c6ca175/items.yaml";
  26.     static String folderPath = "plugins/sfc";
  27.     static String itemDictFile = "itemDict.yaml";
  28.     static String itemDictPath = folderPath + "/" + itemDictFile;
  29.  
  30.    
  31.     public static void downloadItems() throws IOException {
  32.        
  33.         List<String> missingRaw = new ArrayList<String>();
  34.        
  35.         try (InputStream inputStream = new URL(url).openStream()){
  36.        
  37.             new File(folderPath).mkdir();  
  38.             Files.copy(inputStream, Paths.get(itemDictPath), StandardCopyOption.REPLACE_EXISTING);  
  39.             Map<String, Map<String, String>> itemDict = getItemDict();
  40.            
  41.             for (Map<String, String> recipe : itemDict.values()) {
  42.                 for (String material : recipe.keySet()) {
  43.                     if (!itemDict.containsKey(material)) {
  44.                         missingRaw.add(material);
  45.                     }
  46.                 }
  47.             }
  48.         } catch (IOException e) {
  49.             e.printStackTrace();
  50.         }
  51.        
  52.         Writer writer = new FileWriter(itemDictPath, true);
  53.         writer.write("\n");
  54.         for (String item : missingRaw) {
  55.             writer.write(item + ": null");
  56.             writer.write("\n");
  57.         }
  58.         writer.close();
  59.            
  60.     }
  61.    
  62.    
  63.     public static String getVersion() throws IOException {
  64.        
  65.         String version = Files.readAllLines(Paths.get(itemDictPath)).get(0);
  66.         return version;
  67.        
  68.     }
  69.    
  70.    
  71.     public static Map<String, Map<String, String>> getItemDict() throws IOException {
  72.        
  73.         Yaml yaml = new Yaml();
  74.         Map<String, Map<String, String>> itemDict = null;
  75.         try (InputStream inputStream = new FileInputStream(itemDictPath)) {
  76.             itemDict = yaml.load(inputStream);
  77.         } catch (IOException e) {
  78.             e.printStackTrace();
  79.         }
  80.         return itemDict;
  81.     }
  82.  
  83.    
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement