From eda94fb9155289a545e22a5c80c31bb8a24a8c11 Mon Sep 17 00:00:00 2001 From: CarmJos Date: Wed, 9 Mar 2022 04:14:47 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 165 ++++++++++++++++++ src/main/java/cc/carm/plugin/test/Main.java | 17 ++ .../carm/plugin/test/command/TestCommand.java | 32 ++++ .../cc/carm/plugin/test/ui/SoundsGUI.java | 49 ++++++ .../plugin/test/utils/gui/AutoPagedGUI.java | 86 +++++++++ .../plugin/test/utils/gui/CommonPagedGUI.java | 162 +++++++++++++++++ .../carm/plugin/test/utils/gui/PagedGUI.java | 79 +++++++++ src/main/resources/plugin.yml | 13 ++ 8 files changed, 603 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/cc/carm/plugin/test/Main.java create mode 100644 src/main/java/cc/carm/plugin/test/command/TestCommand.java create mode 100644 src/main/java/cc/carm/plugin/test/ui/SoundsGUI.java create mode 100644 src/main/java/cc/carm/plugin/test/utils/gui/AutoPagedGUI.java create mode 100644 src/main/java/cc/carm/plugin/test/utils/gui/CommonPagedGUI.java create mode 100644 src/main/java/cc/carm/plugin/test/utils/gui/PagedGUI.java create mode 100644 src/main/resources/plugin.yml diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..af89430 --- /dev/null +++ b/pom.xml @@ -0,0 +1,165 @@ + + + 4.0.0 + + + 8 + 8 + UTF-8 + UTF-8 + + + cc.carm.plugin + TestPlugin + 1.0-SNAPSHOT + + + + + + cc.carm.lib + easyplugin-bom + 1.3.1 + pom + import + + + + + + + + + central + https://repo1.maven.org/maven2/ + + + + carm-repo + Carm's Repo + https://repo.carm.cc/repository/maven-public/ + + + + + + + + cc.carm.lib + easyplugin-main + + + + cc.carm.lib + easyplugin-configuration + + + + cc.carm.lib + easyplugin-gui + + + + cc.carm.lib + easyplugin-database + + + + de.tr7zw + item-nbt-api + 2.8.0 + compile + + + + org.spigotmc + spigot-api + 1.16.3-R0.1-SNAPSHOT + provided + + + + me.clip + placeholderapi + 2.10.9 + provided + + + + junit + junit + 4.13.2 + test + + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.1 + + false + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 8 + 8 + UTF-8 + -parameters + + + + org.apache.maven.plugins + maven-jar-plugin + 3.2.0 + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.3 + + + package + + shade + + + + + + false + + + cc.carm.lib.easyplugin + cc.carm.plugin.test.lib.easyplugin + + + + + *:* + + META-INF/MANIFEST.MF + META-INF/*.txt + + + + + + + + + src/main/resources + true + + + + \ No newline at end of file diff --git a/src/main/java/cc/carm/plugin/test/Main.java b/src/main/java/cc/carm/plugin/test/Main.java new file mode 100644 index 0000000..b6037b8 --- /dev/null +++ b/src/main/java/cc/carm/plugin/test/Main.java @@ -0,0 +1,17 @@ +package cc.carm.plugin.test; + +import cc.carm.lib.easyplugin.EasyPlugin; +import cc.carm.lib.easyplugin.gui.GUI; +import cc.carm.plugin.test.command.TestCommand; + +public class Main extends EasyPlugin { + + + @Override + protected boolean initialize() { + GUI.initialize(this); + registerCommand("Test", new TestCommand()); + return true; + } + +} diff --git a/src/main/java/cc/carm/plugin/test/command/TestCommand.java b/src/main/java/cc/carm/plugin/test/command/TestCommand.java new file mode 100644 index 0000000..082a40c --- /dev/null +++ b/src/main/java/cc/carm/plugin/test/command/TestCommand.java @@ -0,0 +1,32 @@ +package cc.carm.plugin.test.command; + +import cc.carm.plugin.test.ui.SoundsGUI; +import com.google.gson.Gson; +import de.tr7zw.changeme.nbtapi.NBTItem; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; + +public class TestCommand implements CommandExecutor { + @Override + public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { + if (!(sender instanceof Player)) return true; + Player player = (Player) sender; + if (args.length < 1) return false; + String aim = args[0]; + if (aim.equalsIgnoreCase("gui")) { + SoundsGUI.open((Player) sender); + } else if (aim.equalsIgnoreCase("item")) { + ItemStack item = player.getInventory().getItemInMainHand(); + NBTItem nbtItem = new NBTItem(item); + Gson gson = new Gson(); + player.sendMessage(gson.toJson(nbtItem.getCompound())); + } + + + return true; + } +} diff --git a/src/main/java/cc/carm/plugin/test/ui/SoundsGUI.java b/src/main/java/cc/carm/plugin/test/ui/SoundsGUI.java new file mode 100644 index 0000000..ab2ee6b --- /dev/null +++ b/src/main/java/cc/carm/plugin/test/ui/SoundsGUI.java @@ -0,0 +1,49 @@ +package cc.carm.plugin.test.ui; + +import cc.carm.lib.easyplugin.gui.GUIItem; +import cc.carm.lib.easyplugin.gui.GUIType; +import cc.carm.lib.easyplugin.utils.ItemStackFactory; +import cc.carm.plugin.test.utils.gui.AutoPagedGUI; +import org.bukkit.Material; +import org.bukkit.Sound; +import org.bukkit.entity.Player; +import org.bukkit.event.inventory.ClickType; + +public class SoundsGUI extends AutoPagedGUI { + + Player player; + + + private SoundsGUI(Player player) { + super(GUIType.SIX_BY_NINE, "Sounds", 10, 43); + this.player = player; + setPreviousPageSlot(18); + setNextPageSlot(26); + + loadItems(); + } + + public Player getPlayer() { + return player; + } + + public void loadItems() { + for (Sound value : Sound.values()) { + addItem(new GUIItem(new ItemStackFactory(Material.NOTE_BLOCK) + .setDisplayName(value.name()) + .toItemStack()) { + @Override + public void onClick(ClickType type) { + setDisplay(new ItemStackFactory(Material.DIAMOND_BLOCK).setDisplayName(value.name()).toItemStack()); + getPlayer().playSound(player.getLocation(), value, 1, 1); + updateView(); + } + }); + } + } + + public static void open(Player player) { + SoundsGUI gui = new SoundsGUI(player); + gui.openGUI(player); + } +} diff --git a/src/main/java/cc/carm/plugin/test/utils/gui/AutoPagedGUI.java b/src/main/java/cc/carm/plugin/test/utils/gui/AutoPagedGUI.java new file mode 100644 index 0000000..bd8ca7d --- /dev/null +++ b/src/main/java/cc/carm/plugin/test/utils/gui/AutoPagedGUI.java @@ -0,0 +1,86 @@ +package cc.carm.plugin.test.utils.gui; + +import cc.carm.lib.easyplugin.gui.GUIItem; +import cc.carm.lib.easyplugin.gui.GUIType; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.event.inventory.ClickType; +import org.bukkit.inventory.ItemStack; + +public class AutoPagedGUI extends CommonPagedGUI { + + 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 ? new ItemStack(Material.ARROW) : 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 ? new ItemStack(Material.ARROW) : nextPageUI) { + @Override + public void onClick(ClickType type) { + if (type == ClickType.RIGHT) { + goLastPage(); + } else { + goNextPage(); + } + openGUI(user); + } + }); + } else { + setItem(nextPageSlot, null); + } + } + + + super.openGUI(user); + } + +} diff --git a/src/main/java/cc/carm/plugin/test/utils/gui/CommonPagedGUI.java b/src/main/java/cc/carm/plugin/test/utils/gui/CommonPagedGUI.java new file mode 100644 index 0000000..b25e8cd --- /dev/null +++ b/src/main/java/cc/carm/plugin/test/utils/gui/CommonPagedGUI.java @@ -0,0 +1,162 @@ +package cc.carm.plugin.test.utils.gui; + + +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 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); + } + +} diff --git a/src/main/java/cc/carm/plugin/test/utils/gui/PagedGUI.java b/src/main/java/cc/carm/plugin/test/utils/gui/PagedGUI.java new file mode 100644 index 0000000..510d4f0 --- /dev/null +++ b/src/main/java/cc/carm/plugin/test/utils/gui/PagedGUI.java @@ -0,0 +1,79 @@ +package cc.carm.plugin.test.utils.gui; + + +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 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 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(); + +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..29d20d4 --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,13 @@ +main: cc.carm.plugin.test.Main +name: TestPlugin +version: ${project.version} + +authors: + - CarmJos +api-version: 1.13 + +softdepend: + - PlaceholderAPI + +commands: + "Test": \ No newline at end of file