|
| 1 | +/* |
| 2 | + * Comments generated using 0xAlpha AI Comment Generator v1.4.1 |
| 3 | + * Copyright (c) 2025 by 0xAlpha. All rights reserved. |
| 4 | + * This software is provided "as-is", without warranty of any kind, express or implied. |
| 5 | + */ |
| 6 | +package io.greitan.avion.listeners; |
| 7 | + |
| 8 | +import org.bukkit.Bukkit; |
| 9 | +import org.bukkit.Location; |
| 10 | +import org.bukkit.Material; |
| 11 | +import org.bukkit.NamespacedKey; |
| 12 | +import org.bukkit.configuration.file.YamlConfiguration; |
| 13 | +import org.bukkit.entity.Player; |
| 14 | +import org.bukkit.event.EventHandler; |
| 15 | +import org.bukkit.event.Listener; |
| 16 | +import org.bukkit.event.inventory.InventoryClickEvent; |
| 17 | +import org.bukkit.event.inventory.InventoryType; |
| 18 | +import org.bukkit.inventory.Inventory; |
| 19 | +import org.bukkit.inventory.ItemStack; |
| 20 | +import org.bukkit.persistence.PersistentDataType; |
| 21 | + |
| 22 | +import de.oliver.fancynpcs.api.FancyNpcsPlugin; |
| 23 | +import de.oliver.fancynpcs.api.Npc; |
| 24 | +import de.oliver.fancynpcs.api.events.NpcInteractEvent; |
| 25 | +import de.oliver.fancynpcs.api.events.NpcRemoveEvent; |
| 26 | +import de.oliver.fancynpcs.api.events.NpcStopLookingEvent; |
| 27 | +import io.greitan.avion.PlayerCorpses; |
| 28 | +import io.greitan.avion.utils.LocaleManager; |
| 29 | +import io.greitan.avion.utils.Logger; |
| 30 | +import io.greitan.avion.utils.MenuBuilder; |
| 31 | +import io.greitan.avion.utils.YamlBase; |
| 32 | +import net.kyori.adventure.text.Component; |
| 33 | + |
| 34 | +import java.util.stream.IntStream; |
| 35 | + |
| 36 | +public class NpcInteractListener implements Listener { |
| 37 | + private final LocaleManager localeManager; |
| 38 | + private final Component MENU_TITLE; |
| 39 | + |
| 40 | + /** |
| 41 | + * Constructor initializes the listener with a locale manager. |
| 42 | + * |
| 43 | + * @param localeManager Locale manager for handling messages. |
| 44 | + */ |
| 45 | + public NpcInteractListener(LocaleManager localeManager) { |
| 46 | + this.localeManager = localeManager; |
| 47 | + this.MENU_TITLE = PlayerCorpses.getMM().deserialize(localeManager.getMessage("menu.title")); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Handles NPC interaction, shows the menu when a player interacts with an NPC. |
| 52 | + * |
| 53 | + * @param e The NPC interaction event. |
| 54 | + */ |
| 55 | + @EventHandler |
| 56 | + public void npcInteract(NpcInteractEvent e) { |
| 57 | + showMenu(e.getPlayer(), e.getNpc()); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Handles inventory click events in the menu. |
| 62 | + * |
| 63 | + * @param event The inventory click event. |
| 64 | + */ |
| 65 | + @EventHandler |
| 66 | + public void onInventoryClick(InventoryClickEvent event) { |
| 67 | + if (!(event.getWhoClicked() instanceof Player player)) |
| 68 | + return; |
| 69 | + if (event.getInventory().getType() != InventoryType.CHEST) |
| 70 | + return; |
| 71 | + if (!event.getView().title().equals(MENU_TITLE)) |
| 72 | + return; |
| 73 | + |
| 74 | + event.setCancelled(true); |
| 75 | + |
| 76 | + ItemStack clickedItem = event.getCurrentItem(); |
| 77 | + if (clickedItem == null || clickedItem.getType() == Material.AIR) |
| 78 | + return; |
| 79 | + |
| 80 | + // Handle actions based on the clicked item |
| 81 | + String action = switch (clickedItem.getType()) { |
| 82 | + case ARROW -> "break"; |
| 83 | + case BARRIER -> "back"; |
| 84 | + default -> null; |
| 85 | + }; |
| 86 | + |
| 87 | + if (action != null) { |
| 88 | + player.closeInventory(); |
| 89 | + handleAction(player, action, clickedItem); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Executes the appropriate action based on the clicked item. |
| 95 | + * |
| 96 | + * @param player The player who clicked the item. |
| 97 | + * @param action The action to perform. |
| 98 | + * @param clickedItem The item clicked. |
| 99 | + */ |
| 100 | + private void handleAction(Player player, String action, ItemStack clickedItem) { |
| 101 | + NamespacedKey key = new NamespacedKey(PlayerCorpses.getInstance(), "corpse"); |
| 102 | + String npcID = clickedItem.getItemMeta().getPersistentDataContainer().get(key, PersistentDataType.STRING); |
| 103 | + switch (action) { |
| 104 | + case "break" -> { |
| 105 | + if (npcID != null) |
| 106 | + breakCorpse(player, npcID); |
| 107 | + player.sendMessage(PlayerCorpses.getMM().deserialize(localeManager.getMessage("menu.action.break"))); |
| 108 | + } |
| 109 | + case "back" -> { |
| 110 | + player.sendMessage(PlayerCorpses.getMM().deserialize(localeManager.getMessage("menu.action.back"))); |
| 111 | + } |
| 112 | + default -> { |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Breaks the corpse NPC and handles the drop of items from the corpse. |
| 119 | + * |
| 120 | + * @param player The player interacting with the NPC. |
| 121 | + * @param npcID The NPC ID. |
| 122 | + */ |
| 123 | + private void breakCorpse(Player player, String npcID) { |
| 124 | + Npc npc = FancyNpcsPlugin.get().getNpcManager().getNpcById(npcID); |
| 125 | + String containerId = npc.getData().getName(); |
| 126 | + YamlConfiguration containerData; |
| 127 | + |
| 128 | + try { |
| 129 | + containerData = YamlBase.loadPlayerData(player.getName(), containerId); |
| 130 | + } catch (IllegalStateException e) { |
| 131 | + Logger.error(e); |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + Location dropLocation = npc.getData().getLocation(); |
| 136 | + |
| 137 | + // Remove NPC and handle its drops |
| 138 | + if (new NpcRemoveEvent(npc, Bukkit.getServer().getConsoleSender()).callEvent()) { |
| 139 | + npc.removeForAll(); |
| 140 | + for (Player onlinePlayer : Bukkit.getOnlinePlayers()) { |
| 141 | + if (npc.getIsLookingAtPlayer().getOrDefault(onlinePlayer.getUniqueId(), false)) { |
| 142 | + npc.getIsLookingAtPlayer().put(onlinePlayer.getUniqueId(), false); |
| 143 | + new NpcStopLookingEvent(npc, onlinePlayer).callEvent(); |
| 144 | + } |
| 145 | + } |
| 146 | + FancyNpcsPlugin.get().getNpcManager().removeNpc(npc); |
| 147 | + |
| 148 | + // Drop items if any exist |
| 149 | + if (containerData.contains("inventory")) { |
| 150 | + try { |
| 151 | + ItemStack[] inventoryContents = YamlBase |
| 152 | + .itemStackArrayFromBase64(containerData.getString("inventory")); |
| 153 | + for (ItemStack item : inventoryContents) { |
| 154 | + if (item != null) { |
| 155 | + dropLocation.getWorld().dropItemNaturally(dropLocation, item); |
| 156 | + } |
| 157 | + } |
| 158 | + } catch (IllegalStateException e) { |
| 159 | + Logger.error(e); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + YamlBase.deletePlayerData(player.getName(), containerId); |
| 164 | + } else { |
| 165 | + player.sendMessage(PlayerCorpses.getMM().deserialize(localeManager.getMessage("corpse.break_error"))); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Shows the interaction menu for a player and NPC. |
| 171 | + * |
| 172 | + * @param player The player to show the menu to. |
| 173 | + * @param npc The NPC the player is interacting with. |
| 174 | + */ |
| 175 | + private void showMenu(Player player, Npc npc) { |
| 176 | + Inventory inv = Bukkit.createInventory(null, 27, MENU_TITLE); |
| 177 | + fillBorders(inv); |
| 178 | + inv.setItem(11, |
| 179 | + MenuBuilder.createItem(Material.BARRIER, localeManager.getMessage("menu.button.back"), |
| 180 | + null, npc.getData().getId())); |
| 181 | + inv.setItem(15, |
| 182 | + MenuBuilder.createItem(Material.ARROW, localeManager.getMessage("menu.button.break"), |
| 183 | + null, npc.getData().getId())); |
| 184 | + player.openInventory(inv); |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Fills the borders of the inventory with decorative items. |
| 189 | + * |
| 190 | + * @param inv The inventory to fill. |
| 191 | + */ |
| 192 | + private void fillBorders(Inventory inv) { |
| 193 | + IntStream.range(0, 27) |
| 194 | + .filter(i -> i != 11 && i != 15) |
| 195 | + .forEach(i -> inv.setItem(i, |
| 196 | + MenuBuilder.createItem(Material.LIGHT_BLUE_STAINED_GLASS_PANE, "", null, null))); |
| 197 | + } |
| 198 | +} |
0 commit comments