McImportCommand.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. package com.gmail.nossr50.commands;
  2. import com.gmail.nossr50.datatypes.skills.ModConfigType;
  3. import com.gmail.nossr50.mcMMO;
  4. import com.gmail.nossr50.util.Misc;
  5. import org.bukkit.Material;
  6. import org.bukkit.command.Command;
  7. import org.bukkit.command.CommandExecutor;
  8. import org.bukkit.command.CommandSender;
  9. import org.jetbrains.annotations.NotNull;
  10. import java.io.*;
  11. import java.util.ArrayList;
  12. import java.util.HashMap;
  13. import java.util.Locale;
  14. public class McImportCommand implements CommandExecutor {
  15. int fileAmount;
  16. @Override
  17. public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
  18. if (args.length == 0) {
  19. importModConfig();
  20. return true;
  21. }
  22. return false;
  23. }
  24. public boolean importModConfig() {
  25. String importFilePath = mcMMO.getModDirectory() + File.separator + "import";
  26. File importFile = new File(importFilePath, "import.log");
  27. mcMMO.p.getLogger().info("Starting import of mod materials...");
  28. fileAmount = 0;
  29. HashMap<ModConfigType, ArrayList<String>> materialNames = new HashMap<>();
  30. BufferedReader in = null;
  31. try {
  32. // Open the file
  33. in = new BufferedReader(new FileReader(importFile));
  34. String line;
  35. String materialName;
  36. String modName;
  37. // While not at the end of the file
  38. while ((line = in.readLine()) != null) {
  39. String[] split1 = line.split("material ");
  40. if (split1.length != 2) {
  41. continue;
  42. }
  43. String[] split2 = split1[1].split(" with");
  44. if (split2.length != 2) {
  45. continue;
  46. }
  47. materialName = split2[0];
  48. // Categorise each material under a mod config type
  49. ModConfigType type = ModConfigType.getModConfigType(materialName);
  50. if (!materialNames.containsKey(type)) {
  51. materialNames.put(type, new ArrayList<>());
  52. }
  53. materialNames.get(type).add(materialName);
  54. }
  55. }
  56. catch (FileNotFoundException e) {
  57. mcMMO.p.getLogger().warning("Could not find " + importFile.getAbsolutePath() + " ! (No such file or directory)");
  58. mcMMO.p.getLogger().warning("Copy and paste latest.log to " + importFile.getParentFile().getAbsolutePath() + " and rename it to import.log");
  59. return false;
  60. }
  61. catch (Exception e) {
  62. e.printStackTrace();
  63. return false;
  64. }
  65. finally {
  66. tryClose(in);
  67. }
  68. createOutput(materialNames);
  69. mcMMO.p.getLogger().info("Import finished! Created " + fileAmount + " files!");
  70. return true;
  71. }
  72. private void createOutput(HashMap<ModConfigType, ArrayList<String>> materialNames) {
  73. for (ModConfigType modConfigType : materialNames.keySet()) {
  74. HashMap<String, ArrayList<String>> materialNamesType = new HashMap<>();
  75. for (String materialName : materialNames.get(modConfigType)) {
  76. String modName = Misc.getModName(materialName);
  77. if (!materialNamesType.containsKey(modName)) {
  78. materialNamesType.put(modName, new ArrayList<>());
  79. }
  80. materialNamesType.get(modName).add(materialName);
  81. }
  82. createOutput(modConfigType, materialNamesType);
  83. }
  84. }
  85. private void tryClose(Closeable c) {
  86. if (c == null) {
  87. return;
  88. }
  89. try {
  90. c.close();
  91. }
  92. catch (IOException e) {
  93. e.printStackTrace();
  94. }
  95. }
  96. private void createOutput(ModConfigType modConfigType, HashMap<String, ArrayList<String>> materialNames) {
  97. File outputFilePath = new File(mcMMO.getModDirectory() + File.separator + "output");
  98. if (!outputFilePath.exists() && !outputFilePath.mkdirs()) {
  99. mcMMO.p.getLogger().severe("Could not create output directory! " + outputFilePath.getAbsolutePath());
  100. }
  101. FileWriter out = null;
  102. String type = modConfigType.name().toLowerCase(Locale.ENGLISH);
  103. for (String modName : materialNames.keySet()) {
  104. File outputFile = new File(outputFilePath, modName + "." + type + ".yml");
  105. mcMMO.p.getLogger().info("Creating " + outputFile.getName());
  106. try {
  107. if (outputFile.exists() && !outputFile.delete()) {
  108. mcMMO.p.getLogger().severe("Not able to delete old output file! " + outputFile.getAbsolutePath());
  109. }
  110. if (!outputFile.createNewFile()) {
  111. mcMMO.p.getLogger().severe("Could not create output file! " + outputFile.getAbsolutePath());
  112. continue;
  113. }
  114. StringBuilder writer = new StringBuilder();
  115. HashMap<String, ArrayList<String>> configSections = getConfigSections(modConfigType, modName, materialNames);
  116. if (configSections == null) {
  117. mcMMO.p.getLogger().severe("Something went wrong!! type is " + type);
  118. return;
  119. }
  120. // Write the file, go through each skill and write all the materials
  121. for (String configSection : configSections.keySet()) {
  122. if (configSection.equals("UNIDENTIFIED")) {
  123. writer.append("# This isn't a valid config section and all materials in this category need to be").append("\r\n");
  124. writer.append("# copy and pasted to a valid section of this config file.").append("\r\n");
  125. }
  126. writer.append(configSection).append(":").append("\r\n");
  127. for (String line : configSections.get(configSection)) {
  128. writer.append(line).append("\r\n");
  129. }
  130. writer.append("\r\n");
  131. }
  132. out = new FileWriter(outputFile);
  133. out.write(writer.toString());
  134. } catch (Exception e) {
  135. e.printStackTrace();
  136. return;
  137. } finally {
  138. tryClose(out);
  139. fileAmount++;
  140. }
  141. }
  142. }
  143. private HashMap<String, ArrayList<String>> getConfigSections(ModConfigType type, String modName, HashMap<String, ArrayList<String>> materialNames) {
  144. switch (type) {
  145. case BLOCKS:
  146. return getConfigSectionsBlocks(modName, materialNames);
  147. case TOOLS:
  148. return getConfigSectionsTools(modName, materialNames);
  149. case ARMOR:
  150. return getConfigSectionsArmor(modName, materialNames);
  151. case UNKNOWN:
  152. return getConfigSectionsUnknown(modName, materialNames);
  153. }
  154. return null;
  155. }
  156. private HashMap<String, ArrayList<String>> getConfigSectionsBlocks(String modName, HashMap<String, ArrayList<String>> materialNames) {
  157. HashMap<String, ArrayList<String>> configSections = new HashMap<>();
  158. // Go through all the materials and categorise them under a skill
  159. for (String materialName : materialNames.get(modName)) {
  160. String skillName = "UNIDENTIFIED";
  161. if (materialName.contains("ORE")) {
  162. skillName = "Mining";
  163. }
  164. else if (materialName.contains("LOG") || materialName.contains("LEAVES")) {
  165. skillName = "Woodcutting";
  166. }
  167. else if (materialName.contains("GRASS") || materialName.contains("FLOWER") || materialName.contains("CROP")) {
  168. skillName = "Herbalism";
  169. }
  170. else if (materialName.contains("DIRT") || materialName.contains("SAND")) {
  171. skillName = "Excavation";
  172. }
  173. if (!configSections.containsKey(skillName)) {
  174. configSections.put(skillName, new ArrayList<>());
  175. }
  176. ArrayList<String> skillContents = configSections.get(skillName);
  177. skillContents.add(" " + materialName + "|0:");
  178. skillContents.add(" " + " " + "XP_Gain: 99");
  179. skillContents.add(" " + " " + "Double_Drops_Enabled: true");
  180. if (skillName.equals("Mining")) {
  181. skillContents.add(" " + " " + "Smelting_XP_Gain: 9");
  182. }
  183. else if (skillName.equals("Woodcutting")) {
  184. skillContents.add(" " + " " + "Is_Log: " + materialName.contains("LOG"));
  185. }
  186. }
  187. return configSections;
  188. }
  189. private HashMap<String, ArrayList<String>> getConfigSectionsTools(String modName, HashMap<String, ArrayList<String>> materialNames) {
  190. HashMap<String, ArrayList<String>> configSections = new HashMap<>();
  191. // Go through all the materials and categorise them under a tool type
  192. for (String materialName : materialNames.get(modName)) {
  193. String toolType = "UNIDENTIFIED";
  194. if (materialName.contains("PICKAXE")) {
  195. toolType = "Pickaxes";
  196. }
  197. else if (materialName.contains("AXE")) {
  198. toolType = "Axes";
  199. }
  200. else if (materialName.contains("BOW")) {
  201. toolType = "Bows";
  202. }
  203. else if (materialName.contains("HOE")) {
  204. toolType = "Hoes";
  205. }
  206. else if (materialName.contains("SHOVEL") || materialName.contains("SPADE")) {
  207. toolType = "Shovels";
  208. }
  209. else if (materialName.contains("SWORD")) {
  210. toolType = "Swords";
  211. }
  212. if (!configSections.containsKey(toolType)) {
  213. configSections.put(toolType, new ArrayList<>());
  214. }
  215. ArrayList<String> skillContents = configSections.get(toolType);
  216. skillContents.add(" " + materialName + ":");
  217. skillContents.add(" " + " " + "XP_Modifier: 1.0");
  218. skillContents.add(" " + " " + "Tier: 1");
  219. skillContents.add(" " + " " + "Ability_Enabled: true");
  220. addRepairableLines(materialName, skillContents);
  221. }
  222. return configSections;
  223. }
  224. private HashMap<String, ArrayList<String>> getConfigSectionsArmor(String modName, HashMap<String, ArrayList<String>> materialNames) {
  225. HashMap<String, ArrayList<String>> configSections = new HashMap<>();
  226. // Go through all the materials and categorise them under an armor type
  227. for (String materialName : materialNames.get(modName)) {
  228. String toolType = "UNIDENTIFIED";
  229. if (materialName.contains("BOOT") || materialName.contains("SHOE")) {
  230. toolType = "Boots";
  231. }
  232. else if (materialName.contains("CHESTPLATE") || materialName.contains("CHEST")) {
  233. toolType = "Chestplates";
  234. }
  235. else if (materialName.contains("HELM") || materialName.contains("HAT")) {
  236. toolType = "Helmets";
  237. }
  238. else if (materialName.contains("LEGGINGS") || materialName.contains("LEGS") || materialName.contains("PANTS")) {
  239. toolType = "Leggings";
  240. }
  241. if (!configSections.containsKey(toolType)) {
  242. configSections.put(toolType, new ArrayList<>());
  243. }
  244. ArrayList<String> skillContents = configSections.get(toolType);
  245. skillContents.add(" " + materialName + ":");
  246. addRepairableLines(materialName, skillContents);
  247. }
  248. return configSections;
  249. }
  250. private void addRepairableLines(String materialName, ArrayList<String> skillContents) {
  251. skillContents.add(" " + " " + "Repairable: true");
  252. skillContents.add(" " + " " + "Repair_Material: REPAIR_MATERIAL_NAME");
  253. skillContents.add(" " + " " + "Repair_Material_Data_Value: 0");
  254. skillContents.add(" " + " " + "Repair_Material_Quantity: 9");
  255. skillContents.add(" " + " " + "Repair_Material_Pretty_Name: Repair Item Name");
  256. skillContents.add(" " + " " + "Repair_MinimumLevel: 0");
  257. skillContents.add(" " + " " + "Repair_XpMultiplier: 1.0");
  258. Material material = Material.matchMaterial(materialName);
  259. short durability = (material == null) ? (short) 9999 : material.getMaxDurability();
  260. skillContents.add(" " + " " + "Durability: " + ((durability > 0) ? durability : (short) 9999));
  261. }
  262. private HashMap<String, ArrayList<String>> getConfigSectionsUnknown(String modName, HashMap<String, ArrayList<String>> materialNames) {
  263. HashMap<String, ArrayList<String>> configSections = new HashMap<>();
  264. // Go through all the materials and print them
  265. for (String materialName : materialNames.get(modName)) {
  266. String configKey = "UNIDENTIFIED";
  267. if (!configSections.containsKey(configKey)) {
  268. configSections.put(configKey, new ArrayList<>());
  269. }
  270. ArrayList<String> skillContents = configSections.get(configKey);
  271. skillContents.add(" " + materialName);
  272. }
  273. return configSections;
  274. }
  275. }