Search in sources :

Example 6 with InventoryType

use of org.bukkit.event.inventory.InventoryType in project Denizen-For-Bukkit by DenizenScript.

the class InventoryTag method valueOf.

@Fetchable("in")
public static InventoryTag valueOf(String string, TagContext context) {
    if (string == null) {
        return null;
    }
    if (string.startsWith("in@")) {
        string = string.substring("in@".length());
    }
    List<String> properties = ObjectFetcher.separateProperties(string);
    if (properties != null && properties.size() > 1) {
        InventoryTag result = internalGetInventoryFor(context, properties);
        if (result == null) {
            if (context == null || context.showErrors()) {
                Debug.echoError("Value of InventoryTag returning null. Invalid InventoryTag-with-properties specified: " + string);
            }
            return null;
        }
        trackTemporaryInventory(result);
        return result;
    }
    Notable noted = NoteManager.getSavedObject(string);
    if (noted instanceof InventoryTag) {
        return (InventoryTag) noted;
    }
    if (ScriptRegistry.containsScript(string, InventoryScriptContainer.class)) {
        return ScriptRegistry.getScriptContainerAs(string, InventoryScriptContainer.class).getInventoryFrom(context);
    }
    if (new ElementTag(string).matchesEnum(InventoryType.class)) {
        InventoryType type = InventoryType.valueOf(string.toUpperCase());
        return new InventoryTag(type);
    }
    if (context == null || context.showErrors()) {
        Debug.echoError("Value of InventoryTag returning null. Invalid InventoryTag specified: " + string);
    }
    return null;
}
Also used : InventoryScriptContainer(com.denizenscript.denizen.scripts.containers.core.InventoryScriptContainer) InventoryType(org.bukkit.event.inventory.InventoryType) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) Notable(com.denizenscript.denizencore.objects.notable.Notable)

Example 7 with InventoryType

use of org.bukkit.event.inventory.InventoryType in project Essentials by drtshock.

the class EssentialsPlayerListener method onInventoryClickEvent.

@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onInventoryClickEvent(final InventoryClickEvent event) {
    Player refreshPlayer = null;
    final Inventory top = event.getView().getTopInventory();
    final InventoryType type = top.getType();
    final Inventory clickedInventory;
    if (event.getRawSlot() < 0) {
        clickedInventory = null;
    } else {
        clickedInventory = event.getRawSlot() < top.getSize() ? top : event.getView().getBottomInventory();
    }
    if (type == InventoryType.PLAYER) {
        final User user = ess.getUser((Player) event.getWhoClicked());
        final InventoryHolder invHolder = top.getHolder();
        if (invHolder != null && invHolder instanceof HumanEntity) {
            final User invOwner = ess.getUser((Player) invHolder);
            if (user.isInvSee() && (!user.isAuthorized("essentials.invsee.modify") || invOwner.isAuthorized("essentials.invsee.preventmodify") || !invOwner.getBase().isOnline())) {
                event.setCancelled(true);
                refreshPlayer = user.getBase();
            }
        }
    } else if (type == InventoryType.ENDER_CHEST) {
        final User user = ess.getUser((Player) event.getWhoClicked());
        if (user.isEnderSee() && (!user.isAuthorized("essentials.enderchest.modify"))) {
            event.setCancelled(true);
            refreshPlayer = user.getBase();
        }
    } else if (type == InventoryType.WORKBENCH) {
        User user = ess.getUser((Player) event.getWhoClicked());
        if (user.isRecipeSee()) {
            event.setCancelled(true);
            refreshPlayer = user.getBase();
        }
    } else if (type == InventoryType.CHEST && top.getSize() == 9) {
        final User user = ess.getUser((Player) event.getWhoClicked());
        final InventoryHolder invHolder = top.getHolder();
        if (invHolder != null && invHolder instanceof HumanEntity && user.isInvSee()) {
            event.setCancelled(true);
            refreshPlayer = user.getBase();
        }
    } else if (clickedInventory != null && clickedInventory.getType() == InventoryType.PLAYER) {
        if (ess.getSettings().isDirectHatAllowed() && event.getClick() == ClickType.LEFT && event.getSlot() == 39 && event.getCursor().getType() != Material.AIR && event.getCursor().getType().getMaxDurability() == 0 && ess.getUser(event.getWhoClicked()).isAuthorized("essentials.hat")) {
            event.setCancelled(true);
            final PlayerInventory inv = (PlayerInventory) clickedInventory;
            final ItemStack head = inv.getHelmet();
            inv.setHelmet(event.getCursor());
            event.setCursor(head);
        }
    }
    if (refreshPlayer != null) {
        final Player player = refreshPlayer;
        ess.scheduleSyncDelayedTask(new Runnable() {

            @Override
            public void run() {
                player.updateInventory();
            }
        }, 1);
    }
}
Also used : Player(org.bukkit.entity.Player) InventoryType(org.bukkit.event.inventory.InventoryType) HumanEntity(org.bukkit.entity.HumanEntity) PlayerInventory(org.bukkit.inventory.PlayerInventory) ItemStack(org.bukkit.inventory.ItemStack) InventoryHolder(org.bukkit.inventory.InventoryHolder) Inventory(org.bukkit.inventory.Inventory) PlayerInventory(org.bukkit.inventory.PlayerInventory) EventHandler(org.bukkit.event.EventHandler)

Example 8 with InventoryType

use of org.bukkit.event.inventory.InventoryType in project MagicPlugin by elBukkit.

the class Wand method activate.

public boolean activate(Mage mage, boolean offhand) {
    if (mage == null)
        return false;
    Player player = mage.getPlayer();
    if (player == null)
        return false;
    if (!controller.hasWandPermission(player, this))
        return false;
    InventoryView openInventory = player.getOpenInventory();
    InventoryType inventoryType = openInventory.getType();
    if (inventoryType == InventoryType.ENCHANTING || inventoryType == InventoryType.ANVIL)
        return false;
    if (hasUses && uses <= 0) {
        if (offhand) {
            player.getInventory().setItemInOffHand(new ItemStack(Material.AIR, 1));
        } else {
            player.getInventory().setItemInMainHand(new ItemStack(Material.AIR, 1));
        }
        return false;
    }
    if (!canUse(player)) {
        mage.sendMessage(getMessage("bound").replace("$name", getOwner()));
        return false;
    }
    if (this.isUpgrade) {
        controller.getLogger().warning("Activated an upgrade item- this shouldn't happen");
        return false;
    }
    WandPreActivateEvent preActivateEvent = new WandPreActivateEvent(mage, this);
    Bukkit.getPluginManager().callEvent(preActivateEvent);
    if (preActivateEvent.isCancelled()) {
        return false;
    }
    boolean needsSave = false;
    if (hasId) {
        needsSave = this.checkId() || needsSave;
    } else {
        setProperty("id", null);
    }
    this.mage = mage;
    this.isInOffhand = offhand;
    this.heldSlot = offhand ? OFFHAND_SLOT : player.getInventory().getHeldItemSlot();
    if (mageClassKeys != null && !mageClassKeys.isEmpty()) {
        MageClass mageClass = null;
        for (String mageClassKey : mageClassKeys) {
            mageClass = mage.getClass(mageClassKey);
            if (mageClass != null)
                break;
        }
        if (mageClass == null) {
            Integer lastSlot = mage.getLastActivatedSlot();
            if (!offhand && (lastSlot == null || lastSlot != player.getInventory().getHeldItemSlot())) {
                mage.setLastActivatedSlot(player.getInventory().getHeldItemSlot());
                mage.sendMessage(controller.getMessages().get("mage.no_class").replace("$name", getName()));
            }
            return false;
        }
        setMageClass(mageClass);
        if (!offhand) {
            mage.setActiveClass(mageClass.getKey());
        }
        // This double-load here is not really ideal.
        // Seems hard to prevent without merging Wand construction and activation, though.
        loadProperties();
    }
    mage.setLastActivatedSlot(player.getInventory().getHeldItemSlot());
    // Check for replacement template
    String replacementTemplate = getString("replace_on_activate", "");
    if (!replacementTemplate.isEmpty() && !replacementTemplate.equals(template)) {
        playEffects("replace");
        setTemplate(replacementTemplate);
        loadProperties();
        saveState();
        return activate(mage, offhand);
    }
    // We have to delay this 1 tick so it happens after the Mage has accepted the Wand
    if (getMode() != WandMode.INVENTORY || offhand) {
        Plugin plugin = controller.getPlugin();
        plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {

            @Override
            public void run() {
                showActiveIcon(true);
                playPassiveEffects("open");
            }
        }, 1);
    }
    // Check for an empty wand and auto-fill
    if (!isUpgrade && (controller.fillWands() || autoFill)) {
        fill(mage.getPlayer(), controller.getMaxWandFillLevel());
        needsSave = true;
    }
    if (isHeroes) {
        HeroesManager heroes = controller.getHeroes();
        if (heroes != null) {
            Set<String> skills = heroes.getSkills(player);
            Collection<String> currentSpells = new ArrayList<>(getSpells());
            for (String spellKey : currentSpells) {
                if (spellKey.startsWith("heroes*") && !skills.contains(spellKey.substring(7))) {
                    removeSpell(spellKey);
                }
            }
            // Hack to prevent messaging
            this.mage = null;
            for (String skillKey : skills) {
                String heroesKey = "heroes*" + skillKey;
                if (!spells.contains(heroesKey)) {
                    addSpell(heroesKey);
                }
            }
            this.mage = mage;
        }
    }
    // Check for auto-organize
    if (autoOrganize && !isUpgrade) {
        organizeInventory(mage);
        needsSave = true;
    }
    // Check for auto-alphabetize
    if (autoAlphabetize && !isUpgrade) {
        alphabetizeInventory();
        needsSave = true;
    }
    boolean forceUpdate = false;
    if (checkInventoryForUpgrades()) {
        forceUpdate = true;
        needsSave = true;
    }
    // Check for auto-bind
    if (bound) {
        String mageName = ChatColor.stripColor(mage.getPlayer().getDisplayName());
        String mageId = mage.getId();
        boolean ownerRenamed = owner != null && ownerId != null && ownerId.equals(mageId) && !owner.equals(mageName);
        if (ownerId == null || ownerId.length() == 0 || owner == null || ownerRenamed) {
            takeOwnership(mage.getPlayer());
            needsSave = true;
        }
    }
    // Check for randomized wands
    if (randomizeOnActivate) {
        randomize();
        randomizeOnActivate = false;
        forceUpdate = true;
        needsSave = true;
    }
    // Don't build the inventory until activated so we can take Mage boosts into account
    if (offhand) {
        mage.setOffhandWand(this);
    } else {
        mage.setActiveWand(this);
    }
    buildInventory();
    updateMaxMana(false);
    tick();
    if (!isInOffhand) {
        updateMana();
    }
    checkActiveMaterial();
    if (needsSave) {
        saveState();
    }
    updateActiveMaterial();
    updateName();
    updateLore();
    // Play activate FX
    playPassiveEffects("activate");
    lastSoundEffect = 0;
    lastParticleEffect = 0;
    lastSpellCast = 0;
    if (forceUpdate) {
        DeprecatedUtils.updateInventory(player);
    }
    return true;
}
Also used : Player(org.bukkit.entity.Player) MageClass(com.elmakers.mine.bukkit.magic.MageClass) InventoryType(org.bukkit.event.inventory.InventoryType) ArrayList(java.util.ArrayList) WandPreActivateEvent(com.elmakers.mine.bukkit.api.event.WandPreActivateEvent) HeroesManager(com.elmakers.mine.bukkit.heroes.HeroesManager) InventoryView(org.bukkit.inventory.InventoryView) ItemStack(org.bukkit.inventory.ItemStack) Plugin(org.bukkit.plugin.Plugin)

Example 9 with InventoryType

use of org.bukkit.event.inventory.InventoryType in project MagicPlugin by elBukkit.

the class InventoryController method onInventoryClick.

@SuppressWarnings("deprecation")
@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
    if (event.isCancelled())
        return;
    if (!(event.getWhoClicked() instanceof Player))
        return;
    Player player = (Player) event.getWhoClicked();
    final Mage mage = controller.getMage(player);
    GUIAction gui = mage.getActiveGUI();
    if (gui != null) {
        gui.clicked(event);
        return;
    }
    // Check for temporary items and skill items
    InventoryAction action = event.getAction();
    InventoryType inventoryType = event.getInventory().getType();
    boolean isPlayerInventory = inventoryType == InventoryType.CRAFTING || inventoryType == InventoryType.PLAYER;
    ItemStack clickedItem = event.getCurrentItem();
    boolean isDrop = event.getClick() == ClickType.DROP || event.getClick() == ClickType.CONTROL_DROP;
    boolean isSkill = clickedItem != null && Wand.isSkill(clickedItem);
    // Check for right-click-to-use
    boolean isRightClick = action == InventoryAction.PICKUP_HALF;
    if (isSkill && isRightClick) {
        mage.useSkill(clickedItem);
        player.closeInventory();
        event.setCancelled(true);
        return;
    }
    if (clickedItem != null && NMSUtils.isTemporary(clickedItem)) {
        String message = NMSUtils.getTemporaryMessage(clickedItem);
        if (message != null && message.length() > 1) {
            mage.sendMessage(message);
        }
        ItemStack replacement = NMSUtils.getReplacement(clickedItem);
        event.setCurrentItem(replacement);
        event.setCancelled(true);
        return;
    }
    // Check for wearing spells
    ItemStack heldItem = event.getCursor();
    boolean heldSpell = Wand.isSpell(heldItem);
    if (event.getSlotType() == InventoryType.SlotType.ARMOR) {
        if (heldSpell) {
            event.setCancelled(true);
            return;
        }
        if (Wand.isWand(clickedItem) || Wand.isWand(heldItem)) {
            controller.onArmorUpdated(mage);
        }
    }
    boolean isHotbar = event.getAction() == InventoryAction.HOTBAR_SWAP || event.getAction() == InventoryAction.HOTBAR_MOVE_AND_READD;
    if (isHotbar && event.getSlotType() == InventoryType.SlotType.ARMOR) {
        int slot = event.getHotbarButton();
        ItemStack item = mage.getPlayer().getInventory().getItem(slot);
        if (item != null && Wand.isSpell(item)) {
            event.setCancelled(true);
            return;
        }
        controller.onArmorUpdated(mage);
    }
    Wand activeWand = mage.getActiveWand();
    boolean isWandInventoryOpen = activeWand != null && activeWand.isInventoryOpen();
    // Preventing putting skills in containers
    if (isSkill && !isPlayerInventory && !isWandInventoryOpen) {
        if (!isDrop) {
            event.setCancelled(true);
        }
        return;
    }
    boolean isFurnace = inventoryType == InventoryType.FURNACE;
    boolean isChest = inventoryType == InventoryType.CHEST || inventoryType == InventoryType.HOPPER || inventoryType == InventoryType.DISPENSER || inventoryType == InventoryType.DROPPER;
    // TODO: use enum when dropping backwards compat
    if (!isChest)
        isChest = inventoryType.name().equals("SHULKER_BOX");
    boolean clickedWand = Wand.isWand(clickedItem);
    boolean isContainerSlot = event.getSlot() == event.getRawSlot();
    if (isWandInventoryOpen) {
        // Don't allow the offhand slot to be messed with while the spell inventory is open
        if (event.getRawSlot() == 45) {
            event.setCancelled(true);
            return;
        }
        // Don't allow putting spells in a crafting slot
        if (event.getSlotType() == InventoryType.SlotType.CRAFTING && heldSpell) {
            event.setCancelled(true);
            return;
        }
        if (Wand.isSpell(clickedItem) && clickedItem.getAmount() != 1) {
            clickedItem.setAmount(1);
        }
        if (clickedWand) {
            event.setCancelled(true);
            if (dropChangesPages || isRightClick) {
                activeWand.cycleInventory();
            } else {
                activeWand.cycleHotbar(1);
                // There doesn't seem to be any other way to allow cycling in creative
                if (inventoryType == InventoryType.PLAYER) {
                    activeWand.cycleInventory();
                }
            }
            return;
        }
        // So many ways to try and move the wand around, that we have to watch for!
        if (isHotbar && Wand.isWand(player.getInventory().getItem(event.getHotbarButton()))) {
            event.setCancelled(true);
            return;
        }
        // Can't wear spells
        if (event.getAction() == InventoryAction.MOVE_TO_OTHER_INVENTORY && clickedItem != null) {
            if (controller.isWearable(clickedItem)) {
                event.setCancelled(true);
                return;
            }
        }
        // but perhaps happens with lag?
        if (Wand.isWand(event.getCursor())) {
            activeWand.closeInventory();
            event.setCursor(null);
            event.setCancelled(true);
            return;
        }
    } else if (activeWand != null) {
        // Check for changes that could have been made to the active wand
        int activeSlot = player.getInventory().getHeldItemSlot();
        if (event.getSlot() == activeSlot || (isHotbar && event.getHotbarButton() == activeSlot)) {
            mage.checkWand();
            activeWand = mage.getActiveWand();
        }
    }
    // Don't allow smelting wands
    if (isFurnace && clickedWand) {
        event.setCancelled(true);
        return;
    }
    if (isFurnace && isHotbar) {
        ItemStack destinationItem = player.getInventory().getItem(event.getHotbarButton());
        if (Wand.isWand(destinationItem)) {
            event.setCancelled(true);
            return;
        }
    }
    if (InventoryUtils.getMetaBoolean(clickedItem, "unmoveable", false)) {
        event.setCancelled(true);
        return;
    }
    if (isHotbar) {
        ItemStack destinationItem = player.getInventory().getItem(event.getHotbarButton());
        if (InventoryUtils.getMetaBoolean(destinationItem, "unmoveable", false)) {
            event.setCancelled(true);
            return;
        }
        if (isChest && InventoryUtils.getMetaBoolean(destinationItem, "unstashable", false) && !player.hasPermission("Magic.wand.override_stash")) {
            event.setCancelled(true);
            return;
        }
    }
    // Check for unstashable wands
    if (isChest && !isContainerSlot && !player.hasPermission("Magic.wand.override_stash")) {
        if (InventoryUtils.getMetaBoolean(clickedItem, "unstashable", false)) {
            event.setCancelled(true);
            return;
        }
    }
    // Check for taking bound wands out of chests
    if (isChest && isContainerSlot && Wand.isBound(clickedItem)) {
        Wand testWand = controller.getWand(clickedItem);
        if (!testWand.canUse(player)) {
            event.setCancelled(true);
            return;
        }
    }
    // Check for armor changing
    if (event.getAction() == InventoryAction.MOVE_TO_OTHER_INVENTORY && clickedItem != null) {
        if (controller.isWearable(clickedItem)) {
            controller.onArmorUpdated(mage);
        }
    }
    // or dropping undroppable wands
    if (isDrop) {
        if (InventoryUtils.getMetaBoolean(clickedItem, "undroppable", false)) {
            event.setCancelled(true);
            if (activeWand != null) {
                if (activeWand.getHotbarCount() > 1) {
                    activeWand.cycleHotbar(1);
                } else {
                    activeWand.closeInventory();
                }
            }
            return;
        }
        if (activeWand != null && activeWand.isInventoryOpen()) {
            ItemStack droppedItem = clickedItem;
            if (!Wand.isSpell(droppedItem)) {
                mage.giveItem(droppedItem);
                event.setCurrentItem(null);
                event.setCancelled(true);
                return;
            }
            // This is a hack to deal with spells on cooldown disappearing,
            // Since the event handler doesn't match the zero-count itemstacks
            Integer slot = event.getSlot();
            int heldSlot = player.getInventory().getHeldItemSlot();
            Inventory hotbar = activeWand.getHotbar();
            if (hotbar != null && slot >= 0 && slot <= hotbar.getSize() && slot != heldSlot && activeWand.getMode() == WandMode.INVENTORY) {
                if (slot > heldSlot)
                    slot--;
                if (slot < hotbar.getSize()) {
                    droppedItem = hotbar.getItem(slot);
                } else {
                    slot = null;
                }
            } else {
                slot = null;
            }
            if (!controller.isSpellDroppingEnabled()) {
                player.closeInventory();
                String spellName = Wand.getSpell(droppedItem);
                if (spellName != null && !activeWand.isManualQuickCastDisabled() && enableInventoryCasting) {
                    Spell spell = mage.getSpell(spellName);
                    if (spell != null) {
                        activeWand.cast(spell);
                        // Just in case a spell has levelled up... jeez!
                        if (hotbar != null && slot != null) {
                            droppedItem = hotbar.getItem(slot);
                        }
                    }
                }
                event.setCancelled(true);
                // This is needed to avoid spells on cooldown disappearing from the hotbar
                if (hotbar != null && slot != null && mage.getActiveGUI() == null) {
                    player.getInventory().setItem(event.getSlot(), droppedItem);
                    DeprecatedUtils.updateInventory(player);
                }
                return;
            }
            ItemStack newDrop = controller.removeItemFromWand(activeWand, droppedItem);
            if (newDrop != null) {
                Location location = player.getLocation();
                Item item = location.getWorld().dropItem(location, newDrop);
                SafetyUtils.setVelocity(item, location.getDirection().normalize());
            } else {
                event.setCancelled(true);
            }
        }
        return;
    }
    // Check for wand cycling with active inventory
    if (activeWand != null) {
        WandMode wandMode = activeWand.getMode();
        boolean isChestInventory = wandMode == WandMode.CHEST || wandMode == WandMode.SKILLS;
        if ((wandMode == WandMode.INVENTORY && isPlayerInventory) || (isChestInventory && inventoryType == InventoryType.CHEST)) {
            if (activeWand.isInventoryOpen()) {
                if (event.getAction() == InventoryAction.NOTHING) {
                    int direction = event.getClick() == ClickType.LEFT ? 1 : -1;
                    activeWand.cycleInventory(direction);
                    event.setCancelled(true);
                    return;
                }
                if (event.getSlotType() == InventoryType.SlotType.ARMOR) {
                    event.setCancelled(true);
                    return;
                }
                // Chest mode falls back to selection from here.
                boolean isInventoryQuickSelect = isRightClick && wandMode == WandMode.INVENTORY && enableInventorySelection;
                if (isInventoryQuickSelect || wandMode == WandMode.CHEST) {
                    player.closeInventory();
                    mage.activateIcon(activeWand, clickedItem);
                    event.setCancelled(true);
                }
                // Prevent clicking any non-skill item when a skill inventory is open
                if (wandMode == WandMode.SKILLS && clickedItem != null && clickedItem.getType() != Material.AIR && !Wand.isSkill(clickedItem)) {
                    event.setCancelled(true);
                }
            }
        }
    }
}
Also used : Player(org.bukkit.entity.Player) InventoryType(org.bukkit.event.inventory.InventoryType) GUIAction(com.elmakers.mine.bukkit.api.action.GUIAction) Wand(com.elmakers.mine.bukkit.wand.Wand) Spell(com.elmakers.mine.bukkit.api.spell.Spell) InventoryAction(org.bukkit.event.inventory.InventoryAction) Item(org.bukkit.entity.Item) Mage(com.elmakers.mine.bukkit.magic.Mage) ItemStack(org.bukkit.inventory.ItemStack) CraftingInventory(org.bukkit.inventory.CraftingInventory) Inventory(org.bukkit.inventory.Inventory) WandMode(com.elmakers.mine.bukkit.wand.WandMode) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Example 10 with InventoryType

use of org.bukkit.event.inventory.InventoryType in project MagicPlugin by elBukkit.

the class AnvilController method onInventoryClick.

@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
    if (event.isCancelled())
        return;
    if (!(event.getWhoClicked() instanceof Player))
        return;
    InventoryType inventoryType = event.getInventory().getType();
    SlotType slotType = event.getSlotType();
    Player player = (Player) event.getWhoClicked();
    Mage mage = controller.getMage(player);
    if (inventoryType == InventoryType.ANVIL) {
        ItemStack cursor = event.getCursor();
        ItemStack current = event.getCurrentItem();
        Inventory anvilInventory = event.getInventory();
        InventoryAction action = event.getAction();
        ItemStack firstItem = anvilInventory.getItem(0);
        ItemStack secondItem = anvilInventory.getItem(1);
        // Handle direct movement
        if (action == InventoryAction.MOVE_TO_OTHER_INVENTORY) {
            if (!Wand.isWand(current))
                return;
            // Moving from anvil back to inventory
            if (slotType == SlotType.CRAFTING) {
                Wand wand = controller.getWand(current);
                wand.updateName(true);
            } else if (slotType == SlotType.RESULT) {
                // Don't allow combining
                if (!combiningEnabled) {
                    if (firstItem != null && secondItem != null) {
                        event.setCancelled(true);
                        return;
                    }
                }
                // Taking from result slot
                ItemMeta meta = current.getItemMeta();
                String newName = meta.getDisplayName();
                Wand wand = controller.getWand(current);
                if (!wand.canUse(player)) {
                    event.setCancelled(true);
                    mage.sendMessage(controller.getMessages().get("wand.bound").replace("$name", wand.getOwner()));
                    return;
                }
                wand.setName(newName);
                if (organizingEnabled) {
                    wand.organizeInventory(controller.getMage(player));
                }
                wand.tryToOwn(player);
            } else {
                // Moving from inventory to anvil
                Wand wand = controller.getWand(current);
                wand.updateName(false);
            }
            return;
        }
        // Set/unset active names when starting to craft
        if (slotType == SlotType.CRAFTING) {
            // Putting a wand into the anvil's crafting slot
            if (Wand.isWand(cursor)) {
                Wand wand = controller.getWand(cursor);
                wand.updateName(false);
            }
            // Taking a wand out of the anvil's crafting slot
            if (Wand.isWand(current)) {
                Wand wand = controller.getWand(current);
                if (clearDescriptionOnRename) {
                    wand.setDescription("");
                }
                wand.updateName(true);
                if (event.getWhoClicked() instanceof Player) {
                    wand.tryToOwn((Player) event.getWhoClicked());
                }
            }
            return;
        }
        // Rename wand when taking from result slot
        if (slotType == SlotType.RESULT && Wand.isWand(current)) {
            if (!combiningEnabled) {
                if (firstItem != null && secondItem != null) {
                    event.setCancelled(true);
                    return;
                }
            }
            ItemMeta meta = current.getItemMeta();
            String newName = meta.getDisplayName();
            Wand wand = controller.getWand(current);
            if (!wand.canUse(player)) {
                event.setCancelled(true);
                mage.sendMessage(controller.getMessages().get("wand.bound").replace("$name", wand.getOwner()));
                return;
            }
            wand.setName(newName);
            if (organizingEnabled) {
                wand.organizeInventory(controller.getMage(player));
            }
            wand.tryToOwn(player);
            return;
        }
        if (combiningEnabled && slotType == SlotType.RESULT) {
            // I guess I need to wait for the long-awaited anvil API?
            if (Wand.isWand(firstItem) && Wand.isWand(secondItem)) {
                Wand firstWand = controller.getWand(firstItem);
                Wand secondWand = controller.getWand(secondItem);
                if (!firstWand.isModifiable() || !secondWand.isModifiable()) {
                    mage.sendMessage("One of your wands can not be combined");
                    return;
                }
                if (!firstWand.canUse(player) || !secondWand.canUse(player)) {
                    mage.sendMessage("One of those wands is not bound to you");
                    return;
                }
                if (!firstWand.add(secondWand)) {
                    mage.sendMessage("These wands can not be combined with each other");
                    return;
                }
                anvilInventory.setItem(0, null);
                anvilInventory.setItem(1, null);
                cursor.setType(Material.AIR);
                if (organizingEnabled) {
                    firstWand.organizeInventory(mage);
                }
                firstWand.tryToOwn(player);
                player.getInventory().addItem(firstWand.getItem());
                mage.sendMessage("Your wands have been combined!");
            // This seems to work in the debugger, but.. doesn't do anything.
            // InventoryUtils.setInventoryResults(anvilInventory, newWand.getItem());
            } else if (organizingEnabled && Wand.isWand(firstItem)) {
                Wand firstWand = controller.getWand(firstItem);
                // TODO: Can't get the anvil's text from here.
                anvilInventory.setItem(0, null);
                anvilInventory.setItem(1, null);
                cursor.setType(Material.AIR);
                firstWand.organizeInventory(mage);
                firstWand.tryToOwn(player);
                player.getInventory().addItem(firstWand.getItem());
                mage.sendMessage("Your wand has been organized!");
            }
            return;
        }
    }
}
Also used : Player(org.bukkit.entity.Player) InventoryType(org.bukkit.event.inventory.InventoryType) Mage(com.elmakers.mine.bukkit.api.magic.Mage) Wand(com.elmakers.mine.bukkit.wand.Wand) ItemStack(org.bukkit.inventory.ItemStack) Inventory(org.bukkit.inventory.Inventory) ItemMeta(org.bukkit.inventory.meta.ItemMeta) SlotType(org.bukkit.event.inventory.InventoryType.SlotType) InventoryAction(org.bukkit.event.inventory.InventoryAction) EventHandler(org.bukkit.event.EventHandler)

Aggregations

InventoryType (org.bukkit.event.inventory.InventoryType)16 Player (org.bukkit.entity.Player)10 ItemStack (org.bukkit.inventory.ItemStack)10 EventHandler (org.bukkit.event.EventHandler)8 Inventory (org.bukkit.inventory.Inventory)7 HumanEntity (org.bukkit.entity.HumanEntity)6 SlotType (org.bukkit.event.inventory.InventoryType.SlotType)4 InventoryHolder (org.bukkit.inventory.InventoryHolder)4 PlayerInventory (org.bukkit.inventory.PlayerInventory)4 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)3 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)3 Wand (com.elmakers.mine.bukkit.wand.Wand)3 InventoryScriptContainer (com.denizenscript.denizen.scripts.containers.core.InventoryScriptContainer)2 ListTag (com.denizenscript.denizencore.objects.core.ListTag)2 InventoryAction (org.bukkit.event.inventory.InventoryAction)2 CraftingInventory (org.bukkit.inventory.CraftingInventory)2 BookMeta (org.bukkit.inventory.meta.BookMeta)2 ItemMeta (org.bukkit.inventory.meta.ItemMeta)2 ImprovedOfflinePlayer (com.denizenscript.denizen.nms.abstracts.ImprovedOfflinePlayer)1 InventoryTag (com.denizenscript.denizen.objects.InventoryTag)1