实现 #12 MenuBuilder 父级菜单嵌套

This commit is contained in:
IzzelAliz 2019-02-19 20:16:38 +08:00
parent 9b4448a033
commit b3233e4049
3 changed files with 31 additions and 2 deletions

View File

@ -20,6 +20,7 @@ public class MenuBuilder {
private boolean lock; private boolean lock;
private String name; private String name;
private int rows = 9; private int rows = 9;
private Inventory parent;
private HashMap<Integer, MenuBuilderItem> items = new HashMap<>(); private HashMap<Integer, MenuBuilderItem> items = new HashMap<>();
@ -56,8 +57,13 @@ public class MenuBuilder {
return this; return this;
} }
public MenuBuilder parent(Inventory parent) {
this.parent = parent;
return this;
}
public Inventory build() { public Inventory build() {
Inventory inventory = Bukkit.createInventory(new MenuBuilderHolder(lock, items), rows, name); Inventory inventory = Bukkit.createInventory(new MenuBuilderHolder(lock, items, parent), rows, name);
items.forEach((key, value) -> inventory.setItem(key, value.getItemStack())); items.forEach((key, value) -> inventory.setItem(key, value.getItemStack()));
return inventory; return inventory;
} }

View File

@ -13,10 +13,16 @@ public class MenuBuilderHolder implements InventoryHolder {
private final boolean lock; private final boolean lock;
private final HashMap<Integer, MenuBuilderItem> items; private final HashMap<Integer, MenuBuilderItem> items;
private final Inventory parent;
public MenuBuilderHolder(boolean lock, HashMap<Integer, MenuBuilderItem> items) { public MenuBuilderHolder(boolean lock, HashMap<Integer, MenuBuilderItem> items, Inventory parent) {
this.lock = lock; this.lock = lock;
this.items = items; this.items = items;
this.parent = parent;
}
public MenuBuilderHolder(boolean lock, HashMap<Integer, MenuBuilderItem> items) {
this(lock, items, null);
} }
@Override @Override
@ -37,4 +43,8 @@ public class MenuBuilderHolder implements InventoryHolder {
public HashMap<Integer, MenuBuilderItem> getItems() { public HashMap<Integer, MenuBuilderItem> getItems() {
return items; return items;
} }
public Inventory getParent() {
return parent;
}
} }

View File

@ -1,12 +1,15 @@
package me.skymc.taboolib.inventory.builder.menu; package me.skymc.taboolib.inventory.builder.menu;
import me.skymc.taboolib.Main;
import me.skymc.taboolib.listener.TListener; import me.skymc.taboolib.listener.TListener;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryAction; import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import java.util.Optional; import java.util.Optional;
@ -33,4 +36,14 @@ public class MenuBuilderListener implements Listener {
}); });
} }
@EventHandler
public void onClose(InventoryCloseEvent event) {
if (event.getInventory().getHolder() instanceof MenuBuilderHolder) {
MenuBuilderHolder holder = (MenuBuilderHolder) event.getInventory().getHolder();
if (holder.getParent() != null) {
Bukkit.getScheduler().runTask(Main.getInst(), () -> event.getPlayer().openInventory(holder.getParent()));
}
}
}
} }