Factions/src/com/massivecraft/factions/cmd/CmdFactionsClean.java

168 lines
4.0 KiB
Java
Raw Normal View History

2017-03-24 20:19:04 +01:00
package com.massivecraft.factions.cmd;
import com.massivecraft.factions.Rel;
import com.massivecraft.factions.TerritoryAccess;
import com.massivecraft.factions.entity.Board;
2017-03-24 20:19:04 +01:00
import com.massivecraft.factions.entity.BoardColl;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.FactionColl;
2017-04-20 11:05:56 +02:00
import com.massivecraft.factions.entity.Invitation;
import com.massivecraft.factions.entity.MPerm;
import com.massivecraft.factions.entity.MPlayer;
2017-03-24 20:19:04 +01:00
import com.massivecraft.factions.entity.MPlayerColl;
import com.massivecraft.massivecore.MassiveException;
2017-05-28 17:56:37 +02:00
import com.massivecraft.massivecore.collections.MassiveSet;
import com.massivecraft.massivecore.ps.PS;
2017-04-20 11:05:56 +02:00
import com.massivecraft.massivecore.store.EntityInternalMap;
2017-03-24 20:19:04 +01:00
import com.massivecraft.massivecore.util.Txt;
import java.util.Iterator;
import java.util.Map.Entry;
2017-05-28 17:56:37 +02:00
import java.util.Set;
2017-03-24 20:19:04 +01:00
public class CmdFactionsClean extends FactionsCommand
{
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public void perform() throws MassiveException
{
Object message;
int count;
2017-03-24 20:19:04 +01:00
// Title
message = Txt.titleize("Factions Cleaner Results");
message(message);
// Yada
cleanMessage(this.cleanPlayer(), "player");
cleanMessage(this.cleanFactionInvites(), "faction invites");
cleanMessage(this.cleanFactionRelationWhishes(), "faction relation whishes");
cleanMessage(this.cleanBoardHost(), "chunk whole");
cleanMessage(this.cleanBoardGrant(), "chunk access");
}
// -------------------------------------------- //
// CLEAN
// -------------------------------------------- //
private void cleanMessage(int count, String name)
{
msg("<v>%d<k> %s", count, name);
}
private int cleanPlayer()
{
int ret = 0;
2017-03-24 20:19:04 +01:00
for (MPlayer mplayer : MPlayerColl.get().getAll())
{
if (!mplayer.isFactionOrphan()) continue;
mplayer.resetFactionData();
ret += 1;
}
return ret;
}
private int cleanFactionInvites()
{
int ret = 0;
for (Faction faction : FactionColl.get().getAll())
{
2017-04-20 11:05:56 +02:00
EntityInternalMap<Invitation> invitations = faction.getInvitations();
if (invitations.isEmpty()) continue;
2017-04-20 11:05:56 +02:00
ret += invitations.size();
2017-05-28 17:56:37 +02:00
Set<String> invitationIds = new MassiveSet<>(invitations.keySet());
for (String inviteId : invitationIds)
{
invitations.detachIdFixed(inviteId);
}
faction.changed();
}
return ret;
}
private int cleanFactionRelationWhishes()
{
int ret = 0;
for (Faction faction : FactionColl.get().getAll())
{
for (Iterator<Entry<String, Rel>> iterator = faction.getRelationWishes().entrySet().iterator(); iterator.hasNext();)
{
Entry<String, Rel> entry = iterator.next();
String factionId = entry.getKey();
if (FactionColl.get().containsId(factionId)) continue;
iterator.remove();
ret += 1;
faction.changed();
}
}
return ret;
}
private int cleanBoardHost()
{
int ret = 0;
for (Board board : BoardColl.get().getAll())
{
for (Entry<PS, TerritoryAccess> entry : board.getMap().entrySet())
{
PS ps = entry.getKey();
TerritoryAccess territoryAccess = entry.getValue();
String factionId = territoryAccess.getHostFactionId();
if (FactionColl.get().containsId(factionId)) continue;
board.removeAt(ps);
ret += 1;
}
}
return ret;
}
private int cleanBoardGrant()
{
int ret = 0;
for (Board board : BoardColl.get().getAll())
{
for (Entry<PS, TerritoryAccess> entry : board.getMap().entrySet())
{
PS ps = entry.getKey();
TerritoryAccess territoryAccess = entry.getValue();
boolean changed = false;
for (String grantedIds : territoryAccess.getGrantedIds())
{
if (MPerm.idToMPermableOptional(grantedIds).isPresent()) continue;
territoryAccess = territoryAccess.withGrantedId(grantedIds, false);
ret += 1;
changed = true;
}
if (changed)
{
board.setTerritoryAccessAt(ps, territoryAccess);
}
}
}
return ret;
2017-03-24 20:19:04 +01:00
}
}