Factions3/src/com/massivecraft/factions/entity/UPlayerColls.java
2013-05-03 09:58:43 +02:00

96 lines
2.3 KiB
Java

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.Aspect;
import com.massivecraft.mcore.MCore;
import com.massivecraft.mcore.util.DiscUtil;
import com.massivecraft.mcore.xlib.gson.reflect.TypeToken;
public class UPlayerColls extends XColls<UPlayerColl, UPlayer>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static UPlayerColls i = new UPlayerColls();
public static UPlayerColls get() { return i; }
// -------------------------------------------- //
// OVERRIDE: COLLS
// -------------------------------------------- //
@Override
public UPlayerColl createColl(String collName)
{
return new UPlayerColl(collName);
}
@Override
public Aspect getAspect()
{
return Factions.get().getAspect();
}
@Override
public String getBasename()
{
return Const.COLLECTION_BASENAME_UPLAYER;
}
@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(), "players.json");
File newFile = new File(Factions.get().getDataFolder(), "players.json.migrated");
// Already migrated?
if ( ! oldFile.exists()) return;
// Read the file content through GSON.
Type type = new TypeToken<Map<String, UPlayer>>(){}.getType();
Map<String, UPlayer> id2uplayer = Factions.get().gson.fromJson(DiscUtil.readCatch(oldFile), type);
// The Coll
UPlayerColl coll = this.getForUniverse(MCore.DEFAULT);
// Set the data
for (Entry<String, UPlayer> entry : id2uplayer.entrySet())
{
String playerId = entry.getKey();
UPlayer uplayer = entry.getValue();
coll.attach(uplayer, playerId);
}
// Mark as migrated
oldFile.renameTo(newFile);
}
// -------------------------------------------- //
// EXTRAS
// -------------------------------------------- //
public void clean()
{
for (UPlayerColl coll : this.getColls())
{
coll.clean();
}
}
}