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

288 lines
9.6 KiB
Java

package com.gmail.nossr50.skills;
import org.bukkit.Bukkit;
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;
import org.bukkit.inventory.PlayerInventory;
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;
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 (LoadProperties.enableSmoothToMossy && type.equals(Material.SMOOTH_BRICK)) {
block.setData((byte) 0x1);
}
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);
}
}
}
/**
* 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);
int herbLevel = PP.getSkillLevel(SkillType.HERBALISM);
int id = block.getTypeId();
Material type = block.getType();
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 (!plugin.misc.blockWatchList.contains(b)) {
if(herbLevel > 1000 || (Math.random() * 1000 <= herbLevel)) {
catciDrops++;
}
xp += LoadProperties.mcactus;
}
}
}
break;
case CROPS:
if (data == (byte) 0x7) {
mat = Material.WHEAT;
xp = LoadProperties.mwheat;
greenThumb(block, player, event, plugin);
}
break;
case MELON_BLOCK:
if (data != (byte) 0x5) {
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 (!plugin.misc.blockWatchList.contains(block)) {
mat = Material.getMaterial(id);
xp = LoadProperties.mpumpkin;
}
break;
case RED_ROSE:
case YELLOW_FLOWER:
if (!plugin.misc.blockWatchList.contains(block)) {
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 (!plugin.misc.blockWatchList.contains(b)) {
if(herbLevel > 1000 || (Math.random() * 1000 <= herbLevel)) {
caneDrops++;
}
xp += LoadProperties.msugar;
}
}
}
break;
case VINE:
if (!plugin.misc.blockWatchList.contains(block)) {
mat = type;
xp = LoadProperties.mvines;
}
break;
case WATER_LILY:
if (data != (byte) 0x5) {
mat = type;
xp = LoadProperties.mlilypad;
}
break;
default:
break;
}
if (mat == null) {
return;
}
else {
ItemStack is = new ItemStack(mat);
if (herbLevel > 1000 || (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);
}
}
PP.addXP(SkillType.HERBALISM, xp, player);
Skills.XpCheckSkill(SkillType.HERBALISM, player);
}
}
/**
* Apply the Green Thumb ability.
*
* @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 greenThumb(final Block block, Player player, BlockBreakEvent event, mcMMO plugin) {
final PlayerProfile PP = Users.getProfile(player);
int herbLevel = PP.getSkillLevel(SkillType.HERBALISM);
PlayerInventory inventory = player.getInventory();
boolean hasSeeds = inventory.contains(Material.SEEDS);
Location loc = block.getLocation();
if (hasSeeds && PP.getGreenTerraMode() || hasSeeds && (herbLevel >= 1500 || (Math.random() * 1500 <= herbLevel))) {
event.setCancelled(true);
m.mcDropItem(loc, new ItemStack(Material.WHEAT, 1));
m.mcDropItems(loc, new ItemStack(Material.SEEDS, 1), 3);
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
public void run() {
block.setType(Material.CROPS);
//This replants the wheat at a certain stage in development based on Herbalism Skill
if (!PP.getGreenTerraMode()) {
if (PP.getSkillLevel(SkillType.HERBALISM) >= 600)
block.setData((byte) 0x4);
else if (PP.getSkillLevel(SkillType.HERBALISM) >= 400)
block.setData((byte) 0x3);
else if (PP.getSkillLevel(SkillType.HERBALISM) >= 200)
block.setData((byte) 0x2);
else
block.setData((byte) 0x1);
}
else
block.setData((byte) 0x4);
}
}, 1);
inventory.removeItem(new ItemStack(Material.SEEDS, 1));
player.updateInventory();
}
}
}