Change Txt.getPage to taking List<?> and Pager to Mson only

This commit is contained in:
ulumulu1510 2015-09-05 11:09:00 +02:00 committed by Olof Larsson
parent ab88ed8176
commit 74553bcf72
6 changed files with 130 additions and 155 deletions

View File

@ -56,7 +56,7 @@ public class HelpCommand extends MassiveCommand
}
// Send Lines
message(Txt.getPage(lines, page, "Help for command \"" + parentCommand.getAliases().get(0) + "\"", sender));
message(Txt.getPage(lines, page, "Help for command \"" + parentCommand.getAliases().get(0) + "\"", this));
}
}

View File

@ -50,7 +50,7 @@ public class CmdMassiveCoreUsysAspectList extends MassiveCommand
}
// Send them
this.message(Txt.getPage(lines, page, "Aspect List", sender));
this.message(Txt.getPage(lines, page, "Aspect List", this));
}
}

View File

@ -49,7 +49,7 @@ public class CmdMassiveCoreUsysMultiverseList extends MassiveCommand
}
// Send them
this.message(Txt.getPage(lines, page, "Multiverse List", sender));
this.message(Txt.getPage(lines, page, "Multiverse List", this));
}
}

View File

@ -24,19 +24,18 @@ public interface Pager<T>
public boolean isEmpty();
public List<T> get(int number);
// -------------------------------------------- //
// TXT
// -------------------------------------------- //
public String getMessageEmpty();
public String getMessageInvalid();
public List<String> getPageTxt(int number, String title, Stringifier<T> stringifier);
// -------------------------------------------- //
// MSON
// -------------------------------------------- //
public List<Mson> getPageMson(int number, String title, MassiveCommand command, List<String> args, Msonifier<T> msonifier);
public List<Mson> getPageMson(int number, String title, MassiveCommand command, Msonifier<T> msonifier);
public List<Mson> getPage(int number, String title, MassiveCommand command, List<String> args, Msonifier<T> msonifier);
public List<Mson> getPage(int number, String title, MassiveCommand command, Msonifier<T> msonifier);
// -------------------------------------------- //
// STRING
// -------------------------------------------- //
public List<Mson> getPage(int number, String title, MassiveCommand command, List<String> args, Stringifier<T> stringifier);
public List<Mson> getPage(int number, String title, MassiveCommand command, Stringifier<T> stringifier);
}

View File

@ -66,90 +66,73 @@ public abstract class PagerAbstract<T> implements Pager<T>
}
// -------------------------------------------- //
// TXT
// MSON
// -------------------------------------------- //
@Override
public String getMessageEmpty()
{
return Txt.getMessageEmpty().toPlain(true);
}
@Override
public String getMessageInvalid()
{
return Txt.getMessageInvalid(this.size()).toPlain(true);
}
@Override
public List<String> getPageTxt(int number, String title, final Stringifier<T> stringifier)
{
List<String> ret = new ArrayList<String>();
List<Mson> msons = getPageMson(number, title, null, null, new Msonifier<T>(){
@Override
public Mson toMson(T item, int index)
{
return Mson.mson(stringifier.toString(item, index));
}
});
for (Mson mson : msons)
{
ret.add(mson.toPlain(true));
}
return ret;
}
// -------------------------------------------- //
// Mson
// -------------------------------------------- //
@Override
public List<Mson> getPageMson(int number, String title, MassiveCommand command, List<String> args, Msonifier<T> msonifier)
public List<Mson> getPage(int number, String title, MassiveCommand command, List<String> args, Msonifier<T> msonifier)
{
// Create ret
List<Mson> ret = new ArrayList<Mson>();
// Add title
ret.add(Txt.titleizeMson(title, this.size(), number, command, args));
// Check empty
if (this.isEmpty())
{
ret.add(Txt.getMessageEmpty());
return ret;
}
// Get items
List<T> pageItems = this.get(number);
// Check invalid
if (pageItems == null)
{
ret.add(Txt.getMessageInvalid(this.size()));
return ret;
}
// Add items
int index = (number - 1) * this.getItemsPerPage();
for (T pageItem : pageItems)
{
if (msonifier != null)
{
ret.add(msonifier.toMson(pageItem, index));
}
else
{
ret.add(Mson.mson(pageItem.toString()));
}
ret.add(msonifier.toMson(pageItem, index));
index++;
}
// Return ret
return ret;
}
@Override
public List<Mson> getPageMson(int number, String title, MassiveCommand command, Msonifier<T> msonifier)
public List<Mson> getPage(int number, String title, MassiveCommand command, Msonifier<T> msonifier)
{
return this.getPageMson(number, title, command, command.getArgs(), msonifier);
return this.getPage(number, title, command, command.getArgs(), msonifier);
}
// -------------------------------------------- //
// STRING
// -------------------------------------------- //
@Override
public List<Mson> getPage(int number, String title, MassiveCommand command, List<String> args, Stringifier<T> stringifier)
{
return this.getPage(number, title, command, args, new Msonifier<T>(){
@Override
public Mson toMson(T item, int index)
{
return Mson.fromParsedMessage(stringifier.toString(item, index));
}
});
}
@Override
public List<Mson> getPage(int number, String title, MassiveCommand command, Stringifier<T> stringifier)
{
return this.getPage(number, title, command, command.getArgs(), stringifier);
}
}

View File

@ -477,39 +477,6 @@ public class Txt
}
}
public static List<String> getPage(List<String> lines, int pageHumanBased, String title)
{
return getPage(lines, pageHumanBased, title, PAGEHEIGHT_PLAYER);
}
public static List<String> getPage(List<String> lines, int pageHumanBased, String title, CommandSender sender)
{
return getPage(lines, pageHumanBased, title, (sender instanceof Player) ? Txt.PAGEHEIGHT_PLAYER : Txt.PAGEHEIGHT_CONSOLE);
}
public static List<String> getPage(List<String> lines, int pageHumanBased, String title, int pageheight)
{
ArrayList<String> ret = new ArrayList<String>();
int pageZeroBased = pageHumanBased - 1;
int pagecount = (int)Math.ceil(((double)lines.size()) / pageheight);
title = titleize(title + parse("<a>") + " " + pageHumanBased + "/" + pagecount);
ret.add(title);
if (pagecount == 0)
{
ret.add(getMessageEmpty().toPlain(true));
return ret;
}
else if (pageZeroBased < 0 || pageHumanBased > pagecount)
{
ret.add(getMessageInvalid(pagecount).toPlain(true));
return ret;
}
return createPage(lines, pageHumanBased, title, pageheight);
}
public static Mson titleizeMson(String str, int pagecount, int pageHumanBased, MassiveCommand command, List<String> args)
{
if (command == null) return mson(titleize(str + parse("<a>") + " " + pageHumanBased + "/" + pagecount));
@ -546,6 +513,90 @@ public class Txt
}
}
public static List<Mson> getPage(List<?> lines, int pageHumanBased, String title)
{
return getPage(lines, pageHumanBased, title, PAGEHEIGHT_PLAYER, null, null);
}
public static List<Mson> getPage(List<?> lines, int pageHumanBased, String title, CommandSender sender)
{
return getPage(lines, pageHumanBased, title, (sender instanceof Player) ? Txt.PAGEHEIGHT_PLAYER : Txt.PAGEHEIGHT_CONSOLE, null, null);
}
public static List<Mson> getPage(List<?> lines, int pageHumanBased, String title, MassiveCommand command)
{
return getPage(lines, pageHumanBased, title, command, command.getArgs());
}
public static List<Mson> getPage(List<?> lines, int pageHumanBased, String title, MassiveCommand command, List<String> args)
{
return getPage(lines, pageHumanBased, title, (command.sender instanceof Player) ? Txt.PAGEHEIGHT_PLAYER : Txt.PAGEHEIGHT_CONSOLE, command, args);
}
@SuppressWarnings("unchecked")
public static List<Mson> getPage(List<?> lines, int pageHumanBased, String title, int pageheight, MassiveCommand command, List<String> args)
{
// Check if command is present
boolean flipPage = command != null;
// If command is present, reduce pageheight in favor of flipsection
if (flipPage) pageheight--;
// Create ret
ArrayList<Mson> ret = new ArrayList<Mson>();
int pageZeroBased = pageHumanBased - 1;
int pagecount = (int)Math.ceil(((double)lines.size()) / pageheight);
// Add title
Mson msonTitle = Txt.titleizeMson(title, pagecount, pageHumanBased, command, args);
ret.add(msonTitle);
// Check empty and invalid
if (pagecount == 0)
{
ret.add(getMessageEmpty());
return ret;
}
else if (pageZeroBased < 0 || pageHumanBased > pagecount)
{
ret.add(getMessageInvalid(pagecount));
return ret;
}
// Get lines
int from = pageZeroBased * pageheight;
int to = from + pageheight;
if (to > lines.size())
{
to = lines.size();
}
// Check object type and add lines
Object first = lines.get(0);
if (first instanceof String)
{
for (String line : (List<String>) lines.subList(from, to))
{
ret.add(Mson.fromParsedMessage(line));
}
}
else if (first instanceof Mson)
{
ret.addAll((List<Mson>) lines.subList(from, to));
}
else
{
throw new IllegalArgumentException("The lines must be either String or Mson.");
}
// Add flipsection if command is present
if (flipPage) ret.add(getFlipSection(pagecount, pageHumanBased, args, command));
// Return ret
return ret;
}
private static Mson getFlipSection(int pagecount, int pageHumanBased, List<String> args, MassiveCommand command)
{
// Construct Mson
@ -601,64 +652,6 @@ public class Txt
return mson.command(commandLine).tooltip(Txt.parse(tooltip, commandLine));
}
public static List<Mson> getPageMson(List<Mson> lines, int pageHumanBased, String title, MassiveCommand command, List<String> args)
{
return getPageMson(lines, pageHumanBased, title, PAGEHEIGHT_PLAYER, command, args);
}
public static List<Mson> getPageMson(List<Mson> lines, int pageHumanBased, String title, CommandSender sender, MassiveCommand command, List<String> args)
{
return getPageMson(lines, pageHumanBased, title, (sender instanceof Player) ? Txt.PAGEHEIGHT_PLAYER : Txt.PAGEHEIGHT_CONSOLE, command, args);
}
public static List<Mson> getPageMson(List<Mson> lines, int pageHumanBased, String title, int pageheight, MassiveCommand command, List<String> args)
{
// reduce pageheight in favor of flipsection
pageheight--;
ArrayList<Mson> ret = new ArrayList<Mson>();
int pageZeroBased = pageHumanBased - 1;
int pagecount = (int)Math.ceil(((double)lines.size()) / pageheight);
Mson msonTitle = Txt.titleizeMson(title, pagecount, pageHumanBased, command, args);
ret.add(msonTitle);
if (pagecount == 0)
{
ret.add(getMessageEmpty());
return ret;
}
else if (pageZeroBased < 0 || pageHumanBased > pagecount)
{
ret.add(getMessageInvalid(pagecount));
return ret;
}
List<Mson> page = createPage(lines, pageHumanBased, msonTitle, pageheight);
page.add(getFlipSection(pagecount, pageHumanBased, args, command));
return page;
}
private static <T> List<T> createPage(List<T> lines, int pageHumanBased, T title, int pageheight)
{
ArrayList<T> ret = new ArrayList<T>();
int pageZeroBased = pageHumanBased - 1;
ret.add(title);
int from = pageZeroBased * pageheight;
int to = from + pageheight;
if (to > lines.size())
{
to = lines.size();
}
ret.addAll(lines.subList(from, to));
return ret;
}
// -------------------------------------------- //
// Describing Time
// -------------------------------------------- //