Fixed books placed in lecterns not being logged (fixes #761)

This commit is contained in:
Intelli committed 2026-04-24 09:31:16 -06:00
1 parent aa2a3b9e8e
commit 829bcc530d
2 files changed
+56 -3

No files matched your search

@@ -25,7 +25,6 @@ import org.bukkit.inventory.ItemStack;
import net.coreprotect.bukkit.BukkitAdapter; import net.coreprotect.bukkit.BukkitAdapter;
import net.coreprotect.config.Config; import net.coreprotect.config.Config;
import net.coreprotect.consumer.Queue; import net.coreprotect.consumer.Queue;
import net.coreprotect.listener.player.InventoryChangeListener;
import net.coreprotect.model.BlockGroup; import net.coreprotect.model.BlockGroup;
import net.coreprotect.paper.PaperAdapter; import net.coreprotect.paper.PaperAdapter;
import net.coreprotect.utility.BlockUtils; import net.coreprotect.utility.BlockUtils;
@@ -48,8 +47,7 @@ public final class BlockPlaceListener extends Queue implements Listener {
boolean abort = false; boolean abort = false;
if (blockType == Material.LECTERN && blockReplaced.getType() == Material.LECTERN) { if (blockType == Material.LECTERN && blockReplaced.getType() == Material.LECTERN) {
// Placing a book in a lectern - log this as a new item being placed in the existing lectern // Logged by PlayerInteractListener; prevent a false lectern block-place record.
InventoryChangeListener.inventoryTransaction(player.getName(), blockLogged.getLocation(), new ItemStack[1]);
abort = true; abort = true;
} }
else if (MaterialUtils.listContains(BlockGroup.CONTAINERS, blockType) || MaterialUtils.listContains(BlockGroup.DIRECTIONAL_BLOCKS, blockType) || blockType.name().toUpperCase(Locale.ROOT).endsWith("_STAIRS")) { else if (MaterialUtils.listContains(BlockGroup.CONTAINERS, blockType) || MaterialUtils.listContains(BlockGroup.DIRECTIONAL_BLOCKS, blockType) || blockType.name().toUpperCase(Locale.ROOT).endsWith("_STAIRS")) {
@@ -72,6 +72,47 @@ public final class PlayerInteractListener extends Queue implements Listener {
private final ContainerInspector containerInspector = new ContainerInspector(); private final ContainerInspector containerInspector = new ContainerInspector();
private final InteractionInspector interactionInspector = new InteractionInspector(); private final InteractionInspector interactionInspector = new InteractionInspector();
private static boolean containsItem(ItemStack itemStack) {
return itemStack != null && itemStack.getType() != Material.AIR;
}
private static ItemStack getHandItem(Player player, EquipmentSlot hand) {
if (hand == EquipmentSlot.HAND) {
return player.getInventory().getItemInMainHand();
}
else if (hand == EquipmentSlot.OFF_HAND) {
return player.getInventory().getItemInOffHand();
}
return null;
}
private static boolean isLecternBook(ItemStack itemStack) {
if (!containsItem(itemStack)) {
return false;
}
Material type = itemStack.getType();
return type == Material.WRITABLE_BOOK || type == Material.WRITTEN_BOOK;
}
private static boolean containsLecternBook(ItemStack[] contents) {
return contents != null && contents.length > 0 && isLecternBook(contents[0]);
}
private static void logLecternBookPlacement(Player player, Block block, ItemStack[] oldContents) {
Location location = block.getLocation();
Scheduler.scheduleSyncDelayedTask(CoreProtect.getInstance(), () -> {
BlockState blockState = location.getBlock().getState();
if (blockState.getType() == Material.LECTERN && blockState instanceof InventoryHolder) {
InventoryHolder inventoryHolder = (InventoryHolder) blockState;
if (containsLecternBook(inventoryHolder.getInventory().getContents())) {
InventoryChangeListener.inventoryTransaction(player.getName(), location, oldContents);
}
}
}, location, 1);
}
@EventHandler(priority = EventPriority.LOWEST) @EventHandler(priority = EventPriority.LOWEST)
protected void onPlayerInspect(PlayerInteractEvent event) { protected void onPlayerInspect(PlayerInteractEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
@@ -430,6 +471,20 @@ public final class PlayerInteractListener extends Queue implements Listener {
} }
} }
} }
else if (type == Material.LECTERN && Config.getConfig(world).ITEM_TRANSACTIONS && event.useItemInHand() != Event.Result.DENY) {
BlockState blockState = block.getState();
if (blockState instanceof InventoryHolder) {
InventoryHolder inventoryHolder = (InventoryHolder) blockState;
ItemStack[] oldContents = ItemUtils.getContainerState(inventoryHolder.getInventory().getContents());
ItemStack handItem = getHandItem(player, event.getHand());
boolean oldContainsBook = containsLecternBook(oldContents);
boolean handIsBook = isLecternBook(handItem);
if (!oldContainsBook && handIsBook) {
logLecternBookPlacement(player, block, oldContents);
}
}
}
else if (BukkitAdapter.ADAPTER.isChiseledBookshelf(type)) { else if (BukkitAdapter.ADAPTER.isChiseledBookshelf(type)) {
BlockState blockState = block.getState(); BlockState blockState = block.getState();
if (blockState instanceof BlockInventoryHolder) { if (blockState instanceof BlockInventoryHolder) {