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

304 lines
10 KiB
Java
Raw Normal View History

2012-01-09 20:00:13 +01:00
package com.gmail.nossr50.skills;
import org.bukkit.Bukkit;
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;
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.config.LoadProperties;
import com.gmail.nossr50.datatypes.PlayerProfile;
import com.gmail.nossr50.datatypes.SkillType;
import com.gmail.nossr50.locale.mcLocale;
import com.gmail.nossr50.runnables.GreenThumbTimer;
2012-01-09 20:00:13 +01:00
2012-03-10 19:03:31 +01:00
public class Herbalism {
/**
* 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);
Material type = block.getType();
if (!hasSeeds) {
player.sendMessage("You need more seeds to spread Green Terra");
}
else if (hasSeeds && !block.getType().equals(Material.WHEAT)) {
inventory.removeItem(new ItemStack(Material.SEEDS, 1));
player.updateInventory();
if (m.blockBreakSimulate(block, player, false)) {
if (LoadProperties.enableSmoothToMossy && type.equals(Material.SMOOTH_BRICK)) {
2012-03-13 08:31:07 +01:00
block.setData((byte) 0x1); //Set type of the brick to mossy
}
else if (LoadProperties.enableDirtToGrass && type.equals(Material.DIRT)) {
block.setType(Material.GRASS);
}
else if (LoadProperties.enableCobbleToMossy && type.equals(Material.COBBLESTONE)) {
block.setType(Material.MOSSY_COBBLESTONE);
}
2012-03-10 19:03:31 +01:00
}
}
}
/**
* Check if a block can be made mossy.
*
* @param material The type of Block to check
* @return true if the block can be made mossy, false otherwise
*/
public static Boolean makeMossy(Material type) {
switch (type) {
case COBBLESTONE:
case DIRT:
case SMOOTH_BRICK:
return true;
default:
return false;
}
}
/**
* Check if a block is affected by Herbalism abilities.
*
* @param type The type of Block to check
* @return true if the block is affected, false otherwise
*/
public static Boolean canBeGreenTerra(Material type){
switch (type) {
case BROWN_MUSHROOM:
case CACTUS:
case CROPS:
case JACK_O_LANTERN:
case MELON_BLOCK:
case PUMPKIN:
case RED_MUSHROOM:
case RED_ROSE:
case SUGAR_CANE_BLOCK:
case VINE:
case WATER_LILY:
case YELLOW_FLOWER:
return true;
default:
return false;
}
}
/**
* 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) {
final PlayerProfile PP = Users.getProfile(player);
2012-03-13 08:31:07 +01:00
final int MAX_BONUS_LEVEL = 1000;
2012-03-10 19:03:31 +01:00
int herbLevel = PP.getSkillLevel(SkillType.HERBALISM);
int id = block.getTypeId();
Material type = block.getType();
2012-03-10 19:03:31 +01:00
Byte data = block.getData();
Location loc = block.getLocation();
Material mat = null;
int xp = 0;
int catciDrops = 0;
int caneDrops = 0;
switch (type) {
case BROWN_MUSHROOM:
case RED_MUSHROOM:
if (data != (byte) 0x5) {
mat = Material.getMaterial(id);
xp = LoadProperties.mmushroom;
}
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 (!b.hasMetadata("placedBlock")) {
2012-03-13 08:31:07 +01:00
if(herbLevel > MAX_BONUS_LEVEL || (Math.random() * 1000 <= herbLevel)) {
2012-03-10 19:03:31 +01:00
catciDrops++;
}
xp += LoadProperties.mcactus;
}
}
}
break;
case CROPS:
if (data == CropState.RIPE.getData()) {
2012-03-10 19:03:31 +01:00
mat = Material.WHEAT;
xp = LoadProperties.mwheat;
greenThumbWheat(block, player, event, plugin);
2012-03-10 19:03:31 +01:00
}
break;
case MELON_BLOCK:
if (!block.hasMetadata("placedBlock")) {
2012-03-10 19:03:31 +01:00
mat = Material.MELON;
xp = LoadProperties.mmelon;
}
break;
case NETHER_WARTS:
if (data == (byte) 0x3) {
mat = Material.NETHER_STALK;
xp = LoadProperties.mnetherwart;
}
break;
case PUMPKIN:
case JACK_O_LANTERN:
if (!block.hasMetadata("placedBlock")) {
2012-03-10 19:03:31 +01:00
mat = Material.getMaterial(id);
xp = LoadProperties.mpumpkin;
}
break;
case RED_ROSE:
case YELLOW_FLOWER:
if (!block.hasMetadata("placedBlock")) {
2012-03-10 19:03:31 +01:00
mat = Material.getMaterial(id);
xp = LoadProperties.mflower;
}
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 (!b.hasMetadata("placedBlock")) {
2012-03-13 08:31:07 +01:00
if(herbLevel > MAX_BONUS_LEVEL || (Math.random() * 1000 <= herbLevel)) {
2012-03-10 19:03:31 +01:00
caneDrops++;
}
xp += LoadProperties.msugar;
}
}
}
break;
case VINE:
if (!block.hasMetadata("placedBlock")) {
2012-03-10 19:03:31 +01:00
mat = type;
xp = LoadProperties.mvines;
}
break;
case WATER_LILY:
if (!block.hasMetadata("placedBlock")) {
2012-03-10 19:03:31 +01:00
mat = type;
xp = LoadProperties.mlilypad;
}
break;
default:
break;
}
if (mat == null) {
return;
}
else {
ItemStack is = new ItemStack(mat);
2012-03-10 19:03:31 +01:00
2012-03-13 08:31:07 +01:00
if (herbLevel > MAX_BONUS_LEVEL || (Math.random() * 1000 <= herbLevel)) {
if (type.equals(Material.CACTUS)) {
m.mcDropItems(loc, is, catciDrops);
}
else if (type.equals(Material.MELON_BLOCK)) {
m.mcDropItems(loc, is, 3);
m.mcRandomDropItems(loc, is, 50, 4);
}
else if (type.equals(Material.NETHER_WARTS)) {
m.mcDropItems(loc, is, 2);
m.mcRandomDropItems(loc, is, 50, 3);
}
else if (type.equals(Material.SUGAR_CANE_BLOCK)) {
m.mcDropItems(loc, is, caneDrops);
}
else {
m.mcDropItem(loc, is);
}
2012-03-10 19:03:31 +01:00
}
PP.addXP(SkillType.HERBALISM, xp, player);
Skills.XpCheckSkill(SkillType.HERBALISM, player);
}
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;
PlayerProfile PP = Users.getProfile(player);
2012-03-10 19:03:31 +01:00
int herbLevel = PP.getSkillLevel(SkillType.HERBALISM);
PlayerInventory inventory = player.getInventory();
boolean hasSeeds = inventory.contains(Material.SEEDS);
Location loc = block.getLocation();
2012-03-13 08:31:07 +01:00
if (hasSeeds && PP.getGreenTerraMode() || hasSeeds && (herbLevel > MAX_BONUS_LEVEL || (Math.random() * 1500 <= herbLevel))) {
2012-03-10 19:03:31 +01:00
event.setCancelled(true);
2012-03-10 19:03:31 +01:00
m.mcDropItem(loc, new ItemStack(Material.WHEAT, 1));
m.mcRandomDropItems(loc, new ItemStack(Material.SEEDS, 1), 50, 3);
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new GreenThumbTimer(block, PP), 1);
2012-03-10 19:03:31 +01:00
inventory.removeItem(new ItemStack(Material.SEEDS, 1));
player.updateInventory();
}
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;
PlayerProfile PP = Users.getProfile(player);
int skillLevel = PP.getSkillLevel(SkillType.HERBALISM);
int seeds = is.getAmount();
player.setItemInHand(new ItemStack(Material.SEEDS, seeds - 1));
2012-03-13 08:31:07 +01:00
if (skillLevel > MAX_BONUS_LEVEL || Math.random() * 1500 <= skillLevel) {
greenTerra(player, block);
}
else {
player.sendMessage(mcLocale.getString("mcPlayerListener.GreenThumbFail"));
}
}
2012-01-09 20:00:13 +01:00
}