Improved GUI robustness and fixed critical bug

This commit is contained in:
Tomás F 2020-07-02 00:14:26 +01:00
parent 5e9f3bc791
commit 416167e5c1
7 changed files with 126 additions and 53 deletions

View File

@ -5,7 +5,7 @@ import com.tomff.beesplus.handlers.RightClickHandler;
import com.tomff.beesplus.items.*;
import com.tomff.beesplus.core.UpdateChecker;
import com.tomff.beesplus.core.gui.GuiHandler;
import com.tomff.beesplus.core.gui.GuiManager;
import com.tomff.beesplus.core.gui.GuiViewTracker;
import com.tomff.beesplus.core.items.CustomItemManager;
import com.tomff.beesplus.localization.Localization;
import com.tomff.beesplus.localization.LocalizationWrapper;
@ -19,7 +19,7 @@ import java.util.Locale;
public class BeesPlus extends JavaPlugin {
private GuiManager guiManager;
private GuiViewTracker guiViewTracker;
private CustomItemManager customItemManager;
private LocalizationWrapper localizationWrapper;
@ -28,7 +28,7 @@ public class BeesPlus extends JavaPlugin {
public void onEnable() {
saveDefaultConfig();
guiManager = new GuiManager();
guiViewTracker = new GuiViewTracker();
customItemManager = new CustomItemManager(this);
getServer().getPluginManager().registerEvents(new GuiHandler(this), this);
@ -50,6 +50,11 @@ public class BeesPlus extends JavaPlugin {
});
}
@Override
public void onDisable() {
guiViewTracker.clearViews();
}
private boolean loadLocale() {
String locale = getConfig().getString("locale", Locale.ENGLISH.toLanguageTag());
localizationWrapper = new LocalizationWrapper(this, "locale");
@ -89,8 +94,8 @@ public class BeesPlus extends JavaPlugin {
}
}
public GuiManager getGuiManager() {
return guiManager;
public GuiViewTracker getGuiViewTracker() {
return guiViewTracker;
}
public CustomItemManager getCustomItemManager() {

View File

@ -30,6 +30,10 @@ public abstract class Gui {
}
}
public boolean hasIcon(int slot) {
return icons.containsKey(slot);
}
public Icon getIcon(int slot) {
return icons.get(slot);
}

View File

@ -6,6 +6,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.player.PlayerQuitEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
@ -15,42 +17,69 @@ import java.util.function.Consumer;
public class GuiHandler implements Listener {
private final BeesPlus beesPlus;
private final GuiManager guiManager;
private final GuiViewTracker guiViewTracker;
public GuiHandler(BeesPlus beesPlus) {
this.beesPlus = beesPlus;
this.guiManager = beesPlus.getGuiManager();
this.guiViewTracker = beesPlus.getGuiViewTracker();
}
@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
if (!(event.getWhoClicked() instanceof Player)) {
return;
}
if (!(event.getWhoClicked() instanceof Player)) return;
Inventory clickedInventory = event.getInventory();
Player player = (Player) event.getWhoClicked();
UUID uuid = player.getUniqueId();
if (guiManager.getOpenedGuis().containsKey(uuid) &&
guiManager.getOpenedGuis().get(uuid).getInventory().equals(clickedInventory)) {
if (!guiViewTracker.isViewingGui(uuid)) {
return;
}
event.setCancelled(true);
if (guiViewTracker.getView(uuid).getInventoryView() != event.getView()) {
return;
}
ItemStack clickedItem = event.getCurrentItem();
event.setCancelled(true);
if (clickedItem == null || clickedItem.getType() == Material.AIR) return;
ItemStack clickedItem = event.getCurrentItem();
Gui gui = guiManager.getOpenedGuis().get(uuid);
if (clickedItem == null) return;
if (clickedItem.getType() == Material.AIR) return;
Icon icon = gui.getIcon(event.getRawSlot());
if (icon == null) return;
int slot = event.getRawSlot();
Consumer<Player> callback = icon.getCallback();
if(callback == null) return;
View view = guiViewTracker.getView(uuid);
Gui gui = view.getGui();
icon.getCallback().accept(player);
if (!gui.hasIcon(slot)) return;
Icon icon = gui.getIcon(event.getRawSlot());
Consumer<Player> callback = icon.getCallback();
if(callback == null) return;
icon.getCallback().accept(player);
}
@EventHandler
public void onInventoryClose(InventoryCloseEvent event) {
if (!(event.getPlayer() instanceof Player)) return;
Player player = (Player) event.getPlayer();
UUID uuid = player.getUniqueId();
if (!guiViewTracker.isViewingGui(uuid)) {
return;
}
if (guiViewTracker.getView(uuid).getInventoryView() == event.getView()) {
guiViewTracker.removeView(uuid);
}
}
@EventHandler
public void onLeave(PlayerQuitEvent event) {
guiViewTracker.removeView(event.getPlayer().getUniqueId());
}
}

View File

@ -1,26 +0,0 @@
package com.tomff.beesplus.core.gui;
import org.bukkit.entity.Player;
import java.util.HashMap;
import java.util.UUID;
public class GuiManager {
private HashMap<UUID, Gui> openedGuis;
public GuiManager() {
openedGuis = new HashMap<>();
}
public void openGui(Player player, Gui gui) {
openedGuis.put(player.getUniqueId(), gui);
gui.buildIcons();
player.openInventory(gui.getInventory());
}
public HashMap<UUID, Gui> getOpenedGuis() {
return openedGuis;
}
}

View File

@ -0,0 +1,39 @@
package com.tomff.beesplus.core.gui;
import org.bukkit.entity.Player;
import org.bukkit.inventory.InventoryView;
import java.util.HashMap;
import java.util.UUID;
public class GuiViewTracker {
private HashMap<UUID, View> views;
public GuiViewTracker() {
views = new HashMap<>();
}
public boolean isViewingGui(UUID uuid) {
return views.containsKey(uuid);
}
public View getView(UUID uuid) {
return views.get(uuid);
}
public void removeView(UUID uuid) {
views.remove(uuid);
}
public void clearViews() {
views.forEach((uuid, view) -> view.getInventoryView().close());
}
public void openGui(Player player, Gui gui) {
gui.buildIcons();
InventoryView view = player.openInventory(gui.getInventory());
views.put(player.getUniqueId(), new View(view, gui));
}
}

View File

@ -0,0 +1,22 @@
package com.tomff.beesplus.core.gui;
import org.bukkit.inventory.InventoryView;
public class View {
private final InventoryView inventoryView;
private final Gui gui;
public View(InventoryView inventoryView, Gui gui) {
this.inventoryView = inventoryView;
this.gui = gui;
}
public InventoryView getInventoryView() {
return inventoryView;
}
public Gui getGui() {
return gui;
}
}

View File

@ -3,7 +3,7 @@ package com.tomff.beesplus.handlers;
import com.tomff.beesplus.BeesPlus;
import com.tomff.beesplus.gui.BeeHiveInfo;
import com.tomff.beesplus.gui.BeeInfo;
import com.tomff.beesplus.core.gui.GuiManager;
import com.tomff.beesplus.core.gui.GuiViewTracker;
import org.bukkit.EntityEffect;
import org.bukkit.Material;
import org.bukkit.block.Beehive;
@ -24,7 +24,7 @@ import java.util.Arrays;
public class RightClickHandler implements Listener {
private final BeesPlus beesPlus;
private final GuiManager guiManager;
private final GuiViewTracker guiViewTracker;
private final boolean removeAnger;
@ -40,7 +40,7 @@ public class RightClickHandler implements Listener {
public RightClickHandler(BeesPlus beesPlus) {
this.beesPlus = beesPlus;
this.guiManager = beesPlus.getGuiManager();
this.guiViewTracker = beesPlus.getGuiViewTracker();
removeAnger = beesPlus.getConfig().getBoolean("healing.removeanger", true);
}
@ -64,7 +64,7 @@ public class RightClickHandler implements Listener {
if (player.isSneaking() && player.hasPermission("beesplus.bee.view")) {
event.setCancelled(true);
guiManager.openGui(player, new BeeInfo(bee));
guiViewTracker.openGui(player, new BeeInfo(bee));
return;
}
@ -99,7 +99,7 @@ public class RightClickHandler implements Listener {
event.setCancelled(true);
Beehive beehive = (Beehive) clickedBlock.getState();
guiManager.openGui(player, new BeeHiveInfo(beehive));
guiViewTracker.openGui(player, new BeeHiveInfo(beehive));
}
}
}