Update BlockListener.java
This commit is contained in:
parent
9ef940503a
commit
69e3da421c
1 changed files with 62 additions and 7 deletions
|
@ -5,6 +5,7 @@ import org.bukkit.Material;
|
||||||
import org.bukkit.block.Block;
|
import org.bukkit.block.Block;
|
||||||
import org.bukkit.block.BlockFace;
|
import org.bukkit.block.BlockFace;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.Location;
|
||||||
|
|
||||||
// Needed for handling events
|
// Needed for handling events
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
|
@ -13,8 +14,11 @@ import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.block.BlockBreakEvent;
|
import org.bukkit.event.block.BlockBreakEvent;
|
||||||
import lightling.gibsoniacraft.GibsoniaCraft;
|
import lightling.gibsoniacraft.GibsoniaCraft;
|
||||||
|
|
||||||
// Needed to determine if using a GibsoniaCraft tool or not
|
// Needed for GibsoniaCraft tools
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.inventory.meta.Damageable;
|
||||||
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
|
import org.bukkit.enchantments.Enchantment;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A listener for block-based events, used primarily for blocks being broken by GibsoniaCraft tools
|
* A listener for block-based events, used primarily for blocks being broken by GibsoniaCraft tools
|
||||||
|
@ -30,10 +34,12 @@ public class BlockListener implements Listener
|
||||||
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
||||||
public void Break(GibsoniaCraft plugin, BlockBreakEvent bbEvent)
|
public void Break(GibsoniaCraft plugin, BlockBreakEvent bbEvent)
|
||||||
{
|
{
|
||||||
|
// Grab current tool information
|
||||||
Player player = bbEvent.getPlayer();
|
Player player = bbEvent.getPlayer();
|
||||||
ItemStack stack = player.getInventory().getItemInMainHand();
|
ItemStack item = player.getInventory().getItemInMainHand();
|
||||||
Material itemType = stack.getType();
|
Material itemType = item.getType();
|
||||||
|
|
||||||
|
// Only do extra block-breaking if the player is not sneaking (method of disabling) or if the player doesn't have the appropriate tools
|
||||||
if (player != null && (player instanceof Player))
|
if (player != null && (player instanceof Player))
|
||||||
{
|
{
|
||||||
if (player.isSneaking())
|
if (player.isSneaking())
|
||||||
|
@ -42,9 +48,58 @@ public class BlockListener implements Listener
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get blockface information via the player listener
|
||||||
Block block = bbEvent.getBlock();
|
Block block = bbEvent.getBlock();
|
||||||
final String pName = player.getName();
|
String pName = player.getName();
|
||||||
final PlayerInteractListener pListener = plugin.GetPlayerInteractListener();
|
PlayerInteractListener pListener = plugin.GetPlayerInteractListener();
|
||||||
final BlockFace blockFace = pListener.GetFaceByName(pName);
|
BlockFace blockFace = pListener.GetFaceByName(pName);
|
||||||
|
|
||||||
|
// getDurability deprecated, must now go through meta information
|
||||||
|
ItemMeta meta = item.getItemMeta();
|
||||||
|
Damageable dMeta = (Damageable)meta;
|
||||||
|
int currDur = dMeta.getDamage();
|
||||||
|
int maxDur = item.getType().getMaxDurability();
|
||||||
|
|
||||||
|
// Used in determining if an extra block was broken
|
||||||
|
boolean success = false;
|
||||||
|
|
||||||
|
// Iterates through to break surrounding blocks
|
||||||
|
for (Block b : ToolUtil.GetSurroundingBlocks(blockFace, block))
|
||||||
|
{
|
||||||
|
// Determine the block type and position
|
||||||
|
Material blockMat = b.getType();
|
||||||
|
Location blockLoc = b.getLocation();
|
||||||
|
|
||||||
|
// Determine whether an appropriate tool is being used
|
||||||
|
boolean isExc = ToolUtil.IsExcavatable(itemType, blockMat);
|
||||||
|
boolean isHam = ToolUtil.IsHammerable(itemType, blockMat);
|
||||||
|
|
||||||
|
// If using the correct tools, break surrounding blocks
|
||||||
|
if (isExc || isHam)
|
||||||
|
{
|
||||||
|
// Handle snow
|
||||||
|
if (blockMat == Material.SNOW && isExc)
|
||||||
|
{
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
|
final ItemStack snow = new ItemStack(Material.SNOWBALL, 1 + b.getData());
|
||||||
|
b.getWorld().dropItemNaturally(blockLoc, snow);
|
||||||
|
}
|
||||||
|
success = true;
|
||||||
|
|
||||||
|
b.breakNaturally();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Do extra damage to durability if extra blocks were broken
|
||||||
|
if (success && !item.getEnchantments().containsKey(Enchantment.DURABILITY))
|
||||||
|
{
|
||||||
|
int addToDamage = 2;
|
||||||
|
if (itemType == Material.DIAMOND_PICKAXE && itemType == Material.DIAMOND_SHOVEL)
|
||||||
|
{
|
||||||
|
addToDamage = 1;
|
||||||
|
}
|
||||||
|
dMeta.setDamage(currDur + addToDamage);
|
||||||
|
item.setItemMeta(meta);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue