2013-04-22 09:37:53 +02:00
|
|
|
package com.massivecraft.factions.entity;
|
2011-10-08 22:03:44 +02:00
|
|
|
|
2013-04-22 09:37:53 +02:00
|
|
|
import com.massivecraft.factions.ConfServer;
|
|
|
|
import com.massivecraft.factions.Factions;
|
|
|
|
import com.massivecraft.factions.Rel;
|
2013-04-18 12:25:05 +02:00
|
|
|
import com.massivecraft.mcore.mixin.Mixin;
|
2013-04-12 08:56:26 +02:00
|
|
|
import com.massivecraft.mcore.store.MStore;
|
|
|
|
import com.massivecraft.mcore.store.SenderColl;
|
2013-04-18 12:25:05 +02:00
|
|
|
import com.massivecraft.mcore.util.TimeUnit;
|
2011-10-08 22:03:44 +02:00
|
|
|
|
2013-04-22 17:59:51 +02:00
|
|
|
public class UPlayerColl extends SenderColl<UPlayer>
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
2013-04-12 08:56:26 +02:00
|
|
|
// -------------------------------------------- //
|
2013-04-22 12:26:13 +02:00
|
|
|
// CONSTRUCT
|
2013-04-12 08:56:26 +02:00
|
|
|
// -------------------------------------------- //
|
2011-10-08 22:03:44 +02:00
|
|
|
|
2013-04-22 17:59:51 +02:00
|
|
|
public UPlayerColl(String name)
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
2013-04-22 17:59:51 +02:00
|
|
|
super(name, UPlayer.class, MStore.getDb(ConfServer.dburi), Factions.get());
|
2011-10-08 22:03:44 +02:00
|
|
|
}
|
|
|
|
|
2013-04-12 08:56:26 +02:00
|
|
|
// -------------------------------------------- //
|
2013-04-12 09:47:43 +02:00
|
|
|
// OVERRIDE: COLL
|
2013-04-12 08:56:26 +02:00
|
|
|
// -------------------------------------------- //
|
|
|
|
|
2013-04-17 14:44:08 +02:00
|
|
|
@Override
|
2013-04-22 17:59:51 +02:00
|
|
|
protected synchronized String attach(UPlayer entity, Object oid, boolean noteChange)
|
2013-04-17 14:44:08 +02:00
|
|
|
{
|
|
|
|
String ret = super.attach(entity, oid, noteChange);
|
|
|
|
|
|
|
|
// If inited ...
|
|
|
|
if (!this.inited()) return ret;
|
2013-04-22 15:05:00 +02:00
|
|
|
if (!Factions.get().isDatabaseInitialized()) return ret;
|
2013-04-17 14:44:08 +02:00
|
|
|
|
|
|
|
// ... update the index.
|
|
|
|
Faction faction = entity.getFaction();
|
2013-04-22 17:59:51 +02:00
|
|
|
faction.uplayers.add(entity);
|
2013-04-17 14:44:08 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-04-22 17:59:51 +02:00
|
|
|
public UPlayer detachId(Object oid)
|
2013-04-17 14:44:08 +02:00
|
|
|
{
|
2013-04-25 10:27:39 +02:00
|
|
|
UPlayer ret = this.get(oid);
|
2013-04-17 14:44:08 +02:00
|
|
|
if (ret == null) return null;
|
|
|
|
|
|
|
|
// If inited ...
|
2013-04-25 10:27:39 +02:00
|
|
|
if (this.inited())
|
|
|
|
{
|
|
|
|
// ... update the index.
|
|
|
|
Faction faction = ret.getFaction();
|
|
|
|
faction.uplayers.remove(ret);
|
|
|
|
}
|
2013-04-17 14:44:08 +02:00
|
|
|
|
2013-04-25 10:27:39 +02:00
|
|
|
super.detachId(oid);
|
|
|
|
|
2013-04-17 14:44:08 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-04-12 08:56:26 +02:00
|
|
|
// -------------------------------------------- //
|
|
|
|
// EXTRAS
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
2011-10-08 22:03:44 +02:00
|
|
|
public void clean()
|
|
|
|
{
|
2013-04-22 17:59:51 +02:00
|
|
|
for (UPlayer uplayer : this.getAll())
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
2013-04-22 17:59:51 +02:00
|
|
|
if (FactionColls.get().get(this).containsId(uplayer.getFactionId())) continue;
|
2013-04-12 09:47:43 +02:00
|
|
|
|
2013-04-22 17:59:51 +02:00
|
|
|
Factions.get().log("Reset faction data (invalid faction) for player "+uplayer.getName());
|
2011-10-08 22:03:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-24 08:39:26 +02:00
|
|
|
public void removePlayerDataAfterInactiveDaysRoutine()
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
2013-04-24 08:39:26 +02:00
|
|
|
if (MConf.get().removePlayerDataAfterInactiveDays <= 0.0) return;
|
2013-04-18 12:25:05 +02:00
|
|
|
|
2011-10-08 22:03:44 +02:00
|
|
|
long now = System.currentTimeMillis();
|
2013-04-24 08:39:26 +02:00
|
|
|
double toleranceMillis = MConf.get().removePlayerDataAfterInactiveDays * TimeUnit.MILLIS_PER_DAY;
|
2011-10-08 22:03:44 +02:00
|
|
|
|
2013-04-22 17:59:51 +02:00
|
|
|
for (UPlayer uplayer : this.getAll())
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
2013-04-22 17:59:51 +02:00
|
|
|
Long lastPlayed = Mixin.getLastPlayed(uplayer.getId());
|
2013-04-18 12:25:05 +02:00
|
|
|
if (lastPlayed == null) continue;
|
|
|
|
|
2013-04-22 17:59:51 +02:00
|
|
|
if (uplayer.isOnline()) continue;
|
2013-04-18 13:36:52 +02:00
|
|
|
if (now - lastPlayed <= toleranceMillis) continue;
|
|
|
|
|
2013-04-22 10:05:03 +02:00
|
|
|
if (MConf.get().logFactionLeave || MConf.get().logFactionKick)
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
2013-04-22 17:59:51 +02:00
|
|
|
Factions.get().log("Player "+uplayer.getName()+" was auto-removed due to inactivity.");
|
2013-04-18 13:36:52 +02:00
|
|
|
}
|
2011-12-18 14:50:41 +01:00
|
|
|
|
2013-04-18 13:36:52 +02:00
|
|
|
// if player is faction leader, sort out the faction since he's going away
|
2013-04-22 17:59:51 +02:00
|
|
|
if (uplayer.getRole() == Rel.LEADER)
|
2013-04-18 13:36:52 +02:00
|
|
|
{
|
2013-04-22 17:59:51 +02:00
|
|
|
Faction faction = uplayer.getFaction();
|
2013-04-18 13:36:52 +02:00
|
|
|
if (faction != null)
|
2012-01-28 10:16:25 +01:00
|
|
|
{
|
2013-04-22 17:59:51 +02:00
|
|
|
uplayer.getFaction().promoteNewLeader();
|
2012-01-28 10:16:25 +01:00
|
|
|
}
|
2011-10-08 22:03:44 +02:00
|
|
|
}
|
2013-04-18 13:36:52 +02:00
|
|
|
|
2013-04-24 13:26:59 +02:00
|
|
|
uplayer.leave();
|
2013-04-22 17:59:51 +02:00
|
|
|
uplayer.detach();
|
2011-10-08 22:03:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|