Change from using Block to BlockState in many locations

Convert Herbalism ability to use BlockState instead of Block.
Move all block checks back to BlockChecks.
Don't need this if we're using BlockState
Convert Excavation to BlockState. We don't need to return booleans here
because we never edit the block state.Switch ModCheck.getCustomBlock to use BlockState
More work on the conversion to BlockState
More conversion to BlockState
Better way to handle mining drops, I believe.
Remove useless imports.
A test of making the diff look nicer
BlockChecks diff cleanup
Herbalism diff cleanup
Gotta update the block states here.
Moar blockstate.
Little more blockState stuff.
Even more blockstate.
This commit is contained in:
NuclearW
2013-02-22 11:23:46 -05:00
parent 513a9212e4
commit d052d7a3ce
24 changed files with 994 additions and 1037 deletions

View File

@ -6,13 +6,14 @@ import java.util.List;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.Tree;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.datatypes.McMMOPlayer;
import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.mods.ModChecks;
import com.gmail.nossr50.mods.datatypes.CustomBlock;
@ -22,6 +23,7 @@ import com.gmail.nossr50.skills.utilities.SkillType;
import com.gmail.nossr50.skills.woodcutting.Woodcutting.ExperienceGainMethod;
import com.gmail.nossr50.util.BlockChecks;
import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.Users;
public final class TreeFeller {
private static boolean treeFellerReachedThreshold = false;
@ -32,26 +34,24 @@ public final class TreeFeller {
* Begins Tree Feller
*
* @param mcMMOPlayer Player using Tree Feller
* @param block Block being broken
* @param blockState Block being broken
*/
public static void process(McMMOPlayer mcMMOPlayer, Block block) {
List<Block> treeFellerBlocks = new ArrayList<Block>();
protected static void process(BlockState blockState, Player player) {
List<BlockState> treeFellerBlocks = new ArrayList<BlockState>();
processRecursively(block, treeFellerBlocks);
processRecursively(blockState, treeFellerBlocks);
// If the player is trying to break to many block
// If the player is trying to break too many blocks
if (treeFellerReachedThreshold) {
treeFellerReachedThreshold = false;
mcMMOPlayer.getPlayer().sendMessage(LocaleLoader.getString("Woodcutting.Skills.TreeFellerThreshold"));
player.sendMessage(LocaleLoader.getString("Woodcutting.Skills.TreeFellerThreshold"));
return;
}
Player player = mcMMOPlayer.getPlayer();
// If the tool can't sustain the durability loss
if (!handleDurabilityLoss(treeFellerBlocks, player.getItemInHand())) {
mcMMOPlayer.getPlayer().sendMessage(LocaleLoader.getString("Woodcutting.Skills.TreeFeller.Splinter"));
player.sendMessage(LocaleLoader.getString("Woodcutting.Skills.TreeFeller.Splinter"));
int health = player.getHealth();
@ -62,28 +62,28 @@ public final class TreeFeller {
return;
}
dropBlocks(treeFellerBlocks, mcMMOPlayer);
dropBlocks(treeFellerBlocks, player);
}
/**
* Processes Tree Feller
*
* @param block Block being checked
* @param blockState Block being checked
* @param treeFellerBlocks List of blocks to be removed
*/
private static void processRecursively(Block block, List<Block> treeFellerBlocks) {
if (!BlockChecks.isLog(block)) {
private static void processRecursively(BlockState blockState, List<BlockState> treeFellerBlocks) {
if (!BlockChecks.isLog(blockState)) {
return;
}
List<Block> futureCenterBlocks = new ArrayList<Block>();
World world = block.getWorld();
List<BlockState> futureCenterBlocks = new ArrayList<BlockState>();
World world = blockState.getWorld();
// Handle the blocks around 'block'
for (int y = 0 ; y <= 1 ; y++) {
for (int x = -1 ; x <= 1 ; x++) {
for (int z = -1 ; z <= 1 ; z++) {
Block nextBlock = world.getBlockAt(block.getLocation().add(x, y, z));
for (int y = 0; y <= 1; y++) {
for (int x = -1; x <= 1; x++) {
for (int z = -1; z <= 1; z++) {
BlockState nextBlock = world.getBlockAt(blockState.getLocation().add(x, y, z)).getState();
handleBlock(nextBlock, futureCenterBlocks, treeFellerBlocks);
@ -95,12 +95,12 @@ public final class TreeFeller {
}
// Recursive call for each log found
for (Block futurCenterBlock : futureCenterBlocks) {
for (BlockState futureCenterBlock : futureCenterBlocks) {
if (treeFellerReachedThreshold) {
return;
}
processRecursively(futurCenterBlock, treeFellerBlocks);
processRecursively(futureCenterBlock, treeFellerBlocks);
}
}
@ -108,23 +108,23 @@ public final class TreeFeller {
* Handle a block addition to the list of blocks to be removed
* and to the list of blocks used for future recursive calls of 'processRecursively()'
*
* @param block Block to be added
* @param blockState Block to be added
* @param futureCenterBlocks List of blocks that will be used to call 'processRecursively()'
* @param treeFellerBlocks List of blocks to be removed
*/
private static void handleBlock(Block block, List<Block> futureCenterBlocks, List<Block> treeFellerBlocks) {
if (!BlockChecks.treeFellerCompatible(block) || mcMMO.placeStore.isTrue(block) || treeFellerBlocks.contains(block)) {
private static void handleBlock(BlockState blockState, List<BlockState> futureCenterBlocks, List<BlockState> treeFellerBlocks) {
if (!BlockChecks.affectedByTreeFeller(blockState) || mcMMO.placeStore.isTrue(blockState) || treeFellerBlocks.contains(blockState)) {
return;
}
treeFellerBlocks.add(block);
treeFellerBlocks.add(blockState);
if (treeFellerBlocks.size() > Woodcutting.CONFIG.getTreeFellerThreshold()) {
if (treeFellerBlocks.size() > Config.getInstance().getTreeFellerThreshold()) {
treeFellerReachedThreshold = true;
return;
}
futureCenterBlocks.add(block);
futureCenterBlocks.add(blockState);
}
/**
@ -134,15 +134,15 @@ public final class TreeFeller {
* @param inHand tool being used
* @return True if the tool can sustain the durability loss
*/
private static boolean handleDurabilityLoss(List<Block> treeFellerBlocks, ItemStack inHand) {
private static boolean handleDurabilityLoss(List<BlockState> treeFellerBlocks, ItemStack inHand) {
Material inHandMaterial = inHand.getType();
if (inHandMaterial != Material.AIR) {
short durabilityLoss = 0;
int unbreakingLevel = inHand.getEnchantmentLevel(Enchantment.DURABILITY);
for (Block block : treeFellerBlocks) {
if (BlockChecks.isLog(block) && Misc.getRandom().nextInt(unbreakingLevel + 1) == 0) {
for (BlockState blockState : treeFellerBlocks) {
if (BlockChecks.isLog(blockState) && Misc.getRandom().nextInt(unbreakingLevel + 1) == 0) {
durabilityLoss += SkillTools.toolDurabilityLoss;
}
}
@ -165,87 +165,74 @@ public final class TreeFeller {
* Handles the dropping of blocks
*
* @param treeFellerBlocks List of blocks to be dropped
* @param mcMMOPlayer Player using the ability
* @param player Player using the ability
*/
private static void dropBlocks(List<Block> treeFellerBlocks, McMMOPlayer mcMMOPlayer) {
private static void dropBlocks(List<BlockState> treeFellerBlocks, Player player) {
int xp = 0;
for (Block block : treeFellerBlocks) {
if (!SkillTools.blockBreakSimulate(block, mcMMOPlayer.getPlayer(), true)) {
for (BlockState blockState : treeFellerBlocks) {
if (!SkillTools.blockBreakSimulate(blockState.getBlock(), player, true)) {
break; // TODO: Shouldn't we use continue instead?
}
Material material = block.getType();
Material material = blockState.getType();
switch (material) {
case HUGE_MUSHROOM_1:
case HUGE_MUSHROOM_2:
try {
xp += Woodcutting.getExperienceFromLog(block, ExperienceGainMethod.TREE_FELLER);
}
catch (IllegalArgumentException exception) {
break;
}
xp += Woodcutting.getExperienceFromLog(blockState, ExperienceGainMethod.TREE_FELLER);
// Stems have a block data value of 15 and should not drop mushrooms
// 0-2 mushrooms drop when you break a block
if (block.getData() == (byte) 15) {
break;
}
if (material == Material.HUGE_MUSHROOM_1) {
Misc.randomDropItems(block.getLocation(), new ItemStack(Material.BROWN_MUSHROOM), 2);
}
else {
Misc.randomDropItems(block.getLocation(), new ItemStack(Material.RED_MUSHROOM), 2);
for (ItemStack drop : blockState.getBlock().getDrops()) {
Misc.dropItem(blockState.getLocation(), drop);
}
break;
case LOG:
Woodcutting.checkForDoubleDrop(mcMMOPlayer, block);
try {
xp += Woodcutting.getExperienceFromLog(block, ExperienceGainMethod.TREE_FELLER);
}
catch (IllegalArgumentException exception) {
break;
}
Misc.dropItem(block.getLocation(), new ItemStack(Material.LOG, 1, Woodcutting.extractLogItemData(block.getData())));
break;
case LEAVES:
Misc.randomDropItem(block.getLocation(), new ItemStack(Material.SAPLING, 1, Woodcutting.extractLogItemData(block.getData())), 10);
break;
default:
if (ModChecks.isCustomLogBlock(block)) {
Woodcutting.checkForDoubleDrop(mcMMOPlayer, block);
CustomBlock customBlock = ModChecks.getCustomBlock(block);
xp = customBlock.getXpGain();
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, maximumDropAmount - minimumDropAmount);
}
}
else if (ModChecks.isCustomLeafBlock(block)) {
CustomBlock customBlock = ModChecks.getCustomBlock(block);
Misc.randomDropItem(block.getLocation(), customBlock.getItemDrop(), 10);
}
break;
}
block.setData((byte) 0);
block.setType(Material.AIR);
if (ModChecks.isCustomLogBlock(blockState)) {
Woodcutting.checkForDoubleDrop(player, blockState);
CustomBlock customBlock = ModChecks.getCustomBlock(blockState);
xp = customBlock.getXpGain();
int minimumDropAmount = customBlock.getMinimumDropAmount();
int maximumDropAmount = customBlock.getMaximumDropAmount();
Location location = blockState.getLocation();
ItemStack item = customBlock.getItemDrop();;
Misc.dropItems(location, item, minimumDropAmount);
if (minimumDropAmount < maximumDropAmount) {
Misc.randomDropItems(location, item, maximumDropAmount - minimumDropAmount);
}
}
else if (ModChecks.isCustomLeafBlock(blockState)) {
Misc.randomDropItem(blockState.getLocation(), ModChecks.getCustomBlock(blockState).getItemDrop(), 10);
}
Tree tree = (Tree) blockState.getData();
switch (material) {
case LOG:
Woodcutting.checkForDoubleDrop(player, blockState);
xp += Woodcutting.getExperienceFromLog(blockState, ExperienceGainMethod.TREE_FELLER);
Misc.dropItem(blockState.getLocation(), new ItemStack(Material.LOG, 1, tree.getSpecies().getData()));
break;
case LEAVES:
Misc.randomDropItem(blockState.getLocation(), new ItemStack(Material.SAPLING, 1, tree.getSpecies().getData()), 10);
break;
default:
break;
}
blockState.setRawData((byte) 0x0);
blockState.setType(Material.AIR);
blockState.update();
}
mcMMOPlayer.beginXpGain(SkillType.WOODCUTTING, xp);
Users.getPlayer(player).beginXpGain(SkillType.WOODCUTTING, xp);
}
}

View File

@ -1,29 +1,27 @@
package com.gmail.nossr50.skills.woodcutting;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.TreeSpecies;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.Tree;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.config.AdvancedConfig;
import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.datatypes.McMMOPlayer;
import com.gmail.nossr50.events.fake.FakePlayerAnimationEvent;
import com.gmail.nossr50.mods.ModChecks;
import com.gmail.nossr50.mods.datatypes.CustomBlock;
import com.gmail.nossr50.skills.utilities.PerksUtils;
import com.gmail.nossr50.skills.utilities.SkillTools;
import com.gmail.nossr50.skills.utilities.SkillType;
import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.Users;
public final class Woodcutting {
static final AdvancedConfig ADVANCED_CONFIG = AdvancedConfig.getInstance();
static final Config CONFIG = Config.getInstance();
public static double doubleDropsMaxChance = AdvancedConfig.getInstance().getWoodcuttingDoubleDropChance();
public static int doubleDropsMaxLevel = AdvancedConfig.getInstance().getWoodcuttingDoubleDropMaxLevel();
protected enum ExperienceGainMethod {
DEFAULT,
@ -38,8 +36,8 @@ public final class Woodcutting {
* @param mcMMOPlayer Player using the ability
* @param block Block being broken
*/
public static void beginTreeFeller(McMMOPlayer mcMMOPlayer, Block block) {
TreeFeller.process(mcMMOPlayer, block);
public static void beginTreeFeller(BlockState blockState, Player player) {
TreeFeller.process(blockState, player);
}
/**
@ -48,10 +46,9 @@ public final class Woodcutting {
* @param player Player using the ability
* @param block Block being broken
*/
public static void beginLeafBlower(Player player, Block block) {
public static void beginLeafBlower(Player player, BlockState blockState) {
mcMMO.p.getServer().getPluginManager().callEvent(new FakePlayerAnimationEvent(player));
player.playSound(block.getLocation(), Sound.ITEM_PICKUP, Misc.POP_VOLUME, Misc.POP_PITCH);
player.playSound(blockState.getLocation(), Sound.ITEM_PICKUP, Misc.POP_VOLUME, Misc.POP_PITCH);
}
/**
@ -60,75 +57,61 @@ public final class 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);
}
catch (IllegalArgumentException exception) {
return;
}
}
Player player = mcMMOPlayer.getPlayer();
public static void beginWoodcutting(Player player, BlockState blockState) {
int xp = getExperienceFromLog(blockState, ExperienceGainMethod.DEFAULT);
if (Permissions.doubleDrops(player, SkillType.WOODCUTTING)) {
checkForDoubleDrop(mcMMOPlayer, block);
checkForDoubleDrop(player, blockState);
}
mcMMOPlayer.beginXpGain(SkillType.WOODCUTTING, xp);
Users.getPlayer(player).beginXpGain(SkillType.WOODCUTTING, xp);
}
/**
* Retrieves the experience reward from a log
*
* @param log Log being broken
* @param blockState 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) {
protected static int getExperienceFromLog(BlockState blockState, ExperienceGainMethod experienceGainMethod) {
// Mushrooms aren't trees so we could never get species data from them
switch (log.getType()) {
switch (blockState.getType()) {
case HUGE_MUSHROOM_1:
return Config.getInstance().getWoodcuttingXPHugeBrownMushroom();
case HUGE_MUSHROOM_2:
return Config.getInstance().getWoodcuttingXPHugeRedMushroom();
default:
break;
}
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();
if (ModChecks.isCustomLogBlock(blockState)) {
return ModChecks.getCustomBlock(blockState).getXpGain();
}
switch (logType) {
switch (((Tree) blockState.getData()).getSpecies()) {
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;
if (experienceGainMethod == ExperienceGainMethod.TREE_FELLER) {
xp *= 0.5;
}
return xp;
default:
throw new IllegalArgumentException();
return 0;
}
}
@ -136,28 +119,18 @@ public final class Woodcutting {
* Checks for double drops
*
* @param mcMMOPlayer Player breaking the block
* @param block Block being broken
* @param blockState 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 = PerksUtils.handleLuckyPerks(player, SkillType.WOODCUTTING);
if (probability > configDoubleDropChance) {
probability = (int) configDoubleDropChance;
}
if (probability <= Misc.getRandom().nextInt(activationChance)) {
protected static void checkForDoubleDrop(Player player, BlockState blockState) {
if (!SkillTools.activationSuccessful(player, SkillType.WOODCUTTING, doubleDropsMaxChance, doubleDropsMaxLevel)) {
return;
}
if (Config.getInstance().getBlockModsEnabled() && ModChecks.isCustomLogBlock(block)) {
CustomBlock customBlock = ModChecks.getCustomBlock(block);
if (ModChecks.isCustomLogBlock(blockState)) {
CustomBlock customBlock = ModChecks.getCustomBlock(blockState);
int minimumDropAmount = customBlock.getMinimumDropAmount();
int maximumDropAmount = customBlock.getMaximumDropAmount();
Location location = block.getLocation();
Location location = blockState.getLocation();
ItemStack item = customBlock.getItemDrop();
Misc.dropItems(location, item, minimumDropAmount);
@ -167,44 +140,37 @@ public final class Woodcutting {
}
}
else {
byte itemData = extractLogItemData(block.getData());
Location location = block.getLocation();
ItemStack item = new ItemStack(Material.LOG, 1, itemData);
Location location = blockState.getLocation();
ItemStack item = blockState.getData().toItemStack();
switch (TreeSpecies.getByData(itemData)) {
switch (((Tree) blockState.getData()).getSpecies()) {
case GENERIC:
if (Config.getInstance().getOakDoubleDropsEnabled()) {
Misc.dropItem(location, item);
}
break;
return;
case REDWOOD:
if (Config.getInstance().getSpruceDoubleDropsEnabled()) {
Misc.dropItem(location, item);
}
break;
return;
case BIRCH:
if (Config.getInstance().getBirchDoubleDropsEnabled()) {
Misc.dropItem(location, item);
}
break;
return;
case JUNGLE:
if (Config.getInstance().getJungleDoubleDropsEnabled()) {
Misc.dropItem(location, item);
}
break;
return;
default:
break;
return;
}
}
}
/**
* 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);
}
}

View File

@ -29,8 +29,7 @@ public class WoodcuttingCommand extends SkillCommand {
treeFellerLengthEndurance = treeFellerStrings[1];
//DOUBLE DROPS
AdvancedConfig advancedConfig = AdvancedConfig.getInstance();
String[] doubleDropStrings = calculateAbilityDisplayValues(advancedConfig.getWoodcuttingDoubleDropMaxLevel(), advancedConfig.getWoodcuttingDoubleDropChance());
String[] doubleDropStrings = calculateAbilityDisplayValues(Woodcutting.doubleDropsMaxLevel, Woodcutting.doubleDropsMaxChance);
doubleDropChance = doubleDropStrings[0];
doubleDropChanceLucky = doubleDropStrings[1];
}