SHARE
TWEET

Untitled

a guest Aug 23rd, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package me.matthew.zhcf.handler.faction;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.UUID;
  9.  
  10. import org.bukkit.configuration.file.YamlConfiguration;
  11.  
  12. import lombok.Getter;
  13. import me.matthew.zcore.handler.position.Position;
  14. import me.matthew.zcore.util.ListUtil;
  15. import me.matthew.zhcf.Main;
  16. import me.matthew.zhcf.handler.faction.type.PlayerFaction;
  17. import me.matthew.zhcf.handler.faction.type.SystemFaction;
  18.  
  19. /*
  20.  * Created by Matthew
  21.  * 2019. aug. 23.
  22.  */
  23. public class FactionHandler {
  24.    
  25.     @Getter
  26.     private Main plugin;
  27.     @Getter
  28.     private File playerFactionFile;
  29.     @Getter
  30.     private YamlConfiguration playerFactionConfig;
  31.     @Getter
  32.     private File systemFactionFile;
  33.     @Getter
  34.     private YamlConfiguration systemFactionConfig;
  35.     @Getter
  36.     private Map<String, PlayerFaction> playerFactions;
  37.     @Getter
  38.     private Map<String, SystemFaction> systemFactions;
  39.    
  40.     public FactionHandler() {
  41.         this.plugin = Main.getInstance();
  42.         this.playerFactionFile = new File(this.plugin.getDataFolder(), "playerfactions.yml");
  43.         this.playerFactionConfig = YamlConfiguration.loadConfiguration(this.playerFactionFile);
  44.         this.systemFactionFile = new File(this.plugin.getDataFolder(), "systemfactions.yml");
  45.         this.systemFactionConfig = YamlConfiguration.loadConfiguration(this.systemFactionFile);
  46.         this.playerFactions = new HashMap<String, PlayerFaction>();
  47.         this.systemFactions = new HashMap<String, SystemFaction>();
  48.     }
  49.    
  50.     public Faction getFaction(String factionName) {
  51.         factionName = factionName.toLowerCase();
  52.         if (this.playerFactions.containsKey(factionName)) {
  53.             return this.playerFactions.get(factionName);
  54.         }
  55.         if (this.systemFactions.containsKey(factionName)) {
  56.             return this.systemFactions.get(factionName);
  57.         }
  58.         return null;
  59.     }
  60.    
  61.     public void saveFaction(String factionName, boolean save) {
  62.         factionName = factionName.toLowerCase();
  63.         Faction faction = getFaction(factionName);
  64.         if (faction == null) {
  65.             return;
  66.         }
  67.         if (faction instanceof PlayerFaction) {
  68.             this.playerFactionConfig.set(factionName, null);
  69.             this.playerFactionConfig.set(factionName + ".displayName", faction.getDisplayName());
  70.             this.playerFactionConfig.set(factionName + ".home", faction.getHome().toString(true));
  71.             this.playerFactionConfig.set(factionName + ".leader", ((PlayerFaction) faction).getLeader().toString());
  72.             this.playerFactionConfig.set(factionName + ".captains", ListUtil.UUIDListToString(((PlayerFaction) faction).getCaptains()));
  73.             this.playerFactionConfig.set(factionName + ".members", ListUtil.UUIDListToString(((PlayerFaction) faction).getMembers()));
  74.             this.playerFactionConfig.set(factionName + ".kills", ((PlayerFaction) faction).getKills());
  75.             this.playerFactionConfig.set(factionName + ".balance", ((PlayerFaction) faction).getBalance());
  76.             this.playerFactionConfig.set(factionName + ".dtr", ((PlayerFaction) faction).getDtr());
  77.             this.playerFactionConfig.set(factionName + ".regenTime", ((PlayerFaction) faction).getRegenTime());
  78.             if (save) {
  79.                 saveConfig(true);
  80.             }
  81.         } else {
  82.             this.playerFactionConfig.set(factionName, null);
  83.             this.playerFactionConfig.set(factionName + ".displayName", faction.getDisplayName());
  84.             this.playerFactionConfig.set(factionName + ".home", faction.getHome().toString(false));
  85.             this.playerFactionConfig.set(factionName + ".deathban", ((SystemFaction) faction).isDeathban());
  86.             if (save) {
  87.                 saveConfig(false);
  88.             }
  89.         }
  90.     }
  91.    
  92.     public void deleteFaction(String factionName) {
  93.         factionName = factionName.toLowerCase();
  94.         Faction faction = getFaction(factionName);
  95.         if (faction != null) {
  96.             if (faction instanceof PlayerFaction) {
  97.                 this.playerFactions.remove(factionName);
  98.                 this.playerFactionConfig.set(factionName, (Object) null);
  99.                 saveConfig(true);
  100.             } else {
  101.                 this.systemFactions.remove(factionName);
  102.                 this.systemFactionConfig.set(factionName, (Object) null);
  103.                 saveConfig(false);
  104.             }
  105.         }
  106.     }
  107.    
  108.     public void loadFaction(String factionName, boolean player) {
  109.         factionName = factionName.toLowerCase();
  110.         if (player) {
  111.             if (this.playerFactionConfig.contains(factionName)) {
  112.                 String displayName = this.playerFactionConfig.getString(factionName + ".displayName");
  113.                 Position home = new Position(this.playerFactionConfig.getString(factionName + ".home"), false);
  114.                 UUID leader = UUID.fromString(this.playerFactionConfig.getString(factionName + ".leader"));
  115.                 List<UUID> captains = ListUtil.StringListToUUID(this.playerFactionConfig.getStringList(factionName + ".captains"));
  116.                 List<UUID> members = ListUtil.StringListToUUID(this.playerFactionConfig.getStringList(factionName + ".members"));
  117.                 int kills = this.playerFactionConfig.getInt(factionName + ".kills");
  118.                 double balance = this.playerFactionConfig.getDouble(factionName + ".balance");
  119.                 double dtr = this.playerFactionConfig.getDouble(factionName + ".dtr");
  120.                 long regenTime = this.playerFactionConfig.getLong(factionName + ".regenTime");
  121.                 this.playerFactions.put(factionName, new PlayerFaction(displayName, home, leader, captains, members, kills, balance, dtr, regenTime));
  122.             }
  123.         } else {
  124.             if (this.systemFactionConfig.contains(factionName)) {
  125.                 String displayName = this.systemFactionConfig.getString(factionName + ".displayName");
  126.                 Position home = new Position(this.systemFactionConfig.getString(factionName + ".home"), false);
  127.                 boolean deathban = this.playerFactionConfig.getBoolean(factionName + ".leader");
  128.                 this.systemFactions.put(factionName, new SystemFaction(displayName, home, deathban));
  129.             }
  130.         }
  131.     }
  132.    
  133.     public void saveFactions() {
  134.         for (String factionName : this.playerFactions.keySet()) {
  135.             saveFaction(factionName, false);
  136.         }
  137.         for (String factionName : this.systemFactions.keySet()) {
  138.             saveFaction(factionName, true);
  139.         }
  140.         saveConfig(false);
  141.         saveConfig(true);
  142.     }
  143.    
  144.     public void loadFactions() {
  145.         for (String key : this.playerFactionConfig.getKeys(false)) {
  146.             loadFaction(key, true);
  147.         }
  148.         for (String key : this.systemFactionConfig.getKeys(false)) {
  149.             loadFaction(key, false);
  150.         }
  151.     }
  152.    
  153.     public void saveConfig(boolean player) {
  154.         try {
  155.             if (player) {
  156.                 this.playerFactionConfig.save(this.playerFactionFile);
  157.             } else {
  158.                 this.systemFactionConfig.save(this.systemFactionFile);
  159.             }
  160.         } catch (IOException e) {
  161.             e.printStackTrace();
  162.         }
  163.     }
  164.    
  165. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top