Hi guys. I have a snowball - ItemStack snowball = new ItemStack(Material.SNOW_BALL); that I give to someone with player.getInventory().addItem(snowball);, but I want to make the snowball I give them never actually disappear from their inventory, no matter how many times they throw it. They can keep throwing the one snowball and it never runs out - that's the goal. Thanks, Lactem
Give it to a player, when they interact (PlayerInteractEvent), cancel the event, and player.launchProjectile(Snowball.class). However you could also check for PlayerInteractEvent or ProjectileLaunchEvent and re-add the snowball to the player's inventory.
Yeah, so basically do this: Code:java @EventHandlerpublic void onProjectileLaunch(ProjectileLaunchEvent event){Player player = (Player) sender;if(player.getItemInHand().equals(Material.SNOWBALL){player.getInventory().addItemStack(Material.SNOWBALL);
I think canceling the event would be better. I did this (re-adding the item) once and it didn't always work. sometimes if you throw too many snowballs in a short time you run out of snowballs.
kreashenz you probably could do that but canceling the event and throwing a snowball yourself would be much easier I think.
Your idea should work. I've tried things like this, but none of them work. I'm not quite sure why. Does this need to be in it's own class or something? Edit: Now there's only one problem: no one is sending a command. Eclipse doesn't like sender and I think it's because no one sent a command. Edit Edit: Now giving infinite snowballs is working, but I also want infinite enderpearls. I used the same code that worked for enderpearls, but I changed SNOW_BALL to ENDER_PEARL. Now it doesn't like the fact that I have two publicvoid onProjectileLaunch(ProjectileLaunchEvent event)'s. It doesn't like me having duplicate "sender"'s and duplicate "ProjectileLaunchEvent"'s. What do I do?
Don't make two methods. Put them in the same method! It is faster than having two methods listening to an event. Code: public void myMethod() { if(conditionOne) { } else if(conditionTwo) { } }
Thanks. I'll try it. Edit: Okay, but then what about my "onProjectileLaunch(ProjectileLaunchEvent event?" If you remember, I had the public void onProjectileLaunch(ProjectileLaunchEvent event. None of this works, but I'll show you what I've come up with. Code: package chartx; import org.bukkit.Material; import org.bukkit.inventory.ItemStack; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.entity.ProjectileLaunchEvent; public class infinitesnowballs { ItemStack snowball = new ItemStack(Material.SNOW_BALL); @EventHandler public void onProjectileLaunch(ProjectileLaunchEvent event) { if(event.getEntity().getShooter() instanceof Player) { Player player = (Player) event.getEntity().getShooter(); player.getInventory().addItem(snowball); } } } EDIT by Moderator: merged posts, please use the edit button instead of double posting.