Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.lisenochek.ftnpc;
- import org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- import org.bukkit.Location;
- import org.bukkit.World;
- import org.bukkit.configuration.file.YamlConfiguration;
- import org.bukkit.entity.Villager;
- import org.bukkit.potion.PotionEffect;
- import org.bukkit.potion.PotionEffectType;
- import java.io.File;
- import java.io.IOException;
- import java.util.HashMap;
- public class ConfigManager {
- public static HashMap<String, CustomVillager> entitySave = new HashMap<>();
- public static File f = new File(API.getInstance().getDataFolder(), "NPCbase.yml");
- public static YamlConfiguration y = YamlConfiguration.loadConfiguration(f);
- public static void initConfig() throws IOException {
- if (!API.getInstance().getDataFolder().mkdirs()) API.getInstance().getDataFolder().mkdirs();
- if (!f.exists()) f.createNewFile();
- }
- public static void saveConfig() {
- try {
- y.save(f);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void loadNPC() {
- if (!y.isConfigurationSection("NPC")) return;
- for (String s : y.getConfigurationSection("NPC").getKeys(false)) {
- World w = Bukkit.getWorld(y.getString("NPC." + s + ".world"));
- double x = y.getDouble("NPC." + s + ".x");
- double Y = y.getDouble("NPC." + s + ".y");
- double z = y.getDouble("NPC." + s + ".z");
- boolean adult = y.getBoolean("NPC." + s + ".adult");
- String prof = y.getString("NPC." + s + ".profession");
- String cmd = y.getString("NPC." + s + ".command");
- Location loc = new Location(w, x, Y, z);
- ChunkEntityListener.addEntity(new CustomVillager(loc, s, cmd), ChatColor.stripColor(s));
- Villager ent = ChunkEntityListener.getEntityByLocation(loc);
- if (ent != null) continue;
- ConfigManager.createNPC(loc, adult, prof, s, cmd);
- }
- }
- public static Villager createNPC(Location loc, boolean age, String prof, String name, String cmd) {
- Villager ent = loc.getWorld().spawn(loc, Villager.class);
- ent.setCustomName(name);
- ent.setCustomNameVisible(true);
- ent.setProfession(Villager.Profession.valueOf(prof));
- ent.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, Integer.MAX_VALUE, 10));
- ent.setCollidable(false);
- CustomVillager ce = new CustomVillager(loc, name, null);
- ChunkEntityListener.addEntity(ce, ce.getNameWithoutColors());
- ce.setCommand(cmd);
- saveNPCToDisc(ce);
- if (!age) return ent;
- ent.setAgeLock(true);
- ent.setBaby();
- return ent;
- }
- public static void removeNPC(CustomVillager ent) {
- ent.getBukkitEntity().remove();
- y.set("NPC." + ent.getName(), null);
- saveConfig();
- entitySave.remove(ent.getNameWithoutColors());
- }
- public static void renameNPC(CustomVillager ent, String name) {
- entitySave.remove(ent.getNameWithoutColors());
- entitySave.put(ChatColor.stripColor(name), ent);
- y.set("NPC." + ent.getName(), null);
- ent.setName(name);
- saveNPCToDisc(ent);
- }
- public static void setCommand(CustomVillager ent, String cmd) {
- ent.setCommand(cmd);
- saveCommandToDisc(ent);
- }
- public static void removeCommand(CustomVillager ent) {
- y.set("NPC." + ent.getName() + ".command", null);
- saveConfig();
- ent.setCommand(null);
- }
- public static void saveNPCToDisc(CustomVillager ent) {
- y.set("NPC." + ent.getName() + ".world", ent.getLocation().getWorld().getName());
- y.set("NPC." + ent.getName() + ".x", ent.getLocation().getX());
- y.set("NPC." + ent.getName() + ".y", ent.getLocation().getY());
- y.set("NPC." + ent.getName() + ".z", ent.getLocation().getZ());
- y.set("NPC." + ent.getName() + ".adult", ent.getVillager().isAdult());
- y.set("NPC." + ent.getName() + ".profession", ent.getVillager().getProfession().name());
- y.set("NPC." + ent.getName() + ".command", ent.getCommand());
- saveConfig();
- }
- public static void saveCommandToDisc(CustomVillager ent) {
- y.set("NPC." + ent.getName() + ".command", ent.getCommand());
- saveConfig();
- }
- public static CustomVillager getEntity(String name) {
- return ConfigManager.entitySave.get(name);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement