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

[v1.3.1] [A] 添加PagedGUI接口

This commit is contained in:
Carm Jos 2022-01-13 02:00:32 +08:00
parent d39951bf65
commit ff4da6b73e
16 changed files with 351 additions and 15 deletions

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.3.0</version>
<version>1.3.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.3.0</version>
<version>1.3.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.3.0</version>
<version>1.3.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.3.0</version>
<version>1.3.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.3.0</version>
<version>1.3.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.3.0</version>
<version>1.3.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.3.0</version>
<version>1.3.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -25,7 +25,6 @@ public class GUI {
public static void initialize(JavaPlugin plugin) {
GUI.plugin = plugin;
}
public static JavaPlugin getPlugin() {
@ -36,8 +35,8 @@ public class GUI {
return openedGUIs;
}
GUIType type;
String name;
protected GUIType type;
protected String name;
public HashMap<Integer, GUIItem> items;
public Inventory inv;

View File

@ -0,0 +1,96 @@
package cc.carm.lib.easyplugin.gui.paged;
import cc.carm.lib.easyplugin.gui.GUIItem;
import cc.carm.lib.easyplugin.gui.GUIType;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
import java.util.function.Function;
public class AutoPagedGUI extends CommonPagedGUI {
public static Function<Player, ItemStack> defaultPreviousPage = null;
public static Function<Player, ItemStack> defaultNextPage = null;
ItemStack previousPageUI;
ItemStack nextPageUI;
int previousPageSlot = -1;
int nextPageSlot = -1;
public AutoPagedGUI(GUIType type, String name, int[] range) {
super(type, name, range);
}
public AutoPagedGUI(GUIType type, String name, int a, int b) {
super(type, name, a, b);
}
public void setPreviousPageUI(ItemStack lastPageUI) {
this.previousPageUI = lastPageUI;
}
public void setNextPageUI(ItemStack nextPageUI) {
this.nextPageUI = nextPageUI;
}
public void setPreviousPageSlot(int slot) {
this.previousPageSlot = slot;
}
public void setNextPageSlot(int slot) {
this.nextPageSlot = slot;
}
@Override
public void openGUI(Player user) {
if (previousPageSlot >= 0) {
if (hasPreviousPage()) {
setItem(previousPageSlot, new GUIItem(
previousPageUI == null ? getDefaultPreviousPage(user) : previousPageUI) {
@Override
public void onClick(ClickType type) {
if (type == ClickType.RIGHT) {
goFirstPage();
} else {
goPreviousPage();
}
openGUI(user);
}
});
} else {
setItem(previousPageSlot, null);
}
}
if (nextPageSlot >= 0) {
if (hasNextPage()) {
setItem(nextPageSlot, new GUIItem(
nextPageUI == null ? getDefaultNextPage(user) : nextPageUI) {
@Override
public void onClick(ClickType type) {
if (type == ClickType.RIGHT) {
goLastPage();
} else {
goNextPage();
}
openGUI(user);
}
});
} else {
setItem(nextPageSlot, null);
}
}
super.openGUI(user);
}
private static ItemStack getDefaultNextPage(Player player) {
return defaultNextPage != null ? defaultNextPage.apply(player) : null;
}
private static ItemStack getDefaultPreviousPage(Player player) {
return defaultPreviousPage != null ? defaultPreviousPage.apply(player) : null;
}
}

View File

@ -0,0 +1,162 @@
package cc.carm.lib.easyplugin.gui.paged;
import cc.carm.lib.easyplugin.gui.GUIItem;
import cc.carm.lib.easyplugin.gui.GUIType;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class CommonPagedGUI extends PagedGUI {
private int[] range;
private CommonPagedGUI(GUIType type, String name) {
super(type, name);
}
public CommonPagedGUI(GUIType type, String Name, int a, int b) {
this(type, Name, toRange(type, a, b));
}
public CommonPagedGUI(GUIType type, String Name, int[] range) {
super(type, Name);
Arrays.sort(range);
this.range = range;
}
/*
int[] matrix = new int[]{
0, 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24, 25, 26,
27, 28, 29, 30, 31, 32, 33, 34, 35,
36, 37, 38, 39, 40, 41, 42, 43, 44,
45, 46, 47, 48, 49, 50, 51, 52, 53
}
*/
private static int[] toRange(GUIType type, int a, int b) {
if (a > b) {
a = a ^ b;
b = a ^ b;
a = a ^ b;
}
int lineA = getLine(a);
int columnA = getColumn(a);
int lineB = getLine(b);
int columnB = getColumn(b);
if (lineB > type.getLines())
throw new IndexOutOfBoundsException("页面内容范围超过了GUI的大小");
int[] range = new int[(lineB - lineA + 1) * (columnB - columnA + 1)];
for (int i = 0, l = 0; i < type.getSize(); i++) {
int li = getLine(i);
int ci = getColumn(i);
if (li >= lineA && li <= lineB && ci >= columnA && ci <= columnB) {
range[l] = i;
l++;
}
}
return range;
}
private static int getLine(int i) {
return i / 9 + 1;
}
private static int getColumn(int i) {
return i % 9 + 1;
}
@Override
public boolean hasPreviousPage() {
return page > 1;
}
@Override
public boolean hasNextPage() {
return page < getLastPageNumber();
}
/**
* 前往第一页
*/
public void goFirstPage() {
if (hasPreviousPage())
this.page = 1;
else
throw new IndexOutOfBoundsException();
}
/**
* 前往最后一页
*/
public void goLastPage() {
if (hasNextPage())
this.page = getLastPageNumber();
else
throw new IndexOutOfBoundsException();
}
/**
* 得到最后一页的页码
*
* @return 最后一页的页码
*/
public int getLastPageNumber() {
return (this.container.size() / range.length) + 1;
}
/**
* 得到第一页的页码
*
* @return 第一页页码(默认为1)
*/
public int getFirstPageNumber() {
return 1;
}
@Override
public void openGUI(Player player) {
if (container.isEmpty()) {
super.openGUI(player);
return;
}
List<GUIItem> list = new ArrayList<>();
int start = (page - 1) * range.length;
for (int i = start; i < start + range.length; i++) {
if (i < container.size()) {
list.add(container.get(i));
} else {
break;
}
}
int i = 0;
Arrays.stream(range).forEach(index -> setItem(index, null));
for (int index : range) {
if (i < list.size()) {
setItem(index, list.get(i));
i++;
} else {
break;
}
}
super.openGUI(player);
}
}

View File

@ -0,0 +1,79 @@
package cc.carm.lib.easyplugin.gui.paged;
import cc.carm.lib.easyplugin.gui.GUI;
import cc.carm.lib.easyplugin.gui.GUIItem;
import cc.carm.lib.easyplugin.gui.GUIType;
import java.util.ArrayList;
import java.util.List;
public abstract class PagedGUI extends GUI {
List<GUIItem> container = new ArrayList<>();
public int page = 1;
public PagedGUI(GUIType type, String name) {
super(type, name);
}
public int addItem(GUIItem i) {
container.add(i);
return container.size() - 1;
}
/**
* 从GUI中移除一个物品
*
* @param item 物品
*/
public void removeItem(GUIItem item) {
container.remove(item);
}
/**
* 从GUI中移除一个物品
*
* @param slot 物品格子数
*/
public void removeItem(int slot) {
container.remove(slot);
}
public List<GUIItem> getItemsContainer() {
return new ArrayList<>(container);
}
/**
* 前往上一页
*/
public void goPreviousPage() {
if (hasPreviousPage())
page--;
else
throw new IndexOutOfBoundsException();
}
/**
* 前往下一页
*/
public void goNextPage() {
if (hasNextPage())
page++;
else
throw new IndexOutOfBoundsException();
}
/**
* @return 是否有上一页
*/
public abstract boolean hasPreviousPage();
/**
* @return 是否有下一页
*/
public abstract boolean hasNextPage();
}

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.3.0</version>
<version>1.3.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.3.0</version>
<version>1.3.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.3.0</version>
<version>1.3.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.3.0</version>
<version>1.3.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -14,7 +14,7 @@
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-parent</artifactId>
<packaging>pom</packaging>
<version>1.3.0</version>
<version>1.3.1</version>
<modules>
<module>easyplugin-bom</module>