package com.massivecraft.factions.entity; import java.io.File; import java.lang.reflect.Type; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import com.massivecraft.factions.Const; import com.massivecraft.factions.Factions; import com.massivecraft.mcore.Aspect; import com.massivecraft.mcore.MCore; import com.massivecraft.mcore.util.DiscUtil; import com.massivecraft.mcore.util.MUtil; import com.massivecraft.mcore.xlib.gson.reflect.TypeToken; public class FactionColls extends XColls { // -------------------------------------------- // // INSTANCE & CONSTRUCT // -------------------------------------------- // private static FactionColls i = new FactionColls(); public static FactionColls get() { return i; } // -------------------------------------------- // // OVERRIDE: COLLS // -------------------------------------------- // @Override public FactionColl createColl(String collName) { return new FactionColl(collName); } @Override public Aspect getAspect() { return Factions.get().getAspect(); } @Override public String getBasename() { return Const.COLLECTION_BASENAME_FACTION; } @Override public void init() { super.init(); this.migrate(); } // This method is for the 1.8.X --> 2.0.0 migration public void migrate() { // Create file objects File oldFile = new File(Factions.get().getDataFolder(), "factions.json"); File newFile = new File(Factions.get().getDataFolder(), "factions.json.migrated"); // Already migrated? if ( ! oldFile.exists()) return; // Faction ids /delete // For simplicity we just drop the old special factions. // They will be replaced with new autogenerated ones per universe. Set factionIdsToDelete = MUtil.set("0", "-1", "-2"); // Read the file content through GSON. Type type = new TypeToken>(){}.getType(); Map id2faction = Factions.get().gson.fromJson(DiscUtil.readCatch(oldFile), type); // The Coll FactionColl coll = this.getForUniverse(MCore.DEFAULT); // Set the data for (Entry entry : id2faction.entrySet()) { String factionId = entry.getKey(); if (factionIdsToDelete.contains(factionId)) continue; Faction faction = entry.getValue(); coll.attach(faction, factionId); } // Mark as migrated oldFile.renameTo(newFile); } // -------------------------------------------- // // INDEX // -------------------------------------------- // public void reindexUPlayers() { for (FactionColl coll : this.getColls()) { coll.reindexUPlayers(); } } }