Items with velocities

Discussion in 'Spigot Plugin Development' started by Kingbluesapphire, Jul 8, 2018.

  1. I am working on a Spigot plugin with drop parties, I am trying to throw out items with different velocities the problem is my current code throws them all in the same direction also doesn't seem very smooth and sometimes they go too far. I tried changing from getting a random int to a random double but it still doesn't throw them correctly.

    Code (Java):
      private static Random r = new Random();
        private static double randomValue = 0 + (1 - 0) * r.nextDouble();
     
        private static void dropParty(Location location) {      
            BukkitRunnable r = new BukkitRunnable() {
                int count = 0;
                Location loc = location.add(0, 2, 0).getBlock().getLocation();
                @Override
                public void run() {
                    if(count > 20)
                        cancel();
                    Item item = loc.getWorld().dropItemNaturally(loc, items[Math.random(0, items.length - 1)]);
                    item.setVelocity(new Vector(randomValue, 0.5, randomValue));
                    count++;
                }
            };
            r.runTaskTimer(Main.getPlugin(), 10, 10);
        }
     
  2. _Ug

    _Ug

    You need to get a new random double each time you drop an item. Also, writing randomValue as both the X and Z velocity will mean that the item will always be tossed on the line X = Z. Instead you should create a random for X and Z when tossing the item.

    Code (Text):

      private static Random r = new Random();

     
        private static void dropParty(Location location) {  
            BukkitRunnable r = new BukkitRunnable() {
                int count = 0;
                Location loc = location.add(0, 2, 0).getBlock().getLocation();
                @Override
                public void run() {
                    if(count > 20)
                        cancel();
                    Item item = loc.getWorld().dropItemNaturally(loc, items[Math.random(0, items.length - 1)]);
                    double rX = 0 + (1 - 0) * r.nextDouble();
                    double rZ = 0 + (1 - 0) * r.nextDouble();
                    item.setVelocity(new Vector(rX, 0.5, rZ));
                    count++;
                }
            };
            r.runTaskTimer(Main.getPlugin(), 10, 10);
        }
    Also, 0 + (1-0) * r.nextDouble(); is the exact same as r.nextDouble();
     
  3. The items only seem to go East and South now, also how can I make the distance they go less?
     
  4. From the doc:
    Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.

    Use a range, since #nextXYZ(x) goes from [0, x), then given y you can go from [-y, y) by using nextDouble(2 * y) - y