Infinite Items

Discussion in 'Plugin Development' started by Lactem, Feb 25, 2013.

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

    Lactem

    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
     
  2. Offline

    chasechocolate

    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.
     
  3. Offline

    StyL_TwisT

    Yeah, so basically do this:
    Code:java
    1. @EventHandler
    2. public void onProjectileLaunch(ProjectileLaunchEvent event){
    3. Player player = (Player) sender;
    4. if(player.getItemInHand().equals(Material.SNOWBALL){
    5. player.getInventory().addItemStack(Material.SNOWBALL);
     
  4. Offline

    Cjreek

    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.
     
  5. Offline

    kreashenz

    Cjreek , couldn't you just set up a TaskTimer to check all the players inventories, after 1 tick? :p
     
  6. Offline

    Cjreek

    kreashenz you probably could do that but canceling the event and throwing a snowball yourself would be much easier I think.
     
  7. Offline

    kreashenz

    Cjreek yeah, mine was just a suggestion, I would find the event cancelling easier.
     
  8. Offline

    Lactem

    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?
     
  9. Offline

    Technius

    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)
        {
           
        }
    }
    
     
  10. Offline

    Lactem

    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.
     
    Last edited by a moderator: May 31, 2016
  11. Offline

    AmShaegar

    This piece of code works fine for me.
     
  12. Offline

    stuntguy3000

    Have you registered the event?
     
Thread Status:
Not open for further replies.

Share This Page