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

97 lines
3.3 KiB
Java
Raw Normal View History

package com.massivecraft.factions.cmd;
import com.massivecraft.factions.Perm;
import com.massivecraft.factions.cmd.type.TypeFaction;
import com.massivecraft.factions.entity.Faction;
2017-04-20 11:05:56 +02:00
import com.massivecraft.factions.entity.Invitation;
import com.massivecraft.factions.entity.MPerm;
2015-02-12 12:00:55 +01:00
import com.massivecraft.massivecore.MassiveException;
2017-04-20 11:05:56 +02:00
import com.massivecraft.massivecore.collections.MassiveList;
2015-11-06 02:10:29 +01:00
import com.massivecraft.massivecore.command.Parameter;
2017-04-20 11:05:56 +02:00
import com.massivecraft.massivecore.comparator.ComparatorSmart;
import com.massivecraft.massivecore.mixin.MixinDisplayName;
2015-09-05 13:44:24 +02:00
import com.massivecraft.massivecore.pager.Pager;
import com.massivecraft.massivecore.pager.Stringifier;
2017-04-20 11:05:56 +02:00
import com.massivecraft.massivecore.util.TimeDiffUtil;
import com.massivecraft.massivecore.util.TimeUnit;
import com.massivecraft.massivecore.util.Txt;
2017-04-20 11:05:56 +02:00
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
2017-03-24 13:05:58 +01:00
import java.util.List;
2017-04-20 11:05:56 +02:00
import java.util.Map.Entry;
2017-03-24 13:05:58 +01:00
public class CmdFactionsInviteList extends FactionsCommand
{
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public CmdFactionsInviteList()
{
// Parameters
this.addParameter(Parameter.getPage());
this.addParameter(TypeFaction.get(), "faction", "you");
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
2015-02-12 12:00:55 +01:00
public void perform() throws MassiveException
{
// Args
2015-05-13 23:00:53 +02:00
int page = this.readArg();
2015-05-06 17:04:35 +02:00
Faction faction = this.readArg(msenderFaction);
if ( faction != msenderFaction && ! Perm.INVITE_LIST_OTHER.has(sender, true)) return;
// MPerm
if ( ! MPerm.getPermInvite().has(msender, msenderFaction, true)) return;
2015-09-05 13:44:24 +02:00
// Pager Create
2017-04-20 11:05:56 +02:00
final List<Entry<String, Invitation>> invitations = new MassiveList<>(faction.getInvitations().entrySet());
Collections.sort(invitations, new Comparator<Entry<String, Invitation>>()
{
@Override
public int compare(Entry<String, Invitation> i1, Entry<String, Invitation> i2)
{
return ComparatorSmart.get().compare(i2.getValue().getCreationMillis(), i1.getValue().getCreationMillis());
}
});
final long now = System.currentTimeMillis();
final Pager<Entry<String, Invitation>> pager = new Pager<>(this, "Invited Players List", page, invitations, new Stringifier<Entry<String, Invitation>>()
2017-03-24 14:03:29 +01:00
{
2017-04-20 11:05:56 +02:00
public String toString(Entry<String, Invitation> entry, int index)
{
2017-04-20 11:05:56 +02:00
String inviteeId = entry.getKey();
String inviterId = entry.getValue().getInviterId();
String inviteeDisplayName = MixinDisplayName.get().getDisplayName(inviteeId, sender);
String inviterDisplayName = inviterId != null ? MixinDisplayName.get().getDisplayName(inviterId, sender) : Txt.parse("<silver>unknown");
String ageDesc = "";
if (entry.getValue().getCreationMillis() != null)
{
2017-04-20 11:05:56 +02:00
long millis = now - entry.getValue().getCreationMillis();
LinkedHashMap<TimeUnit, Long> ageUnitcounts = TimeDiffUtil.limit(TimeDiffUtil.unitcounts(millis, TimeUnit.getAllButMillis()), 2);
ageDesc = TimeDiffUtil.formatedMinimal(ageUnitcounts, "<i>");
ageDesc = " " + ageDesc + Txt.parse(" ago");
}
2017-04-20 11:05:56 +02:00
return Txt.parse("%s<i> was invited by %s<reset>%s<i>.", inviteeDisplayName, inviterDisplayName, ageDesc);
}
});
2015-09-05 13:44:24 +02:00
// Pager Message
pager.message();
}
}