Skip to content

Commit

Permalink
Created a 'configuration' for messages/button labels
Browse files Browse the repository at this point in the history
Fixed #1
  • Loading branch information
Sam Jakob Mearns committed Dec 18, 2017
1 parent d9dbd29 commit 7416d79
Showing 1 changed file with 50 additions and 9 deletions.
59 changes: 50 additions & 9 deletions com/cloutteam/samjakob/gui/types/PaginatedGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,20 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

public class PaginatedGUI implements InventoryHolder {

/* BEGIN: CONFIGURATION */
private static final String CHAT_PREFIX = "&c&lGUI &c";
private static final String NO_PREVIOUS_PAGES = "There are no previous pages.";
private static final String NO_ADDITIONAL_PAGES = "There are no additional pages.";

private static final String PREVIOUS_PAGE = "&c&lPrevious Page";
private static final String CURRENT_PAGE = "&c&lPage {currentPage} of {maxPages}";
private static final String NEXT_PAGE = "&c&lNext Page";
/* END: CONFIGURATION */

private Map<Integer, GUIButton> items;
private Map<Integer, GUIButton> toolbarItems;
private int currentPage;
Expand All @@ -40,22 +51,42 @@ public void setButton(int slot, GUIButton button){
public Inventory getInventory() {
Inventory inventory = Bukkit.createInventory(this, (getMaxPage() > 1) ? 54 : 45, name);
// Include pagination
GUIButton backButton = new GUIButton(ItemBuilder.start(Material.ARROW).name("&c&lPrevious Page").build());
GUIButton pageIndicator = new GUIButton(ItemBuilder.start(Material.NAME_TAG).name("&c&lPage " + (currentPage + 1) + " of " + (getMaxPage() + 1)).build());
GUIButton nextButton = new GUIButton(ItemBuilder.start(Material.ARROW).name("&c&lNext Page").build());
GUIButton backButton = new GUIButton(ItemBuilder.start(Material.ARROW).name(PREVIOUS_PAGE).build());
GUIButton pageIndicator = new GUIButton(ItemBuilder.start(Material.NAME_TAG)
.name(
CURRENT_PAGE
.replaceAll(Pattern.quote("{currentPage}"), String.valueOf(currentPage + 1))
.replaceAll(Pattern.quote("{maxPages}"), String.valueOf(getMaxPage() + 1))
)
.build());
GUIButton nextButton = new GUIButton(ItemBuilder.start(Material.ARROW).name(NEXT_PAGE).build());

backButton.setListener(event -> {
event.setCancelled(true);
PaginatedGUI menu = (PaginatedGUI) event.getClickedInventory().getHolder();
menu.previousPage();

if(!menu.previousPage()){
event.getWhoClicked().sendMessage(ChatColor.translateAlternateColorCodes('&',
CHAT_PREFIX + NO_PREVIOUS_PAGES));
return;
}

event.getWhoClicked().closeInventory();
event.getWhoClicked().openInventory(getInventory());
});

pageIndicator.setListener(event -> event.setCancelled(true));

nextButton.setListener(event -> {
event.setCancelled(true);
PaginatedGUI menu = (PaginatedGUI) event.getClickedInventory().getHolder();
menu.nextPage();

if(!menu.nextPage()){
event.getWhoClicked().sendMessage(ChatColor.translateAlternateColorCodes('&',
CHAT_PREFIX + NO_ADDITIONAL_PAGES));
return;
}

event.getWhoClicked().closeInventory();
event.getWhoClicked().openInventory(getInventory());
});
Expand Down Expand Up @@ -95,12 +126,22 @@ public GUIButton getButton(int slot){
}
}

public void nextPage(){
currentPage++;
public boolean nextPage(){
if(currentPage < getMaxPage()){
currentPage++;
return true;
}else{
return false;
}
}

public void previousPage(){
currentPage--;
public boolean previousPage(){
if(currentPage > 0) {
currentPage--;
return true;
}else{
return false;
}
}

public int getMaxPage(){
Expand Down

0 comments on commit 7416d79

Please sign in to comment.