Solved Launch Snowball entity

Discussion in 'Plugin Development' started by Official_Spendrew, Dec 22, 2014.

Thread Status:
Not open for further replies.
  1. I am trying to spawn an entity (snowball) at random coords between x:-398 y:161 z:352 and x:-227 y:161 z:541 with downward velocity. How can I go about doing that?
     
  2. Offline

    ChipDev

    What do you have?
     
  3. Offline

    Skionz

  4. Snowball snowball = player.getWorld().spawn(player.getLocation(), Snowball.class);snowball.setVelocity(player.getLocation().getDirection().multiply(3));

    I have this sample code but its for launching the entity from a player.
     
  5. Offline

    Skionz

    @Official_Spendrew Use the Random class to create a random Location instead of using the Player's
     
  6. Offline

    pookeythekid

    @Official_Spendrew #spoodfeed, but I may be wrong about this. Trying it will prove me right or wrong.
    Code:
    Location corner1 = new Location(world, -398, 161, 352);
    Location corner2 = new Location(world, -227, 161, 541);
    int xdiff = corner1.getX() - corner2.getX();
    int zdiff = corner2.getZ() - corner1.getZ();
    int xspawn = new Random().nextInt(xdiff) + 1 + corner1.getX();
    int zspawn = new Random().nextInt(zdiff) + 1 + corner1.getZ();
    Location spawnLoc = new Location(world, xspawn, 161, zspawn);
    Entity entity = world.spawnEntity(spawnLoc, EntityType.SNOWBALL);
    entity.setVelocity(new org.bukkit.util.Vector(xspawn, 160, zspawn));
     
  7. So, I used some of your code and switched it up a bit to make it work.
    Code:
                    Location corner1 = new Location(getServer().getWorld("world"), -398, 161, 352);
                    Location corner2 = new Location(getServer().getWorld("world"), -227, 161, 541);
                    double xdiff = (corner1.getX() - corner2.getX());
                    double zdiff = (corner2.getZ() - corner1.getZ());
                    double xspawn = new Random().nextInt(171) + 1 + corner1.getX();
                    double zspawn = new Random().nextInt(189) + 1 + corner1.getZ();
                    Location spawnLoc = new Location(getServer().getWorld("world"), xspawn, 161, zspawn);
                    Entity entity = getServer().getWorld("world").spawnEntity(spawnLoc, EntityType.SNOWBALL);
                    entity.setVelocity(new Vector(xspawn, 160, zspawn));
    
    Now its working great but, I have encountered a weird glitch. It only works if I am not moving. If I stand still it works fine but as soon as I move the snowballs stop spawning. Any reason why this might happen?

    Bump

    Solved: So I had to change the velocity coords to 0, -1, 0. The snowballs were basically ghost snowballs that the client was seeing.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
Thread Status:
Not open for further replies.

Share This Page