Player Inventory Issue

Discussion in 'Spigot Plugin Development' started by Mariozgr8, Apr 13, 2017.

  1. HI, so I am trying to code a plugin that will replace the diamonds in a player inventory with gold( 10 gold per diamond exchanged) I have written this until now:
    Code (Text):
    package me.mariozgr8.trainingplugin;

    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 org.bukkit.inventory.PlayerInventory;

    import net.md_5.bungee.api.ChatColor;

    public class ExchangeCommand implements CommandExecutor {

        public ExchangeCommand(TutorialPlugins plugin) {
        }
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player p = (Player) sender;
            PlayerInventory inv = p.getInventory();
            ItemStack diamond = new ItemStack(Material.DIAMOND);
            ItemStack gold = new ItemStack(Material.GOLD_INGOT);
            ItemStack goldnumber = new ItemStack(Material.DIAMOND, 1);
            ItemStack dianumber = new ItemStack(Material.GOLD_INGOT,1);
            if(!(p instanceof Player)){
                p.sendMessage("This Command can only be permformed by a player!");}
            if(p instanceof Player){
                if(cmd.getName().equalsIgnoreCase("exchangetestt")){
                    if(!(p.hasPermission("test.exchange"))){
                        p.sendMessage(ChatColor.RED+"You are not allowed to use this command!");}
                    else if(p.hasPermission("test.exchange")){
                        if(args.length<1){
                            p.sendMessage("Not enought arguments!");}
                        if(args.length>1){
                            p.sendMessage("Too Many arguments!");}
                        if(args.length ==1){
                          if(args[0].equalsIgnoreCase("diagold")){
                              if(inv.contains(diamond)){
                                  int numberdia = inv.getContents().getamount
                              }
                          }
            return false;
        }
    }}}}}
    But I don't know how to get the amont of diamonds that are in the player inventory. Can anyone help me with this one ? :)
     
  2. You could get all the inventory content and loop through it all. For each item stack in the inventory check if its Item Type is equal to Diamond. If so, get the amount of diamonds in the item stack and then add it to a Interger called diamondcount. If more diamonds are found add that stacks count to the diamondcount. When it has gone through the players inventory give the player the dimaondcount x 10 in gold.

    Code (Text):
            int diamondcount = 0;
            for (ItemStack i : e.getInventory().getContents()){
                if (i.getType().equals(Material.DIAMOND)){
                    diamondcount += i.getAmount();
                }
    // Now give the player either by command or by ItemStack their gold!
            }
     
    • Like Like x 2
  3. Might as well make one method called "trade(Player p)" or something that just removes diamonds, and gives the gold

    Also for your if checks... Many redundant checks... Just have an else check after "if(args.length == 1)", and then put a message that tells the command sender the usage. Also you can use "return false;" to just stop the code after a short if check. Like your "if(p.hasPermission("bla")" check can just have a return statement to make it much shorter
     
    • Like Like x 1