2013-04-22 12:26:13 +02:00
|
|
|
package com.massivecraft.factions.entity;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.lang.reflect.Type;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
2013-04-25 09:59:25 +02:00
|
|
|
import java.util.Set;
|
2013-04-22 12:26:13 +02:00
|
|
|
|
|
|
|
import com.massivecraft.factions.Const;
|
|
|
|
import com.massivecraft.factions.Factions;
|
|
|
|
import com.massivecraft.mcore.MCore;
|
|
|
|
import com.massivecraft.mcore.usys.Aspect;
|
|
|
|
import com.massivecraft.mcore.util.DiscUtil;
|
2013-04-25 09:59:25 +02:00
|
|
|
import com.massivecraft.mcore.util.MUtil;
|
2013-04-22 12:26:13 +02:00
|
|
|
import com.massivecraft.mcore.xlib.gson.reflect.TypeToken;
|
|
|
|
|
2013-04-24 16:27:47 +02:00
|
|
|
public class FactionColls extends XColls<FactionColl, Faction>
|
2013-04-22 12:26:13 +02:00
|
|
|
{
|
|
|
|
// -------------------------------------------- //
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
2013-04-25 09:49:39 +02:00
|
|
|
// This method is for the 1.8.X --> 2.0.0 migration
|
2013-04-22 12:26:13 +02:00
|
|
|
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;
|
|
|
|
|
2013-04-25 09:59:25 +02:00
|
|
|
// Faction ids /delete
|
|
|
|
// For simplicity we just drop the old special factions.
|
|
|
|
// They will be replaced with new autogenerated ones per universe.
|
|
|
|
Set<String> factionIdsToDelete = MUtil.set("0", "-1", "-2");
|
|
|
|
|
2013-04-22 12:26:13 +02:00
|
|
|
// Read the file content through GSON.
|
|
|
|
Type type = new TypeToken<Map<String, Faction>>(){}.getType();
|
|
|
|
Map<String, Faction> id2faction = Factions.get().gson.fromJson(DiscUtil.readCatch(oldFile), type);
|
|
|
|
|
2013-04-22 15:05:00 +02:00
|
|
|
// The Coll
|
|
|
|
FactionColl coll = this.getForUniverse(MCore.DEFAULT);
|
|
|
|
|
2013-04-22 12:26:13 +02:00
|
|
|
// Set the data
|
|
|
|
for (Entry<String, Faction> entry : id2faction.entrySet())
|
|
|
|
{
|
|
|
|
String factionId = entry.getKey();
|
2013-04-25 09:59:25 +02:00
|
|
|
if (factionIdsToDelete.contains(factionId)) continue;
|
2013-04-22 12:26:13 +02:00
|
|
|
Faction faction = entry.getValue();
|
2013-04-22 15:05:00 +02:00
|
|
|
coll.attach(faction, factionId);
|
2013-04-22 12:26:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mark as migrated
|
|
|
|
oldFile.renameTo(newFile);
|
|
|
|
}
|
|
|
|
|
2013-04-22 15:05:00 +02:00
|
|
|
// -------------------------------------------- //
|
|
|
|
// INDEX
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
2013-04-22 17:59:51 +02:00
|
|
|
public void reindexUPlayers()
|
2013-04-22 15:05:00 +02:00
|
|
|
{
|
|
|
|
for (FactionColl coll : this.getColls())
|
|
|
|
{
|
2013-04-22 19:57:11 +02:00
|
|
|
coll.reindexUPlayers();
|
2013-04-22 15:05:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-22 12:26:13 +02:00
|
|
|
}
|