Spawning an Item

Discussion in 'Plugin Development' started by Jaaakee224, Oct 6, 2014.

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

    Jaaakee224

    I recently made a method to spawn an item at the location.
    Code:java
    1. public static void speedPowerupSpawn() {
    2. for (Player p : Bukkit.getOnlinePlayers()) {
    3. World world = p.getWorld();
    4.  
    5. World w = Bukkit.getServer().getWorld(plugin.getConfig().getString("Powerup.world"));
    6. double x = plugin.getConfig().getDouble("Powerup.x");
    7. double y = plugin.getConfig().getDouble("Powerup.y");
    8. double z = plugin.getConfig().getDouble("Powerup.z");
    9. Location l = new Location(w, x, y, z);
    10.  
    11. world.dropItem(l, new ItemStack(Material.SUGAR));
    12. Bukkit.broadcastMessage(prefix + ChatColor.YELLOW + " The " + ChatColor.GREEN + "" + ChatColor.WHITE + "SPEED" + ChatColor.YELLOW + " powerup has spawned!");
    13. }
    14. }

    It works, but the item is kind of "thrown" in any direction.
    http://imgur.com/fdDzPeT
    In the picture, the green is where I have the location set. Is there a way for the item to only stay where the location is set?
     

    Attached Files:

  2. Offline

    adam753

    This should work:
    Code:
    Item item = world.dropItem(l, new ItemStack(Material.SUGAR));
    item.setVelocity(new Vector(0, 0, 0));
     
  3. Offline

    Jaaakee224

  4. Offline

    adam753

    Jaaakee224
    Hmm. You could try manually setting its location after that?
    Code:
    item.setLocation(l);
     
  5. Offline

    BillyBobJoe168

    This probably has nothing to do with it going in random directions but you are spawning a powerup for each online player and I'm not sure if you want that. Also, you already have World world = p.getWorld(); I don't think you need world w = Bukkit.get...
     
  6. Offline

    Jaaakee224

    BillyBobJoe168
    Fixed the World thing.

    Current Code:
    Code:java
    1. public static void speedPowerupSpawn() {
    2. for (Player p : Bukkit.getOnlinePlayers()) {
    3.  
    4. World w = Bukkit.getServer().getWorld(plugin.getConfig().getString("Powerup.world"));
    5. double x = plugin.getConfig().getDouble("Powerup.x");
    6. double y = plugin.getConfig().getDouble("Powerup.y");
    7. double z = plugin.getConfig().getDouble("Powerup.z");
    8. final Location l = new Location(w, x, y, z);
    9.  
    10. final Item item = w.dropItem(l, new ItemStack(Material.SUGAR));
    11. item.setVelocity(new Vector(0, 0, 0));
    12. item.teleport(l);
    13. p.sendMessage(prefix + ChatColor.YELLOW + " The " + ChatColor.GREEN + "" + ChatColor.WHITE + "SPEED" + ChatColor.YELLOW + " powerup has spawned!");
    14. }
    15. }
     
  7. Offline

    Jaaakee224

  8. Offline

    97WaterPolo

    Jaaakee224
    Not to sure why it is being moved so far, but try dropItemNaturally() instead of dropItem(). Not sure it will work but worth a shot :p
     
  9. Offline

    Code0

    Actually don't do that. dropItemNaturally does what OP doesn't want the item to do. Jaaakee224

    Edit: Explanation: dropItemNaturally spawns it with a random offset while dropItem should spawn it at a set location. No need to use vectors. Read the docs for more info. It worked perfectly fine for me.
     
  10. Offline

    Jaaakee224

    Code0
    The item is thrown, and after like 1-2 seconds it moves again.
     
  11. Offline

    Code0

    That's a client-side only glitch where it ends up in a different location than where it actually should be. You can't cancel that movement by setting the velocity to straight up 0. It's a teleportation, not a movement.
     
  12. Offline

    Jaaakee224

    Code0
    Is there a way to fix this? Can I just spawn the item riding a wither skull?
     
  13. Offline

    Code0

    Jaaakee224 I recently tested this and it works perfectly fine for me. Also to note, when i said client-side I was implying that this only happens for some people.

    As for the wither skull, I guess it'll work but always be sure to kill the skull after the item hits the ground
    Code:java
    1. item.isOnGround();
     
Thread Status:
Not open for further replies.

Share This Page