Don't know the right code for this.

Discussion in 'Plugin Development' started by evantheis, Jun 23, 2013.

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

    evantheis

    Code:java
    1. public class Main extends JavaPlugin {
    2. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    3. Bukkit.getServer().broadcastMessage(ChatColor.GREEN + "The player" + getServer().getPlayer(label) + "says hello!");
    4. return true;
    5. }
    6. }

    What do I do right here?
    (ChatColor.GREEN + "The player" + getServer().getPlayer(label) + "says hello!");
    I want it to say the person that sent the command? Also, what do I do if I want to have mulitple commands. Where do I say the command name?
    also problem in my plugin.yml
    Code:
    name: ShoutCraft
    version: 1.2 Beta
    main: com.endlesswar.ShoutCraft
    commands:
      Hello:
        description: Will make you say Hello!
    I get this in the console:
    03:03:26 [SEVERE] Could not load 'plugins\Shout.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidDescriptionException: Invalid plugin.yml
    at org.bukkit.plugin.java.JavaPluginLoader.getPluginDescription(JavaPlug
    inLoader.java:257)
    at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager
    .java:132)
    at org.bukkit.craftbukkit.v1_5_R3.CraftServer.loadPlugins(CraftServer.ja
    va:239)
    at org.bukkit.craftbukkit.v1_5_R3.CraftServer.<init>(CraftServer.java:21
    7)
    at net.minecraft.server.v1_5_R3.PlayerList.<init>(PlayerList.java:55)
    at net.minecraft.server.v1_5_R3.DedicatedPlayerList.<init>(SourceFile:11
    )
    at net.minecraft.server.v1_5_R3.DedicatedServer.init(DedicatedServer.jav
    a:106)
    at net.minecraft.server.v1_5_R3.MinecraftServer.run(MinecraftServer.java
    :382)
    at net.minecraft.server.v1_5_R3.ThreadServerApplication.run(SourceFile:5
    73)
    Caused by: mapping values are not allowed here
    in "<reader>", line 4, column 10:
    commands:
    ^

    at org.yaml.snakeyaml.scanner.ScannerImpl.fetchValue(ScannerImpl.java:73
    3)
    at org.yaml.snakeyaml.scanner.ScannerImpl.fetchMoreTokens(ScannerImpl.ja
    va:305)
    at org.yaml.snakeyaml.scanner.ScannerImpl.checkToken(ScannerImpl.java:17
    9)
    at org.yaml.snakeyaml.parser.ParserImpl$ParseBlockMappingKey.produce(Par
    serImpl.java:563)
    at org.yaml.snakeyaml.parser.ParserImpl.peekEvent(ParserImpl.java:161)
    at org.yaml.snakeyaml.parser.ParserImpl.checkEvent(ParserImpl.java:146)
    at org.yaml.snakeyaml.composer.Composer.composeMappingNode(Composer.java
    :230)
    at org.yaml.snakeyaml.composer.Composer.composeNode(Composer.java:160)
    at org.yaml.snakeyaml.composer.Composer.composeDocument(Composer.java:12
    3)
    at org.yaml.snakeyaml.composer.Composer.getSingleNode(Composer.java:106)

    at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseCons
    tructor.java:121)
    at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:480)
    at org.yaml.snakeyaml.Yaml.load(Yaml.java:411)
    at org.bukkit.plugin.PluginDescriptionFile.<init>(PluginDescriptionFile.
    java:188)
    at org.bukkit.plugin.java.JavaPluginLoader.getPluginDescription(JavaPlug
    inLoader.java:252)
    ... 8 more
     
  2. Offline

    G4meM0ment

    You need to build a plugin, with plugin.yml, onEnable, onDisable and various stuff.
    Just search for tutorials how to create bukkit plugins.
    (If thats all of your code)
     
  3. Offline

    jackwilsdon

    It seems there's something up with your plugin.yml. The correct format for the plugin.yml can be found here.
    Now, as for the rest of your plugin, G4meM0ment was correct, you need at least an onEnable() inside your Main class. Here's a corrected version;

    Code:java
    1. public class Main extends JavaPlugin implements CommandExecutor {
    2. public void onEnable()
    3. {
    4. this.getCommand("hello").setExecutor(this);
    5. }
    6.  
    7. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    8. Bukkit.getServer().broadcastMessage(ChatColor.GREEN + "The player" + sender.getName() + "says hello!");
    9. return true;
    10. }
    11. }

    Code:
    name: "ShoutCraft"
    version: "1.2b"
    main: "com.endlesswar.ShoutCraft"
    commands:
      hello:
        description: Will make you say Hello!
     
  4. Offline

    Rocoty

    jackwilsdon No? You don't need to override the onEnable(). It can be left without being overriden and the plugin would work just as good, assuming you don't need something initialized or done when the plugin is enabled. Which is not necessary here, as the main class is by default set as the default executor for all the plugin's commands.

    This class also does not need to implement CommandExecutor, by the way, because JavaPlugin does that already.
     
    Minnymin3 and dillyg10 like this.
  5. Offline

    jackwilsdon

    Ah, I did not know this, I apologise. Well, it had something to do with the YML anyway, so I hope my version works
     
  6. Offline

    Rocoty

    evantheis You can get the name of the CommandSender by doing sender.getName()
    Line number 4, column 10 in the plugin.yml.....are you sure there is no space behind command: ?
     
  7. first of all, you didn't give your command a name, to do this, let your command look like this : (see the part after if (cmd.getName))
    Code:java
    1. public class Main extends JavaPlugin {
    2. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    3. if (cmd.getName().equalsIgnoreCase("hello")
    4. Bukkit.getServer().broadcastMessage(ChatColor.GREEN + "The player" + getServer().getPlayer(label) + "says hello!");
    5. return true;

    second, you forgot to put in onEnable() and onDisable(), do it like this:
    Code:java
    1. public class Main extends JavaPlugin {
    2. @Override
    3. public void onEnable() {
    4. getLogger().info("Some text to say when plugin enables")
    5. }
    6. @Override
    7. public void onDisable() {
    8. getLogger.info("Some text to say when plugin disables")
    9. }
    10. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    11. if (cmd.getName().equalsIgnoreCase("hello")
    12. Bukkit.getServer().broadcastMessage(ChatColor.GREEN + "The player" + getServer().getPlayer(label) + "says hello!");
    13. return true;

    third, you also have to import all of the methods that aren't defined, and you forgot to define the package: you have to import org.bukkit.ChatColor; ,
    import org.bukkit.plugin.java.JavaPlugin;, import org.bukkit.command.Command;, import org.bukkit.command.CommandSender;, org.bukkit.Bukkit;, like this:
    Code:java
    1.  
    2. package [FONT=Consolas]com.endlesswar.ShoutCraft.("MainClassName")[/FONT]
    3.  
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.plugin.java.JavaPlugin;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9.  
    10. public class Main extends JavaPlugin {
    11. @Override
    12. public void onEnable() {
    13. getLogger().info("Some text to say when plugin enables");
    14. }
    15. @Override
    16. public void onDisable() {
    17. getLogger.info("Some text to say when plugin disables");
    18. }
    19. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    20. if (cmd.getName().equalsIgnoreCase("hello"){
    21. Bukkit.getServer().broadcastMessage(ChatColor.GREEN + "The player" + getServer().getPlayer(label) + "says hello!");
    22. return true;
    23. }
    24. return false;
    25. }

    and last, to get the playername, you have to add this: sender.getName(), (and you forgot to add spaces, you do it like this: + " ") so this is the final code you should want:
    Code:java
    1.  
    2. package [FONT=Consolas]com.endlesswar.ShoutCraft[/FONT]
    3.  
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10.  
    11. public class Shout extends JavaPlugin {
    12. @Override
    13. public void onEnable() {
    14. getLogger().info("Some text to say when plugin enables");
    15. }
    16. @Override
    17. public void onDisable() {
    18. getLogger().info("Some text to say when plugin disables");
    19. }
    20. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    21. if (cmd.getName().equalsIgnoreCase("hello"));{
    22. Bukkit.getServer().broadcastMessage(ChatColor.GREEN + "The player" + " " + sender.getName() + " " + "says hello!");
    23. return true;
    24. }
    25. }
    26. }
    27. }

    and you want the plugin.yml to look like this:
    Code:
    name: (name you want for the plugin)
    main: com.endlesswar.ShoutCraft.main
    version: (version you want)
    commands:
        hello:
            description: says hello to the server
            usage: /<command>
            permission: (text).(text)
    To add more commands to your plugin.yml do this:
    Code:
    name: (name you want for the plugin)
    main: com.endlesswar.ShoutCraft.main
    version: (version you want)
    commands:
        hello:
            description: says hello to the server
            usage: /<command>
            permission: (text).(text)
      (other command name):
            description: (description)
            usage: /<command> (alternative args)
            permission: (other permission.
    and so on................
    to add other commands in your class, do it like this (I only used the public boolean part so it was a bit easier to understand):
    Code:java
    1.  
    2. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    3. if (cmd.getName().equalsIgnoreCase("hello"));{
    4. //do stuff
    5. return true;
    6. }
    7. if (cmd.getName().equalsIgnoreCase(othercommandname){
    8. //do other stuff
    9. return true;
    10. }
    11. return false;
    12. }
    13.  

    Hope it helps, say so if you don't understand a part.
     
  8. Offline

    Rocoty

     
  9. the @Override thing doesn't matter, since it (in this case) only is used to print a message to your console when it enables/disables the plugin. but did the rest help you?\
    no I guess
     
  10. Offline

    evantheis



    You are the biggest help! Thank you so much!
     
  11. You are welcome!
     
  12. Offline

    evantheis

    I am actually having a problem. Do you have skype?
     
  13. yup, but I have to go to school, and in the weekend and monady I am away, so tuesday around 3 o clock?
     
  14. Offline

    evantheis

    Yes what's your username? Also, go to school right now?
     
  15. my username is jcdra1, and my school has finished now, so I have time :)
     
  16. Offline

    kreashenz

    So did the problem get fixed, because his plugin.yml was missing the 'description' part, so that's what the YAMLParse errors were.
     
  17. Offline

    evantheis


    Yes, thank you.
     
  18. evantheis I don't have time today, I'm sorry, but you could just post your class and errors and what it should do that it doesn't do, so I can help you right away.
    (If you read this before 10:00, you can skype me.)
     
Thread Status:
Not open for further replies.

Share This Page