1
0
Fork 0

completed playerinteractlistener, began work on block listener, made toolutil methods static

This commit is contained in:
lightling 2019-09-13 14:08:37 -04:00
parent fa63606d90
commit 9ef940503a
5 changed files with 126 additions and 6 deletions

View file

@ -3,10 +3,12 @@ package lightling.gibsoniacraft;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import lightling.gibsoniacraft.crafting.Excavator; import lightling.gibsoniacraft.crafting.Excavator;
import lightling.gibsoniacraft.util.PlayerInteractListener;
public final class GibsoniaCraft extends JavaPlugin { public final class GibsoniaCraft extends JavaPlugin {
private Excavator excavatorClass; private Excavator excavatorClass;
private PlayerInteractListener pListener;
@Override @Override
public void onEnable() { public void onEnable() {
@ -18,4 +20,9 @@ public final class GibsoniaCraft extends JavaPlugin {
} }
public PlayerInteractListener GetPlayerInteractListener()
{
return pListener;
}
} }

View file

@ -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);
}
}

View file

@ -4,6 +4,10 @@ import java.util.ArrayList;
import org.bukkit.Material; import org.bukkit.Material;
/**
* Contains constant blocks and items of a certain type
* @author Lightling
*/
public class BlockRef public class BlockRef
{ {
/** /**

View file

@ -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<String, BlockFace> faces;
public PlayerInteractListener(JavaPlugin plugin)
{
faces = new HashMap<String, BlockFace>();
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);
}
}

View file

@ -8,6 +8,10 @@ import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.World; import org.bukkit.World;
/**
* Manages calculations regarding added tools
* @author Lightling
*/
public class ToolUtil public class ToolUtil
{ {
/** /**
@ -15,7 +19,7 @@ public class ToolUtil
* @param mat The material being checked * @param mat The material being checked
* @return Whether the block is mineable * @return Whether the block is mineable
*/ */
public boolean IsMineable(Material mat) public static boolean IsMineable(Material mat)
{ {
return BlockRef.ValidHammerBlocks.contains(mat); return BlockRef.ValidHammerBlocks.contains(mat);
} }
@ -25,7 +29,7 @@ public class ToolUtil
* @param mat The material being checked * @param mat The material being checked
* @return Whether the block is diggable * @return Whether the block is diggable
*/ */
public boolean IsDiggable(Material mat) public static boolean IsDiggable(Material mat)
{ {
return BlockRef.ValidExcavatorBlocks.contains(mat); return BlockRef.ValidExcavatorBlocks.contains(mat);
} }
@ -35,7 +39,7 @@ public class ToolUtil
* @param mat The material being checked * @param mat The material being checked
* @return Whether the item is a valid hammer * @return Whether the item is a valid hammer
*/ */
public boolean IsHammer(Material mat) public static boolean IsHammer(Material mat)
{ {
return BlockRef.ValidHammers.contains(mat); return BlockRef.ValidHammers.contains(mat);
} }
@ -45,7 +49,7 @@ public class ToolUtil
* @param mat The material being checked * @param mat The material being checked
* @return Whether the item is a valid excavator * @return Whether the item is a valid excavator
*/ */
public boolean IsExcavator(Material mat) public static boolean IsExcavator(Material mat)
{ {
return BlockRef.ValidExcavators.contains(mat); return BlockRef.ValidExcavators.contains(mat);
} }
@ -56,7 +60,7 @@ public class ToolUtil
* @param block The block that should be mineable * @param block The block that should be mineable
* @return Valid or invalid action * @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); return IsMineable(block) && IsHammer(ham);
} }
@ -67,7 +71,7 @@ public class ToolUtil
* @param block The block that should be diggable * @param block The block that should be diggable
* @return Valid or invalid action * @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); return IsDiggable(block) && IsExcavator(exc);
} }
@ -138,4 +142,8 @@ public class ToolUtil
return blocks; return blocks;
} }
// TO-DO: Implement enchantments
// TO-DO: Implement scythes (3x3 harvest) and tillers (3x3 hoe)
} }