Solved Need help for use a String in config

Discussion in 'Spigot Plugin Help' started by Klimex_x, Jul 5, 2018.

Thread Status:
Not open for further replies.
  1. Hello,
    I encountered a problem when I wanted to use a String written in the configuration file of my plugin.
    Indeed, after have recovered a String in my config (which worrespond to the Material that will have the player when he join), I can't use that I recovere for give written item to player.
    When I write the name of the variable in place of Material given to the player, I have the error "The constructor ItemStack(String) is undefined". I understand this error but don't know how resolve it and arrive to the desired result.
    How can I resolve my problem ?
    Thanks.
    Knowing that my class is like that :
    Code (Text):
    package fr.klimex.ffarush.events;

    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;

    import fr.klimex.ffarush.Main;
    import net.minecraft.server.v1_9_R2.Block;
    import net.minecraft.server.v1_9_R2.ItemStack;

    public class PlayerJoin implements Listener {
       
        private Main main = Main.getInstance();
       
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            Player player = e.getPlayer();
            player.setInvulnerable(true);
            String customObjectTP = main.getConfig().getString("Matériau");
           
            ItemStack tpObject = new ItemStack(customObjectTP);
        }
       
    }
     
     
    #1 Klimex_x, Jul 5, 2018
    Last edited: Jul 5, 2018
    • Like Like x 1
  2. The ItemStack needs to have a Material and amount, if your string is the material name, you need to change it to the enum

    Example

    Code (Text):

    ItemStack tpObject = new ItemStack(Material.valueOf(customObjectTP), 1);
     
     
    • Like Like x 1
  3. You imported the wrong ItemStack source use the bukkit one

    replace this

    import net.minecraft.server.v1_9_R2.Block;
    import net.minecraft.server.v1_9_R2.ItemStack;

    use this

    import org.bukkit.inventory.ItemStack;
     
    • Like Like x 1
  4. that too!

    working code will be like:
    Code (Java):

    package fr.klimex.ffarush.events;

    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.event.player.PlayerJoinEvent;
    import fr.klimex.ffarush.Main;

    public class PlayerJoin implements Listener {
     
        private Main main = Main.getInstance();
     
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            Player player = e.getPlayer();
            player.setInvulnerable(true);
            String customObjectTP = main.getConfig().getString("Matériau");
         
            ItemStack tpObject = new ItemStack(Material.valueOf(customObjectTP), 1);
        }
    }
     
     
    #4 mindgamesnl, Jul 5, 2018
    Last edited: Jul 5, 2018
    • Like Like x 1
  5. The solution of @mindgamesnl should work. I do feel like mentioning that you may want to refrain from using special characters in Yaml files. I'm not sure if an é character is a special character or how its generally dealt with in Yaml as a key, but it's something to maybe take into account as it might cause issues. Again, I'm really not sure, but I feel like mentioning it anyway just be sure.

    None the less it's best practice to keep your configuration keys English. Just a tip.
     
  6. Thank you for your fast answer.
    I didn't know the instruction "valueof".
    Regarding the imports, I didn't worried it because that wasn't my problem. Now I can think it.
    My plugin works now.
    Thanks again.
     
    • Like Like x 1
  7. So my problem is solved. Thanks
    (Sorry for my bad English).
     
    #7 Klimex_x, Jul 5, 2018
    Last edited: Jul 5, 2018
    • Like Like x 1
Thread Status:
Not open for further replies.