Advertisement
Lisenochek

Untitled

Jan 22nd, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. package ru.lisenochek.ftnpc;
  2.  
  3. import org.bukkit.Bukkit;
  4. import org.bukkit.ChatColor;
  5. import org.bukkit.Location;
  6. import org.bukkit.World;
  7. import org.bukkit.configuration.file.YamlConfiguration;
  8. import org.bukkit.entity.Villager;
  9. import org.bukkit.potion.PotionEffect;
  10. import org.bukkit.potion.PotionEffectType;
  11.  
  12. import java.io.File;
  13. import java.io.IOException;
  14. import java.util.HashMap;
  15.  
  16. public class ConfigManager {
  17.  
  18. public static HashMap<String, CustomVillager> entitySave = new HashMap<>();
  19. public static File f = new File(API.getInstance().getDataFolder(), "NPCbase.yml");
  20. public static YamlConfiguration y = YamlConfiguration.loadConfiguration(f);
  21.  
  22. public static void initConfig() throws IOException {
  23.  
  24. if (!API.getInstance().getDataFolder().mkdirs()) API.getInstance().getDataFolder().mkdirs();
  25. if (!f.exists()) f.createNewFile();
  26. }
  27.  
  28. public static void saveConfig() {
  29. try {
  30. y.save(f);
  31. } catch (IOException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35.  
  36. public static void loadNPC() {
  37.  
  38. if (!y.isConfigurationSection("NPC")) return;
  39.  
  40. for (String s : y.getConfigurationSection("NPC").getKeys(false)) {
  41.  
  42. World w = Bukkit.getWorld(y.getString("NPC." + s + ".world"));
  43. double x = y.getDouble("NPC." + s + ".x");
  44. double Y = y.getDouble("NPC." + s + ".y");
  45. double z = y.getDouble("NPC." + s + ".z");
  46. boolean adult = y.getBoolean("NPC." + s + ".adult");
  47. String prof = y.getString("NPC." + s + ".profession");
  48. String cmd = y.getString("NPC." + s + ".command");
  49.  
  50. Location loc = new Location(w, x, Y, z);
  51. ChunkEntityListener.addEntity(new CustomVillager(loc, s, cmd), ChatColor.stripColor(s));
  52. Villager ent = ChunkEntityListener.getEntityByLocation(loc);
  53.  
  54. if (ent != null) continue;
  55. ConfigManager.createNPC(loc, adult, prof, s, cmd);
  56. }
  57. }
  58.  
  59. public static Villager createNPC(Location loc, boolean age, String prof, String name, String cmd) {
  60.  
  61. Villager ent = loc.getWorld().spawn(loc, Villager.class);
  62.  
  63. ent.setCustomName(name);
  64. ent.setCustomNameVisible(true);
  65. ent.setProfession(Villager.Profession.valueOf(prof));
  66. ent.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, Integer.MAX_VALUE, 10));
  67. ent.setCollidable(false);
  68.  
  69. CustomVillager ce = new CustomVillager(loc, name, null);
  70. ChunkEntityListener.addEntity(ce, ce.getNameWithoutColors());
  71. ce.setCommand(cmd);
  72.  
  73. saveNPCToDisc(ce);
  74.  
  75. if (!age) return ent;
  76. ent.setAgeLock(true);
  77. ent.setBaby();
  78.  
  79. return ent;
  80. }
  81.  
  82. public static void removeNPC(CustomVillager ent) {
  83.  
  84. ent.getBukkitEntity().remove();
  85. y.set("NPC." + ent.getName(), null);
  86. saveConfig();
  87. entitySave.remove(ent.getNameWithoutColors());
  88. }
  89.  
  90. public static void renameNPC(CustomVillager ent, String name) {
  91.  
  92. entitySave.remove(ent.getNameWithoutColors());
  93. entitySave.put(ChatColor.stripColor(name), ent);
  94.  
  95. y.set("NPC." + ent.getName(), null);
  96. ent.setName(name);
  97. saveNPCToDisc(ent);
  98. }
  99.  
  100. public static void setCommand(CustomVillager ent, String cmd) {
  101.  
  102. ent.setCommand(cmd);
  103. saveCommandToDisc(ent);
  104. }
  105.  
  106. public static void removeCommand(CustomVillager ent) {
  107.  
  108. y.set("NPC." + ent.getName() + ".command", null);
  109. saveConfig();
  110. ent.setCommand(null);
  111. }
  112.  
  113. public static void saveNPCToDisc(CustomVillager ent) {
  114.  
  115. y.set("NPC." + ent.getName() + ".world", ent.getLocation().getWorld().getName());
  116. y.set("NPC." + ent.getName() + ".x", ent.getLocation().getX());
  117. y.set("NPC." + ent.getName() + ".y", ent.getLocation().getY());
  118. y.set("NPC." + ent.getName() + ".z", ent.getLocation().getZ());
  119. y.set("NPC." + ent.getName() + ".adult", ent.getVillager().isAdult());
  120. y.set("NPC." + ent.getName() + ".profession", ent.getVillager().getProfession().name());
  121. y.set("NPC." + ent.getName() + ".command", ent.getCommand());
  122. saveConfig();
  123. }
  124.  
  125. public static void saveCommandToDisc(CustomVillager ent) {
  126.  
  127. y.set("NPC." + ent.getName() + ".command", ent.getCommand());
  128. saveConfig();
  129. }
  130.  
  131. public static CustomVillager getEntity(String name) {
  132. return ConfigManager.entitySave.get(name);
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement