From c3b90f4df1f66ff0005661994317edbf93b61d4f Mon Sep 17 00:00:00 2001 From: Lightling Date: Sat, 14 Sep 2019 13:59:23 -0400 Subject: [PATCH] continued work on lumber-axe --- .../gibsoniacraft/crafting/LumberAxe.java | 2 +- .../gibsoniacraft/util/BlockListener.java | 74 ++++++++++++- .../gibsoniacraft/util/BlockRef.java | 32 ++++++ .../gibsoniacraft/util/ToolUtil.java | 101 +++++++++++++++++- 4 files changed, 204 insertions(+), 5 deletions(-) diff --git a/plugin/src/lightling/gibsoniacraft/crafting/LumberAxe.java b/plugin/src/lightling/gibsoniacraft/crafting/LumberAxe.java index 557e7d5..70d10f5 100644 --- a/plugin/src/lightling/gibsoniacraft/crafting/LumberAxe.java +++ b/plugin/src/lightling/gibsoniacraft/crafting/LumberAxe.java @@ -90,7 +90,7 @@ public class LumberAxe ItemMeta diamondLumAxeMeta = diamondLumberAxe.getItemMeta(); // Define lore - String loreString = "Based off of item from PowerMining/Tinkers' Construct"; + String loreString = "Based off of item from Tinkers' Construct"; List lore = new ArrayList() {{ add(loreString); }}; woodLumAxeMeta.setLore(lore); stoneLumAxeMeta.setLore(lore); diff --git a/plugin/src/lightling/gibsoniacraft/util/BlockListener.java b/plugin/src/lightling/gibsoniacraft/util/BlockListener.java index 15ce2c3..6a5414b 100644 --- a/plugin/src/lightling/gibsoniacraft/util/BlockListener.java +++ b/plugin/src/lightling/gibsoniacraft/util/BlockListener.java @@ -33,6 +33,10 @@ public class BlockListener implements Listener gcPlugin.getServer().getPluginManager().registerEvents(this, gcPlugin); } + /** + * Handle the breaking of blocks via the hammer/excavator + * @param bbEvent The event that triggered this method + */ @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) public void Break(BlockBreakEvent bbEvent) { @@ -60,13 +64,13 @@ public class BlockListener implements Listener PlayerInteractListener pListener = gcPlugin.GetPlayerInteractListener(); BlockFace blockFace = pListener.GetFaceByName(pName); - // getDurability deprecated, must now go through meta information + // Grab durability 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 + // Used in determining if an extra block was broken (for durability) boolean success = false; // Iterates through to break surrounding blocks @@ -108,4 +112,70 @@ public class BlockListener implements Listener item.setItemMeta(meta); } } + + /** + * Handle the breaking of blocks via the lumber-axe + * @param bbEvent The event that triggered this method + */ + @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) + public void Chop(BlockBreakEvent bbEvent) + { + // Grab current tool information + Player player = bbEvent.getPlayer(); + ItemStack item = player.getInventory().getItemInMainHand(); + 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.isSneaking()) + { + return; + } + if (!ToolUtil.IsLumberAxe(item)) + { + return; + } + } + + // Get block information via the player listener + Block block = bbEvent.getBlock(); + + // Grab durability 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 (for durability) + boolean success = false; + + for (Block b : ToolUtil.GetUpwardLogs(block)) + { + // Determine the block type and position + Material blockMat = b.getType(); + Location blockLoc = b.getLocation(); + + // Determine whether an appropriate tool is being used + boolean isAxe = ToolUtil.IsLumberAxeable(item, blockMat); + + if (isAxe) + { + 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); + } + } } diff --git a/plugin/src/lightling/gibsoniacraft/util/BlockRef.java b/plugin/src/lightling/gibsoniacraft/util/BlockRef.java index 36f3a12..7aa19e4 100644 --- a/plugin/src/lightling/gibsoniacraft/util/BlockRef.java +++ b/plugin/src/lightling/gibsoniacraft/util/BlockRef.java @@ -70,6 +70,26 @@ public class BlockRef add(Material.SOUL_SAND); }}; + /** + * A list of all valid lumber-axe blocks (must be a log that stacks upward) + */ + public static ArrayList ValidLumberAxeBlocks = new ArrayList() + {{ + add(Material.ACACIA_LOG); + add(Material.BIRCH_LOG); + add(Material.DARK_OAK_LOG); + add(Material.JUNGLE_LOG); + add(Material.OAK_LOG); + add(Material.SPRUCE_LOG); + + add(Material.STRIPPED_ACACIA_LOG); + add(Material.STRIPPED_BIRCH_LOG); + add(Material.STRIPPED_DARK_OAK_LOG); + add(Material.STRIPPED_JUNGLE_LOG); + add(Material.STRIPPED_OAK_LOG); + add(Material.STRIPPED_SPRUCE_LOG); + }}; + /** * A list of all valid hammers (pickaxes) */ @@ -93,4 +113,16 @@ public class BlockRef add(Material.GOLDEN_SHOVEL); add(Material.DIAMOND_SHOVEL); }}; + + /** + * A list of all valid lumber-axes (axes) + */ + public static ArrayList ValidLumberAxes = new ArrayList() + {{ + add(Material.WOODEN_AXE); + add(Material.STONE_AXE); + add(Material.IRON_AXE); + add(Material.GOLDEN_AXE); + add(Material.DIAMOND_AXE); + }}; } diff --git a/plugin/src/lightling/gibsoniacraft/util/ToolUtil.java b/plugin/src/lightling/gibsoniacraft/util/ToolUtil.java index 332e9a2..6bbe600 100644 --- a/plugin/src/lightling/gibsoniacraft/util/ToolUtil.java +++ b/plugin/src/lightling/gibsoniacraft/util/ToolUtil.java @@ -3,6 +3,7 @@ package lightling.gibsoniacraft.util; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Stack; import org.bukkit.Material; import org.bukkit.block.Block; @@ -37,8 +38,18 @@ public class ToolUtil } /** - * Determines whether an item is a valid hammer + * Determines whether a block is axe-able * @param mat The material being checked + * @return Whether the block is axe-able + */ + public static boolean IsAxeable(Material mat) + { + return BlockRef.ValidLumberAxeBlocks.contains(mat); + } + + /** + * Determines whether an item is a valid hammer + * @param item The item being checked * @return Whether the item is a valid hammer */ public static boolean IsHammer(ItemStack item) @@ -53,7 +64,7 @@ public class ToolUtil /** * Determines whether an item is a valid excavator - * @param mat The material being checked + * @param item The item being checked * @return Whether the item is a valid excavator */ public static boolean IsExcavator(ItemStack item) @@ -66,6 +77,21 @@ public class ToolUtil return false; } + /** + * Determines whether an item is a valid lumber-axe + * @param item The item being checked + * @return Whether the item is a valid lumber-axe + */ + public static boolean IsLumberAxe(ItemStack item) + { + List lore = item.getLore(); + Material mat = item.getType(); + if (lore != null && mat != null) + return BlockRef.ValidLumberAxes.contains(mat) && lore.contains("Based off of item from Tinkers' Construct"); + else + return false; + } + /** * Determines whether a valid hammer can take place * @param ham The item that should be a hammer @@ -88,6 +114,17 @@ public class ToolUtil return IsDiggable(block) && IsExcavator(exc); } + /** + * Determines whether a valid lumber-axe execution can take place + * @param axe The item that should be a lumber-axe + * @param block The block that should be axe-able + * @return Valid or invalid action + */ + public static boolean IsLumberAxeable(ItemStack axe, Material block) + { + return IsAxeable(block) && IsLumberAxe(axe); + } + /** * Grabs the surrounding blocks in the world from the one that was mined/dug, and adds them to a list * @param blockFace The face that was targeted (changes the direction of the 3x3 grid) @@ -155,6 +192,66 @@ public class ToolUtil return blocks; } + public static ArrayList GetUpwardLogs(Block target) + { + // Create the list to work with + ArrayList blocks = new ArrayList(); + + // Reference the world-space + World world = target.getWorld(); + + // Determine the anchor XYZ coordinates + int x = target.getX(); + int y = target.getY(); + int z = target.getZ(); + + // Create control for loop + boolean moreBlocks = true; + + // Grab all logs in a tree + do + { + // Variable that ends loop if remains 0 + int blocksAbove = 0; + + // Grab the surrounding blocks that may be wood + for (int i = -2; i <= 2; i++) + { + for (int j = -2; j <= 2; j++) + { + // Add all the surrounding logs to the list + Block temp = world.getBlockAt(x + i, y, z + j); + if (BlockRef.ValidLumberAxeBlocks.contains(temp.getType())) + { + blocks.add(temp); + } + + // Determine if there are still blocks left + Block tempAbove = world.getBlockAt(x + i, y + 1, z + j); + if (BlockRef.ValidLumberAxeBlocks.contains(temp.getType())) + { + blocksAbove++; + } + } + } + + // If there are still blocks left, go up a level for the for-loop + if (blocksAbove != 0) + { + y++; + } + + // Otherwise, end the loop + else + { + moreBlocks = false; + } + } + while (moreBlocks); + + return blocks; + } + // TO-DO: Implement enchantments // TO-DO: Implement scythes (3x3 harvest) and tillers (3x3 hoe)