Remove 1 item on right click

Discussion in 'Plugin Development' started by TheAJ471, Mar 15, 2014.

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

    TheAJ471

    Ok so I am trying to make my own Gun plugin for my server and and get snowballs to act like ammo. So when I right click with lets say Iron Horse Armor it will shoot the snowball but then it will remove all of the snowballs in my inventory, not just one. So when they right click if they use the iron horse armor ans have a snowball in their inventory it will shoot but after that i want it to remove just 1 snowball not all of them.

    Here is my code (I am using GunsAPI btw. http://dev.bukkit.org/bukkit-plugins/gunsapi/)

    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void shoot(PlayerInteractEvent event){
    4. Player p = event.getPlayer();
    5. if(event.getAction().equals(Action.RIGHT_CLICK_AIR)){
    6. if(event.getPlayer().getItemInHand()!= null){
    7. if(event.getPlayer().getItemInHand().getType().equals(Material.IRON_BARDING)){
    8. GunsAPI gun = (GunsAPI) Bukkit.getPluginManager().getPlugin("GunsAPI");
    9. if(p.hasPermission("gun.use")){
    10. if(p.getInventory().contains(Material.SNOW_BALL)){
    11. gun.shootGun(2, event.getPlayer(), event.getPlayer().getLocation().getDirection().multiply(3), BulletType.SNOWBALL, false);
    12. p.getWorld().playSound(p.getLocation(), Sound.ITEM_PICKUP,1, 0);
    13. p.getInventory().remove(Material.SNOW_BALL);
    14. p.getInventory().remove(1);
    15. p.updateInventory();
    16. }
    17. }
    18. }
    19. }
    20. }
    21. }
     
  2. Offline

    TehVoyager

    int x = snowball.getAmount
    remove snowball
    add a snowball itemstack with the amount x-1.
     
  3. Offline

    TheAJ471

    TehVoyager Ok so I have this now

    Code:java
    1. p.getWorld().playSound(p.getLocation(), Sound.ITEM_PICKUP,1, 0);
    2. ItemStack snowball = new ItemStack(Material.SNOW_BALL,64);
    3. int x = snowball.getAmount();
    4. p.getInventory().remove(Material.SNOW_BALL);
    5. p.getInventory().addItem(x-1);
    6. p.updateInventory();


    But I have an error on addItem. It says 'The method addItem(ItemStack...) in the type Inventory is not applicable for the arguments (int)'
     
  4. Offline

    TehVoyager

    TheAJ471
    Do p.getInventory().addItem(new ItemStack(Material.SNOW_BALL), x-1)

    And Also Don't define snowball as a stack of 64, Get The Amount of Snowballs in their inventory

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

    TheAJ471

    TehVoyager So now i have this. But it doesnt seem to work.

    Code:java
    1. ItemStack snowball = new ItemStack(Material.SNOW_BALL);
    2. int x = snowball.getAmount();
    3. p.getInventory().remove(Material.SNOW_BALL);
    4. ItemStack snowball2 = new ItemStack(Material.SNOW_BALL,x-1);
    5. p.getInventory().addItem(snowball2);
    6. p.updateInventory();
     
  6. Offline

    TehVoyager

    TheAJ471
    The problem is you're getting the amount of a new snowball object that you created, Use a for loop to loop through every item in their nventory and if its a snowball make x the amount of snowballs and break the loop.
     
  7. Offline

    TheAJ471

    TehVoyager Could i have an example on how to do this? Or is there a page i could try to look at.
     
  8. Offline

    TehVoyager

    TheAJ471
    Code:java
    1. int x = 0;
    2. for(ItemStack i : p.getInventory().getContents()){
    3. if(i.getType() == Material.SNOW_BALL){
    4. x = i.getAmount();
    5. break;
    6. }
    7. }
     
  9. Offline

    TheAJ471

    TehVoyager Would I use this to replace my code or would I add this in?
     
  10. Offline

    TehVoyager

    replace your first 2 lines shown with this, the ones defining x and snowball. TheAJ471
     
  11. Offline

    TheAJ471

    TehVoyager So now it looks like this. Nothing happens when i shoot. If i have for example 5 snowballs i stil have all 5 snowballs after shooting.

    Code:java
    1. int x = 0;
    2. for(ItemStack i : p.getInventory().getContents()){
    3. if(i.getType() == Material.SNOW_BALL){
    4. x = i.getAmount();
    5. break;
    6. }
    7. }
    8. p.getInventory().remove(Material.SNOW_BALL);
    9. ItemStack snowball2 = new ItemStack(Material.SNOW_BALL,x-1);
    10. p.getInventory().addItem(snowball2);
    11. p.updateInventory();


    Also i get this error in my server log.

     
  12. Offline

    TehVoyager

    Is that all the code in your interact event?
     
  13. Offline

    TheAJ471

    TehVoyager This is now all of it in the event.

    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void shoot(PlayerInteractEvent event){
    4. Player p = event.getPlayer();
    5. if(event.getAction().equals(Action.RIGHT_CLICK_AIR)){
    6. if(event.getPlayer().getItemInHand()!= null){
    7. if(event.getPlayer().getItemInHand().getType().equals(Material.IRON_BARDING)){
    8. GunsAPI gun = (GunsAPI) Bukkit.getPluginManager().getPlugin("GunsAPI");
    9. if(p.hasPermission("gun.use")){
    10. if(p.getInventory().contains(Material.SNOW_BALL)){
    11. gun.shootGun(2, event.getPlayer(), event.getPlayer().getLocation().getDirection().multiply(3), BulletType.SNOWBALL, false);
    12. p.getWorld().playSound(p.getLocation(), Sound.ITEM_PICKUP,1, 0);
    13. int x = 0;
    14. for(ItemStack i : p.getInventory().getContents()){
    15. if(i.getType() == Material.SNOW_BALL){
    16. x = i.getAmount();
    17. break;
    18. }
    19. }
    20. p.getInventory().remove(Material.SNOW_BALL);
    21. ItemStack snowball2 = new ItemStack(Material.SNOW_BALL,x-1);
    22. p.getInventory().addItem(snowball2);
    23. p.updateInventory();
    24. }
    25. }
    26. }
    27. }
    28. }
    29. }
     
  14. Offline

    TehVoyager

    remove updateinventory
     
  15. Offline

    TheAJ471

    TehVoyager I get this error in the server log.

     
  16. Offline

    TehVoyager

    its at line 39 of your gunlistener class
     
  17. Offline

    TheAJ471

    TehVoyager this is line 39.

    Code:java
    1. if(i.getType() == Material.SNOW_BALL){
     
  18. Offline

    TehVoyager

    is getType() depricated? Add Supress Warnings
     
  19. Offline

    TheAJ471

Thread Status:
Not open for further replies.

Share This Page