Getting itemstack name in config problem

Discussion in 'Plugin Development' started by yewtree8, Oct 14, 2015.

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

    yewtree8

    Ok, so the point of this method here is to iterate through an inventory, and for each item iterated, check the value in the config and add it up until the end, then give the player that value.

    my Iteration class
    Code:
    package uk.co.fordevelopment.commerce.empire.utils;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    
    
    
    public class Iterat0r {
    public static double total;
    public static double total2;
    public static List<ItemStack> iContents0 = new ArrayList<ItemStack>();
    
        public static void iterateItems(Inventory iv, String id, Player p)
        {
            total2=0;
            for(ItemStack i : iv.getContents()) //for all itemstacks in the player's inventory
            {
               
                   
                   
                    double sellAmount = calcSellPrice(id,i); //get sell value
                    total2=sellAmount+total2; //add sell value to total.
                   
               
               
               
            }
            EUtils.give(total2, p);
        }
    
        private static double calcSellPrice(String id, ItemStack i) {
            total =0;
          
               
                String itemname = i.toString().toUpperCase();
                double iprice = chooseIteratedItemPrice(itemname, id);
                total = iprice+total;
            
            return total;
        }
    
           
        public static double chooseIteratedItemPrice(String name, String id)
        {
            switch(name)
            {
            case("COBBLESTONE"):
                return Cfg.getCobblePrice(id);
            case("STONE"):
                return Cfg.getStonePrice(id);
            case("IRON_INGOT"):
                return Cfg.getIronIngotPrice(id);
            case("COAL_BLOCK"):
                return Cfg.getCoalPrice(id);
            case("COAL"):
                return Cfg.getCoalPrice(id);
            case("IRON_BLOCK"):
                return Cfg.getIronBlockPrice(id);
            case("LOG"):
                return Cfg.getLogPrice(id);
            case("WOOL"):
                return Cfg.getWoolPrice(id);
            case("LAPIS_BLOCK"):
                return Cfg.getLapisBlockPrice(id);
            case("SANDSTONE"):
                return Cfg.getSandStonePrice(id);
            case("HARD_CLAY"):
                return Cfg.getHardClayPrice(id);
            case("QUARTZ_BLOCK"):
                return Cfg.getQuartzBlockPrice(id);
            case("GOLD_INGOT"):
                return Cfg.getGoldIngotPrice(id);
            case("GOLD_BLOCK"):
                return Cfg.getGoldBlockPrice(id);
            case("REDSTONE"):
                return Cfg.getRedstonePrice(id);
            case("REDSTONE_BLOCK"):
                return Cfg.getRedstoneBlockPrice(id);
            case("BRICK"):
                return Cfg.getBrickPrice(id);
            case("END_STONE"):
                return Cfg.getEndStonePrice(id);
            case("CHISELED_STONE_BRICK"):
                return Cfg.getChisledStonePrice(id);
            case("NETHERRACK"):
                return Cfg.getNetherrackPrice(id);
            case("NETHER_BRICK"):
                return Cfg.getNetherBrickPrice(id);
            case("GLOWSTONE"):
                return Cfg.getGlowStonePrice(id);
            case("EMERALD"):
                return Cfg.getEmeraldPrice(id);
            case("EMERALD_BLOCK"):
                return Cfg.getEmeraldBlockPrice(id);
            case("DIAMOND"):
                return Cfg.getDiamondPrice(id);
            case("DIAMOND_BLOCK"):
                return Cfg.getDiamondBlockPrice(id);
           
            }
           
            return 0;
        }
       
       
    }


    I'm trying to retrieve the id by converting the itemstack to string then capitalizing it, is this the right way? I'm not even sure if this is the problem, as a nullpointer exception is being created when the method it trying to be executed, however I'm not sure if this is even a problem? :|

    Help? D:
     
  2. Offline

    Zombie_Striker

    OH MY GOD THE SPACING.

    Itemstack stores
    1. Material
    2. Duribility
    3. enchantmens
    4. DisplayName
    5. Lore
    6. AND SO MUCH MORE.
    So by converting it into a string, you are getting that information getting turned into one line. You might want to use .getType().toString to get the itemstack's type.
     
  3. Offline

    yewtree8

    Doesn't Work :| This is really starting to bug me, am I doing this correctly?

    Here's my ShopSellButtonClick class:
    Code:
    package uk.co.fordevelopment.commerce.empire.listeners.inventory;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.ItemStack;
    
    import uk.co.fordevelopment.commerce.empire.utils.CI;
    import uk.co.fordevelopment.commerce.empire.utils.Iterat0r;
    
    public class SellEvent implements Listener {
    
        @EventHandler
        public void stopSellButtonClick(InventoryClickEvent e)
        {
            if(e.getCurrentItem() == null) return;
            if(e.getCurrentItem().getItemMeta() == null) return;
            Player p = (Player) e.getWhoClicked();
            if(e.getCurrentItem().equals(new ItemStack(CI.sellButtonIcon)))
            {
                Iterat0r.iterateItems(e.getInventory(), e.getInventory().getName(), p);
                System.out.println("Supposedly Iterated Items");
                e.getInventory().clear();
                p.updateInventory();
                p.closeInventory();
                Iterat0r.iContents0.clear();
            } else {
                return;
            }
        }
       
       
    }


    and the stack trace that was created:
    [​IMG]
     
  4. Offline

    ShadowRanger

    In your calcSellPrice method, change the itemname variable from i.toString().toUpperCase(); to i.getType().toString(), just like @Zombie_Striker suggested. It might be worth noting that it would probably be easier to just pass the item's type to the calcSellPrice, and run the switch with Material enums instead. Either way should work fine. Hope this helped.
     
  5. Offline

    asaffisher.dev

    Can you please find the line of the crash...
    Try debugging/ comment stuff until it will work then locate the line that throws you exception...
     
  6. Offline

    Zombie_Striker

Thread Status:
Not open for further replies.

Share This Page