Search in sources :

Example 26 with Wand

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

the class LevitateSpell method onActivate.

@Override
public void onActivate() {
    final Player player = mage.getPlayer();
    if (player == null)
        return;
    // Prevent the player from death by fall
    direction = null;
    mountBoostTicksRemaining = 0;
    boostTicksRemaining = 0;
    controller.addFlightExemption(player);
    if (stashItem) {
        final PlayerInventory inventory = player.getInventory();
        heldItem = inventory.getItem(heldItemSlot);
        Plugin plugin = controller.getPlugin();
        plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {

            @Override
            public void run() {
                com.elmakers.mine.bukkit.api.wand.Wand wand = mage.getActiveWand();
                if (wand != null) {
                    wand.deactivate();
                }
                heldItem = inventory.getItem(heldItemSlot);
                inventory.setItem(heldItemSlot, null);
            }
        }, 1);
    } else {
        heldItem = null;
    }
    if (flySpeed > 0) {
        player.setFlySpeed(flySpeed * defaultFlySpeed);
    }
    if (thrustSpeed > 0) {
        if (thrust != null) {
            thrust.stop();
        }
        thrust = new ThrustAction(this, thrustFrequency + flyDelay + startDelay, thrustFrequency);
    } else if (deactivateFrequency > 0) {
        thrust = new ThrustAction(this, deactivateFrequency + flyDelay + startDelay, deactivateFrequency);
    }
    if (mountType != null) {
        Location location = mage.getLocation();
        World world = location.getWorld();
        Entity entity = null;
        try {
            String mountName = DeprecatedUtils.getName(mountType);
            if (mountName.indexOf("Entity") != 0) {
                // TODO: Look into spawning the entity via API calls.
                // I think the only reason we jump through all of these hoops is to be able to modify the entity before
                // spawning, such as making it invisible. There must be a better way.
                mountName = mountName.substring(0, 1).toUpperCase() + mountName.substring(1);
                mountName = "Entity" + mountName;
            }
            Class<?> mountClass = NMSUtils.getBukkitClass("net.minecraft.server." + mountName);
            if (mountClass != null) {
                if (worldClass == null) {
                    worldClass = NMSUtils.getBukkitClass("net.minecraft.server.World");
                }
                Constructor<? extends Object> constructor = mountClass.getConstructor(worldClass);
                Object nmsWorld = NMSUtils.getHandle(world);
                Object nmsEntity = constructor.newInstance(nmsWorld);
                entity = NMSUtils.getBukkitEntity(nmsEntity);
                if (entity != null) {
                    if (entity instanceof LivingEntity && mountInvisible) {
                        ((LivingEntity) entity).addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 2 << 24, 0));
                    }
                    if (entity instanceof Tameable) {
                        ((Tameable) entity).setTamed(true);
                    }
                    if (mountSilent) {
                        CompatibilityUtils.setSilent(entity, true);
                    }
                    Method setLocationMethod = mountClass.getMethod("setLocation", Double.TYPE, Double.TYPE, Double.TYPE, Float.TYPE, Float.TYPE);
                    setLocationMethod.invoke(nmsEntity, location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
                    controller.setForceSpawn(true);
                    try {
                        CompatibilityUtils.addToWorld(world, entity, mountSpawnReason);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                        ;
                    }
                    controller.setForceSpawn(false);
                } else {
                    mage.sendMessage("Failed to spawn entity of type: " + mountType + " (" + DeprecatedUtils.getName(mountType) + ")");
                    return;
                }
            } else {
                mage.sendMessage("Invalid entity type: " + mountType + " (" + DeprecatedUtils.getName(mountType) + ")");
                return;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        if (entity != null) {
            mountEntity = entity;
            CompatibilityUtils.setInvulnerable(mountEntity);
            if (entity instanceof Horse) {
                Horse horse = (Horse) mountEntity;
                horse.setTamed(true);
                horse.setOwner(player);
                horse.setAdult();
                horse.setStyle(mountHorseStyle);
                // horse.setVariant(mountHorseVariant);
                horse.setColor(mountHorseColor);
                horse.getInventory().setSaddle(new ItemStack(Material.SADDLE, 1));
                if (mountItem != null) {
                    horse.getInventory().setArmor(new ItemStack(mountItem, 1));
                }
                if (mountBaby) {
                    horse.setBaby();
                }
            }
            if (entity instanceof Pig) {
                Pig pig = (Pig) entity;
                pig.setSaddle(true);
                if (mountBaby) {
                    pig.setBaby();
                }
            }
            if (entity instanceof LivingEntity) {
                LivingEntity living = (LivingEntity) mountEntity;
                living.setHealth(0.5);
                living.setMaxHealth(mountHealth);
            }
            if (entity instanceof ArmorStand) {
                ArmorStand armorStand = (ArmorStand) entity;
                configureArmorStand(armorStand);
            } else if (useArmorStand) {
                armorStand = CompatibilityUtils.createArmorStand(mage.getLocation());
                configureArmorStand(armorStand);
                armorStand.setPassenger(mountEntity);
                armorStand.setMetadata("notarget", new FixedMetadataValue(controller.getPlugin(), true));
                armorStand.setMetadata("broom", new FixedMetadataValue(controller.getPlugin(), true));
                controller.setForceSpawn(true);
                try {
                    CompatibilityUtils.addToWorld(mage.getLocation().getWorld(), armorStand, mountSpawnReason);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                controller.setForceSpawn(false);
            } else {
                armorStand = null;
            }
            mountEntity.setPassenger(mage.getEntity());
            mountEntity.setMetadata("notarget", new FixedMetadataValue(controller.getPlugin(), true));
            mountEntity.setMetadata("broom", new FixedMetadataValue(controller.getPlugin(), true));
            if (listener == null) {
                listener = new LevitateListener(controller);
                Plugin plugin = controller.getPlugin();
                plugin.getServer().getPluginManager().registerEvents(listener, plugin);
            }
        }
    }
    grounded = false;
    if (maxHeight > 0 && player.getLocation().getY() >= maxHeight) {
        grounded = true;
    } else if (maxHeightAboveGround > 0) {
        Block block = player.getLocation().getBlock();
        int height = 0;
        while (height < maxHeightAboveGround && block.getType() == Material.AIR) {
            block = block.getRelative(BlockFace.DOWN);
            height++;
        }
        if (block.getType() == Material.AIR) {
            grounded = true;
        }
    }
    if (!grounded) {
        Vector velocity = player.getVelocity();
        velocity.setY(velocity.getY() + yBoost);
        if (mountEntity != null) {
            if (armorStand != null) {
                SafetyUtils.setVelocity(armorStand, velocity);
            } else {
                SafetyUtils.setVelocity(mountEntity, velocity);
            }
        } else {
            SafetyUtils.setVelocity(player, velocity);
        }
    }
    if (flight) {
        Bukkit.getScheduler().scheduleSyncDelayedTask(controller.getPlugin(), new Runnable() {

            @Override
            public void run() {
                player.setAllowFlight(true);
                player.setFlying(true);
            }
        }, flyDelay);
    }
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) HumanEntity(org.bukkit.entity.HumanEntity) PotionEffect(org.bukkit.potion.PotionEffect) Wand(com.elmakers.mine.bukkit.wand.Wand) World(org.bukkit.World) LivingEntity(org.bukkit.entity.LivingEntity) ArmorStand(org.bukkit.entity.ArmorStand) Horse(org.bukkit.entity.Horse) Vector(org.bukkit.util.Vector) Player(org.bukkit.entity.Player) Tameable(org.bukkit.entity.Tameable) FixedMetadataValue(org.bukkit.metadata.FixedMetadataValue) PlayerInventory(org.bukkit.inventory.PlayerInventory) Method(java.lang.reflect.Method) Pig(org.bukkit.entity.Pig) Block(org.bukkit.block.Block) ItemStack(org.bukkit.inventory.ItemStack) Plugin(org.bukkit.plugin.Plugin) Location(org.bukkit.Location)

Example 27 with Wand

use of com.elmakers.mine.bukkit.wand.Wand 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 28 with Wand

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

the class InventoryController method onInventoryClosed.

@EventHandler
public void onInventoryClosed(InventoryCloseEvent event) {
    if (!(event.getPlayer() instanceof Player))
        return;
    // Update the active wand, it may have changed around
    Player player = (Player) event.getPlayer();
    Mage mage = controller.getRegisteredMage(player);
    if (mage == null)
        return;
    Wand previousWand = mage.getActiveWand();
    // Prevent spells getting smuggled out via crafting slots
    Inventory inventory = event.getInventory();
    if (inventory instanceof CraftingInventory && previousWand != null && previousWand.wasInventoryOpen()) {
        CraftingInventory craftingInventory = (CraftingInventory) inventory;
        ItemStack[] matrix = craftingInventory.getMatrix();
        for (int i = 0; i < matrix.length; i++) {
            matrix[i] = new ItemStack(Material.AIR);
        }
        craftingInventory.setMatrix(matrix);
    }
    GUIAction gui = mage.getActiveGUI();
    if (gui != null) {
        mage.onGUIDeactivate();
    }
    // This is just to make sure we don't lose changes made to the inventory
    if (previousWand != null && previousWand.isInventoryOpen()) {
        if (previousWand.getMode() == WandMode.INVENTORY) {
            previousWand.saveInventory();
            // Update hotbar names
            previousWand.updateHotbar();
        } else if (previousWand.getMode() == WandMode.CHEST || previousWand.getMode() == WandMode.SKILLS) {
            // Close and save an open chest mode inventory
            previousWand.closeInventory();
        }
    } else {
        if (previousWand != null && !previousWand.wasInventoryOpen()) {
            previousWand.checkInventoryForUpgrades();
        }
        mage.checkWand();
    }
}
Also used : CraftingInventory(org.bukkit.inventory.CraftingInventory) Player(org.bukkit.entity.Player) Mage(com.elmakers.mine.bukkit.magic.Mage) GUIAction(com.elmakers.mine.bukkit.api.action.GUIAction) Wand(com.elmakers.mine.bukkit.wand.Wand) ItemStack(org.bukkit.inventory.ItemStack) CraftingInventory(org.bukkit.inventory.CraftingInventory) Inventory(org.bukkit.inventory.Inventory) EventHandler(org.bukkit.event.EventHandler)

Example 29 with Wand

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

the class PlayerController method onPlayerSwapItem.

@EventHandler
public void onPlayerSwapItem(PlayerSwapHandItemsEvent event) {
    final Player player = event.getPlayer();
    Mage mage = controller.getRegisteredMage(player);
    if (mage == null)
        return;
    final Wand activeWand = mage.getActiveWand();
    final Wand offhandWand = mage.getOffhandWand();
    if (activeWand == null && offhandWand == null)
        return;
    if (activeWand != null && activeWand.performAction(activeWand.getSwapAction())) {
        event.setCancelled(true);
    } else if (activeWand != null && activeWand.isInventoryOpen()) {
        activeWand.closeInventory();
        event.setCancelled(true);
    } else if (activeWand != null || offhandWand != null || Wand.isWand(event.getMainHandItem()) || Wand.isWand(event.getOffHandItem())) {
        mage.checkWandNextTick();
    }
}
Also used : Player(org.bukkit.entity.Player) Mage(com.elmakers.mine.bukkit.magic.Mage) Wand(com.elmakers.mine.bukkit.wand.Wand) EventHandler(org.bukkit.event.EventHandler)

Example 30 with Wand

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

the class PlayerController method onPlayerEquip.

@EventHandler
public void onPlayerEquip(PlayerItemHeldEvent event) {
    if (!controller.isLoaded())
        return;
    Player player = event.getPlayer();
    PlayerInventory inventory = player.getInventory();
    ItemStack next = inventory.getItem(event.getNewSlot());
    Mage mage = controller.getMage(player);
    // Check for self-destructing and temporary items
    if (Wand.isSelfDestructWand(next)) {
        mage.sendMessageKey("wand.self_destruct");
        inventory.setItem(event.getNewSlot(), null);
        mage.checkWand();
        return;
    }
    if (NMSUtils.isTemporary(next)) {
        inventory.setItem(event.getNewSlot(), null);
        mage.checkWand();
        return;
    }
    Wand activeWand = mage.getActiveWand();
    boolean isSkill = Wand.isSkill(next);
    if (isSkill && Wand.isQuickCastSkill(next)) {
        mage.useSkill(next);
        event.setCancelled(true);
        return;
    }
    boolean isQuickCast = activeWand != null && activeWand.isQuickCast() && activeWand.isInventoryOpen();
    if (isQuickCast) {
        Spell spell = mage.getSpell(Wand.getSpell(next));
        if (spell != null) {
            activeWand.cast(spell);
        }
        event.setCancelled(true);
        return;
    }
    // Check for active Wand
    boolean isWand = Wand.isWand(next);
    if (activeWand != null && activeWand.isInventoryOpen()) {
        // If the wand inventory is open, we're going to let them select a spell or material
        if (!isWand) {
            mage.activateIcon(activeWand, next);
            // Make sure we have the current wand item in the player's inventory so the
            // item text gets updated on hotbar item selection "bounce"
            int previousSlot = event.getPreviousSlot();
            ItemStack previous = inventory.getItem(previousSlot);
            if (previous != null && previous.equals(activeWand.getItem())) {
                player.getInventory().setItem(previousSlot, activeWand.getItem());
            }
        }
        event.setCancelled(true);
    } else if (isWand || activeWand != null) {
        // Check to see if we've switched to/from a wand
        mage.checkWandNextTick();
    }
    // Check for map selection if no wand is active
    activeWand = mage.getActiveWand();
    if (activeWand == null && next != null) {
        if (next.getType() == Material.MAP) {
            mage.setLastHeldMapId(next.getDurability());
        }
    }
}
Also used : Player(org.bukkit.entity.Player) Mage(com.elmakers.mine.bukkit.magic.Mage) Wand(com.elmakers.mine.bukkit.wand.Wand) PlayerInventory(org.bukkit.inventory.PlayerInventory) ItemStack(org.bukkit.inventory.ItemStack) Spell(com.elmakers.mine.bukkit.api.spell.Spell) EventHandler(org.bukkit.event.EventHandler)

Aggregations

Wand (com.elmakers.mine.bukkit.wand.Wand)36 Player (org.bukkit.entity.Player)24 ItemStack (org.bukkit.inventory.ItemStack)23 EventHandler (org.bukkit.event.EventHandler)17 LostWand (com.elmakers.mine.bukkit.api.wand.LostWand)11 Mage (com.elmakers.mine.bukkit.magic.Mage)11 PlayerInventory (org.bukkit.inventory.PlayerInventory)6 Spell (com.elmakers.mine.bukkit.api.spell.Spell)5 Nullable (javax.annotation.Nullable)5 LostWand (com.elmakers.mine.bukkit.wand.LostWand)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Inventory (org.bukkit.inventory.Inventory)4 GUIAction (com.elmakers.mine.bukkit.api.action.GUIAction)3 Messages (com.elmakers.mine.bukkit.api.magic.Messages)3 Location (org.bukkit.Location)3 Block (org.bukkit.block.Block)3 Entity (org.bukkit.entity.Entity)3 Item (org.bukkit.entity.Item)3