Solved get config?

Discussion in 'Plugin Development' started by VictoryShot, Nov 17, 2013.

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

    VictoryShot

    Hello! I was wondering how would I be able to import this so it works in the config:
    Code:java
    1. @EventHandler
    2. public void onKill(PlayerDeathEvent event) {
    3. Player p = event.getEntity();
    4. if ((p.getKiller() instanceof Player))
    5. p.getKiller().giveExp(15);
    6. }
    7.  


    I want people to edit how much exp they get in the config so how would I be able to do this?

    Code:java
    1. p.getKiller().giveExp(getConfig); // Something like this?


    I am a noob when it comes to doing configs....
     
  2. You'll need to load data from the config file. Assuming you're working from the main class:

    int xpToGive = getConfig().getInt("xp:");

    That will load up the value located after the key "xp" in your config file. You can then simply pass that value to the giveExp function.
     
  3. Offline

    chasechocolate

    Code:java
    1. float exp = (float) getConfig().getDouble("exp-to-give");
    2. p.getKiller().setExp(p.getKiller().getExp() + exp);
     
  4. Offline

    Gater12

    VictoryShot It would be like:
    Code:java
    1. p.getKiller().giveExp(getConfig().getInt("path.here"));

    path.here is where you specify which path. For example like in a config.yml:
    Code:
    xp:
      amount: 10
    then the path will be "xp.amount".
     
  5. Offline

    VictoryShot

    Im soo confused.....
     
  6. Offline

    xTrollxDudex

    VictoryShot
    Three different people gave you CODE examples, and it would help to mention what you are confused on so people can help.
     
  7. Offline

    VictoryShot

    Yes I have done all that and put the code for the config in the onEnable but everytime I kill someone I get an error code
     
  8. Offline

    1Rogue


    And that error is...?
     
  9. Offline

    VictoryShot

    Here is the image to the error:

    http://gyazo.com/313364e01368e81ce1f9ed7099627a8b

    My full code,
    Main Class:
    Code:java
    1. package me.mike1665.killbar;
    2. import java.util.logging.Logger;
    3. import me.mike1665.killbar.BukkitListener;
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.plugin.PluginDescriptionFile;
    6. import org.bukkit.plugin.PluginManager;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Main extends JavaPlugin
    10. {
    11. public final Logger logger = Logger.getLogger("Minecraft");
    12. public static Main plugin;
    13. public final BukkitListener bl = new BukkitListener(this);
    14.  
    15. public void onDisable()
    16. {
    17. PluginDescriptionFile pdfFile = getDescription();
    18. this.logger.info(pdfFile.getName() + " Has Been Disabled!");
    19. }
    20.  
    21. public void onEnable()
    22. {
    23. getConfig().options().copyDefaults(true);
    24. saveConfig();
    25. PluginDescriptionFile pdfFile = getDescription();
    26. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Been Enabled!");
    27. PluginManager pm = getServer().getPluginManager();
    28. pm.registerEvents(this.bl, this);
    29. Bukkit.getServer().getPluginManager().registerEvents(new BukkitListener(null), this);
    30. {
    31. }
    32. }
    33. }
    34.  


    The Listener:

    Code:java
    1. package me.mike1665.killbar;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.entity.PlayerDeathEvent;
    8.  
    9. public class BukkitListener
    10. implements Listener
    11. {
    12. public static Main plugin;
    13.  
    14. public BukkitListener(Main instance)
    15. {
    16. plugin = instance;
    17. }
    18. @EventHandler
    19. public void onKill(PlayerDeathEvent event) {
    20. Player p = event.getEntity();
    21. if ((p.getKiller() instanceof Player));
    22. p.getKiller().giveExp(plugin.getConfig().getInt("exp.amount"));
    23. p.sendMessage(ChatColor.GREEN + "You have earned" + plugin.getConfig().getInt("exp.amount" + "exp"));
    24. }
    25. }
     
  10. Offline

    Peary

    VictoryShot cant you just get the int from the config and give that to the player?
    ex:
    player.giveExp(getConfig().getInt("Exp"))
     
  11. Offline

    VictoryShot

    Then what?
     
  12. Offline

    Peary

    VictoryShot isnt that all you need? you just put that in your event that you want to give them the exp.

    O wait sorry i didnt know you were trying to do that on kill....
     
  13. Offline

    AoH_Ruthless

    Peary
    I don't believe that works. experience is stored as a float if I am correct. But I don't really manipulate things with exp so I probably am wrong.
    VictoryShot
    Can you post your config.yml? For future references, you should learn to read and diagnose the stack trace yourself.

    Look for the "Caused by" as this will point to exactly which line in your code is an error. It also posts the class and the type of exception. In your case, you are getting a NullPointerException (NPE) pointing at Line 22 of BukkitListener.
     
  14. Offline

    Peary

    If its float couldnt you just * 1f ?
     
  15. Offline

    AoH_Ruthless

    Peary
    Most likely; but as I said in my edit, I don't work with exp.
     
  16. Offline

    bartboy8

    VictoryShot
    In your main class in your onEnable method you have the listener registered to use the value of null for the plugin variable. That is bad! Change:
    Code:
    Bukkit.getServer().getPluginManager().registerEvents(new BukkitListener(null), this);
    To:
    Code:
    Bukkit.getServer().getPluginManager().registerEvents(new BukkitListener(this), this);
     
    VictoryShot likes this.
  17. Offline

    VictoryShot

    Config:
    Code:
    Exp: 12
    Line 22:
    Code:java
    1. p.getKiller().giveExp(plugin.getConfig().getInt("Exp"));
     
  18. Offline

    AoH_Ruthless

    VictoryShot
    That wasn't what the code said before .. try it now, then.
     
  19. Offline

    VictoryShot


    Thanks man that helped! :DD
     
Thread Status:
Not open for further replies.

Share This Page