Search in sources :

Example 1 with ContainerAnvilHandle

use of com.bergerkiller.generated.net.minecraft.world.inventory.ContainerAnvilHandle in project BKCommonLib by bergerhealer.

the class MapWidgetAnvil method setWindowOpen.

private void setWindowOpen(boolean enabled) {
    if (this._isWindowOpen == enabled) {
        return;
    }
    this._isWindowOpen = enabled;
    // Close all old views first (to be sure), both when enabling and disabling
    for (InventoryView view : this._openInventories) {
        view.close();
    }
    this._openInventories.clear();
    if (enabled) {
        // Start listening
        JavaPlugin plugin = this.getDisplay().getPlugin();
        Bukkit.getPluginManager().registerEvents(this._listener, plugin);
        // Reset text
        this._text = "";
        // Open windows for all viewing players
        for (Player player : (_openFor.isEmpty() ? this.display.getViewers() : _openFor)) {
            if (_openFor.isEmpty() && !this.display.isControlling(player)) {
                continue;
            }
            final InventoryView view = EntityPlayerHandle.fromBukkit(player).openAnvilWindow(null);
            // Required for handling text changes < MC 1.9
            if (!CommonCapabilities.HAS_PREPARE_ANVIL_EVENT) {
                ContainerAnvilHandle container = ContainerAnvilHandle.fromBukkit(view);
                LegacyContainerAnvilHook hook = ClassHook.get(container.getRaw(), LegacyContainerAnvilHook.class);
                hook.textChangeCallback = () -> handleTextChange(view);
            }
            this._openInventories.add(view);
            this.refreshButtons(view);
        }
        // If it couldn't be opened for anyone, close itself
        if (this._openInventories.isEmpty()) {
            setWindowOpen(false);
        }
    } else {
        // Unregister event listener
        CommonUtil.unregisterListener(this._listener);
        // Deactivate the widget (to be sure)
        deactivate();
        // Event
        onClose();
    }
}
Also used : InventoryView(org.bukkit.inventory.InventoryView) Player(org.bukkit.entity.Player) LegacyContainerAnvilHook(com.bergerkiller.bukkit.common.internal.hooks.LegacyContainerAnvilHook) JavaPlugin(org.bukkit.plugin.java.JavaPlugin) ContainerAnvilHandle(com.bergerkiller.generated.net.minecraft.world.inventory.ContainerAnvilHandle)

Aggregations

LegacyContainerAnvilHook (com.bergerkiller.bukkit.common.internal.hooks.LegacyContainerAnvilHook)1 ContainerAnvilHandle (com.bergerkiller.generated.net.minecraft.world.inventory.ContainerAnvilHandle)1 Player (org.bukkit.entity.Player)1 InventoryView (org.bukkit.inventory.InventoryView)1 JavaPlugin (org.bukkit.plugin.java.JavaPlugin)1