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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
import com.massivecraft.mcore.xlib.gson.reflect.TypeToken;
|
|
|
|
|
2013-04-24 16:27:47 +02:00
|
|
|
public class UPlayerColls extends XColls<UPlayerColl, UPlayer>
|
2013-04-22 12:26:13 +02:00
|
|
|
{
|
|
|
|
// -------------------------------------------- //
|
|
|
|
// INSTANCE & CONSTRUCT
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
2013-04-22 17:59:51 +02:00
|
|
|
private static UPlayerColls i = new UPlayerColls();
|
|
|
|
public static UPlayerColls get() { return i; }
|
2013-04-22 12:26:13 +02:00
|
|
|
|
|
|
|
// -------------------------------------------- //
|
|
|
|
// OVERRIDE: COLLS
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
|
|
|
@Override
|
2013-04-22 17:59:51 +02:00
|
|
|
public UPlayerColl createColl(String collName)
|
2013-04-22 12:26:13 +02:00
|
|
|
{
|
2013-04-22 17:59:51 +02:00
|
|
|
return new UPlayerColl(collName);
|
2013-04-22 12:26:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Aspect getAspect()
|
|
|
|
{
|
|
|
|
return Factions.get().getAspect();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getBasename()
|
|
|
|
{
|
2013-04-22 17:59:51 +02:00
|
|
|
return Const.COLLECTION_BASENAME_UPLAYER;
|
2013-04-22 12:26:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void init()
|
|
|
|
{
|
|
|
|
super.init();
|
|
|
|
|
|
|
|
this.migrate();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void migrate()
|
|
|
|
{
|
|
|
|
// Create file objects
|
|
|
|
File oldFile = new File(Factions.get().getDataFolder(), "players.json");
|
|
|
|
File newFile = new File(Factions.get().getDataFolder(), "players.json.migrated");
|
|
|
|
|
|
|
|
// Already migrated?
|
|
|
|
if ( ! oldFile.exists()) return;
|
|
|
|
|
|
|
|
// Read the file content through GSON.
|
2013-04-22 17:59:51 +02:00
|
|
|
Type type = new TypeToken<Map<String, UPlayer>>(){}.getType();
|
2013-04-22 19:57:11 +02:00
|
|
|
Map<String, UPlayer> id2uplayer = Factions.get().gson.fromJson(DiscUtil.readCatch(oldFile), type);
|
2013-04-22 12:26:13 +02:00
|
|
|
|
2013-04-22 15:05:00 +02:00
|
|
|
// The Coll
|
2013-04-22 17:59:51 +02:00
|
|
|
UPlayerColl coll = this.getForUniverse(MCore.DEFAULT);
|
2013-04-22 15:05:00 +02:00
|
|
|
|
2013-04-22 12:26:13 +02:00
|
|
|
// Set the data
|
2013-04-22 19:57:11 +02:00
|
|
|
for (Entry<String, UPlayer> entry : id2uplayer.entrySet())
|
2013-04-22 12:26:13 +02:00
|
|
|
{
|
|
|
|
String playerId = entry.getKey();
|
2013-04-22 19:57:11 +02:00
|
|
|
UPlayer uplayer = entry.getValue();
|
|
|
|
coll.attach(uplayer, playerId);
|
2013-04-22 12:26:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mark as migrated
|
|
|
|
oldFile.renameTo(newFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|