+ update MenuBuilder v2

This commit is contained in:
坏黑 2019-05-21 23:42:13 +08:00
parent 560b407435
commit 47d481bbfc
11 changed files with 316 additions and 2 deletions

View File

@ -5,7 +5,7 @@ plugins {
id 'com.github.johnrengelman.shadow' version '4.0.4'
}
group = 'me.skymc'
version = '4.79'
version = '4.8'
sourceCompatibility = 1.8
targetCompatibility = 1.8

View File

@ -96,7 +96,6 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor {
public void registerSubCommand(BaseSubCommand subCommand) {
if (subCommand != null) {
Preconditions.checkArgument(subCommand.getLabel() != null, "Command label can not be null");
Preconditions.checkArgument(subCommand.getDescription() != null, "Command description can not be null");
Preconditions.checkArgument(subCommand.getArguments() != null, "Command arguments can not be null");
}
subCommands.add(subCommand);

View File

@ -15,6 +15,7 @@ import java.util.HashMap;
* @Since 2018-08-22 13:40
* @BuilderVersion 1.0
*/
@Deprecated
public class MenuBuilder {
private boolean lock;

View File

@ -0,0 +1,38 @@
package me.skymc.taboolib.inventory.builder.v2;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.inventory.InventoryInteractEvent;
/**
* @Author 坏黑
* @Since 2019-05-21 18:09
*/
public class ClickEvent {
private ClickType clickType;
private Event event;
public ClickEvent(ClickType clickType, Event event) {
this.clickType = clickType;
this.event = event;
}
public InventoryClickEvent castClick() {
return (InventoryClickEvent) event;
}
public InventoryDragEvent castDrag() {
return (InventoryDragEvent) event;
}
public ClickType getClickType() {
return clickType;
}
public Player getClicker() {
return (Player) ((InventoryInteractEvent) event).getWhoClicked();
}
}

View File

@ -0,0 +1,66 @@
package me.skymc.taboolib.inventory.builder.v2;
import me.skymc.taboolib.listener.TListener;
import org.bukkit.Bukkit;
import org.bukkit.entity.HumanEntity;
import org.bukkit.event.EventHandler;
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.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerItemHeldEvent;
import org.bukkit.event.server.PluginDisableEvent;
import java.util.Optional;
/**
* @Author 坏黑
* @Since 2019-05-21 18:16
*/
@TListener
class ClickListener implements Listener {
@EventHandler
public void onDisable(PluginDisableEvent e) {
Bukkit.getOnlinePlayers().stream().filter(player -> player.getOpenInventory().getTopInventory().getHolder() instanceof MenuHolder && e.getPlugin().equals(((MenuHolder) player.getOpenInventory().getTopInventory().getHolder()).getBuilder().getPlugin())).forEach(HumanEntity::closeInventory);
}
@EventHandler
public void onClick(InventoryClickEvent e) {
if (e.getInventory().getHolder() instanceof MenuHolder) {
if (((MenuHolder) e.getInventory().getHolder()).getBuilder().isLockHand() && (e.getRawSlot() - e.getInventory().getSize() - 27 == e.getWhoClicked().getInventory().getHeldItemSlot() || (e.getClick() == org.bukkit.event.inventory.ClickType.NUMBER_KEY && e.getHotbarButton() == e.getWhoClicked().getInventory().getHeldItemSlot()))) {
e.setCancelled(true);
}
Optional.ofNullable(((MenuHolder) e.getInventory().getHolder()).getBuilder().getClickTask()).ifPresent(t -> t.run(new ClickEvent(ClickType.CLICK, e)));
}
}
@EventHandler
public void onDrag(InventoryDragEvent e) {
if (e.getInventory().getHolder() instanceof MenuHolder) {
Optional.ofNullable(((MenuHolder) e.getInventory().getHolder()).getBuilder().getClickTask()).ifPresent(t -> t.run(new ClickEvent(ClickType.DRAG, e)));
}
}
@EventHandler
public void onDrag(InventoryCloseEvent e) {
if (e.getInventory().getHolder() instanceof MenuHolder) {
Optional.ofNullable(((MenuHolder) e.getInventory().getHolder()).getBuilder().getCloseTask()).ifPresent(t -> t.run(e));
}
}
@EventHandler
public void onDrop(PlayerDropItemEvent e) {
if (e.getPlayer().getOpenInventory().getTopInventory().getHolder() instanceof MenuHolder && ((MenuHolder) e.getPlayer().getOpenInventory().getTopInventory().getHolder()).getBuilder().isLockHand()) {
e.setCancelled(true);
}
}
@EventHandler
public void onHeld(PlayerItemHeldEvent e) {
if (e.getPlayer().getOpenInventory().getTopInventory().getHolder() instanceof MenuHolder && ((MenuHolder) e.getPlayer().getOpenInventory().getTopInventory().getHolder()).getBuilder().isLockHand()) {
e.setCancelled(true);
}
}
}

View File

@ -0,0 +1,22 @@
package me.skymc.taboolib.inventory.builder.v2;
import me.skymc.taboolib.listener.TListener;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerSwapHandItemsEvent;
/**
* @Author 坏黑
* @Since 2019-05-21 22:04
*/
@TListener
class ClickListener1_9 implements Listener {
@EventHandler
public void onSwap(PlayerSwapHandItemsEvent e) {
if (e.getPlayer().getOpenInventory().getTopInventory().getHolder() instanceof MenuHolder && ((MenuHolder) e.getPlayer().getOpenInventory().getTopInventory().getHolder()).getBuilder().isLockHand()) {
e.setCancelled(true);
}
}
}

View File

@ -0,0 +1,11 @@
package me.skymc.taboolib.inventory.builder.v2;
/**
* @Author 坏黑
* @Since 2019-05-21 18:14
*/
public interface ClickTask {
void run(ClickEvent event);
}

View File

@ -0,0 +1,11 @@
package me.skymc.taboolib.inventory.builder.v2;
/**
* @Author 坏黑
* @Since 2019-05-21 18:10
*/
public enum ClickType {
CLICK, DRAG
}

View File

@ -0,0 +1,13 @@
package me.skymc.taboolib.inventory.builder.v2;
import org.bukkit.event.inventory.InventoryCloseEvent;
/**
* @Author 坏黑
* @Since 2019-05-21 18:14
*/
public interface CloseTask {
void run(InventoryCloseEvent event);
}

View File

@ -0,0 +1,127 @@
package me.skymc.taboolib.inventory.builder.v2;
import com.google.common.collect.Maps;
import com.ilummc.tlib.util.Ref;
import me.skymc.taboolib.Main;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import java.util.Map;
/**
* @Author 坏黑
* @Since 2019-05-21 15:54
*/
public class MenuBuilder {
private Map<Character, ItemStack> keys = Maps.newHashMap();
private Plugin plugin;
private String title;
private int rows;
private char[][] items;
private ClickTask clickTask;
private CloseTask closeTask;
private boolean lockHand;
public MenuBuilder(Plugin plugin) {
this.plugin = plugin;
}
public static MenuBuilder builder(Plugin plugin) {
return new MenuBuilder(plugin);
}
public static MenuBuilder builder() {
return new MenuBuilder(Ref.getCallerPlugin(Ref.getCallerClass(3).orElse(Main.class)));
}
public MenuBuilder lockHand() {
this.lockHand = true;
return this;
}
public MenuBuilder event(ClickTask clickTask) {
this.clickTask = clickTask;
return this;
}
public MenuBuilder close(CloseTask closeTask) {
this.closeTask = closeTask;
return this;
}
public MenuBuilder title(String title) {
this.title = title;
return this;
}
public MenuBuilder rows(int rows) {
this.rows = rows * 9;
return this;
}
public MenuBuilder put(char key, ItemStack item) {
keys.put(key, item);
return this;
}
public MenuBuilder items(String... placeholder) {
items = new char[placeholder.length][];
for (int i = 0; i < placeholder.length; i++) {
items[i] = placeholder[i].toCharArray();
}
return this;
}
public Inventory build() {
Inventory inventory = Bukkit.createInventory(new MenuHolder(this), rows, String.valueOf(title));
for (int i = 0; i < items.length && i < rows; i++) {
char[] line = items[i];
for (int j = 0; j < line.length && j < 9; j++) {
inventory.setItem(i * 9 + j, keys.getOrDefault(line[j], new ItemStack(Material.AIR)));
}
}
return inventory;
}
// *********************************
//
// Getter and Setter
//
// *********************************
public Map<Character, ItemStack> getKeys() {
return keys;
}
public Plugin getPlugin() {
return plugin;
}
public String getTitle() {
return title;
}
public int getRows() {
return rows;
}
public char[][] getItems() {
return items;
}
public ClickTask getClickTask() {
return clickTask;
}
public CloseTask getCloseTask() {
return closeTask;
}
public boolean isLockHand() {
return lockHand;
}
}

View File

@ -0,0 +1,26 @@
package me.skymc.taboolib.inventory.builder.v2;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
/**
* @Author 坏黑
* @Since 2019-05-21 20:28
*/
class MenuHolder implements InventoryHolder {
private MenuBuilder builder;
public MenuHolder(MenuBuilder builder) {
this.builder = builder;
}
public MenuBuilder getBuilder() {
return builder;
}
@Override
public Inventory getInventory() {
return null;
}
}