1
mirror of https://github.com/CarmJos/EasyPlugin.git synced 2024-09-19 19:25:45 +00:00

feat(gui): 为GUI提供更多方法 (#10)

* feat(gui): 为GUI提供更多方法

* feat(gui): 修复翻页按钮消失的bug
This commit is contained in:
Lyzen 2024-02-08 02:12:58 +08:00 committed by GitHub
parent 1b7f60fe43
commit 17b7c23c54
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 32 additions and 3 deletions

View File

@ -81,6 +81,19 @@ public class GUI {
} }
} }
/**
* 设置GUI上方(箱子部分)
* @param row 行数1为第1行
* @param column 列数1为第1列
* @param item GUIItem
*/
public void setItem(int row, int column, @NotNull GUIItem item){
if(row <= 0 || column <= 0) throw new IllegalArgumentException("行数和列数都不能小于等于零");
if(row > type.getLines()) throw new IllegalArgumentException("行数("+row+")大于GUI大小限制("+type.getLines()+")");
if(column > 9) throw new IllegalArgumentException("列数("+column+")不能大于9");
setItem(9*(row-1)+column-1, item);
}
public GUIItem getItem(int index) { public GUIItem getItem(int index) {
return this.items.get(index); return this.items.get(index);
} }

View File

@ -77,7 +77,14 @@ public class AutoPagedGUI extends CommonPagedGUI {
public void openGUI(Player user) { public void openGUI(Player user) {
if (previousPageSlot >= 0) { if (previousPageSlot >= 0) {
if (hasPreviousPage()) { if (hasPreviousPage()) {
setItem(previousPageSlot, new GUIItem(Optional.ofNullable(defaultPreviousPage).map(d -> d.apply(user)).orElse(previousPageUI)) { ItemStack finalPreviousPageUI;
if(previousPageUI != null)
finalPreviousPageUI = previousPageUI;
else if (defaultPreviousPage != null)
finalPreviousPageUI = defaultPreviousPage.apply(user);
else
finalPreviousPageUI = null;
setItem(previousPageSlot, new GUIItem(finalPreviousPageUI) {
@Override @Override
public void onClick(Player clicker, ClickType type) { public void onClick(Player clicker, ClickType type) {
if (type == ClickType.RIGHT) { if (type == ClickType.RIGHT) {
@ -95,7 +102,14 @@ public class AutoPagedGUI extends CommonPagedGUI {
if (nextPageSlot >= 0) { if (nextPageSlot >= 0) {
if (hasNextPage()) { if (hasNextPage()) {
setItem(nextPageSlot, new GUIItem(Optional.ofNullable(defaultNextPage).map(d -> d.apply(user)).orElse(nextPageUI)) { ItemStack finalNextPageUI;
if(previousPageUI != null)
finalNextPageUI = nextPageUI;
else if (defaultNextPage != null)
finalNextPageUI = defaultNextPage.apply(user);
else
finalNextPageUI = null;
setItem(nextPageSlot, new GUIItem(finalNextPageUI) {
@Override @Override
public void onClick(Player clicker, ClickType type) { public void onClick(Player clicker, ClickType type) {
if (type == ClickType.RIGHT) { if (type == ClickType.RIGHT) {

View File

@ -101,6 +101,8 @@ public class CommonPagedGUI extends PagedGUI {
super.openGUI(player); super.openGUI(player);
return; return;
} }
if(page > getLastPageNumber())
page = getLastPageNumber();
List<GUIItem> list = new ArrayList<>(); List<GUIItem> list = new ArrayList<>();
int start = (page - 1) * range.length; int start = (page - 1) * range.length;
for (int i = start; i < start + range.length; i++) { for (int i = start; i < start + range.length; i++) {

View File

@ -20,7 +20,7 @@ public abstract class PagedGUI extends GUI {
} }
public int setCurrentPage(int page) { public int setCurrentPage(int page) {
this.page = Math.max(1, Math.min(page, getLastPageNumber())); this.page = Math.max(1, page);
return this.page; return this.page;
} }