Skip to content

Commit c890414

Browse files
committed
Fixed GUIs not being played after Player reconnection
1 parent 5250153 commit c890414

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

src/main/java/com/sentropic/guiapi/GUIManager.java

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.sentropic.guiapi;
22

33
import com.sentropic.guiapi.gui.GUI;
4+
import org.bukkit.Bukkit;
45
import org.bukkit.entity.Player;
56
import org.bukkit.event.EventHandler;
67
import org.bukkit.event.Listener;
8+
import org.bukkit.event.player.PlayerJoinEvent;
79
import org.bukkit.event.player.PlayerQuitEvent;
810
import org.bukkit.metadata.FixedMetadataValue;
911
import org.bukkit.scheduler.BukkitRunnable;
@@ -24,7 +26,10 @@ public class GUIManager implements Listener {
2426
/**
2527
* For internal use only. Use {@link GUIAPI#getGUIManager()} to get the instance used by the plugin
2628
*/
27-
public GUIManager() { task.runTaskTimer(GUIAPI.getPlugin(), 0, 1); }
29+
public GUIManager() {
30+
task.runTaskTimer(GUIAPI.getPlugin(), 0, 1);
31+
Bukkit.getServer().getOnlinePlayers().forEach(this::createGUI);
32+
}
2833

2934
/**
3035
* For internal use only. Used to clear the stored {@link GUI}s from memory and cancel GUI updating
@@ -43,22 +48,34 @@ void disable() {
4348
* Gets the {@link GUI} of a given {@link Player}
4449
*
4550
* @param player the {@link Player} to get the {@link GUI} for
46-
* @return the existing {@link GUI} of the {@link Player}, or a new one if nonexistent
51+
* @return the existing {@link GUI} of the {@link Player}
52+
* @throws IllegalStateException if the given player is offline
4753
*/
4854
public GUI getGUI(Player player) {
49-
GUI gui;
50-
if (player.hasMetadata(METADATA_KEY)) {
51-
gui = (GUI) player.getMetadata(METADATA_KEY).get(0).value();
55+
if (player.isOnline()) {
56+
return (GUI) player.getMetadata(METADATA_KEY).get(0).value();
5257
} else {
53-
gui = new GUI(player);
54-
GUIS.add(gui);
55-
player.setMetadata(METADATA_KEY, new FixedMetadataValue(GUIAPI.getPlugin(), gui));
58+
throw new IllegalStateException();
5659
}
57-
return gui;
5860
}
5961

6062
@EventHandler
61-
public void onPlayerLeave(PlayerQuitEvent event) { GUIS.remove(this.getGUI(event.getPlayer())); }
63+
public void onPlayerLeave(PlayerQuitEvent event) {
64+
Player player = event.getPlayer();
65+
GUIS.remove(this.getGUI(player));
66+
player.removeMetadata(METADATA_KEY, GUIAPI.getPlugin());
67+
}
68+
69+
@EventHandler
70+
public void onPlayerJoin(PlayerJoinEvent event) {
71+
createGUI(event.getPlayer());
72+
}
73+
74+
private void createGUI(Player player) {
75+
GUI gui = new GUI(player);
76+
player.setMetadata(METADATA_KEY, new FixedMetadataValue(GUIAPI.getPlugin(), gui));
77+
GUIS.add(gui);
78+
}
6279

6380
private class Task extends BukkitRunnable {
6481
@Override

0 commit comments

Comments
 (0)