2013-01-22 21:51:07 +01:00
|
|
|
package com.gmail.nossr50.skills.woodcutting;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.bukkit.Location;
|
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.World;
|
|
|
|
import org.bukkit.block.Block;
|
2013-01-25 22:32:52 +01:00
|
|
|
import org.bukkit.enchantments.Enchantment;
|
2013-01-22 21:51:07 +01:00
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
|
|
|
import com.gmail.nossr50.mcMMO;
|
|
|
|
import com.gmail.nossr50.datatypes.mods.CustomBlock;
|
|
|
|
import com.gmail.nossr50.locale.LocaleLoader;
|
|
|
|
import com.gmail.nossr50.skills.Combat;
|
|
|
|
import com.gmail.nossr50.skills.SkillType;
|
2013-01-26 23:01:55 +01:00
|
|
|
import com.gmail.nossr50.skills.SkillTools;
|
2013-01-27 04:19:26 +01:00
|
|
|
import com.gmail.nossr50.skills.woodcutting.Woodcutting.ExperienceGainMethod;
|
2013-01-22 21:51:07 +01:00
|
|
|
import com.gmail.nossr50.util.BlockChecks;
|
|
|
|
import com.gmail.nossr50.util.Misc;
|
|
|
|
import com.gmail.nossr50.util.ModChecks;
|
|
|
|
import com.gmail.nossr50.util.Users;
|
|
|
|
|
2013-01-30 00:12:55 +01:00
|
|
|
public final class TreeFeller {
|
2013-01-22 21:51:07 +01:00
|
|
|
private static boolean treeFellerReachedThreshold = false;
|
|
|
|
|
2013-01-30 00:12:55 +01:00
|
|
|
private TreeFeller() {}
|
|
|
|
|
2013-01-22 21:51:07 +01:00
|
|
|
/**
|
2013-01-23 19:37:30 +01:00
|
|
|
* Begins Tree Feller
|
2013-01-22 21:51:07 +01:00
|
|
|
*
|
2013-01-30 00:12:55 +01:00
|
|
|
* @param player Player using Tree Feller
|
|
|
|
* @param block Block being broken
|
2013-01-22 21:51:07 +01:00
|
|
|
*/
|
2013-01-30 00:12:55 +01:00
|
|
|
public static void process(Player player, Block block) {
|
2013-01-22 21:51:07 +01:00
|
|
|
List<Block> treeFellerBlocks = new ArrayList<Block>();
|
|
|
|
|
2013-01-30 00:12:55 +01:00
|
|
|
processRecursively(block, treeFellerBlocks);
|
2013-01-22 21:51:07 +01:00
|
|
|
|
|
|
|
// If the player is trying to break to many block
|
|
|
|
if (treeFellerReachedThreshold) {
|
|
|
|
treeFellerReachedThreshold = false;
|
|
|
|
|
2013-01-26 23:01:55 +01:00
|
|
|
player.sendMessage(LocaleLoader.getString("Woodcutting.SkillTools.TreeFellerThreshold"));
|
2013-01-22 21:51:07 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the tool can't sustain the durability loss
|
|
|
|
if (!handleDurabilityLoss(treeFellerBlocks, player)) {
|
2013-01-26 23:01:55 +01:00
|
|
|
player.sendMessage(LocaleLoader.getString("Woodcutting.SkillTools.TreeFeller.Splinter"));
|
2013-01-22 21:51:07 +01:00
|
|
|
|
|
|
|
int health = player.getHealth();
|
|
|
|
|
|
|
|
if (health > 1) {
|
|
|
|
Combat.dealDamage(player, Misc.getRandom().nextInt(health - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-23 19:37:30 +01:00
|
|
|
dropBlocks(treeFellerBlocks, player);
|
2013-01-22 21:51:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-01-23 19:37:30 +01:00
|
|
|
* Processes Tree Feller
|
2013-01-22 21:51:07 +01:00
|
|
|
*
|
2013-01-30 00:12:55 +01:00
|
|
|
* @param block Block being checked
|
2013-01-22 21:51:07 +01:00
|
|
|
* @param treeFellerBlocks List of blocks to be removed
|
|
|
|
*/
|
|
|
|
private static void processRecursively(Block block, List<Block> treeFellerBlocks) {
|
2013-01-24 00:52:32 +01:00
|
|
|
if (!BlockChecks.isLog(block)) {
|
|
|
|
return;
|
2013-01-22 21:51:07 +01:00
|
|
|
}
|
|
|
|
|
2013-01-24 00:52:32 +01:00
|
|
|
List<Block> futureCenterBlocks = new ArrayList<Block>();
|
2013-01-22 21:51:07 +01:00
|
|
|
World world = block.getWorld();
|
|
|
|
|
|
|
|
// Handle the blocks around 'block'
|
2013-01-24 00:52:32 +01:00
|
|
|
for (int y = 0 ; y <= 1 ; y++) {
|
|
|
|
for (int x = -1 ; x <= 1 ; x++) {
|
|
|
|
for (int z = -1 ; z <= 1 ; z++) {
|
|
|
|
Block nextBlock = world.getBlockAt(block.getLocation().add(x, y, z));
|
|
|
|
|
|
|
|
handleBlock(nextBlock, futureCenterBlocks, treeFellerBlocks);
|
2013-01-22 21:51:07 +01:00
|
|
|
|
|
|
|
if (treeFellerReachedThreshold) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recursive call for each log found
|
|
|
|
for (Block futurCenterBlock : futureCenterBlocks) {
|
|
|
|
if (treeFellerReachedThreshold) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
processRecursively(futurCenterBlock, treeFellerBlocks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-01-24 00:52:32 +01:00
|
|
|
* Handle a block addition to the list of blocks to be removed
|
|
|
|
* and to the list of blocks used for future recursive calls of 'processRecursively()'
|
2013-01-22 21:51:07 +01:00
|
|
|
*
|
|
|
|
* @param block Block to be added
|
2013-01-24 00:52:32 +01:00
|
|
|
* @param futureCenterBlocks List of blocks that will be used to call 'processRecursively()'
|
2013-01-22 21:51:07 +01:00
|
|
|
* @param treeFellerBlocks List of blocks to be removed
|
|
|
|
*/
|
2013-01-24 00:52:32 +01:00
|
|
|
private static void handleBlock(Block block, List<Block> futureCenterBlocks, List<Block> treeFellerBlocks) {
|
|
|
|
if (!BlockChecks.treeFellerCompatible(block) || mcMMO.placeStore.isTrue(block) || treeFellerBlocks.contains(block)) {
|
|
|
|
return;
|
|
|
|
}
|
2013-01-22 21:51:07 +01:00
|
|
|
|
2013-01-24 00:52:32 +01:00
|
|
|
treeFellerBlocks.add(block);
|
2013-01-22 21:51:07 +01:00
|
|
|
|
2013-01-30 00:21:34 +01:00
|
|
|
if (treeFellerBlocks.size() > Woodcutting.CONFIG.getTreeFellerThreshold()) {
|
2013-01-24 00:52:32 +01:00
|
|
|
treeFellerReachedThreshold = true;
|
|
|
|
return;
|
2013-01-22 21:51:07 +01:00
|
|
|
}
|
|
|
|
|
2013-01-24 00:52:32 +01:00
|
|
|
futureCenterBlocks.add(block);
|
2013-01-22 21:51:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-01-23 19:37:30 +01:00
|
|
|
* Handles the durability loss
|
2013-01-22 21:51:07 +01:00
|
|
|
*
|
|
|
|
* @param treeFellerBlocks List of blocks to be removed
|
|
|
|
* @param Player Player using the ability
|
|
|
|
* @return True if the tool can sustain the durability loss
|
|
|
|
*/
|
|
|
|
private static boolean handleDurabilityLoss(List<Block> treeFellerBlocks, Player player) {
|
|
|
|
ItemStack inHand = player.getItemInHand();
|
|
|
|
Material inHandMaterial = inHand.getType();
|
|
|
|
|
|
|
|
if (inHandMaterial != Material.AIR) {
|
|
|
|
short durabilityLoss = 0;
|
2013-01-25 22:32:52 +01:00
|
|
|
int unbreakingLevel = inHand.getEnchantmentLevel(Enchantment.DURABILITY);
|
2013-01-22 21:51:07 +01:00
|
|
|
|
|
|
|
for (Block block : treeFellerBlocks) {
|
2013-01-25 22:32:52 +01:00
|
|
|
if (BlockChecks.isLog(block) && Misc.getRandom().nextInt(unbreakingLevel + 1) == 0) {
|
|
|
|
durabilityLoss += Misc.toolDurabilityLoss;
|
2013-01-22 21:51:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
short finalDurability = (short) (inHand.getDurability() + durabilityLoss);
|
|
|
|
short maxDurability = ModChecks.isCustomTool(inHand) ? ModChecks.getToolFromItemStack(inHand).getDurability() : inHandMaterial.getMaxDurability();
|
|
|
|
|
|
|
|
if (finalDurability >= maxDurability) {
|
|
|
|
inHand.setDurability(maxDurability);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
inHand.setDurability(finalDurability);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-01-23 19:37:30 +01:00
|
|
|
* Handles the dropping of blocks
|
2013-01-22 21:51:07 +01:00
|
|
|
*
|
2013-01-23 19:37:30 +01:00
|
|
|
* @param treeFellerBlocks List of blocks to be dropped
|
2013-01-22 21:51:07 +01:00
|
|
|
* @param player Player using the ability
|
|
|
|
*/
|
2013-01-23 19:37:30 +01:00
|
|
|
private static void dropBlocks(List<Block> treeFellerBlocks, Player player) {
|
2013-01-22 21:51:07 +01:00
|
|
|
int xp = 0;
|
|
|
|
|
|
|
|
for (Block block : treeFellerBlocks) {
|
|
|
|
if (!Misc.blockBreakSimulate(block, player, true)) {
|
|
|
|
break; // TODO: Shouldn't we use continue instead?
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (block.getType()) {
|
|
|
|
case LOG:
|
2013-01-30 00:12:55 +01:00
|
|
|
Woodcutting.checkForDoubleDrop(player, block);
|
2013-01-22 21:51:07 +01:00
|
|
|
|
2013-01-22 22:24:53 +01:00
|
|
|
try {
|
2013-01-27 04:19:26 +01:00
|
|
|
xp += Woodcutting.getExperienceFromLog(block, ExperienceGainMethod.TREE_FELLER);
|
2013-01-22 22:24:53 +01:00
|
|
|
}
|
|
|
|
catch (IllegalArgumentException exception) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-01-24 16:14:15 +01:00
|
|
|
Misc.dropItem(block.getLocation(), new ItemStack(Material.LOG, 1, Woodcutting.extractLogItemData(block.getData())));
|
2013-01-22 21:51:07 +01:00
|
|
|
break;
|
|
|
|
case LEAVES:
|
2013-01-23 19:30:23 +01:00
|
|
|
Misc.randomDropItem(block.getLocation(), new ItemStack(Material.SAPLING, 1, Woodcutting.extractLogItemData(block.getData())), 10);
|
2013-01-22 21:51:07 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (ModChecks.isCustomLogBlock(block)) {
|
2013-01-30 00:12:55 +01:00
|
|
|
Woodcutting.checkForDoubleDrop(player, block);
|
2013-01-22 21:51:07 +01:00
|
|
|
|
|
|
|
CustomBlock customBlock = ModChecks.getCustomBlock(block);
|
|
|
|
xp = customBlock.getXpGain();
|
|
|
|
int minimumDropAmount = customBlock.getMinimumDropAmount();
|
|
|
|
int maximumDropAmount = customBlock.getMaximumDropAmount();
|
|
|
|
Location location = block.getLocation();
|
|
|
|
ItemStack item = customBlock.getItemDrop();;
|
|
|
|
|
|
|
|
Misc.dropItems(location, item, minimumDropAmount);
|
|
|
|
|
|
|
|
if (minimumDropAmount < maximumDropAmount) {
|
|
|
|
Misc.randomDropItems(location, item, 50, maximumDropAmount - minimumDropAmount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ModChecks.isCustomLeafBlock(block)) {
|
|
|
|
CustomBlock customBlock = ModChecks.getCustomBlock(block);
|
|
|
|
|
|
|
|
Misc.randomDropItem(block.getLocation(), customBlock.getItemDrop(), 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
block.setData((byte) 0);
|
|
|
|
block.setType(Material.AIR);
|
|
|
|
}
|
|
|
|
|
2013-01-30 00:12:55 +01:00
|
|
|
SkillTools.xpProcessing(player, Users.getProfile(player), SkillType.WOODCUTTING, xp);
|
2013-01-22 21:51:07 +01:00
|
|
|
}
|
|
|
|
}
|