My plugin doesn't work.

Discussion in 'Plugin Development' started by boss86741, Dec 11, 2012.

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

    reesylou

    Code:java
    1.  
    2. String prefix;
    3. public String getPlayerPrefix(Player player) {
    4. return prefix;
    5. }
    6. //snipped stuff
    7. String g = (prefix);
    8.  


    You define the string prefix, return the (empty) prefix value in a function (that you don't seem to call) and then use the (still empty) value to assign to 'g' which you use to print the prefix.

    There is no way prefix can have a value with the code you have shown.

    To integrate with Vault, you need to:
    1. Put Vault as a dependency in your plugin.yml
    2. Check in your onEnable that Vault is loaded (optional as Bukkit should ensure this if you have the dependency)
    3. Set up the chat provider (see https://github.com/MilkBowl/Vault#implementing-vault)
    4. Remove your getPlayerPrefix function (and the prefix variable)
    5. Replace the line "String g = (prefix);" with "String g = chat.getPlayerPrefix(p);"

    That should do it
     
  2. Offline

    boss86741

    LOL, the cut was already registered! The problem was I didn't know how to use the function! Thanks! Now it works perfectly.

    Actually, now it is giving a nullpointerexception again:

    16:02:22 [INFO] boss86741[/127.0.0.1:60275] logged in with entity id 830 at ([world] -2052.326800969757, 66.0, 1280.271361218455)
    Code:
    16:02:26 [INFO] boss86741 issued server command: /shout hi
    16:02:27 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'shout' in plugin ShoutCraft v0.1.6
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:186)
    at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:502)
    at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:985)
    at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:903)
    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:858)
    at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:44)
    at net.minecraft.server.NetworkManager.b(NetworkManager.java:290)
    at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:113)
    at net.minecraft.server.ServerConnection.b(SourceFile:39)
    at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
    at net.minecraft.server.MinecraftServer.r(MinecraftServer.java:595)
    at net.minecraft.server.DedicatedServer.r(DedicatedServer.java:222)
    at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:493)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:426)
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:856)
    Caused by: java.lang.NullPointerException
    at com.boss86741.plugins.ShoutCraft.ShoutCraftCommands.onCommand(ShoutCraftCommands.java:50)
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    ... 15 more
    
    Cruel, huh? The problem is at this line of code:

    Code:java
    1. if (player.hasPermission("shoutcraft.shout")) {
    2. String plName = player.getName();
    3. String g = chat.getPlayerPrefix(player);


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

    fireblast709

    player is null?
     
  4. Offline

    boss86741

    don't think so...

    the command is executed from the player

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

    fireblast709

    then chat is null. What is line 50 anyway?
     
  6. Offline

    boss86741

    Code:java
    1. String g = chat.getPlayerPrefix(player);
     
  7. Offline

    fireblast709

    then chat is null
     
  8. Offline

    boss86741

    how do I fix that?
     
  9. Offline

    fireblast709

    post your current code. I need to know what 'chat' is
     
  10. Offline

    boss86741

    Code:java
    1. package com.boss86741.plugins.ShoutCraft;
    2.  
    3. import java.lang.String;
    4.  
    5. import net.milkbowl.vault.chat.Chat;
    6.  
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.Bukkit;
    10. import org.bukkit.ChatColor;
    11. import org.bukkit.command.CommandExecutor;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.plugin.RegisteredServiceProvider;
    14.  
    15. public class ShoutCraftCommands implements CommandExecutor {
    16. private ShoutCraft plugin;
    17.  
    18. public ShoutCraftCommands(ShoutCraft plugin) {
    19. this.plugin = plugin;
    20. }
    21.  
    22. Player player = null;
    23.  
    24. public static Chat chat = null;
    25.  
    26. private boolean setupChat()
    27. {
    28. RegisteredServiceProvider<Chat> chatProvider = Bukkit.getServer().getServicesManager().getRegistration(net.milkbowl.vault.chat.Chat.class);
    29. if (chatProvider != null) {
    30. chat = chatProvider.getProvider();
    31. }
    32.  
    33. return (chat != null);
    34. }
    35.  
    36.  
    37. // Below is the command code.
    38.  
    39. @Override
    40. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    41. if(cmd.getName().equalsIgnoreCase("shout")) {
    42. if (sender instanceof Player) {
    43. player = (Player) sender;
    44. }
    45. if (player == null) {
    46. sender.sendMessage(ChatColor.RED + "Can't be used from the console.");
    47. } else {
    48. if (player.hasPermission("shoutcraft.shout")) {
    49. String plName = player.getName();
    50. String g = chat.getPlayerPrefix(player);
    51.  
    52. String message = "";
    53.  
    54. if (args.length > 0) {
    55. for (int i = 1; i < args.length; ++i) {
    56. message += args[i] + " ";
    57. }
    58. Bukkit.getServer().broadcastMessage(ChatColor.RED + "[S]" + ChatColor.WHITE + "=[" + g + ChatColor.WHITE + "]=" + plName + ChatColor.WHITE + ": " + ChatColor.GOLD + message);
    59. } else {
    60. sender.sendMessage(ChatColor.RED + "Usage: /shout <your message here>");
    61. }
    62. }
    63. return true;
    64. }
    65. }
    66. return false;
    67. }
    68. }
    69. [/S][/i]
     
  11. Offline

    fireblast709

    chat is null. also, don't use static when not needed ;3
     
  12. Offline

    boss86741

    How do I make it not null? I use PEX.
     
  13. Offline

    fireblast709

    do you have Vault as a dependency?
     
  14. Offline

    boss86741

  15. Offline

    fireblast709

    do you check what setupChat() returns?
     
  16. Offline

    boss86741

  17. Offline

    fireblast709

    do that. If this returns false, it means chat is null, and it will throw a NPE if you use it any further
     
  18. Offline

    Tehmaker

    Boss, I suggest you watch a few NewBoston Java Tutorials first...
     
    lol768 likes this.
Thread Status:
Not open for further replies.

Share This Page