Solved for material from config

Discussion in 'Spigot Plugin Development' started by jcardonne, Jun 6, 2018.

  1. Hello,
    Its possible to do something like this ? (Or similar)
    Code (Java):
    Case <path of item in config> :
    //Do something
    Thanks you
  2. Strahan


    Yes and no. Yes, you can switch on what they are holding. I do not believe using a config lookup in case will work though. You'd be better off doing it another way. Without any idea of what you are doing or what the config looks like though, I can't suggest said method.

    Let's say for assumption's sake you are evaluating what material is in their hand for some shop valuation routine and want to switch by materials in a config to get the value. Let's say the config is like:

    Code (Text):
        price: 20
        price: 50
        price: 5
    Then instead of switching on item and matching each case material, I'd just do:

    Code (Text):
    if (player.getInventory().getItemInMainHand().getType() == Material.AIR) {
      player.sendMessage("Hold an item first, dumbass");
      return true;
    int price = getConfig().getString("items." + p.getInventory().getItemInMainHand().getType().name() + ".value", -1);
    player.sendMessage("Value of " + p.getInventory().getItemInMainHand().getType().name() + " is " + ((value < 0)?"not set":value));
    Of course, as I said this is just me making up a use case without knowing what you are actually trying to do.
    #2 Strahan, Jun 6, 2018
    Last edited: Jun 6, 2018
  3. I try to do Inventory Look for moderator plugin, but the config is like
    Code (YAML):
    : Name of item
    : lore of item
    And i would like get item from config InvSee.item...
    (Or the name)


    or its possible to do something like this ?
    Code (Java):
    if (player.getInventory().getItemInHand().getType() == (main.getConfig().getString("Items.InvSee.Item"))) {
    #3 jcardonne, Jun 6, 2018
    Last edited: Jun 6, 2018
  4. If it is an ItemStack you're storing, (which you should as that persists all data and will be retrieved exactly how it was when it was stored, the material won't store ANY metadata), you can use config#getItemStack.
    • Agree Agree x 1
  5. Don't work i use ItemBuilder...
    But great idea
  6. Strahan


    Well, I have no idea what ItemBuilder is. Do you mean this? If so, then that outputs an ItemStack. Then you set the ItemStack to the config. It's easier as Velariyel said to save the ItemStack itself rather than try to serialize it yourself.

    Code (Text):
    if (player.getInventory().getItemInHand().getType() == (main.getConfig().getString("Items.InvSee.Item")))
    No, that won't work. You can't check a String against a Material. You would have to store the Material and retrieve it with the .get, casting to Material. But again as Velariyel said, if the lore is important this will not do what you want. Plus trying to do it all on one line will prevent you from error handling if the config is not correct.

    If ItemBuilder doesn't give you an ItemStack (which I find hard to believe; if my link above isn't the one you mean can you give us a link to what you mean?) then you can make it work by grabbing the item in the player's hand and checking the type and meta values against your config.
  7. Try with Material#valueOf(arg0) were arg0 is the name of the material:
    Code (Text):

    if (player.getInventory().getItemInHand().getType() == Material.valueOf(main.getConfig().getString("Items.InvSee.Item").toUpperCase())) {
        // Do something
  8. Thanks you !