Solved Needing help with plugin config!

Discussion in 'Plugin Development' started by HiddenAether, Jan 7, 2014.

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

    HiddenAether

    So i am making a kitpvp plugin, and i want to make it so the classes are configurable through a config. Now this will be my first time using configs and what not (configs getting items), so please if you could provide me with step-by-step instructions on setting up the config to making the config stuff in onEnable, and making it so i can get items from the config and give it to players on sign right click.
     
  2. Offline

    ShadowLAX

    HiddenAether Why don't you try to learn all that stuff first, and then try to do this instead of asking people to give it to you. It would better yourself as a coder, so you actually know what you are doing and not just putting in code you don't know what it does, and I'm sure not many people are wanting to spend the next hour of their life writing code for someone. Not trying to be rude or anything, but that's kind of reality. :)
     
  3. Offline

    Failplease

    HiddenAether
    In your onEnable() put these two lines of code:
    Code:
    getConfig().options.copyDefaults(true);
    saveConfig();
    The first line makes it so any missing values in the config are replaced. It also copies the whole config if it is not there.
    The second line saves the config after copying defaults.

    In your onDisable() put this:
    Code:
    reloadConfig();
    This line reloads the config from disk, showing any changes that you have made to the config and making sure your changes are not overridden.

    To add item names to the config, put this line:
    Code:
    getConfig().set("path.to.string", /*itemName*/);
    To check if the item is the desirable item, add these lines:
    Code:
    String item = new ItemStack(Material.valueOf(getConfig().getString("path.to.string"))).toString();
     
    if(item.equals(Material.valueof(/*desirableItem*/))) {
      //do stuff
    }
    This is all the code I'm giving you, try to make use of it.
    To find more about signs, click my avatar and search my posts. I've posted a few things about signs.
     
  4. Offline

    foldagerdk

  5. Offline

    HiddenAether

    Failplease
    By far the most helpful. I don't know why people bother reading these forums if they wont help. So I have a basic understanding of java, I just need a little help is all. Im not sure where all that code you gave me fits in. I got the first few lines (onEnable and onDisable) Just not the "To add item names to the config, put this line:" and the "To check if the item is the desirable item, add these lines:" parts. Thanks for helping me!

    Jeez, basically i have these classes here:
    Main.Java:
    http://pastebin.com/35qmbiQu
    Commands.Java:
    http://pastebin.com/xBbsTJgy
    SignsEvent.Java:
    http://pastebin.com/qRJv85Ws
    And what i want my config to look like:
    http://pastebin.com/3qr6QhFm

    I want it so in the Commands.java I can pull info from the config for setting say, custom boots or pants or chest plate, etc.
    I have done everything in other tutorials, read up on the official bukkit documentations. Some one please help me LOL.

    Here is a example of something like I want:
    Code:java
    1. p.getInventory().setBoots(new ItemStack(Material.(this.getConfig().getString("Boots")));

    Except i know Commands.Java doesn't know where to find the config or what the config even is, know if you have a solution please lay it out as simple as you can. Make it very understandable.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  6. Offline

    fireblast709

    People rather help you to understand what you are doing (like foldagerdk sending you a link to the configuration API documentation), instead of writing code, which you could just copy-paste without actually studying what it does.
     
  7. Offline

    Failplease

    HiddenAether
    In your Commands class put this code:
    Code:
    public Main plugin;
       
    public Commands(Main plugin) {
      this.plugin = plugin;
    }
    Then change the code that you showed me to:
    Code:
    p.getInventory().setBoots(new ItemStack(Material.(plugin.getConfig().getString("Boots")));
     
  8. Offline

    Superckl1

     
  9. Offline

    HiddenAether

    So I am still getting errors, just at "plugin" now, in the line:
    Code:java
    1. p.getInventory().setBoots(new ItemStack(Material.(plugin.getConfig().getString("Boots")));

    Here is my code:
    Code:
    public class Commands
    implements CommandExecutor{
        public Main plugin;
        public Commands(Main plugin) {
              this.plugin = plugin;
            }
     
        String prefix = ChatColor.BLUE + "[" + ChatColor.AQUA + "SSK" + ChatColor.BLUE + "] ";
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
        {
            Player p = (Player)sender;
            if ((args.length == 0) || (args.length > 1)) {
                p.sendMessage(this.prefix + ChatColor.RED + "Invalid arguments!");
            }
            if ((args.length == 1) &&
                    (args[0].equalsIgnoreCase("leather"))) {
                if (p.hasPermission("ssk.kit.leather"))
                {
                    p.getInventory().clear();
                    p.getInventory().setArmorContents(null);
                    p.getInventory().setBoots(new ItemStack(Material.(plugin.getConfig().getString("Boots")));
     
  10. Offline

    Failplease

  11. Offline

    HiddenAether

    "plugin cannot be resolved or is not a field"
     
  12. Offline

    JRL1004

    Failplease Not to bash on how you are helping HiddenAether but perhaps you could explain the hows and whys of what you are doing and it's functions? Just spoonfeeding them is not helping them learn to solve the issue, all spoonfeeding is is providing the solution without any hope for the OP to learn (easily).
     
  13. Offline

    HiddenAether

    I do understand what is happening. He linked the commands class to the main class since the commands class doesnt know where the config is but the main class does know where its at (I think). JRL1004
     
  14. Offline

    Failplease

    HiddenAether
    You have to use valueOf() to convert the string in your config to a material.
    Make your code this:
    Code:
    p.getInventory().setBoots(new ItemStack(Material.valueOf(plugin.getConfig().getString("Boots"))));
    HiddenAether, the reason I told you to add
    Code:
    public Main plugin;
     
    public Commands(Main plugin) {
      this.plugin = plugin;
    }
    is because now you can get methods from your main class using plugin.method, replacing method with the method. So since you can only get the config from the main class, I told you to use plugin.getConfig().

    EDIT: My explanation was ninja'd
     
  15. Offline

    JRL1004

    HiddenAether So, you don't really understand... if you are trying to access a config (FileConfiguration) in a class, the config must be:
    A) Created within the class accessing it
    B) Accessed from another class by being marked as static
    C) Passed to the class when the class is initialized
    or D) Called through use of an instance of a class that has access to the file
    If it is not any of these methods (these are no doubt not all the possible ways as I am sure a few other devs could list some) then the config will not work properly, instead it will throw various exceptions.
     
  16. Offline

    HiddenAether

    JRL1004 Failplease Thank you for explaining to me! I understand it now! Thank everyone very much! Code worked along with some knowledge of how configs and classes work together. :D
     
Thread Status:
Not open for further replies.

Share This Page