Factions/src/main/java/com/massivecraft/factions/entity/MPlayerColl.java

124 lines
3.6 KiB
Java
Raw Normal View History

package com.massivecraft.factions.entity;
import com.massivecraft.factions.Const;
import com.massivecraft.factions.Factions;
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;
import com.massivecraft.massivecore.util.IdUtil;
import com.massivecraft.massivecore.util.TimeUnit;
import com.massivecraft.massivecore.util.Txt;
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());
}
// -------------------------------------------- //
// 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:29:58 +02:00
String factionId = mplayer.getFactionId();
if (FactionColl.get().containsId(factionId)) continue;
2014-09-17 13:29:58 +02:00
mplayer.resetFactionData();
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);
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:29:58 +02:00
Long lastPlayed = Mixin.getLastPlayed(mplayer.getId());
if (lastPlayed == null) continue;
2014-09-17 13:29:58 +02:00
if (mplayer.isOnline()) continue;
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.");
}
// 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:29:58 +02:00
Faction faction = mplayer.getFaction();
if (faction != null)
{
2014-09-17 13:29:58 +02:00
mplayer.getFaction().promoteNewLeader();
}
}
2014-09-17 13:29:58 +02:00
mplayer.leave();
mplayer.detach();
}
}
/*
// 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);
// The Coll
2014-09-17 13:29:58 +02:00
MPlayerColl coll = this.getForUniverse(MassiveCore.DEFAULT);
// Set the data
2014-09-17 13:29:58 +02:00
for (Entry<String, MPlayer> entry : id2mplayer.entrySet())
{
String playerId = entry.getKey();
2014-09-17 13:29:58 +02:00
MPlayer mplayer = entry.getValue();
coll.attach(mplayer, playerId);
}
// Mark as migrated
oldFile.renameTo(newFile);
}
// -------------------------------------------- //
// EXTRAS
// -------------------------------------------- //
public void clean()
{
2014-09-17 13:29:58 +02:00
for (MPlayerColl coll : this.getColls())
{
coll.clean();
}
}
*/
}