Spawning a falling block at random coords?

Discussion in 'Plugin Development' started by Johnzeh, May 8, 2013.

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

    Johnzeh

    So far, I have this:

    Code:
    World map = Bukkit.getWorld("world");
                       
                        double x = generator.nextDouble()*50-20;
                        double y = 220;
                        double z = generator.nextDouble()*50-20;
                           
                        Location objspawn = new Location(map,x,y,z);
                           
                        FallingBlock objective = map.spawnFallingBlock(objspawn, Material.CHEST, (byte) 0);
                        objective.setVelocity(new Vector(x, y, z));
                           
                        Bukkit.broadcastMessage("The chest has spawned at " + x + "," + y + "," + z);
    What it does as of now is broadcasts the random coords, but doesn't spawn anything at them. Also, it gives a nasty random number like 172.2029182349. Anybody have a solution to that? I want it to print like so: X: 172 Y: 255 Z: 850
     
  2. Offline

    Paper

    Johnzeh





    (int)Math.round(x) - That rounds it to the nearest whole number. I think that should work.
     
  3. Offline

    Johnzeh

    Ah yes thanks! Now, any ideas for spawning a falling block?
     
  4. Offline

    Paper

    Not sure :/
     
  5. Offline

    socram8888

    Code:
    World world = Bukkit.getWorld("world");
                     
    int x = (int) Math.round(generator.nextDouble() * 50 - 25);
    int y = 220;
    int z = (int) Math.round(generator.nextDouble() * 50 - 25);
    Location pos = new Location(world, x, y, z);
    
    FallingBlock objective = map.spawnFallingBlock(pos, Material.CHEST, (byte) 0);
     
    Bukkit.broadcastMessage("The chest has spawned at " + x + "," + y + "," + z);
    Untested, but it should work.
     
  6. Offline

    Johnzeh

    socram8888
    That's exactly the same or close to what I have. What I have right now does everything but spawn the falling chest. Here's a update of the code:

    Code:
    World map = Bukkit.getWorld("world");
                       
                        double x = generator.nextDouble()*5000;
                        double y = 220;
                        double z = generator.nextDouble()*5000;
                       
                        x = Math.round(x);
                        y = Math.round(y);
                        z = Math.round(z);
                       
                        Location objspawn = new Location(map,x,y,z);
                           
                        FallingBlock objective = map.spawnFallingBlock(objspawn, Material.BEDROCK, (byte) 0);
                       
                        objective.setVelocity(new Vector(x,y,z));
                       
                        Bukkit.broadcastMessage(ChatColor.AQUA + "The objective has spawned at X: " + x + ", Y: " + y + ", Z: " + z);
     
  7. Offline

    socram8888

    Floats are a bit nasty. When for example adding 1 + 1, they might not return 2, but rather 1.999999, and blocks can be spawned at 2, but not at 1.9999999. I recommend trying to modify your code to create a Location using integers rather than floats.
     
  8. Offline

    Johnzeh

    I have it so that it will round the coords so it should always return a real number.

    So, does anyone else have any ideas as to why the falling block doesn't spawn?

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

    Comphenix

    Why are you setting the velocity to the coordinates?
    Code:
    objective.setVelocity(new Vector(x,y,z));
    That would cause the falling block to fly off at enormous speeds (depending on coordinate though).

    Spawning a falling block right above the player seems to work just fine:
    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    3. if (sender instanceof Player) {
    4. Player player = (Player) sender;
    5. spawnFalling(player.getWorld(), player.getLocation().add(0, 120, 0));
    6. }
    7. return true;
    8. }
    9.  
    10. private void spawnFalling(World world, Location location) {
    11. world.spawnFallingBlock(location, Material.BEDROCK, (byte) 0);
    12. }

    Unfortunately, if you spawn this block at a random location not yet generated (or perhaps loaded), the entity is simply lost:
    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    3. if (sender instanceof Player) {
    4. Player player = (Player) sender;
    5. Random rnd = new Random();
    6.  
    7. spawnFalling(new Location(player.getWorld(),
    8. rnd.nextDouble() * 5000, 220, rnd.nextDouble() * 5000));
    9. }
    10. return true;
    11. }
    12.  
    13. private void spawnFalling(Location location) {
    14. location.getWorld().spawnFallingBlock(location, Material.BEDROCK, (byte) 0);
    15. Bukkit.broadcastMessage(ChatColor.AQUA + "The objective has spawned at " + location);
    16. }

    Instead, just create the chest manually by getting the highest non-air block and setting the block above to a chest:
    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    3. if (sender instanceof Player) {
    4. Player player = (Player) sender;
    5. Random rnd = new Random();
    6.  
    7. createChest(player.getWorld(), (int) (rnd.nextDouble() * 5000), (int) (rnd.nextDouble() * 5000));
    8. }
    9. return true;
    10. }
    11.  
    12. private void createChest(World world, int x, int z) {
    13. int y = world.getHighestBlockYAt(x, z);
    14.  
    15. // And so on
    16. world.getBlockAt(x, y + 1, z).setType(Material.CHEST);
    17. Bukkit.broadcastMessage(ChatColor.AQUA + "Created chest at " + x + ", " + z);
    18. }

    That seems to work fine.

    Maybe it would be nice to have a falling animation, but players are unlikely to catch it until it lands anyway. And you can always keep track of the spawned location until the chunk has been loaded and generated, and only then spawn the falling block.
     
Thread Status:
Not open for further replies.

Share This Page