mcMMO/src/main/java/com/gmail/nossr50/skills/WoodCutting.java

292 lines
11 KiB
Java
Raw Normal View History

2012-01-09 20:00:13 +01:00
/*
This file is part of mcMMO.
mcMMO is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
mcMMO is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with mcMMO. If not, see <http://www.gnu.org/licenses/>.
*/
package com.gmail.nossr50.skills;
import java.util.ArrayList;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.World;
2012-01-09 20:00:13 +01:00
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.player.PlayerAnimationEvent;
import org.bukkit.Bukkit;
2012-01-09 20:00:13 +01:00
import com.gmail.nossr50.Users;
import com.gmail.nossr50.m;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.datatypes.AbilityType;
2012-01-09 20:00:13 +01:00
import com.gmail.nossr50.datatypes.PlayerProfile;
import com.gmail.nossr50.datatypes.SkillType;
import com.gmail.nossr50.locale.mcLocale;
2012-02-04 07:36:03 +01:00
import com.gmail.nossr50.spout.SpoutStuff;
2012-01-09 20:00:13 +01:00
import com.gmail.nossr50.config.*;
2012-02-04 07:59:31 +01:00
import org.getspout.spoutapi.sound.SoundEffect;
2012-01-09 20:00:13 +01:00
public class WoodCutting
{
public static void treeFeller(BlockBreakEvent event, mcMMO plugin)
{
//Setup vars
Player player = event.getPlayer();
Block firstBlock = event.getBlock();
PlayerProfile PP = Users.getProfile(player);
World world = firstBlock.getWorld();
//Prepare array
ArrayList<Block> toBeFelled = new ArrayList<Block>();
//NOTE: Tree Feller will cut upwards like how you actually fell trees
processTreeFelling(firstBlock, world, toBeFelled);
removeBlocks(toBeFelled, player, PP, plugin);
}
private static void removeBlocks(ArrayList<Block> toBeFelled, Player player, PlayerProfile PP, mcMMO plugin)
{
2012-02-23 10:59:00 +01:00
int durabilityLoss = 0, xp = 0;
2012-02-23 08:42:03 +01:00
for(Block x : toBeFelled)
{
//Stupid NoCheat compatibility stuff
PlayerAnimationEvent armswing = new PlayerAnimationEvent(player);
Bukkit.getPluginManager().callEvent(armswing);
if(m.blockBreakSimulate(x, player))
{
2012-02-23 10:34:34 +01:00
if(x.getType() == Material.LOG)
{
2012-02-23 10:34:34 +01:00
byte type = x.getData();
ItemStack item = new ItemStack(x.getType(), 1, (byte)0, type);
2012-02-23 10:34:34 +01:00
if(!plugin.misc.blockWatchList.contains(x))
{
WoodCutting.woodCuttingProcCheck(player, x);
2012-02-23 10:59:00 +01:00
2012-02-23 10:34:34 +01:00
switch(x.getData())
{
case 0:
xp += LoadProperties.mpine;
break;
case 1:
xp += LoadProperties.mspruce;
break;
case 2:
xp += LoadProperties.mbirch;
break;
}
2012-02-23 10:34:34 +01:00
}
2012-02-23 10:34:34 +01:00
//Drop the block
x.getWorld().dropItemNaturally(x.getLocation(), item);
2012-02-23 10:34:34 +01:00
//Remove the block
x.setData((byte) 0);
x.setType(Material.AIR);
2012-02-23 10:34:34 +01:00
//Damage the tool more if the Tree is larger
durabilityLoss++;
} else if(x.getType() == Material.LEAVES)
{
Material mat = Material.SAPLING;
ItemStack item = new ItemStack(mat, 1, (short)0, (byte)(x.getData()-8));
2012-02-23 10:34:34 +01:00
//1 in 10 chance to drop sapling
if(Math.random() * 10 > 9)
m.mcDropItem(x.getLocation(), item);
2012-02-23 10:34:34 +01:00
//Remove the block
x.setData((byte) 0);
x.setType(Material.AIR);
//Damage the tool more if the Tree is larger
durabilityLoss++;
}
}
}
2012-02-23 08:42:03 +01:00
2012-02-23 10:59:00 +01:00
PP.addXP(SkillType.WOODCUTTING, xp, player);
Skills.XpCheckSkill(SkillType.WOODCUTTING, player);
2012-02-23 08:42:03 +01:00
if(LoadProperties.toolsLoseDurabilityFromAbilities)
{
if(!player.getItemInHand().containsEnchantment(Enchantment.DURABILITY))
{
short durability = player.getItemInHand().getDurability();
durability += (LoadProperties.abilityDurabilityLoss * durabilityLoss);
player.getItemInHand().setDurability(durability);
}
}
}
private static boolean treeFellerCompatible(Block block)
{
2012-02-23 05:53:20 +01:00
return block.getType() == Material.LOG || block.getType() == Material.LEAVES || block.getType() == Material.AIR;
}
private static void processTreeFelling(Block currentBlock, World world, ArrayList<Block> toBeFelled)
{
int x = currentBlock.getX(), y = currentBlock.getY(), z = currentBlock.getZ();
2012-02-23 05:53:20 +01:00
toBeFelled.add(currentBlock);
2012-02-23 05:53:20 +01:00
//These 2 are to make sure that Tree Feller isn't so aggressive
boolean isAirOrLeaves = currentBlock.getType() == Material.LEAVES || currentBlock.getType() == Material.AIR;
Block xPositive = world.getBlockAt(x+1, y, z);
Block xNegative = world.getBlockAt(x-1, y, z);
Block zPositive = world.getBlockAt(x, y, z+1);
Block zNegative = world.getBlockAt(x, y, z-1);
2012-02-23 05:53:20 +01:00
if(!isTooAgressive(isAirOrLeaves, xPositive) && treeFellerCompatible(xPositive) && !toBeFelled.contains(xPositive))
processTreeFelling(xPositive, world, toBeFelled);
2012-02-23 05:53:20 +01:00
if(!isTooAgressive(isAirOrLeaves, xNegative) && treeFellerCompatible(xNegative) && !toBeFelled.contains(xNegative))
processTreeFelling(xNegative, world, toBeFelled);
2012-02-23 05:53:20 +01:00
if(!isTooAgressive(isAirOrLeaves, zPositive) && treeFellerCompatible(zPositive) && !toBeFelled.contains(zPositive))
processTreeFelling(zPositive, world, toBeFelled);
2012-02-23 05:53:20 +01:00
if(!isTooAgressive(isAirOrLeaves, zNegative) && treeFellerCompatible(zNegative) && !toBeFelled.contains(zNegative))
processTreeFelling(zNegative, world, toBeFelled);
//Finally go Y+
Block yPositive = world.getBlockAt(x, y+1, z);
if(treeFellerCompatible(yPositive))
{
if(!toBeFelled.contains(yPositive))
{
processTreeFelling(yPositive, world, toBeFelled);
}
}
}
2012-02-23 05:53:20 +01:00
private static boolean isTooAgressive(boolean bool, Block block)
{
return bool && (block.getType() == Material.AIR || block.getType() == Material.LEAVES);
}
2012-01-09 20:00:13 +01:00
public static void woodCuttingProcCheck(Player player, Block block)
{
PlayerProfile PP = Users.getProfile(player);
byte type = block.getData();
Material mat = Material.getMaterial(block.getTypeId());
if(player != null)
{
2012-02-09 18:47:17 +01:00
if(PP.getSkillLevel(SkillType.WOODCUTTING) > 1000 || (Math.random() * 1000 <= PP.getSkillLevel(SkillType.WOODCUTTING)))
2012-01-09 20:00:13 +01:00
{
ItemStack item = new ItemStack(mat, 1, (short) 0, type);
m.mcDropItem(block.getLocation(), item);
2012-01-09 20:00:13 +01:00
}
}
}
2012-01-09 20:00:13 +01:00
public static void treeFellerCheck(Player player, Block block)
{
PlayerProfile PP = Users.getProfile(player);
if(m.isAxes(player.getItemInHand()))
{
if(block != null)
{
if(!m.abilityBlockCheck(block))
return;
}
/*
* CHECK FOR AXE PREP MODE
*/
if(PP.getAxePreparationMode())
{
PP.setAxePreparationMode(false);
}
2012-01-09 20:00:13 +01:00
int ticks = 2;
int x = PP.getSkillLevel(SkillType.WOODCUTTING);
2012-01-09 20:00:13 +01:00
while(x >= 50)
{
x-=50;
ticks++;
}
if(!PP.getTreeFellerMode() && Skills.cooldownOver(player, (PP.getSkillDATS(AbilityType.TREE_FELLER)*1000), LoadProperties.treeFellerCooldown))
2012-01-09 20:00:13 +01:00
{
player.sendMessage(mcLocale.getString("Skills.TreeFellerOn"));
for(Player y : player.getWorld().getPlayers())
{
if(y != null && y != player && m.getDistance(player.getLocation(), y.getLocation()) < 10)
y.sendMessage(mcLocale.getString("Skills.TreeFellerPlayer", new Object[] {player.getName()}));
}
PP.setSkillDATS(AbilityType.TREE_FELLER, System.currentTimeMillis()+(ticks*1000));
2012-01-09 20:00:13 +01:00
PP.setTreeFellerMode(true);
}
if(!PP.getTreeFellerMode() && !Skills.cooldownOver(player, (PP.getSkillDATS(AbilityType.TREE_FELLER)*1000), LoadProperties.treeFellerCooldown)){
2012-01-09 20:00:13 +01:00
player.sendMessage(ChatColor.RED+"You are too tired to use that ability again."
+ChatColor.YELLOW+" ("+Skills.calculateTimeLeft(player, (PP.getSkillDATS(AbilityType.TREE_FELLER)*1000), LoadProperties.treeFellerCooldown)+"s)");
2012-01-09 20:00:13 +01:00
}
}
}
public static void woodcuttingBlockCheck(Player player, Block block, mcMMO plugin)
{
PlayerProfile PP = Users.getProfile(player);
int xp = 0;
byte data = block.getData();
if(plugin.misc.blockWatchList.contains(block))
return;
switch(data)
{
case 0:
xp += LoadProperties.mpine;
break;
case 1:
xp += LoadProperties.mspruce;
break;
case 2:
xp += LoadProperties.mbirch;
break;
}
if(block.getTypeId() == 17)
2012-02-05 18:53:40 +01:00
{
WoodCutting.woodCuttingProcCheck(player, block);
2012-02-05 18:53:40 +01:00
PP.addXP(SkillType.WOODCUTTING, xp, player);
Skills.XpCheckSkill(SkillType.WOODCUTTING, player);
}
}
2012-02-04 07:36:03 +01:00
public static void leafBlower(Player player, Block block){
PlayerAnimationEvent armswing = new PlayerAnimationEvent(player);
Bukkit.getPluginManager().callEvent(armswing);
if(LoadProperties.toolsLoseDurabilityFromAbilities)
2012-02-04 07:36:03 +01:00
{
if(!player.getItemInHand().containsEnchantment(Enchantment.DURABILITY))
{
short durability = player.getItemInHand().getDurability();
durability += LoadProperties.abilityDurabilityLoss;
player.getItemInHand().setDurability(durability);
}
2012-02-04 07:36:03 +01:00
}
if(LoadProperties.spoutEnabled)
SpoutStuff.playSoundForPlayer(SoundEffect.POP, player, block.getLocation());
}
2012-01-09 20:00:13 +01:00
}