108 lines
3.9 KiB
Java
108 lines
3.9 KiB
Java
package dev.zontreck.ase;
|
|
|
|
import java.util.List;
|
|
|
|
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.PrimitiveAction;
|
|
import dev.zontreck.ase.guis.PrimitiveItem;
|
|
import io.papermc.paper.event.player.AsyncChatEvent;
|
|
import net.kyori.adventure.text.Component;
|
|
import net.kyori.adventure.text.event.HoverEvent;
|
|
import net.kyori.adventure.text.format.NamedTextColor;
|
|
import net.md_5.bungee.api.ChatColor;
|
|
|
|
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 onChat(AsyncChatEvent ev) {
|
|
Player sender = ev.getPlayer();
|
|
Component message = ev.message();
|
|
|
|
// Get player's saved data
|
|
SavedData playerData = AriasServerEssentials.getPlayerData(sender.getUniqueId().toString());
|
|
String nickname = playerData.profile.nickname;
|
|
String prefix = playerData.profile.prefix;
|
|
|
|
// Fallbacks
|
|
if (nickname == null || nickname.isEmpty()) {
|
|
nickname = sender.getName();
|
|
}
|
|
|
|
if (prefix == null) {
|
|
prefix = "";
|
|
}
|
|
|
|
if (!prefix.isBlank()) {
|
|
prefix = ChatColor.YELLOW + "[" + ChatColor.RESET + prefix + ChatColor.YELLOW + "] ";
|
|
}
|
|
|
|
// Build the chat format: [prefix] [nickname/username]: message
|
|
// TODO: Implement a GUI color picker for the prefix color, and nickname
|
|
// colorizer.
|
|
Component prefixComponent = Component.text(prefix, NamedTextColor.GRAY);
|
|
Component nameComponent = Component.text(nickname, NamedTextColor.AQUA);
|
|
Component colonComponent = Component.text(": ", NamedTextColor.GRAY);
|
|
|
|
Component finalMessage = Component.empty()
|
|
.append(prefixComponent)
|
|
.append(nameComponent)
|
|
.append(colonComponent)
|
|
.append(message)
|
|
.hoverEvent(HoverEvent.showText(Component.text("Username: " + sender.getName())));
|
|
|
|
ev.setCancelled(true); // Cancel the original message
|
|
ev.viewers().forEach(viewer -> viewer.sendMessage(finalMessage));
|
|
}
|
|
|
|
@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);
|
|
|
|
if (ice.isShiftClick()) {
|
|
List<PrimitiveAction> acts = PrimitiveAction.getSingleAction(ice.getCurrentItem(),
|
|
"shiftclickaction");
|
|
for (PrimitiveAction primitiveAction : acts) {
|
|
primitiveAction.handleAction(player);
|
|
}
|
|
}
|
|
// player.closeInventory();
|
|
|
|
List<PrimitiveAction> actions = PrimitiveAction.getActions(ice.getCurrentItem());
|
|
for (PrimitiveAction primitiveAction : actions) {
|
|
primitiveAction.handleAction(player);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|