Search in sources :

Example 1 with ToolType

use of net.glowstone.inventory.ToolType in project Glowstone by GlowstoneMC.

the class GlowPlayer method setDigging.

/**
 * Starts breaking a block.
 *
 * @param block the block to start breaking
 */
public void setDigging(GlowBlock block) {
    if (Objects.equals(block, digging)) {
        return;
    }
    if (block == null) {
        totalDiggingTicks = Long.MAX_VALUE;
        // remove the animation
        broadcastBlockBreakAnimation(digging, 10);
    } else {
        double hardness = block.getMaterialValues().getHardness();
        if (hardness >= Float.MAX_VALUE) {
            // This block can't be broken by digging.
            setDigging(null);
            return;
        }
        // default of 5 when using bare hands
        double breakingTimeMultiplier = 5;
        ItemStack tool = getItemInHand();
        if (tool != null) {
            Material toolType = tool.getType();
            if (block.getType() == Material.COBWEB && ToolType.SWORD.matches(toolType)) {
                breakingTimeMultiplier = 0.1;
            } else if (Tag.WOOL.isTagged(block.getType()) && toolType == Material.SHEARS) {
                breakingTimeMultiplier = 0.3;
            } else {
                ToolType effectiveTool = block.getMaterialValues().getTool();
                if (effectiveTool != null && effectiveTool.matches(toolType)) {
                    double miningMultiplier = ToolType.getMiningMultiplier(toolType);
                    int efficiencyLevel = tool.getEnchantmentLevel(Enchantment.DIG_SPEED);
                    if (efficiencyLevel > 0) {
                        miningMultiplier += efficiencyLevel * efficiencyLevel + 1;
                    }
                    breakingTimeMultiplier = 1.5 / miningMultiplier;
                } else if (effectiveTool == null || !effectiveTool.matches(Material.DIAMOND_PICKAXE)) {
                    // If the current tool isn't optimal but can still mine the block, the
                    // multiplier is 1.5. Here, we assume for simplicity that this is true of
                    // all non-pickaxe blocks.
                    // FIXME: Does this always match vanilla?
                    breakingTimeMultiplier = 1.5;
                }
            }
        }
        // TODO: status effects (e.g. Mining Fatigue, Slowness); effect of underwater digging
        totalDiggingTicks = (long) // seconds to ticks, round half-up
        (breakingTimeMultiplier * hardness * 20.0 + 0.5);
        // show other clients the block is beginning to crack
        broadcastBlockBreakAnimation(block, 0);
    }
    diggingTicks = 0;
    digging = block;
}
Also used : ToolType(net.glowstone.inventory.ToolType) Material(org.bukkit.Material) ItemStack(org.bukkit.inventory.ItemStack)

Aggregations

ToolType (net.glowstone.inventory.ToolType)1 Material (org.bukkit.Material)1 ItemStack (org.bukkit.inventory.ItemStack)1