Use Pager and Asynchronous execution for the f list command.

This commit is contained in:
Olof Larsson 2015-01-08 10:00:38 +01:00
parent e4aa8cb633
commit 747e9c1d3a

View File

@ -1,17 +1,22 @@
package com.massivecraft.factions.cmd; package com.massivecraft.factions.cmd;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import com.massivecraft.factions.FactionListComparator; import com.massivecraft.factions.FactionListComparator;
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.Perm; import com.massivecraft.factions.Perm;
import com.massivecraft.factions.entity.Faction; import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.FactionColl; import com.massivecraft.factions.entity.FactionColl;
import com.massivecraft.massivecore.cmd.arg.ARInteger; import com.massivecraft.massivecore.cmd.arg.ARInteger;
import com.massivecraft.massivecore.cmd.req.ReqHasPerm; import com.massivecraft.massivecore.cmd.req.ReqHasPerm;
import com.massivecraft.massivecore.mixin.Mixin;
import com.massivecraft.massivecore.pager.PagerSimple;
import com.massivecraft.massivecore.pager.Stringifier;
import com.massivecraft.massivecore.util.Txt; import com.massivecraft.massivecore.util.Txt;
public class CmdFactionsList extends FactionsCommand public class CmdFactionsList extends FactionsCommand
{ {
// -------------------------------------------- // // -------------------------------------------- //
@ -37,46 +42,49 @@ public class CmdFactionsList extends FactionsCommand
@Override @Override
public void perform() public void perform()
{ {
Integer pageHumanBased = this.arg(0, ARInteger.get(), 1); // Args
final Integer pageHumanBased = this.arg(0, ARInteger.get(), 1);
if (pageHumanBased == null) return; if (pageHumanBased == null) return;
// Create Messages // Create Pager
List<String> lines = new ArrayList<String>(); final List<Faction> timings = FactionColl.get().getAll(null, FactionListComparator.get());
final PagerSimple<Faction> pager = new PagerSimple<Faction>(timings, sender);
ArrayList<Faction> factionList = new ArrayList<Faction>(FactionColl.get().getAll(null, FactionListComparator.get())); // NOTE: The faction list is quite slow and mostly thread safe.
// We run it asynchronously to spare the primary server thread.
final int pageheight = 9; final CommandSender sender = this.sender;
Bukkit.getScheduler().runTaskAsynchronously(Factions.get(), new Runnable()
int pagecount = (factionList.size() / pageheight) + 1; {
if (pageHumanBased > pagecount) @Override
pageHumanBased = pagecount; public void run()
else if (pageHumanBased < 1) {
pageHumanBased = 1; // Use Pager
int start = (pageHumanBased - 1) * pageheight; List<String> messages = pager.getPageTxt(pageHumanBased, "Faction List", new Stringifier<Faction>() {
int end = start + pageheight; @Override
if (end > factionList.size()) public String toString(Faction faction)
end = factionList.size();
lines.add(Txt.titleize("Faction List "+pageHumanBased+"/"+pagecount));
for (Faction faction : factionList.subList(start, end))
{ {
if (faction.isNone()) if (faction.isNone())
{ {
lines.add(Txt.parse("<i>Factionless<i> %d online", FactionColl.get().getNone().getMPlayersWhereOnline(true).size())); return Txt.parse("<i>Factionless<i> %d online", FactionColl.get().getNone().getMPlayersWhereOnline(true).size());
continue;
} }
lines.add(Txt.parse("%s<i> %d/%d online, %d/%d/%d", else
{
return Txt.parse("%s<i> %d/%d online, %d/%d/%d",
faction.getName(msender), faction.getName(msender),
faction.getMPlayersWhereOnline(true).size(), faction.getMPlayersWhereOnline(true).size(),
faction.getMPlayers().size(), faction.getMPlayers().size(),
faction.getLandCount(), faction.getLandCount(),
faction.getPowerRounded(), faction.getPowerRounded(),
faction.getPowerMaxRounded()) faction.getPowerMaxRounded()
); );
} }
}
});
sendMessage(lines); // Send Messages
Mixin.messageOne(sender, messages);
}
});
} }
} }