Solved How to get Material name?!

Discussion in 'Plugin Development' started by CactusComboPvP, Aug 25, 2015.

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

    CactusComboPvP

    So I made a gold based economy where you can sell things on the market and they are removed from your inventory. However, if you rename the item and then try it, you keep the item and it is on the market. So, I tried adding a check to stop that.
    How can you check if a material has a custom name?

    Code:
    Material material = Material.matchMaterial(args[1].toUpperCase());
    How would I check if this has a custom display name or not?
     
  2. Offline

    SuperSniper

    @CactusComboPvP hasDisplayName()

    You can do this with ItemStacks, not sure about Materials.
     
  3. Offline

    CactusComboPvP

    @SuperSniper Yes I know but my whole plugin is Material based so I can't change. I tried:

    ItemStack matCheck = new ItemStack(material);
    if(matCheck.getItemMeta().hasDisplayName()) {
    player.sendMessage(Main.prefix + ChatColor.GRAY + "You cannot sell renamed items.");
    }
    but no success. Any help?
     
  4. Offline

    Zombie_Striker

    This sounds light you are checking for if Itemstacks are the same, not if it's the same type of Material. Can you show us the rest of your code

    Also use the [ code][/code]brackets when posting code.
     
  5. Offline

    CactusComboPvP

    @Zombie_Striker

    Code:
    package chargedpvp.hardcore.economy.commands;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    
    import chargedpvp.hardcore.economy.ItemUtils;
    import chargedpvp.hardcore.economy.Main;
    import chargedpvp.hardcore.economy.SaleUtils;
    
    public class SellCommand
    implements CommandExecutor
    {
    @SuppressWarnings("deprecation")
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    {
      if ((sender instanceof Player))
      {
        Player player = (Player)sender;
        if (args.length == 3) {
          try
          {
            int amount = Integer.parseInt(args[0]);
            if (amount <= 0)
            {
              player.sendMessage(Main.prefix + ChatColor.GRAY + "You must use positive integers to sell items.");
             
              return true;
            }
            Material material = Material.matchMaterial(args[1].toUpperCase());
            if (material == null)
            {
              player.sendMessage(Main.prefix + ChatColor.GRAY + "Item '" + args[1] + "' not found.");
             
              return true;
            }
            double price = Double.parseDouble(args[2]);
           
            int count = 0;
            for (ItemStack is : player.getInventory().getContents()) {
              if ((is != null) && (is.getType() == material) && (is.getData().getData() == 0)) {
                count += is.getAmount();
              }
            }
            if (amount > count)
            {
              player.sendMessage(Main.prefix + ChatColor.GRAY + "You only have " + count + " " + ItemUtils.toFriendlyName(material) + ".");
             
              return true;
            }
            double unitPrice = price / amount;
            for (int i = 0; i < amount; i++) {
              SaleUtils.constructSale(player, material, unitPrice);
            }
            ItemStack itemstack = new ItemStack(Material.getMaterial(args[1].toUpperCase()));
            if(itemstack.getItemMeta().hasDisplayName()) {
                player.sendMessage(Main.prefix + ChatColor.GRAY + "You cannot sell renamed items.");
                return true;
            } else {
                        player.getInventory().removeItem(new ItemStack[] { new ItemStack(material, amount) }); 
            player.sendMessage(Main.prefix + ChatColor.GRAY + "You have put " + amount + " " + ItemUtils.toFriendlyName(material) + " on the market for " + price + " gold.");
           
            return true;
          }
          }
          catch (NumberFormatException ex)
          {
            player.sendMessage(Main.prefix + ChatColor.GRAY + "/sell <amount> <item> <price>");
           
            return true;
          }
        }
        player.sendMessage(Main.prefix + ChatColor.GRAY + "/sell <amount> <item> <price>");
       
        return true;
      }
      return true;
    }
    }
    
    the check is here:

    Code:
    ItemStack itemstack = new ItemStack(Material.getMaterial(args[1].toUpperCase()));
            if(itemstack.getItemMeta().hasDisplayName()) {
                player.sendMessage(Main.prefix + ChatColor.GRAY + "You cannot sell renamed items.");
                return true;
            } else {
                        player.getInventory().removeItem(new ItemStack[] { new ItemStack(material, amount) }); 
            player.sendMessage(Main.prefix + ChatColor.GRAY + "You have put " + amount + " " +
    
    but it doesn't work.
     
  6. Offline

    au2001

    @CactusComboPvP You probably want to replace spaces by underscores too.

    For the display name, you create a blank item of a certain type, it will never have a display name. Check for the player's item in hand.
    Also, if the material is air your code will throw a NPE: air doesn't have ItemMeta.

    Inventory#remove(ItemStack) also exists, it doesn't have to be an array.
     
  7. Offline

    CactusComboPvP

    @au2001 What do you mean? Sorry I just woke up i'm a bit rusty

    Never mind guys, I fixed it thanks to all your help :)
    End code:

    Code:
      ItemStack itemstack = new ItemStack(player.getItemInHand());
            if(itemstack.getItemMeta().hasDisplayName()) {
                player.sendMessage(Main.prefix + ChatColor.GRAY + "You cannot sell renamed items.");
                return true;
            } else {
            player.getInventory().removeItem(new ItemStack[] { new ItemStack(material, amount) });    
            player.sendMessage(Main.prefix + ChatColor.GRAY + "You have put " + amount + " " + ItemUtils.toFriendlyName(material) + " on the market for " + price + " gold.");
           
            return true;
          }
          }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  8. Offline

    au2001

    @CactusComboPvP player.getInventory().removeItem(new ItemStack[] { new ItemStack(material, amount) });
    Here, you put one itemstack in an array. Don't, just pass the itemstack as argument.
     
Thread Status:
Not open for further replies.

Share This Page