|
20 | 20 | import org.bukkit.event.inventory.InventoryClickEvent; |
21 | 21 | import org.bukkit.event.inventory.InventoryDragEvent; |
22 | 22 | import org.bukkit.event.inventory.InventoryMoveItemEvent; |
| 23 | +import org.bukkit.event.inventory.InventoryType; |
23 | 24 | import org.bukkit.inventory.BlockInventoryHolder; |
24 | 25 | import org.bukkit.inventory.Inventory; |
25 | 26 | import org.bukkit.inventory.InventoryHolder; |
26 | 27 | import org.bukkit.inventory.ItemStack; |
| 28 | +import org.bukkit.inventory.meta.EnchantmentStorageMeta; |
27 | 29 |
|
28 | 30 | import net.coreprotect.CoreProtect; |
29 | 31 | import net.coreprotect.config.Config; |
@@ -268,12 +270,69 @@ static void onInventoryInteractAsync(Player player, Inventory inventory, boolean |
268 | 270 | }); |
269 | 271 | } |
270 | 272 |
|
| 273 | + /** |
| 274 | + * Checks for anvil operations to properly track enchanted item results |
| 275 | + * @param event The inventory click event |
| 276 | + * @return true if this was an anvil result operation that was handled, false otherwise |
| 277 | + */ |
| 278 | + private boolean checkAnvilOperation(InventoryClickEvent event) { |
| 279 | + if (event.getInventory().getType() != InventoryType.ANVIL) { |
| 280 | + return false; |
| 281 | + } |
| 282 | + |
| 283 | + // Only process result slot clicks in anvils (slot 2) |
| 284 | + if (event.getRawSlot() != 2) { |
| 285 | + return false; |
| 286 | + } |
| 287 | + |
| 288 | + // Ensure we have a valid player and item |
| 289 | + Player player = (Player) event.getWhoClicked(); |
| 290 | + ItemStack resultItem = event.getCurrentItem(); |
| 291 | + if (resultItem == null || resultItem.getType() == Material.AIR) { |
| 292 | + return false; |
| 293 | + } |
| 294 | + |
| 295 | + // Get the input items (slots 0 and 1 in the anvil) |
| 296 | + ItemStack firstItem = event.getInventory().getItem(0); |
| 297 | + ItemStack secondItem = event.getInventory().getItem(1); |
| 298 | + |
| 299 | + if (firstItem == null || secondItem == null) { |
| 300 | + return false; |
| 301 | + } |
| 302 | + |
| 303 | + // Process the enchantment operation |
| 304 | + Location location = player.getLocation(); |
| 305 | + String loggingItemId = player.getName().toLowerCase(Locale.ROOT) + "." + location.getBlockX() + "." + location.getBlockY() + "." + location.getBlockZ(); |
| 306 | + int itemId = getItemId(loggingItemId); |
| 307 | + |
| 308 | + // Log the input items as removed |
| 309 | + List<ItemStack> removedItems = new ArrayList<>(); |
| 310 | + removedItems.add(firstItem.clone()); |
| 311 | + removedItems.add(secondItem.clone()); |
| 312 | + ConfigHandler.itemsDestroy.put(loggingItemId, removedItems); |
| 313 | + |
| 314 | + // Log the output item as created |
| 315 | + List<ItemStack> createdItems = new ArrayList<>(); |
| 316 | + createdItems.add(resultItem.clone()); |
| 317 | + ConfigHandler.itemsCreate.put(loggingItemId, createdItems); |
| 318 | + |
| 319 | + int time = (int) (System.currentTimeMillis() / 1000L) + 1; |
| 320 | + Queue.queueItemTransaction(player.getName(), location.clone(), time, 0, itemId); |
| 321 | + |
| 322 | + return true; |
| 323 | + } |
| 324 | + |
271 | 325 | @EventHandler(priority = EventPriority.LOWEST) |
272 | 326 | protected void onInventoryClick(InventoryClickEvent event) { |
273 | 327 | InventoryAction inventoryAction = event.getAction(); |
274 | 328 | if (inventoryAction == InventoryAction.NOTHING) { |
275 | 329 | return; |
276 | 330 | } |
| 331 | + |
| 332 | + // Check if this is an anvil operation first |
| 333 | + if (checkAnvilOperation(event)) { |
| 334 | + return; |
| 335 | + } |
277 | 336 |
|
278 | 337 | boolean enderChest = false; |
279 | 338 | boolean advancedChest; |
|
0 commit comments