Placing Blocks ontop of the one previously placed

Discussion in 'Plugin Development' started by maxben34, Nov 12, 2013.

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

    maxben34

    The title is confusing, i know, but that isn't the point. What I'm trying to do is take a placed block, and set a block of the same type with a 1 higher y value. Then another block gets placed ontop of that one, and the previous one gets removed and so on. This is the code that i currently have, which isn't working right as it only places a block ontop of the first placed block and then stops.
    Code:java
    1. package me.maxben34.blocks;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Location;
    6. import org.bukkit.Material;
    7. import org.bukkit.World;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.EventPriority;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.block.BlockPlaceEvent;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class blocks extends JavaPlugin implements Listener{
    15. public void onEnable(){
    16. Bukkit.getPluginManager().registerEvents(this, this);
    17. }
    18. @EventHandler(priority = EventPriority.HIGH)
    19. public void onblockplace(final BlockPlaceEvent e){
    20. if(e.getBlock().getType().equals(Material.DIAMOND_BLOCK)){
    21. e.getBlock().setType(Material.AIR);
    22. Bukkit.broadcastMessage(ChatColor.AQUA + "" + ChatColor.BOLD + "Placing Blocks!");
    23. Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    24. public void run(){
    25. World w = Bukkit.getWorld("world");
    26. double x = e.getBlock().getLocation().getX();
    27. double y = e.getBlock().getLocation().getY();
    28. double z = e.getBlock().getLocation().getZ();
    29. Location loc = new Location(w,x,y,z);
    30. loc.getBlock().setType(Material.DIAMOND_BLOCK);
    31. y++;
    32. }}, 1, 5);
    33. Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    34. public void run(){
    35. World w = Bukkit.getWorld("world");
    36. double x = e.getBlock().getLocation().getX();
    37. double y = e.getBlock().getLocation().getY();
    38. double z = e.getBlock().getLocation().getZ();
    39. Location loc = new Location(w,x,y,z);
    40. loc.getBlock().setType(Material.AIR);
    41. y++;
    42. }}, 5, 5);
    43. }
    44. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    45. public void run(){
    46. stop();
    47. }
    48. },100);
    49. }
    50. public void stop(){
    51. Bukkit.getScheduler().cancelTasks(this);
    52. }
    53. }
     
  2. Offline

    Xacero

    maxben34
    BlockPlaceEvent isn't called on block.setType();
    You'll need to create a looping thread for this pretty sure.
    (or rather, loop what you already have)
     
  3. Offline

    TeeePeee

    This code is really strange... If you want a synchronous task to keep putting blocks above the one placed, use this.

    Code:java
    1. @EventHandler(priority = EventPriority.HIGH)
    2. public void onblockplace(final BlockPlaceEvent e){
    3. if(e.getBlock().getType().equals(Material.DIAMOND_BLOCK)){
    4. e.getBlock().setType(Material.AIR);
    5. Bukkit.broadcastMessage(ChatColor.AQUA + "" + ChatColor.BOLD + "Placing Blocks!");
    6. Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    7. public void run(){
    8. for(int y = e.getLocation().getY(); y < e.getLocation().getWorld().getMaxBuildHeight(); y++) {
    9. e.getLocation().getWorld().getBlockAt(e.getLocation().getX(), y, e.getLocation().getZ()).setType(Material.DIAMOND_BLOCK);
    10. }, 0L);
    11.  


    Wrote it in the forum java menu, so there might be some syntax issues.
     
  4. Offline

    AirWick

    Do you want it to end? If not, you can just loop through getting the block under that one using getBlockAt() I believe then setting it to Air, and getting it's type and setting it to the location you're at now, until reaching the max build height
     
  5. Offline

    maxben34

    TeeePeee
    Seems like it will work, except i'm getting one little problem

    Code:java
    1. public void run(){
    2. for(double y = ((Block) e).getLocation().getY(); y < ((Block) e).getLocation().getWorld().getMaxHeight(); y++) {
    3. ((Block) e).getLocation().getWorld().getBlockAt(((Block) e).getLocation().getX(), y, ((Block) e).getLocation().getZ()).setType(Material.DIAMOND_BLOCK);
    4. }
    5. }}, 0L, 0L);
    6. Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    7. public void run(){
    8. for(double y = ((Block) e).getLocation().getY(); y < ((Block) e).getLocation().getWorld().getMaxHeight(); y++) {
    9. ((Block) e).getLocation().getWorld().getBlockAt((((Block) e).getLocation().getX()), y, ((Block) e).getLocation().getZ()).setType(Material.DIAMOND_BLOCK);
    10. }
    11.  
    12. }}, 1L, 1L);

    It looks like this now, but the getBlockAt is returning an error that the args int,int,int aren't applicable for double,double,double, however it's referring to coordinates so by default wouldn't it be a double? I just don't understand that.
     
  6. Offline

    TeeePeee

    maxben34
    Just cast the doubles to integers.
     
  7. Offline

    maxben34

    That's what I thought but it got confusing due to all the casting. Thanks!

    It ended up working "correctly" as in where it places the blocks, but it doesn't do it one by one. It just sets a big pillar of diamond blocks. Instead of setting a diamond block and then placing another one on top and then removing the one below and so on.

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

Share This Page