Placing and removing a block?

Discussion in 'Plugin Development' started by curlyfries1999, Sep 20, 2012.

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

    curlyfries1999

    How can I place a block for about a second then delete it?
     
  2. Offline

    makskay

    Look into World.getBlockAt(Location) and Block.setType(Material). To "delete" the block, set the material type to air.
     
  3. Offline

    curlyfries1999

    makskay I've tryed what you said but I can't get it to work. Can you give me an example or something please?
     
  4. Offline

    XbannisherX

    for this you need a Location
    its like this....
    Location yourblock = event.getBlock();
    yourblock.setType(Material.AIR);
     
  5. Offline

    HouseMD

    event.setCanceled(true);
    To actually cancel it...
     
  6. Offline

    curlyfries1999

    I still can't get it to work. Could you show me on TeamViewer or something please?
     
  7. Offline

    XbannisherX

    show me your code , than ill make a example
     
  8. easy peasy. use a delayed sync task. I did this myself a minute ago:
    Code:
    plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                      public void run(){
                          event.getBlock().setType(Material.AIR); //sets the block to air
                      }
                }, 20 * 1); // 20 ticks is one second, probably dont need the *1 part.
    
    i used this for a block to only stay there for 3 seconds.
     
  9. Offline

    curlyfries1999

    For some reason it doesn't like event.getBlock()

    Edit:
    Code:
    The method getBlock() is undefined for the type Event
     
  10. Offline

    HouseMD

    Put the code into a block break event -.- and then name the event "event" do I can grab the events block.
     
  11. Offline

    curlyfries1999

    Still can't get it to work. Can you show me on TeamViewer or something please?
     
  12. Offline

    HouseMD

    no only noob use teamviewer.

    you need to register your event listener probably. look for a tutorial.
     
  13. Offline

    curlyfries1999

    If only that were true...

    The way you're telling me, does it work triggered by command?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  14. if you cant get to work, post you full code and mayby stack traces/errors then we can better help you
     
  15. Offline

    curlyfries1999

    Code:
    package me.CurlyFries.Healer;
     
    import java.util.logging.Logger;
     
    import net.minecraft.server.Block;
    import net.minecraft.server.World;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Healer extends JavaPlugin {
        public final Logger logger = Logger.getLogger("Minecraft");
        public static Healer plugin;
     
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion()
                    + " Has Been Disabled!");
           
        }
     
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion()
                    + " Has Been Enabled!");
        }
     
        public boolean onCommand(CommandSender sender, Command cmd,    String commandLabel, String[] args) {
            Player player = (Player) sender;
     
            if (commandLabel.equalsIgnoreCase("start")) {
                if (args.length == 0) {
     
                    for (Player p : player.getServer().getOnlinePlayers()) {
                        p.setHealth(20);
                        p.setFireTicks(0);
                        p.setFoodLevel(20);
                    }
     
                    Bukkit.broadcastMessage(ChatColor.GOLD
                            + "Everybody Was Healed!");
                    Bukkit.broadcastMessage(ChatColor.GOLD + "Game is Starting!");
                   
                   
                    //int x = -495;
                    //int y = 66;
                    //int z = 522;
                   
                                                         
                   
                }
     
            }
            return false;
        }
     
           
           
        }
    
     
  16. you seen the tutorials on the wiki how to do block manupulation?
     
    curlyfries1999 likes this.
  17. Offline

    curlyfries1999

    No.... I'll have a look

    I just tried what what it said on the wiki and I got this:
    Code:
    Type mismatch: cannot convert from org.bukkit.World to net.minecraft.server.World
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  18. Offline

    Kodfod

    you need to:

    Code:
    import org.bukkit.World;
    not ne.minecraft.server.World
     
    curlyfries1999 likes this.
  19. Offline

    curlyfries1999

    Thank you :D

    This is my new code:
    Code:
    package me.CurlyFries.Healer;
     
    import java.util.logging.Logger;
    import org.bukkit.World;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Healer extends JavaPlugin {
        public final Logger logger = Logger.getLogger("Minecraft");
        public static Healer plugin;
     
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion()
                    + " Has Been Disabled!");
     
        }
     
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion()
                    + " Has Been Enabled!");
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args, Location loc, int length) {
            Player player = (Player) sender;
     
            if (commandLabel.equalsIgnoreCase("start")) {
                if (args.length == 0) {
     
                    for (Player p : player.getServer().getOnlinePlayers()) {
                        p.setHealth(20);
                        p.setFireTicks(0);
                        p.setFoodLevel(20);
                    }
     
                    Bukkit.broadcastMessage(ChatColor.GOLD
                            + "Everybody Was Healed!");
                    Bukkit.broadcastMessage(ChatColor.GOLD + "Game is Starting!");
                   
                    World world = loc.getWorld();
     
                    int x = -495;
                    int y = 66;
                    int z = 522;
     
                    for (int x_operate = x; x_operate <= x;) {
                        for (int y_operate = y; y_operate <= y; y_operate++) {
                            for (int z_operate = z; z_operate <= z; z_operate++) {
                                org.bukkit.block.Block blockToChange = (world.getBlockAt(x, y, z));
                                blockToChange.setTypeId(57);
                            }
     
                        }
     
                    }
                   
                }
            }
           
            return false;
               
        }
    }
    I typed the command and it broadcasts the messages but the block isn't getting created. Anybody know why?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  20. Offline

    Kodfod

    Hold on, are you JUST trying to place a block for 20 ticks(One second) and then remove it?


    If so: Why do you have a for loop???
     
  21. Offline

    curlyfries1999

    um.... I just copied what it said on the wiki. I just got rid of it
     
  22. Offline

    Kodfod

    Okay Here is what you need to do.

    1. Get the location of where you want the block to be placed/removed.
    2. Create the block there:
    Code:
    loc.getBlock().setTypeID(57)
    3. Create a Delayed task to remove the block:
    Code:
    getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
     
      public void run() {
          loc.getBlock().setTypeID(0);
      }
    }, 60L);
    That Should do what you are trying to accomplish.
     
  23. Offline

    curlyfries1999

    How do I set loc?
     
  24. Offline

    Kodfod

    Let me get a little more information....

    What are you trying to accomplish? - Please be specific this will better aid me in helping you
    Do you have a Specific Location to Place/Remove the block?
    Is it just one block or many blocks?

    Sorry, should have asked these before the post above. =P


    Thanks,

    Kodfod
     
  25. Offline

    curlyfries1999

    Sorry. Its only one block. The coords are:
    x = -495
    y = 66
    z = 522
    This is only temporary while I am writing the plugin but I will change it when it's finished

    Might as well just tell you the coords that are needed:
    x = -452
    y = 69
    z = 527

    If you're wondering why I didn't say before is because I hadn't built the platform where the block needs to go but I have done it now

    BUMP!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  26. Offline

    Woobie

  27. Offline

    curlyfries1999

    ?

    BUMP!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  28. Is this where you are stuck right now? If so:

    I don't remember exactly, since I'm on a phone, but I think it is
    Location loc = new Location(world, x, y,z);

    The world might have been after the coordinates, pleace check that from the javadocs
     
  29. Offline

    curlyfries1999

    Code:
    package me.CurlyFries.Healer;
     
    import java.util.logging.Logger;
    import org.bukkit.World;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Healer extends JavaPlugin {
        public final Logger logger = Logger.getLogger("Minecraft");
        public static Healer plugin;
     
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion()
                    + " Has Been Disabled!");
     
        }
     
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion()
                    + " Has Been Enabled!");
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args, int length) {
            Player player = (Player) sender;
     
            if (commandLabel.equalsIgnoreCase("start")) {
                if (args.length == 0) {
     
                    for (Player p : player.getServer().getOnlinePlayers()) {
                        p.setHealth(20);
                        p.setFireTicks(0);
                        p.setFoodLevel(20);
                    }
     
                    Bukkit.broadcastMessage(ChatColor.GOLD
                            + "Everybody Was Healed!");
                    Bukkit.broadcastMessage(ChatColor.GOLD + "Game is Starting!");
                   
                    int x = -452;
                    int y = 69;
                    int z = 527;
                   
                    Location loc = new Location (player.getWorld(), x, y, z);   
                   
                    World world = loc.getWorld();           
                   
                    loc.getBlock().setTypeId(57);
                   
                                   
                }
            }
           
            return false;
               
       
        }
    }
    So why isn't the block getting created?

    BUMP!

    le bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
Thread Status:
Not open for further replies.

Share This Page