Jackhammer kit, crashing server (time out)

Discussion in 'Plugin Development' started by CMG, Jun 1, 2013.

  1. I have made many kits replicating the mcpvp's ones and my own for a Hunger Games plugin. There has only been maybe 1 or 2 ones that i have had trouble with and this is one of them. Basically when a player breaks a block with a stone axe and has the jackhammer kit ability it should break all blocks above it like mcpvp's jackhammer. But instead it crashes my server anyway to fix this or can you give me some better code?:
    Code (Text):
    1. package me.CMG.HungerGames.Listeners;
    2.  
    3. import me.CMG.HungerGames.Main.HungerGames;
    4. import me.CMG.HungerGames.Main.HungerGames.Kit;
    5. import me.CMG.HungerGames.Main.Methods;
    6.  
    7. import org.bukkit.Material;
    8. import org.bukkit.block.Block;
    9. import org.bukkit.block.BlockFace;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.block.BlockBreakEvent;
    14.  
    15. public class JackhammerAbility implements Listener
    16. {
    17.     HungerGames plugin;
    18.  
    19.     public final Methods m = new Methods();
    20.  
    21.     public JackhammerAbility(HungerGames plugin) {
    22.         this.plugin = plugin;
    23.     }
    24.  
    25.     int specType = Material.STONE_AXE.getId();
    26.  
    27.     @EventHandler
    28.     public void onBreakBlock(BlockBreakEvent e)
    29.     {
    30.         Player p = (Player) e.getPlayer();
    31.         Kit k = HungerGames.players.get(p.getName());
    32.         int type = p.getItemInHand().getTypeId();
    33.  
    34.         if(type == specType)
    35.         {
    36.             if(k == Kit.Jackhammer)
    37.             {
    38.                 if(e.getBlock().getType() != null)
    39.                 {
    40.                     Block b = e.getBlock().getRelative(BlockFace.UP);
    41.  
    42.                     while(b.getType() != null)
    43.                     {
    44.                         b.setType(Material.AIR);
    45.                         b = b.getRelative(BlockFace.UP);
    46.                     }
    47.                 }
    48.                 else
    49.                 {
    50.  
    51.                 }
    52.             }
    53.             else
    54.             {
    55.  
    56.             }
    57.         }
    58.         else
    59.         {
    60.  
    61.         }
    62.     }
    63. }
     
  2. Offline

    GodzOfMadness

    CMG instead of b.getType() != null do while(!b.getType().equals(Material.AIR))
     
  3. Thanks :D, that works but how would i make it so it does each block one by one like an animation? so a little pause inbetween? EDIT what about gaps though? so if it goes Block Air Block?
     
  4. Anyone?
     
  5. Offline

    GodzOfMadness

    CMG Just make something like create a custom event and then loop through all the blocks. When it reaches a certain amount then just stop, store the rest and then schedule a delayed task for how ever long and then call the event again and again until it finishes.
     
  6. Offline

    GodzOfMadness

    CMG Don't bump after 5 minutes of no one answering you..
     
  7. Yeah sorry.
     
  8. Ok, but how would i loop through all the blocks that's what i don't know how to do.
     
  9. Offline

    GodzOfMadness

    CMG Create an arraylist that stores their locations like ArrayList<Location> list = new ArrayList<Location>()
    then add the location of each block it finds. Then you could make another list and then go through each one of them and when the size of that list is large enough stop the loop and then build the blocks and start the delayed task to start again with the rest of the blocks.
     
  10. Ill try that
     
  11. Nope had a go but still didn't understand.
     
  12. Offline

    GodzOfMadness

    CMG create a list that holds all the blocks you find. Create another list for the certain amount of blocks you want regened. Then loop through the list that holds all the blocks. make a while loop like
    while(tempList.size() < amount){
    tempList.add(all.get(0));
    all.remove(0);
    }
    then keep going until the amount is done
    then go and regen those blocks in that list like
    for(Location loc : tempList){
    loc.getBlock().setType(Material.AIR);
    }
    then schedule the delayed task
    when the delay is over call the custom event again with the list that holds all the blocks and it will repeat until it is over
     
  13. Something like this:
    Code (Text):
    1. package me.CMG.HungerGames.Listeners;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import me.CMG.HungerGames.Main.HungerGames;
    7. import me.CMG.HungerGames.Main.HungerGames.Kit;
    8. import me.CMG.HungerGames.Main.Methods;
    9.  
    10. import org.bukkit.Bukkit;
    11. import org.bukkit.Location;
    12. import org.bukkit.Material;
    13. import org.bukkit.block.Block;
    14. import org.bukkit.block.BlockFace;
    15. import org.bukkit.entity.Player;
    16. import org.bukkit.event.EventHandler;
    17. import org.bukkit.event.Listener;
    18. import org.bukkit.event.block.BlockBreakEvent;
    19.  
    20. public class JackhammerAbility implements Listener
    21. {
    22.     HungerGames plugin;
    23.  
    24.     public final Methods m = new Methods();
    25.  
    26.     public JackhammerAbility(HungerGames plugin) {
    27.         this.plugin = plugin;
    28.     }
    29.  
    30.     int specType = Material.STONE_AXE.getId();
    31.  
    32.     int maxRemoveHeight = 128;
    33.  
    34.     List<Location> tempList = new ArrayList<Location>();
    35.  
    36.     @EventHandler
    37.     public void onBreakBlock(BlockBreakEvent e)
    38.     {
    39.         Player p = (Player) e.getPlayer();
    40.         Kit k = HungerGames.players.get(p.getName());
    41.         int type = p.getItemInHand().getTypeId();
    42.  
    43.         if(type == specType)
    44.         {
    45.             if(k == Kit.Jackhammer)
    46.             {
    47.                 Block b = e.getBlock().getRelative(BlockFace.UP);
    48.                 while(tempList.size() < maxRemoveHeight)
    49.                 {
    50.                     tempList.add(b.getLocation());
    51.                     b = b.getRelative(BlockFace.UP);
    52.                 }
    53.                 if(tempList.size() == maxRemoveHeight)
    54.                 {
    55.                     for(int i = 0; i < maxRemoveHeight; i++)
    56.                     {
    57.                         final int no = i;
    58.                         Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable()
    59.                         {
    60.                             public void run()
    61.                             {
    62.                              
    63.                                 if(no == maxRemoveHeight)
    64.                                 {
    65.                                  
    66.                                 }
    67.                             }
    68.  
    69.                         }, i * 20);
    70.                     }
    71.                 }
    72.             }
    73.             else
    74.             {
    75.  
    76.             }
    77.         }
    78.         else
    79.         {
    80.  
    81.         }
    82.     }
    83. }
    Then?....
     

Share This Page