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

feat(gui): 重构部分GUI代码,新增填充空格功能

This commit is contained in:
Carm Jos 2023-11-06 04:57:22 +08:00
parent d26987c0d2
commit de4dbbe637
23 changed files with 210 additions and 137 deletions

View File

@ -6,7 +6,7 @@
<parent>
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-parent</artifactId>
<version>1.5.9</version>
<version>1.5.10</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<properties>

View File

@ -6,7 +6,7 @@
<parent>
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-parent</artifactId>
<version>1.5.9</version>
<version>1.5.10</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<properties>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.9</version>
<version>1.5.10</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.9</version>
<version>1.5.10</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -19,7 +19,7 @@ import java.util.stream.IntStream;
public class GUI {
private static JavaPlugin plugin;
private static final HashMap<UUID, GUI> openedGUIs = new HashMap<>();
private static final Map<UUID, GUI> openedGUIs = new HashMap<>();
public static void initialize(JavaPlugin plugin) {
GUI.plugin = plugin;
@ -29,42 +29,32 @@ public class GUI {
return plugin;
}
public static HashMap<UUID, GUI> getOpenedGUIs() {
public static Map<UUID, GUI> getOpenedGUIs() {
return openedGUIs;
}
protected GUIType type;
protected String title;
public HashMap<Integer, GUIItem> items;
public Inventory inv;
protected final @NotNull GUIType type;
protected @NotNull String title;
protected Inventory inv;
/**
* 当玩家点击目标GUI时是否取消
*/
boolean cancelOnTarget = true;
protected final SortedMap<Integer, GUIItem> items = new TreeMap<>();
protected ItemStack emptyItem = null;
/**
* 当玩家点击自己背包时是否取消
*/
boolean cancelOnSelf = true;
/**
* 当玩家点击界面外时是否取消
*/
boolean cancelOnOuter = true;
protected boolean cancelOnTarget = true; // 当玩家点击GUI时是否取消对应事件
protected boolean cancelOnSelf = true; // 当玩家点击自己背包时是否取消对应事件
protected boolean cancelOnOuter = true; // 当玩家点击界面外时是否取消对应事件
protected final Map<String, Object> flags = new LinkedHashMap<>();
protected GUIListener listener;
public GUI(GUIType type, String title) {
public GUI(@NotNull GUIType type, @NotNull String title) {
this.type = type;
this.title = ColorParser.parse(title);
this.items = new HashMap<>();
}
public Map<@NotNull Integer, @NotNull GUIItem> getItems() {
return new HashMap<>(items);
public SortedMap<@NotNull Integer, @NotNull GUIItem> getItems() {
return items;
}
public final void setItem(int index, @Nullable GUIItem item) {
@ -75,16 +65,51 @@ public class GUI {
}
}
public void setItemStack(int index, @Nullable ItemStack item) {
setItem(index, item == null ? null : new GUIItem(item));
}
public void setItem(GUIItem item, int... index) {
for (int i : index) {
setItem(i, item);
}
}
public void setItemStack(ItemStack item, int... index) {
for (int i : index) {
setItemStack(i, item);
}
}
public GUIItem getItem(int index) {
return this.items.get(index);
}
public void setEmptyItem(ItemStack item) {
this.emptyItem = item;
}
protected void fillEmptySlots(@NotNull Inventory inventory) {
if (emptyItem == null) return;
IntStream.range(0, inventory.getSize())
.filter(i -> inventory.getItem(i) == null)
.forEach(index -> inventory.setItem(index, emptyItem));
}
protected void applyToInventory(Inventory inventory) {
IntStream.range(0, inventory.getSize()).forEach(index -> inventory.setItem(index, new ItemStack(Material.AIR)));
getItems().forEach((index, item) -> inventory.setItem(index, item.getDisplay()));
fillEmptySlots(inventory);
}
public void updateTitle(@NotNull String title) {
this.title = ColorParser.parse(title);
if (this.inv != null) {
this.inv = Bukkit.createInventory(null, this.type.getSize(), this.title);
applyToInventory(this.inv);
}
}
/**
* 更新玩家箱子的视图
*/
@ -92,8 +117,7 @@ public class GUI {
this.onUpdate();
if (this.inv != null) {
List<HumanEntity> viewers = this.inv.getViewers();
IntStream.range(0, this.inv.getSize()).forEach(index -> inv.setItem(index, new ItemStack(Material.AIR)));
getItems().forEach((index, item) -> inv.setItem(index, item.getDisplay()));
applyToInventory(this.inv);
viewers.forEach(p -> ((Player) p).updateInventory());
}
}
@ -148,22 +172,22 @@ public class GUI {
throw new IllegalStateException("被取消或不存在的GUI");
}
Inventory inv = Bukkit.createInventory(null, this.type.getSize(), this.title);
IntStream.range(0, inv.getSize()).forEach(index -> inv.setItem(index, new ItemStack(Material.AIR)));
getItems().forEach((index, item) -> inv.setItem(index, item.getDisplay()));
Inventory ui = Bukkit.createInventory(null, this.type.getSize(), this.title);
applyToInventory(ui);
GUI previous = getOpenedGUI(player);
if (previous != null) {
if (previous != null && previous.listener != null) {
previous.listener.close(player);
}
setOpenedGUI(player, this);
this.inv = inv;
player.openInventory(inv);
this.inv = ui;
player.openInventory(ui);
if (listener == null) {
Bukkit.getPluginManager().registerEvents(listener = new GUIListener(this), getPlugin());
this.listener = new GUIListener(this);
Bukkit.getPluginManager().registerEvents(this.listener, getPlugin());
}
}
@ -211,4 +235,11 @@ public class GUI {
getOpenedGUIs().remove(player.getUniqueId());
}
public static void closeAll() {
Set<HumanEntity> viewers = new HashSet<>();
getOpenedGUIs().values().stream().flatMap(gui -> gui.inv.getViewers().stream()).forEach(viewers::add);
viewers.forEach(HumanEntity::closeInventory);
getOpenedGUIs().clear();
}
}

View File

@ -10,11 +10,11 @@ import java.util.Set;
public class GUIItem {
ItemStack display;
boolean actionActive = true;
protected ItemStack display;
protected boolean actionActive = true;
public Set<GUIClickAction> actions = new HashSet<>();
public Set<GUIClickAction> actionsIgnoreActive = new HashSet<>();
protected final Set<GUIClickAction> actions = new HashSet<>();
protected final Set<GUIClickAction> actionsIgnoreActive = new HashSet<>();
public GUIItem(ItemStack display) {
this.display = display;

View File

@ -11,7 +11,7 @@ import org.bukkit.event.player.PlayerQuitEvent;
public class GUIListener implements Listener {
GUI currentGUI;
protected final GUI currentGUI;
public GUIListener(GUI gui) {
this.currentGUI = gui;

View File

@ -12,6 +12,7 @@ public enum GUIType {
FOUR_BY_NINE(4, 36),
FIVE_BY_NINE(5, 45),
SIX_BY_NINE(6, 54),
CANCEL(0, 0);
private final int lines;

View File

@ -4,36 +4,54 @@ 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.Inventory;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
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;
protected ItemStack previousPageUI;
protected ItemStack nextPageUI;
protected ItemStack noPreviousPageUI;
protected ItemStack noNextPageUI;
protected int previousPageSlot = -1;
protected int nextPageSlot = -1;
public AutoPagedGUI(GUIType type, String name, int[] range) {
super(type, name, range);
public AutoPagedGUI(@NotNull GUIType type, @NotNull String title, int[] range) {
super(type, title, range);
}
public AutoPagedGUI(GUIType type, String name, int a, int b) {
super(type, name, a, b);
public AutoPagedGUI(@NotNull GUIType type, @NotNull String title, int a, int b) {
super(type, title, a, b);
}
public void setPreviousPageUI(ItemStack lastPageUI) {
public void setPreviousPageUI(@Nullable ItemStack lastPageUI) {
this.previousPageUI = lastPageUI;
}
public void setNextPageUI(ItemStack nextPageUI) {
public void setNoPreviousPageUI(@Nullable ItemStack noPreviousPageUI) {
this.noPreviousPageUI = noPreviousPageUI;
}
public void setNextPageUI(@Nullable ItemStack nextPageUI) {
this.nextPageUI = nextPageUI;
}
public void setNoNextPageUI(@Nullable ItemStack noNextPageUI) {
this.noNextPageUI = noNextPageUI;
}
public void setPreviousPageSlot(int slot) {
this.previousPageSlot = slot;
}
@ -42,12 +60,24 @@ public class AutoPagedGUI extends CommonPagedGUI {
this.nextPageSlot = slot;
}
@Override
protected void fillEmptySlots(@NotNull Inventory inventory) {
if (emptyItem == null) return;
Set<Integer> rangeSet = Arrays.stream(this.range).boxed().collect(Collectors.toSet());
if (previousPageSlot >= 0) rangeSet.add(previousPageSlot);
if (nextPageSlot >= 0) rangeSet.add(nextPageSlot);
IntStream.range(0, inventory.getSize())
.filter(i -> inventory.getItem(i) == null)
.filter(i -> !rangeSet.contains(i))
.forEach(index -> inventory.setItem(index, emptyItem));
}
@Override
public void openGUI(Player user) {
if (previousPageSlot >= 0) {
if (hasPreviousPage()) {
setItem(previousPageSlot, new GUIItem(
previousPageUI == null ? getDefaultPreviousPage(user) : previousPageUI) {
setItem(previousPageSlot, new GUIItem(Optional.ofNullable(defaultPreviousPage).map(d -> d.apply(user)).orElse(previousPageUI)) {
@Override
public void onClick(Player clicker, ClickType type) {
if (type == ClickType.RIGHT) {
@ -59,14 +89,13 @@ public class AutoPagedGUI extends CommonPagedGUI {
}
});
} else {
setItem(previousPageSlot, null);
setItem(previousPageSlot, this.noPreviousPageUI == null ? null : new GUIItem(noPreviousPageUI));
}
}
if (nextPageSlot >= 0) {
if (hasNextPage()) {
setItem(nextPageSlot, new GUIItem(
nextPageUI == null ? getDefaultNextPage(user) : nextPageUI) {
setItem(nextPageSlot, new GUIItem(Optional.ofNullable(defaultNextPage).map(d -> d.apply(user)).orElse(nextPageUI)) {
@Override
public void onClick(Player clicker, ClickType type) {
if (type == ClickType.RIGHT) {
@ -78,19 +107,12 @@ public class AutoPagedGUI extends CommonPagedGUI {
}
});
} else {
setItem(nextPageSlot, null);
setItem(nextPageSlot, this.noNextPageUI == null ? null : new GUIItem(noNextPageUI));
}
}
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

@ -4,70 +4,38 @@ 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.inventory.Inventory;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class CommonPagedGUI extends PagedGUI {
private int[] range;
protected final int[] range;
private CommonPagedGUI(GUIType type, String name) {
super(type, name);
public CommonPagedGUI(@NotNull GUIType type, @NotNull String title, int a, int b) {
this(type, title, toRange(type, a, b));
}
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);
public CommonPagedGUI(@NotNull GUIType type, @NotNull String title, int[] range) {
super(type, title);
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;
@Override
protected void fillEmptySlots(@NotNull Inventory inventory) {
if (emptyItem == null) return;
Set<Integer> rangeSet = Arrays.stream(this.range).boxed().collect(Collectors.toSet());
IntStream.range(0, inventory.getSize())
.filter(i -> inventory.getItem(i) == null)
.filter(i -> !rangeSet.contains(i))
.forEach(index -> inventory.setItem(index, emptyItem));
}
private static int getLine(int i) {
@ -155,4 +123,44 @@ public class CommonPagedGUI extends PagedGUI {
super.openGUI(player);
}
/*
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)];
int l = 0;
for (int i = 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;
}
}

View File

@ -4,44 +4,55 @@ 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 org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
public abstract class PagedGUI extends GUI {
List<GUIItem> container = new ArrayList<>();
public int page = 1;
protected List<GUIItem> container = new ArrayList<>();
protected int page = 1;
public PagedGUI(GUIType type, String name) {
super(type, name);
protected PagedGUI(@NotNull GUIType type, @NotNull String title) {
super(type, title);
}
public int addItem(GUIItem i) {
public int addItem(@NotNull GUIItem i) {
container.add(i);
return container.size() - 1;
}
public int addItem(int index, @NotNull GUIItem i) {
container.add(index, i);
return container.size() - 1;
}
public int addItemStack(@NotNull ItemStack itemStack) {
return addItem(new GUIItem(itemStack));
}
/**
* 从GUI中移除一个物品
*
* @param item 物品
*/
public void removeItem(GUIItem item) {
public void removeItem(@NotNull GUIItem item) {
container.remove(item);
}
/**
* 从GUI中移除一个物品
*
* @param slot 物品格子数
* @param index 物品格子数
*/
public void removeItem(int slot) {
container.remove(slot);
public void removeItem(int index) {
container.remove(index);
}
public List<GUIItem> getItemsContainer() {
return new ArrayList<>(container);
public @NotNull List<GUIItem> getPagedContainer() {
return this.container;
}
/**

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.9</version>
<version>1.5.10</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -6,7 +6,7 @@
<parent>
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-parent</artifactId>
<version>1.5.9</version>
<version>1.5.10</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<properties>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.9</version>
<version>1.5.10</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -6,7 +6,7 @@
<parent>
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-parent</artifactId>
<version>1.5.9</version>
<version>1.5.10</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<properties>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.9</version>
<version>1.5.10</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.9</version>
<version>1.5.10</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.9</version>
<version>1.5.10</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.9</version>
<version>1.5.10</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.9</version>
<version>1.5.10</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.9</version>
<version>1.5.10</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.9</version>
<version>1.5.10</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -15,7 +15,7 @@
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-parent</artifactId>
<packaging>pom</packaging>
<version>1.5.9</version>
<version>1.5.10</version>
<modules>
<module>base/color</module>
<module>base/utils</module>