Plugin API NullPointerException

Discussion in 'Plugin Development' started by exload, Oct 2, 2012.

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

    exload

    Now I may just be derping here but I cannot seem to figure this one out. This is the error that I am getting: http://pastebin.com/ezkiH8ey

    In my class containing the onEnable method I set up the constustor like so:
    Code:java
    1. WorldmotdAPI worldMOTD = new WorldmotdAPI(this);


    Code for the class WorldmotdAPI (Note: If you guys see anything that can be improved please to comment on it and criticize it. Thanks :))
    Code:java
    1.  
    2. package com.mcacraft.worldmotd;
    3.  
    4. import java.util.ArrayList;
    5. import java.util.List;
    6. import net.milkbowl.vault.economy.Economy;
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.plugin.RegisteredServiceProvider;
    11.  
    12. /**
    13.  *
    14.  * @author kennywilliams
    15.  */
    16. class WorldmotdAPI {
    17. public static Economy economy = null;
    18. WorldMOTD plugin;
    19.  
    20. public WorldmotdAPI(WorldMOTD instance){
    21. plugin = instance;
    22. }
    23.  
    24. public WorldmotdAPI(){}
    25.  
    26.  
    27. private boolean setupEconomy()
    28. {
    29. RegisteredServiceProvider<Economy> economyProvider = Bukkit.getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
    30. if (economyProvider != null) {
    31. economy = economyProvider.getProvider();
    32. }
    33. return (economy != null);
    34. }
    35.  
    36. public double getBalance(String player)
    37. {
    38. setupEconomy();
    39. double bal = economy.getBalance(player);
    40. return bal;
    41. }
    42.  
    43. public String getPlayerList()
    44. {
    45. String output = "";
    46. boolean first = true;
    47. for(Player p : Bukkit.getServer().getOnlinePlayers())
    48. {
    49. if(!first)
    50. {
    51. output += ", ";
    52. }else
    53. {
    54. first = false;
    55. }
    56. output += p.getDisplayName();
    57. }
    58. return output;
    59. }
    60.  
    61. public ArrayList<String> getWorldMotd(Player player, String world)
    62. {
    63. ArrayList<String> motd = new ArrayList<String>();
    64. List<String> configMotd = plugin.getConfig().getStringList(world+".motd");
    65. for(String s : configMotd)
    66. {
    67. if(s.contains("%playerlist%"))
    68. {
    69. s.replaceAll("%playerlist%", getPlayerList());
    70. }
    71. if(s.contains("%player%"))
    72. {
    73. s.replaceAll("%player%", player.getDisplayName());
    74. }
    75. if(s.contains("%balance%"))
    76. {
    77. if(Bukkit.getServer().getPluginManager().isPluginEnabled("Vault"))
    78. {
    79. getBalance(player.getName());
    80. }
    81. }
    82. motd.add(s);
    83. }
    84. return motd;
    85. }
    86.  
    87. public ArrayList<String> getLoginMotd(Player player, String world)
    88. {
    89. ArrayList<String> motd = new ArrayList<String>();
    90. List<String> configMotd = plugin.getConfig().getStringList(world+".on-player-login.motd");
    91. for(String s : configMotd)
    92. {
    93. if(s.contains("%playerlist%"))
    94. {
    95. s.replaceAll("%playerlist%", getPlayerList());
    96. }
    97. if(s.contains("%player%"))
    98. {
    99. s.replaceAll("%player%", player.getDisplayName());
    100. }
    101. if(s.contains("%balance%"))
    102. {
    103. if(Bukkit.getServer().getPluginManager().isPluginEnabled("Vault"))
    104. {
    105. getBalance(player.getName());
    106. }
    107. }
    108. motd.add(s);
    109. }
    110. return motd;
    111. }
    112.  
    113. }
    114.  


    How it is being used:
    Code:java
    1.  
    2. package com.mcacraft.worldmotd;
    3.  
    4. import java.util.ArrayList;
    5. import java.util.List;
    6. import java.util.logging.Logger;
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.World;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.EventPriority;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.player.PlayerChangedWorldEvent;
    15. import org.bukkit.event.player.PlayerJoinEvent;
    16.  
    17. /**
    18.  *
    19.  * @author kennywilliams
    20.  */
    21. public class PlayerListener implements Listener{
    22. private Logger log = Logger.getLogger("Minecraft");
    23. WorldMOTD plugin;
    24.  
    25. public PlayerListener(WorldMOTD instance){
    26. plugin = instance;
    27. }
    28.  
    29. @EventHandler
    30. public void playerChangedWorldEvent(PlayerChangedWorldEvent event){
    31. Player player = event.getPlayer();
    32. if(!player.hasPermission("worldmotd.motd."+player.getWorld().getName())){
    33. return;
    34. }
    35. List<String> worlds = plugin.getConfig().getStringList("worlds");
    36. World w;
    37. for(String str : worlds){
    38. w = Bukkit.getServer().getWorld(str);
    39. if(w == player.getWorld()){
    40. if(plugin.getConfig().getBoolean(w.getName()+".clear-chat")){
    41. for(int i = 0; i<121; i++){
    42. player.sendMessage("");
    43. }
    44. }
    45.  
    46. WorldmotdAPI api = new WorldmotdAPI();
    47. ArrayList<String> motd = api.getWorldMotd(player, player.getWorld().getName());
    48. for(String s : motd)
    49. {
    50. player.sendMessage(ChatColor.translateAlternateColorCodes('&', s));
    51. }
    52. }
    53. }
    54. }
    55.  
    56. @EventHandler
    57. public void onPlayerLogin(PlayerJoinEvent event){
    58. final Player player = event.getPlayer();
    59. final World w = player.getWorld();
    60. if(!plugin.getConfig().getBoolean(w.getName()+".on-player-login.enable")){
    61. return;
    62. }
    63. if(!player.hasPermission("worldmotd.login.motd."+w.getName())){
    64. return;
    65. }
    66. List<String> worlds = plugin.getConfig().getStringList("worlds");
    67. final List<String> motd = plugin.getConfig().getStringList("");
    68. final String output = "";
    69. final WorldmotdAPI api = new WorldmotdAPI();
    70.  
    71. for(String str : worlds){
    72. if(str.equalsIgnoreCase(w.getName())){
    73. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){
    74. public void run(){
    75. if(plugin.getConfig().getBoolean(w.getName()+".on-player-login.clear-chat")){
    76. for(int i = 0; i<121; i++){
    77. player.sendMessage("");
    78. }
    79. }
    80. ArrayList<String> motd = api.getLoginMotd(player, w.getName());
    81. for(String s : motd)
    82. {
    83. player.sendMessage(ChatColor.translateAlternateColorCodes('&', s));
    84. }
    85. }
    86. }, 1L);
    87. }
    88. }
    89. }
    90. }
    91.  



    If you need any more info just ask. Thanks in advance for the help!
     
  2. Offline

    Timr

    looks like configMotd is null, check that it was set.
     
  3. Offline

    exload

    Yes I knew that. I should have included an example of what is in the config. It looks something like this:
    Code:
    build:
      motd:
        - 'Welcome to this awesome world!'
      clear-chat: false
      on-player-login:
        enable: true
        motd:
          - 'This message will be sent when the player logs in to build!!'
        clear-chat: false
     
Thread Status:
Not open for further replies.

Share This Page