Solved Obtain the location of a block hit by a projectile and change the material of the projectile.

Discussion in 'Spigot Plugin Development' started by TheUncleBartoDev, Mar 26, 2020 at 3:06 AM.

  1. Hello !, I was needing help with 2 questions and that is why I have gone to this medium in search of answers and help.
    1. Is it possible to change the item of a projectile? That is, I would like to be able to throw cobwebs, as if they were snowballs, only with cobwebs.
    2. If it is true that in more recent versions of bukkit, there is an Event "ProjectileHitEvent" if I am not mistaken, which allows you to obtain the block that the projectile has hit, I would like to know if that is possible in 1.8, since if well in that version the event exists, there is no method that returns the block hit by a projectile.
    I hope I have expressed myself well And thanks in advance :D.
  2. You should only bump once ever 24 hours.

    you can spawn in an item to use as a projectile but you’d have to listen to multiple events to treat it as one. ProjectileHitEvent doesn’t return a block because it projectiles can hit entities too. You have to check the velocity and location to get a block.

  3. ProjectileHitEvent does return a block if a block was hit and also returns an entity if one was hit.
  4. Let's go slowly, first, how do I generate an item to use as a projectile?
  5. You will not be able to just "use a different item" for your projectile.
    Projectile is an interface and can only be implemented, not instantiated on itself.
    Since it has no way of "setting" a display item, you would only be able to change the item if one of its implementing classes would have such a method ... which they do not (speaking about Snowball, Arrow, Fireball, Egg and so on).

    Since Projectile is an Entity, you can create a custom Entity (an Item could work), on which you -can- change the Item / Apperance and make it behave like a regular projectile, by setting it's velocity for example.

    While this is a little bit hacky and it would not fire events, with enough effort put into it, you can create a very similar effect as a projectile.
    See this example I made a while ago:

    In that video you'll see that you can quite comfortably control velocities of Entities and make them (in this case FallingBlocks) fly like a projectile and collide with things as well.
    All it takes is a few well written lines of code :)
  6. Does what you mention seem quite functional, could you give an example? I don't ask for a code
  7. Choco


    Slightly off topic, I apologize, but that's very creatively done. Looks great (aside from some z-fighting between items when re-calculating the ring positions). If it's open sourced I'd be interested in taking a look for the sake of curiosity.
  8. I think the ring entities are done using armor stands.
  9. They are not. It's your generic Item entity (hence why they are floating up and down a little bit) with some velocity calculations applied to them.

    At the time I took the video, I removed the item from the ring items (which lead to the ring recalculation), but didn't delete it right away, but instead deleted it when the block arrived at the target. It's already "fixed" and doesn't look that glitchy anymore ;)

    It was more a small experiment conducted for possible future use cases, but if you are really interested I can tidy up the code a little bit, add one or two comments and make the git repo public:)
  10. Well, I have fixed it, using Item item = world.dropItem () I just modified the speed to make it look like a projectile, and I did a check Location location = new Location (itemx, itemY-1, itemz) if (! world.getBlockAt (location (). getType == Material.AIR) that way I check if it has a block underneath, that is to say if it has already fallen, then Location blockLocation = item.getLocation ()

    I am in favor :)
    #12 TheUncleBartoDev, Mar 27, 2020 at 5:45 AM
    Last edited by a moderator: Mar 27, 2020 at 8:19 PM
  11. Code (Java):
    // To check if a block is empty / air

    // To check if an entity / item touches the ground
  12. Thanks!
    I did not know, I did not know it!