Skip to content

Commit bdbc465

Browse files
committed
Fix anvil transaction logging (fixes #616)
1 parent 6d3df41 commit bdbc465

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ Desktop.ini
173173
#############
174174

175175
.cursorrules
176+
.windsurfrules
176177

177178
# Compiled class file
178179
*.class

src/main/java/net/coreprotect/listener/player/InventoryChangeListener.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020
import org.bukkit.event.inventory.InventoryClickEvent;
2121
import org.bukkit.event.inventory.InventoryDragEvent;
2222
import org.bukkit.event.inventory.InventoryMoveItemEvent;
23+
import org.bukkit.event.inventory.InventoryType;
2324
import org.bukkit.inventory.BlockInventoryHolder;
2425
import org.bukkit.inventory.Inventory;
2526
import org.bukkit.inventory.InventoryHolder;
2627
import org.bukkit.inventory.ItemStack;
28+
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
2729

2830
import net.coreprotect.CoreProtect;
2931
import net.coreprotect.config.Config;
@@ -268,12 +270,69 @@ static void onInventoryInteractAsync(Player player, Inventory inventory, boolean
268270
});
269271
}
270272

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+
271325
@EventHandler(priority = EventPriority.LOWEST)
272326
protected void onInventoryClick(InventoryClickEvent event) {
273327
InventoryAction inventoryAction = event.getAction();
274328
if (inventoryAction == InventoryAction.NOTHING) {
275329
return;
276330
}
331+
332+
// Check if this is an anvil operation first
333+
if (checkAnvilOperation(event)) {
334+
return;
335+
}
277336

278337
boolean enderChest = false;
279338
boolean advancedChest;

0 commit comments

Comments
 (0)