Solved Placing a block when another one's broken.

Discussion in 'Plugin Development' started by roundrider, Sep 6, 2013.

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

    roundrider

    OK, so I've never developed a plugin, yeah, this is my first time, and I've been following the wiki. But I'm quite lost now.

    I want that when a player breaks a block, that runs a script that places another block there.

    Example:
    • Player "PLAYER" breaks STONE
    • STONE coordinates: 1 64 1
    • Generate LIGHT GRAY WOOL in 1 64 1
    • After 30s STONE replaces LIGHT GRAY WOOL in 1 64 1
    I think it is a very, very simple plugin, but I haven't found any like this. I've seen one, but I want to be able to choose a different wool for each block, and also a different timing.

    It's a plugin for private use, I won't release it, so if you could help me... I would really appreciate that.
     
  2. Offline

    joehot2000

    So, without a delay:
    Code:
    @EventHandler
    public void onBreak(BlockBreakEvent e){
    Block b = e.getBlock();
    if (b.getType == Material.STONE) rm(b.getLocation);
    
    To do a delayed task, add this:

    Code:
    public void rm(Location l){
    final Location loc = l; //Has to be final.
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable()
    {
     
     
     
      @Override
      public void run()
      {
     
          //Set type here
                          loc.getBlock().setType(Material.WOOL);
      }
    }, 20*5); //ignore the 20*, 5 is the amount of seconds.
    }
    
     
  3. Offline

    roundrider


    So I did this in my class:
    Code:java
    1. package me.Eric.roundrider;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Location;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.block.BlockBreakEvent;
    13. import org.bukkit.plugin.PluginDescriptionFile;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. public class Bloque extends JavaPlugin
    17. public static Logger logger = Logger.getLogger("Minecraft");
    18. public static Bloque plugin;
    19.  
    20. @EventHandler
    21. public void onBreak(BlockBreakEvent e)
    22. Block b = e.getBlock();
    23. if (b.getType == Material.STONE) rm(b.getLocation);
    24.  
    25. public void rm(Location l)
    26. final Location loc = l; //Has to be final.
    27. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable()
    28. {
    29.  
    30.  
    31.  
    32. @Override
    33. public void run()
    34. {
    35.  
    36. //Set type here
    37. loc.getBlock().setType(Material.WOOL);
    38. }
    39. }, 20*5); //ignore the 20*, 5 is the amount of seconds.
    40. }


    Mostly just copying your code and importing stuff from the Bukkit library. So now, do I have to export it as a jar? Is it ready? Any errors?

    You're really helping me here I didn't think I would have such a quick answer, thanks mate!
     
  4. Offline

    joehot2000

    the public void rm(Location l) is a seperate method. Take it out of the onBlockBreak method - put it and all its contents above/outside the method.
     
  5. Offline

    The_Doctor_123

    You need to register events to that class.
     
  6. Offline

    roundrider

    Im so confused... can you export it for me?
     
  7. Offline

    The_Coder

    roundrider likes this.
  8. Offline

    Garris0n

    If you want a plugin you should probably post in the plugin requests section...this section is for people looking help with the code, not exported plugins.
     
  9. Offline

    sgavster

    You need to do this:
    Code:java
    1. public class Bloque extends JavaPlugin implements Listener{

    And you need to add these:
    Code:
        @Override
        public void onEnable(){
            PluginManager pm = Bukkit.getPluginManager();
            pm.registerEvents(this, this);
        }
     
        @Override
        public void onDisable(){
        }
    }
    before you do the @EventHandlers.
     
    roundrider likes this.
  10. Offline

    roundrider

    And then what do I do with the eventhandlers?
     
  11. Offline

    sgavster

    roundrider You just put it below the code I gave you, so it'd be like this:

    Code:java
    1. package me.Eric.roundrider;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.Location;
    7. import org.bukkit.Material;
    8. import org.bukkit.block.Block;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.block.BlockBreakEvent;
    12. import org.bukkit.plugin.PluginManager;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. public class Bloque extends JavaPlugin implements Listener
    16. {
    17. public static Logger logger = Logger.getLogger("Minecraft");
    18. public static Bloque plugin;
    19.  
    20. @Override
    21. public void onEnable()
    22. {
    23. PluginManager pm = Bukkit.getPluginManager();
    24. pm.registerEvents(this, this);
    25. }
    26.  
    27. @Override
    28. public void onDisable()
    29. {
    30. }
    31.  
    32.  
    33. @EventHandler
    34. public void onBreak(BlockBreakEvent e)
    35. {
    36. Block b = e.getBlock();
    37. Location l = b.getLocation();
    38. if (b.getType() == Material.STONE)
    39. {
    40. final Location loc = l;
    41. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable()
    42. {
    43. @Override
    44. public void run()
    45. {
    46. loc.getBlock().setType(Material.WOOL);
    47. }
    48. }, 20*5);
    49. }
    50. }
    51. }
    52.  


    Hope that helps a bit :)


    Just to note, I cleaned up the code a lot, fixed brackets and stuff!
     
    roundrider likes this.
  12. Offline

    roundrider


    Thanks for everything, you really helped a lot!
     
Thread Status:
Not open for further replies.

Share This Page