Search in sources :

Example 41 with Material

use of org.bukkit.Material in project MagicPlugin by elBukkit.

the class CraftingController method onPrepareCraftItem.

@EventHandler
public void onPrepareCraftItem(PrepareItemCraftEvent event) {
    CraftingInventory inventory = event.getInventory();
    ItemStack[] contents = inventory.getMatrix();
    // Check for wands glitched into the crafting inventory
    for (int i = 0; i < 9 && i < contents.length; i++) {
        ItemStack item = contents[i];
        if (Wand.isSpecial(item)) {
            inventory.setResult(new ItemStack(Material.AIR));
            return;
        }
    }
    if (!craftingEnabled)
        return;
    Recipe recipe = event.getRecipe();
    if (recipe == null)
        return;
    ItemStack result = recipe.getResult();
    if (result == null)
        return;
    Material resultType = result.getType();
    List<MagicRecipe> candidates = recipes.get(resultType);
    if (candidates == null || candidates.size() == 0)
        return;
    for (MagicRecipe candidate : candidates) {
        MagicRecipe.MatchType matchType = candidate.getMatchType(contents);
        Material substitute = candidate.getSubstitute();
        if (matchType != MagicRecipe.MatchType.NONE) {
            for (HumanEntity human : event.getViewers()) {
                if (human instanceof Player && (matchType == MagicRecipe.MatchType.PARTIAL || !hasCraftPermission((Player) human, candidate))) {
                    inventory.setResult(new ItemStack(Material.AIR));
                    return;
                }
            }
            if (matchType == MagicRecipe.MatchType.MATCH) {
                ItemStack crafted = candidate.craft();
                inventory.setResult(crafted);
            }
            break;
        } else if (substitute != null) {
            inventory.setResult(new ItemStack(substitute, 1));
        }
    }
}
Also used : CraftingInventory(org.bukkit.inventory.CraftingInventory) Player(org.bukkit.entity.Player) Recipe(org.bukkit.inventory.Recipe) MagicRecipe(com.elmakers.mine.bukkit.magic.MagicRecipe) HumanEntity(org.bukkit.entity.HumanEntity) Material(org.bukkit.Material) ItemStack(org.bukkit.inventory.ItemStack) MagicRecipe(com.elmakers.mine.bukkit.magic.MagicRecipe) EventHandler(org.bukkit.event.EventHandler)

Example 42 with Material

use of org.bukkit.Material in project MagicPlugin by elBukkit.

the class BaseSpell method checkForHalfBlock.

protected Location checkForHalfBlock(Location location) {
    // This is a hack, but data-driving would be a pain.
    boolean isHalfBlock = false;
    Block downBlock = location.getBlock().getRelative(BlockFace.DOWN);
    Material material = downBlock.getType();
    if (material == Material.STEP || material == Material.WOOD_STEP) {
        // Drop down to half-steps
        isHalfBlock = (DeprecatedUtils.getData(downBlock) < 8);
    } else {
        isHalfBlock = isHalfBlock(material);
    }
    if (isHalfBlock) {
        location.setY(location.getY() - 0.5);
    }
    return location;
}
Also used : Block(org.bukkit.block.Block) Material(org.bukkit.Material)

Example 43 with Material

use of org.bukkit.Material 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 44 with Material

use of org.bukkit.Material in project MagicPlugin by elBukkit.

the class PlayerController method onPlayerInteract.

@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerInteract(PlayerInteractEvent event) {
    if (!controller.isLoaded())
        return;
    Action action = event.getAction();
    boolean isLeftClick = action == Action.LEFT_CLICK_AIR || action == Action.LEFT_CLICK_BLOCK;
    boolean isRightClick = (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK);
    // We only care about left click and right click.
    if (!isLeftClick && !isRightClick)
        return;
    // Note that an interact on air event will arrive pre-cancelled
    // So this is kind of useless. :\
    // if (event.isCancelled()) return;
    // Block block = event.getClickedBlock();
    // controller.getLogger().info("INTERACT: " + event.getAction() + " on " + (block == null ? "NOTHING" : block.getType()) + " cancelled: " + event.isCancelled());
    Player player = event.getPlayer();
    // Don't allow interacting while holding spells, brushes or upgrades
    ItemStack itemInHand = player.getInventory().getItemInMainHand();
    boolean isSkill = Wand.isSkill(itemInHand);
    boolean isSpell = !isSkill && Wand.isSpell(itemInHand);
    if (isSpell || Wand.isBrush(itemInHand) || Wand.isUpgrade(itemInHand)) {
        event.setCancelled(true);
        return;
    }
    boolean isOffhandSkill = false;
    ItemStack itemInOffhand = player.getInventory().getItemInOffHand();
    if (isRightClick) {
        isOffhandSkill = Wand.isSkill(itemInOffhand);
        boolean isOffhandSpell = !isOffhandSkill && Wand.isSpell(itemInOffhand);
        if (isOffhandSpell || Wand.isBrush(itemInOffhand) || Wand.isUpgrade(itemInOffhand)) {
            event.setCancelled(true);
            return;
        }
    }
    Mage mage = controller.getMage(player);
    Wand wand = mage.checkWand();
    if (action == Action.RIGHT_CLICK_BLOCK) {
        Material material = event.getClickedBlock().getType();
        isRightClick = !controller.isInteractable(event.getClickedBlock());
        // This is to prevent Essentials signs from giving you an item in your wand inventory.
        if (wand != null && (material == Material.SIGN_POST || material == Material.WALL_SIGN)) {
            wand.closeInventory();
        }
    }
    if (!isLeftClick && !mage.checkLastClick(clickCooldown)) {
        return;
    }
    // Prefer wand right-click if wand is active
    if (isOffhandSkill && wand != null) {
        if (wand.getRightClickAction() != WandAction.NONE) {
            isOffhandSkill = false;
        }
    }
    if (isRightClick && (isOffhandSkill || isSkill)) {
        if (isSkill) {
            mage.useSkill(itemInHand);
        } else {
            mage.useSkill(itemInOffhand);
        }
        event.setCancelled(true);
        return;
    }
    // Check for offhand casting
    if (isRightClick) {
        if (allowOffhandCasting && mage.offhandCast()) {
            // which in the offhand case are right-click actions.
            if (cancelInteractOnLeftClick) {
                event.setCancelled(true);
            }
            return;
        }
    }
    // Special-case here for skulls, which actually are not wearable via right-click.
    if (itemInHand != null && isRightClick && controller.isWearable(itemInHand) && itemInHand.getType() != Material.SKULL_ITEM) {
        if (wand != null) {
            wand.deactivate();
        }
        controller.onArmorUpdated(mage);
        return;
    }
    if (wand == null)
        return;
    Messages messages = controller.getMessages();
    if (!controller.hasWandPermission(player)) {
        return;
    }
    // Check for region or wand-specific permissions
    if (!controller.hasWandPermission(player, wand)) {
        wand.deactivate();
        mage.sendMessage(messages.get("wand.no_permission").replace("$wand", wand.getName()));
        return;
    }
    // Check for enchantment table click
    Block clickedBlock = event.getClickedBlock();
    if (clickedBlock != null && clickedBlock.getType() != Material.AIR && enchantBlockMaterial != null && enchantBlockMaterial.is(clickedBlock)) {
        Spell spell = null;
        if (player.isSneaking()) {
            spell = enchantSneakClickSpell != null ? mage.getSpell(enchantSneakClickSpell) : null;
        } else {
            spell = enchantClickSpell != null ? mage.getSpell(enchantClickSpell) : null;
        }
        if (spell != null) {
            spell.cast();
            event.setCancelled(true);
            return;
        }
    }
    if (isLeftClick && !wand.isUpgrade() && wand.getLeftClickAction() != WandAction.NONE && cancelInteractOnLeftClick) {
        event.setCancelled(true);
    }
    if (isRightClick && wand.performAction(wand.getRightClickAction()) && cancelInteractOnRightClick) {
        event.setCancelled(true);
    }
}
Also used : Action(org.bukkit.event.block.Action) WandAction(com.elmakers.mine.bukkit.api.wand.WandAction) Player(org.bukkit.entity.Player) Messages(com.elmakers.mine.bukkit.api.magic.Messages) Mage(com.elmakers.mine.bukkit.magic.Mage) Wand(com.elmakers.mine.bukkit.wand.Wand) Block(org.bukkit.block.Block) Material(org.bukkit.Material) ItemStack(org.bukkit.inventory.ItemStack) Spell(com.elmakers.mine.bukkit.api.spell.Spell) EventHandler(org.bukkit.event.EventHandler)

Example 45 with Material

use of org.bukkit.Material 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)

Aggregations

Material (org.bukkit.Material)427 ItemStack (org.bukkit.inventory.ItemStack)99 Block (org.bukkit.block.Block)87 EventHandler (org.bukkit.event.EventHandler)51 ArrayList (java.util.ArrayList)46 Player (org.bukkit.entity.Player)44 Location (org.bukkit.Location)42 Vector (org.bukkit.util.Vector)27 EntityType (org.bukkit.entity.EntityType)26 GlowBlock (net.glowstone.block.GlowBlock)24 Test (org.junit.Test)24 BlockFace (org.bukkit.block.BlockFace)23 Entity (org.bukkit.entity.Entity)22 MaterialData (org.bukkit.material.MaterialData)22 World (org.bukkit.World)20 HashMap (java.util.HashMap)19 IOException (java.io.IOException)17 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)16 File (java.io.File)15 HashSet (java.util.HashSet)13