Get the data of a config and put in Inventory

Discussion in 'Plugin Development' started by AntonioC94, Mar 24, 2020.

  1. Offline

    AntonioC94

    Hello there!
    I'm making a teleport plugin for a RPG style.
    So, The problem is that Im not really sure about how to get the data in the config and put the data in a custom inventory (The item on an "slot" and the item with the name ).
    Code:
    Location:
      asd:
        World: world
        X: 196.47117474858277
        Y: 110.0
        Z: -66.62703968091523
        Item: minecraft:granite
    
     
    Last edited: Mar 24, 2020
  2. Offline

    FreakyPear5

    You can use Material to get an item from config then set it to whoever's inventory you want.

    Code:
    Material.getMaterial(this.getConfig().getString("Location."+player.getName()+".Item")
     
  3. Offline

    AntonioC94

    Thanks for the reply!

    I will explain a litle bit more...
    I save in the config all the stations with a command

    Code:
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (sender instanceof Player) {
                Player player = (Player) sender;
                if (cmd.getName().equalsIgnoreCase("polvos")) {
                    if (args.length == 1) {
                        if (args[0].equalsIgnoreCase("ver")) {            }
                    }
                    if (args.length == 2) {
                        if (args[0].equalsIgnoreCase("añadir")) {
                            String nombre = args[1];
                            Location localizacion = player.getLocation();
                            ItemStack item = player.getInventory().getItemInMainHand();
                            if (item != null) {
                                main.getConfig().set("Location." + nombre + ".World", localizacion.getWorld().getName());
                                main.getConfig().set("Location." + nombre + ".X",
                                        Double.valueOf(player.getLocation().getX()));
                                main.getConfig().set("Location." + nombre + ".Y",
                                        Double.valueOf(player.getLocation().getY()));
                                main.getConfig().set("Location." + nombre + ".Z",
                                        Double.valueOf(player.getLocation().getZ()));
                                main.getConfig().set("Location." + nombre + ".Item",
                                        item.getType().getKey().toString());
                                main.saveConfig();                   
                                player.sendMessage("Estación " + nombre + " añadida con exito");
                            } else {
                                player.sendMessage("debes tener un item en la mano");
                            }
                        }
                    }
    
                }
            }
            return true;
        }
    
    So how can i add the value "nombre" (name of the station) in the menu class, to get first the item and then put in the inventory?
     
  4. Offline

    KarimAKL

    @AntonioC94 Loop the keys in "Location" using "getConfig()#getConfigurationSection("Location")#getKeys(false)".
     
  5. Offline

    AntonioC94

    @KarimAKL thanks a lot :D , Now the idea in my mind is get all items and then put into a hashmap and then create a new loop to create the inventory with all the items. What do you think about this?
     
  6. Offline

    KarimAKL

    @AntonioC94 It sounds to me like you could add the items to the inventory in the loop.

    I'm not sure what exactly you want to do, so i can't really help any further with that.
     
  7. Offline

    AntonioC94

    @KarimAKL so, when I do a Right-Click in a flower pot (above bedrock) an inventory is created with all the "warps" saved in the config like this

    [​IMG]

    The problem that I have is..... I don't really know, how can I get the data (Item + name) and make a menu and then put in the inventory . So that is my doubt
     
  8. Offline

    KarimAKL

    @AntonioC94 You should create the inventory and load all the items in the onEnable, that way you won't have to do it everytime a flower pot is clicked.

    If we say the config looks like this:
    Code:YAML
    1. Warps:
    2. warp1:
    3. World: world
    4. X: 106.0
    5. Y: 72.0
    6. Z: -286.0
    7. Yaw: 0.0
    8. Pitch: 0.0
    9. Item:
    10. Material: diamond
    11. Amount: 27
    12. Name: "&rName: {warp}"
    13. Lore:
    14. - "&7Click here to warp to"
    15. - "&a{warp}"


    Something like this should work:
    Code:Java
    1. // Create a list of warps
    2. List<Warp> warps = new ArrayList<>();
    3.  
    4. // Get the config
    5. FileConfiguration config = getConfig();
    6.  
    7. // Get the section "Warps" inside the config
    8. ConfigurationSection warpsSection = config.getConfigurationSection("Warps");
    9.  
    10. // Loop the section's keys
    11. for (String warpName : warpsSection.getKeys(false)) {
    12. // Declare the warp location
    13. Location location = null;
    14.  
    15. // Declare the warp's item in the GUI
    16. ItemStack item = null;
    17.  
    18. // Get the warp's section
    19. ConfigurationSection warpSection = warpsSection.getConfigurationSection(warpName);
    20.  
    21. // Location
    22. {
    23. // Get the location, i didn't do any null checks for the sake of shortening the example
    24. World world = Bukkit.getWorld(warpSection.getString("World"));
    25. double x = warpSection.getDouble("X");
    26. double y = warpSection.getDouble("Y");
    27. double z = warpSection.getDouble("Z");
    28. float yaw = (float) warpSection.getDouble("Yaw");
    29. float pitch = (float) warpSection.getDouble("Pitch");
    30.  
    31. // Set the location
    32. location = new Location(world, x, y, z, yaw, pitch);
    33. }
    34.  
    35. // Item
    36. {
    37. // Get the warp section's item section
    38. ConfigurationSection itemSection = warpSection.getConfigurationSection("Item");
    39.  
    40. // Get the values for the ItemStack constructor
    41. Material material = Material.matchMaterial(itemSection.getString("Material"));
    42. int amount = itemSection.getInt("Amount");
    43.  
    44. // Set the item
    45. item = new ItemStack(material, amount);
    46.  
    47. // Get the item's meta
    48. ItemMeta meta = item.getItemMeta();
    49.  
    50. // Replacements
    51. List<SimpleEntry<String, String>> replacements = new ArrayList<>();
    52. replacements.add(new SimpleEntry("{warp}", warpName));
    53.  
    54. // Set the item's name and lore
    55. meta.setDisplayName(custom(itemSection.getString("Name"), replacements));
    56. meta.setLore(custom(itemSection.getStringList("Lore"), replacements));
    57.  
    58. // Set the item's meta
    59. item.setItemMeta(meta);
    60. }
    61.  
    62. // Add the warp to the list of warps
    63. warps.add(new Warp(warpName, location, item));
    64. }
    65.  
    66. // Below are the methods i use for the name and lore in this example
    67.  
    68. // Customize a string by replacing '&' with '§' (Using ChatColor) and adding replacements such as the warp's name
    69. public String custom(String string, List<SimpleEntry<String, String>> replacements) {
    70. String customized = ChatColor.translateAlternateColorCodes('&', string);
    71.  
    72. for (SimpleEntry<String, String> replacement : replacements) {
    73. customized = customized.replace(replacement.getKey(), replacement.getValue());
    74. }
    75.  
    76. return customized;
    77. }
    78.  
    79. // This method just loops a List<String> and uses the "custom(String, ...)" method on all of the strings inside
    80. public List<String> custom(List<String> list, List<SimpleEntry<String, String>> replacements) {
    81. List<String> customized = new ArrayList<>();
    82.  
    83. for (String string : list) customized.add(custom(string, replacements));
    84.  
    85. return customized;
    86. }


    Note: I haven't tested this code before, i've just written it now.
     
  9. Offline

    AntonioC94

    @KarimAKL thanks for your help a lot!

    Your idea and your code looks great, but I don't really understand how works the List<Warp>, I mean.. creating this list Im saving a location of a x variable , so... how works the List<Warp>? Maybe the List that I need to create is "Location"?

    Edit: I did a litle changes and the code work more or less (thanks again), I have a problem when I add all the variables to the List
    Code:
    estaciones.add(new Location(location, item));
    
    I have the error "The constructor Location(Location, ItemStack) is undefined" . I don't really understand what constructor mean if this code is in onEnable
     
    Last edited: Mar 31, 2020 at 1:04 AM
  10. Offline

    KarimAKL

    No, i just used Warp as an example to hold data for warps.
    How it would look:
    Code:Java
    1. public class Warp {
    2.  
    3. private final String name;
    4.  
    5. private Location location;
    6. private ItemStack item;
    7.  
    8. public Warp(String name, Location location, ItemStack item) {
    9. this.name = name;
    10. this.location = location;
    11. this.item = item;
    12. }
    13. }

    When you call "new SomeClass()", you're calling a constructor, in this case you're trying to call the constructor "Location(Location, ItemStack)", that doesn't exist.
     
  11. Offline

    AntonioC94

    @KarimAKL Wow , all right I understand, you are saving all the data in a class and then calling that method.
    It's a new thing for me sorry hehehe
     
  12. Offline

    KarimAKL

    @AntonioC94 No need to be sorry.

    Let me know if you have any problems.
     

Share This Page