How to remove a block from code?

Discussion in 'Plugin Development' started by ViciousVanilla, Apr 29, 2014.

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

    ViciousVanilla

    Hi, I'm trying to code something where when they hold an item blocks will spawn around them. I have that figured out, but I'm trying to figure out how to remove the blocks spawned after 2 seconds.

    This is what I have for the blocks spawning:

    Player p = event.getPlayer();

    Location loc = p.getLocation();

    loc.getBlock().setType(Material.WOOD_STAIRS);

    I'm trying to figure out how to remove those blocks and what event it would be under.
    I've tried BlockPlacedEvent but the player isn't actually placing anything so I don't think that works.
    Thanks for the help, sorry if this sounds confusing, its hard to explain.
     
  2. Offline

    thecrystalflame

    BlockChangeEvent? also what exactly do you want, do you want to have the blocks removing after a delay or whats the point in even changing them?
     
  3. Offline

    ViciousVanilla

    Yes I want the blocks removing after a delay.

    BlockChangeEvent isn't in their? Or do I have the wrong imports?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  4. Offline

    thecrystalflame

    ok sorry my bad i was on my phone at the time, blockchangevent doesn't exist your imports are fine. if you want to change the blocks back after a certain time limit i would suggest this.

    Make an object that contains a material(Make this the origonal material not the tempory) and a location. name it something like TempBlock, make getters for both variables.
    make a new class that implements Runnable, in the class have these elements. name the runnable TempBlockRestoreTask or something else relative.

    Code:JAVA
    1.  
    2. private Map<TempBlock, Long> tempBlocks = new HashMap<TempBlock, Long>();
    3.  
    4. public Map<TempBlock, Long> getTempBlocks() {
    5. return TempBlocks;
    6. }
    7.  
    8. public void run() {
    9. for (Map.Entry<TempBlock, Long> current : tempBlocks.entrySet()) {
    10. if (current.getValue() >= System.getCurrentTimeMillis()) {
    11. current.getKey().getLocation().getBlock().setType(current.getKey.getMaterial());
    12. }
    13. }
    14. }
    15.  


    make sure you save this runnable as an instance that you can retrieve from your pluygins main class or something similar.
    for example in your plugins main class:

    Code:java
    1.  
    2. //make sure you also make a getter
    3. private TempBlockRestoreClass tempBlockRestoreClass = new TempBlockRestoreClass();
    4.  


    then after you make the temporary blcok in your event, before this line: loc.getBlock().setType(Material.WOOD_STAIRS);

    reference to the TempBlockRestoreTask.getTempBlocks().put(new TempBlock(loc.getBlock().getType(), loc), System.getCurrentTimeMillis()+whateverlengthyouwant);

    then register the runnable in your onenable();

    i have explained this assuming you have fairly good knowledge of java. let me know if you need moore help on the matter.
     
  5. Offline

    ViciousVanilla


    ok I'll try it thanks, I'll get back to you after I test it.


    So your pretty much saying to store the block that was there before and after the delay replace it?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  6. Offline

    thecrystalflame

    oh and remember to remove the tempblock from the map in your runnable after changing it back to the origonal form
     
  7. Offline

    mrgreen33gamer

    Hello there! I have a simple something something that I think you would like, it's a chest code, but just remove the chest part, here is the code (#I-Love-To-Spoon-Feed!)

    Put this anywhere in your class except for inside of a public void or a private void:
    Code:java
    1.  
    2. private void carePackage(World world, int x, int z, final Player player) {
    3. int y = world.getHighestBlockYAt(x, z);
    4.  
    5. Block chest1 = world.getBlockAt(x, y + 1, z);
    6. chest1.setType(Material.CHEST);
    7.  
    8.  
    9. Random random = new Random();
    10.  
    11. int r1 = random.nextInt(3);
    12. int r2 = random.nextInt(3);
    13. int r3 = random.nextInt(3);
    14.  
    15.  
    16. //Example of Fake Item: chestInv.setItem(random.nextInt(27), new ItemStack(Material.WEB, (random.nextInt(1))));
    17.  
    18.  
    19. final Block b = Bukkit.getWorld("World").getBlockAt(x, y + 1, z);
    20. Chest getchest1 = (Chest) b.getState();
    21. Inventory chestInv = getchest1.getInventory();
    22.  
    23.  
    24. ItemStack knockbackstick = new ItemStack (Material.STICK, 1);
    25. ItemMeta knockbackstickMeta = knockbackstick.getItemMeta();
    26. knockbackstickMeta.setDisplayName(ChatColor.GREEN + "Knocky Backy Sticky");
    27. knockbackstickMeta.addEnchant(Enchantment.KNOCKBACK, 2, true);
    28. knockbackstick.setItemMeta(knockbackstickMeta);
    29.  
    30. ItemStack trickdsword = new ItemStack (Material.DIAMOND_SWORD, (random.nextInt(1)));
    31. ItemMeta trickdswordMeta = trickdsword.getItemMeta();
    32. trickdswordMeta.setDisplayName(ChatColor.RED + "Ahahaha Tricked Ya!");
    33. trickdsword.setItemMeta(trickdswordMeta);
    34.  
    35. if(r1 == 0) chestInv.setItem(random.nextInt(27), new ItemStack(Material.MUSHROOM_SOUP, (random.nextInt(6))));
    36. if(r1 == 1) chestInv.setItem(random.nextInt(27), new ItemStack(Material.IRON_SWORD, 1));
    37. if(r1 == 2) chestInv.setItem(random.nextInt(27), new ItemStack(Material.IRON_CHESTPLATE, 1));
    38. if(r2 == 0) chestInv.setItem(random.nextInt(27),new ItemStack(Material.IRON_LEGGINGS, 1));
    39. if(r2 == 1) chestInv.setItem(random.nextInt(27),new ItemStack(Material.MUSHROOM_SOUP, (random.nextInt(10))));
    40. if(r2 == 2) chestInv.setItem(random.nextInt(27),new ItemStack(trickdsword));
    41. if(r3 == 0) chestInv.setItem(random.nextInt(27),new ItemStack(knockbackstick));
    42. if(r3 == 1) chestInv.setItem(random.nextInt(27),new ItemStack(Material.IRON_HELMET, 1));
    43. if(r3 == 2) chestInv.setItem(random.nextInt(27),new ItemStack(Material.MUSHROOM_SOUP, (random.nextInt(5))));
    44.  
    45. this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    46. public void run() {
    47. b.setType(Material.AIR);
    48. player.sendMessage(ChatColor.RED + "Your carepackage was removed!");
    49. }
    50. }, 10 * 20);
    51. this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    52. public void run() {
    53. player.sendMessage(ChatColor.RED + "1");
    54. }
    55. }, 9 * 20);
    56. this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    57. public void run() {
    58. player.sendMessage(ChatColor.RED + "2");
    59. }
    60. }, 8 * 20);
    61. this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    62. public void run() {
    63. player.sendMessage(ChatColor.RED + "3");
    64. }
    65. }, 7 * 20);
    66. this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    67. public void run() {
    68. player.sendMessage(ChatColor.RED + "4");
    69. }
    70. }, 6 * 20);
    71. this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    72. public void run() {
    73. player.sendMessage(ChatColor.RED + "5");
    74. }
    75. }, 5 * 20);
    76. this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    77. public void run() {
    78. player.sendMessage(ChatColor.RED + "Your carepackage will be removed in");
    79. }
    80. }, 4 * 20);
    81. this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    82. public void run() {
    83. player.sendMessage(ChatColor.GOLD + "You have 10 seconds before your carepackage is gone!");
    84. player.sendMessage(ChatColor.GOLD + "Collect your items fast!");
    85. player.getServer().broadcastMessage(ChatColor.RED + "The player " + ChatColor.WHITE + player.getName() + ChatColor.RED + " has bought a carepackage at " + player.getLocation().getBlockX() + " " + player.getLocation().getBlockZ());
    86. }
    87. }, 1);
    88.  
    89. }
    90.  


    Then if you want to spawn a chest using a command, here is the command form:
    Code:java
    1.  
    2. if ((commandLabel.equalsIgnoreCase("carepackage")))
    3. {
    4. World world = player.getWorld();
    5. carePackage(world, player.getLocation().getBlockX(), player.getLocation().getBlockZ(), player);
    6. }
    7. }
    8.  


    Enjoy!
     
  8. Offline

    thecrystalflame

    this is far more messy and complicated then what Vanilla needs, as i previosuly stated only 1 runnable is required and it reuires hardly any code. this method would only complicate things.

    thats the general idea yes. the get the runnable to run every second or something and you should see clean results

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  9. Offline

    Zach_1919

    ViciousVanilla First, create a HashMap that stores locations and Materials. When they hold the item, set the block to whatever you want, but then add that block's information to the HashSet. At the same time, use Bukkit's Scheduler to start a delayed task and set the delay to whatever you want. When that delay is up, loop through the area around the player and check if each location is a key in the HashMap. Set the location to the stored corresponding Material. Bam. Done.
     
  10. Offline

    xTigerRebornx

    Zach_1919 Really, all he'd need is a List<BlockState> that is method-specific to the method he uses to restore the blocks, he simply store the BlockState(s) before he edits the blocks, then schedule a task to call an update to those stored BlockState, causing them to restore to their previous value.
     
  11. Offline

    thecrystalflame

    D
    Hmm funny that sounds like what I explained good work detective
     
  12. Offline

    mrgreen33gamer

    Well it's a code example, I am just not gonna make one up for the user to have...... What were you thinking?
     
  13. Offline

    ViciousVanilla

    Thanks guys, Ill try your ideas
     
Thread Status:
Not open for further replies.

Share This Page