项目初始化

This commit is contained in:
2022-03-09 04:14:47 +08:00
commit eda94fb915
8 changed files with 603 additions and 0 deletions
@@ -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;
}
}
@@ -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;
}
}
@@ -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);
}
}
@@ -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);
}
}
@@ -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<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.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<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();
}
+13
View File
@@ -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":