Search in sources :

Example 56 with ItemMeta

use of org.bukkit.inventory.meta.ItemMeta in project MagicPlugin by elBukkit.

the class ItemShopAction method initialize.

@Override
public void initialize(Spell spell, ConfigurationSection parameters) {
    super.initialize(spell, parameters);
    items.clear();
    if (parameters.contains("items")) {
        if (parameters.isConfigurationSection("items")) {
            ConfigurationSection itemSection = ConfigurationUtils.getConfigurationSection(parameters, "items");
            Collection<String> itemKeys = itemSection.getKeys(false);
            for (String itemKey : itemKeys) {
                items.add(createShopItem(spell.getController(), itemKey, itemSection.getDouble(itemKey, -1)));
            }
        } else {
            List<?> itemList = parameters.getList("items");
            for (Object itemEntry : itemList) {
                if (itemEntry instanceof String) {
                    String itemKey = (String) itemEntry;
                    items.add(createShopItem(spell.getController(), itemKey, -1));
                } else if (itemEntry instanceof ConfigurationSection || itemEntry instanceof Map) {
                    ConfigurationSection itemConfig = (itemEntry instanceof ConfigurationSection) ? (ConfigurationSection) itemEntry : ConfigUtils.toConfigurationSection((Map<?, ?>) itemEntry);
                    ShopItem shopItem = null;
                    if (itemConfig != null) {
                        double cost = itemConfig.getDouble("cost");
                        if (itemConfig.isString("item")) {
                            shopItem = createShopItem(spell.getController(), itemConfig);
                            if (shopItem != null) {
                                String name = itemConfig.getString("name");
                                List<String> lore = ConfigurationUtils.getStringList(itemConfig, "lore");
                                if (name != null || lore != null) {
                                    ItemStack item = shopItem.getItem();
                                    ItemMeta meta = item.getItemMeta();
                                    if (name != null) {
                                        meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', name));
                                    }
                                    if (lore != null) {
                                        List<String> translatedLore = new ArrayList<>();
                                        for (String line : lore) {
                                            if (line != null) {
                                                translatedLore.add(ChatColor.translateAlternateColorCodes('&', line));
                                            }
                                        }
                                        meta.setLore(translatedLore);
                                    }
                                    item.setItemMeta(meta);
                                }
                            }
                        } else {
                            ItemStack itemStack = itemConfig.getItemStack("item");
                            if (itemStack != null) {
                                shopItem = new ShopItem(itemStack, cost);
                            }
                        }
                    }
                    items.add(shopItem);
                }
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ItemStack(org.bukkit.inventory.ItemStack) Map(java.util.Map) ItemMeta(org.bukkit.inventory.meta.ItemMeta) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 57 with ItemMeta

use of org.bukkit.inventory.meta.ItemMeta in project MagicPlugin by elBukkit.

the class BrushSelectAction method perform.

@Override
public SpellResult perform(CastContext context) {
    Mage mage = context.getMage();
    MageController controller = context.getController();
    Wand wand = context.getWand();
    schematics.clear();
    variants.clear();
    this.context = context;
    Player player = mage.getPlayer();
    if (player == null) {
        return SpellResult.PLAYER_REQUIRED;
    }
    if (wand == null) {
        return SpellResult.FAIL;
    }
    List<String> brushKeys = new ArrayList<>(wand.getBrushes());
    Collections.sort(brushKeys);
    List<ItemStack> brushes = new ArrayList<>();
    List<ItemStack> specials = new ArrayList<>();
    MaterialAndData previous = null;
    for (String brushKey : brushKeys) {
        ItemStack brushItem = com.elmakers.mine.bukkit.wand.Wand.createBrushItem(brushKey, controller, null, false);
        if (MaterialBrush.isSchematic(brushKey)) {
            schematics.add(brushItem);
            continue;
        }
        if (MaterialBrush.isSpecialMaterialKey(brushKey)) {
            specials.add(brushItem);
            continue;
        }
        if (brushItem != null) {
            MaterialAndData material = new MaterialAndData(brushKey);
            if (previous != null && material.getMaterial() == previous.getMaterial()) {
                List<ItemStack> variantList = variants.get(material.getMaterial());
                ItemStack lastAdded = brushes.get(brushes.size() - 1);
                if (variantList == null) {
                    String baseName = material.getBaseName();
                    variantList = new ArrayList<>();
                    variantList.add(lastAdded);
                    brushes.remove(brushes.size() - 1);
                    ItemStack category = InventoryUtils.getCopy(lastAdded);
                    ItemMeta meta = category.getItemMeta();
                    String name = context.getMessage("variant_name", "" + ChatColor.AQUA + "$variant");
                    meta.setDisplayName(name.replace("$variant", baseName));
                    List<String> lore = new ArrayList<>();
                    String description = context.getMessage("variant_description", "Choose a type of $variant");
                    lore.add(description.replace("$variant", baseName));
                    meta.setLore(lore);
                    category.setItemMeta(meta);
                    InventoryUtils.setMeta(category, "brush_set", "variants");
                    variants.put(material.getMaterial(), variantList);
                    brushes.add(category);
                }
                variantList.add(brushItem);
            } else {
                brushes.add(brushItem);
            }
            previous = material;
        }
    }
    ItemStack schematicItem = null;
    if (schematics.size() == 1) {
        schematicItem = schematics.get(0);
    } else if (schematics.size() > 0) {
        schematicItem = InventoryUtils.getCopy(schematics.get(0));
        ItemMeta meta = schematicItem.getItemMeta();
        meta.setDisplayName(context.getMessage("schematics_name", "" + ChatColor.AQUA + "Schematics"));
        List<String> lore = new ArrayList<>();
        lore.add(context.getMessage("schematics_description", "Choose a schematic"));
        meta.setLore(lore);
        schematicItem.setItemMeta(meta);
        InventoryUtils.setMeta(schematicItem, "brush_set", "schematics");
    }
    if (schematicItem != null) {
        brushes.add(schematicItem);
    }
    brushes.addAll(specials);
    if (brushes.size() == 0) {
        return SpellResult.NO_TARGET;
    }
    int inventorySize = 9 * INVENTORY_ROWS;
    int numPages = (int) Math.ceil((float) brushes.size() / inventorySize);
    if (page < 1)
        page = numPages;
    else if (page > numPages)
        page = 1;
    int pageIndex = page - 1;
    int startIndex = pageIndex * inventorySize;
    int maxIndex = (pageIndex + 1) * inventorySize - 1;
    List<ItemStack> showBrushes = new ArrayList<>();
    for (int i = startIndex; i <= maxIndex && i < brushes.size(); i++) {
        showBrushes.add(brushes.get(i));
    }
    String inventoryTitle = context.getMessage("title", "Brushes");
    if (numPages > 1) {
        inventoryTitle += " (" + page + "/" + numPages + ")";
    }
    int invSize = (int) Math.ceil(showBrushes.size() / 9.0f) * 9;
    Inventory displayInventory = CompatibilityUtils.createInventory(null, invSize, inventoryTitle);
    for (ItemStack brush : showBrushes) {
        displayInventory.addItem(brush);
    }
    mage.activateGUI(this, displayInventory);
    return SpellResult.CAST;
}
Also used : Player(org.bukkit.entity.Player) ArrayList(java.util.ArrayList) Wand(com.elmakers.mine.bukkit.api.wand.Wand) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Mage(com.elmakers.mine.bukkit.api.magic.Mage) MaterialAndData(com.elmakers.mine.bukkit.block.MaterialAndData) ArrayList(java.util.ArrayList) List(java.util.List) ItemStack(org.bukkit.inventory.ItemStack) ItemMeta(org.bukkit.inventory.meta.ItemMeta) Inventory(org.bukkit.inventory.Inventory)

Example 58 with ItemMeta

use of org.bukkit.inventory.meta.ItemMeta in project MagicPlugin by elBukkit.

the class CaptureAction method perform.

@Override
public SpellResult perform(CastContext context) {
    Entity targetEntity = context.getTargetEntity();
    if (targetEntity instanceof Player) {
        return SpellResult.NO_TARGET;
    }
    if (minHealth >= 0) {
        if (!(targetEntity instanceof LivingEntity)) {
            return SpellResult.NO_TARGET;
        }
        LivingEntity li = (LivingEntity) targetEntity;
        if (li.getHealth() > minHealth) {
            return SpellResult.NO_TARGET;
        }
    }
    String entityTypeString = CompatibilityUtils.getEntityType(targetEntity);
    if (entityTypeString == null) {
        return SpellResult.FAIL;
    }
    Object savedEntity = CompatibilityUtils.getEntityData(targetEntity);
    if (savedEntity == null) {
        return SpellResult.FAIL;
    }
    if (targetEntity instanceof LivingEntity) {
        LivingEntity li = (LivingEntity) targetEntity;
        li.setHealth(li.getMaxHealth());
    }
    targetEntity.remove();
    ItemStack spawnEgg = new ItemStack(Material.MONSTER_EGG);
    String entityName = targetEntity.getCustomName();
    if (entityName != null && !entityName.isEmpty()) {
        ItemMeta meta = spawnEgg.getItemMeta();
        meta.setDisplayName("Spawn " + entityName);
        spawnEgg.setItemMeta(meta);
    }
    spawnEgg = InventoryUtils.makeReal(spawnEgg);
    // Add entity type attribute
    CompatibilityUtils.setMeta(savedEntity, "id", entityTypeString);
    // Remove instance-specific attributes
    CompatibilityUtils.removeMeta(savedEntity, "Pos");
    CompatibilityUtils.removeMeta(savedEntity, "Motion");
    CompatibilityUtils.removeMeta(savedEntity, "Rotation");
    CompatibilityUtils.removeMeta(savedEntity, "FallDistance");
    CompatibilityUtils.removeMeta(savedEntity, "Dimension");
    CompatibilityUtils.removeMeta(savedEntity, "UUID");
    CompatibilityUtils.removeMeta(savedEntity, "PortalCooldown");
    if (!CompatibilityUtils.setMetaNode(spawnEgg, "EntityTag", savedEntity)) {
        return SpellResult.FAIL;
    }
    targetEntity.getWorld().dropItemNaturally(targetEntity.getLocation(), spawnEgg);
    return SpellResult.CAST;
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) ItemStack(org.bukkit.inventory.ItemStack) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 59 with ItemMeta

use of org.bukkit.inventory.meta.ItemMeta in project MagicPlugin by elBukkit.

the class SpellShopAction method getItems.

@Nullable
@Override
public List<ShopItem> getItems(CastContext context) {
    Mage mage = context.getMage();
    Wand wand = mage.getActiveWand();
    CasterProperties caster = getCaster(context);
    // Check for auto upgrade, if the player can no longer progress on their current path and is
    // eligible for an upgrade, we will upgrade them and skip showing the spell shop so the player
    // can see their upgrade rewards.
    boolean canProgress = false;
    // TODO: Make this work on CasterProperties
    if (autoUpgrade && wand != null) {
        canProgress = caster.canProgress();
        if (!canProgress && wand.checkUpgrade(true) && wand.upgrade(false)) {
            return null;
        }
    }
    ProgressionPath currentPath = caster.getPath();
    if (!castsSpells && !allowLocked && wand != null && wand.isLocked()) {
        context.showMessage(context.getMessage("no_path", getDefaultMessage(context, "no_path")).replace("$wand", wand.getName()));
        return null;
    }
    // Load spells
    Map<String, Double> spellPrices = new HashMap<>();
    if (spells.size() > 0) {
        spellPrices.putAll(spells);
    } else {
        if (currentPath == null) {
            String name = wand == null ? "" : wand.getName();
            context.showMessage(context.getMessage("no_path", getDefaultMessage(context, "no_path")).replace("$wand", name));
            return null;
        }
        if (showPath) {
            Collection<String> pathSpells = currentPath.getSpells();
            for (String pathSpell : pathSpells) {
                spellPrices.put(pathSpell, null);
            }
        }
        if (showRequired) {
            Collection<String> requiredSpells = currentPath.getRequiredSpells();
            for (String requiredSpell : requiredSpells) {
                spellPrices.put(requiredSpell, null);
            }
        }
        if (showUpgrades) {
            Collection<String> spells = caster.getSpells();
            for (String spellKey : spells) {
                MageSpell spell = mage.getSpell(spellKey);
                SpellTemplate upgradeSpell = spell.getUpgrade();
                if (upgradeSpell != null) {
                    spellPrices.put(upgradeSpell.getKey(), null);
                }
            }
        }
    }
    List<ShopItem> shopItems = new ArrayList<>();
    for (Map.Entry<String, Double> spellValue : spellPrices.entrySet()) {
        ShopItem shopItem = createShopItem(spellValue.getKey(), spellValue.getValue(), context);
        if (shopItem != null) {
            shopItems.add(shopItem);
        }
    }
    Collections.sort(shopItems);
    if (spells.size() == 0 && showExtra && !castsSpells && currentPath != null) {
        Collection<String> extraSpells = currentPath.getExtraSpells();
        List<ShopItem> extraItems = new ArrayList<>();
        for (String spellKey : extraSpells) {
            ShopItem shopItem = createShopItem(spellKey, null, context);
            if (shopItem != null) {
                ItemStack spellItem = shopItem.getItem();
                ItemMeta meta = spellItem.getItemMeta();
                List<String> itemLore = meta.getLore();
                itemLore.add(context.getMessage("extra_spell", getDefaultMessage(context, "extra_spell")));
                meta.setLore(itemLore);
                spellItem.setItemMeta(meta);
                extraItems.add(shopItem);
            }
        }
        Collections.sort(extraItems);
        shopItems.addAll(extraItems);
    }
    // If there is nothing here for us to do, check for upgrades being blocked
    // we will upgrade the wand here, but in theory that should never happen since we
    // checked for upgrades above.
    // TODO: Make this work for caster...
    mage.sendDebugMessage(ChatColor.GOLD + "Spells to buy: " + shopItems.size(), 2);
    if (wand != null && shopItems.size() == 0) {
        boolean canUpgrade = autoUpgrade && wand.checkUpgrade(false);
        boolean hasUpgrade = autoUpgrade && wand.hasUpgrade();
        if (!canProgress && !hasUpgrade) {
            context.showMessage(context.getMessage("no_upgrade", getDefaultMessage(context, "no_upgrade")).replace("$wand", wand.getName()));
            return null;
        } else if (canUpgrade) {
            wand.upgrade(false);
            return null;
        } else {
            return null;
        }
    }
    return shopItems;
}
Also used : ProgressionPath(com.elmakers.mine.bukkit.api.magic.ProgressionPath) CasterProperties(com.elmakers.mine.bukkit.api.magic.CasterProperties) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Wand(com.elmakers.mine.bukkit.api.wand.Wand) Mage(com.elmakers.mine.bukkit.api.magic.Mage) ItemStack(org.bukkit.inventory.ItemStack) HashMap(java.util.HashMap) Map(java.util.Map) MageSpell(com.elmakers.mine.bukkit.api.spell.MageSpell) ItemMeta(org.bukkit.inventory.meta.ItemMeta) SpellTemplate(com.elmakers.mine.bukkit.api.spell.SpellTemplate) Nullable(javax.annotation.Nullable)

Example 60 with ItemMeta

use of org.bukkit.inventory.meta.ItemMeta in project MagicPlugin by elBukkit.

the class SpellShopAction method createShopItem.

@Nullable
private ShopItem createShopItem(String key, Double worth, CastContext context) {
    CasterProperties caster = getCaster(context);
    Mage mage = context.getMage();
    MageController controller = context.getController();
    ProgressionPath currentPath = caster.getPath();
    key = context.parameterize(key);
    String spellKey = key.split(" ", 2)[0];
    if (!castsSpells && caster.hasSpell(spellKey)) {
        mage.sendDebugMessage(ChatColor.GRAY + " Skipping " + spellKey + ", already have it", 3);
        return null;
    }
    SpellTemplate spell = controller.getSpellTemplate(spellKey);
    if (spell == null) {
        mage.sendDebugMessage(ChatColor.RED + " Skipping " + spellKey + ", invalid spell", 0);
        return null;
    }
    if (worth == null) {
        worth = spell.getWorth();
    }
    if (worth <= 0 && !showFree) {
        mage.sendDebugMessage(ChatColor.YELLOW + " Skipping " + spellKey + ", is free (worth " + worth + ")", 3);
        return null;
    }
    if (!spell.hasCastPermission(mage.getCommandSender())) {
        mage.sendDebugMessage(ChatColor.YELLOW + " Skipping " + spellKey + ", no permission", 3);
        return null;
    }
    ItemStack spellItem = controller.createSpellItem(key, castsSpells);
    if (!castsSpells) {
        Spell mageSpell = caster.getSpell(spellKey);
        String requiredPathKey = mageSpell != null ? mageSpell.getRequiredUpgradePath() : null;
        Set<String> requiredPathTags = mageSpell != null ? mageSpell.getRequiredUpgradeTags() : null;
        long requiredCastCount = mageSpell != null ? mageSpell.getRequiredUpgradeCasts() : 0;
        long castCount = mageSpell != null ? Math.min(mageSpell.getCastCount(), requiredCastCount) : 0;
        Collection<PrerequisiteSpell> missingSpells = PrerequisiteSpell.getMissingRequirements(caster, spell);
        ItemMeta meta = spellItem.getItemMeta();
        List<String> itemLore = meta.getLore();
        if (itemLore == null) {
            mage.sendDebugMessage(ChatColor.RED + " Skipping " + spellKey + ", spell item is invalid", 0);
            return null;
        }
        List<String> lore = new ArrayList<>();
        if (spell.getSpellKey().getLevel() > 1 && itemLore.size() > 0) {
            lore.add(itemLore.get(0));
        }
        String upgradeDescription = spell.getUpgradeDescription();
        if (showUpgrades && upgradeDescription != null && !upgradeDescription.isEmpty()) {
            upgradeDescription = controller.getMessages().get("spell.upgrade_description_prefix") + upgradeDescription;
            InventoryUtils.wrapText(upgradeDescription, lore);
        }
        String unpurchasableMessage = null;
        Collection<Requirement> requirements = spell.getRequirements();
        String requirementMissing = controller.checkRequirements(context, requirements);
        if (requirementMissing != null) {
            unpurchasableMessage = requirementMissing;
            InventoryUtils.wrapText(unpurchasableMessage, lore);
        }
        if ((requiredPathKey != null && !currentPath.hasPath(requiredPathKey)) || (requiresCastCounts && requiredCastCount > 0 && castCount < requiredCastCount) || (requiredPathTags != null && !currentPath.hasAllTags(requiredPathTags)) || !missingSpells.isEmpty()) {
            if (mageSpell != null && !spell.getName().equals(mageSpell.getName())) {
                lore.add(context.getMessage("upgrade_name_change", getDefaultMessage(context, "upgrade_name_change")).replace("$name", mageSpell.getName()));
            }
            if (requiredPathKey != null && !currentPath.hasPath(requiredPathKey)) {
                requiredPathKey = currentPath.translatePath(requiredPathKey);
                com.elmakers.mine.bukkit.wand.WandUpgradePath upgradePath = com.elmakers.mine.bukkit.wand.WandUpgradePath.getPath(requiredPathKey);
                if (upgradePath != null) {
                    unpurchasableMessage = context.getMessage("level_requirement", getDefaultMessage(context, "level_requirement")).replace("$path", upgradePath.getName());
                    InventoryUtils.wrapText(unpurchasableMessage, lore);
                }
            } else if (requiredPathTags != null && !requiredPathTags.isEmpty() && !currentPath.hasAllTags(requiredPathTags)) {
                Set<String> tags = currentPath.getMissingTags(requiredPathTags);
                unpurchasableMessage = context.getMessage("tags_requirement", getDefaultMessage(context, "tags_requirement")).replace("$tags", controller.getMessages().formatList("tags", tags, "name"));
                InventoryUtils.wrapText(unpurchasableMessage, lore);
            }
            if (requiresCastCounts && requiredCastCount > 0 && castCount < requiredCastCount) {
                unpurchasableMessage = ChatColor.RED + context.getMessage("cast_requirement", getDefaultMessage(context, "cast_requirement")).replace("$current", Long.toString(castCount)).replace("$required", Long.toString(requiredCastCount));
                InventoryUtils.wrapText(unpurchasableMessage, lore);
            }
            if (!missingSpells.isEmpty()) {
                List<String> spells = new ArrayList<>(missingSpells.size());
                for (PrerequisiteSpell s : missingSpells) {
                    SpellTemplate template = context.getController().getSpellTemplate(s.getSpellKey().getKey());
                    String spellMessage = context.getMessage("prerequisite_spell_level", getDefaultMessage(context, "prerequisite_spell_level")).replace("$name", template.getName());
                    if (s.getProgressLevel() > 1) {
                        spellMessage += context.getMessage("prerequisite_spell_progress_level", getDefaultMessage(context, "prerequisite_spell_progress_level")).replace("$level", Long.toString(s.getProgressLevel())).replace("$max_level", Long.toString(Math.max(1, template.getMaxProgressLevel())));
                    }
                    spells.add(spellMessage);
                }
                unpurchasableMessage = ChatColor.RED + context.getMessage("required_spells", getDefaultMessage(context, "required_spells")).replace("$spells", StringUtils.join(spells, ", "));
                InventoryUtils.wrapText(ChatColor.GOLD + unpurchasableMessage, lore);
            }
        }
        for (int i = (spell.getSpellKey().getLevel() > 1 ? 1 : 0); i < itemLore.size(); i++) {
            lore.add(itemLore.get(i));
        }
        meta.setLore(lore);
        spellItem.setItemMeta(meta);
        if (unpurchasableMessage != null)
            InventoryUtils.setMeta(spellItem, "unpurchasable", unpurchasableMessage);
    }
    return new ShopItem(spellItem, worth);
}
Also used : ProgressionPath(com.elmakers.mine.bukkit.api.magic.ProgressionPath) CasterProperties(com.elmakers.mine.bukkit.api.magic.CasterProperties) Set(java.util.Set) ArrayList(java.util.ArrayList) Spell(com.elmakers.mine.bukkit.api.spell.Spell) MageSpell(com.elmakers.mine.bukkit.api.spell.MageSpell) BaseSpell(com.elmakers.mine.bukkit.spell.BaseSpell) PrerequisiteSpell(com.elmakers.mine.bukkit.api.spell.PrerequisiteSpell) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Requirement(com.elmakers.mine.bukkit.api.requirements.Requirement) Mage(com.elmakers.mine.bukkit.api.magic.Mage) PrerequisiteSpell(com.elmakers.mine.bukkit.api.spell.PrerequisiteSpell) ItemStack(org.bukkit.inventory.ItemStack) ItemMeta(org.bukkit.inventory.meta.ItemMeta) SpellTemplate(com.elmakers.mine.bukkit.api.spell.SpellTemplate) Nullable(javax.annotation.Nullable)

Aggregations

ItemMeta (org.bukkit.inventory.meta.ItemMeta)361 ItemStack (org.bukkit.inventory.ItemStack)205 ArrayList (java.util.ArrayList)87 Player (org.bukkit.entity.Player)45 Inventory (org.bukkit.inventory.Inventory)33 Map (java.util.Map)29 Enchantment (org.bukkit.enchantments.Enchantment)23 EventHandler (org.bukkit.event.EventHandler)23 SkullMeta (org.bukkit.inventory.meta.SkullMeta)22 CompoundTag (com.wasteofplastic.org.jnbt.CompoundTag)20 ListTag (com.wasteofplastic.org.jnbt.ListTag)20 StringTag (com.wasteofplastic.org.jnbt.StringTag)20 Tag (com.wasteofplastic.org.jnbt.Tag)20 PlayerInventory (org.bukkit.inventory.PlayerInventory)17 IOException (java.io.IOException)15 Material (org.bukkit.Material)14 PotionMeta (org.bukkit.inventory.meta.PotionMeta)14 Mage (com.elmakers.mine.bukkit.api.magic.Mage)12 BookMeta (org.bukkit.inventory.meta.BookMeta)12 List (java.util.List)11