[URGENT] Config Files

Discussion in 'Plugin Development' started by ProMCKingz, Aug 19, 2014.

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

    ProMCKingz

    Hey there,
    I have always been terrible with config.yml files, due to the lack of information given to them.
    Anyways, I was wondering how I could make the following configurable, so that you can change the amount of money in the config (Note: This plugin does not currently have the Vault API). By that I mean so that it changes the lore of $__ to the set amount of money in the config, and also the actual balance it takes from the person, once I hook it up with the vault API.
    Code:java
    1. public static Inventory getHelmetInventory() {
    2. Inventory inv = Bukkit.createInventory(null, 36, " Helmet Wardrobe");
    3. //Needs to be a multiple of nine! Else will will not work
    4. {
    5. ItemStack item = new ItemStack(Material.DIAMOND_HELMET, 1);
    6. ItemMeta meta = item.getItemMeta();
    7. meta.setDisplayName(ChatColor.AQUA + "Diamond Helmet");
    8. List<String> lore = new ArrayList<String>();
    9. lore.add("Adds a diamond helmet to your character!");
    10. lore.add(ChatColor.GREEN + "$160");
    11. meta.setLore(lore);
    12. item.setItemMeta(meta);
    13. inv.setItem(1, item);
    14. }

    Many thanks,
    ProMCKingz,
     
  2. Offline

    Totom3

    ProMCKingz You just get the cost in the config :
    Code:java
    1. Double cost = getConfig().getDouble("path.to.cost", [default value in case it doesn't find it (optional)]);

    The rest is pretty easy, you get the meta of the item, add the lore ("$"+cost) and update the meta.

    Also, for the config API, did you check this page : http://wiki.bukkit.org/Configuration_API_Reference
     
    ProMCKingz and Gerov like this.
  3. Offline

    ProMCKingz

    Totom3
    Thanks, I'll reply if I need further assistance
     
  4. Offline

    ProMCKingz

    Totom3
    Hi, I have this
    Code:java
    1. public class SubHelmet implements Listener{
    2.  
    3. static CompassPort plugin;
    4. public SubHelmet(CompassPort instance) {
    5. plugin = instance;
    6. }
    7. public static Inventory getHelmetInventory() {
    8. Inventory inv = Bukkit.createInventory(null, 36, " Helmet Wardrobe");
    9. //Needs to be a multiple of nine! Else will will not work
    10. {
    11. ItemStack item = new ItemStack(Material.DIAMOND_HELMET, 1);
    12. ItemMeta meta = item.getItemMeta();
    13. meta.setDisplayName(ChatColor.AQUA + "Diamond Helmet");
    14. List<String> lore = new ArrayList<String>();
    15. lore.add("Adds a diamond helmet to your character!");
    16. Double Set_DiamondHelmet_Cost = plugin.getConfig().getDouble("Set_DiamondHelmet_Cost");
    17. lore.add(ChatColor.GREEN + "" + Set_DiamondHelmet_Cost);
    18. meta.setLore(lore);
    19. item.setItemMeta(meta);
    20. inv.setItem(1, item);
    21. }

    However the GUI will not open up now.
    This is my Config:
    Code:
     ######################
    #    Wardrobe Plus    #
    #      Config      #
      ####################
     
    #    Helmet Pricing
     
    Set_DiamondHelmet_Cost: 20
    Set_GoldHelmet_Cost:
    Set_IronHelmet_Cost:
    Set_ChainmailHelmet_Cost:
    Set_LeatherHelmet_Cost:
     
    #  Chestplate Pricing
     
    Set_DiamondChestplate_Cost:
    Set_GoldChestplate_Cost:
    Set_IronChestplate_Cost:
    Set_ChainmailChestplate_Cost:
    Set_LeatherChestplate_Cost:
     
    #  Leggings Pricing
     
    Set_DiamondLeggings_Cost:
    Set_GoldLeggings_Cost:
    Set_IronLeggings_Cost:
    Set_ChainmailLeggings_Cost:
    Set_LeatherLeggings_Cost:
     
    #  Boots Pricing
     
    Set_DiamondBoots_Cost:
    Set_GoldBoots_Cost:
    Set_IronBoots_Cost:
    Set_ChainmailBoots_Cost:
    Set_LeatherBoots_Cost:
    
    And this is part of my Main Class:
    Code:java
    1. public class CompassPort extends JavaPlugin {
    2. final FileConfiguration config = this.getConfig();
    3. private static Location targetPoint = null;
    4.  
    5. @Override
    6. public void onEnable() {
    7. CompassPort.getTarget();
    8. String BukkitV = Bukkit.getBukkitVersion();
    9. getLogger().info("Wardrobe is currently running on" + BukkitV);
    10. getLogger().warning("Thanks for supporting this plugin, and keep supporting us, by downloading our new versions!");
    11. getServer().getPluginManager().registerEvents(new InteractListener(), this);
    12. getServer().getPluginManager().registerEvents(new ClickListener(), this);
    13. getCommand("wardrobe").setExecutor(new Commands());
    14. getConfig().options().copyDefaults(true);
    15. saveConfig();
    16. if (Bukkit.getServer().getBukkitVersion() != null){
    17. getLogger().warning("Your plugin is not compatible with our version!");
    18. }
    19. else{
    20. getLogger().info("We have sucessfully made this plugin run on your version!");
    21. }
    22. }
    23.  
    24. @Override
    25. public void onDisable() {
    26. CompassPort.getTarget();
    27. }

    This is the error once opening the GUI,
    [17:42:48 ERROR]: Could not pass event InventoryClickEvent to WardrobePlus v1.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:294) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:501) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:486) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
    at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java
    :1361) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
    at net.minecraft.server.v1_7_R1.PacketPlayInWindowClick.a(SourceFile:32)
    [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
    at net.minecraft.server.v1_7_R1.PacketPlayInWindowClick.handle(SourceFil
    e:10) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
    at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
    at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craf
    tbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:6
    55) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
    at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:2
    50) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:5
    45) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
    :457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
    at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
    17) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
    Caused by: java.lang.NullPointerException
    at me.ProMCKingz.Hope.SubHelmet.getHelmetInventory(SubHelmet.java:31) ~[
    ?:?]
    at me.ProMCKingz.Hope.ClickListener.onClick(ClickListener.java:52) ~[?:?
    ]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.6.0
    _31]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.6.0
    _31]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .6.0_31]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.6.0_31]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:292) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
    ... 13 more
    >[/CODE]
    Thanks,

    Bump

    Please help me!
    I really need this done quick, I have been waiting days. I need this done because if I dont post the file within 2 days they will remove my plugin

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

    xTigerRebornx

    ProMCKingz Few things:
    - Don't use static if you don't know what it is and when you should/shouldn't use it (don't say that you "know when to use it", your code directly contradicts that, you clearly don't)
    - Remove all unneeded usage of static.
    - Post full classes
     
  6. Offline

    Totom3

    ProMCKingz Let me guess... Is this line 31 of SubHelmet.java ?
    Code:java
    1. Double Set_DiamondHelmet_Cost = plugin.getConfig().getDouble("Set_DiamondHelmet_Cost");

    If it is, then the error probably comes from you forgetting to init your class and therefore your "plugin" field is null (calling a method on a null object throws NPE). If it's not line 31 then we would need the whole class, or at least the specified line.

    For the rest, I agree with xTigerRebornx. You don't really need to use static methods & field in this case.
     
  7. Offline

    Yekllurt

    Line 52 is where you check if the player click's the helmet
    Line 31 is the thing it click's or so
    (i can't say much more im not sure if it's true it's what i guess)
     
  8. Offline

    ProMCKingz

    Totom3
    It is line 31 indeed, however what do you mean by "init"?

    @Rockoty Rocoty Anyone?

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

    Totom3

    ProMCKingz I meant that you need to call the constructor at least once (this will give your "plugin" field a value). Or if you really want the static you could create a init(CompassPort) method that would set a value for "plugin".

    Sorry for late reply
     
Thread Status:
Not open for further replies.

Share This Page