import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.player.PlayerTakeLecternBookEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;

public class LecternGUI {

    private static final Wrapper1_16_R3 WRAPPER = new Wrapper1_16_R3();

    private final Plugin plugin;
    private final Player player;
    private final ItemStack bookItem;
    private final ListenUp listener = new ListenUp();

    private Inventory inventory;
    private int containerId;
    private boolean open;

    LecternGUI(Plugin plugin, Player player, ItemStack bookItem, int page) {
        this.plugin = plugin;
        this.player = player;
        this.bookItem = bookItem;

        openInventory(page);
    }

    private void openInventory(int page) {
        WRAPPER.handleInventoryCloseEvent(player);
        WRAPPER.setActiveContainerDefault(player);

        Bukkit.getPluginManager().registerEvents(listener, plugin);

        final Object container = WRAPPER.newContainerLectern(player, bookItem);

        inventory = WRAPPER.toBukkitInventory(container);

        containerId = WRAPPER.getNextContainerId(container);
        WRAPPER.sendPacketOpenWindow(player, containerId, "container.lectern");
        WRAPPER.setActiveContainer(player, container);
        WRAPPER.addActiveContainerSlotListener(container, player);
        WRAPPER.setPage(container, page);
        open = true;
    }

    public void closeInventory() {
        if (!open) {
            return;
        }

        open = false;

        WRAPPER.handleInventoryCloseEvent(player);
        WRAPPER.setActiveContainerDefault(player);
        WRAPPER.sendPacketCloseWindow(player, containerId);

        HandlerList.unregisterAll(listener);
    }

    private class ListenUp implements Listener {
        @EventHandler
        public void onInventoryClose(InventoryCloseEvent event) {
            if (open && event.getInventory().equals(inventory))
                closeInventory();
        }

        @EventHandler
        public void onPlayerTakeLecternBook(PlayerTakeLecternBookEvent event) {
            if (open && event.getPlayer().getUniqueId().equals(player.getUniqueId()))
                event.setCancelled(true);
        }
    }
}

Add a code snippet to your website: www.paste.org