Solved Get String From Config.

Discussion in 'Plugin Development' started by Brendan22, Jan 17, 2017.

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

    Brendan22

    So i'm making a sell plugin but I want only items the server owner puts into a config file to be sellable. I figured I could easily have my plugin do this by asking this config shown here for the plugin.getConfig().getString("Item1").toUpperCase(). This way I would have the string STONE and i could use that in this code.
    if(inv.getItem(x).getType().equals(Material.(plugin.getConfig().getString("Item1").toUpperCase()))){
    The thing is though. It says "Material cannot be resolved to a variable". Any ideas on how I could get this to work? Thank you!

    # Fist plugin
    # Version 1.0
    Welcome Message: 'Hello'
    Code:
    #####SellItems#####
    Item1: Stone
      Cost: 10
    Item2: Grass
      Cost: 10
    Item3: String
      Cost 10
    
     
  2. Offline

    JanTuck

    Code:java
    1.  
    2. Material.getMaterial(name)
    3.  


    or

    Code:java
    1.  
    2. Material.matchMaterial(name)
    3.  


    Get the material like this.
     
    Last edited: Jan 17, 2017
  3. @Brendan22
    Code:java
    1. if(inv.getItem(x).getType().equals(Material.(plugin.getConfig().getString("Item1").toUpperCase()))){
    If this is the code you're trying to use, there is one thing you must understand, you cannot reference a method/variable the usual way using a String, like you can in some more primitive languages. For example, the compiler will not allow you to do MyClass.myMethodNameInAVariable, even though the "myMethodNameInAVariable" variable might be set to a method that exists for that class, because it's simply impossible to make sure that this works in a consistent way.

    . What you should do in this case is the Material.matchMaterial method, passing in the same String as above.
     
  4. Offline

    DoggyCode™

    I believe a thread similar to this was opened just a few days ago. Loop through the keys in the config section, this would require you to have a parent key. However, you have misinterpreted how the YAML syntax works. You can always use a syntax checker for your appropriate format. If you use the linked tool, you can see it gives the error "(<unknown>): mapping values are not allowed in this context at line 3 column 7". You would want to make yours like this:

    Code:
    #####SellItems#####
    items:
      STONE:
        cost: 10
      GRASS:
        cost: 10
      STRING:
        cost: 10
    However, you must make sure the item name is a valid Material according to the bukkit documentation. In order to loop through the children of the parent key ("items"), use FileConfiguration#getConfigurationSection("items").getKeys(false). This will return a collection with all the item names as String-s. In your case the collection would contain: "STONE", "GRASS", and "STRING". You can easily loop through this collection and proceed:

    Code:
    double price = 0.0;
    for(String key : FileConfiguration#getConfigurationSection("items").getKeys(false)) {
      price = FileConfiguration#getDouble("items." + key + ".cost");
      System.out.println("Price for " + key.toLowerCase() + " is " + price + ".");
    }
     
  5. Offline

    Brendan22

    Thank you for the feedback! Sorry for the delayed response most of my house was inaccessible yesterday (water pipe damage). I will try your suggestions soon and thank you again!
     
  6. Offline

    Brendan22

    @AlvinB @JanTuck @DoggyCode™
    Thank you guys for the help. Your suggestions worked.

    Code:java
    1.  
    2. package b.brendan.sellmanager.commands;
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Material;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandExecutor;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.inventory.Inventory;
    11.  
    12. import b.brendan.sellmanager.SellManager;
    13.  
    14. public class sellhand implements CommandExecutor {
    15. private SellManager instance = SellManager.getSellManager();
    16. private SellManager plugin;
    17. public sellhand(SellManager pl) {plugin = pl;}
    18.  
    19. @SuppressWarnings("deprecation")
    20. @EventHandler
    21. public boolean onCommand(CommandSender sender, Command command, String label, String[] args){
    22.  
    23. Player player = (Player) sender;
    24. Inventory inv = player.getInventory();
    25. int total = 0;
    26. int x = player.getInventory().getHeldItemSlot();
    27.  
    28. //checks the config for a matching item. If it exists then the loop stops and adds the config cost of that item to the total.
    29. if(inv.getItem(x) != null ){
    30. for(int i = 1; i <= plugin.getConfig().getInt("ItemAmount"); i++){
    31. if(inv.getItem(x) != null ){
    32. if(inv.getItem(x).getType().equals(Material.getMaterial(plugin.getConfig().getString("Item" + Integer.toString(i)).toUpperCase()))){
    33. int y = inv.getItem(x).getAmount();
    34. total+=(y * plugin.getConfig().getInt("Cost" + Integer.toString(i)));
    35. inv.clear(player.getInventory().getHeldItemSlot());}
    36. else{player.sendMessage(ChatColor.GREEN + "That item is not sellable");return true;}
    37. }}}
    38.  
    39. //will run if no item is initially in hand
    40. else{player.sendMessage(ChatColor.GREEN + "You need an item in your hand!");return true;}
    41.  
    42. //will run if there is a sellable item in hand
    43. instance.economy.depositPlayer(player.getName(), total);
    44. player.sendMessage(ChatColor.GREEN + "Recieved:" + ChatColor.RED + " $" + total);
    45. total = 0;
    46.  
    47. return true;
    48. }}
    49.  
    50.  
     
    Last edited: Jan 19, 2017
Thread Status:
Not open for further replies.

Share This Page