Checking if the same block is being hit

Discussion in 'Plugin Development' started by CRAZYxMUNK3Y, Dec 25, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    CRAZYxMUNK3Y

    After a bit of research i can't find out weather it's possible to do this or not.

    What i am trying to do is, when the player hits/mines a block it starts a scheduler for x amount of seconds. If the player is still hitting/mining the same block when the scheduler finishes then the block will change.

    The code i have tried so far.
    Code:java
    1.  
    2. @EventHandler
    3. public void playerInteract(final BlockDamageEvent e) {
    4. final Player player = e.getPlayer();
    5. final Block block = e.getBlock();
    6. Material iih = player.getInventory().getItemInHand().getType();
    7. if (block.getType() == Material.BEDROCK) {
    8. if (block.getLocation().getY() > 0) {
    9. if (player.hasPermission("bedrockbreak.use")) {
    10. if (iih == Material.DIAMOND_PICKAXE) {
    11. getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    12. @Override
    13. public void run() {
    14. if ((player.getLineOfSight(null, 100)).equals(Material.BEDROCK)
    15. && player.getLastTwoTargetBlocks(null, 100).contains(Material.BEDROCK)) {
    16. block.setType(Material.AIR);
    17. } else {
    18. block.setType(Material.BEDROCK);
    19. }
    20. }
    21. }, 20L * getConfig().getInt("breakTime")); // config.getInt("breakTime") = seconds, 20 ticks to a second;
    22. } else {
    23. return;
    24. }
    25. } else {
    26. return;
    27. }
    28. }
    29. }
    30. }
    31.  


    Thanks in advance.
     
  2. Offline

    caseif

    Well, I was going to post a solution involving a global block array, but then I realized it wouldn't work at all. But, maybe you could still use the idea?
     
  3. Offline

    CRAZYxMUNK3Y

    I've never really tried anything like that and I'm not sure on the best way to do it. Mind giving me a small example/way to do it?

    Thanks
     
  4. Offline

    RealDope

    Maybe I don't understand what you're trying to do, but couldn't you just store a hashmap with the player's name and block they are hitting, then get that name after the scheduler is up and see if the block matches the block they are hitting?
     
  5. Offline

    CRAZYxMUNK3Y

    RealDope
    What i am attempting to do is check to see if a player is hitting the same block for x amount of seconds, if they did, set it to air, and if they didn't/stopped midway, leave the block as normal.

    Code so far (open)

    Code:java
    1.  
    2. public void playerInteract(final BlockDamageEvent e) {
    3. final ArrayList<Location> b = new ArrayList<Location>();
    4. final Player player = e.getPlayer();
    5. final String pName = player.getDisplayName();
    6. final Block block = e.getBlock();
    7. final Location loc = block.getLocation();
    8. Material iih = player.getInventory().getItemInHand().getType();
    9. if (block.getType() == Material.BEDROCK) {
    10. if (block.getLocation().getY() > 0) {
    11. if (player.hasPermission("bedrockbreak.use")) {
    12. if (iih == Material.DIAMOND_PICKAXE) {
    13. b.add(loc);
    14. getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    15. @Override
    16. public void run() {
    17. if (b.contains(loc)) {
    18. block.setType(Material.AIR);
    19. b.remove(pName);
    20. } else {
    21. player.sendMessage("FAIL");
    22. }
    23. }
    24. }, 20L * getConfig().getInt("breakTime"));
    25.  


    The block delays like it should, but you only have to hit it once for it to disappear.
    I have tried;
    ArrayList<Location>, HashMap<String(Player Name), Location> and HashMap<Player, location>.
     
  6. Offline

    tommycake50

    ok lol wow you are seeing if he hits it and if he does creating a delayed event to remove the block w/o even checking if its still being hit.
     
  7. Offline

    CRAZYxMUNK3Y

    tommycake50
    Correct, but I'm wondering if there is a way to check if they are still hitting it after x amount of seconds.
     
  8. Offline

    tommycake50

    idk probs but idk.
     
  9. Offline

    RealDope

    Why not schedule something to run every tick for x seconds and keep a boolean isHitting. If on any of the ticks they aren't hitting it, set the boolean to false and .cancel() the scheduler.
    CRAZYxMUNK3Y
     
  10. Offline

    caseif

    But what if more than one player is hitting a block at the same time?

    You could probably use this idea. Create a global HashMap, and if a player is hitting a block, map the player's username (not the player's instance!) to the block and run the scheduler, then when the time elapses, check if the player is still breaking the block they're mapped to, and if they are, set it to air, and then remove the player from the HashMap. I know this is essentially the same thing RealDope said, but I'm elaborating just to clarify.
     
  11. Offline

    RealDope

    AngryNerd yeah I kinda meant a combination of my two ideas, the "boolean" that I refer to in my second post meant the boolean associated with that player's name in the hashmap
     
  12. Offline

    caseif

    Ah. okay. I somehow didn't see that both posts were by you.
     
Thread Status:
Not open for further replies.

Share This Page