organize core package, add gui instance protection

This commit is contained in:
jascotty2 2019-08-28 08:02:05 -05:00
parent 02de13eed2
commit d5eb450f5c
10 changed files with 51 additions and 28 deletions

View File

@ -1,5 +1,10 @@
package com.songoda.core;
import com.songoda.core.core.PluginInfo;
import com.songoda.core.core.LocaleModule;
import com.songoda.core.core.PluginInfoModule;
import com.songoda.core.core.SongodaCoreCommand;
import com.songoda.core.core.SongodaCoreDiagCommand;
import com.songoda.core.commands.CommandManager;
import com.songoda.core.compatibility.LegacyMaterials;
import com.songoda.core.gui.GuiManager;
@ -37,7 +42,6 @@ public class SongodaCore {
private static SongodaCore INSTANCE = null;
private JavaPlugin piggybackedPlugin;
protected GuiManager guiManager;
private final CommandManager commandManager;
private final EventListener loginListener = new EventListener();
private final HashMap<UUID, Long> lastCheck = new HashMap();
@ -177,9 +181,6 @@ public class SongodaCore {
}
if(event.getPlugin() == piggybackedPlugin) {
// uh-oh! Abandon ship!!
if(guiManager != null) {
guiManager.closeAll();
}
Bukkit.getServicesManager().unregisterAll(piggybackedPlugin);
// can we move somewhere else?
if((pi = registeredPlugins.stream().findFirst().orElse(null)) != null) {
@ -188,7 +189,6 @@ public class SongodaCore {
Bukkit.getServicesManager().register(SongodaCore.class, INSTANCE, piggybackedPlugin, ServicePriority.Normal);
Bukkit.getPluginManager().registerEvents(loginListener, piggybackedPlugin);
CommandManager.registerCommandDynamically(piggybackedPlugin, "songoda", commandManager, commandManager);
guiManager = null;
}
}
}

View File

@ -1,5 +1,6 @@
package com.songoda.core;
package com.songoda.core.core;
import com.songoda.core.core.PluginInfoModule;
import com.songoda.core.locale.Locale;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

View File

@ -1,5 +1,6 @@
package com.songoda.core;
package com.songoda.core.core;
import com.songoda.core.core.PluginInfoModule;
import com.songoda.core.compatibility.LegacyMaterials;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.simple.JSONObject;
@ -22,7 +23,7 @@ public final class PluginInfo {
private String marketplaceLink;
private JSONObject json;
protected PluginInfo(JavaPlugin javaPlugin, int songodaId, String icon) {
public PluginInfo(JavaPlugin javaPlugin, int songodaId, String icon) {
this.javaPlugin = javaPlugin;
this.songodaId = songodaId;
this.coreIcon = icon;

View File

@ -1,4 +1,4 @@
package com.songoda.core;
package com.songoda.core.core;
public interface PluginInfoModule {

View File

@ -1,15 +1,18 @@
package com.songoda.core;
package com.songoda.core.core;
import com.songoda.core.SongodaCore;
import com.songoda.core.commands.AbstractCommand;
import com.songoda.core.gui.GuiManager;
import java.util.List;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
class SongodaCoreCommand extends AbstractCommand {
public class SongodaCoreCommand extends AbstractCommand {
final SongodaCore instance;
protected SongodaCoreCommand(SongodaCore instance) {
protected GuiManager guiManager;
public SongodaCoreCommand(SongodaCore instance) {
super(false, "songoda");
this.instance = instance;
}
@ -17,10 +20,10 @@ class SongodaCoreCommand extends AbstractCommand {
@Override
protected ReturnType runCommand(CommandSender sender, String... args) {
if(sender instanceof Player) {
if(instance.guiManager == null) {
instance.guiManager = new GuiManager(SongodaCore.getHijackedPlugin());
if(guiManager == null || guiManager.isClosed()) {
guiManager = new GuiManager(SongodaCore.getHijackedPlugin());
}
instance.guiManager.showGUI((Player) sender, new SongodaCoreOverviewGUI(instance));
guiManager.showGUI((Player) sender, new SongodaCoreOverviewGUI(instance));
} else {
sender.sendMessage("/songoda diag");
}

View File

@ -1,5 +1,6 @@
package com.songoda.core;
package com.songoda.core.core;
import com.songoda.core.SongodaCore;
import com.songoda.core.commands.AbstractCommand;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
@ -21,7 +22,7 @@ public class SongodaCoreDiagCommand extends AbstractCommand {
private Field tpsField;
protected SongodaCoreDiagCommand(SongodaCore instance) {
public SongodaCoreDiagCommand(SongodaCore instance) {
super(false, "diag");
this.instance = instance;

View File

@ -1,5 +1,6 @@
package com.songoda.core;
package com.songoda.core.core;
import com.songoda.core.SongodaCore;
import com.songoda.core.compatibility.LegacyMaterials;
import com.songoda.core.gui.Gui;
import com.songoda.core.gui.GuiUtils;

View File

@ -513,21 +513,21 @@ public class Gui {
}
}
protected Inventory getOrCreateInventory() {
return inventory != null ? inventory : generateInventory();
protected Inventory getOrCreateInventory(GuiManager manager) {
return inventory != null ? inventory : generateInventory(manager);
}
protected Inventory generateInventory() {
protected Inventory generateInventory(GuiManager manager) {
final int cells = rows * 9;
InventoryType t = inventoryType == null ? InventoryType.CHEST : inventoryType.type;
switch (t) {
case DISPENSER:
case HOPPER:
inventory = Bukkit.getServer().createInventory(new GuiHolder(this), t,
inventory = Bukkit.getServer().createInventory(new GuiHolder(manager, this), t,
title == null ? "" : trimTitle(ChatColor.translateAlternateColorCodes('&', title)));
break;
default:
inventory = Bukkit.getServer().createInventory(new GuiHolder(this), cells,
inventory = Bukkit.getServer().createInventory(new GuiHolder(manager, this), cells,
title == null ? "" : trimTitle(ChatColor.translateAlternateColorCodes('&', title)));
}

View File

@ -1,5 +1,6 @@
package com.songoda.core.gui;
import com.sun.istack.internal.NotNull;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
@ -12,9 +13,11 @@ import org.bukkit.inventory.InventoryHolder;
class GuiHolder implements InventoryHolder {
final Gui gui;
final GuiManager manager;
GuiHolder(Gui gui) {
public GuiHolder(@NotNull GuiManager manager, @NotNull Gui gui) {
this.gui = gui;
this.manager = manager;
}
@Override

View File

@ -3,6 +3,7 @@ package com.songoda.core.gui;
import com.songoda.core.compatibility.CompatibleSounds;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
@ -26,6 +27,7 @@ import org.bukkit.plugin.Plugin;
public class GuiManager {
final Plugin plugin;
final UUID uuid = UUID.randomUUID(); // manager tracking to fix weird bugs from lazy programming
final GuiListener listener = new GuiListener(this);
final Map<Player, Inventory> openInventories = new HashMap();
private boolean initialized = false;
@ -44,6 +46,15 @@ public class GuiManager {
shutdown = false;
}
/**
* Check to see if this manager cannot open any more GUI screens
*
* @return true if the owning plugin has shutdown
*/
public boolean isClosed() {
return shutdown;
}
/**
* Create and display a GUI interface for a player
*
@ -56,7 +67,7 @@ public class GuiManager {
} else if (!initialized) {
init();
}
Inventory inv = gui.generateInventory();
Inventory inv = gui.generateInventory(this);
player.openInventory(inv);
gui.onOpen(this, player);
openInventories.put(player, inv);
@ -89,7 +100,8 @@ public class GuiManager {
Inventory openInv = event.getInventory();
final Player player = (Player) event.getWhoClicked();
Gui gui;
if (openInv.getHolder() != null && openInv.getHolder() instanceof GuiHolder) {
if (openInv.getHolder() != null && openInv.getHolder() instanceof GuiHolder
&& ((GuiHolder) openInv.getHolder()).manager.uuid.equals(manager.uuid)) {
gui = ((GuiHolder) openInv.getHolder()).getGUI();
if (event.getSlotType() == SlotType.OUTSIDE) {
@ -118,9 +130,10 @@ public class GuiManager {
@EventHandler(priority = EventPriority.LOW)
void onCloseGUI(InventoryCloseEvent event) {
Inventory openInv = event.getInventory();
if (openInv.getHolder() != null && openInv.getHolder() instanceof GuiHolder) {
final Player player = (Player) event.getPlayer();
if (openInv.getHolder() != null && openInv.getHolder() instanceof GuiHolder
&& ((GuiHolder) openInv.getHolder()).manager.uuid.equals(manager.uuid)) {
Gui gui = ((GuiHolder) openInv.getHolder()).getGUI();
final Player player = (Player) event.getPlayer();
if (!gui.allowDropItems) {
player.setItemOnCursor(null);
}