Throwable Item

Discussion in 'Plugin Development' started by monkeymanboy, Oct 2, 2013.

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

    monkeymanboy

    How would I go about making an item that does exactly what an enderpearl does (I mean like the way it is sorta thrown and stuff) and then make it do something else when it hits the ground and be a dropped item (e.g. Yellow Dye)
     
  2. What you can do is spawn the item then set the velioty how ever you want it to go. For when it hits the ground idk how you would go around doing it.
     
  3. Offline

    monkeymanboy

    legostarwarszach well the thing is I don't know how to set the velocity so it would go in the sort of way an enderpearl does
     
  4. i didnt understand what you want..but thats how you drop items ...
    the velocity is the .multy....(1) change it :p
    final Item item = p.getWorld().dropItemNaturally(p.getEyeLocation(), new ItemStack(385, 1));
    item.setVelocity(p.getEyeLocation().getDirection().multiply(1));
     
  5. Offline

    Loogeh

    monkeymanboy If by the 'way an enderpearl does' you mean exactly like it then you could probably find some vectors and variables in the Minecraft client source code. Otherwise, if you just mean to launch them into the air then Infinity_Crafter 's way will work and you could just the int inside the multiply(int) method to change the amount of speed that it gets.
     
  6. Offline

    Plo124

    multiply(1) is awfully slow, I would think at least 2.
     
  7. Plo124 2 is too far from me :p
     
  8. Offline

    monkeymanboy

    Ok now what about the ground hitting part? I want it to be so if it collides with anything I can make it do something
     
  9. Offline

    Codex Arcanum

    The easiest way would be to use a repeating task to check if the thrown item's velocity is zero - but that wouldn't happen for a while if the item landed in water or on ice.
     
  10. Offline

    monkeymanboy

    Codex Arcanum Is there a way that would include water and ice?
     
  11. Offline

    Codex Arcanum

    You could check the block under the item every so often, and if it's a solid block, then detonate/activate it.
     
  12. Offline

    monkeymanboy

    Codex Arcanum Not launching item im doing it like this
    might be the way im checking for the damage value
    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void LemonGrenade(PlayerInteractEvent event){
    4. final Player player = event.getPlayer();
    5. Location player_location = player.getLocation();
    6. ItemStack lg = new ItemStack(Material.INK_SACK, 1, (byte) 11);
    7. if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK) || event.getAction().equals(Action.RIGHT_CLICK_AIR))
    8. if(player.getItemInHand().getType().equals(lg)){
    9. double pitch = ((player_location.getPitch() + 90) * Math.PI) / 180;
    10. double yaw = ((player_location.getYaw() + 90) * Math.PI) / 180;
    11. double x = Math.sin(pitch) * Math.cos(yaw);
    12. double y = Math.sin(pitch) * Math.sin(yaw);
    13. double z = Math.cos(pitch);
    14. Vector vector = new Vector(x, z, y);
    15. final Entity item = player.getWorld().dropItemNaturally(player_location, lg);
    16. item.setVelocity(vector.multiply(10));
    17. plugin.getServer().getScheduler().scheduleAsyncRepeatingTask(plugin, new Runnable() {
    18. public void run (){
    19. if(item.getVelocity().equals(0) || item.isOnGround());
    20. item.getWorld().createExplosion(item.getLocation(), 1);
    21. item.remove();
    22. }
    23. }, 3L, 3L);
    24. }
    25. }
     
  13. Offline

    Codex Arcanum

    You might be better off checking the material and damage value like
    Code:
    if(player.getItemInHand().getType() == Material.INK_SAK && player.getItemInHand().getDurability() == 11){
    
    Not 100% sure why you are having a problem - if you're curious, look at the .equals(...) method in ItemStack.java, but the above ought to work.
     
  14. Offline

    monkeymanboy

    Codex Arcanum It works but I get a ton of errors and I think it has to do with the fact that the item just infinitely explodes

    Codex Arcanum Ok I got it kind of working with a few problems
    Code:java
    1. @EventHandler
    2. public void LemonGrenade(PlayerInteractEvent event){
    3. final Player player = event.getPlayer();
    4. Location player_location = player.getLocation();
    5. if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK) || event.getAction().equals(Action.RIGHT_CLICK_AIR))
    6. if(player.getItemInHand() != null || !player.getItemInHand().equals(Material.AIR))
    7. if(player.getItemInHand().getType() == Material.INK_SACK && player.getItemInHand().getDurability() == 11)
    8. if(player.getItemInHand().getItemMeta().hasLore())
    9. if(player.getItemInHand().getItemMeta().getLore().contains("ยง6In Cave Johnsons name")){
    10. final Entity item = player.getWorld().dropItemNaturally(player_location, new ItemStack(Material.INK_SACK, 1, (byte) 11));
    11. item.setVelocity(player.getEyeLocation().getDirection().multiply(1));
    12. if(player.getItemInHand().getAmount() >= 1){
    13. player.getItemInHand().setAmount(player.getItemInHand().getAmount() -1);
    14. } else if(player.getItemInHand().getAmount() == 1){
    15. player.getInventory().removeItem(event.getPlayer().getItemInHand());
    16. }
    17. grenadecheck = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    18. public void run (){
    19. if(item.getVelocity().equals(0.0D) || item.isOnGround());
    20. item.getWorld().createExplosion(item.getLocation(), 1);
    21. item.remove();
    22. plugin.getServer().getScheduler().cancelTask(grenadecheck);
    23. }
    24. }, 3L, 3L);
    25. }
    26. }

    A few of the problems are that sometimes they won't explode and another is that they sometimes won't stop exploding in place

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

    Loogeh

    monkeymanboy One thing I noticed is that on the line just under run() you don't actually wrap that around the other code inside the scheduler. You just end it without doing anything using a ; at the end of the line. Fix that and see what happens. Also, in the line which checks if the lore contains "In Cave Johnsons name" you don't need to check for colours unless you have other items with the same lore in a different colour.
     
  16. Offline

    Goblom

    Code:java
    1. player.launchProjectile(EnderPearl.class);
     
  17. Offline

    monkeymanboy

    Loogeh Wow, didn't even notice I did that anyways that all works now but the problem still remains where there is a chance it will explode forever if it bounces in a certain way or you throw two.
     
Thread Status:
Not open for further replies.

Share This Page