Get credits GUI to function properly

This commit is contained in:
zontreck 2025-04-05 23:43:51 -07:00
parent 28ec395978
commit 4acb0baa7a
8 changed files with 129 additions and 29 deletions

View file

@ -16,7 +16,7 @@ static def getTime() {
} }
// Set version to version property if supplied // Set version to version property if supplied
String shortVersion = "v1.0" String shortVersion = "1.0"
if (hasProperty('ver')) { if (hasProperty('ver')) {
if (ver.charAt(0) == "v") { if (ver.charAt(0) == "v") {
shortVersion = ver.substring(1).toUpperCase() shortVersion = ver.substring(1).toUpperCase()

View file

@ -9,13 +9,11 @@ import java.util.Map;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.plugin.PluginManager;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitScheduler;
import com.google.common.eventbus.Subscribe; import dev.zontreck.ase.guis.ChestGUI;
import dev.zontreck.ase.utils.TPACaches; import dev.zontreck.ase.utils.TPACaches;
import io.papermc.lib.PaperLib; import io.papermc.lib.PaperLib;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
@ -23,7 +21,7 @@ import net.kyori.adventure.text.format.NamedTextColor;
public class AriasServerEssentials extends JavaPlugin implements Listener { public class AriasServerEssentials extends JavaPlugin implements Listener {
private static JavaPlugin plugin; private static JavaPlugin plugin;
private static Map<String, SavedData> cachedPlayerData = new HashMap<>(); protected static Map<String, SavedData> cachedPlayerData = new HashMap<>();
private static List<TPACaches> cacheTPA = new ArrayList<>(); private static List<TPACaches> cacheTPA = new ArrayList<>();
public static boolean disabled = false; public static boolean disabled = false;
@ -42,6 +40,9 @@ public class AriasServerEssentials extends JavaPlugin implements Listener {
if (disabled) if (disabled)
task.cancel(); task.cancel();
}, 0, 20); }, 0, 20);
PluginManager pm = getServer().getPluginManager();
pm.registerEvents(new EventsHandler(), this);
} }
@Override @Override
@ -66,21 +67,6 @@ public class AriasServerEssentials extends JavaPlugin implements Listener {
} }
} }
@Subscribe
public static void onPlayerJoin(PlayerJoinEvent ev) {
getPlayerData(ev.getPlayer().getUniqueId().toString()); // Pre-cache it
}
@Subscribe
public static void onPlayerLeave(PlayerQuitEvent ev) {
// Save the player's data and remove from object cache
String id = ev.getPlayer().getUniqueId().toString();
SavedData data = getPlayerData(id);
data.save(id);
cachedPlayerData.remove(id);
}
public static void onTick() { public static void onTick() {
for (TPACaches cache : cacheTPA) { for (TPACaches cache : cacheTPA) {
cache.tick(); cache.tick();
@ -97,6 +83,8 @@ public class AriasServerEssentials extends JavaPlugin implements Listener {
it.remove(); it.remove();
} }
} }
ChestGUI.onTick();
} }
public static boolean hasTPARequest(Player from) { public static boolean hasTPARequest(Player from) {

View file

@ -0,0 +1,44 @@
package dev.zontreck.ase;
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.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import dev.zontreck.ase.guis.ChestGUI;
import dev.zontreck.ase.guis.PrimitiveItem;
public class EventsHandler implements Listener {
@EventHandler
public static void onPlayerJoin(PlayerJoinEvent ev) {
AriasServerEssentials.getPlayerData(ev.getPlayer().getUniqueId().toString()); // Pre-cache it
}
@EventHandler
public static void onPlayerLeave(PlayerQuitEvent ev) {
// Save the player's data and remove from object cache
String id = ev.getPlayer().getUniqueId().toString();
SavedData data = AriasServerEssentials.getPlayerData(id);
data.save(id);
AriasServerEssentials.cachedPlayerData.remove(id);
}
@EventHandler
public static void onPlayerInteractGUI(InventoryClickEvent ice) {
// Get the player who clicked
Player player = (Player) ice.getWhoClicked();
// Check if the current inventory is a valid ChestGUI
ChestGUI gui = ChestGUI.getGUIForPlayer(player);
if (gui != null) {
// Check if clicked item is a GUI button
if (PrimitiveItem.isGUIButton(ice.getCurrentItem())) {
ice.setCancelled(true);
}
}
}
}

View file

@ -1,5 +1,7 @@
package dev.zontreck.ase.guis; package dev.zontreck.ase.guis;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -8,6 +10,7 @@ import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView; import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import dev.zontreck.ase.AriasServerEssentials;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
public class ChestGUI { public class ChestGUI {
@ -19,10 +22,14 @@ public class ChestGUI {
} }
public static void checkValidity() { public static void checkValidity() {
for (int i = 0; i < GUI_LIST.size(); i++) { Iterator<ChestGUI> it = GUI_LIST.iterator();
ChestGUI gui = GUI_LIST.get(i);
while (it.hasNext()) {
ChestGUI gui = it.next();
if (!gui.valid()) { if (!gui.valid()) {
GUI_LIST.remove(gui);
AriasServerEssentials.getSelf().getLogger().info("A gui has been invalidated, removing from caches");
it.remove();
} }
} }
} }
@ -33,12 +40,18 @@ public class ChestGUI {
return gui; return gui;
} }
} }
AriasServerEssentials.getSelf().getLogger().info("No such GUI, returning null");
return null; return null;
} }
public static void onTick() {
checkValidity();
}
// END STATIC REGISTRY // END STATIC REGISTRY
public String guiTitle; public String guiTitle;
public List<ItemStack> buttons; public List<ItemStack> buttons = new ArrayList<>();
public int totalSize; public int totalSize;
public Player player; public Player player;
public boolean closed = false; public boolean closed = false;
@ -58,6 +71,10 @@ public class ChestGUI {
} }
} }
public void add(PrimitiveItem item) {
buttons.add(item.generate());
}
/** /**
* This method presents the GUI to the player, using the defined layout above. * This method presents the GUI to the player, using the defined layout above.
*/ */

View file

@ -4,16 +4,30 @@ import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
public class CreditsGui { public class CreditsGui {
public static void openGUI(Player player) { public static void openGUI(Player player) {
// First step, construct the layout // First step, construct the layout
ChestGUI gui = new ChestGUI("Credits", 9 * 4); ChestGUI gui = new ChestGUI("Credits", 9 * 3);
gui.fill(9, new PrimitiveItem(Material.BLACK_STAINED_GLASS_PANE, Component.text(""), true, PrimitiveItem StainedGlass = new PrimitiveItem(Material.BLACK_STAINED_GLASS_PANE, Component.text(""), true,
Component.text().build())); Component.text().build());
PrimitiveItem zontreckHead = new PrimitiveItem(Material.PLAYER_HEAD,
Component.text("Aria", NamedTextColor.DARK_RED), true,
Component.text("Username: zontreck", NamedTextColor.DARK_PURPLE),
Component.text("Creator of ASE", NamedTextColor.DARK_PURPLE));
zontreckHead.skull("zontreck");
gui.fill(9, StainedGlass);
gui.add(StainedGlass);
gui.add(zontreckHead);
gui.fill(9 - 2, StainedGlass);
gui.fill(9, StainedGlass);
// Second step, present to the player // Second step, present to the player
gui.setPlayer(player);
gui.doPresentation(); gui.doPresentation();
} }
} }

View file

@ -3,10 +3,12 @@ package dev.zontreck.ase.guis;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.NamespacedKey; import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.persistence.PersistentDataContainer; import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType; import org.bukkit.persistence.PersistentDataType;
@ -22,6 +24,8 @@ public class PrimitiveItem {
public Component name; public Component name;
public Component[] itemLore; public Component[] itemLore;
public boolean isGUIButton; public boolean isGUIButton;
public boolean isSkull = false;
public String skullOwner = "";
public PrimitiveItem(Material type, Component name, boolean isGUIButton, Component... lore) { public PrimitiveItem(Material type, Component name, boolean isGUIButton, Component... lore) {
itemType = type; itemType = type;
@ -57,6 +61,12 @@ public class PrimitiveItem {
true); true);
} }
if (isSkull) {
SkullMeta skullMeta = (SkullMeta) meta;
skullMeta.setOwningPlayer(Bukkit.getOfflinePlayer(skullOwner));
meta = skullMeta;
}
item.setItemMeta(meta); item.setItemMeta(meta);
return item; return item;
} }
@ -74,4 +84,10 @@ public class PrimitiveItem {
PersistentDataType.BOOLEAN); PersistentDataType.BOOLEAN);
} }
public void skull(String string) {
isSkull = true;
skullOwner = string;
itemType = Material.PLAYER_HEAD;
}
} }

View file

@ -36,6 +36,9 @@ commands:
shareitem: shareitem:
description: Shares the item in your hand with everyone in chat description: Shares the item in your hand with everyone in chat
usage: "- /shareitem -" usage: "- /shareitem -"
asecredits:
description: Displays the plugin's credits
usage: "- /asecredits -"
permissions: permissions:
ase.commands.*: ase.commands.*:
description: Allow all commands description: Allow all commands
@ -51,6 +54,7 @@ permissions:
ase.commands.tpacancel: true ase.commands.tpacancel: true
ase.commands.tpahere: true ase.commands.tpahere: true
ase.commands.shareitem: true ase.commands.shareitem: true
ase.commands.asecredits: true
ase.commands.home: ase.commands.home:
description: Allows usage of the /home command description: Allows usage of the /home command
default: true default: true
@ -81,3 +85,6 @@ permissions:
ase.commands.shareitem: ase.commands.shareitem:
description: Allow /shareitem description: Allow /shareitem
default: true default: true
ase.commands.asecredits:
description: Allow /asecredits
default: true

View file

@ -32,6 +32,12 @@ commands:
tpahere: tpahere:
description: Request a player to teleport to you description: Request a player to teleport to you
usage: "- /tpahere [player] -" usage: "- /tpahere [player] -"
shareitem:
description: Shares the item in your hand with everyone in chat
usage: "- /shareitem -"
asecredits:
description: Displays the plugin's credits
usage: "- /asecredits -"
permissions: permissions:
ase.commands.*: ase.commands.*:
description: Allow all commands description: Allow all commands
@ -46,6 +52,8 @@ permissions:
ase.commands.tpdeny: true ase.commands.tpdeny: true
ase.commands.tpacancel: true ase.commands.tpacancel: true
ase.commands.tpahere: true ase.commands.tpahere: true
ase.commands.shareitem: true
ase.commands.asecredits: true
ase.commands.home: ase.commands.home:
description: Allows usage of the /home command description: Allows usage of the /home command
default: true default: true
@ -73,3 +81,9 @@ permissions:
ase.commands.tpahere: ase.commands.tpahere:
description: Allow usage of /tpahere description: Allow usage of /tpahere
default: true default: true
ase.commands.shareitem:
description: Allow /shareitem
default: true
ase.commands.asecredits:
description: Allow /asecredits
default: true