133 lines
4.6 KiB
Java
133 lines
4.6 KiB
Java
package com.io.yutian.aulib.gui;
|
|
|
|
import com.io.yutian.aulib.AuLib;
|
|
import com.io.yutian.aulib.gui.button.Button;
|
|
import com.io.yutian.aulib.gui.button.ClickType;
|
|
import com.io.yutian.aulib.gui.button.ItemButton;
|
|
import org.bukkit.Sound;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.event.inventory.InventoryAction;
|
|
import org.bukkit.event.inventory.InventoryClickEvent;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
public class Gui extends IGui {
|
|
|
|
private Map<Integer, Button> buttons = new HashMap<>();
|
|
|
|
public Gui(Player player, String title, int size) {
|
|
super(player, title, size);
|
|
}
|
|
|
|
@Override
|
|
public void init() {
|
|
}
|
|
|
|
@Override
|
|
public void handler(Player player, int slot, InventoryClickEvent event) {
|
|
if (buttons.containsKey(slot)) {
|
|
Button button = buttons.get(slot);
|
|
if (button == null) {
|
|
if (slot < inventory.getSize()) {
|
|
event.setCancelled(true);
|
|
} else {
|
|
if (event.getAction().equals(InventoryAction.MOVE_TO_OTHER_INVENTORY)) {
|
|
event.setCancelled(true);
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
event.setCancelled(true);
|
|
clickButton(event, slot, button);
|
|
if (button.isPlaySound()) {
|
|
player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 1.0f, 1.0f);
|
|
}
|
|
InventoryAction action = event.getAction();
|
|
ClickType clickType = ClickType.LEFT_CLICK;
|
|
if (action.equals(InventoryAction.PICKUP_ALL)) {
|
|
clickType = ClickType.LEFT_CLICK;
|
|
} else if (action.equals(InventoryAction.PICKUP_HALF)) {
|
|
clickType = ClickType.RIGHT_CLICK;
|
|
} else if (action.equals(InventoryAction.MOVE_TO_OTHER_INVENTORY)) {
|
|
clickType = ClickType.SHIFT_CLICK;
|
|
}
|
|
if (button.getClickConsumer() != null) {
|
|
button.getClickConsumer().accept(player, clickType);
|
|
}
|
|
} else {
|
|
if (slot < inventory.getSize()) {
|
|
event.setCancelled(true);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void addButton(int index, Button button) {
|
|
buttons.put(index, button);
|
|
}
|
|
|
|
public Button getButton(int index) {
|
|
return buttons.getOrDefault(index, null);
|
|
}
|
|
|
|
public final void initButton(int index) {
|
|
inventory.setItem(index, null);
|
|
Button button = getButton(index);
|
|
if (button == null) {
|
|
return;
|
|
}
|
|
if (button.isAsynchronous()) {
|
|
AuLib.inst().getServer().getScheduler().runTaskAsynchronously(AuLib.inst(), ()->{
|
|
if (button instanceof ItemButton itemButton) {
|
|
if (itemButton.getItem() != null) {
|
|
inventory.setItem(index, itemButton.getItem());
|
|
return;
|
|
}
|
|
}
|
|
inventory.setItem(index, button.getItemStack());
|
|
});
|
|
} else {
|
|
if (button instanceof ItemButton itemButton) {
|
|
if (itemButton.getItem() != null) {
|
|
inventory.setItem(index, itemButton.getItem());
|
|
return;
|
|
}
|
|
}
|
|
inventory.setItem(index, button.getItemStack());
|
|
}
|
|
}
|
|
|
|
public final void initButton() {
|
|
for (int i = 0; i < inventory.getSize(); i++) {
|
|
inventory.setItem(i, null);
|
|
}
|
|
for (Map.Entry<Integer, Button> entry : buttons.entrySet()) {
|
|
Button button = entry.getValue();
|
|
if (button.isAsynchronous()) {
|
|
AuLib.inst().getServer().getScheduler().runTaskAsynchronously(AuLib.inst(), ()->{
|
|
if (button instanceof ItemButton itemButton) {
|
|
if (itemButton.getItem() != null) {
|
|
inventory.setItem(entry.getKey(), itemButton.getItem());
|
|
return;
|
|
}
|
|
}
|
|
inventory.setItem(entry.getKey(), button.getItemStack());
|
|
});
|
|
} else {
|
|
if (button instanceof ItemButton itemButton) {
|
|
if (itemButton.getItem() != null) {
|
|
inventory.setItem(entry.getKey(), itemButton.getItem());
|
|
continue;
|
|
}
|
|
}
|
|
inventory.setItem(entry.getKey(), button.getItemStack());
|
|
}
|
|
}
|
|
}
|
|
|
|
public Map<Integer, Button> getButtons() {
|
|
return buttons;
|
|
}
|
|
|
|
}
|