2013-04-22 21:00:00 +02:00
package com.massivecraft.factions.entity ;
import com.massivecraft.factions.Const ;
import com.massivecraft.factions.Factions ;
2014-09-17 13:17:33 +02:00
import com.massivecraft.factions.Rel ;
import com.massivecraft.massivecore.mixin.Mixin ;
2014-06-04 14:02:23 +02:00
import com.massivecraft.massivecore.store.MStore ;
import com.massivecraft.massivecore.store.SenderColl ;
2014-09-17 13:17:33 +02:00
import com.massivecraft.massivecore.util.IdUtil ;
import com.massivecraft.massivecore.util.TimeUnit ;
import com.massivecraft.massivecore.util.Txt ;
2013-04-22 21:00:00 +02:00
public class MPlayerColl extends SenderColl < MPlayer >
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static MPlayerColl i = new MPlayerColl ( ) ;
public static MPlayerColl get ( ) { return i ; }
private MPlayerColl ( )
{
2013-11-11 09:31:04 +01:00
super ( Const . COLLECTION_MPLAYER , MPlayer . class , MStore . getDb ( ) , Factions . get ( ) ) ;
2013-04-22 21:00:00 +02:00
}
2014-09-17 13:17:33 +02:00
// -------------------------------------------- //
// EXTRAS
// -------------------------------------------- //
public void clean ( )
{
String universe = this . getUniverse ( ) ;
2014-09-17 13:29:58 +02:00
for ( MPlayer mplayer : this . getAll ( ) )
2014-09-17 13:17:33 +02:00
{
2014-09-17 13:29:58 +02:00
String factionId = mplayer . getFactionId ( ) ;
2014-09-17 13:17:33 +02:00
if ( FactionColl . get ( ) . containsId ( factionId ) ) continue ;
2014-09-17 13:29:58 +02:00
mplayer . resetFactionData ( ) ;
2014-09-17 13:17:33 +02:00
2014-09-17 13:29:58 +02:00
String message = Txt . parse ( " <i>Reset data for <h>%s <i>in <h>%s <i>universe. Unknown factionId <h>%s " , mplayer . getDisplayName ( IdUtil . getConsole ( ) ) , universe , factionId ) ;
2014-09-17 13:17:33 +02:00
Factions . get ( ) . log ( message ) ;
}
}
public void removePlayerDataAfterInactiveDaysRoutine ( )
{
if ( MConf . get ( ) . removePlayerDataAfterInactiveDays < = 0 . 0 ) return ;
long now = System . currentTimeMillis ( ) ;
double toleranceMillis = MConf . get ( ) . removePlayerDataAfterInactiveDays * TimeUnit . MILLIS_PER_DAY ;
2014-09-17 13:29:58 +02:00
for ( MPlayer mplayer : this . getAll ( ) )
2014-09-17 13:17:33 +02:00
{
2014-09-17 13:29:58 +02:00
Long lastPlayed = Mixin . getLastPlayed ( mplayer . getId ( ) ) ;
2014-09-17 13:17:33 +02:00
if ( lastPlayed = = null ) continue ;
2014-09-17 13:29:58 +02:00
if ( mplayer . isOnline ( ) ) continue ;
2014-09-17 13:17:33 +02:00
if ( now - lastPlayed < = toleranceMillis ) continue ;
if ( MConf . get ( ) . logFactionLeave | | MConf . get ( ) . logFactionKick )
{
2014-09-17 13:29:58 +02:00
Factions . get ( ) . log ( " Player " + mplayer . getName ( ) + " was auto-removed due to inactivity. " ) ;
2014-09-17 13:17:33 +02:00
}
// if player is faction leader, sort out the faction since he's going away
2014-09-17 13:29:58 +02:00
if ( mplayer . getRole ( ) = = Rel . LEADER )
2014-09-17 13:17:33 +02:00
{
2014-09-17 13:29:58 +02:00
Faction faction = mplayer . getFaction ( ) ;
2014-09-17 13:17:33 +02:00
if ( faction ! = null )
{
2014-09-17 13:29:58 +02:00
mplayer . getFaction ( ) . promoteNewLeader ( ) ;
2014-09-17 13:17:33 +02:00
}
}
2014-09-17 13:29:58 +02:00
mplayer . leave ( ) ;
mplayer . detach ( ) ;
2014-09-17 13:17:33 +02:00
}
}
/ *
// 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.
2014-09-17 13:29:58 +02:00
Type type = new TypeToken < Map < String , MPlayer > > ( ) { } . getType ( ) ;
Map < String , MPlayer > id2mplayer = Factions . get ( ) . gson . fromJson ( DiscUtil . readCatch ( oldFile ) , type ) ;
2014-09-17 13:17:33 +02:00
// The Coll
2014-09-17 13:29:58 +02:00
MPlayerColl coll = this . getForUniverse ( MassiveCore . DEFAULT ) ;
2014-09-17 13:17:33 +02:00
// Set the data
2014-09-17 13:29:58 +02:00
for ( Entry < String , MPlayer > entry : id2mplayer . entrySet ( ) )
2014-09-17 13:17:33 +02:00
{
String playerId = entry . getKey ( ) ;
2014-09-17 13:29:58 +02:00
MPlayer mplayer = entry . getValue ( ) ;
coll . attach ( mplayer , playerId ) ;
2014-09-17 13:17:33 +02:00
}
// Mark as migrated
oldFile . renameTo ( newFile ) ;
}
// -------------------------------------------- //
// EXTRAS
// -------------------------------------------- //
public void clean ( )
{
2014-09-17 13:29:58 +02:00
for ( MPlayerColl coll : this . getColls ( ) )
2014-09-17 13:17:33 +02:00
{
coll . clean ( ) ;
}
}
* /
2013-04-22 21:00:00 +02:00
}