Search in sources :

Example 1 with MaterialAndData

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

the class MagicItemCommandExecutor method onItemExport.

public boolean onItemExport(Player player, ItemStack item, String[] parameters) {
    if (parameters.length == 0) {
        player.sendMessage(ChatColor.RED + "Usage: /mitem export filename");
        return true;
    }
    PlayerInventory inventory = player.getInventory();
    int itemSlot = inventory.getHeldItemSlot();
    Map<String, MaterialAndData> items = new TreeMap<>();
    VaultController vault = VaultController.getInstance();
    for (Material material : Material.values()) {
        ItemStack testItem = new ItemStack(material, 1);
        inventory.setItem(itemSlot, testItem);
        ItemStack setItem = inventory.getItem(itemSlot);
        if (setItem == null || setItem.getType() != testItem.getType()) {
            player.sendMessage("Skipped: " + material.name());
            continue;
        }
        MaterialAndData mat = new MaterialAndData(material);
        items.put(mat.getKey(), mat);
        String baseName = mat.getName();
        for (short data = 1; data < 32; data++) {
            testItem = new ItemStack(material, 1, data);
            inventory.setItem(itemSlot, testItem);
            setItem = inventory.getItem(itemSlot);
            if (setItem == null || setItem.getType() != testItem.getType() || setItem.getDurability() != testItem.getDurability())
                break;
            mat = new MaterialAndData(material, data);
            if (mat.getName().equals(baseName))
                break;
            String testVaultName = vault == null ? null : vault.getItemName(material, data);
            if (testVaultName == null || testVaultName.isEmpty())
                break;
            items.put(mat.getKey(), mat);
        }
    }
    File file = new File(api.getPlugin().getDataFolder(), parameters[0] + ".csv");
    try (Writer output = new OutputStreamWriter(new FileOutputStream(file), "UTF-8")) {
        output.append("Name,Key,Cost\n");
        for (MaterialAndData material : items.values()) {
            Double worth = api.getController().getWorth(material.getItemStack(1));
            String worthString = worth == null ? "" : worth.toString();
            output.append(material.getName() + "," + material.getKey() + "," + worthString + "\n");
        }
    } catch (Exception ex) {
        player.sendMessage(ChatColor.RED + "Error exporting data: " + ex.getMessage());
        ex.printStackTrace();
    }
    inventory.setItem(itemSlot, item);
    return true;
}
Also used : VaultController(com.elmakers.mine.bukkit.integration.VaultController) Material(org.bukkit.Material) PlayerInventory(org.bukkit.inventory.PlayerInventory) TreeMap(java.util.TreeMap) IOException(java.io.IOException) MaterialAndData(com.elmakers.mine.bukkit.block.MaterialAndData) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) ItemStack(org.bukkit.inventory.ItemStack) File(java.io.File) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer)

Example 2 with MaterialAndData

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

the class ConstructSpell method onCast.

@Override
public SpellResult onCast(ConfigurationSection parameters) {
    Block target = null;
    boolean isSelect = getTargetType() == TargetType.SELECT;
    boolean finalCast = !isSelect || this.targetBlock != null;
    if (finalCast && parameters.getBoolean("select_self", true) && isLookingDown()) {
        target = mage.getLocation().getBlock().getRelative(BlockFace.DOWN);
    } else {
        Target t = getTarget();
        target = t.getBlock();
    }
    if (target == null) {
        return SpellResult.NO_TARGET;
    }
    MaterialBrush buildWith = getBrush();
    boolean hasPermission = buildWith != null && buildWith.isErase() ? hasBreakPermission(target) : hasBuildPermission(target);
    if (!hasPermission) {
        return SpellResult.INSUFFICIENT_PERMISSION;
    }
    int radius = parameters.getInt("radius", DEFAULT_RADIUS);
    radius = parameters.getInt("r", radius);
    radius = parameters.getInt("size", radius);
    boolean falling = parameters.getBoolean("falling", false);
    boolean physics = parameters.getBoolean("physics", false);
    boolean commit = parameters.getBoolean("commit", false);
    boolean consume = parameters.getBoolean("consume", false);
    double breakable = parameters.getDouble("breakable", 0);
    double backfireChance = parameters.getDouble("reflect_chance", 0);
    Vector orientTo = null;
    Vector bounds = null;
    if (parameters.getBoolean("use_brush_size", false)) {
        if (!buildWith.isReady()) {
            long timeout = System.currentTimeMillis() + 10000;
            while (System.currentTimeMillis() < timeout) {
                try {
                    Thread.sleep(500);
                    if (buildWith.isReady()) {
                        break;
                    }
                } catch (InterruptedException ex) {
                    break;
                }
            }
            if (!buildWith.isReady()) {
                return SpellResult.NO_ACTION;
            }
        }
        bounds = buildWith.getSize();
        radius = (int) Math.max(Math.max(bounds.getX() / 2, bounds.getZ() / 2), bounds.getY());
    } else if (isSelect) {
        if (targetLocation2 != null) {
            this.targetBlock = targetLocation2.getBlock();
        }
        if (targetBlock == null || !targetBlock.getWorld().equals(target.getWorld())) {
            targetBlock = target;
            activate();
            return SpellResult.TARGET_SELECTED;
        } else {
            radius = (int) targetBlock.getLocation().distance(target.getLocation());
            if (parameters.getBoolean("orient")) {
                orientTo = target.getLocation().toVector().subtract(targetBlock.getLocation().toVector());
                orientTo.setX(Math.abs(orientTo.getX()));
                orientTo.setY(Math.abs(orientTo.getY()));
                orientTo.setZ(Math.abs(orientTo.getZ()));
                if (orientTo.getX() < orientTo.getZ() && orientTo.getX() < orientTo.getY()) {
                    orientTo = new Vector(1, 0, 0);
                } else if (orientTo.getZ() < orientTo.getX() && orientTo.getZ() < orientTo.getY()) {
                    orientTo = new Vector(0, 0, 1);
                } else {
                    orientTo = new Vector(0, 1, 0);
                }
            }
            target = targetBlock;
            targetBlock = null;
        }
    } else if (parameters.getBoolean("orient")) {
        // orientTo = mage.getLocation().toVector().crossProduct(target.getLocation().toVector());
        orientTo = mage.getLocation().toVector().subtract(target.getLocation().toVector());
        orientTo.setX(Math.abs(orientTo.getX()));
        orientTo.setY(Math.abs(orientTo.getY()));
        orientTo.setZ(Math.abs(orientTo.getZ()));
        if (orientTo.getX() > orientTo.getZ() && orientTo.getX() > orientTo.getY()) {
            orientTo = new Vector(1, 0, 0);
        } else if (orientTo.getZ() > orientTo.getX() && orientTo.getZ() > orientTo.getY()) {
            orientTo = new Vector(0, 0, 1);
        } else {
            orientTo = new Vector(0, 1, 0);
        }
    }
    if (!parameters.contains("radius")) {
        int maxDimension = parameters.getInt("max_dimension", DEFAULT_MAX_DIMENSION);
        maxDimension = parameters.getInt("md", maxDimension);
        maxDimension = (int) (mage.getConstructionMultiplier() * maxDimension);
        int diameter = radius * 2;
        if (diameter > maxDimension) {
            return SpellResult.FAIL;
        }
    }
    // TODO : Is this needed? Or just use "ty"?
    if (parameters.contains("y_offset")) {
        target = target.getRelative(BlockFace.UP, parameters.getInt("y_offset", 0));
    }
    buildWith.setTarget(target.getLocation());
    ConstructionType conType = DEFAULT_CONSTRUCTION_TYPE;
    int thickness = parameters.getInt("thickness", 0);
    String typeString = parameters.getString("type", "");
    ConstructionType testType = ConstructionType.parseString(typeString, ConstructionType.UNKNOWN);
    if (testType != ConstructionType.UNKNOWN) {
        conType = testType;
    }
    ConstructBatch batch = new ConstructBatch(this, target.getLocation(), conType, radius, thickness, falling, orientTo);
    batch.setCommit(commit);
    batch.setConsume(consume);
    UndoList undoList = getUndoList();
    if (undoList != null && !currentCast.isConsumeFree()) {
        undoList.setConsumed(consume);
    }
    if (parameters.getBoolean("replace", false)) {
        List<com.elmakers.mine.bukkit.api.block.MaterialAndData> replaceMaterials = new ArrayList<>();
        MaterialAndData wildReplace = new MaterialAndData(target);
        if (!parameters.getBoolean("match_data", true)) {
            wildReplace.setData(null);
        }
        // Hacky, but generally desired - maybe abstract to a parameterized list?
        Material targetMaterial = target.getType();
        if (targetMaterial == Material.STATIONARY_WATER || targetMaterial == Material.WATER || targetMaterial == Material.STATIONARY_LAVA || targetMaterial == Material.LAVA) {
            wildReplace.setData(null);
        }
        replaceMaterials.add(wildReplace);
        batch.setReplace(replaceMaterials);
    }
    // Check for command block overrides
    if (parameters.contains("commands")) {
        ConfigurationSection commandMap = parameters.getConfigurationSection("commands");
        Set<String> keys = commandMap.getKeys(false);
        for (String key : keys) {
            batch.addCommandMapping(key, commandMap.getString(key));
        }
    }
    if (falling) {
        float force = (float) parameters.getDouble("speed", 0);
        batch.setFallingDirection(ConfigurationUtils.getVector(parameters, "falling_direction"));
        batch.setFallingBlockSpeed(force);
    }
    batch.setApplyPhysics(physics);
    if (breakable > 0) {
        batch.setBreakable(breakable);
    }
    if (backfireChance > 0) {
        batch.setBackfireChance(backfireChance);
    }
    if (parameters.contains("orient_dimension_max")) {
        batch.setOrientDimensionMax(parameters.getInt("orient_dimension_max"));
    } else if (parameters.contains("odmax")) {
        batch.setOrientDimensionMax(parameters.getInt("odmax"));
    }
    if (parameters.contains("orient_dimension_min")) {
        batch.setOrientDimensionMin(parameters.getInt("orient_dimension_min"));
    } else if (parameters.contains("odmin")) {
        batch.setOrientDimensionMin(parameters.getInt("odmin"));
    }
    if (parameters.getBoolean("power", false)) {
        batch.setPower(true);
    }
    if (bounds != null) {
        batch.setBounds(bounds);
        batch.setOrientDimensionMin(0);
    }
    boolean success = mage.addBatch(batch);
    deactivate();
    return success ? SpellResult.CAST : SpellResult.FAIL;
}
Also used : ConstructBatch(com.elmakers.mine.bukkit.batch.ConstructBatch) ConstructionType(com.elmakers.mine.bukkit.block.ConstructionType) ArrayList(java.util.ArrayList) Material(org.bukkit.Material) Target(com.elmakers.mine.bukkit.utility.Target) MaterialBrush(com.elmakers.mine.bukkit.api.block.MaterialBrush) UndoList(com.elmakers.mine.bukkit.block.UndoList) MaterialAndData(com.elmakers.mine.bukkit.block.MaterialAndData) Block(org.bukkit.block.Block) Vector(org.bukkit.util.Vector) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 3 with MaterialAndData

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

the class AnimateSpell method onCast.

@Override
public SpellResult onCast(ConfigurationSection parameters) {
    if (parameters.getString("animate", null) != null) {
        return super.onCast(parameters);
    }
    final Block targetBlock = getTargetBlock();
    if (targetBlock == null) {
        return SpellResult.NO_TARGET;
    }
    if (!hasBuildPermission(targetBlock)) {
        return SpellResult.INSUFFICIENT_PERMISSION;
    }
    int seedRadius = parameters.getInt("seed_radius", 0);
    MaterialAndData targetMaterial = new MaterialAndData(targetBlock);
    List<String> materials = ConfigurationUtils.getStringList(parameters, "materials");
    if (seedRadius > 0 && materials != null && !materials.isEmpty()) {
        targetMaterial = new MaterialAndData(RandomUtils.getRandom(materials));
    } else if (parameters.contains("material")) {
        targetMaterial = ConfigurationUtils.getMaterialAndData(parameters, "material", targetMaterial);
        if (targetMaterial.isValid()) {
            addDestructible(targetMaterial);
        }
    }
    if (!mage.isSuperPowered() && seedRadius == 0) {
        MaterialSetManager materialSets = controller.getMaterialSetManager();
        MaterialSet restricted = materialSets.getMaterialSet("restricted");
        if (restricted != null && restricted.testMaterialAndData(targetMaterial)) {
            return SpellResult.FAIL;
        }
        if (parameters.contains("restricted")) {
            String customRestricted = parameters.getString("restricted");
            if (customRestricted != null && !customRestricted.equals("restricted")) {
                restricted = materialSets.fromConfigEmpty(customRestricted);
                if (restricted.testMaterialAndData(targetMaterial)) {
                    return SpellResult.FAIL;
                }
            }
        }
    }
    if (!isDestructible(targetBlock)) {
        return SpellResult.INSUFFICIENT_PERMISSION;
    }
    registerForUndo(targetBlock);
    if (seedRadius > 0) {
        for (int dx = -seedRadius; dx < seedRadius; dx++) {
            for (int dz = -seedRadius; dz < seedRadius; dz++) {
                for (int dy = -seedRadius; dy < seedRadius; dy++) {
                    Block seedBlock = targetBlock.getRelative(dx, dy, dz);
                    if (isDestructible(seedBlock)) {
                        registerForUndo(seedBlock);
                        targetMaterial.modify(seedBlock);
                    }
                }
            }
        }
    }
    // Look for randomized levels
    int level = 0;
    if (parameters.contains("level")) {
        level = parameters.getInt("level", level);
    } else if (levelWeights != null) {
        level = RandomUtils.weightedRandom(levelWeights);
    }
    boolean simCheckDestructible = parameters.getBoolean("sim_check_destructible", true);
    simCheckDestructible = parameters.getBoolean("scd", simCheckDestructible);
    final ConfigurationSection automataParameters = new MemoryConfiguration();
    automataParameters.set("target", "self");
    automataParameters.set("cooldown", 0);
    automataParameters.set("m", targetMaterial.getKey());
    automataParameters.set("cd", (simCheckDestructible ? true : false));
    automataParameters.set("level", level);
    String automataName = parameters.getString("name", "Automata");
    Messages messages = controller.getMessages();
    String automataType = parameters.getString("message_type", "evil");
    List<String> prefixes = messages.getAll("automata." + automataType + ".prefixes");
    List<String> suffixes = messages.getAll("automata." + automataType + ".suffixes");
    automataName = prefixes.get(random.nextInt(prefixes.size())) + " " + automataName + " " + suffixes.get(random.nextInt(suffixes.size()));
    if (level > 1) {
        automataName += " " + escapeLevel(messages, "automata.level", level);
    }
    String message = getMessage("cast_broadcast").replace("$name", automataName);
    if (message.length() > 0) {
        controller.sendToMages(message, targetBlock.getLocation());
    }
    automataParameters.set("animate", automataName);
    String automataId = UUID.randomUUID().toString();
    final Mage mage = controller.getAutomaton(automataId, automataName);
    mage.setLocation(targetBlock.getLocation());
    mage.setQuiet(true);
    mage.addTag(getKey());
    final Spell spell = mage.getSpell(getKey());
    Bukkit.getScheduler().runTaskLater(controller.getPlugin(), new Runnable() {

        @Override
        public void run() {
            spell.cast(automataParameters);
        }
    }, 1);
    registerForUndo();
    return SpellResult.CAST;
}
Also used : Messages(com.elmakers.mine.bukkit.api.magic.Messages) MaterialSet(com.elmakers.mine.bukkit.api.magic.MaterialSet) MemoryConfiguration(org.bukkit.configuration.MemoryConfiguration) Spell(com.elmakers.mine.bukkit.api.spell.Spell) Mage(com.elmakers.mine.bukkit.api.magic.Mage) MaterialAndData(com.elmakers.mine.bukkit.block.MaterialAndData) Block(org.bukkit.block.Block) MaterialSetManager(com.elmakers.mine.bukkit.api.magic.MaterialSetManager) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 4 with MaterialAndData

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

the class MagicController method getItemKey.

@Override
public String getItemKey(ItemStack item) {
    if (item == null) {
        return "";
    }
    if (Wand.isUpgrade(item)) {
        return "upgrade:" + Wand.getWandTemplate(item);
    }
    if (Wand.isWand(item)) {
        return "wand:" + Wand.getWandTemplate(item);
    }
    if (Wand.isSpell(item)) {
        return "spell:" + Wand.getSpell(item);
    }
    if (Wand.isBrush(item)) {
        return "brush:" + Wand.getBrush(item);
    }
    ItemData mappedItem = getItem(item);
    if (mappedItem != null) {
        return mappedItem.getKey();
    }
    if (item.getType() == Material.SKULL_ITEM) {
        String url = InventoryUtils.getSkullURL(item);
        if (url != null && url.length() > 0) {
            return "skull_item:" + url;
        }
    }
    MaterialAndData material = new MaterialAndData(item);
    return material.getKey();
}
Also used : MaterialAndData(com.elmakers.mine.bukkit.block.MaterialAndData) ItemData(com.elmakers.mine.bukkit.api.item.ItemData)

Example 5 with MaterialAndData

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

the class MagicController method getEntityName.

protected String getEntityName(Entity target, boolean display) {
    if (target == null) {
        return "Unknown";
    }
    if (target instanceof Player) {
        return display ? ((Player) target).getDisplayName() : ((Player) target).getName();
    }
    if (isElemental(target)) {
        return "Elemental";
    }
    if (display) {
        if (target instanceof LivingEntity) {
            LivingEntity li = (LivingEntity) target;
            String customName = li.getCustomName();
            if (customName != null && customName.length() > 0) {
                return customName;
            }
        } else if (target instanceof Item) {
            Item item = (Item) target;
            ItemStack itemStack = item.getItemStack();
            if (itemStack.hasItemMeta()) {
                ItemMeta meta = itemStack.getItemMeta();
                if (meta.hasDisplayName()) {
                    return meta.getDisplayName();
                }
            }
            MaterialAndData material = new MaterialAndData(itemStack);
            return material.getName();
        }
    }
    return target.getType().name().toLowerCase().replace('_', ' ');
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) CurrencyItem(com.elmakers.mine.bukkit.api.block.CurrencyItem) Item(org.bukkit.entity.Item) EffectPlayer(com.elmakers.mine.bukkit.effect.EffectPlayer) Player(org.bukkit.entity.Player) MaterialAndData(com.elmakers.mine.bukkit.block.MaterialAndData) ItemStack(org.bukkit.inventory.ItemStack) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Aggregations

MaterialAndData (com.elmakers.mine.bukkit.block.MaterialAndData)47 Block (org.bukkit.block.Block)20 ItemStack (org.bukkit.inventory.ItemStack)11 ArrayList (java.util.ArrayList)10 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)10 Material (org.bukkit.Material)8 Nullable (javax.annotation.Nullable)7 ItemMeta (org.bukkit.inventory.meta.ItemMeta)7 Mage (com.elmakers.mine.bukkit.api.magic.Mage)5 Target (com.elmakers.mine.bukkit.utility.Target)5 Player (org.bukkit.entity.Player)5 MageController (com.elmakers.mine.bukkit.api.magic.MageController)4 Wand (com.elmakers.mine.bukkit.api.wand.Wand)4 List (java.util.List)4 ItemData (com.elmakers.mine.bukkit.api.item.ItemData)3 MaterialSet (com.elmakers.mine.bukkit.api.magic.MaterialSet)3 IOException (java.io.IOException)3 Location (org.bukkit.Location)3 CurrencyItem (com.elmakers.mine.bukkit.api.block.CurrencyItem)2 ParseException (java.text.ParseException)2