Search in sources :

Example 1 with WandMode

use of com.elmakers.mine.bukkit.wand.WandMode in project MagicPlugin by elBukkit.

the class WandCommandExecutor method onTabComplete.

@Override
public Collection<String> onTabComplete(CommandSender sender, String commandName, String[] args) {
    List<String> options = new ArrayList<>();
    Player player = (sender instanceof Player) ? (Player) sender : null;
    String permissionKey = "wand";
    if (commandName.contains("wandp")) {
        permissionKey = "wandp";
        if (args.length > 0) {
            player = DeprecatedUtils.getPlayer(args[0]);
        }
        if (args.length == 1) {
            options.addAll(api.getPlayerNames());
            return options;
        } else if (args.length > 1) {
            args = Arrays.copyOfRange(args, 1, args.length);
        }
    }
    if (args.length == 1) {
        addIfPermissible(sender, options, "Magic.commands." + permissionKey + ".", "add");
        addIfPermissible(sender, options, "Magic.commands." + permissionKey + ".", "remove");
        addIfPermissible(sender, options, "Magic.commands." + permissionKey + ".", "name");
        addIfPermissible(sender, options, "Magic.commands." + permissionKey + ".", "fill");
        addIfPermissible(sender, options, "Magic.commands." + permissionKey + ".", "configure");
        addIfPermissible(sender, options, "Magic.commands." + permissionKey + ".", "override");
        addIfPermissible(sender, options, "Magic.commands." + permissionKey + ".", "organize");
        addIfPermissible(sender, options, "Magic.commands." + permissionKey + ".", "alphabetize");
        addIfPermissible(sender, options, "Magic.commands." + permissionKey + ".", "combine");
        addIfPermissible(sender, options, "Magic.commands." + permissionKey + ".", "upgrade");
        addIfPermissible(sender, options, "Magic.commands." + permissionKey + ".", "describe");
        addIfPermissible(sender, options, "Magic.commands." + permissionKey + ".", "enchant");
        addIfPermissible(sender, options, "Magic.commands." + permissionKey + ".", "create");
        addIfPermissible(sender, options, "Magic.commands." + permissionKey + ".", "destroy");
        addIfPermissible(sender, options, "Magic.commands." + permissionKey + ".", "duplicate");
        addIfPermissible(sender, options, "Magic.commands." + permissionKey + ".", "restore");
        addIfPermissible(sender, options, "Magic.commands." + permissionKey + ".", "unlock");
        addIfPermissible(sender, options, "Magic.commands." + permissionKey + ".", "bind");
        addIfPermissible(sender, options, "Magic.commands." + permissionKey + ".", "unbind");
        addIfPermissible(sender, options, "Magic.commands." + permissionKey + ".", "save");
        addIfPermissible(sender, options, "Magic.commands." + permissionKey + ".", "delete");
        addIfPermissible(sender, options, "Magic.commands." + permissionKey + ".", "levelspells");
        Collection<String> allWands = api.getWandKeys();
        for (String wandKey : allWands) {
            addIfPermissible(sender, options, "Magic.create.", wandKey);
        }
    }
    if (args.length == 2) {
        String subCommand = args[0];
        String subCommandPNode = "Magic.commands." + permissionKey + "." + subCommand;
        if (!api.hasPermission(sender, subCommandPNode)) {
            return options;
        }
        subCommandPNode += ".";
        if (subCommand.equalsIgnoreCase("add")) {
            Collection<SpellTemplate> spellList = api.getSpellTemplates(sender.hasPermission("Magic.bypass_hidden"));
            for (SpellTemplate spell : spellList) {
                addIfPermissible(sender, options, subCommandPNode, spell.getKey(), true);
            }
            addIfPermissible(sender, options, subCommandPNode, "brush", true);
        }
        if (subCommand.equalsIgnoreCase("configure") || subCommand.equalsIgnoreCase("describe") || subCommand.equalsIgnoreCase("upgrade")) {
            for (String key : BaseMagicProperties.PROPERTY_KEYS) {
                options.add(key);
            }
            for (String damageType : api.getController().getDamageTypes()) {
                options.add("protection." + damageType);
                options.add("strength." + damageType);
                options.add("weakness." + damageType);
            }
        }
        if (subCommand.equalsIgnoreCase("override")) {
            Collection<SpellTemplate> spellList = api.getController().getSpellTemplates(true);
            String partial = args[1];
            if (partial.indexOf('.') > 0) {
                String[] pieces = StringUtils.split(partial, '.');
                String spellKey = pieces[0];
                SpellTemplate spell = api.getController().getSpellTemplate(spellKey);
                if (spell != null) {
                    List<String> spellOptions = new ArrayList<>();
                    spell.getParameters(spellOptions);
                    for (String option : spellOptions) {
                        options.add(spellKey + "." + option);
                    }
                }
            } else {
                for (SpellTemplate spell : spellList) {
                    String spellKey = spell.getKey();
                    if (api.hasPermission(sender, subCommandPNode + spellKey)) {
                        options.add(spellKey + ".");
                    }
                }
            }
        }
        if (subCommand.equalsIgnoreCase("remove")) {
            Wand activeWand = null;
            if (player != null) {
                Mage mage = controller.getMage(player);
                activeWand = mage.getActiveWand();
            }
            if (activeWand != null) {
                Collection<String> spellNames = activeWand.getSpells();
                for (String spellName : spellNames) {
                    options.add(spellName);
                }
                options.add("brush");
            }
        }
        if (subCommand.equalsIgnoreCase("combine")) {
            Collection<String> allWands = api.getWandKeys();
            for (String wandKey : allWands) {
                addIfPermissible(sender, options, "Magic.commands." + permissionKey + ".combine.", wandKey, true);
            }
        }
        if (subCommand.equalsIgnoreCase("delete")) {
            File wandFolder = new File(api.getController().getConfigFolder(), "wands");
            if (wandFolder.exists()) {
                File[] files = wandFolder.listFiles();
                for (File file : files) {
                    if (file.getName().endsWith(".yml")) {
                        options.add(file.getName().replace(".yml", ""));
                    }
                }
            }
        }
    }
    if (args.length == 3) {
        String subCommand = args[0];
        String subCommand2 = args[1];
        String commandPNode = "Magic.commands." + permissionKey + "." + subCommand;
        if (!api.hasPermission(sender, commandPNode)) {
            return options;
        }
        if (subCommand.equalsIgnoreCase("override")) {
            String[] pieces = StringUtils.split(subCommand2, '.');
            if (pieces.length > 1) {
                String spellKey = pieces[0];
                String argument = pieces[1];
                SpellTemplate spell = api.getSpellTemplate(spellKey);
                if (spell != null) {
                    spell.getParameterOptions(options, argument);
                }
            }
        }
        if (subCommand.equalsIgnoreCase("configure") || subCommand.equalsIgnoreCase("upgrade")) {
            if (subCommand2.equals("effect_sound")) {
                Sound[] sounds = Sound.values();
                for (Sound sound : sounds) {
                    options.add(sound.name().toLowerCase());
                }
            } else if (subCommand2.equals("effect_particle")) {
                ParticleEffect[] particleTypes = ParticleEffect.values();
                for (ParticleEffect particleType : particleTypes) {
                    options.add(particleType.name().toLowerCase());
                }
            } else if (subCommand2.equals("mode")) {
                for (WandMode mode : WandMode.values()) {
                    options.add(mode.name().toLowerCase());
                }
            } else if (subCommand2.equals("left_click") || subCommand2.equals("right_click") || subCommand2.equals("drop") || subCommand2.equals("swap")) {
                for (WandAction action : WandAction.values()) {
                    options.add(action.name().toLowerCase());
                }
            }
        }
        String subCommandPNode = "Magic.commands." + permissionKey + "." + subCommand + "." + subCommand2;
        if (!api.hasPermission(sender, subCommandPNode)) {
            return options;
        }
        boolean isBrushCommand = subCommand2.equalsIgnoreCase("material") || subCommand2.equalsIgnoreCase("brush");
        if (subCommand.equalsIgnoreCase("remove") && isBrushCommand) {
            Wand activeWand = null;
            if (player != null) {
                Mage mage = controller.getMage(player);
                activeWand = mage.getActiveWand();
            }
            if (activeWand != null) {
                Collection<String> materialNames = activeWand.getBrushes();
                for (String materialName : materialNames) {
                    options.add(materialName);
                }
            }
        }
        if (subCommand.equalsIgnoreCase("add") && isBrushCommand) {
            options.addAll(api.getBrushes());
        }
    }
    return options;
}
Also used : Player(org.bukkit.entity.Player) ArrayList(java.util.ArrayList) Wand(com.elmakers.mine.bukkit.api.wand.Wand) Sound(org.bukkit.Sound) ParticleEffect(de.slikey.effectlib.util.ParticleEffect) Mage(com.elmakers.mine.bukkit.api.magic.Mage) File(java.io.File) WandMode(com.elmakers.mine.bukkit.wand.WandMode) WandAction(com.elmakers.mine.bukkit.api.wand.WandAction) SpellTemplate(com.elmakers.mine.bukkit.api.spell.SpellTemplate)

Example 2 with WandMode

use of com.elmakers.mine.bukkit.wand.WandMode 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)

Aggregations

WandMode (com.elmakers.mine.bukkit.wand.WandMode)2 Player (org.bukkit.entity.Player)2 GUIAction (com.elmakers.mine.bukkit.api.action.GUIAction)1 Mage (com.elmakers.mine.bukkit.api.magic.Mage)1 Spell (com.elmakers.mine.bukkit.api.spell.Spell)1 SpellTemplate (com.elmakers.mine.bukkit.api.spell.SpellTemplate)1 Wand (com.elmakers.mine.bukkit.api.wand.Wand)1 WandAction (com.elmakers.mine.bukkit.api.wand.WandAction)1 Mage (com.elmakers.mine.bukkit.magic.Mage)1 Wand (com.elmakers.mine.bukkit.wand.Wand)1 ParticleEffect (de.slikey.effectlib.util.ParticleEffect)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Location (org.bukkit.Location)1 Sound (org.bukkit.Sound)1 Item (org.bukkit.entity.Item)1 EventHandler (org.bukkit.event.EventHandler)1 InventoryAction (org.bukkit.event.inventory.InventoryAction)1 InventoryType (org.bukkit.event.inventory.InventoryType)1 CraftingInventory (org.bukkit.inventory.CraftingInventory)1