From 9ef940503aa86f12ea367fe4cd956b2990f411d3 Mon Sep 17 00:00:00 2001 From: Lightling Date: Fri, 13 Sep 2019 14:08:37 -0400 Subject: [PATCH] completed playerinteractlistener, began work on block listener, made toolutil methods static --- .../gibsoniacraft/GibsoniaCraft.java | 7 +++ .../gibsoniacraft/util/BlockListener.java | 50 ++++++++++++++++++ .../gibsoniacraft/util/BlockRef.java | 4 ++ .../util/PlayerInteractListener.java | 51 +++++++++++++++++++ .../gibsoniacraft/util/ToolUtil.java | 20 +++++--- 5 files changed, 126 insertions(+), 6 deletions(-) create mode 100644 plugin/src/lightling/gibsoniacraft/util/BlockListener.java create mode 100644 plugin/src/lightling/gibsoniacraft/util/PlayerInteractListener.java diff --git a/plugin/src/lightling/gibsoniacraft/GibsoniaCraft.java b/plugin/src/lightling/gibsoniacraft/GibsoniaCraft.java index 16e301a..9cded59 100644 --- a/plugin/src/lightling/gibsoniacraft/GibsoniaCraft.java +++ b/plugin/src/lightling/gibsoniacraft/GibsoniaCraft.java @@ -3,10 +3,12 @@ package lightling.gibsoniacraft; import org.bukkit.plugin.java.JavaPlugin; import lightling.gibsoniacraft.crafting.Excavator; +import lightling.gibsoniacraft.util.PlayerInteractListener; public final class GibsoniaCraft extends JavaPlugin { private Excavator excavatorClass; + private PlayerInteractListener pListener; @Override public void onEnable() { @@ -18,4 +20,9 @@ public final class GibsoniaCraft extends JavaPlugin { } + public PlayerInteractListener GetPlayerInteractListener() + { + return pListener; + } + } diff --git a/plugin/src/lightling/gibsoniacraft/util/BlockListener.java b/plugin/src/lightling/gibsoniacraft/util/BlockListener.java new file mode 100644 index 0000000..7883a30 --- /dev/null +++ b/plugin/src/lightling/gibsoniacraft/util/BlockListener.java @@ -0,0 +1,50 @@ +package lightling.gibsoniacraft.util; + +// Needed for block information +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.entity.Player; + +// Needed for handling events +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockBreakEvent; +import lightling.gibsoniacraft.GibsoniaCraft; + +// Needed to determine if using a GibsoniaCraft tool or not +import org.bukkit.inventory.ItemStack; + +/** + * A listener for block-based events, used primarily for blocks being broken by GibsoniaCraft tools + * @author Lightling + */ +public class BlockListener implements Listener +{ + public BlockListener(GibsoniaCraft plugin) + { + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } + + @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) + public void Break(GibsoniaCraft plugin, BlockBreakEvent bbEvent) + { + Player player = bbEvent.getPlayer(); + ItemStack stack = player.getInventory().getItemInMainHand(); + Material itemType = stack.getType(); + + if (player != null && (player instanceof Player)) + { + if (player.isSneaking()) + return; + if (!ToolUtil.IsExcavator(itemType) || !ToolUtil.IsHammer(itemType)) + return; + } + + Block block= bbEvent.getBlock(); + final String pName = player.getName(); + final PlayerInteractListener pListener = plugin.GetPlayerInteractListener(); + final BlockFace blockFace = pListener.GetFaceByName(pName); + } +} diff --git a/plugin/src/lightling/gibsoniacraft/util/BlockRef.java b/plugin/src/lightling/gibsoniacraft/util/BlockRef.java index aa7bcac..36f3a12 100644 --- a/plugin/src/lightling/gibsoniacraft/util/BlockRef.java +++ b/plugin/src/lightling/gibsoniacraft/util/BlockRef.java @@ -4,6 +4,10 @@ import java.util.ArrayList; import org.bukkit.Material; +/** + * Contains constant blocks and items of a certain type + * @author Lightling + */ public class BlockRef { /** diff --git a/plugin/src/lightling/gibsoniacraft/util/PlayerInteractListener.java b/plugin/src/lightling/gibsoniacraft/util/PlayerInteractListener.java new file mode 100644 index 0000000..98cb54a --- /dev/null +++ b/plugin/src/lightling/gibsoniacraft/util/PlayerInteractListener.java @@ -0,0 +1,51 @@ +package lightling.gibsoniacraft.util; + +import java.util.HashMap; // Needed to store block-face information + +import org.bukkit.block.BlockFace; // Needed by BlockBreakListener which calls upon this class +import org.bukkit.entity.Player; // The main focus of this class +import org.bukkit.plugin.java.JavaPlugin; // Needed to register events on the server + +// Needed for events +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerInteractEvent; + +/** + * A listener for player interactions, primarily saves block-face information + * @author Lightling + */ +public class PlayerInteractListener implements Listener +{ + private HashMap faces; + + public PlayerInteractListener(JavaPlugin plugin) + { + faces = new HashMap(); + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } + + @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) + public void SaveBlockFace(PlayerInteractEvent event) + { + Player player = event.getPlayer(); + BlockFace bFace = event.getBlockFace(); + + if (player != null && bFace != null) + { + String pName = player.getName(); + faces.put(pName, bFace); + } + } + + /** + * Gets a block-face with a player's name + * @param name The name being searched + * @return The matching block face + */ + public BlockFace GetFaceByName(String name) + { + return faces.get(name); + } +} diff --git a/plugin/src/lightling/gibsoniacraft/util/ToolUtil.java b/plugin/src/lightling/gibsoniacraft/util/ToolUtil.java index f57e591..d8fe4be 100644 --- a/plugin/src/lightling/gibsoniacraft/util/ToolUtil.java +++ b/plugin/src/lightling/gibsoniacraft/util/ToolUtil.java @@ -8,6 +8,10 @@ import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.World; +/** + * Manages calculations regarding added tools + * @author Lightling + */ public class ToolUtil { /** @@ -15,7 +19,7 @@ public class ToolUtil * @param mat The material being checked * @return Whether the block is mineable */ - public boolean IsMineable(Material mat) + public static boolean IsMineable(Material mat) { return BlockRef.ValidHammerBlocks.contains(mat); } @@ -25,7 +29,7 @@ public class ToolUtil * @param mat The material being checked * @return Whether the block is diggable */ - public boolean IsDiggable(Material mat) + public static boolean IsDiggable(Material mat) { return BlockRef.ValidExcavatorBlocks.contains(mat); } @@ -35,7 +39,7 @@ public class ToolUtil * @param mat The material being checked * @return Whether the item is a valid hammer */ - public boolean IsHammer(Material mat) + public static boolean IsHammer(Material mat) { return BlockRef.ValidHammers.contains(mat); } @@ -45,7 +49,7 @@ public class ToolUtil * @param mat The material being checked * @return Whether the item is a valid excavator */ - public boolean IsExcavator(Material mat) + public static boolean IsExcavator(Material mat) { return BlockRef.ValidExcavators.contains(mat); } @@ -56,7 +60,7 @@ public class ToolUtil * @param block The block that should be mineable * @return Valid or invalid action */ - public boolean IsHammerable(Material ham, Material block) + public static boolean IsHammerable(Material ham, Material block) { return IsMineable(block) && IsHammer(ham); } @@ -67,7 +71,7 @@ public class ToolUtil * @param block The block that should be diggable * @return Valid or invalid action */ - public boolean IsExcavatable(Material exc, Material block) + public static boolean IsExcavatable(Material exc, Material block) { return IsDiggable(block) && IsExcavator(exc); } @@ -138,4 +142,8 @@ public class ToolUtil return blocks; } + + // TO-DO: Implement enchantments + + // TO-DO: Implement scythes (3x3 harvest) and tillers (3x3 hoe) }