Skip to content

Commit

Permalink
Fixed plain-pagination exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Mqzn committed Aug 11, 2024
1 parent b93a040 commit 5153329
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/main/java/io/github/mqzen/menus/base/BaseMenuView.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,11 @@ public void replaceItemStack(Slot slot, @Nullable ItemStack newItem, boolean ove
public void replaceButton(@NotNull Slot slot, @Nullable Button newButton) {
if (!isOpen())
return;

getContent().setButton(slot, newButton);
if(newButton != null) {
this.currentOpenInventory.setItem(slot.getSlot(), newButton.getItem());
this.currentOpener.updateInventory();
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/github/mqzen/menus/base/MenuContentImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void fill(Button button) {

@Override
public void fillRow(int row, Button button) {
fillRow(row, capacity.getColumns() + 1, button);
fillRow(row, capacity.getColumns()-1, button);
}

@Override
Expand Down Expand Up @@ -123,7 +123,7 @@ public void fillColumn(final int column, final int endRow, Button button) {

@Override
public void fillColumn(final int column, Button button) {
fillColumn(column, capacity.getRows() + 1, button);
fillColumn(column, capacity.getRows()-1, button);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,17 @@ public void initialize(Page page, Player opener) {

final Content totalDefaultContent = playerDefaultContent.mergeWith(pageDefaultContent);
final int defaultContentExpectedCapacity = capacity.getTotalSize()-maxButtonsCount;
if(totalDefaultContent.size() > defaultContentExpectedCapacity) {
if(pagination.isAutomatic() && totalDefaultContent.size() > defaultContentExpectedCapacity) {
throw new IllegalStateException(
String.format("Exceeded expected default capacity of the page \n " +
"Expected= %s, Actual= %s", defaultContentExpectedCapacity, totalDefaultContent.size())
);
}
if(!pagination.isAutomatic() && totalDefaultContent.size() > capacity.getTotalSize()) {
throw new IllegalStateException(
String.format("Content of plain page #%s has exceeded the page's capacity(%s)", (index+1), capacity.getTotalSize())
);
}

currentOpenedData = new ViewData(
page.getTitle(this.dataRegistry, opener), capacity, totalDefaultContent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static Button transformerButton(ItemStack item, BiFunction<MenuView<?>, I
public static Button transformerItem(ItemStack item,
BiFunction<MenuView<?>, InventoryClickEvent, ItemStack> transformer) {
return new Button(item, (view, click) ->
view.replaceClickedItemStack(click, transformer.apply(view, click)));
view.replaceClickedItemStack(click, transformer.apply(view, click), false));
}

public boolean isClickable() {
Expand Down
17 changes: 5 additions & 12 deletions src/test/java/io/github/mqzen/menus/ExampleAutoPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ public ItemStack nextPageItem(Player player) {
@Override
public ItemStack previousPageItem(Player player) {
return ItemBuilder.legacy(Material.PAPER)
.setDisplay("Previous page")
.setDisplay("&ePrevious page")
.build();
}

@Override
public String getName() {
return "Islands";
return "Example auto-pagination";
}

@Override
public @NotNull MenuTitle getTitle(DataRegistry dataRegistry, Player player) {
int index = dataRegistry.getData("index");
return MenuTitles.createModern("<gold>Islands Page #" + (index+1));
return MenuTitles.createModern("<gold>Example Page #" + (index+1));
}

@Override
Expand All @@ -62,23 +62,16 @@ public String getName() {
.setDisplay("Hi")
.build(),
(menuView, inventoryClickEvent) -> inventoryClickEvent.setCancelled(true));

TextLayoutPane pane = new TextLayoutPane(capacity,
TextLayout.builder().setDefault(button).build(),
TextLayout.builder().set('#', button).build(),
"#########",
"# #",
"# #",
"#########"
);

builder = builder.applyPane(pane);

/**
* old code here
builder.draw(0, 8, Direction.RIGHT,button);
builder.draw(9, 27, Direction.DOWNWARDS, button);
builder.draw(28, 35, Direction.RIGHT, button);
builder.draw(35, 8, Direction.UPWARDS, button);
**/
return builder.build();
}
}
87 changes: 87 additions & 0 deletions src/test/java/io/github/mqzen/menus/ExampleMenu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package io.github.mqzen.menus;

import io.github.mqzen.menus.base.Content;
import io.github.mqzen.menus.base.Menu;
import io.github.mqzen.menus.misc.Capacity;
import io.github.mqzen.menus.misc.DataRegistry;
import io.github.mqzen.menus.misc.button.Button;
import io.github.mqzen.menus.misc.itembuilder.ItemBuilder;
import io.github.mqzen.menus.titles.MenuTitle;
import io.github.mqzen.menus.titles.MenuTitles;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;

public final class ExampleMenu implements Menu {

private final ItemStack[] fruits = {
ItemBuilder.legacy(Material.APPLE).build(),
ItemBuilder.legacy(Material.CARROT_ITEM).build(),
ItemBuilder.legacy(Material.GOLDEN_APPLE).build(),
ItemBuilder.legacy(Material.GOLDEN_CARROT).build()
};
private int index = 0;
/**
* @return The unique name for this menu
*/
@Override
public String getName() {
return "example menu";
}

/**
* @param extraData the data container for this menu for extra data
* @param opener the player who is opening this menu
* @return the title for this menu
*/
@Override
public @NotNull MenuTitle getTitle(DataRegistry extraData, Player opener) {
return MenuTitles.createLegacy("&cExample Menu");
}

/**
* @param extraData the data container for this menu for extra data
* @param opener the player who is opening this menu
* @return the capacity/size for this menu
*/
@Override
public @NotNull Capacity getCapacity(DataRegistry extraData, Player opener) {
return Capacity.ofRows(3);//Alternative approach: Capacity.ofRows(3)
}

/**
* Creates the content for the menu
*
* @param extraData the data container for this menu for extra data
* @param opener the player opening this menu
* @param capacity the capacity set by the user above
* @return the content of the menu to add (this includes items)
*/
@Override
public @NotNull Content getContent(DataRegistry extraData,
Player opener, Capacity capacity) {
Button borderPane = Button.clickable(
ItemBuilder.legacy(Material.STAINED_GLASS_PANE, 1, (short)5).build(),
(menuView, event)-> {
event.setCancelled(true);
//we want nothing to happen here
}
);
Button transformingFruit = Button.transformerItem(fruits[0], (menuView, event)-> {
event.setCancelled(true);
index++;
if(index >= fruits.length) {
index = 0;
}
return fruits[index];
});

return Content.builder(capacity)
.apply(content -> {
content.fill(transformingFruit);
content.fillBorder(borderPane);
})
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public ItemStack toItem() {
public void onClick(PageView pageView, InventoryClickEvent event) {
event.setCancelled(true);
Player player = (Player) event.getWhoClicked();
player.sendMessage("Visiting island " + name);
player.sendMessage("Clicking on component '" + name + "'");
}
}
11 changes: 5 additions & 6 deletions src/test/java/io/github/mqzen/menus/ExamplePlainPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
public final class ExamplePlainPage extends Page {

private final int amountOfItems;


public ExamplePlainPage(int amountOfItems) {
this.amountOfItems = amountOfItems;
}
Expand All @@ -39,23 +37,24 @@ public ItemStack nextPageItem(Player player) {
@Override
public ItemStack previousPageItem(Player player) {
return ItemBuilder.legacy(Material.PAPER)
.setDisplay("&aPrevious page")
.setDisplay("&ePrevious page")
.build();
}

@Override
public String getName() {
return "Islands";
return "Example plain-pagination";
}

@Override
public @NotNull MenuTitle getTitle(DataRegistry dataRegistry, Player player) {
return MenuTitles.createLegacy("Islands");
int index = dataRegistry.getData("index");
return MenuTitles.createLegacy("&6Example Plain page #" + (index+1));
}

@Override
public @NotNull Capacity getCapacity(DataRegistry dataRegistry, Player player) {
return Capacity.ofRows(6);
return Capacity.ofRows(3);
}


Expand Down
6 changes: 6 additions & 0 deletions src/test/java/io/github/mqzen/menus/LotusExamplePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@ public void onEnable() {
public boolean onCommand(CommandSender sender, Command command,
String label, String[] args) {

if(args.length == 0) {
lotus.openMenu((Player) sender, new ExampleMenu());
return true;
}
if(args.length != 1) {
return false;
}


String sub = args[0];
Pagination pagination;
if(sub.equalsIgnoreCase("auto")) {
Expand Down

0 comments on commit 5153329

Please sign in to comment.