Upgrading Item's Help?

Discussion in 'Spigot Plugin Development' started by Markoz, Jun 29, 2015.

  1. Hello guys i am trying to make a upgrading system for my new plugin .. I made this code but it didn't work with me :( , can anyone help me ? because i don't see any thing wrong with it

    Code (Text):
     if (commandLabel.equalsIgnoreCase("upgrade")) {
              if(p.getItemInHand() == null){
                  return true;
              }
              for (String s : upgrades.getConfig().getConfigurationSection("Upgrades." + p.getItemInHand().getType().name()).getKeys(true)) {
              if(upgrades.getConfig().contains("Upgrades." + p.getItemInHand().getType().name() + "." + s)) {
                  for (Enchantment enchant : p.getItemInHand().getEnchantments().keySet()){
                      if(!upgrades.getConfig().contains("Upgrades."+ p.getItemInHand().getType().name() + "." + s)){
                          sendMessage(p, "&cyou can't enchant this item");
                          return true;
                  }
                  double cost = upgrades.getConfig().getInt("Upgrades." + p.getItemInHand().getType().name() + "." + s + ".cost") +upgrades.getConfig().getInt("Upgrades." + p.getItemInHand().getType().name() + "." + s + ".increment") * (1 - 1);
                  int level = p.getItemInHand().getEnchantmentLevel(enchant) + 1;
                  if(getCoins(p) <= cost){
                      sendMessage(p, "&cyou need more coins");
                      return true;
                  }
                if(p.getItemInHand().getEnchantmentLevel(enchant) <= upgrades.getConfig().getInt("Upgrades." + p.getItemInHand().toString() + "." + s + ".max-enchant")){
                    sendMessage(p, "&cYour item has access the enchant limit !");
                    return true;
                }
                p.getItemInHand().addUnsafeEnchantment(Enchantment.getByName(upgrades.getConfig().getString("Upgrades." + p.getItemInHand().getType().name() + "." + s)), level);
                removeCoins(p, cost);
              }
              }
              }
              p.updateInventory();
          }
     
  2. What specifically doesn't work and what is it supposed to do?
     
  3. Did you get any error?

    If you do, please provide us error messages.
     
  4. I am not on my laptop now , I will send it asap
     
  5. First off, you should try using
    Code (Text):
    this.getCommand("commandName").setExecutor(new CommandExecutor());
    instead of checking the commandLabel, second you can simplify your code by changing
    Code (Text):
    p.getItemInHand();
    to something set at the top like
    Code (Text):
    ItemStack handItem = p.getItemInHand();
    and then using handItem when you need to get the item. thirdly
    Code (Text):
    if(getCoins(p) <= cost){
                      sendMessage(p, "&cyou need more coins");
                      return true;
                  }
    should be
    Code (Text):
    if(getCoins(p) < cost){
                      sendMessage(p, "&cyou need more coins");
                      return true;
                  }
    because if you are checking for <= then you are telling the plugin that if lets say it costs 50 coins, and you have 50 coins, the plugin would output that the player doesn't have enough money because it is saying if they have EQUAL to or LESS THAN the amount (50 coins) they can't buy it.
    and after all of my words xD (sorry it was so long) make sure when you are posting these, to include any console errors you may have and to explain what the plugin is doing like people have said above, it really helps you and the people that are willing to help you fix your problem :)
     
  6. i don't think that the problem from the that :| .. but i will try it
     
    • Like Like x 1
  7. FIXED IT ! , I just replace the enchantment name with id .. also i changed some staff with the code

    final result

    Code (Text):
    if (commandLabel.equalsIgnoreCase("upgrade")) {
              for (String s : upgrades.getConfig().getConfigurationSection("Upgrades." + p.getItemInHand().getType().toString()).getKeys(true)) {
              ItemStack iteminhand = p.getItemInHand();
              Double cost = (double) (upgrades.getConfig().getInt("Upgrades." + iteminhand.getType().toString() + "." + s + ".cost") + upgrades.getConfig().getInt("Upgrades." + iteminhand.getType().toString() + "." + s + ".increment") * (//YOR LEVEL - 1));
              if(iteminhand.getType() == Material.AIR){
                  return true;
              }
              if(getCoins(p) < cost){
                  sendMessage(p, "&cyou need more coins");
                  return true;
              }
                Iterator<?> Iterator2 = upgrades.getConfig().getConfigurationSection("Upgrades." + iteminhand.getType().toString()).getKeys(true).iterator();
                      while (Iterator2.hasNext())
                      {
                        String enchs = (String)Iterator2.next();
                        if (!enchs.contains("."))
                        {
                         //TODO Check if the enchantment has enter the max limit
                          Enchantment ench = Enchantment.getById(Integer.parseInt(enchs));
                          iteminhand.addUnsafeEnchantment(ench, (//GET YOU ENCHANTMENT LEVEL) + 1);
                          removeCoins(p, cost);
                        }
                      }
              p.updateInventory();
          }
          }
    Config:
    Code (Text):

    Upgrades:
      DIAMOND_SWORD:
        '19':
          max-enchant: 20
          cost: 3
          increment: 5