From 0d9a3b47ba4eca62a1eb78942ddcd1d25fa97c87 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Sat, 5 Sep 2015 13:41:11 +0200 Subject: [PATCH] Pager Improvement. --- .../massivecraft/massivecore/pager/Pager.java | 257 +++++++++++++++++- .../massivecore/pager/PagerAbstract.java | 138 ---------- .../massivecore/pager/PagerSimple.java | 39 --- .../massivecraft/massivecore/util/Txt.java | 11 +- 4 files changed, 251 insertions(+), 194 deletions(-) delete mode 100644 src/com/massivecraft/massivecore/pager/PagerAbstract.java delete mode 100644 src/com/massivecraft/massivecore/pager/PagerSimple.java diff --git a/src/com/massivecraft/massivecore/pager/Pager.java b/src/com/massivecraft/massivecore/pager/Pager.java index e473a964..4dca57e1 100644 --- a/src/com/massivecraft/massivecore/pager/Pager.java +++ b/src/com/massivecraft/massivecore/pager/Pager.java @@ -1,41 +1,270 @@ package com.massivecraft.massivecore.pager; import com.massivecraft.massivecore.cmd.MassiveCommand; +import com.massivecraft.massivecore.mixin.Mixin; import com.massivecraft.massivecore.mson.Mson; +import com.massivecraft.massivecore.util.Txt; +import java.util.ArrayList; import java.util.Collection; import java.util.List; -public interface Pager +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Pager { // -------------------------------------------- // // DATA SUPPLY // -------------------------------------------- // - public Collection getItems(); - public int getItemsPerPage(); + // The command to use for back and forward buttons. + protected MassiveCommand command = null; + public boolean hasCommand() { return this.command != null; } + public Pager setCommand(MassiveCommand command) { this.command = command; return this; } + public MassiveCommand getCommand() { return this.command; } + + // The CommandSender for fallback height. + protected CommandSender sender = null; + public boolean hasSender() { return this.sender != null; } + public Pager setSender(CommandSender sender) { this.sender = sender; return this; } + public CommandSender getSender() { return this.sender; } + public CommandSender getSenderCalc() + { + CommandSender ret = this.getSender(); + if (ret != null) return ret; + + MassiveCommand command = this.getCommand(); + if (command != null) return command.sender; + + return null; + } + + // The args to use for back and forward buttons. + protected List args = null; + public boolean hasArgs() { return this.args != null; } + public Pager setArgs(List args) { this.args = args; return this; } + public List getArgs() { return this.args; } + public List getArgsCalc() + { + List ret = this.getArgs(); + if (ret != null) return ret; + + MassiveCommand command = this.getCommand(); + if (command != null) return new ArrayList(command.getArgs()); + + return null; + } + + // The page height. The asmount of items per page. + protected Integer height = null; + public boolean hasHeight() { return this.height != null; } + public Pager setHeight(Integer height) { this.height = height; return this; } + public Integer getHeight() { return this.height; } + public Integer getHeightCalc() + { + Integer ret = this.getHeight(); + if (ret != null) return ret; + + CommandSender sender = this.getSenderCalc(); + if (sender == null) return Txt.PAGEHEIGHT_PLAYER; + if (sender instanceof Player) return Txt.PAGEHEIGHT_PLAYER; + + return Txt.PAGEHEIGHT_CONSOLE; + } + + // The title to use at the top of the page. + protected String title = null; + public boolean hasTitle() { return this.title != null; } + public Pager setTitle(String title) { this.title = title; return this; } + public String getTitle() { return this.title; } + + // The page number we want to show. + protected Integer number = null; + public boolean hasNumber() { return this.number != null; } + public Pager setNumber(Integer number) { this.number = number; return this; } + public Integer getNumber() { return this.number; } + + // The items we are paging. + protected Collection items = null; + public boolean hasItems() { return this.items != null; } + public Pager setItems(Collection items) { this.items = items; return this; } + public Collection getItems() { return this.items; } + + // The method of converting from item to Mson. + protected Msonifier msonifier = null; + public boolean hasMsonifier() { return this.msonifier != null; } + public Pager setMsonifier(Msonifier msonifier) { this.msonifier = msonifier; return this; } + public Pager setMsonifier(final Stringifier stringifier) { this.msonifier = new Msonifier(){ + @Override + public Mson toMson(T item, int index) + { + return Mson.fromParsedMessage(stringifier.toString(item, index)); + } + }; return this; } + public Msonifier getMsonifier() { return this.msonifier; } + + // -------------------------------------------- // + // CALC + // -------------------------------------------- // + + public void calc() + { + this.setSender(this.getSenderCalc()); + this.setArgs(this.getArgsCalc()); + this.setHeight(this.getHeightCalc()); + } + + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public Pager() + { + + } + + public Pager(MassiveCommand command, String title, Integer number, Stringifier stringifier) + { + this(command, title, number, null, stringifier); + } + + public Pager(MassiveCommand command, String title, Integer number, Collection items, Stringifier stringifier) + { + this(command, title, number, items); + this.setMsonifier(stringifier); + } + + public Pager(MassiveCommand command, String title, Integer number) + { + this(command, title, number, (Collection)null); + } + + public Pager(MassiveCommand command, String title, Integer number, Collection items) + { + this(command, title, number, items, (Msonifier)null); + } + + public Pager(MassiveCommand command, String title, Integer number, Msonifier msonifier) + { + this(command, title, number, null, msonifier); + } + + public Pager(MassiveCommand command, String title, Integer number, Collection items, Msonifier msonifier) + { + this.command = command; + this.title = title; + this.number = number; + this.items = items; + this.msonifier = msonifier; + this.calc(); + } // -------------------------------------------- // // CORE // -------------------------------------------- // - public int size(); - public boolean isValid(int number); - public boolean isEmpty(); - public List get(int number); + public int size() + { + return (int) Math.ceil((double) this.getItems().size() / this.getHeight()); + } + + public boolean isValid(int number) + { + if (this.isEmpty()) return false; + if (number < 1) return false; + if (number > this.size()) return false; + return true; + } + + public boolean isEmpty() + { + return this.getItems().isEmpty(); + } + + @SuppressWarnings("unchecked") + public List getPage(int number) + { + // Return null if the page number is invalid + if ( ! this.isValid(number)) return null; + + // Forge list from collection + List items = null; + if (this.getItems() instanceof List) + { + items = (List) this.getItems(); + } + else + { + items = new ArrayList(this.getItems()); + } + + int index = number - 1; + + // Calculate from and to + int from = index * this.getHeight(); + int to = from + this.getHeight(); + if (to > items.size()) + { + to = items.size(); + } + + // Pick them + return items.subList(from, to); + } // -------------------------------------------- // - // MSON + // GET // -------------------------------------------- // - public List getPage(int number, String title, MassiveCommand command, List args, Msonifier msonifier); - public List getPage(int number, String title, MassiveCommand command, Msonifier msonifier); + public List get() + { + // Create ret + List ret = new ArrayList(); + + // Add title + ret.add(Txt.titleizeMson(this.getTitle(), this.size(), this.getNumber(), this.getCommand(), this.getArgs())); + + // Check empty + if (this.isEmpty()) + { + ret.add(Txt.getMessageEmpty()); + return ret; + } + + // Get items + List pageItems = this.getPage(this.getNumber()); + + // Check invalid + if (pageItems == null) + { + ret.add(Txt.getMessageInvalid(this.size())); + return ret; + } + + // Add items + int index = (this.getNumber() - 1) * this.getHeight(); + for (T pageItem : pageItems) + { + ret.add(this.getMsonifier().toMson(pageItem, index)); + index++; + } + + // Return ret + return ret; + } // -------------------------------------------- // - // STRING + // MESSAGE // -------------------------------------------- // - public List getPage(int number, String title, MassiveCommand command, List args, Stringifier stringifier); - public List getPage(int number, String title, MassiveCommand command, Stringifier stringifier); - + public void message() + { + // Get + List messages = this.get(); + + // Message + Mixin.messageOne(this.getSender(), messages); + } + } diff --git a/src/com/massivecraft/massivecore/pager/PagerAbstract.java b/src/com/massivecraft/massivecore/pager/PagerAbstract.java deleted file mode 100644 index 5f84e1df..00000000 --- a/src/com/massivecraft/massivecore/pager/PagerAbstract.java +++ /dev/null @@ -1,138 +0,0 @@ -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 implements Pager -{ - // -------------------------------------------- // - // CORE - // -------------------------------------------- // - - @Override - public int size() - { - return (int) Math.ceil((double) this.getItems().size() / this.getItemsPerPage()); - } - - @Override - public boolean isValid(int number) - { - if (this.isEmpty()) return false; - if (number < 1) return false; - if (number > this.size()) return false; - return true; - } - - @Override - public boolean isEmpty() - { - return this.getItems().isEmpty(); - } - - @SuppressWarnings("unchecked") - public List get(int number) - { - // Return null if the page number is invalid - if (!this.isValid(number)) return null; - - // Forge list from collection - List items = null; - if (this.getItems() instanceof List) - { - items = (List) this.getItems(); - } - else - { - items = new ArrayList(this.getItems()); - } - - int index = number - 1; - - // Calculate from and to - int from = index * this.getItemsPerPage(); - int to = from + this.getItemsPerPage(); - if (to > items.size()) - { - to = items.size(); - } - - // Pick them - return items.subList(from, to); - } - - // -------------------------------------------- // - // MSON - // -------------------------------------------- // - - @Override - public List getPage(int number, String title, MassiveCommand command, List args, Msonifier msonifier) - { - // Create ret - List ret = new ArrayList(); - - // 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 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) - { - ret.add(msonifier.toMson(pageItem, index)); - index++; - } - - // Return ret - return ret; - } - - @Override - public List getPage(int number, String title, MassiveCommand command, Msonifier msonifier) - { - return this.getPage(number, title, command, command.getArgs(), msonifier); - } - - // -------------------------------------------- // - // STRING - // -------------------------------------------- // - - @Override - public List getPage(int number, String title, MassiveCommand command, List args, Stringifier stringifier) - { - return this.getPage(number, title, command, args, new Msonifier(){ - @Override - public Mson toMson(T item, int index) - { - return Mson.fromParsedMessage(stringifier.toString(item, index)); - } - }); - } - - @Override - public List getPage(int number, String title, MassiveCommand command, Stringifier stringifier) - { - return this.getPage(number, title, command, command.getArgs(), stringifier); - } - -} diff --git a/src/com/massivecraft/massivecore/pager/PagerSimple.java b/src/com/massivecraft/massivecore/pager/PagerSimple.java deleted file mode 100644 index c3c51167..00000000 --- a/src/com/massivecraft/massivecore/pager/PagerSimple.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.massivecraft.massivecore.pager; - -import java.util.Collection; - -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -import com.massivecraft.massivecore.util.Txt; - -public class PagerSimple extends PagerAbstract -{ - // -------------------------------------------- // - // FIELDS - // -------------------------------------------- // - - private Collection items; - @Override public Collection getItems() { return items; } - public void setItems(Collection items) { this.items = items; } - - private int itemsPerPage; - @Override public int getItemsPerPage() { return this.itemsPerPage; } - public void setItemsPerPage(int itemsPerPage) { this.itemsPerPage = itemsPerPage; } - - // -------------------------------------------- // - // CONSTRUCT - // -------------------------------------------- // - - public PagerSimple(Collection items, int itemsPerPage) - { - this.items = items; - this.itemsPerPage = itemsPerPage; - } - - public PagerSimple(Collection items, CommandSender sender) - { - this(items, (sender instanceof Player) ? Txt.PAGEHEIGHT_PLAYER : Txt.PAGEHEIGHT_CONSOLE); - } - -} diff --git a/src/com/massivecraft/massivecore/util/Txt.java b/src/com/massivecraft/massivecore/util/Txt.java index 2a5e845e..8c7224a7 100644 --- a/src/com/massivecraft/massivecore/util/Txt.java +++ b/src/com/massivecraft/massivecore/util/Txt.java @@ -515,12 +515,12 @@ public class Txt public static List getPage(List lines, int pageHumanBased, String title) { - return getPage(lines, pageHumanBased, title, PAGEHEIGHT_PLAYER, null, null); + return getPage(lines, pageHumanBased, title, null, null, null); } public static List 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); + return getPage(lines, pageHumanBased, title, sender, null, null); } public static List getPage(List lines, int pageHumanBased, String title, MassiveCommand command) @@ -530,7 +530,12 @@ public class Txt public static List getPage(List lines, int pageHumanBased, String title, MassiveCommand command, List args) { - return getPage(lines, pageHumanBased, title, (command.sender instanceof Player) ? Txt.PAGEHEIGHT_PLAYER : Txt.PAGEHEIGHT_CONSOLE, command, args); + return getPage(lines, pageHumanBased, title, command.sender, command, args); + } + + public static List getPage(List lines, int pageHumanBased, String title, CommandSender sender, MassiveCommand command, List args) + { + return getPage(lines, pageHumanBased, title, (sender == null || sender instanceof Player) ? Txt.PAGEHEIGHT_PLAYER : Txt.PAGEHEIGHT_CONSOLE, command, args); } @SuppressWarnings("unchecked")