Skip to content

Commit

Permalink
GUI Tracking Enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
APickledWalrus committed Sep 5, 2022
1 parent 1d88394 commit ddb3a7f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ private void changeInventory(int size, @Nullable String name) {
viewer.openInventory(newInventory);
viewer.setItemOnCursor(cursor);
}
SkriptGUI.getGUIManager().transferRegistration(this, newInventory);
inventory = newInventory;
this.name = name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@
import org.eclipse.jdt.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;

public class GUIManager {

/**
* A list of all tracked GUIs.
* This list is used for event processing.
* A map for tracking all GUIs based on their Inventory.
* Used mainly during event processing (see {@link io.github.apickledwalrus.skriptgui.gui.events.GUIEvents}).
*/
private final List<GUI> guis = new ArrayList<>();
private final Map<Inventory, GUI> guis = new HashMap<>();

/**
* A map to track the GUI involved in an event.
* Used for tracking GUIs within Skript {@link ch.njol.skript.lang.Trigger}s.
*/
private final WeakHashMap<Event, GUI> eventGUIs = new WeakHashMap<>();

Expand All @@ -28,7 +31,7 @@ public class GUIManager {
* @param gui The GUI to register.
*/
public void register(GUI gui) {
guis.add(gui);
guis.put(gui.getInventory(), gui);
}

/**
Expand All @@ -39,16 +42,28 @@ public void register(GUI gui) {
public void unregister(GUI gui) {
new ArrayList<>(gui.getInventory().getViewers()).forEach(HumanEntity::closeInventory);
gui.clear();
guis.remove(gui);
guis.remove(gui.getInventory());
// Just remove them from the event GUIs list now
eventGUIs.values().removeIf(eventGUI -> eventGUI == gui);
}

/**
* Transfers the Inventory reference tied to the provided GUI's registration.
* This method is preferred over {@link #register(GUI)} and then {@link #unregister(GUI)},
* as it ensures no information (such as Event associations) may be lost.
* @param gui The GUI to modify the registration of.
* @param newInventory The new Inventory to associate with the provided GUI.
*/
public void transferRegistration(GUI gui, Inventory newInventory) {
guis.remove(gui.getInventory());
guis.put(newInventory, gui);
}

/**
* @return A list of tracked GUIs.
*/
public List<GUI> getTrackedGUIs() {
return guis;
public Collection<GUI> getTrackedGUIs() {
return guis.values();
}

/**
Expand Down Expand Up @@ -80,7 +95,7 @@ public void setGUI(Event event, @Nullable GUI gui) {
*/
@Nullable
public GUI getGUI(Player player) {
for (GUI gui : guis) {
for (GUI gui : getTrackedGUIs()) {
if (gui.getInventory().getViewers().contains(player)) {
return gui;
}
Expand All @@ -93,7 +108,7 @@ public GUI getGUI(Player player) {
* @return Whether the player has a GUI open.
*/
public boolean hasGUI(Player player) {
for (GUI gui : guis) {
for (GUI gui : getTrackedGUIs()) {
if (gui.getInventory().getViewers().contains(player)) {
return true;
}
Expand All @@ -107,7 +122,7 @@ public boolean hasGUI(Player player) {
*/
@Nullable
public GUI getGUI(String id) {
for (GUI gui : guis) {
for (GUI gui : getTrackedGUIs()) {
if (id.equals(gui.getID())) {
return gui;
}
Expand All @@ -121,12 +136,7 @@ public GUI getGUI(String id) {
*/
@Nullable
public GUI getGUI(Inventory inventory) {
for (GUI gui : guis) {
if (gui.getInventory().equals(inventory)) {
return gui;
}
}
return null;
return guis.get(inventory);
}

}
}

0 comments on commit ddb3a7f

Please sign in to comment.