1
mirror of https://github.com/CarmJos/EasyPlugin.git synced 2026-06-04 16:48:16 +08:00

build(all): 项目结构优化

优化整体项目结构,优化Javadoc生成方式与部署方式。
This commit is contained in:
2022-05-22 23:01:52 +08:00
parent 94c9cc382d
commit 226121c1df
54 changed files with 211 additions and 2114 deletions
+86
View File
@@ -0,0 +1,86 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.0</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
<maven.compiler.target>${project.jdk.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties>
<artifactId>easyplugin-gui</artifactId>
<packaging>jar</packaging>
<name>EasyPlugin-GUI</name>
<description>轻松插件GUI接口模块,方便快捷的创建箱子GUI界面。</description>
<url>https://github.com/CarmJos/EasyPlugin</url>
<developers>
<developer>
<id>CarmJos</id>
<name>Carm Jos</name>
<email>carm@carm.cc</email>
<url>https://www.carm.cc</url>
</developer>
</developers>
<licenses>
<license>
<name>The MIT License</name>
<url>https://opensource.org/licenses/MIT</url>
</license>
</licenses>
<issueManagement>
<system>GitHub Issues</system>
<url>https://github.com/CarmJos/EasyPlugin/issues</url>
</issueManagement>
<ciManagement>
<system>GitHub Actions</system>
<url>https://github.com/CarmJos/EasyPlugin/actions/workflows/maven.yml</url>
</ciManagement>
<dependencies>
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>easyplugin-main</artifactId>
<version>${project.parent.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,211 @@
package cc.carm.lib.easyplugin.gui;
import cc.carm.lib.easyplugin.utils.ColorParser;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.IntStream;
public class GUI {
private static JavaPlugin plugin;
private static final HashMap<UUID, GUI> openedGUIs = new HashMap<>();
public static void initialize(JavaPlugin plugin) {
GUI.plugin = plugin;
}
public static JavaPlugin getPlugin() {
return plugin;
}
public static HashMap<UUID, GUI> getOpenedGUIs() {
return openedGUIs;
}
protected GUIType type;
protected String name;
public HashMap<Integer, GUIItem> items;
public Inventory inv;
/**
* 当玩家点击目标GUI时是否取消
*/
boolean cancelOnTarget = true;
/**
* 当玩家点击自己背包时是否取消
*/
boolean cancelOnSelf = true;
/**
* 当玩家点击界面外时是否取消
*/
boolean cancelOnOuter = true;
Map<String, Object> flags;
GUIListener listener;
public GUI(GUIType type, String name) {
this.type = type;
this.name = ColorParser.parse(name);
this.items = new HashMap<>();
}
public HashMap<@NotNull Integer, @NotNull GUIItem> getItems() {
return new HashMap<>(items);
}
public final void setItem(int index, @Nullable GUIItem item) {
if (item == null) {
this.items.remove(index);
} else {
this.items.put(index, item);
}
}
public void setItem(GUIItem item, int... index) {
for (int i : index) {
setItem(i, item);
}
}
public GUIItem getItem(int index) {
return this.items.get(index);
}
/**
* 更新玩家箱子的视图
*/
public void updateView() {
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()));
viewers.forEach(p -> ((Player) p).updateInventory());
}
}
/**
* 设置是否取消点击GUI内物品的事件
* 如果不取消,玩家可以从GUI中拿取物品。
*
* @param b 是否取消
*/
public void setCancelOnTarget(boolean b) {
this.cancelOnTarget = b;
}
/**
* 设置是否取消点击自己背包内物品的事件
* 如果不取消,玩家可以从自己的背包中拿取物品。
*
* @param b 是否取消
*/
public void setCancelOnSelf(boolean b) {
this.cancelOnSelf = b;
}
/**
* 设置是否取消点击GUI外的事件
* 如果不取消,玩家可以把物品从GUI或背包中丢出去
*
* @param b 是否取消
*/
public void setCancelOnOuter(boolean b) {
this.cancelOnOuter = b;
}
public void addFlag(String flag, Object obj) {
if (this.flags == null) this.flags = new HashMap<>();
this.flags.put(flag, obj);
}
public Object getFlag(String flag) {
if (this.flags == null) return null;
else
return this.flags.get(flag);
}
public void setFlag(String flag, Object obj) {
if (this.flags == null) this.flags = new HashMap<>();
this.flags.replace(flag, obj);
}
public void removeFlag(String flag) {
if (this.flags == null) this.flags = new HashMap<>();
this.flags.remove(flag);
}
public void rawClickListener(InventoryClickEvent event) {
}
public void openGUI(Player player) {
if (this.type == GUIType.CANCEL) { throw new IllegalStateException("被取消或不存在的GUI"); }
Inventory inv = Bukkit.createInventory(null, this.type.getSize(), this.name);
IntStream.range(0, inv.getSize()).forEach(index -> inv.setItem(index, new ItemStack(Material.AIR)));
getItems().forEach((index, item) -> inv.setItem(index, item.getDisplay()));
GUI previous = getOpenedGUI(player);
if(previous != null){
previous.listener.close(player);
}
setOpenedGUI(player, this);
this.inv = inv;
player.openInventory(inv);
if (listener == null) {
Bukkit.getPluginManager().registerEvents(listener = new GUIListener(this), getPlugin());
}
}
/**
* 拖动GUI内物品是执行的代码
*
* @param event InventoryDragEvent
*/
public void onDrag(InventoryDragEvent event) {
}
/**
* 关闭GUI时执行的代码
*/
public void onClose() {
}
public static void setOpenedGUI(Player player, GUI gui) {
getOpenedGUIs().put(player.getUniqueId(), gui);
}
public static boolean hasOpenedGUI(Player player) {
return getOpenedGUIs().containsKey(player.getUniqueId());
}
public static GUI getOpenedGUI(Player player) {
return getOpenedGUIs().get(player.getUniqueId());
}
public static void removeOpenedGUI(Player player) {
getOpenedGUIs().remove(player.getUniqueId());
}
}
@@ -0,0 +1,73 @@
package cc.carm.lib.easyplugin.gui;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import java.util.HashSet;
import java.util.Set;
public class GUIItem {
ItemStack display;
boolean actionActive = true;
public Set<GUIClickAction> actions = new HashSet<>();
public Set<GUIClickAction> actionsIgnoreActive = new HashSet<>();
public GUIItem(ItemStack display) {
this.display = display;
}
public final ItemStack getDisplay() {
return this.display;
}
public final void setDisplay(ItemStack display) {
this.display = display;
}
public final boolean isActionActive() {
return this.actionActive;
}
public final void setActionActive(boolean b) {
actionActive = b;
}
/**
* 玩家点击GUI后执行的代码
*
* @param type 点击的类型
*/
public void onClick(ClickType type) {
}
public void addClickAction(GUIClickAction action) {
actions.add(action);
}
public void addActionIgnoreActive(GUIClickAction action) {
actionsIgnoreActive.add(action);
}
public void rawClickAction(InventoryClickEvent event) {
}
/**
* 玩家点击GUI后执行的代码
*
* @param player 点击GUI的玩家
*/
public void customAction(Player player) {
}
public abstract static class GUIClickAction {
public abstract void run(ClickType type, Player player);
}
}
@@ -0,0 +1,92 @@
package cc.carm.lib.easyplugin.gui;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.player.PlayerQuitEvent;
public class GUIListener implements Listener {
GUI currentGUI;
public GUIListener(GUI gui) {
this.currentGUI = gui;
}
public GUI getCurrentGUI() {
return currentGUI;
}
@EventHandler
public void onInventoryClickEvent(InventoryClickEvent event) {
if (!(event.getWhoClicked() instanceof Player)) return;
Player player = (Player) event.getWhoClicked();
if (!GUI.hasOpenedGUI(player)) return;
if (GUI.getOpenedGUI(player) != getCurrentGUI()) return;
getCurrentGUI().rawClickListener(event);
if (event.getSlot() == -999 && getCurrentGUI().cancelOnOuter) {
event.setCancelled(true);
return;
}
if (event.getClickedInventory() == null) return;
if (event.getClickedInventory().equals(getCurrentGUI().inv)) {
if (getCurrentGUI().cancelOnTarget) event.setCancelled(true);
if (event.getSlot() != -999) {
GUIItem clickedItem = getCurrentGUI().getItem(event.getSlot());
if (clickedItem != null) {
if (clickedItem.isActionActive()) {
clickedItem.onClick(event.getClick());
clickedItem.rawClickAction(event);
clickedItem.actions.forEach(action -> action.run(event.getClick(), player));
}
clickedItem.actionsIgnoreActive.forEach(action -> action.run(event.getClick(), player));
}
}
} else if (event.getClickedInventory().equals(player.getInventory()) && getCurrentGUI().cancelOnSelf) {
event.setCancelled(true);
}
}
@EventHandler
public void onDrag(InventoryDragEvent e) {
if (!(e.getWhoClicked() instanceof Player)) return;
if (e.getInventory().equals(getCurrentGUI().inv)
|| e.getInventory().equals(e.getWhoClicked().getInventory())) {
getCurrentGUI().onDrag(e);
}
}
@EventHandler
public void onInventoryCloseEvent(InventoryCloseEvent event) {
if (!(event.getPlayer() instanceof Player)) return;
if (!event.getInventory().equals(getCurrentGUI().inv)) return;
close((Player) event.getPlayer());
}
protected void close(Player p){
HandlerList.unregisterAll(this);
getCurrentGUI().listener = null;
GUI.removeOpenedGUI(p);
getCurrentGUI().onClose();
}
@EventHandler
public void onPlayerLeave(PlayerQuitEvent event) {
GUI.removeOpenedGUI(event.getPlayer());
}
}
@@ -0,0 +1,49 @@
package cc.carm.lib.easyplugin.gui;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
public enum GUIType {
ONE_BY_NINE(1, 9),
TWO_BY_NINE(2, 18),
THREE_BY_NINE(3, 27),
FOUR_BY_NINE(4, 36),
FIVE_BY_NINE(5, 45),
SIX_BY_NINE(6, 54),
CANCEL(0, 0);
int lines;
int size;
GUIType(int lines, int size) {
this.lines = lines;
this.size = size;
}
public int getLines() {
return lines;
}
public int getSize() {
return size;
}
@NotNull
public static GUIType getBySize(int size) {
return Arrays.stream(values()).filter(type -> type.getSize() == size).findFirst().orElse(CANCEL);
}
@NotNull
public static GUIType getByLines(int lines) {
return Arrays.stream(values()).filter(type -> type.getLines() == lines).findFirst().orElse(CANCEL);
}
@NotNull
public static GUIType getByName(String name) {
return Arrays.stream(values()).filter(type -> type.name().equalsIgnoreCase(name)).findFirst().orElse(CANCEL);
}
}
@@ -0,0 +1,53 @@
package cc.carm.lib.easyplugin.gui.configuration;
import cc.carm.lib.easyplugin.gui.GUIItem;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class GUIActionConfiguration {
@Nullable ClickType clickType;
final @NotNull GUIActionType actionType;
final @Nullable String actionContent;
public GUIActionConfiguration(@Nullable ClickType clickType,
@NotNull GUIActionType actionType,
@Nullable String actionContent) {
this.clickType = clickType;
this.actionType = actionType;
this.actionContent = actionContent;
}
public @Nullable ClickType getClickType() {
return clickType;
}
public @NotNull GUIActionType getActionType() {
return actionType;
}
public @Nullable String getActionContent() {
return actionContent;
}
public void checkAction(Player player, ClickType type) {
if (getClickType() == null || getClickType() == type) executeAction(player);
}
public void executeAction(Player targetPlayer) {
getActionType().getExecutor().accept(targetPlayer, getActionContent());
}
public GUIItem.GUIClickAction toClickAction() {
return new GUIItem.GUIClickAction() {
@Override
public void run(ClickType type, Player player) {
checkAction(player, type);
}
};
}
}
@@ -0,0 +1,88 @@
package cc.carm.lib.easyplugin.gui.configuration;
import cc.carm.lib.easyplugin.utils.MessageUtils;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.Collections;
import java.util.function.BiConsumer;
public enum GUIActionType {
/**
* 以玩家聊天的形式执行
* 若内容以 “/" 开头,则会以玩家身份执行命令。
*/
CHAT((player, string) -> {
if (string == null) return;
MessageUtils.setPlaceholders(player, Collections.singletonList(string)).forEach(player::chat);
}),
/**
* 以后台的形式执行指令
* 指令内容不需要以“/”开头。
*/
CONSOLE((player, string) -> {
if (string == null) return;
MessageUtils.setPlaceholders(player, Collections.singletonList(string))
.forEach(message -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(), message));
}),
/**
* 向玩家发送消息。
*/
MESSAGE(MessageUtils::send),
/**
* 向玩家发送声音。
* 允许配置音量与音调
* <ul>
* <li>SOUND_NAME</li>
* <li>SOUND_NAME:VOLUME</li>
* <li>SOUND_NAME:VOLUME:PITCH</li>
* </ul>
*/
SOUND((player, string) -> {
if (string == null) return;
try {
String[] args = string.contains(":") ? string.split(":") : new String[]{string};
Sound sound = Arrays.stream(Sound.values())
.filter(s -> s.name().equals(args[0]))
.findFirst().orElse(null);
if (sound == null) return;
float volume = args.length > 1 ? Float.parseFloat(args[1]) : 1F;
float pitch = args.length > 2 ? Float.parseFloat(args[2]) : 1F;
player.playSound(player.getLocation(), sound, volume, pitch);
} catch (Exception ignored) {
}
}),
/**
* 为玩家关闭GUI。
*/
CLOSE((player, string) -> player.closeInventory());
BiConsumer<@NotNull Player, @Nullable String> executor;
GUIActionType(BiConsumer<@NotNull Player, @Nullable String> executor) {
this.executor = executor;
}
public BiConsumer<@NotNull Player, @Nullable String> getExecutor() {
return executor;
}
public static GUIActionType readActionType(String string) {
return Arrays.stream(GUIActionType.values())
.filter(action -> action.name().equalsIgnoreCase(string))
.findFirst().orElse(null);
}
}
@@ -0,0 +1,72 @@
package cc.carm.lib.easyplugin.gui.configuration;
import cc.carm.lib.easyplugin.gui.GUI;
import cc.carm.lib.easyplugin.gui.GUIType;
import cc.carm.lib.easyplugin.utils.ColorParser;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.jetbrains.annotations.Nullable;
import java.util.*;
import java.util.stream.Collectors;
public class GUIConfiguration {
String title;
int lines;
List<GUIItemConfiguration> guiItems;
public GUIConfiguration(String title, int lines, List<GUIItemConfiguration> guiItems) {
this.title = title;
this.lines = lines;
this.guiItems = guiItems;
}
public String getTitle() {
return ColorParser.parse(title);
}
public int getLines() {
return lines;
}
public GUIType getGUIType() {
return Optional.of(GUIType.getByLines(lines))
.map(type -> type == GUIType.CANCEL ? GUIType.SIX_BY_NINE : type)
.get();
}
public List<GUIItemConfiguration> getGuiItems() {
return guiItems;
}
public void setupItems(Player player, GUI gui) {
getGuiItems().forEach(itemConfiguration -> itemConfiguration.setupItems(player, gui));
}
public static GUIConfiguration readConfiguration(@Nullable ConfigurationSection section) {
if (section == null) return new GUIConfiguration("name", 6, new ArrayList<>());
String title = section.getString("title", "");
int lines = section.getInt("lines", 6);
ConfigurationSection itemsSection = section.getConfigurationSection("items");
if (itemsSection == null) return new GUIConfiguration(title, lines, new ArrayList<>());
return new GUIConfiguration(
title, lines, itemsSection.getKeys(false).stream()
.map(key -> GUIItemConfiguration.readFrom(itemsSection.getConfigurationSection(key)))
.filter(Objects::nonNull)
.collect(Collectors.toList())
);
}
public static ClickType readClickType(String type) {
return Arrays.stream(ClickType.values())
.filter(click -> click.name().equalsIgnoreCase(type))
.findFirst().orElse(null);
}
}
@@ -0,0 +1,92 @@
package cc.carm.lib.easyplugin.gui.configuration;
import cc.carm.lib.easyplugin.gui.GUI;
import cc.carm.lib.easyplugin.gui.GUIItem;
import cc.carm.lib.easyplugin.utils.ItemStackFactory;
import cc.carm.lib.easyplugin.utils.MessageUtils;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
public class GUIItemConfiguration {
Material material;
int data;
String name;
@NotNull List<String> lore;
@NotNull List<Integer> slots;
@NotNull List<GUIActionConfiguration> actions;
public GUIItemConfiguration(Material material, int data,
String name, @NotNull List<String> lore,
@NotNull List<GUIActionConfiguration> actions,
@NotNull List<Integer> slots) {
this.material = material;
this.data = data;
this.name = name;
this.lore = lore;
this.slots = slots;
this.actions = actions;
}
public void setupItems(Player player, GUI gui) {
ItemStackFactory icon = new ItemStackFactory(this.material);
icon.setDurability(this.data);
if (this.name != null) icon.setDisplayName(this.name);
icon.setLore(MessageUtils.setPlaceholders(player, this.lore));
GUIItem item = new GUIItem(icon.toItemStack());
this.actions.stream().map(GUIActionConfiguration::toClickAction).forEach(item::addClickAction);
this.slots.forEach(slot -> gui.setItem(slot, item));
}
@Nullable
public static GUIItemConfiguration readFrom(@Nullable ConfigurationSection itemSection) {
if (itemSection == null) return null;
Material material = Optional.ofNullable(Material.matchMaterial(itemSection.getString("material", "STONE"))).orElse(Material.STONE);
int data = itemSection.getInt("data", 0);
String name = itemSection.getString("name");
List<String> lore = itemSection.getStringList("lore");
List<Integer> slots = itemSection.getIntegerList("slots");
int slot = itemSection.getInt("slot", 0);
List<String> actionsString = itemSection.getStringList("actions");
List<GUIActionConfiguration> actions = new ArrayList<>();
for (String actionString : actionsString) {
int prefixStart = actionString.indexOf("[");
int prefixEnd = actionString.indexOf("]");
if (prefixStart < 0 || prefixEnd < 0) continue;
String prefix = actionString.substring(prefixStart + 1, prefixEnd);
ClickType clickType = null;
GUIActionType actionType;
if (prefix.contains(":")) {
String[] args = prefix.split(":");
clickType = GUIConfiguration.readClickType(args[0]);
actionType = GUIActionType.readActionType(args[1]);
} else {
actionType = GUIActionType.readActionType(prefix);
}
if (actionType == null) continue;
actions.add(new GUIActionConfiguration(clickType, actionType, actionString.substring(prefixEnd + 1).trim()));
}
return new GUIItemConfiguration(
material, data, name, lore, actions,
slots.size() > 0 ? slots : Collections.singletonList(slot)
);
}
}
@@ -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;
}
}
@@ -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);
}
}
@@ -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();
}
@@ -0,0 +1,52 @@
import cc.carm.lib.easyplugin.gui.configuration.GUIActionType;
import cc.carm.lib.easyplugin.gui.configuration.GUIConfiguration;
import org.bukkit.event.inventory.ClickType;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
public class ActionReadTest {
@Test
public void test() {
List<String> actions = Arrays.asList(
"[CHAT] 123123",
"[SHIFT_LEFT:CHAT] /test qwq",
"[CONSOLE] say hello",
"[CLOSE]"
);
for (String actionString : actions) {
int prefixStart = actionString.indexOf("[");
int prefixEnd = actionString.indexOf("]");
if (prefixStart < 0 || prefixEnd < 0) continue;
String prefix = actionString.substring(prefixStart + 1, prefixEnd);
ClickType clickType = null;
GUIActionType actionType;
if (prefix.contains(":")) {
String[] args = prefix.split(":");
clickType = GUIConfiguration.readClickType(args[0]);
actionType = GUIActionType.readActionType(args[1]);
} else {
actionType = GUIActionType.readActionType(prefix);
}
if (actionType == null) {
System.out.println("# " + actionString);
System.out.println("- actionType is Null");
continue;
}
System.out.println("# " + actionType.name() + " " + (clickType == null ? "" : clickType.name()));
System.out.println("- " + actionString.substring(prefixEnd + 1).trim());
}
}
}