Account for unbreaking enchantment

This commit is contained in:
Esophose 2019-02-15 23:29:58 -07:00
parent 319298968f
commit 9610cebb39
2 changed files with 55 additions and 27 deletions

View File

@ -39,7 +39,7 @@ public class DefaultConfig {
Configuration configuration = plugin.getConfig();
configuration.addDefault(MAX_BRANCH_BLOCKS, 100);
configuration.addDefault(MAX_BRANCH_BLOCKS, 120);
configuration.addDefault(LEAVES_FOR_TREE, 5);
configuration.addDefault(ONLY_BREAK_LOGS_UPWARDS, true);
configuration.addDefault(ALLOW_MIXED_TREE_TYPES, false);

View File

@ -1,7 +1,11 @@
package com.songoda.ultimatetimber.treefall;
import java.util.HashSet;
import java.util.Random;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
@ -9,47 +13,71 @@ import org.bukkit.inventory.meta.ItemMeta;
import com.songoda.ultimatetimber.utils.WoodToLogConverter;
import java.util.HashSet;
public class AxeDurability {
private static Random random = new Random();
class AxeDurability {
/*
This class handles all durability damage dealt to the axe used to chop down the tree, only takes into account
wood blocks chopped down
/**
* Applies damage to the axe the player is holding based on logs they broke
*
* @param blocks The blocks that are part of the tree
* @param player The player
*/
static void adjustAxeDamage(HashSet<Block> blocks, Player player) {
public static void adjustAxeDamage(HashSet<Block> blocks, Player player) {
ItemStack item = player.getInventory().getItemInMainHand();
if (!(item.getType().equals(Material.DIAMOND_AXE) ||
item.getType().equals(Material.GOLDEN_AXE) ||
item.getType().equals(Material.IRON_AXE) ||
item.getType().equals(Material.STONE_AXE) ||
item.getType().equals(Material.WOODEN_AXE))) return;
Material itemType = item.getType();
if (!(itemType.equals(Material.DIAMOND_AXE) ||
itemType.equals(Material.GOLDEN_AXE) ||
itemType.equals(Material.IRON_AXE) ||
itemType.equals(Material.STONE_AXE) ||
itemType.equals(Material.WOODEN_AXE))) return;
int unbreakingLevel = item.getEnchantmentLevel(Enchantment.DURABILITY);
ItemMeta itemMeta = item.getItemMeta();
Damageable damageableMeta = (Damageable) itemMeta;
for (Block block : blocks) {
Material material = WoodToLogConverter.convert(block.getType());
if (material.equals(Material.ACACIA_LOG) ||
material.equals(Material.BIRCH_LOG) ||
material.equals(Material.DARK_OAK_LOG) ||
material.equals(Material.JUNGLE_LOG) ||
material.equals(Material.OAK_LOG) ||
material.equals(Material.SPRUCE_LOG) ||
material.equals(Material.STRIPPED_ACACIA_LOG) ||
material.equals(Material.STRIPPED_BIRCH_LOG) ||
material.equals(Material.STRIPPED_DARK_OAK_LOG) ||
material.equals(Material.STRIPPED_JUNGLE_LOG) ||
material.equals(Material.STRIPPED_OAK_LOG) ||
material.equals(Material.STRIPPED_SPRUCE_LOG))
if (isMaterialDurable(material) && checkUnbreakingChance(unbreakingLevel))
damageableMeta.setDamage(damageableMeta.getDamage() + 1);
}
item.setItemMeta((ItemMeta) damageableMeta);
if (item.getDurability() >= item.getType().getMaxDurability())
player.setItemInHand(new ItemStack(Material.AIR));
player.getInventory().setItemInMainHand(new ItemStack(Material.AIR));
}
/**
* Checks is a material should apply durability
*
* @param material The material to check
* @return If durability should be applied
*/
private static boolean isMaterialDurable(Material material) {
return material.equals(Material.ACACIA_LOG) ||
material.equals(Material.BIRCH_LOG) ||
material.equals(Material.DARK_OAK_LOG) ||
material.equals(Material.JUNGLE_LOG) ||
material.equals(Material.OAK_LOG) ||
material.equals(Material.SPRUCE_LOG) ||
material.equals(Material.STRIPPED_ACACIA_LOG) ||
material.equals(Material.STRIPPED_BIRCH_LOG) ||
material.equals(Material.STRIPPED_DARK_OAK_LOG) ||
material.equals(Material.STRIPPED_JUNGLE_LOG) ||
material.equals(Material.STRIPPED_OAK_LOG) ||
material.equals(Material.STRIPPED_SPRUCE_LOG);
}
/**
* Check if durbility should be applied based on the unbreaking enchantment
*
* @param level The level of the unbreaking enchantment
* @return True if durability should be applied, otherwise false
*/
private static boolean checkUnbreakingChance(int level) {
return ((double) 1 / (level + 1)) > random.nextDouble();
}
}