Skip to content

Commit

Permalink
Optimize switching operations within Inventory.
Browse files Browse the repository at this point in the history
  • Loading branch information
huanmeng-qwq committed Mar 17, 2023
1 parent 18c0dee commit c9e7189
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 6 deletions.
53 changes: 47 additions & 6 deletions src/main/kotlin/me/huanmeng/opensource/bukkit/gui/GuiManager.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.huanmeng.opensource.bukkit.gui;

import com.google.common.base.Preconditions;
import me.huanmeng.opensource.bukkit.gui.event.InventorySwitchEvent;
import me.huanmeng.opensource.bukkit.gui.holder.GuiHolder;
import me.huanmeng.opensource.bukkit.scheduler.SchedulerAsync;
import me.huanmeng.opensource.bukkit.scheduler.SchedulerSync;
Expand All @@ -9,10 +10,8 @@
import org.bukkit.entity.Player;
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.inventory.InventoryOpenEvent;
import org.bukkit.event.inventory.*;
import org.bukkit.inventory.CraftingInventory;
import org.bukkit.inventory.Inventory;
import org.bukkit.plugin.java.JavaPlugin;

Expand Down Expand Up @@ -84,8 +83,32 @@ public boolean isOpenGui(UUID user, AbstractGui<?> gui) {
}

@EventHandler
public void onInventoryClick(InventoryClickEvent e) {
public void onClick(InventoryClickEvent e) {
if (!(e.getWhoClicked() instanceof Player)) {
return;
}
Player player = (Player) e.getWhoClicked();
onInventoryClick(e, player);
if (e instanceof InventorySwitchEvent) {
return;
}
// 玩家在自己背包里面改尝试切换物品栏(0-9)的物品
if (e.getClick() == ClickType.NUMBER_KEY && (Objects.equals(e.getClickedInventory(), e.getWhoClicked().getInventory())
|| e.getClickedInventory() instanceof CraftingInventory)
) {
InventorySwitchEvent event = new InventorySwitchEvent(e.getView(), InventoryType.SlotType.OUTSIDE, e.getHotbarButton(), ClickType.UNKNOWN, e.getAction(), e.getHotbarButton());
event.setCurrentItem(player.getInventory().getItem(e.getHotbarButton()));
onInventoryClick(event, player);
if (event.disable()) {
return;
}
if (event.isCancelled()) {
e.setCancelled(true);
}
}
}

protected void onInventoryClick(InventoryClickEvent e, Player player) {
UUID uuid = player.getUniqueId();
if (!isOpenGui(uuid)) {
return;
Expand All @@ -101,6 +124,12 @@ public void onInventoryClick(InventoryClickEvent e) {

@EventHandler
public void onInventoryDrag(InventoryDragEvent e) {
if (e.getInventory() == null) {
return;
}
if (!(e.getWhoClicked() instanceof Player)) {
return;
}
Player player = (Player) e.getWhoClicked();
UUID uuid = player.getUniqueId();
if (!isOpenGui(uuid)) {
Expand All @@ -111,6 +140,12 @@ public void onInventoryDrag(InventoryDragEvent e) {

@EventHandler
public void onInventoryOpen(InventoryOpenEvent e) {
if (null == e.getInventory()) {
return;
}
if (!(e.getPlayer() instanceof Player)) {
return;
}
Player player = (Player) e.getPlayer();
UUID uuid = player.getUniqueId();
if (userNextOpenGui.containsKey(uuid)) {
Expand All @@ -121,6 +156,12 @@ public void onInventoryOpen(InventoryOpenEvent e) {

@EventHandler
public void onInventoryClose(InventoryCloseEvent e) {
if (e.getInventory() == null) {
return;
}
if (!(e.getPlayer() instanceof Player)) {
return;
}
Player player = (Player) e.getPlayer();
UUID uuid = player.getUniqueId();
AbstractGui<?> gui = getUserOpenGui(uuid);
Expand All @@ -132,7 +173,7 @@ public void onInventoryClose(InventoryCloseEvent e) {
public void refreshGui(Player player) {
UUID uuid = player.getUniqueId();
if (isOpenGui(uuid)) {
// 当更换gui后应该刷新所以按钮的显示
// 当更换gui后应该刷新所有按钮的显示
Runnable refreshRunnable = () -> getUserOpenGui(uuid).refresh(true);
if (Bukkit.isPrimaryThread()) {
refreshRunnable.run();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package me.huanmeng.opensource.bukkit.gui.event;

import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.InventoryView;

/**
* 2023/3/17<br>
* Gui<br>
*
* @author huanmeng_qwq
*/
public class InventorySwitchEvent extends InventoryClickEvent {
private boolean disable;

public InventorySwitchEvent(InventoryView view, InventoryType.SlotType type, int slot, ClickType click, InventoryAction action) {
super(view, type, slot, click, action);
}

public InventorySwitchEvent(InventoryView view, InventoryType.SlotType type, int slot, ClickType click, InventoryAction action, int key) {
super(view, type, slot, click, action, key);
}

public boolean disable() {
return disable;
}

public InventorySwitchEvent disable(boolean disable) {
this.disable = disable;
return this;
}
}

0 comments on commit c9e7189

Please sign in to comment.