Solved Pick a random location

Discussion in 'Plugin Development' started by Letscrime, Apr 16, 2015.

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

    Letscrime

    Hello there!
    At first: I'am german so please dont judge my english :)


    I currently programming a new minigame, the goal is to collect 6 items and bring them outside to an entity.
    My problem is:

    If you play this one time you know where the items are, so i decided to spawn them random at PRE DEFINED locations.
    But i dont know how i spawn items at one of these locations?

    My current code doesnt throws an error message or something but it doesnt work, no items anywhere.

    My Locations (yes harcoded)
    Code:
    static World world = Bukkit.getWorld("Synfire2");
        static Location location = new Location(world, 40, 8, -6);
        static Location location2 = new Location(world, 34, 8, 28);
        static Location location3 = new Location(world, 39, 12, 32);
        static Location location4 = new Location(world, 37, 13, -7);
        static Location location5 = new Location(world, 46, 12, 6);
        static Location location6 = new Location(world, 46, 12, -7);
        static Location location7 = new Location(world, 45, 8, 2);
        static Location location8 = new Location(world, 38, 2, -10);
        static Location location9 = new Location(world, 47, 8, 30);
        static Location location10 = new Location(world, 47, 13, 27);
        static Location location11 = new Location(world, 45, 12, 18);
        static Location location12 = new Location(world, 41, 12, -8);
        static Location location13 = new Location(world, 38, 12, 17);
        static Location location14 = new Location(world, 33, 12, -4);
        static Location location15 = new Location(world, 33, 12, 0);
        static Location location16 = new Location(world, 14, 5, 23);
        static Location location17 = new Location(world, 16, 5, -9);
        static Location location18 = new Location(world, 28, 5, 28);

    My Itemstacks:
    Code:
        final static ItemStack dropPaper = new ItemStack(Material.PAPER, 1);
    
        final static ItemStack dropMap = new ItemStack(Material.EMPTY_MAP, 1);
    
        final static ItemStack dropEmerald = new ItemStack(Material.EMERALD, 1);
    
        final static ItemStack dropRedstone = new ItemStack(Material.REDSTONE, 1);
    
        final static ItemStack dropLever = new ItemStack(Material.LEVER, 1);
    
        final static ItemStack dropAnvil = new ItemStack(Material.ANVIL, 1);
    
    And here is my method to spawn the items:

    Code:
    ArrayList<Location> list = new ArrayList<Location>();
            list.add(location);
            list.add(location2);
            list.add(location3);
            list.add(location4);
            list.add(location5);
            list.add(location6);
            list.add(location7);
            list.add(location8);
            list.add(location9);
            list.add(location10);
            list.add(location11);
            list.add(location12);
            list.add(location13);
            list.add(location14);
            list.add(location15);
            list.add(location16);
            list.add(location17);
            list.add(location18);
         
            Random randomGenerator = new Random();
            int i = randomGenerator.nextInt(list.size());
       
            Location rand = list.get(i);
         
            world.dropItemNaturally(rand, dropPaper);
            world.dropItemNaturally(rand, dropMap);
            world.dropItemNaturally(rand, dropEmerald);
            world.dropItemNaturally(rand, dropRedstone);
            world.dropItemNaturally(rand, dropAnvil);
            world.dropItemNaturally(rand, dropLever);

    Can anyone help me?

    EDIT: I have to say: I cant use something like "getHighestLocation" at the y-coordinate.
    I have to spawn these items in a house
     
    Last edited: Apr 16, 2015
  2. Offline

    xmarinusx

    @Letscrime
    I can't see why your code doesn't work. What is the error you're getting?
     
  3. Offline

    _kampalus

    @xmarinusx He said, that there isn't any error.

    @Letscrime Did you disabled the Gamerule doItemDrops?
     
  4. Offline

    Letscrime

    @xmarinusx This is my problem, no error message.
    There is nothing..

    @_kampalus @xmarinusx

    AH I FOUND IT!
    All items are spawned at the same location, but i want them on diffrent locations. How can i do that :/

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

    xmarinusx

    @Letscrime
    You're now getting one location randomly and storing it in the rand variable. If you want all of them to be random use something like this:

    Code:java
    1.  
    2. world.dropItemNaturally(list.get(randomGenerator.nextInt(list.size())), dropPaper);
    3. world.dropItemNaturally(list.get(randomGenerator.nextInt(list.size())), dropMap);
    4. world.dropItemNaturally(list.get(randomGenerator.nextInt(list.size())), dropEmerald);
    5. world.dropItemNaturally(list.get(randomGenerator.nextInt(list.size())), dropRedstone);
    6. world.dropItemNaturally(list.get(randomGenerator.nextInt(list.size())), dropAnvil);
    7. world.dropItemNaturally(list.get(randomGenerator.nextInt(list.size())), dropLever);
    8.  


    This way you're getting a new random location for every item. Keep in mind though that some locations can still be the same.
     
  6. Offline

    AoH_Ruthless

    @xmarinusx @Letscrime
    It would be much more efficient to use a loop and remove each location object from the list after you use it...

    For example:
    Code:
    // Define your itemstacks, location list, and random number generator
    
    Random random = new Random();
    
    // Define a list to loop through with the itemstacks.
    List<ItemStack> items = new ArrayList<ItemStack>();
    
    for (ItemStack is : items) {
        int index = random.nextInt(list.size());
        // drop the item       
        world.dropItemNaturally(list.get(index);, is);
        // now remove this location from the list to avoid duplicates
        list.remove(index);
    }
     
  7. Offline

    Hex_27

    @Letscrime Theres actually a much better way to do this.
    Put all the locations in a new Arraylist<Locations>();
    Example:
    Code:
    ArrayList<Location> locations = new ArrayList<Location>();
    //put your locations in the arraylist
    //now, get a random location from this arraylist
    
    
    int locationnumber = new Random().nextInt(locations.length);
    
    //there you have it, your random location out of your list of locations.
    Location locationToUse = locations.get(locationnumber);
    
    
    
    This can give you a random location. You can make this into a method, and for each item, drop it and that location.
     
  8. Offline

    Letscrime

    Thank for all the answers, solved :)
     
  9. Offline

    nverdier

    Please mark this thread as so.
     
Thread Status:
Not open for further replies.

Share This Page