ItemStack Problems in Bullets System

Discussion in 'Plugin Development' started by Wazup93, Feb 16, 2014.

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

    Wazup93

    Hello everybody ...
    I have a stupid problem about itemstacks which is in a bullets system ... its goes as following ...
    I add an itemstack for bullets as clay balls BUT its added as a recipe as the bullets MUST have a name and ItemMeta as the itemstack and the recipe .... When you craft it and you get 5 Balls as the recipe gives which is ammounted at the itemstack ... And then you try to remove one bullet only from the inventory .. it delets the whole 2 Or it doesnt delet any ! and the command of shooting a snowball fails !
    Now these are the parts of my code : ( ItemStack & Recipes Code )
    Code:
    //Damn o' Sniper
        public static ItemStack DamnoSniper = new ItemStack(Material.GOLD_HOE);
        //Damn o' Sniper Ammo
        public static ItemStack DamnoSniperAmmo = new ItemStack(Material.CLAY_BALL, 2);
     
    //Damn o' Sniper
            ItemMeta DamnoSniperMeta = DamnoSniper.getItemMeta();
            DamnoSniperMeta.setDisplayName(ChatColor.BLUE + "Damn " + ChatColor.GOLD + "o' " + ChatColor.GREEN + "Sniper");
            DamnoSniper.setItemMeta(DamnoSniperMeta);
            ShapedRecipe DamnoSniperRecipe = new ShapedRecipe(DamnoSniper);
            DamnoSniperRecipe.shape("RRR","DHD","RRR");
            DamnoSniperRecipe.setIngredient('H', Material.GOLD_HOE);
            DamnoSniperRecipe.setIngredient('D', Material.DIAMOND);
            DamnoSniperRecipe.setIngredient('R', Material.REDSTONE);
           
            //Damn o' Sniper Ammo
            ItemMeta DamnoSniperAmmoMeta = DamnoSniperAmmo.getItemMeta();
            DamnoSniperAmmoMeta.setDisplayName(ChatColor.BLUE + "D" + ChatColor.GOLD + "o" + ChatColor.GREEN + "S" + ChatColor.DARK_GRAY + " Ammo");
            DamnoSniperAmmo.setItemMeta(DamnoSniperAmmoMeta);
            ShapedRecipe DamnoSniperAmmoRecipe = new ShapedRecipe(DamnoSniperAmmo);
            DamnoSniperAmmoRecipe.shape("RIR","ICI","RIR");
            DamnoSniperAmmoRecipe.setIngredient('C', Material.CLAY_BALL);
            DamnoSniperAmmoRecipe.setIngredient('R', Material.REDSTONE);
            DamnoSniperAmmoRecipe.setIngredient('I', Material.IRON_INGOT);
    Ok now in the Events .. :
    Code:
    if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
                if(p.getItemInHand().equals(WolverZCraft.DamnoSniper)){
                    if(p.getInventory().contains(WolverZCraft.DamnoSniperAmmo))    {
                        p.launchProjectile(Snowball.class);
                    }
                }   
            }
    And there's a small problem too .. If there are no 2 Balls ONLY in one slot in the players inventory its doesnt shoot too aswell .. And this isnt good for me and for the plugin by the way !
    I wish you guys got it ...
    I'll be thankful for Any help :D !
    DarkWolverZBoss

    Bump

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

    Garris0n

    First of all, you need to fix your variable names and (probably) your naming conventions of everything else. Read this and fix everything.

    Second, I can't really understand what you're saying. Could you try to be a bit clearer?
     
  3. Offline

    Wazup93

    Garris0n
    What i wanted to say that when i try to remove the itemstack itself to avoid removing a normal clay ball its clears all the balls i have Which are 2 and if i have more than 2 balls in one slot or less than 2 .. It doesnt even work and it shoots nothing ... Wish you got it
    DarkWolverZBoss

    bump

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

    xTigerRebornx

  5. Offline

    Wazup93

    xTigerRebornx
    Can you please give me a small example ? And how can i craft while having a material not an itemstack ? ... Ummmmmm really confused ...
     
  6. Offline

    xTigerRebornx

    Wazup93 Okay, so you have this ItemStack as your ammo
    Code:
    public static ItemStack DamnoSniperAmmo = new ItemStack(Material.CLAY_BALL, 2);
    And you are checking for it like this
    Code:
    if(p.getInventory().contains(WolverZCraft.DamnoSniperAmmo)) 
    And the Javadocs say this
    Matching counting a few things (Such as ItemStack size, Material, ItemMeta), and so if there is an ItemStack in your inventory that is the same name and material, but not the same size, then the contains() method won't return true.
    For an example, you'd do something like this:
    Code:
    Inventory i = <The Inventory you are checking for>
    if(i.first(Material.YOUR_MATERIAL) != -1){
      ItemStack is = i.getItem(i.first(Material.YOUR_MATERIAL));
      // Check is for your ammo (Name, Lore, etc)
    }
    
    You could also store i.first(Material.YOUR_MATERIAL) in an int, but its not needed.
     
  7. Offline

    Wazup93

    Ok i gort everything this time but you didnt explain the most needed part .. the checking if "is" is my needed material with its ItemMeta and as you know we cant use itemmeta for checking till its on an item ... Well i cant make that on a material But i can do it for an itemstack gotten from a material ... Lemme check

    Hola :D Fixed xD
    I just made it with a small feature and i donno too xD .. Now it takes 2 Bullets in one shot !!! and this was a thing i Wanted SO FAR ! Whatever :D This is the code for anyone who wants it :p
    Code:java
    1. if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
    2. if(p.getItemInHand().equals(WolverZCraft.DamnoSniper)){
    3.  
    4. Inventory i = p.getInventory();
    5. if(i.first(Material.CLAY_BALL) != -1){
    6. ItemStack is = i.getItem(i.first(Material.CLAY_BALL));
    7.  
    8. if(is.getAmount() <= 64) {
    9. p.launchProjectile(Snowball.class);
    10.  
    11. is.setAmount(is.getAmount() - 1);
    12. ItemStack is2 = i.getItem(i.first(Material.CLAY_BALL));
    13. is2.setAmount(is2.getAmount() - 1);
    14. p.updateInventory();
    15. }
    16. }
    17.  
    18. }
    19. }

    Aswell the itemstacks of the recipe are the same :p
    Thanks for everyone TRIED to help ^^

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
Thread Status:
Not open for further replies.

Share This Page