Search in sources :

Example 1 with MaterialAndData

use of com.elmakers.mine.bukkit.api.block.MaterialAndData in project MagicPlugin by elBukkit.

the class SpellsCommandExecutor method listSpells.

public void listSpells(CommandSender sender, int pageNumber, String category) {
    if (category != null) {
        listSpellsByCategory(sender, category);
        return;
    }
    Player player = sender instanceof Player ? (Player) sender : null;
    Collection<SpellTemplate> spellVariants = api.getSpellTemplates(sender.hasPermission("Magic.bypass_hidden"));
    int spellCount = 0;
    for (SpellTemplate spell : spellVariants) {
        if (player != null && !spell.hasCastPermission(player)) {
            continue;
        }
        if (spell.getCategory() == null)
            continue;
        spellCount++;
    }
    // Kinda hacky internals-reaching
    Collection<SpellCategory> allCategories = api.getController().getCategories();
    List<SpellCategory> sortedGroups = new ArrayList<>(allCategories);
    Collections.sort(sortedGroups);
    int maxLines = -1;
    if (pageNumber >= 0) {
        maxLines = 5;
        int maxPages = spellCount / maxLines + 1;
        if (pageNumber > maxPages) {
            pageNumber = maxPages;
        }
        String message = api.getMessages().get("general.spell_list_page");
        message = message.replace("$count", Integer.toString(spellCount));
        message = message.replace("$pages", Integer.toString(maxPages));
        message = message.replace("$page", Integer.toString(pageNumber));
        sender.sendMessage(message);
    } else {
        String message = api.getMessages().get("general.spell_list");
        message = message.replace("$count", Integer.toString(spellCount));
        sender.sendMessage(message);
    }
    int currentPage = 1;
    int lineCount = 0;
    int printedCount = 0;
    for (SpellCategory group : sortedGroups) {
        if (printedCount > maxLines && maxLines > 0)
            break;
        boolean isFirst = true;
        Collection<SpellTemplate> spells = group.getSpells();
        for (SpellTemplate spell : spells) {
            if (printedCount > maxLines && maxLines > 0)
                break;
            if (!spell.hasCastPermission(sender))
                continue;
            if (currentPage == pageNumber || maxLines < 0) {
                if (isFirst) {
                    sender.sendMessage(group.getName() + ":");
                    isFirst = false;
                }
                String name = spell.getName();
                String description = spell.getDescription();
                if (!name.equals(spell.getKey())) {
                    description = name + " : " + description;
                }
                MaterialAndData spellIcon = spell.getIcon();
                Material material = spellIcon == null ? null : spellIcon.getMaterial();
                String icon = material == null ? "None" : material.name().toLowerCase();
                sender.sendMessage(ChatColor.AQUA + spell.getKey() + ChatColor.BLUE + " [" + icon + "] : " + ChatColor.YELLOW + description);
                printedCount++;
            }
            lineCount++;
            if (lineCount == maxLines) {
                lineCount = 0;
                currentPage++;
            }
        }
    }
}
Also used : Player(org.bukkit.entity.Player) SpellCategory(com.elmakers.mine.bukkit.api.spell.SpellCategory) MaterialAndData(com.elmakers.mine.bukkit.api.block.MaterialAndData) ArrayList(java.util.ArrayList) Material(org.bukkit.Material) SpellTemplate(com.elmakers.mine.bukkit.api.spell.SpellTemplate)

Example 2 with MaterialAndData

use of com.elmakers.mine.bukkit.api.block.MaterialAndData in project MagicPlugin by elBukkit.

the class HeroesSpellSkill method getDefaultConfig.

@Override
public ConfigurationSection getDefaultConfig() {
    ConfigurationSection node = super.getDefaultConfig();
    // Add in all configured spell parameters
    // Change keys to match heroes-format
    ConfigurationSection spellParameters = spellTemplate.getSpellParameters();
    if (spellParameters != null) {
        Set<String> parameterKeys = spellParameters.getKeys(false);
        for (String parameterKey : parameterKeys) {
            if (!spellParameters.isConfigurationSection(parameterKey) && !spellParameters.isList(parameterKey)) {
                String heroesKey = magicToHeroes(parameterKey);
                Object value = spellParameters.get(parameterKey);
                node.set(heroesKey, value);
                parameters.set(heroesKey, value);
            }
        }
    }
    node.set("icon-url", spellTemplate.getIconURL());
    MaterialAndData icon = spellTemplate.getIcon();
    if (icon != null && icon.getMaterial() != Material.AIR) {
        node.set("icon", icon.getKey());
    }
    MaterialAndData disabledIcon = spellTemplate.getDisabledIcon();
    if (disabledIcon != null && disabledIcon.getMaterial() != Material.AIR) {
        node.set("icon-disabled", disabledIcon.getKey());
    }
    node.set("icon-url", spellTemplate.getIconURL());
    node.set("icon-disabled-url", spellTemplate.getDisabledIconURL());
    node.set("cooldown", spellTemplate.getCooldown());
    node.set("name", spellTemplate.getName());
    Collection<CastingCost> costs = spellTemplate.getCosts();
    if (costs != null) {
        for (CastingCost cost : costs) {
            if (cost.getMana() > 0) {
                node.set(SkillSetting.MANA.node(), cost.getMana());
            }
        // TODO: Reagent costs from item costs
        }
    }
    // Check for an upgrade, set tier if present
    SpellKey upgradeKey = new SpellKey(spellTemplate.getSpellKey().getBaseKey(), 2);
    SpellTemplate upgrade = controller.getSpellTemplate(upgradeKey.getKey());
    if (upgrade != null) {
        int maxUpgrade = 2;
        while (upgrade != null) {
            upgradeKey = new SpellKey(spellTemplate.getSpellKey().getBaseKey(), maxUpgrade + 1);
            upgrade = controller.getSpellTemplate(upgradeKey.getKey());
            if (upgrade != null)
                maxUpgrade++;
        }
        node.set("tier", 1);
        node.set("tier-max", maxUpgrade);
    }
    return node;
}
Also used : CastingCost(com.elmakers.mine.bukkit.api.spell.CastingCost) MaterialAndData(com.elmakers.mine.bukkit.api.block.MaterialAndData) SpellKey(com.elmakers.mine.bukkit.api.spell.SpellKey) ConfigurationSection(org.bukkit.configuration.ConfigurationSection) SpellTemplate(com.elmakers.mine.bukkit.api.spell.SpellTemplate)

Example 3 with MaterialAndData

use of com.elmakers.mine.bukkit.api.block.MaterialAndData in project MagicPlugin by elBukkit.

the class Schematic method load.

@SuppressWarnings("deprecation")
public void load(short width, short height, short length, short[] blockTypes, byte[] data, Collection<Object> tileEntityData, Collection<Object> entityData, Vector origin, Vector offset) {
    size = new Vector(width, height, length);
    center = new Vector(Math.floor(size.getBlockX() / 2), 0, Math.floor(size.getBlockZ() / 2));
    blocks = new MaterialAndData[width][height][length];
    entities = new ArrayList<>();
    // Load entities
    for (Object entity : entityData) {
        String type = NMSUtils.getMetaString(entity, "id");
        Vector position = NMSUtils.getPosition(entity, "Pos");
        if (position == null)
            continue;
        position = position.subtract(origin).subtract(center);
        if (type == null)
            continue;
        // Only doing paintings and item frames for now.
        if (type.equals("Painting")) {
            String motive = NMSUtils.getMetaString(entity, "Motive");
            motive = motive.toLowerCase();
            Art art = Art.ALBAN;
            for (Art test : Art.values()) {
                if (test.name().toLowerCase().replace("_", "").equals(motive)) {
                    art = test;
                    break;
                }
            }
            byte facing = NMSUtils.getMetaByte(entity, "Facing");
            EntityData painting = com.elmakers.mine.bukkit.entity.EntityData.loadPainting(position, art, getFacing(facing));
            entities.add(painting);
        } else if (type.equals("ItemFrame")) {
            byte facing = NMSUtils.getMetaByte(entity, "Facing");
            byte rotation = NMSUtils.getMetaByte(entity, "ItemRotation");
            Rotation rot = Rotation.NONE;
            if (rotation < Rotation.values().length) {
                rot = Rotation.values()[rotation];
            }
            ItemStack item = NMSUtils.getItem(NMSUtils.getNode(entity, "Item"));
            EntityData itemFrame = com.elmakers.mine.bukkit.entity.EntityData.loadItemFrame(position, item, getFacing(facing), rot);
            entities.add(itemFrame);
        }
    }
    // Map tile entity data
    Map<BlockVector, Object> tileEntityMap = new HashMap<>();
    for (Object tileEntity : tileEntityData) {
        try {
            Integer x = NMSUtils.getMetaInt(tileEntity, "x");
            Integer y = NMSUtils.getMetaInt(tileEntity, "y");
            Integer z = NMSUtils.getMetaInt(tileEntity, "z");
            if (x == null || y == null || z == null)
                continue;
            BlockVector location = new BlockVector(x, y, z);
            tileEntityMap.put(location, tileEntity);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    for (int y = 0; y < height; y++) {
        for (int z = 0; z < length; z++) {
            for (int x = 0; x < width; x++) {
                int index = x + (y * length + z) * width;
                Material material = null;
                try {
                    material = Material.getMaterial(blockTypes[index]);
                } catch (Exception ex) {
                    material = null;
                    ex.printStackTrace();
                }
                if (material != null) {
                    MaterialAndData block = new com.elmakers.mine.bukkit.block.MaterialAndData(material, data[index]);
                    // Check for tile entity data
                    BlockVector blockLocation = new BlockVector(x, y, z);
                    Object tileEntity = tileEntityMap.get(blockLocation);
                    if (tileEntity != null) {
                        try {
                            if (material == Material.COMMAND) {
                                String customName = NMSUtils.getMetaString(tileEntity, "CustomName");
                                if (!customName.isEmpty()) {
                                    block.setCustomName(customName);
                                }
                                block.setCommandLine(NMSUtils.getMetaString(tileEntity, "Command"));
                            } else {
                                block.setRawData(tileEntity);
                            }
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                    blocks[x][y][z] = block;
                }
            }
        }
    }
    loaded = true;
}
Also used : Art(org.bukkit.Art) HashMap(java.util.HashMap) EntityData(com.elmakers.mine.bukkit.api.entity.EntityData) Material(org.bukkit.Material) Rotation(org.bukkit.Rotation) MaterialAndData(com.elmakers.mine.bukkit.api.block.MaterialAndData) BlockVector(org.bukkit.util.BlockVector) ItemStack(org.bukkit.inventory.ItemStack) BlockVector(org.bukkit.util.BlockVector) Vector(org.bukkit.util.Vector)

Example 4 with MaterialAndData

use of com.elmakers.mine.bukkit.api.block.MaterialAndData in project MagicPlugin by elBukkit.

the class WearAction method perform.

@Override
public SpellResult perform(CastContext context) {
    Entity entity = context.getTargetEntity();
    if (entity == null) {
        if (!context.getTargetsCaster())
            return SpellResult.NO_TARGET;
        entity = context.getEntity();
    }
    if (entity == null || !(entity instanceof Player)) {
        return SpellResult.NO_TARGET;
    }
    Player player = (Player) entity;
    MaterialAndData material = this.material;
    MageController controller = context.getController();
    Mage mage = controller.getMage(player);
    if (useItem) {
        Wand activeWand = mage.getActiveWand();
        // Not handling this for now.
        if (activeWand != context.getWand()) {
            return SpellResult.NO_TARGET;
        }
        if (activeWand != null) {
            activeWand.deactivate();
        }
        ItemStack itemInHand = player.getInventory().getItemInMainHand();
        if (itemInHand == null || itemInHand.getType() == Material.AIR) {
            return SpellResult.FAIL;
        }
        ItemStack[] armor = player.getInventory().getArmorContents();
        ItemStack currentItem = armor[slotNumber];
        armor[slotNumber] = itemInHand;
        player.getInventory().setArmorContents(armor);
        if (!InventoryUtils.isTemporary(currentItem)) {
            player.getInventory().setItemInMainHand(currentItem);
        } else {
            player.getInventory().setItemInMainHand(new ItemStack(Material.AIR));
        }
        if (mage instanceof com.elmakers.mine.bukkit.magic.Mage) {
            ((com.elmakers.mine.bukkit.magic.Mage) mage).armorUpdated();
        }
        return SpellResult.CAST;
    }
    ItemStack wearItem = null;
    String materialName = null;
    if (item == null) {
        if (material == null && (context.getSpell().usesBrush() || context.getSpell().hasBrushOverride())) {
            material = context.getBrush();
        }
        if (material == null) {
            Block targetBlock = context.getTargetBlock();
            if (targetBlock != null) {
                material = new com.elmakers.mine.bukkit.block.MaterialAndData(targetBlock);
                // Check for Banners with 1.7 support
                // TODO: this is outdated?
                Material baseMaterial = material.getMaterial();
                if (DeprecatedUtils.getId(baseMaterial) == 176 || DeprecatedUtils.getId(baseMaterial) == 177) {
                    ((com.elmakers.mine.bukkit.block.MaterialAndData) material).setMaterialId(425);
                }
            }
        }
        if (material == null || material.getMaterial() == Material.AIR) {
            return SpellResult.NO_TARGET;
        }
        wearItem = material.getItemStack(1);
        materialName = material.getName();
    } else {
        wearItem = InventoryUtils.getCopy(item);
        materialName = context.getController().describeItem(wearItem);
    }
    ItemMeta meta = wearItem.getItemMeta();
    // Legacy support
    String displayName = context.getMessage("hat_name", "");
    displayName = context.getMessage("wear_name", displayName);
    if (materialName == null || materialName.isEmpty()) {
        materialName = "?";
    }
    if (displayName != null && !displayName.isEmpty()) {
        meta.setDisplayName(displayName.replace("$hat", materialName).replace("$item", materialName));
    }
    List<String> lore = new ArrayList<>();
    String loreLine = context.getMessage("hat_lore");
    loreLine = context.getMessage("wear_lore", loreLine);
    lore.add(loreLine);
    meta.setLore(lore);
    wearItem.setItemMeta(meta);
    wearItem = InventoryUtils.makeReal(wearItem);
    NMSUtils.makeTemporary(wearItem, context.getMessage("removed").replace("$hat", materialName).replace("$item", materialName));
    if (enchantments != null) {
        wearItem.addUnsafeEnchantments(enchantments);
    }
    if (unbreakable) {
        CompatibilityUtils.makeUnbreakable(wearItem);
    }
    ItemStack[] armor = player.getInventory().getArmorContents();
    ItemStack itemStack = armor[slotNumber];
    if (itemStack != null && itemStack.getType() != Material.AIR) {
        if (NMSUtils.isTemporary(itemStack)) {
            ItemStack replacement = NMSUtils.getReplacement(itemStack);
            if (replacement != null) {
                itemStack = replacement;
            }
        }
        NMSUtils.setReplacement(wearItem, itemStack);
    }
    armor[slotNumber] = wearItem;
    player.getInventory().setArmorContents(armor);
    // Sanity check to make sure the block was allowed to be created
    armor = player.getInventory().getArmorContents();
    ItemStack helmetItem = armor[slotNumber];
    if (!NMSUtils.isTemporary(helmetItem)) {
        armor[slotNumber] = itemStack;
        player.getInventory().setArmorContents(armor);
        return SpellResult.NO_TARGET;
    }
    targetMage = mage;
    context.registerForUndo(new WearUndoAction());
    if (mage instanceof com.elmakers.mine.bukkit.magic.Mage) {
        ((com.elmakers.mine.bukkit.magic.Mage) mage).armorUpdated();
    }
    return SpellResult.CAST;
}
Also used : Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) ArrayList(java.util.ArrayList) Wand(com.elmakers.mine.bukkit.api.wand.Wand) Material(org.bukkit.Material) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Mage(com.elmakers.mine.bukkit.api.magic.Mage) MaterialAndData(com.elmakers.mine.bukkit.api.block.MaterialAndData) Block(org.bukkit.block.Block) ItemStack(org.bukkit.inventory.ItemStack) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 5 with MaterialAndData

use of com.elmakers.mine.bukkit.api.block.MaterialAndData in project MagicPlugin by elBukkit.

the class ThrowItemAction method start.

@Override
public SpellResult start(CastContext context) {
    Location spawnLocation = sourceLocation.getLocation(context);
    if (spawnLocation == null) {
        return SpellResult.NO_TARGET;
    }
    ItemStack itemStack = null;
    if (item != null) {
        itemStack = item.getItemStack(1);
    } else {
        MaterialAndData material = context.getBrush();
        if (material != null) {
            itemStack = new ItemStack(material.getMaterial(), 1, material.getData());
        }
    }
    if (itemStack == null) {
        return SpellResult.NO_TARGET;
    }
    double itemSpeed = context.getRandom().nextDouble() * (itemSpeedMax - itemSpeedMin) + itemSpeedMin;
    Vector velocity = spawnLocation.getDirection().normalize().multiply(itemSpeed);
    String removedMessage = context.getMessage("removed");
    if (removedMessage != null) {
        String name = context.getController().describeItem(itemStack);
        if (name == null) {
            name = "";
        }
        removedMessage = removedMessage.replace("$material", name);
    }
    NMSUtils.makeTemporary(itemStack, removedMessage);
    if (unbreakable) {
        itemStack = InventoryUtils.makeReal(itemStack);
        InventoryUtils.makeUnbreakable(itemStack);
    }
    Item droppedItem = null;
    try {
        droppedItem = spawnLocation.getWorld().dropItem(spawnLocation, itemStack);
    } catch (Exception ignored) {
    }
    if (droppedItem == null) {
        context.getMage().sendDebugMessage("Failed to spawn item of type " + itemStack.getType());
        return SpellResult.FAIL;
    }
    droppedItem.setMetadata("temporary", new FixedMetadataValue(context.getController().getPlugin(), true));
    CompatibilityUtils.ageItem(droppedItem, ageItems);
    SafetyUtils.setVelocity(droppedItem, velocity);
    track(context, droppedItem);
    return checkTracking(context);
}
Also used : Item(org.bukkit.entity.Item) MaterialAndData(com.elmakers.mine.bukkit.api.block.MaterialAndData) FixedMetadataValue(org.bukkit.metadata.FixedMetadataValue) ItemStack(org.bukkit.inventory.ItemStack) Vector(org.bukkit.util.Vector) Location(org.bukkit.Location) SourceLocation(com.elmakers.mine.bukkit.magic.SourceLocation)

Aggregations

MaterialAndData (com.elmakers.mine.bukkit.api.block.MaterialAndData)7 ItemStack (org.bukkit.inventory.ItemStack)5 Player (org.bukkit.entity.Player)4 ArrayList (java.util.ArrayList)3 Material (org.bukkit.Material)3 Mage (com.elmakers.mine.bukkit.api.magic.Mage)2 MageController (com.elmakers.mine.bukkit.api.magic.MageController)2 CastingCost (com.elmakers.mine.bukkit.api.spell.CastingCost)2 SpellTemplate (com.elmakers.mine.bukkit.api.spell.SpellTemplate)2 Location (org.bukkit.Location)2 Vector (org.bukkit.util.Vector)2 EntityData (com.elmakers.mine.bukkit.api.entity.EntityData)1 CastSourceLocation (com.elmakers.mine.bukkit.api.magic.CastSourceLocation)1 MageSpell (com.elmakers.mine.bukkit.api.spell.MageSpell)1 Spell (com.elmakers.mine.bukkit.api.spell.Spell)1 SpellCategory (com.elmakers.mine.bukkit.api.spell.SpellCategory)1 SpellKey (com.elmakers.mine.bukkit.api.spell.SpellKey)1 Wand (com.elmakers.mine.bukkit.api.wand.Wand)1 HeroesManager (com.elmakers.mine.bukkit.heroes.HeroesManager)1 MagicController (com.elmakers.mine.bukkit.magic.MagicController)1