mcMMO/src/main/java/com/gmail/nossr50/skills/gathering/Herbalism.java

397 lines
14 KiB
Java
Raw Normal View History

2012-05-01 19:58:47 +02:00
package com.gmail.nossr50.skills.gathering;
2012-01-09 20:00:13 +01:00
2012-03-26 17:04:17 +02:00
import java.util.Random;
import org.bukkit.CropState;
2012-01-09 20:00:13 +01:00
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.inventory.ItemStack;
2012-02-24 07:02:23 +01:00
import org.bukkit.inventory.PlayerInventory;
import com.gmail.nossr50.mcMMO;
2012-04-26 16:27:57 +02:00
import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.config.mods.CustomBlocksConfig;
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.datatypes.mods.CustomBlock;
2012-04-27 11:47:11 +02:00
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.runnables.GreenThumbTimer;
2012-04-27 11:47:11 +02:00
import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.ModChecks;
2012-04-27 11:47:11 +02:00
import com.gmail.nossr50.util.Permissions;
2012-05-01 19:58:47 +02:00
import com.gmail.nossr50.util.Skills;
2012-04-27 11:47:11 +02:00
import com.gmail.nossr50.util.Users;
2012-01-09 20:00:13 +01:00
2012-03-10 19:03:31 +01:00
public class Herbalism {
2012-03-26 17:04:17 +02:00
private static Random random = new Random();
2012-03-10 19:03:31 +01:00
/**
* Activate the Green Terra ability.
*
* @param player The player activating the ability
* @param block The block to be changed by Green Terra
*/
public static void greenTerra(Player player, Block block) {
PlayerInventory inventory = player.getInventory();
boolean hasSeeds = inventory.contains(Material.SEEDS);
if (!hasSeeds) {
2012-03-26 22:17:35 +02:00
player.sendMessage("You need more seeds to spread Green Terra"); //TODO: Needs more locale.
2012-03-10 19:03:31 +01:00
}
else if (hasSeeds && !block.getType().equals(Material.WHEAT)) {
inventory.removeItem(new ItemStack(Material.SEEDS));
2012-05-15 06:49:19 +02:00
player.updateInventory(); // Needed until replacement available
greenTerraConvert(player, block);
}
}
2012-06-05 15:57:10 +02:00
public static void greenTerraConvert(Player player, Block block) {
Material type = block.getType();
2012-06-05 15:57:10 +02:00
if (Misc.blockBreakSimulate(block, player, false)) {
if (Config.getInstance().getHerbalismGreenThumbSmoothbrickToMossy() && type == Material.SMOOTH_BRICK && block.getData() == 0) {
block.setTypeIdAndData(block.getTypeId(), (byte) 1, false); //Set type of the brick to mossy, force the client update
}
else if (Config.getInstance().getHerbalismGreenThumbDirtToGrass() && type == Material.DIRT) {
block.setType(Material.GRASS);
}
else if (Config.getInstance().getHerbalismGreenThumbCobbleToMossy() && type == Material.COBBLESTONE) {
block.setType(Material.MOSSY_COBBLESTONE);
2012-03-10 19:03:31 +01:00
}
}
}
/**
* Check for extra Herbalism drops.
*
* @param block The block to check for extra drops
* @param player The player getting extra drops
* @param event The event to use for Green Thumb
* @param plugin mcMMO plugin instance
*/
public static void herbalismProcCheck(final Block block, Player player, BlockBreakEvent event, mcMMO plugin) {
2012-07-03 16:04:04 +02:00
final PlayerProfile profile = Users.getProfile(player);
2012-03-13 08:31:07 +01:00
final int MAX_BONUS_LEVEL = 1000;
2012-07-03 16:04:04 +02:00
int herbLevel = profile.getSkillLevel(SkillType.HERBALISM);
2012-03-10 19:03:31 +01:00
int id = block.getTypeId();
Material type = block.getType();
2012-03-10 19:03:31 +01:00
Byte data = block.getData();
2012-07-04 21:35:14 +02:00
Location location = block.getLocation();
2012-03-10 19:03:31 +01:00
Material mat = null;
2012-03-10 19:03:31 +01:00
int xp = 0;
int catciDrops = 0;
int caneDrops = 0;
boolean customPlant = false;
int randomChance = 1000;
if (player.hasPermission("mcmmo.perks.lucky.herbalism")) {
randomChance = (int) (randomChance * 0.75);
}
2012-03-10 19:03:31 +01:00
switch (type) {
case BROWN_MUSHROOM:
case RED_MUSHROOM:
if (!mcMMO.placeStore.isTrue(block)) {
2012-03-10 19:03:31 +01:00
mat = Material.getMaterial(id);
xp = Config.getInstance().getHerbalismXPMushrooms();
2012-03-10 19:03:31 +01:00
}
break;
case CACTUS:
for (int y = 0; y <= 2; y++) {
Block b = block.getRelative(0, y, 0);
if (b.getType().equals(Material.CACTUS)) {
mat = Material.CACTUS;
if (!mcMMO.placeStore.isTrue(b)) {
if (herbLevel > MAX_BONUS_LEVEL || random.nextInt(randomChance) <= herbLevel) {
2012-03-10 19:03:31 +01:00
catciDrops++;
}
xp += Config.getInstance().getHerbalismXPCactus();
2012-03-10 19:03:31 +01:00
}
}
}
break;
case CROPS:
if (data == CropState.RIPE.getData()) {
2012-03-10 19:03:31 +01:00
mat = Material.WHEAT;
xp = Config.getInstance().getHerbalismXPWheat();
if (Permissions.getInstance().greenThumbWheat(player)) {
greenThumbWheat(block, player, event, plugin);
}
2012-03-10 19:03:31 +01:00
}
break;
case MELON_BLOCK:
if (!mcMMO.placeStore.isTrue(block)) {
2012-03-10 19:03:31 +01:00
mat = Material.MELON;
xp = Config.getInstance().getHerbalismXPMelon();
2012-03-10 19:03:31 +01:00
}
break;
case NETHER_WARTS:
if (data == (byte) 0x3) {
mat = Material.NETHER_STALK;
xp = Config.getInstance().getHerbalismXPNetherWart();
2012-03-10 19:03:31 +01:00
}
break;
case PUMPKIN:
case JACK_O_LANTERN:
if (!mcMMO.placeStore.isTrue(block)) {
2012-03-10 19:03:31 +01:00
mat = Material.getMaterial(id);
xp = Config.getInstance().getHerbalismXPPumpkin();
2012-03-10 19:03:31 +01:00
}
break;
case RED_ROSE:
case YELLOW_FLOWER:
if (!mcMMO.placeStore.isTrue(block)) {
2012-03-10 19:03:31 +01:00
mat = Material.getMaterial(id);
xp = Config.getInstance().getHerbalismXPFlowers();
2012-03-10 19:03:31 +01:00
}
break;
case SUGAR_CANE_BLOCK:
for (int y = 0; y <= 2; y++) {
Block b = block.getRelative(0, y, 0);
if (b.getType().equals(Material.SUGAR_CANE_BLOCK)) {
mat = Material.SUGAR_CANE;
if (!mcMMO.placeStore.isTrue(b)) {
if (herbLevel > MAX_BONUS_LEVEL || random.nextInt(randomChance) <= herbLevel) {
2012-03-10 19:03:31 +01:00
caneDrops++;
}
xp += Config.getInstance().getHerbalismXPSugarCane();
2012-03-10 19:03:31 +01:00
}
}
}
break;
case VINE:
if (!mcMMO.placeStore.isTrue(block)) {
2012-03-10 19:03:31 +01:00
mat = type;
xp = Config.getInstance().getHerbalismXPVines();
2012-03-10 19:03:31 +01:00
}
break;
case WATER_LILY:
if (!mcMMO.placeStore.isTrue(block)) {
2012-03-10 19:03:31 +01:00
mat = type;
xp = Config.getInstance().getHerbalismXPLilyPads();
2012-03-10 19:03:31 +01:00
}
break;
case COCOA:
if ((((byte) data) & 0x8) == 0x8) {
mat = Material.COCOA;
xp = Config.getInstance().getHerbalismXPCocoa();
}
break;
2012-03-10 19:03:31 +01:00
default:
if (Config.getInstance().getBlockModsEnabled() && CustomBlocksConfig.getInstance().customHerbalismBlocks.contains(new ItemStack(block.getTypeId(), 1, (short) 0, block.getData()))) {
customPlant = true;
xp = ModChecks.getCustomBlock(block).getXpGain();
}
2012-03-10 19:03:31 +01:00
break;
}
if (mat == null && !customPlant) {
return;
}
if (Permissions.getInstance().herbalismDoubleDrops(player)) {
ItemStack is = null;
if (customPlant) {
is = new ItemStack(ModChecks.getCustomBlock(block).getItemDrop());
}
else {
if (mat == Material.COCOA) {
is = new ItemStack(Material.INK_SACK, 1, (short) 3);
}
else {
is = new ItemStack(mat);
}
}
2012-03-10 19:03:31 +01:00
if (herbLevel > MAX_BONUS_LEVEL || random.nextInt(randomChance) <= herbLevel) {
Config configInstance = Config.getInstance();
switch (type) {
case BROWN_MUSHROOM:
if (configInstance.getBrownMushroomsDoubleDropsEnabled()) {
2012-07-04 21:35:14 +02:00
Misc.dropItem(location, is);
}
break;
case CACTUS:
if (configInstance.getCactiDoubleDropsEnabled()) {
2012-07-04 21:35:14 +02:00
Misc.dropItems(location, is, catciDrops);
}
break;
case CROPS:
if (configInstance.getWheatDoubleDropsEnabled()) {
2012-07-04 21:35:14 +02:00
Misc.dropItem(location, is);
}
break;
case MELON_BLOCK:
if (configInstance.getMelonsDoubleDropsEnabled()) {
2012-07-04 21:35:14 +02:00
Misc.dropItems(location, is, 3);
Misc.randomDropItems(location, is, 50, 4);
}
break;
case NETHER_WARTS:
if (configInstance.getNetherWartsDoubleDropsEnabled()) {
2012-07-04 21:35:14 +02:00
Misc.dropItems(location, is, 2);
Misc.randomDropItems(location, is, 50, 3);
}
break;
case PUMPKIN:
if (configInstance.getPumpkinsDoubleDropsEnabled()) {
2012-07-04 21:35:14 +02:00
Misc.dropItem(location, is);
}
break;
case RED_MUSHROOM:
if (configInstance.getRedMushroomsDoubleDropsEnabled()) {
2012-07-04 21:35:14 +02:00
Misc.dropItem(location, is);
}
break;
case SUGAR_CANE_BLOCK:
if (configInstance.getSugarCaneDoubleDropsEnabled()) {
2012-07-04 21:35:14 +02:00
Misc.dropItems(location, is, caneDrops);
}
break;
case VINE:
if (configInstance.getVinesDoubleDropsEnabled()) {
2012-07-04 21:35:14 +02:00
Misc.dropItem(location, is);
}
break;
case WATER_LILY:
if (configInstance.getWaterLiliesDoubleDropsEnabled()) {
2012-07-04 21:35:14 +02:00
Misc.dropItem(location, is);
}
break;
case YELLOW_FLOWER:
if (configInstance.getYellowFlowersDoubleDropsEnabled()) {
2012-07-04 21:35:14 +02:00
Misc.dropItem(location, is);
}
break;
case COCOA:
if (configInstance.getCocoaDoubleDropsEnabled()) {
2012-09-07 23:49:00 +02:00
Misc.dropItem(location, is);
}
break;
default:
if (customPlant) {
CustomBlock customBlock = ModChecks.getCustomBlock(block);
int minimumDropAmount = customBlock.getMinimumDropAmount();
int maximumDropAmount = customBlock.getMaximumDropAmount();
is = customBlock.getItemDrop();
if (minimumDropAmount != maximumDropAmount) {
2012-07-04 21:35:14 +02:00
Misc.dropItems(location, is, minimumDropAmount);
Misc.randomDropItems(location, is, 50, maximumDropAmount - minimumDropAmount);
}
else {
2012-07-04 21:35:14 +02:00
Misc.dropItems(location, is, minimumDropAmount);
}
}
break;
}
2012-03-10 19:03:31 +01:00
}
}
2012-07-03 16:04:04 +02:00
Skills.xpProcessing(player, profile, SkillType.HERBALISM, xp);
2012-01-09 20:00:13 +01:00
}
2012-03-10 19:03:31 +01:00
/**
* Apply the Green Thumb ability to crops.
2012-03-10 19:03:31 +01:00
*
* @param block The block to apply the ability to
* @param player The player using the ability
* @param event The event triggering the ability
* @param plugin mcMMO plugin instance
*/
private static void greenThumbWheat(Block block, Player player, BlockBreakEvent event, mcMMO plugin) {
2012-03-13 08:31:07 +01:00
final int MAX_BONUS_LEVEL = 1500;
2012-07-03 16:04:04 +02:00
PlayerProfile profile = Users.getProfile(player);
int herbLevel = profile.getSkillLevel(SkillType.HERBALISM);
2012-03-10 19:03:31 +01:00
PlayerInventory inventory = player.getInventory();
boolean hasSeeds = inventory.contains(Material.SEEDS);
2012-07-04 21:35:14 +02:00
Location location = block.getLocation();
2012-03-10 19:03:31 +01:00
int randomChance = 1500;
if (player.hasPermission("mcmmo.perks.lucky.herbalism")) {
randomChance = (int) (randomChance * 0.75);
}
2012-07-03 16:04:04 +02:00
if (hasSeeds && profile.getAbilityMode(AbilityType.GREEN_TERRA) || hasSeeds && (herbLevel > MAX_BONUS_LEVEL || random.nextInt(randomChance) <= herbLevel)) {
2012-03-10 19:03:31 +01:00
event.setCancelled(true);
2012-07-04 21:35:14 +02:00
Misc.dropItem(location, new ItemStack(Material.WHEAT));
Misc.randomDropItems(location, new ItemStack(Material.SEEDS), 50, 3);
2012-07-03 16:04:04 +02:00
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new GreenThumbTimer(block, profile), 1);
2012-03-10 19:03:31 +01:00
inventory.removeItem(new ItemStack(Material.SEEDS));
2012-05-15 06:49:19 +02:00
player.updateInventory(); // Needed until replacement available
2012-03-10 19:03:31 +01:00
}
2012-01-09 20:00:13 +01:00
}
/**
* Apply the Green Thumb ability to blocks.
*
* @param is The item in the player's hand
* @param player The player activating the ability
* @param block The block being used in the ability
*/
public static void greenThumbBlocks(ItemStack is, Player player, Block block) {
2012-03-13 08:31:07 +01:00
final int MAX_BONUS_LEVEL = 1500;
2012-07-03 16:04:04 +02:00
PlayerProfile profile = Users.getProfile(player);
int skillLevel = profile.getSkillLevel(SkillType.HERBALISM);
int seeds = is.getAmount();
player.setItemInHand(new ItemStack(Material.SEEDS, seeds - 1));
int randomChance = 1500;
if (player.hasPermission("mcmmo.perks.lucky.herbalism")) {
randomChance = (int) (randomChance * 0.75);
}
if (skillLevel > MAX_BONUS_LEVEL || random.nextInt(randomChance) <= skillLevel) {
greenTerraConvert(player, block);
}
else {
2012-05-06 09:55:15 +02:00
player.sendMessage(LocaleLoader.getString("Herbalism.Ability.GTh.Fail"));
}
}
2012-01-09 20:00:13 +01:00
}