Solved Keep rerolling until requirement is met

Discussion in 'Plugin Development' started by jumbledProgram, Aug 29, 2020.

Thread Status:
Not open for further replies.
  1. Im making a /wild command in my plugin and this is the code :
    Code:
    if(sender instanceof Player) {
                    Player p = (Player) sender;
                    if(p.isOp() == true) {
                        if(args.length == 1) {
                            int randX = ThreadLocalRandom.current().nextInt(-100000, 100000 + 1);
                            int randZ = ThreadLocalRandom.current().nextInt(-100000, 100000 + 1);
                            World world = Bukkit.getServer().getWorlds().get(0);
                            Location randLoc = world.getHighestBlockAt(randX, randZ).getLocation();
                            Material i = (randLoc.add(0, -1, 0).getBlock().getType());
                            if (i != Material.LAVA && i != Material.WATER && i != Material.FIRE) {
                                p.teleport(randLoc);
                                sender.sendMessage(MSG + ChatColor.GREEN + "You are now in the wild.");
                            } else {
                                //do all the code again
                            }
                        }
                    } else {
                        sender.sendMessage(NOPERM);
                        return true;
                    }
                } else {
                    sender.sendMessage(NOPLAYER);
                    return true;
                }
    
    Im trying to make it so that if the material is lava, water, or fire, it re rolls the random coordinates and does all the code again.
     
  2. Offline

    KarimAKL

    @jumbledProgram Have you heard of a while loop? If not, try looking it up.
     
  3. ok so i tried this but no luck
    Code:
    if(sender instanceof Player) {
                    Player p = (Player) sender;
                    if(p.isOp() == true) {
                        if(args.length == 1) {
                            int randX = ThreadLocalRandom.current().nextInt(-100000, 100000 + 1);
                            int randZ = ThreadLocalRandom.current().nextInt(-100000, 100000 + 1);
                            World world = Bukkit.getServer().getWorlds().get(0);
                            Location randLoc = world.getHighestBlockAt(randX, randZ).getLocation();
                            Material i = (randLoc.add(0, -1, 0).getBlock().getType());
                            while(i == Material.LAVA || i == Material.WATER || i == Material.FIRE) {
                                randX = ThreadLocalRandom.current().nextInt(-100000, 100000 + 1);
                                randZ = ThreadLocalRandom.current().nextInt(-100000, 100000 + 1);
                                randLoc = world.getHighestBlockAt(randX, randZ).getLocation();
                                i = (randLoc.add(0, -1, 0).getBlock().getType());
                                if(i != Material.LAVA && i != Material.WATER && i != Material.FIRE) {
                                    p.teleport(randLoc);
                                    sender.sendMessage(MSG + ChatColor.GREEN + "You are now in the wild.");
                                }
                            }
                        }
                    } else {
                        sender.sendMessage(NOPERM);
                        return true;
                    }
                } else {
                    sender.sendMessage(NOPLAYER);
                    return true;
                }
    
     
  4. Offline

    Kars

    Here you go. I didn't test this but it should work.
    PHP:
    if (!(sender instanceof Player)) {
        
    sender.sendMessage(NOPLAYER);
        return 
    true;  
    }

    Player p = (Playersender;

    if (!
    p.isOp()) {
        
    sender.sendMessage(NOPERM);
        return 
    true;
    }

    if (
    args.length != 1) {
        return;
    }

    teleportToLocationIfSafeOrRetry();
    And these functions
    PHP:
    private Location randomLocation() {
        
    int randX ThreadLocalRandom.current().nextInt(-100000100000 1);
        
    int randZ ThreadLocalRandom.current().nextInt(-100000100000 1);
        
    World world Bukkit.getServer().getWorlds().get(0);
        return 
    world.getHighestBlockAt(randXrandZ).getLocation();
    }

    private 
    teleportToLocationIfSafeOrRetry() {
        
    Location randLoc randomLocation();
        
    Material i = (randLoc.add(0, -10).getBlock().getType());
      
        if (
    != Material.LAVA && != Material.WATER && != Material.FIRE) {
            
    p.teleport(randLoc);
            
    sender.sendMessage(MSG ChatColor.GREEN "You are now in the wild.");
            return;
        }
      
        
    teleportToLocationIfSafeOrRetry();  
    }
     
  5. After going out to clear my head, (and some family issues) i figured out a way to do it
    Code:
    if(sender instanceof Player) {
                    Player p = (Player) sender;
                    if(p.isOp() == true) {
                        if(args.length == 0) {
                            Boolean finding = true;
                            World world = Bukkit.getServer().getWorlds().get(0);
                            while(finding) {
                                int randX = ThreadLocalRandom.current().nextInt(-1000000, 1000000 + 1);
                                int randZ = ThreadLocalRandom.current().nextInt(-1000000, 1000000 + 1);
                                Location randLoc = world.getHighestBlockAt(randX, randZ).getLocation().add(0, 2, 0);
                                Material i = (randLoc.add(0, -2, 0).getBlock().getType());
                                if (!(i == Material.LAVA || i == Material.WATER || i == Material.FIRE)) {
                                    p.teleport(randLoc.add(0.5, 1, 0.5));
                                    sender.sendMessage(MSG + ChatColor.GREEN + "You are now in the wilderness.");
                                    p.playSound(p.getLocation(), Sound.ENTITY_ENDERMAN_TELEPORT, 5f, 1f);
                                    //randLoc.add(0, -1, 0).getBlock().setType(Material.REDSTONE_BLOCK);
                                    finding = false;
                                    return true;
                                } else {
                                    //do nothing
                                }
                            }
                            return true;
                        }
                    } else {
                        sender.sendMessage(NOPERM);
                        return true;
                    }
                } else {
                    sender.sendMessage(NOPLAYER);
                    return true;
                }
     
Thread Status:
Not open for further replies.

Share This Page