Solved Updating players inventory

Discussion in 'Plugin Development' started by mike546378, Jul 18, 2014.

Thread Status:
Not open for further replies.
  1. Hello, I am making a paintball plugin for my minigames network and part of it allows the user to purchase a shotgun which fires 9 paintballs (snowballs) at a time. This part is working, it is also supposed to remove 9 paintballs from the players inv every time they fire the gun which also works however the client doesn't see the paintball count decrease until they manually update their inventory by picking/moving the paintballs. The updateInventory() method is depreciated and doesn't seem to work any more, anyone have any tips on a workaround?
    Here is my current code:
    Code:java
    1. @EventHandler
    2. public void shotgun(PlayerInteractEvent ev){
    3. if(ev.getItem() != null && ev.getPlayer().getItemInHand() != null && GameBase.gameStarted)
    4. if(ev.getItem().getType() == Material.IRON_INGOT){
    5. ItemStack[] inv = ev.getPlayer().getInventory().getContents();
    6. for(int i = 0; i < inv.length; i++){
    7. if(inv[i] != null)
    8. if(inv[i].getType().equals(Material.SNOW_BALL)){
    9. if(inv[i].getAmount() >= 9){
    10. inv[i].setAmount(inv[i].getAmount()-9);
    11. Player p = ev.getPlayer();
    12. p.getInventory().setContents(inv);
    13. p.updateInventory();
    14. Location l = p.getLocation();
    15. for(int x = -4; x <= 4; x = x + 4){
    16. for(int y = -4; y <= 4; y = y+4){
    17. l.setYaw(p.getLocation().getYaw()+x);
    18. l.setPitch(p.getLocation().getPitch()+y);
    19. spawnPaintball(l,p,3);
    20. }
    21. }
    22. break;
    23. }
    24. }
    25. }
    26. }
    27. }[/i][/i][/i][/i][/i]
     
  2. Offline

    thomasb454

    player.updateInventory() is what you're looking for. (In reply directly to the title)
     
  3. thomasb454
    "The updateInventory() method is depreciated and doesn't seem to work any more, anyone have any tips on a workaround?"
    I have that method in the code on line 13, not doing anything
     
  4. Offline

    thomasb454


    Being deprecated doesn't mean it doesn't work. I use it, it works fine.
     
  5. Offline

    Necrodoom

    thomasb454 "This method should not be relied upon as it is a temporary work-around for a larger, more complicated issue."
    Its one of the oldest deprecated methods.
     
  6. Offline

    thomasb454

    @Necrodoom
    My point excatly, oldest and no work-around has been implemented to my knowledge. It works fine, so why not use it?
     
  7. thomasb454 I am using it in the code, doesn't work. I know alot of the depreciated methods do still work however in this case it doesn't seem to be working for me
     
  8. Offline

    thomasb454


    Code please.
     
  9. thomasb454 Its in the original post, line 13; p.updateInventory()
     
  10. Offline

    thomasb454

  11. thomasb454 Debug lines for what? All the code that should be getting executed is getting executed, the gun fires and it removes the snowballs from the inventory, just doesn't update client side for some reason.
     
  12. Offline

    will181

    If I've read this right, what I'd do is build an itemstack, and remove that from the inventory, then do the p.updateInventory(). I used that in one of my latest plugins, and it works fine.
     
  13. will181 Would you be able to provide an example of how you would attempt this. What you suggested is similar what I am doing in lines 10 - 13. Prior to those lines its just looping through all slots in the inventory and checking if the player has 9 snowballs. I think the issue is the fact that I am not removing the ItemStack from the players inventory, I am just removing 9 items from the itemstack.
     
  14. Offline

    will181

    Code:java
    1. ItemStack is = new ItemStack(Material.SnowBall);
    2.  
    3. //If don't have 9 snowballs
    4. if(!p.getInventory.containsAtLeast(is, 9) {
    5. return;
    6. }
    7.  
    8. is.setAmount(9);
    9.  
    10. p.getInventory().removeItem(is);
    11. p.updateInventory();


    That's what I had in my plugin
     
  15. Just tested the code
    Seems to be returning false even if the player does have snowballs in the inventory for some reason. Any suggestions?

    Edit:
    Sorted that issue, was because the snowballs had meta data. New issue however; the code is removing the whole ItemStack and not just 9. Current code
    Code:java
    1. ItemStack is = new ItemStack(Material.SNOW_BALL);
    2. ItemStack[] inv = p.getInventory().getContents();
    3. for(int i=0; i < inv.length; i++){
    4. if(inv[i] != null)
    5. if(inv[i].getType().equals(Material.SNOW_BALL)){
    6. is = inv[i];
    7. break;
    8. }
    9. }
    10. if(!p.getInventory().containsAtLeast(is, 9)){
    11. return;
    12. }
    13. is.setAmount(9);
    14. p.getInventory().removeItem(is);
    15. p.updateInventory();
    16. Location l = p.getLocation();
    17. for(int x = -4; x <= 4; x = x + 4){
    18. for(int y = -4; y <= 4; y = y+4){
    19. l.setYaw(p.getLocation().getYaw()+x);
    20. l.setPitch(p.getLocation().getPitch()+y);
    21. spawnPaintball(l,p,3);
    22. }
    23. }[/i][/i][/i]
     
  16. Offline

    RingOfStorms

    The API's methods for removing or finding items is extremely... horrible. You need to remove it yourself by looping through the contents of the inventory, rather than using the removeItem method (which does not look at the stack amount).

    Here is an example of a method that takes in a player, material, and amount and removes the amount of the material specified in the player's inventory.
    Code:java
    1.  
    2. public static void removeItems (Player player, Material material, int amount) {
    3. ItemStack[] items = player.getInventory().getContents();
    4. for(int i=0; i<items.length; i++) {
    5. ItemStack item = items[i];
    6. if(item != null && item.getType() != Material.AIR) {
    7. if(item.getType() == material) {
    8. int n = item.getAmount() - amount;
    9. if(n > 0) {
    10. item.setAmount(n);
    11. break;
    12. }else{
    13. items[i] = new ItemStack(Material.AIR);
    14. amount = -1*n;
    15. if(amount == 0) break;
    16. }
    17. }
    18. }
    19. }
    20. player.getInventory().setContents(items);
    21. player.updateInventory();
    22. }
    23. [/i][/i]
     
  17. Offline

    will181

    Try this
    Code:java
    1. p.getInventory().remove(is);
    instead of
    Code:java
    1. p.getInventory().removeItem(is);
     
  18. will181 That was still removing the whole itemstack
    RingOfStorms Thanks, with a few small tweaks that works perfectly :)
     
Thread Status:
Not open for further replies.

Share This Page