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

252 lines
9.7 KiB
Java
Raw Normal View History

2012-01-09 20:00:13 +01:00
package com.gmail.nossr50.skills;
import java.util.ArrayList;
2012-01-09 20:00:13 +01:00
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.player.PlayerAnimationEvent;
import org.bukkit.Bukkit;
import com.gmail.nossr50.Combat;
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.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);
//Prepare array
ArrayList<Block> toBeFelled = new ArrayList<Block>();
//NOTE: Tree Feller will cut upwards like how you actually fell trees
processTreeFelling(firstBlock, toBeFelled, plugin);
removeBlocks(toBeFelled, player, PP, plugin);
}
private static void removeBlocks(ArrayList<Block> toBeFelled, Player player, PlayerProfile PP, mcMMO plugin)
{
if(toBeFelled.size() > LoadProperties.treeFellerThreshold)
{
player.sendMessage(mcLocale.getString("Skills.Woodcutting.TreeFellerThreshold"));
return;
}
int durabilityLoss = toBeFelled.size(), xp = 0;
//Damage the tool
player.getItemInHand().setDurability((short) (player.getItemInHand().getDurability()+durabilityLoss));
//This is to prevent using wood axes everytime you tree fell
if((player.getItemInHand().getDurability() + durabilityLoss >= player.getItemInHand().getType().getMaxDurability())
|| player.getItemInHand().getType() == Material.AIR || player.getItemInHand() == null)
{
player.sendMessage(mcLocale.getString("TreeFeller.AxeSplinters"));
if(player.getHealth() >= 2)
Combat.dealDamage(player, (int)(Math.random() * (player.getHealth()-1)));
return;
}
//Prepare ItemStacks
ItemStack item;
ItemStack oak = new ItemStack(Material.LOG, 1, (byte)0, (byte)0);
ItemStack spruce = new ItemStack(Material.LOG, 1, (byte)0, (byte)1);
ItemStack birch = new ItemStack(Material.LOG, 1, (byte)0, (byte)2);
ItemStack jungle = new ItemStack(Material.LOG, 1, (byte)0, (byte)3);
2012-02-23 08:42:03 +01:00
for(Block x : toBeFelled)
{
if(m.blockBreakSimulate(x, player, true))
{
2012-02-23 10:34:34 +01:00
if(x.getType() == Material.LOG)
{
switch(x.getData())
{
case 0:
item = oak;
break;
case 1:
item = spruce;
break;
case 2:
item = birch;
break;
case 3:
item = jungle;
break;
default:
item = oak;
break;
}
if(!x.hasMetadata("mcmmoPlacedBlock"))
2012-02-23 10:34:34 +01:00
{
WoodCutting.woodCuttingProcCheck(player, x);
2012-02-23 10:34:34 +01:00
switch(x.getData())
{
case 0:
xp += LoadProperties.moak;
2012-02-23 10:34:34 +01:00
break;
case 1:
xp += LoadProperties.mspruce;
break;
case 2:
xp += LoadProperties.mbirch;
break;
case 3:
xp += LoadProperties.mjungle/4;
break;
}
2012-02-23 10:34:34 +01:00
}
2012-02-23 10:34:34 +01:00
//Remove the block
x.setData((byte) 0);
2012-02-23 10:34:34 +01:00
x.setType(Material.AIR);
//Drop the block
m.mcDropItem(x.getLocation(), item);
2012-02-23 10:34:34 +01:00
} else if(x.getType() == Material.LEAVES)
{
Material mat = Material.SAPLING;
item = new ItemStack(mat, 1, (short)0, (byte)(x.getData()-8));
2012-02-24 04:26:20 +01:00
//90% chance to drop sapling
if(Math.random() * 10 > 9)
m.mcRandomDropItem(x.getLocation(), item, 90);
2012-02-23 10:34:34 +01:00
//Remove the block
x.setData((byte) 0);
x.setType(Material.AIR);
}
}
}
2012-02-23 08:42:03 +01:00
PP.addXP(SkillType.WOODCUTTING, xp, player); //Tree Feller gives nerf'd XP
2012-02-23 10:59:00 +01:00
Skills.XpCheckSkill(SkillType.WOODCUTTING, player);
}
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, ArrayList<Block> toBeFelled, mcMMO plugin)
{
if(currentBlock.getType() == Material.LOG || currentBlock.getType() == Material.LEAVES)
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 = currentBlock.getRelative(1, 0, 0);
Block xNegative = currentBlock.getRelative(-1, 0, 0);
Block zPositive = currentBlock.getRelative(0, 0, 1);
Block zNegative = currentBlock.getRelative(0, 0, -1);
if(!currentBlock.hasMetadata("mcmmoPlacedBlock") &&
2012-02-24 13:52:30 +01:00
!isTooAgressive(isAirOrLeaves, xPositive) && treeFellerCompatible(xPositive) && !toBeFelled.contains(xPositive))
processTreeFelling(xPositive, toBeFelled, plugin);
if(!currentBlock.hasMetadata("mcmmoPlacedBlock") &&
2012-02-24 13:52:30 +01:00
!isTooAgressive(isAirOrLeaves, xNegative) && treeFellerCompatible(xNegative) && !toBeFelled.contains(xNegative))
processTreeFelling(xNegative, toBeFelled, plugin);
if(!currentBlock.hasMetadata("mcmmoPlacedBlock") &&
2012-02-24 13:52:30 +01:00
!isTooAgressive(isAirOrLeaves, zPositive) && treeFellerCompatible(zPositive) && !toBeFelled.contains(zPositive))
processTreeFelling(zPositive, toBeFelled, plugin);
if(!currentBlock.hasMetadata("mcmmoPlacedBlock") &&
2012-02-24 13:52:30 +01:00
!isTooAgressive(isAirOrLeaves, zNegative) && treeFellerCompatible(zNegative) && !toBeFelled.contains(zNegative))
processTreeFelling(zNegative, toBeFelled, plugin);
//Finally go Y+
Block yPositive = currentBlock.getRelative(0, 1, 0);
if(treeFellerCompatible(yPositive))
{
if(!currentBlock.hasMetadata("mcmmoPlacedBlock") && !toBeFelled.contains(yPositive))
{
processTreeFelling(yPositive, toBeFelled, plugin);
}
}
}
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
}
}
}
public static void woodcuttingBlockCheck(Player player, Block block, mcMMO plugin)
{
PlayerProfile PP = Users.getProfile(player);
int xp = 0;
byte data = block.getData();
if(block.hasMetadata("placedBlock"))
return;
switch(data)
{
case 0:
xp += LoadProperties.moak;
break;
case 1:
xp += LoadProperties.mspruce;
break;
case 2:
xp += LoadProperties.mbirch;
break;
case 3:
xp += LoadProperties.mjungle;
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);
2012-03-06 20:27:34 +01:00
if(LoadProperties.woodcuttingrequiresaxe)
Skills.abilityDurabilityLoss(player.getItemInHand(), LoadProperties.abilityDurabilityLoss);
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
}