mcMMO/src/main/java/com/gmail/nossr50/skills/woodcutting/Woodcutting.java

214 lines
7.5 KiB
Java
Raw Normal View History

package com.gmail.nossr50.skills.woodcutting;
2012-01-09 20:00:13 +01:00
import org.bukkit.Location;
2012-01-09 20:00:13 +01:00
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.TreeSpecies;
2012-01-09 20:00:13 +01:00
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import com.gmail.nossr50.mcMMO;
2012-11-21 21:49:54 +01:00
import com.gmail.nossr50.config.AdvancedConfig;
2012-04-26 16:27:57 +02:00
import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.datatypes.McMMOPlayer;
import com.gmail.nossr50.events.fake.FakePlayerAnimationEvent;
2013-01-30 17:35:33 +01:00
import com.gmail.nossr50.mods.ModChecks;
import com.gmail.nossr50.mods.datatypes.CustomBlock;
import com.gmail.nossr50.skills.utilities.SkillTools;
import com.gmail.nossr50.skills.utilities.SkillType;
2012-04-27 11:47:11 +02:00
import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.Users;
2013-01-26 23:01:55 +01:00
public final class Woodcutting {
static final AdvancedConfig ADVANCED_CONFIG = AdvancedConfig.getInstance();
static final Config CONFIG = Config.getInstance();
protected enum ExperienceGainMethod {
DEFAULT,
TREE_FELLER,
};
2013-01-26 23:01:55 +01:00
private Woodcutting() {}
/**
2013-01-23 19:37:30 +01:00
* Begins the Tree Feller ability
*
* @param mcMMOPlayer Player using the ability
* @param block Block being broken
*/
public static void beginTreeFeller(McMMOPlayer mcMMOPlayer, Block block) {
TreeFeller.process(mcMMOPlayer, block);
}
/**
2013-01-23 19:37:30 +01:00
* Begins the Leaf Blower ability
*
* @param player Player using the ability
* @param block Block being broken
*/
public static void beginLeafBlower(Player player, Block block) {
mcMMO.p.getServer().getPluginManager().callEvent(new FakePlayerAnimationEvent(player));
player.playSound(block.getLocation(), Sound.ITEM_PICKUP, Misc.POP_VOLUME, Misc.POP_PITCH);
}
/**
2013-01-23 19:37:30 +01:00
* Begins Woodcutting
*
* @param mcMMOPlayer Player breaking the block
* @param block Block being broken
*/
public static void beginWoodcutting(McMMOPlayer mcMMOPlayer, Block block) {
int xp = 0;
if (Config.getInstance().getBlockModsEnabled() && ModChecks.isCustomLogBlock(block)) {
xp = ModChecks.getCustomBlock(block).getXpGain();
}
else {
try {
xp = getExperienceFromLog(block, ExperienceGainMethod.DEFAULT);
2012-10-31 04:05:37 +01:00
}
catch (IllegalArgumentException exception) {
return;
}
}
Player player = mcMMOPlayer.getPlayer();
if (Permissions.woodcuttingDoubleDrops(player)) {
checkForDoubleDrop(mcMMOPlayer, block);
}
mcMMOPlayer.beginXpGain(SkillType.WOODCUTTING, xp);
}
/**
2013-01-23 19:37:30 +01:00
* Retrieves the experience reward from a log
*
* @param log Log being broken
* @param experienceGainMethod How the log is being broken
* @return Amount of experience
* @throws IllegalArgumentException if 'log' is invalid
*/
protected static int getExperienceFromLog(Block log, ExperienceGainMethod experienceGainMethod) {
//Mushrooms aren't trees so we could never get species data from them
if(log.getType() == Material.HUGE_MUSHROOM_1 || log.getType() == Material.HUGE_MUSHROOM_2) {
switch(log.getType()) {
case HUGE_MUSHROOM_1:
return Config.getInstance().getWoodcuttingXPHugeBrownMushroom();
case HUGE_MUSHROOM_2:
return Config.getInstance().getWoodcuttingXPHugeRedMushroom();
default:
throw new IllegalArgumentException();
}
}
TreeSpecies logType = TreeSpecies.getByData(extractLogItemData(log.getData()));
// Apparently species can be null in certain cases (custom server mods?)
// https://github.com/mcMMO-Dev/mcMMO/issues/229
if (logType == null) {
throw new IllegalArgumentException();
}
2013-01-10 04:43:21 +01:00
switch (logType) {
case GENERIC:
return Config.getInstance().getWoodcuttingXPOak();
case REDWOOD:
return Config.getInstance().getWoodcuttingXPSpruce();
case BIRCH:
return Config.getInstance().getWoodcuttingXPBirch();
case JUNGLE:
int xp = Config.getInstance().getWoodcuttingXPJungle();
switch (experienceGainMethod) {
case TREE_FELLER:
return (int) (xp * 0.5);
default:
return xp;
}
default:
throw new IllegalArgumentException();
}
2012-02-23 05:53:20 +01:00
}
/**
2013-01-23 19:37:30 +01:00
* Checks for double drops
*
* @param mcMMOPlayer Player breaking the block
* @param block Block being broken
*/
protected static void checkForDoubleDrop(McMMOPlayer mcMMOPlayer, Block block) {
Player player = mcMMOPlayer.getPlayer();
double configDoubleDropChance = ADVANCED_CONFIG.getWoodcuttingDoubleDropChance();
int configDoubleDropMaxLevel = ADVANCED_CONFIG.getWoodcuttingDoubleDropMaxLevel();
int probability = (int) ((configDoubleDropChance / configDoubleDropMaxLevel) * Users.getPlayer(player).getProfile().getSkillLevel(SkillType.WOODCUTTING));
int activationChance = SkillTools.calculateActivationChance(Permissions.luckyWoodcutting(player));
if (probability > configDoubleDropChance) {
probability = (int) configDoubleDropChance;
}
if (probability <= Misc.getRandom().nextInt(activationChance)) {
return;
}
if (Config.getInstance().getBlockModsEnabled() && ModChecks.isCustomLogBlock(block)) {
CustomBlock customBlock = ModChecks.getCustomBlock(block);
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 {
2013-01-24 15:37:02 +01:00
byte itemData = extractLogItemData(block.getData());
Location location = block.getLocation();
ItemStack item = new ItemStack(Material.LOG, 1, itemData);
2013-01-24 15:37:02 +01:00
switch (TreeSpecies.getByData(itemData)) {
case GENERIC:
if (Config.getInstance().getOakDoubleDropsEnabled()) {
Misc.dropItem(location, item);
}
break;
case REDWOOD:
if (Config.getInstance().getSpruceDoubleDropsEnabled()) {
Misc.dropItem(location, item);
}
break;
case BIRCH:
if (Config.getInstance().getBirchDoubleDropsEnabled()) {
Misc.dropItem(location, item);
}
break;
case JUNGLE:
if (Config.getInstance().getJungleDoubleDropsEnabled()) {
Misc.dropItem(location, item);
}
break;
default:
break;
}
}
}
/**
2013-01-23 19:37:30 +01:00
* Extracts the log type from the block data (i.e. removes rotation)
*
* @param data Original block data
* @return Extracted log type
*/
protected static byte extractLogItemData(byte data) {
return (byte) (data & 0x3);
}
2012-01-09 20:00:13 +01:00
}