#Generating items from config

1 messages · Page 1 of 1 (latest)

burnt leaf
#

Ok cool I've been trying to figure this out and my brain is shutting itself down lmao

topaz topaz
#

You may name your threads more usefully in the future

burnt leaf
#

Will do

#

Generating items from config

#

So I learned to create custom items like this:

public class FlawlessDiamond {

    public static ItemStack FlawlessDiamond;

    public static void init() {

        createFlawlessDiamond();

    }

    private static void createFlawlessDiamond() {
        ItemStack item = new ItemStack(Material.DIAMOND, 1);
        ItemMeta meta = item.getItemMeta();
        meta.setDisplayName("§bFlawless Diamond");
        List<String> lore = new ArrayList<>();
        lore.add("A rare form of Diamond!");
        meta.setLore(lore);
        meta.addEnchant(Enchantment.LUCK, 1, false);
        meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
        item.setItemMeta(meta);
        PureDiamond = item;
    }
}


#

(just adding some context, the first message here is one of the answers)

#

I was given this piece of code as well. My config is already set up in away that the "path.to.item.super.section" is there, so I'd have to do something like "blocks.types" there, np there. I am just very confused as to how I am supposed to be using this code, honestly

public class CustomItems {

  private static final Map<String, ItemStack> namedItems = new HashMap<>();

  public static ItemStack getNamedItem(final String name) {
    return namedItems.get(name);
  }

  public static void loadFromConfig(final FileConfiguration configuration) {
    final ConfigurationSection itemSuperSection = configuration.getConfigurationSection("path.to.item.super.section");
    for (final String key : itemSuperSection.getKeys(false)) {
      final ConfigurationSection itemSingleSection = itemSuperSection.getConfigurationSection(key);
      loadSingleItemFromSection(key, itemSingleSection);
    }
  }

  private static void loadSingleItemFromSection(final String key, final ConfigurationSection section) {
    // Load the ItemStack from a section and put it in the Map
  }

}


#

I've no issue just copying the code but I don't understand it well enough to implement it

#

Yes basically, I don't know how to use this....

burnt leaf
#

okay so the thing I don't understand here fully is nameditems.get in "getNamedItem" and "loadSingleItemFromSection", that's where I'm a little confused

#

The "loadFromConfig" does confuse a bit but I think I understand it in the sense that I know what to put in the "path.to.item.super.section" bit

burnt leaf
#

I am honestly more stuck now than I was before, should I just start over? 😅

inland turret
#

What are you stuck on?

burnt leaf
#

Well what I am trying to do here is I have a config with a bunch of info that I want to use to create items

#

So I have a setup like

Items:
  Item1:
    Property1:
    Property2:
  Item2:
    Property1:
    Property2:
inland turret
#

blocks?

burnt leaf
#

Well items, my bad

inland turret
#

ItemStacks?

burnt leaf
#

Indeed

inland turret
#

ok

burnt leaf
#

I want to create itemstacks automatically from my config

#

Regardless of the length, so if I add more stuff to the config, as long as it has the needed properties it will be automatically generated

inland turret
#

ItemStacks are serializable

#

For example, this is a simple example I wrote for saving inventory on exit, and reloading it on join. ```java
/**

  • Save and load player Inventory example.
  • @author ElgarL

*/
public class SaveInventory implements Listener{

private JavaPlugin plugin;

public SaveInventory(JavaPlugin plugin) {

    this.plugin = plugin;
}

@EventHandler
public void onQuit(PlayerQuitEvent event) {

    Player player = event.getPlayer();
    Map<Integer, ItemStack> inventory = new HashMap<>();

    ItemStack item;
    for (int slot = 0; slot < player.getInventory().getSize(); slot++) {
        item = player.getInventory().getItem(slot);
        if (item != null && item.getType() != Material.AIR)
            inventory.put(slot, item);
    }
    plugin.getConfig().set("vaults." + player.getUniqueId(), inventory);
    plugin.saveConfig();
    plugin.reloadConfig(); // reload so the Map is correctly converted to a MemorySection
}

@EventHandler
public void onJoin(PlayerJoinEvent event) {

    Player player = event.getPlayer();
    String uuid = player.getUniqueId().toString();

    if (plugin.getConfig().isConfigurationSection("vaults." + uuid)) {
        MemorySection section = (MemorySection) plugin.getConfig().get("vaults." + uuid);

        player.getInventory().clear();
        for (String key: section.getKeys(false)) {
            player.getInventory().setItem(Integer.valueOf(key), section.getItemStack(key));
        }
    }
}

}```

burnt leaf
#

Okay I think I'm understanding a little better

#

Thank you, I'll see what I can do now

burnt leaf
#

idk man I must be mentally challenged*