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,11 +6,11 @@ import java.util.List;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.inventory.ItemStack;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.datatypes.McMMOPlayer;
import com.gmail.nossr50.util.BlockChecks;
import com.gmail.nossr50.util.Misc;
@ -20,9 +20,9 @@ public class BlastMiningDropEventHandler {
private EntityExplodeEvent event;
private float yield;
private List<Block> blocks;
private List<Block> ores = new ArrayList<Block>();
private List<Block> debris = new ArrayList<Block>();
private List<Block> droppedOres = new ArrayList<Block>();
private List<BlockState> ores = new ArrayList<BlockState>();
private List<BlockState> debris = new ArrayList<BlockState>();
private List<BlockState> droppedOres = new ArrayList<BlockState>();
private float oreBonus;
private float debrisReduction;
private int dropMultiplier;
@ -38,38 +38,35 @@ public class BlastMiningDropEventHandler {
protected void sortExplosionBlocks() {
for (Block block : blocks) {
if (BlockChecks.isOre(block)) {
ores.add(block);
BlockState blockState = block.getState();
if (BlockChecks.isOre(blockState)) {
ores.add(blockState);
}
else {
debris.add(block);
debris.add(blockState);
}
}
}
protected void processXPGain() {
McMMOPlayer mcMMOPlayer = manager.getMcMMOPlayer();
for (Block block : droppedOres) {
if (!mcMMO.placeStore.isTrue(block)) {
Mining.miningXP(mcMMOPlayer, block, block.getType());
for (BlockState blockState : droppedOres) {
if (!mcMMO.placeStore.isTrue(blockState)) {
Mining.awardMiningXp(blockState, manager.getMcMMOPlayer().getPlayer());
}
}
}
protected void processDroppedBlocks() {
for (Block block : ores) {
Location location = block.getLocation();
Material type = block.getType();
for (BlockState blockState : ores) {
if (Misc.getRandom().nextFloat() < (yield + oreBonus)) {
droppedOres.add(block);
Mining.miningDrops(block, location, type);
droppedOres.add(blockState);
Mining.handleMiningDrops(blockState);
if (!mcMMO.placeStore.isTrue(block)) {
if (!mcMMO.placeStore.isTrue(blockState)) {
for (int i = 1 ; i < dropMultiplier ; i++) {
droppedOres.add(block);
Mining.miningDrops(block, location, type);
droppedOres.add(blockState);
Mining.handleMiningDrops(blockState);
}
}
}
@ -78,9 +75,9 @@ public class BlastMiningDropEventHandler {
float debrisYield = yield - debrisReduction;
if (debrisYield > 0) {
for (Block block : debris) {
Location location = block.getLocation();
Material type = block.getType();
for (BlockState blockState : debris) {
Location location = blockState.getLocation();
Material type = blockState.getType();
if (Misc.getRandom().nextFloat() < debrisYield) {
Misc.dropItem(location, new ItemStack(type));

View File

@ -1,19 +1,21 @@
package com.gmail.nossr50.skills.mining;
import org.bukkit.CoalType;
import org.bukkit.DyeColor;
import org.bukkit.Location;
import org.bukkit.Material;
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 com.gmail.nossr50.config.AdvancedConfig;
import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.datatypes.McMMOPlayer;
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;
import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.Users;
public class Mining {
private static AdvancedConfig advancedConfig = AdvancedConfig.getInstance();
@ -21,44 +23,58 @@ public class Mining {
public static int doubleDropsMaxLevel = advancedConfig.getMiningDoubleDropMaxLevel();
public static double doubleDropsMaxChance = advancedConfig.getMiningDoubleDropChance();
public static final int DIAMOND_TOOL_TIER = 4;
public static final int IRON_TOOL_TIER = 3;
public static final int STONE_TOOL_TIER = 2;
/**
* Process double drops & XP gain for Mining.
*
* @param blockState The {@link BlockState} to check ability activation for
* @param player The {@link Player} using this ability
*/
public static void miningBlockCheck(BlockState blockState, Player player) {
awardMiningXp(blockState, player);
if (Permissions.doubleDrops(player, SkillType.MINING) && SkillTools.activationSuccessful(player, SkillType.MINING, doubleDropsMaxChance, doubleDropsMaxLevel)) {
if (player.getItemInHand().containsEnchantment(Enchantment.SILK_TOUCH)) {
handleSilkTouchDrops(blockState);
}
else {
handleMiningDrops(blockState);
}
}
}
/**
* Award XP for Mining blocks.
* Award XP gain for Mining.
*
* @param mcMMOPlayer The player to award XP to
* @param block The block to award XP for
* @param blockState The {@link BlockState} to check ability activation for
* @param player The {@link Player} using this ability
*/
protected static void miningXP(McMMOPlayer mcMMOPlayer, Block block, Material type) {
int xp = Config.getInstance().getXp(SkillType.MINING, type);
protected static void awardMiningXp(BlockState blockState, Player player) {
Material blockType = blockState.getType();
int xp = Config.getInstance().getXp(SkillType.MINING, blockType);
if (type == Material.GLOWING_REDSTONE_ORE) {
if (blockType == Material.GLOWING_REDSTONE_ORE) {
xp = Config.getInstance().getXp(SkillType.MINING, Material.REDSTONE_ORE);
}
else if (xp == 0 && ModChecks.isCustomMiningBlock(block)) {
xp = ModChecks.getCustomBlock(block).getXpGain();
else if (xp == 0 && ModChecks.isCustomMiningBlock(blockState)) {
xp = ModChecks.getCustomBlock(blockState).getXpGain();
}
mcMMOPlayer.beginXpGain(SkillType.MINING, xp);
Users.getPlayer(player).beginXpGain(SkillType.MINING, xp);
}
/**
* Handle double drops when using Silk Touch.
*
* @param block The block to process drops for
* @param location The location of the block
* @param type The material type of the block
* @param blockState The {@link BlockState} to check ability activation for
*/
protected static void silkTouchDrops(Block block, Location location, Material type) {
ItemStack item = new ItemStack(type);
protected static void handleSilkTouchDrops(BlockState blockState) {
Material blockType = blockState.getType();
if (type != Material.GLOWING_REDSTONE_ORE && !Config.getInstance().getDoubleDropsEnabled(SkillType.MINING, type)) {
if (blockType != Material.GLOWING_REDSTONE_ORE && !Config.getInstance().getDoubleDropsEnabled(SkillType.MINING, blockType)) {
return;
}
switch (type) {
switch (blockType) {
case ENDER_STONE:
case GOLD_ORE:
case IRON_ORE:
@ -66,14 +82,14 @@ public class Mining {
case NETHERRACK:
case OBSIDIAN:
case SANDSTONE:
miningDrops(block, location, type);
break;
handleMiningDrops(blockState);
return;
case GLOWING_REDSTONE_ORE:
if (Config.getInstance().getDoubleDropsEnabled(SkillType.MINING, Material.REDSTONE_ORE)) {
Misc.dropItem(location, item);
Misc.dropItem(blockState.getLocation(), new ItemStack(Material.REDSTONE_ORE));
}
break;
return;
case COAL_ORE:
case DIAMOND_ORE:
@ -82,75 +98,39 @@ public class Mining {
case LAPIS_ORE:
case STONE:
case EMERALD_ORE:
Misc.dropItem(location, item);
break;
Misc.dropItem(blockState.getLocation(), new ItemStack(blockType));
return;
default:
if (ModChecks.isCustomMiningBlock(block)) {
ItemStack dropItem = new ItemStack(block.getTypeId(), 1, block.getData());
Misc.dropItem(location, dropItem);
if (ModChecks.isCustomMiningBlock(blockState)) {
Misc.dropItem(blockState.getLocation(), blockState.getData().toItemStack());
}
break;
return;
}
}
/**
* Drop items from Mining & Blast Mining skills.
* Handle double drops from Mining & Blast Mining.
*
* @param block The block to process drops for
* @param location The location of the block
* @param type The material type of the block
* @param blockState The {@link BlockState} to check ability activation for
*/
protected static void miningDrops(Block block, Location location, Material type) {
if (type != Material.GLOWING_REDSTONE_ORE && !Config.getInstance().getDoubleDropsEnabled(SkillType.MINING, type)) {
protected static void handleMiningDrops(BlockState blockState) {
Material blockType = blockState.getType();
if (blockType != Material.GLOWING_REDSTONE_ORE && !Config.getInstance().getDoubleDropsEnabled(SkillType.MINING, blockType)) {
return;
}
ItemStack item = new ItemStack(type);
Location location = blockState.getLocation();
ItemStack dropItem;
switch (type) {
switch (blockType) {
case COAL_ORE:
item = new ItemStack(Material.COAL, 1, CoalType.COAL.getData());
Misc.dropItem(location, item);
break;
case DIAMOND_ORE:
item = new ItemStack(Material.DIAMOND);
Misc.dropItem(location, item);
break;
case EMERALD_ORE:
item = new ItemStack(Material.EMERALD);
Misc.dropItem(location, item);
break;
case GLOWING_REDSTONE_ORE:
case REDSTONE_ORE:
if (Config.getInstance().getDoubleDropsEnabled(SkillType.MINING, Material.REDSTONE_ORE)) {
item = new ItemStack(Material.REDSTONE);
Misc.dropItems(location, item, 4);
Misc.randomDropItem(location, item, 50);
}
break;
case GLOWSTONE:
item = new ItemStack(Material.GLOWSTONE_DUST);
Misc.dropItems(location, item, 2);
Misc.randomDropItems(location, item, 2);
break;
case LAPIS_ORE:
item = new ItemStack(Material.INK_SACK, 1, DyeColor.BLUE.getDyeData());
Misc.dropItems(location, item, 4);
Misc.randomDropItems(location, item, 4);
break;
case STONE:
item = new ItemStack(Material.COBBLESTONE);
Misc.dropItem(location, item);
break;
case ENDER_STONE:
case GOLD_ORE:
case IRON_ORE:
@ -158,26 +138,36 @@ public class Mining {
case NETHERRACK:
case OBSIDIAN:
case SANDSTONE:
Misc.dropItem(location, item);
break;
for (ItemStack drop : blockState.getBlock().getDrops()) {
Misc.dropItem(location, drop);
}
return;
case GLOWING_REDSTONE_ORE:
case REDSTONE_ORE:
if (Config.getInstance().getDoubleDropsEnabled(SkillType.MINING, Material.REDSTONE_ORE)) {
for (ItemStack drop : blockState.getBlock().getDrops()) {
Misc.dropItem(location, drop);
}
}
return;
default:
if (ModChecks.isCustomMiningBlock(block)) {
CustomBlock customBlock = ModChecks.getCustomBlock(block);
if (ModChecks.isCustomMiningBlock(blockState)) {
CustomBlock customBlock = ModChecks.getCustomBlock(blockState);
int minimumDropAmount = customBlock.getMinimumDropAmount();
int maximumDropAmount = customBlock.getMaximumDropAmount();
item = ModChecks.getCustomBlock(block).getItemDrop();
dropItem = customBlock.getItemDrop();
if (minimumDropAmount != maximumDropAmount) {
Misc.dropItems(location, item, minimumDropAmount);
Misc.randomDropItems(location, item, maximumDropAmount - minimumDropAmount);
Misc.dropItems(location, dropItem, minimumDropAmount);
Misc.randomDropItems(location, dropItem, maximumDropAmount - minimumDropAmount);
}
else {
Misc.dropItems(location, item, minimumDropAmount);
Misc.dropItems(location, dropItem, minimumDropAmount);
}
}
break;
return;
}
}
}

View File

@ -1,45 +0,0 @@
package com.gmail.nossr50.skills.mining;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.enchantments.Enchantment;
import com.gmail.nossr50.skills.utilities.SkillTools;
public class MiningBlockEventHandler {
private MiningManager manager;
private Block block;
private Location blockLocation;
private Material blockType;
protected int skillModifier;
protected MiningBlockEventHandler(MiningManager manager, Block block) {
this.manager = manager;
this.block = block;
this.blockLocation = block.getLocation();
this.blockType = block.getType();
calculateSkillModifier();
}
private void calculateSkillModifier() {
this.skillModifier = SkillTools.skillCheck(manager.getSkillLevel(), Mining.doubleDropsMaxLevel);
}
/**
* Process Mining block drops.
*/
protected void processDrops() {
if (manager.getMcMMOPlayer().getPlayer().getItemInHand().containsEnchantment(Enchantment.SILK_TOUCH)) {
Mining.silkTouchDrops(block, blockLocation, blockType);
}
else {
Mining.miningDrops(block, blockLocation, blockType);
}
}
protected void processXPGain() {
Mining.miningXP(manager.getMcMMOPlayer(), block, blockType);
}
}

View File

@ -1,7 +1,6 @@
package com.gmail.nossr50.skills.mining;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.ExplosionPrimeEvent;
@ -12,7 +11,6 @@ import com.gmail.nossr50.skills.SkillManager;
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;
public class MiningManager extends SkillManager{
public MiningManager (McMMOPlayer mcMMOPlayer) {
@ -102,24 +100,4 @@ public class MiningManager extends SkillManager{
eventHandler.calculateRadiusIncrease();
eventHandler.modifyBlastRadius();
}
/**
* Process Mining block drops.
*
* @param block The block being broken
*/
public void miningBlockCheck(Block block) {
MiningBlockEventHandler eventHandler = new MiningBlockEventHandler(this, block);
eventHandler.processXPGain();
if (!Permissions.doubleDrops(mcMMOPlayer.getPlayer(), skill)) {
return;
}
float chance = ((float) Mining.doubleDropsMaxChance / Mining.doubleDropsMaxLevel) * eventHandler.skillModifier;
if (chance > Misc.getRandom().nextInt(activationChance)) {
eventHandler.processDrops();
}
}
}