Add getPageMson and Msonifier to Massivecore

This commit is contained in:
ulumulu1510 2015-05-26 07:44:38 +02:00 committed by Olof Larsson
parent 6a6752fdb6
commit 861a431935
4 changed files with 230 additions and 35 deletions

View File

@ -0,0 +1,8 @@
package com.massivecraft.massivecore.pager;
import com.massivecraft.massivecore.mson.Mson;
public interface Msonifier<T>
{
public Mson toMson(T item, int index);
}

View File

@ -1,5 +1,8 @@
package com.massivecraft.massivecore.pager;
import com.massivecraft.massivecore.cmd.MassiveCommand;
import com.massivecraft.massivecore.mson.Mson;
import java.util.Collection;
import java.util.List;
@ -29,4 +32,10 @@ public interface Pager<T>
public String getMessageInvalid();
public List<String> getPageTxt(int number, String title, Stringifier<T> stringifier);
// -------------------------------------------- //
// MSON
// -------------------------------------------- //
public List<Mson> getPageMson(int number, String title, Msonifier<T> msonifier, MassiveCommand command, List<String> args);
}

View File

@ -3,6 +3,8 @@ package com.massivecraft.massivecore.pager;
import java.util.ArrayList;
import java.util.List;
import com.massivecraft.massivecore.cmd.MassiveCommand;
import com.massivecraft.massivecore.mson.Mson;
import com.massivecraft.massivecore.util.Txt;
public abstract class PagerAbstract<T> implements Pager<T>
@ -35,14 +37,11 @@ public abstract class PagerAbstract<T> implements Pager<T>
// Return null if the page number is invalid
if (!this.isValid(number)) return null;
// Create return value
List<T> ret = new ArrayList<T>();
// Forge list from collection
List<T> items = null;
if (this.getItems() instanceof List)
{
items = (List<T>)this.getItems();
items = (List<T>) this.getItems();
}
else
{
@ -60,10 +59,7 @@ public abstract class PagerAbstract<T> implements Pager<T>
}
// Pick them
ret.addAll(items.subList(from, to));
// Return return value
return ret;
return items.subList(from, to);
}
// -------------------------------------------- //
@ -72,23 +68,12 @@ public abstract class PagerAbstract<T> implements Pager<T>
public String getMessageEmpty()
{
return Txt.parse("<i>Sorry, no pages available.");
return Txt.getMessageEmpty().toPlain(true);
}
public String getMessageInvalid()
{
if (this.size() == 0)
{
return this.getMessageEmpty();
}
else if (this.size() == 1)
{
return Txt.parse("<b>Invalid, there is only one page.", this.size());
}
else
{
return Txt.parse("<b>Invalid, page must be between 1 and %d.", this.size());
}
return Txt.getMessageInvalid(this.size()).toPlain(true);
}
@Override
@ -96,11 +81,37 @@ public abstract class PagerAbstract<T> implements Pager<T>
{
List<String> ret = new ArrayList<String>();
ret.add(Txt.titleize(title + Txt.parse("<a>") + " " + number + "/" + this.size()));
List<Mson> msons = getPageMson(number, title, new Msonifier<T>(){
@Override
public Mson toMson(T item, int index)
{
return Mson.mson(stringifier.toString(item, index));
}
}, null, null);
for (Mson mson : msons)
{
ret.add(mson.toPlain(true));
}
return ret;
}
// -------------------------------------------- //
// Mson
// -------------------------------------------- //
public List<Mson> getPageMson(int number, String title, Msonifier<T> msonifier, MassiveCommand command, List<String> args)
{
List<Mson> ret = new ArrayList<Mson>();
ret.add(Txt.titleizeMson(title, this.size(), number, command, args));
if (this.isEmpty())
{
ret.add(this.getMessageEmpty());
ret.add(Txt.getMessageEmpty());
return ret;
}
@ -108,20 +119,20 @@ public abstract class PagerAbstract<T> implements Pager<T>
if (pageItems == null)
{
ret.add(this.getMessageInvalid());
ret.add(Txt.getMessageInvalid(this.size()));
return ret;
}
int index = (number - 1) * this.getItemsPerPage();
for (T pageItem : pageItems)
{
if (stringifier != null)
if (msonifier != null)
{
ret.add(stringifier.toString(pageItem, index));
ret.add(msonifier.toMson(pageItem, index));
}
else
{
ret.add(pageItem.toString());
ret.add(Mson.mson(pageItem.toString()));
}
index++;
}
@ -129,5 +140,4 @@ public abstract class PagerAbstract<T> implements Pager<T>
return ret;
}
}

View File

@ -21,6 +21,10 @@ import org.bukkit.inventory.meta.ItemMeta;
import com.massivecraft.massivecore.Predictate;
import com.massivecraft.massivecore.PredictateStartsWithIgnoreCase;
import com.massivecraft.massivecore.cmd.MassiveCommand;
import com.massivecraft.massivecore.mson.Mson;
import static com.massivecraft.massivecore.mson.Mson.mson;
public class Txt
{
@ -452,37 +456,201 @@ public class Txt
return parse("<a>")+center;
}
public static ArrayList<String> getPage(List<String> lines, int pageHumanBased, String title)
public static Mson getMessageEmpty()
{
return mson("Sorry, no pages available.").color(ChatColor.YELLOW);
}
public static Mson getMessageInvalid(int size)
{
if (size == 0)
{
return getMessageEmpty();
}
else if (size == 1)
{
return mson("Invalid, there is only one page.").color(ChatColor.RED);
}
else
{
return Mson.format("Invalid, page must be between 1 and %d.", size).color(ChatColor.RED);
}
}
public static List<String> getPage(List<String> lines, int pageHumanBased, String title)
{
return getPage(lines, pageHumanBased, title, PAGEHEIGHT_PLAYER);
}
public static ArrayList<String> getPage(List<String> lines, int pageHumanBased, String title, CommandSender sender)
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 ArrayList<String> getPage(List<String> lines, int pageHumanBased, String title, int pageheight)
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);
ret.add(titleize(title+parse("<a>")+" "+pageHumanBased+"/"+pagecount));
title = titleize(title + parse("<a>") + " " + pageHumanBased + "/" + pagecount);
ret.add(title);
if (pagecount == 0)
{
ret.add(parse("<i>Sorry. No Pages available."));
ret.add(getMessageEmpty().toPlain(true));
return ret;
}
else if (pageZeroBased < 0 || pageHumanBased > pagecount)
{
ret.add(parse("<i>Invalid page. Must be between 1 and "+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));
// Math
String title = str + " " + "[<<] [<]" + pageHumanBased + "/" + pagecount + "[>] [>>]";
String center = ".[ " + title + " ].";
int centerlen = center.length();
int pivot = titleizeLine.length() / 2;
int eatLeft = (centerlen / 2) - titleizeBalance;
int eatRight = (centerlen - eatLeft) + titleizeBalance;
// Mson
Mson centerMson = mson(
mson(".[ ").color(ChatColor.GOLD),
mson(str + " ").color(ChatColor.DARK_GREEN),
getFlipSection(pagecount, pageHumanBased, args, command),
mson(" ].").color(ChatColor.GOLD)
);
if (eatLeft < pivot)
{
Mson ret = mson(
mson(titleizeLine.substring(0, pivot - eatLeft)).color(ChatColor.GOLD),
centerMson,
mson(titleizeLine.substring(pivot + eatRight)).color(ChatColor.GOLD)
);
return ret;
}
else
{
return centerMson;
}
}
private static Mson getFlipSection(int pagecount, int pageHumanBased, List<String> args, MassiveCommand command)
{
// Construct Mson
Mson start = mson("[<<] ").color(ChatColor.GRAY);
Mson backward = mson("[<] ").color(ChatColor.GRAY);
Mson forward = mson(" [>]").color(ChatColor.GRAY);
Mson end = mson(" [>>]").color(ChatColor.GRAY);
// Set flip page backward commands
if (pageHumanBased > 1)
{
start = setFlipPageCommand(start, pageHumanBased, 1, args, command);
backward = setFlipPageCommand(backward, pageHumanBased, pageHumanBased - 1, args, command);
}
// Set flip page forward commands
if (pagecount > pageHumanBased)
{
forward = setFlipPageCommand(forward, pageHumanBased, pageHumanBased + 1, args, command);
end = setFlipPageCommand(end, pageHumanBased, pagecount, args, command);
}
Mson flipMson = mson(
start,
backward,
mson(pageHumanBased + "/" + pagecount).color(ChatColor.YELLOW),
forward,
end
);
return flipMson;
}
private static Mson setFlipPageCommand(Mson mson, int pageHumanBased, int destinationPage, List<String> args, MassiveCommand command)
{
String number = String.valueOf(destinationPage);
String oldNumber = String.valueOf(pageHumanBased);
String tooltip = "<i>Click to <c>%s<i>.";
String commandLine;
mson = mson.color(ChatColor.AQUA);
if (args != null && args.contains(oldNumber))
{
List<String> arguments = new ArrayList<String>(args);
arguments.set(arguments.indexOf(oldNumber), number);
commandLine = command.getCommandLine(arguments);
}
else
{
commandLine = command.getCommandLine(number);
}
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;
int to = from + pageheight;
if (to > lines.size())
{
to = lines.size();