Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

Feature/multipagegui #48

Merged
merged 6 commits into from
Jun 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 27 additions & 11 deletions src/main/java/tech/inudev/metaverseplugin/define/Gui.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,23 @@
* @author kumitatepazuru
*/
public class Gui implements Listener {
record PosMenuItem(MenuItem menuItem, int x, int y){
record PosMenuItem(MenuItem menuItem, int x, int y) {
}

@Getter
private final List<PosMenuItem> menuItems = new ArrayList<>();
@Getter
@Setter
private String title;
private List<PosMenuItem> menuItems = new ArrayList<>();

/**
* GUIのタイトル
*/
@Getter
private Inventory inventory;
@Setter
protected String title;

/**
* GUIに使うインベントリ
*/
protected Inventory inventory;

/**
* コンストラクタ
Expand All @@ -55,8 +62,8 @@ public Gui(String title) {
* @param x アイテムを設置するX座標。左が0。Java版のみに適応する。
* @param y アイテムを設置するY座標。上が0。Java版のみに適応する。
*/
public void addItem(MenuItem menuItem,int x,int y) {
menuItems.add(new PosMenuItem(menuItem,x,y));
public void addItem(MenuItem menuItem, int x, int y) {
menuItems.add(new PosMenuItem(menuItem, x, y));
}

/**
Expand All @@ -65,16 +72,25 @@ public void addItem(MenuItem menuItem,int x,int y) {
* @param player GUIを開くプレイヤー
*/
public void open(Player player) {
if (FloodgateApi.getInstance().isFloodgatePlayer(player.getUniqueId())) {
if (isBedrock(player)) {
openBedrockImpl(player);
} else {
openJavaImpl(player);
}
}

/**
* Playerが統合版か確認する関数
*
* @param player プレイヤー
* @return 統合版ならtrue
*/
public static boolean isBedrock(Player player) {
return FloodgateApi.getInstance().isFloodgatePlayer(player.getUniqueId());
}

private void openBedrockImpl(Player player) {
final SimpleForm.Builder builder = SimpleForm.builder()
.title(title);
final SimpleForm.Builder builder = SimpleForm.builder().title(title);

for (PosMenuItem posItem : menuItems) {
MenuItem item = posItem.menuItem();
Expand Down
146 changes: 146 additions & 0 deletions src/main/java/tech/inudev/metaverseplugin/define/MultiPageGui.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package tech.inudev.metaverseplugin.define;

import lombok.Getter;
import lombok.Setter;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import tech.inudev.metaverseplugin.Metaverseplugin;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
* いくらでもMenuItemを追加できるGUI。
* 場所の指定はできず、順番に並んでいく。
*
* @author kumitatepazuru
*/
public class MultiPageGui extends Gui {

private final List<MenuItem> menuItems = new ArrayList<>();

@Getter
@Setter
private MenuItem centerItem = new MenuItem("閉じる", null, null, new ItemStack(Material.BARRIER));

@Getter
private int page = 1;

/**
* コンストラクタ
*
* @param title GUIのタイトル
*/
public MultiPageGui(String title) {
super(title);
}

/**
* GUIのアイテムを追加する
*
* @param menuItem 追加するアイテム
*/
public void addMenuItems(MenuItem... menuItem) {
menuItems.addAll(Arrays.stream(menuItem).toList());
}

/**
* GUIのアイテムを追加する
*
* @param menuItem 追加するアイテム
*/
public void addMenuItems(Collection<MenuItem> menuItem) {
menuItems.addAll(menuItem);
}

@Override
public void open(Player p) {
if (Gui.isBedrock(p)) {
Gui gui = new Gui(title);
gui.setMenuItems(menuItems.stream().map(n -> new Gui.PosMenuItem(n, 0, 0)).collect(Collectors.toList()));
gui.open(p);
} else {
inventory = Bukkit.createInventory(null, 27, Component.text(title));
update();
Bukkit.getPluginManager().registerEvents(this, Metaverseplugin.getInstance());
p.openInventory(inventory);
}
}

private void update() {
inventory.clear();
final ItemStack back_button = new ItemStack(Material.RED_WOOL);
ItemMeta itemMeta = back_button.getItemMeta();
itemMeta.displayName(Component.text("前ページ"));
back_button.setItemMeta(itemMeta);

final ItemStack up_button = new ItemStack(Material.GREEN_WOOL);
itemMeta = up_button.getItemMeta();
itemMeta.displayName(Component.text("次ページ"));
up_button.setItemMeta(itemMeta);

final ItemStack centerButton = centerItem.getIcon();

inventory.setItem(3, back_button);
inventory.setItem(4, centerButton);
inventory.setItem(5, up_button);
int count = 0;
for (MenuItem i : menuItems) {
if ((page - 1) * 18 - 1 < count && count < page * 18) {
inventory.setItem(count + 9 - (page - 1) * 18, i.getIcon());
}
count++;
}
}

@EventHandler
@Override
public void onInventoryClick(InventoryClickEvent e) {
final Inventory inv = e.getInventory();
final int id = e.getRawSlot();

if (inv.equals(inventory)) {
e.setCancelled(true);

switch (id) {
case 3 -> {
if (page != 1) {
page--;
update();
}
}
case 5 -> {
if (page < Math.floor(menuItems.size() / 18f) + 1) {
page++;
update();
}
}
case 4 -> {
if (centerItem.getOnClick() != null)
centerItem.getOnClick().accept(centerItem, (Player) e.getWhoClicked());
if (centerItem.isClose()) {
inventory.close();
}
}
}
if (id > 8 && id < 27 && e.getCurrentItem() != null) {
final MenuItem clickedMenuItem = menuItems.get((page - 1) * 18 + id - 9);
if (clickedMenuItem.getOnClick() != null)
clickedMenuItem.getOnClick().accept(clickedMenuItem, (Player) e.getWhoClicked());
if (clickedMenuItem.isClose()) {
inventory.close();
}
}
}
}
}