Solved Making sure block at y is above ground

Discussion in 'Plugin Development' started by ark9026, Jul 27, 2015.

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

    ark9026

    Hello! I'm trying to spawn a chest every 10 minutes(12000 ticks) at a random x from 1-1000 and a random z from 1-1000(If anyone knows how to make that negative too please comment). Since I want the y to be on the ground, not underground or above the ground, I looped through it in a while loop. The problem is that it keeps repeating, repeating, repeating. Here's my code:
    Code:
    package us.crimsonmc.crimsonsupplydrop;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    @SuppressWarnings("unused")
    public class Main extends JavaPlugin implements Listener {
    
        public List<Integer> newYArray = new ArrayList<Integer>();
    
        @Override
        public void onEnable() {
            getLogger()
                    .info("CrimsonSupplyDrop version 1.0.1 is starting up! This plugin was coded by ark9026");
            getServer().getPluginManager().registerEvents(this, this);
        }
    
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            spawnDrop();
            System.out.println("Event Works");
        }
    
        public void spawnDrop() {
            for (final Player p : Bukkit.getOnlinePlayers()) {
                Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                    public void run() {
                int x = (int) (Math.random() * 1000);
                int z = (int) (Math.random() * 1000);
                int y = 0;
                Block world = Bukkit.getWorld("world").getBlockAt(x, y, z);
                System.out.println("Get block works");
                while (!(world.getType() == Material.AIR)) {
                    int newY = y + 1;
                    System.out.println("While loop works");
                    if (newYArray.contains(0)) {
                        newYArray.clear();
                    }
                    System.out.println("Air block found!");
                    newYArray.add(newY);
                    if (newYArray.contains(1)) {
                        System.out.println("ArrayList glitch found, resolving...");
                        newYArray.set(0, 1);
                        System.out.println("Glitch resolved :)");
                    }
                    world.setType(Material.CHEST);
                    System.out.println("Supply drop incoming at x:" + x + " y:"
                            + newYArray.get(0) + " z:" + z);
                    p.sendMessage("Supply drop incoming at x:" + x + " y:"
                            + newYArray.get(0) + " z:" + z);
                }
            }
            }, 0L, 12000L);
            }
        }
    }
    
    And here is my console:
    Code:
    [20:49:00 INFO]: Air block found!
    [20:49:00 INFO]: ArrayList glitch found, resolving...
    [20:49:00 INFO]: Glitch resolved :)
    [20:49:00 INFO]: Supply drop incoming at x:303 y:1 z:102
    [20:49:00 INFO]: While loop works
    [20:49:00 INFO]: Air block found!
    [20:49:00 INFO]: ArrayList glitch found, resolving...
    [20:49:00 INFO]: Glitch resolved :)
    [20:49:00 INFO]: Supply drop incoming at x:303 y:1 z:102
    [20:49:00 INFO]: While loop works
    [20:49:00 INFO]: Air block found!
    [20:49:00 INFO]: ArrayList glitch found, resolving...
    [20:49:00 INFO]: Glitch resolved :)
    [20:49:00 INFO]: Supply drop incoming at x:303 y:1 z:102
    [20:49:00 INFO]: While loop works
    [20:49:00 INFO]: Air block found!
    [20:49:00 INFO]: ArrayList glitch found, resolving...
    [20:49:00 INFO]: Glitch resolved :)
    [20:49:00 INFO]: Supply drop incoming at x:303 y:1 z:102
    [20:49:00 INFO]: While loop works
    [20:49:00 INFO]: Air block found!
    [20:49:00 INFO]: ArrayList glitch found, resolving...
    [20:49:00 INFO]: Glitch resolved :)
    [20:49:00 INFO]: Supply drop incoming at x:303 y:1 z:102
    [20:49:00 INFO]: While loop works
    [20:49:00 INFO]: Air block found!
    [20:49:00 INFO]: ArrayList glitch found, resolving...
    [20:49:00 INFO]: Glitch resolved :)
    [20:49:00 INFO]: Supply drop incoming at x:303 y:1 z:102
    [20:49:00 INFO]: While loop works
    [20:49:00 INFO]: Air block found!
    [20:49:00 INFO]: ArrayList glitch found, resolving...
    [20:49:00 INFO]: Glitch resolved :)
    [20:49:00 INFO]: Supply drop incoming at x:303 y:1 z:102
    [20:49:00 INFO]: While loop works
    [20:49:00 INFO]: Air block found!
    [20:49:00 INFO]: ArrayList glitch found, resolving...
    [20:49:00 INFO]: Glitch resolved :)
    [20:49:00 INFO]: Supply drop incoming at x:303 y:1 z:102
    [20:49:00 INFO]: While loop works
    [20:49:00 INFO]: Air block found!
    [20:49:00 INFO]: ArrayList glitch found, resolving...
    [20:49:00 INFO]: Glitch resolved :)
    [20:49:00 INFO]: Supply drop incoming at x:303 y:1 z:102
    [20:49:00 INFO]: While loop works
    [20:49:00 INFO]: Air block found!
    [20:49:00 INFO]: ArrayList glitch found, resolving...
    [20:49:00 INFO]: Glitch resolved :)
    [20:49:00 INFO]: Supply drop incoming at x:303 y:1 z:102
    [20:49:00 INFO]: While loop works
    [20:49:00 INFO]: Air block found!
    [20:49:00 INFO]: ArrayList glitch found, resolving...
    [20:49:00 INFO]: Glitch resolved :)
    [20:49:00 INFO]: Supply drop incoming at x:303 y:1 z:102
    [20:49:00 INFO]: While loop works
    [20:49:00 INFO]: Air block found!
    [20:49:00 INFO]: ArrayList glitch found, resolving...
    [20:49:00 INFO]: Glitch resolved :)
    [20:49:00 INFO]: Supply drop incoming at x:303 y:1 z:102
    [20:49:00 INFO]: While loop works
    [20:49:00 INFO]: Air block found!
    [20:49:00 INFO]: ArrayList glitch found, resolving...
    [20:49:00 INFO]: Glitch resolved :)
    [20:49:00 INFO]: Supply drop incoming at x:303 y:1 z:102
    [20:49:00 INFO]: While loop works
    [20:49:00 INFO]: Air block found!
    If someone could help that would be great!
     
    Last edited: Jul 27, 2015
  2. Offline

    cfil360

    @ark9026 I believe this will help.
    Code:
    World.getHighestBlockAt(Location);
     
  3. Offline

    ark9026

    @cfil360
    Thanks! Solved with:
    Code:
    package us.crimsonmc.crimsonsupplydrop;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
    
        @Override
        public void onEnable() {
            getLogger()
                    .info("CrimsonSupplyDrop version 1.0.1 is starting up! This plugin was coded by ark9026");
            getServer().getPluginManager().registerEvents(this, this);
        }
    
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            spawnDrop();
            System.out.println("Event Works");
        }
    
        public void spawnDrop() {
            for (final Player p : Bukkit.getOnlinePlayers()) {
                Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                    @SuppressWarnings("unused")
                    public void run() {
                int x = (int) (Math.random() * 1000);
                int z = (int) (Math.random() * 1000);
                int y = 0;
                Block b = Bukkit.getWorld("world").getBlockAt(x, y, z);
                Location l = new Location(Bukkit.getWorld("world"), x, y, z);
                Block newY = Bukkit.getWorld("world").getHighestBlockAt(l);
                int newYInt = newY.getY();
                Location newL = new Location(Bukkit.getWorld("world"), x, newYInt, z);
                newL.getBlock().setType(Material.CHEST);
                p.sendMessage("Supply drop incoming at x:" + x + " y:"
                            + newYInt + " z:" + z);
                }
                }, 0L, 12000L);
            }
        }
    }
    
     
Thread Status:
Not open for further replies.

Share This Page