Solved Initializing Snowball Variable

Discussion in 'Plugin Development' started by SmearySubset, Aug 7, 2021.

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

    SmearySubset

    I'm creating a grenade (a snowball) that detonates 4 seconds after it is thrown, just like a frag grenade in popular first person shooters.

    I need to make an Entity/Snowball variable and launch it from a player. I am aware that I can use player.getPlayer().launchProjectile(Snowball.class), but that will not work because I will need to create an object for the grenade to get it's locations 4 seconds later to spawn an explosion at it's current location. I cannot use ProjectileHitEvent either because the grenade could explode before it hits anything.

    The API does not let me instantiate snowball or entity variables, and I don't know how to create one. Anyone know?
     
  2. Offline

    rudraksha007

    in my opinion, using snow-balls is a very hard idea... if u use tnt instead, i will not have to handle any of the timings. u will just have to give it a velocity... but as it is too large... yeah u can do it.
    Use scheduler with "run task later" and 4*20L in time. and in case snow ball lands somewhere, you can save location in a hashmap with ProjectileHitEvent and... u get idea no?
     
  3. Offline

    Tim_M

    I'm sure there is a better way, but you could always put in a Map the player's UUID and the amount of time since their last throw. This will however limit all player's of throwing only one granade at a time.
     
  4. Offline

    SmearySubset

    I understand, but as I mentioned in my prompt I cannot use ProjectileHitEvent because there is a chance that the snowball will explode before hitting something. Also, I know how to do the scheduler piece of it. The issue is getting it's location 4 seconds later. TNT also will not work because I am creating it so that you can hold the grenade before releasing it (don't worry about that part, I'm progressively figuring it out)

    Yeah, I initially had it so that the player could only throw one grenade at a time and I was able to successfully do it another way. Perhaps maybe I could you ProjectileLaunchEvent. Every time the player throws a grenade, add that snowball entity to a hashmap or list and somehow use that to figure out their locations. After all, if a player throws 3 grenades one after the other, I will know that the first grenade is the first value in the list, the second grenade is the second, etc etc. I can play around with it but if you have any other ideas lmk.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 8, 2021
  5. Offline

    davidclue

    @SmearySubset Use ProjectileLaunchEvent and start a timer of 4 seconds, if the snowball hits a surface just save the location instead of getting the snowballs location at the end of the timer and do your explosion.
     
  6. Offline

    SmearySubset

    Solved. For people who want to know:

    Entity grenade = player.getWorld().spawnEntity(player.getEyeLocation(), EntityType.SNOWBALL);


    Solved. For those who want it:

    Entity grenade = player.getWorld().spawnEntity(player.getEyeLocation(), EntityType.SNOWBALL);

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    davidclue likes this.
Thread Status:
Not open for further replies.

Share This Page