Add getPageMson and Msonifier to Massivecore
This commit is contained in:
parent
6a6752fdb6
commit
861a431935
8
src/com/massivecraft/massivecore/pager/Msonifier.java
Normal file
8
src/com/massivecraft/massivecore/pager/Msonifier.java
Normal 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);
|
||||||
|
}
|
@ -1,5 +1,8 @@
|
|||||||
package com.massivecraft.massivecore.pager;
|
package com.massivecraft.massivecore.pager;
|
||||||
|
|
||||||
|
import com.massivecraft.massivecore.cmd.MassiveCommand;
|
||||||
|
import com.massivecraft.massivecore.mson.Mson;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -29,4 +32,10 @@ public interface Pager<T>
|
|||||||
public String getMessageInvalid();
|
public String getMessageInvalid();
|
||||||
public List<String> getPageTxt(int number, String title, Stringifier<T> stringifier);
|
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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,8 @@ package com.massivecraft.massivecore.pager;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.massivecraft.massivecore.cmd.MassiveCommand;
|
||||||
|
import com.massivecraft.massivecore.mson.Mson;
|
||||||
import com.massivecraft.massivecore.util.Txt;
|
import com.massivecraft.massivecore.util.Txt;
|
||||||
|
|
||||||
public abstract class PagerAbstract<T> implements Pager<T>
|
public abstract class PagerAbstract<T> implements Pager<T>
|
||||||
@ -35,9 +37,6 @@ public abstract class PagerAbstract<T> implements Pager<T>
|
|||||||
// Return null if the page number is invalid
|
// Return null if the page number is invalid
|
||||||
if (!this.isValid(number)) return null;
|
if (!this.isValid(number)) return null;
|
||||||
|
|
||||||
// Create return value
|
|
||||||
List<T> ret = new ArrayList<T>();
|
|
||||||
|
|
||||||
// Forge list from collection
|
// Forge list from collection
|
||||||
List<T> items = null;
|
List<T> items = null;
|
||||||
if (this.getItems() instanceof List)
|
if (this.getItems() instanceof List)
|
||||||
@ -60,10 +59,7 @@ public abstract class PagerAbstract<T> implements Pager<T>
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Pick them
|
// Pick them
|
||||||
ret.addAll(items.subList(from, to));
|
return items.subList(from, to);
|
||||||
|
|
||||||
// Return return value
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
@ -72,23 +68,12 @@ public abstract class PagerAbstract<T> implements Pager<T>
|
|||||||
|
|
||||||
public String getMessageEmpty()
|
public String getMessageEmpty()
|
||||||
{
|
{
|
||||||
return Txt.parse("<i>Sorry, no pages available.");
|
return Txt.getMessageEmpty().toPlain(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getMessageInvalid()
|
public String getMessageInvalid()
|
||||||
{
|
{
|
||||||
if (this.size() == 0)
|
return Txt.getMessageInvalid(this.size()).toPlain(true);
|
||||||
{
|
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -96,11 +81,37 @@ public abstract class PagerAbstract<T> implements Pager<T>
|
|||||||
{
|
{
|
||||||
List<String> ret = new ArrayList<String>();
|
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())
|
if (this.isEmpty())
|
||||||
{
|
{
|
||||||
ret.add(this.getMessageEmpty());
|
ret.add(Txt.getMessageEmpty());
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,20 +119,20 @@ public abstract class PagerAbstract<T> implements Pager<T>
|
|||||||
|
|
||||||
if (pageItems == null)
|
if (pageItems == null)
|
||||||
{
|
{
|
||||||
ret.add(this.getMessageInvalid());
|
ret.add(Txt.getMessageInvalid(this.size()));
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int index = (number - 1) * this.getItemsPerPage();
|
int index = (number - 1) * this.getItemsPerPage();
|
||||||
for (T pageItem : pageItems)
|
for (T pageItem : pageItems)
|
||||||
{
|
{
|
||||||
if (stringifier != null)
|
if (msonifier != null)
|
||||||
{
|
{
|
||||||
ret.add(stringifier.toString(pageItem, index));
|
ret.add(msonifier.toMson(pageItem, index));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ret.add(pageItem.toString());
|
ret.add(Mson.mson(pageItem.toString()));
|
||||||
}
|
}
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
@ -129,5 +140,4 @@ public abstract class PagerAbstract<T> implements Pager<T>
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,10 @@ import org.bukkit.inventory.meta.ItemMeta;
|
|||||||
|
|
||||||
import com.massivecraft.massivecore.Predictate;
|
import com.massivecraft.massivecore.Predictate;
|
||||||
import com.massivecraft.massivecore.PredictateStartsWithIgnoreCase;
|
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
|
public class Txt
|
||||||
{
|
{
|
||||||
@ -452,35 +456,199 @@ public class Txt
|
|||||||
return parse("<a>")+center;
|
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);
|
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);
|
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>();
|
ArrayList<String> ret = new ArrayList<String>();
|
||||||
int pageZeroBased = pageHumanBased - 1;
|
int pageZeroBased = pageHumanBased - 1;
|
||||||
int pagecount = (int)Math.ceil(((double)lines.size()) / pageheight);
|
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)
|
if (pagecount == 0)
|
||||||
{
|
{
|
||||||
ret.add(parse("<i>Sorry. No Pages available."));
|
ret.add(getMessageEmpty().toPlain(true));
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
else if (pageZeroBased < 0 || pageHumanBased > pagecount)
|
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 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 from = pageZeroBased * pageheight;
|
||||||
int to = from + pageheight;
|
int to = from + pageheight;
|
||||||
if (to > lines.size())
|
if (to > lines.size())
|
||||||
|
Loading…
Reference in New Issue
Block a user