My plugin doesn't work.

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

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

    boss86741

    The commands (/mute and /shout) register on the console but do not return anything in game. The plugin (ShoutCraft) runs without errors.

    ShoutCraft class:

    Code:
    package com.boss86741.plugins.ShoutCraft;
     
    import org.bukkit.plugin.java.JavaPlugin;
     
    /* Below is the code that tells java to use the /shout command in the
    * ShoutCraftCommands.class file. It also tells it what to say to console on the start of
    * the server.*/
     
    public class ShoutCraft extends JavaPlugin {
    @Override
    public void onEnable() {
    getLogger().info("Shout command and timer working! Minecraft version: 1.4.5");
    getCommand("shout").setExecutor(new ShoutCraftCommands(this));
    getCommand("muteshout").setExecutor(new ShoutCraftCommands(this));
    }
     
    @Override
    public void onDisable() {
    getLogger().info("OK, ShoutCraft has successfully been disabled!");
    }
    }
    ShoutCraft Commands:

    Code:
    package com.boss86741.plugins.ShoutCraft;
     
    import net.milkbowl.vault.chat.Chat;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.plugin.RegisteredServiceProvider;
     
    public class ShoutCraftCommands implements CommandExecutor {
    private ShoutCraft plugin;
     
    public ShoutCraftCommands(ShoutCraft plugin) {
    this.plugin = plugin;
    }
    // Here is the chat code!
    public static Chat chat = null;
     
    private boolean setupChat()
    {
    RegisteredServiceProvider<Chat> chatProvider = Bukkit.getServer().getServicesManager().getRegistration(net.milkbowl.vault.chat.Chat.class);
    if (chatProvider != null) {
    chat = chatProvider.getProvider();
    }
     
    return (chat != null);
    }
    // Below is the command code.
     
    @Override
    public boolean onCommand(Player sender, Command cmd, String label, String prefix, String[] args) {
    if(cmd.getName().equalsIgnoreCase("shout")) {
    Player s = (sender);
    String g = (prefix);
     
    String message = "";
     
    if (args.length > 1) {
    for (int i = 1; i < args.length; ++i) {
    message += args + " ";
    }
    Bukkit.getServer().broadcastMessage(ChatColor.RED + "" + ChatColor.WHITE + "=[" + g + ChatColor.WHITE + "]=" + s + ChatColor.WHITE + ":" + ChatColor.string.replaceAll("(&([a-f0-9]))", "\u00A7$2") + message);
    }else{
    s.sendMessage(ChatColor.RED + "Usage: /shout <your message here>");
    }
     
    return true;
    }
    return false;
    }
     
    @Override
    public boolean onCommand(String[] args, CommandSender sender, Command cmd) {
    if(cmd.getName().equalsIgnoreCase("muteshout")) {
    CommandSender s = (sender);
    String t = (args[0]);
     
    Bukkit.getLogger().severe(t + " has been muted by " + s + "! To unmute them, do /unmute <player>!");
     
    if(cmd.getName().equalsIgnoreCase("shout")) {
    }
     
     
    return true;
    }
    return false;
    }
     
     
    @Override
    public boolean onCommand(CommandSender arg0, Command arg1, String arg2, String[] arg3) {
    return false;
    }
    }
    
    Plugin.yml:

    Code:
    main: com.boss86741.plugins.ShoutCraft.ShoutCraft
    name: ShoutCraft
    version: 0.1.5
    author: boss86741
    description: Adds shout messages to your server. Works with Herochat and PermissionsEX.
    depend: [Herochat, Vault]
    prefix: ShoutCraft
    website: [URL]http://www.thebossserver.com/[/URL]
    commands:
    shout:
    description: This command makes you broadcast a message to the server globaly.
    permission: shoutcraft.shout
    permission-message: You do not have permission to use this command.
    muteshout:
    description: This command disables another player from shouting
    permission: shoutcraft.mute
    permission-message: You do not have permission to use this command.
    permissions:
    shoutcraft.shout:
    description: Allows you to shout (talk globaly) with the timer.
    default: true
    shoutcraft.mute:
    description: Allows a mod or admin to mute a player from shouting.
    default: op
     
  2. Offline

    gomeow

    Your onCommand needs to look like this because this is what bukkit passes to the plugins
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
     
  3. Offline

    boss86741

    Then what about the other stuff? I am a noob because I have only been a developer for a few weeks and I am new.
     
  4. Offline

    fireblast709

    Also, use one instance of the ShoutCraftCommands
    Code:java
    1. ShoutCraftCommands scc = new ShoutCraftCommands(this);
    2. getCommand("shout").setExecutor(scc);
    3. getCommand("muteshout").setExecutor(scc);

    Just to prevent some issues in the future.
    For the rest: either set the variables as class variables, or allow people to fill them as arguments
     
  5. Offline

    boss86741

    One thing though, it works perfectly except for a few things. I already know why the prefix is "true". I want to know why it called me craftplayer{name=boss86741}? My username is boss86741 but why the extra stuff?
    [​IMG]
     
  6. Use:
    Code:
    String playername = player.getName();
     
  7. Offline

    ZeusAllMighty11


    What kind of issues?
     
  8. Offline

    boss86741

    Won't let me use the player.getName() function. I imported player but still says that player can't be resolved.
     
  9. Are you sure you know what you're doing? It's because you never initialized "player", you've named that variable "sender".

    This is how I use onCommand.
    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    3. Player player = null;
    4.  
    5. if (sender instanceof Player) {
    6. player = (Player) sender;
    7. }
    8. if (cmd.getName().equalsIgnoreCase("commandnamehere")) {
    9. if (player == null) {
    10. sender.sendMessage("Can't be used from the console.");
    11. } else {
    12. if (player.hasPermission("perhaps.you.want.to.use_permissions_nodes_here")) {
    13. String plName = player.getName();
    14. player.sendMessage(ChatColor.GOLD + "Player name="+plName);
     
  10. Offline

    fireblast709

    Like if you have a value shared over commands, and you assign each command to another instance, it cannot be shared
     
  11. Also doubling the amount of RAM (2 instances) for no reason.
     
  12. Offline

    ZeusAllMighty11

    Okay, thanks.


    But if I did like 4 instead of 2, doesit quadruple? Code:
    Code:
    getCommand("command").setExecutor(new CommandClass());
    getCommand("command2").setExecutor(new CommandClass());
    getCommand("command3").setExecutor(new CommandClass());
    getCommand("command4").setExecutor(new CommandClass());
    
    is that 4x as bad or just 2x?
     
  13. ZeusAllMighty11 It might actually not even be 2x with 2 files cause of shared RAM (not sure if/how the JVM handles that) but you will see more RAM used with every instance. Just a few mb (or was it even just kb?) but measurable.

    The question is why would you want to use 4 instances in the first place?
     
  14. Offline

    ahuby09

    your plugin.yml isn't layed out propley
     
  15. Offline

    boss86741

    Strangely, when I posted it it took out the spaces. It is layed out perfectly, it doesn't crash.
     
  16. Offline

    ahuby09

    oh ok lol i just saw that and thought better warn you :)
     
  17. Offline

    boss86741

    Look at this, and it still says it can't resolve player!

    Code:java
    1. package com.boss86741.plugins.ShoutCraft;
    2.  
    3. import java.lang.String;
    4.  
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.command.CommandExecutor;
    10. import org.bukkit.entity.Player;
    11.  
    12. public class ShoutCraftCommands implements CommandExecutor {
    13. private ShoutCraft plugin;
    14.  
    15. public ShoutCraftCommands(ShoutCraft plugin) {
    16. this.plugin = plugin;
    17. }
    18.  
    19. String prefix;
    20. public String getPlayerPrefix(Player player) {
    21. return prefix;
    22. }
    23.  
    24. String plName = player.getName();
    25. // Below is the command code.
    26.  
    27. @Override
    28. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    29. if(cmd.getName().equalsIgnoreCase("shout")) {
    30. String p = (plName);
    31. String g = (prefix);
    32.  
    33. String message = "";
    34.  
    35. if (args.length > 0) {
    36. for (int i = 1; i < args.length; ++i) {
    37. message += args[i] + " ";
    38. }
    39. Bukkit.getServer().broadcastMessage(ChatColor.RED + "[S]" + ChatColor.WHITE + "=[" + g + ChatColor.WHITE + "]=" + p + ChatColor.WHITE + ": " + ChatColor.GOLD + message);
    40. } else {
    41. sender.sendMessage(ChatColor.RED + "Usage: /shout <your message here>");
    42. }
    43.  
    44. return true;
    45. }
    46.  
    47. if(cmd.getName().equalsIgnoreCase("muteshout")) {
    48. CommandSender s = (sender);
    49. String t = (args[0]);
    50.  
    51. Bukkit.getLogger().severe(t + " has been muted by " + s + "! To unmute them, do /unmute <player>!");
    52.  
    53. if(cmd.getName().equalsIgnoreCase("shout")) {
    54. }
    55.  
    56.  
    57. return true;
    58. }
    59. return false;
    60. }
    61. }
    62.  
    63. [/S][/i]
     
  18. Offline

    gomeow

    We cannot read that. When you post code like that, pass it through a plain text editor first to take the fonts and color out
     
  19. Offline

    boss86741

    Oops, done. Sorry.
     
  20. Offline

    Cammy_the_block

    You might wanna use a different way of doing the colors (shorter in chars) because it's really annoying to read.

    Cammy
     
  21. Offline

    boss86741

    OK here is another post:

    Code:
    package com.boss86741.plugins.ShoutCraft;
     
    import java.lang.String;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.entity.Player;
     
    public class ShoutCraftCommands implements CommandExecutor {
        private ShoutCraft plugin;
     
        public ShoutCraftCommands(ShoutCraft plugin) {
            this.plugin = plugin;
        }
       
        String prefix;
        public String getPlayerPrefix(Player player) {
            return prefix;
        }
       
        String plName = player.getName();
    // Below is the command code.   
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("shout")) {
                String p = (plName);
                String g = (prefix);
     
                String message = "";
                   
                if (args.length > 0) {
                    for (int i = 1; i < args.length; ++i) {
                        message += args[i] + " ";
                    }   
                    Bukkit.getServer().broadcastMessage(ChatColor.RED + "[S]" + ChatColor.WHITE + "=[" + g + ChatColor.WHITE + "]=" + p + ChatColor.WHITE + ": " + ChatColor.GOLD + message);
                } else {
                    sender.sendMessage(ChatColor.RED + "Usage: /shout <your message here>");
                }
               
                return true;
            }
           
            if(cmd.getName().equalsIgnoreCase("muteshout")) {
                CommandSender s = (sender);
                String t = (args[0]);
               
                Bukkit.getLogger().severe(t + " has been muted by " + s + "! To unmute them, do /unmute <player>!");
               
                if(cmd.getName().equalsIgnoreCase("shout")) {
                }
               
               
                return true;
            }
            return false;
        }
    }
    
     
  22. Offline

    Cammy_the_block

    Oops I ment the anoying color tags. I didn't realize that wasn't part of your code but thanks anyways. Derp :p

    Cammy
     
  23. Offline

    fireblast709

    prefix is null, and there is no variable called player in the class body
     
  24. Offline

    boss86741

    Huh?

    It is throwing me null pointer exceptions now.

    06:04:59 [SEVERE] Error occurred while enabling ShoutCraft v0.1.6 (Is it up to date?)
    java.lang.NullPointerException
    at com.boss86741.plugins.ShoutCraft.ShoutCraftCommands.<init>(ShoutCraftCommands.java:26)
    at com.boss86741.plugins.ShoutCraft.ShoutCraft.onEnable(ShoutCraft.java:30)
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:374)
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:381)
    at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:270)
    at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:252)
    at net.minecraft.server.MinecraftServer.j(MinecraftServer.java:320)
    at net.minecraft.server.MinecraftServer.e(MinecraftServer.java:299)
    at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:258)
    at net.minecraft.server.DedicatedServer.init(DedicatedServer.java:147)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:398)
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:856)

    That error says line 26 in ShoutCraftCoommands. This line:

    Code:java
    1. string plName = player.getName();


    This is the whole file:

    Code:
    package com.boss86741.plugins.ShoutCraft;
     
    import java.lang.String;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.entity.Player;
     
    public class ShoutCraftCommands implements CommandExecutor {
        private ShoutCraft plugin;
     
        public ShoutCraftCommands(ShoutCraft plugin) {
            this.plugin = plugin;
        }
       
        String prefix;
        public String getPlayerPrefix(Player player) {
            return prefix;
        }
       
        Player player = null;
       
        String plName = player.getName();
    // Below is the command code.   
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("shout")) {
                if (sender instanceof Player) {
                    player = (Player) sender;
                }
                    if (player == null) {
                        sender.sendMessage(ChatColor.RED + "Can't be used from the console.");
                    } else {
                    if (player.hasPermission("shoutcraft.shout")) {
                        String plName = player.getName();
                        String p = (plName);
                        String g = (prefix);
     
                        String message = "";
                   
                        if (args.length > 0) {
                            for (int i = 1; i < args.length; ++i) {
                                message += args[i] + " ";
                            }   
                            Bukkit.getServer().broadcastMessage(ChatColor.RED + "[S]" + ChatColor.WHITE + "=[" + g + ChatColor.WHITE + "]=" + p + ChatColor.WHITE + ": " + ChatColor.GOLD + message);
                        } else {
                            sender.sendMessage(ChatColor.RED + "Usage: /shout <your message here>");
                        }
                    }
                        return true;
                }
            }
            return false;
        }
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  25. Offline

    fireblast709

    Cannot be the right line, otherwise player.hasPermission() would have thrown it
     
  26. Offline

    boss86741

    Well, what is the problem, the permission is registered in plugin.yml

    Also, I need help with the prefixes syncing with vault.

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

    reesylou

    This is your problem:
    Code:java
    1.  
    2.  
    3. Player player = null;
    4.  
    5. String plName = player.getName();
    6. // Below is the command code.
    7.  
    8.  

    You are setting the player variable up as null (this is OK) and immediately trying to get the name from taht player variable (not OK).

    And then you redeclare the plName later in the code (where you actually use it - this is OK).
    Get rid of the first "String plName = player.getName();" that I have shown above.
     
  28. Offline

    boss86741

    OK, now it works good except for the fact that the prefixes don't work. My prefix is Owner/The_Awesome_Potato and it shows as null.

    [​IMG]
     
  29. Offline

    reesylou

    Hmmm looks like you are defining the variable prefix, but not populating it.
     
  30. Offline

    boss86741

Thread Status:
Not open for further replies.

Share This Page