First Plugin, Help?

Discussion in 'Plugin Development' started by ChumChum, Jan 20, 2012.

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

    ChumChum

    I want a plugin that can make people do Suicide.
    Im really really really new, and i started yesturday.
    I watched some tutorials but i couldnt really understand them and i have all the resources , JDK, Eclipse.
    Here what i have in my class.
    Code:
    package me.ChumChumx.Cuicide;
     
    public class Cuicide  {
     
    }
    
    In the plugin.yml
    Code:
    name: Cuicide
    main: me.ChumChumx.cuicide.Cuicide
    version: 0.1
    description: >
                Easier way to die!
    commands:
    
    Can i please get some help?
    I have some ideas for it.
    Commands: /die , /d
    permissions: Cuicide.die (Able to use /die)
    When they do suicide i want it to broadcast it to the server. e.g: ChumChumx Did Suicide!
     
  2. Well, since you know java.
    You can use the onCommand method.
    then you must get the player class with commandsender.
    and then you can do whatever you want with the player.

    example.

    PHP:
    public boolean onCommand(CommandSender senderCommand cmdString commandLabel,String[] arg3) {
     
            
    Player player = (Player)sender;
       
                
    player.setHealth(0);
       
                return 
    true;
                }
    EDIT:
    your class needs to extends JavaPlugin.
    I recommend you watch the tutorials again.
     
  3. Offline

    ChumChum

    How do i set commands and permissions?
    I told you im really new. I watched like 3 Tutorials and i didnt understand them. :(
    Is their a code to say
    Cuicide Has been Enabled
    in the console?
    I think i saw that somewhere in a tutorial but i cant remember where...
     
  4. Offline

    CRAZYxMUNK3Y

    When a player dies, and sends a message to all, use this (or similar(your liking))

    Code:java
    1.  
    2. public boolean onCommandEvent(CommandSender sender, Command cmd,
    3. String commandLabel, String[] arg3) {
    4. Player player = ((Player)sender);
    5. String PlayerName = player.getName();
    6.  
    7. if (commandLabel.equalsIgnoreCase("die") && player.hasPermission("permission.here")) {
    8. player.setHealth(0);
    9. this.getServer().broadcastMessage(PlayerName + "died");
    10.  
    11. }
    12. return true;
    13. }
    14.  


    To show it being enabled and disabled in log, do this;

    Code:java
    1.  
    2. public void onEnable() {
    3. log.info("enabled");
    4.  
    5. }
    6.  
    7. public void onDisable() {
    8. log.info("disabled");
    9.  
    10. }
    11.  


    You could also do this for logging the start stop of plugin (gets the info straight from the the plugin.yml file)
    Code:java
    1.  
    2. Logger log = Logger.getLogger("Minecraft");
    3.  
    4. public void onEnable() {
    5. PluginDescriptionFile pdfFile = this.getDescription();
    6. log.info(pdfFile.getName() + " has been enabled with version: " + pdfFile.getVersion());
    7.  
    8. }
    9.  
    10. public void onDisable() {
    11. PluginDescriptionFile pdfFile = this.getDescription();
    12. log.info(pdfFile.getName() + " has been disabled");
    13.  
    14. }
    15.  
     
  5. Offline

    ChumChum

    Thanks, ill try it right now.
    Also where can i learn how to do this myself?

    I didnt want you to tell me :(
    Well, Kinda thanks for it :)
    so do i just copy and paste it?
    Is their a way i could learn this, like make my own without asking someone to help me?

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

    CRAZYxMUNK3Y

    Sorry :S

    You can either do that or type it all... Apparently doing it (typing it) makes it easier to learn and know what you are doing

    What i did was used a variety of tutorials, the playlists are on my youtube channel if you want to look at them. Once they had been completed, i started making my own and just refereed back to them if i couldn't remember something.

    The 4 playlists are;

    Anti-Griefing Plugin Tut
    Bukkit Plugin Tutorials
    Hey Server Plugin Tut
    TNT Blocking Plugin
     
  7. Offline

    ChumChum

    Its okayy xD
    Is the command included with the code you gave me?
     
  8. Offline

    Madnessium

    SideNote: Make sure to check if sender =! instanceOf Player
     
  9. Offline

    CRAZYxMUNK3Y

    Yes, it' this line

    Code:java
    1.  
    2. if (commandLabel.equalsIgnoreCase("commandhere")/*<< That's the command in the brackets*/ && player.hasPermission("permission.here"))
    3.  
     
    ChumCHumx likes this.
  10. Offline

    emericask8ur

    I have made a nice Plugin Tutorial
     
  11. Offline

    ChumChum

    thanks

    Code:java
    1.  
    2. package me.ChumChumx.Cuicide;
    3.  
    4. import org.bukkit.command.CommandSender;
    5. import org.bukkit.command.ConsoleCommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Cuicide extends JavaPlugin {
    10.  
    11. @Override
    12. public void onDisable() {
    13. System.out.println("Cuicide Has been Disabled!");
    14.  
    15. }
    16.  
    17. @Override
    18. public void onEnable() {
    19. System.out.println("Cuicide Has Been Enabled!");
    20. }
    21.  
    22. public boolean onCommandEvent(ConsoleCommandSender sender, CommandSender cmd,
    23. String commandLabel, String[] arge3) {
    24. Player player = ((Player)sender);
    25. if (commandLabel.equalsIgnoreCase("die") && player.hasPermission("Cuicide.die")) {
    26. player.setHealth(0);
    27. this.getServer().broadcastMessage("PlayerName just did Suicide!");
    28.  
    29. }
    30. return true;
    31. } }
    32.  

    return true;
    is this good?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
    emericask8ur likes this.
  12. Offline

    theguynextdoor

    Almost.

    Hint- use cmd.getName() instead of commandLabel

    Code:
    public boolean onCommandEvent(ConsoleCommandSender sender, CommandSender cmd,
    String commandLabel, String[] args) {
    Player player = ((Player)sender);
    if (cmd.getName().equalsIgnoreCase("die")) ) {
    if(player.hasPermission("Cuicide.die")){
    player.setHealth(0);
    this.getServer().broadcastMessage("PlayerName just did Suicide!");
    return true;
    } else {
        player.sendMessage("You dont have permission");
    }
    }
    return false;
    }
     
  13. Offline

    emericask8ur

    Doesnt really matter? Doing the same thing
     
  14. Offline

    theguynextdoor

  15. Offline

    emericask8ur

    I use if(cmdLabel.equalsIgnoreCase(""));
    And I made a very large command Plugin if you havent noticed >_>
     
  16. Offline

    theguynextdoor

    I hope you dont use if(cmdLabel.equalsIgnoreCase("")); cos that if statement wont do alot with a ; at the end :p im joking i know what you mean

    I wasn't saying the other way wont work, just saying what the javadocs saying.

    Also i remember reading it in a thread. just found it.
    http://forums.bukkit.org/threads/keeps-saying-the-usage.47659/#post-828526

    Where someone says

    Not a good idea - the commandLabel is the alias by which the command was called. If the command has one or more aliases, your onCommand() will only handle that one case. In nearly all situations, you should use cmd.getName(), which is the command's actual name (as defined in plugin.yml) regardless of what alias was used.
     
  17. Offline

    ChumChum

    Is this the same thing?
    But the permission and the cmd is different?
    Is adding the "You Dont Have Permission that easy?
    thats good :D
    i just coded my first plugin today in 2 days :)
    How can i know these stuff?
    like the codes.
    Java language is really complicated..
    EDIT: Also, how can i add alises? i think thats how you spell it...
    Like /d
    EDIT2: What commands do i add for the plugin.yml?
    Theirs another commandLabel
    _____________________________________________________________
    package me.ChumChumx.Cuicide;

    import org.bukkit.command.CommandSender;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Cuicide extends JavaPlugin {

    @Override
    public void onDisable() {
    System.out.println("Cuicide Has been Disabled!");

    }

    @Override
    public void onEnable() {
    System.out.println("Cuicide Has Been Enabled!");
    }

    public boolean onCommandEvent(ConsoleCommandSender sender, CommandSender cmd,
    String commandLabel, String[] args) {
    Player player = ((Player)sender);
    if (cmd.getName().equalsIgnoreCase("die")) {
    if(player.hasPermission("Cuicide.die")){
    player.setHealth(0);
    this.getServer().broadcastMessage("PlayerName just did Suicide!");
    return true;
    } else {
    player.sendMessage("WHY YOU TRY AND DO SUICIDE?");
    }
    }
    return false;
    } }
    _____________________________________________________________
    Do i also change that into cmd.getName() ?

    theguynextdoor
    is that a yes ?
    It gives me an error.
    so , is it okay if i keep it commandLabel?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
    theguynextdoor likes this.
  18. Offline

    theguynextdoor

    Right, first things first, the command name is the same as when you wrote it. All i did was separate the permissions part so that you could add the else statement for when they don't have permissions. It is basically saying
    if the commands name is die then check if the player has the permissions "Cuicide.die", if they have the permission, set their health to 0 and broadcast a message. Else if the command is die but the player does not have the permission "Cuicide.die" then send dont kill them and instead send them the message "WHY YOU TRY AND DO SUICIDE?".

    To register you command in your plugin.yml
    Code:
    name: <plugin name>
    main: <package name>.<main class name>
    version: 0.1
    commands:
        die:
          description: toggles glowstone block below
          aliases: d[/SIZE]
          usage: /<command>
    And you can leave the bit where is says commandLabel as it is
     
  19. Offline

    emericask8ur

    Oh lol didnt see that ;)
     
  20. Offline

    ChumChum

    So if they die ,without using /die
    the broadcast message is still sent?
     
  21. Offline

    theguynextdoor

    The broadcast is only sent when they die due to your command or any alias you give it.
     
  22. Offline

    ChumChum

    Thanks :D
    This is my plugin.yml
    Code:
    [I][FONT=Consolas]name: Cuicide
    main: me.ChumChumx.Cuicide
    version: 0.1
    commands:
        die:
          description: does suicide
          aliases: d
          usage: /<command>[/FONT][/I]
     
  23. Offline

    theguynextdoor

    Should the main line not be
    Code:
    main: me.ChumChumx.Cuicide.Cuicide
    ?
    But if it is working don't change it
     
  24. Offline

    ChumChum

    This is what it says :(
    Code:java
    1.  
    2. 18:14:32 [SEVERE] Could not load 'plugins\Cuicide.jar' in folder 'plugins':
    3. java.lang.ClassNotFoundException: me.ChumChumx.cuicide.Cuicide
    4. at java.net.URLClassLoader$1.run(Unknown Source)
    5. at java.net.URLClassLoader$1.run(Unknown Source)
    6. at java.security.AccessController.doPrivileged(Native Method)
    7. at java.net.URLClassLoader.findClass(Unknown Source)
    8. at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.
    9. java:41)
    10. at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.
    11. java:29)
    12. at java.lang.ClassLoader.loadClass(Unknown Source)
    13. at java.lang.ClassLoader.loadClass(Unknown Source)
    14. at java.lang.Class.forName0(Native Method)
    15. at java.lang.Class.forName(Unknown Source)
    16. at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    17. ava:176)
    18. at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.
    19. java:215)
    20. at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager
    21. .java:136)
    22. at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:155)
    23. at org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:131)
    24. at net.minecraft.server.ServerConfigurationManager.<init>(ServerConfigur
    25. ationManager.java:52)
    26. at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:148)
    27. at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:407)
    28. at net.minecraft.server.ThreadServerApplication.run(SourceFile:465)
    29. [syntax]
    30. same class.
    31. this si the plugin.yml
    32. [CODE]
    33. [FONT=Consolas]name: Cuicide
    34. main: me.ChumChumx.cuicide.Cuicide
    35. version: 0.1
    36. commands:
    37. die:
    38. description: does suicide
    39. aliases: d
    40. usage: /<command>[/FONT][/CODE][/syntax]
     
  25. Offline

    theguynextdoor

     
    Last edited by a moderator: May 23, 2016
  26. Offline

    ChumChum

    okay its enabled,
    when i do /die it just repeats it. (in the chat) i dont die or anything.
    same with /d
    This is the main class
    Code:java
    1.  
    2. package me.ChumChumx.Cuicide;
    3.  
    4. import org.bukkit.command.CommandSender;
    5. import org.bukkit.command.ConsoleCommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Cuicide extends JavaPlugin {
    10.  
    11. @Override
    12. public void onDisable() {
    13. System.out.println("Cuicide Has been Disabled!");
    14.  
    15. }
    16.  
    17. @Override
    18. public void onEnable() {
    19. System.out.println("Cuicide Has Been Enabled!");
    20. }
    21.  
    22. public boolean onCommandEvent(ConsoleCommandSender sender, CommandSender cmd,
    23. String commandLabel, String[] args) {
    24. Player player = ((Player)sender);
    25. if (cmd.getName().equalsIgnoreCase("die")) {
    26. if(player.hasPermission("Cuicide.die")){
    27. player.setHealth(0);
    28. this.getServer().broadcastMessage("PlayerName just did Suicide!");
    29. return true;
    30. } else {
    31. player.sendMessage("WHY YOU TRY AND DO SUICIDE?");
    32. }
    33. }
    34. return false;
    35. } }
    36.  
    37.  
    38.  
    39.  


    Plugin.yml
    Code:
    name: Cuicide
    main: me.ChumChumx.Cuicide.Cuicide
    version: 0.1
    commands:
        die:
          description: does suicide
          aliases: d
          usage: /<command>
    
     
  27. Offline

    CRAZYxMUNK3Y

    Try this instead for the command;
    Code:java
    1.  
    2. public boolean onCommandEvent(CommandSender sender, CommandSender cmd, String commandLabel, String[] args) {
    3. Player player = ((Player)sender);
    4. String PlayerName = player.getName();
    5. if (commandLabel.equalsIgnoreCase("die") || player.hasPermission("cuicide.die")) {
    6. player.setHealth(0);
    7. this.getServer().broadcastMessage(PlayerName + " just did Suicide!");
    8. return true;
    9. }/* else if (commandLabel.equalsIgnoreCase("d") && player.hasPermission("cuicide.die")){
    10. player.setHealth(0);
    11. this.getServer().broadcastMessage(PlayerName + " just did Suicide!");
    12. return true; */ // Optional
    13. }else {
    14. player.sendMessage("WHY YOU TRY AND DO SUICIDE?");
    15. }
    16. return true;
    17. }
    18.  

    ^^^^ Cancel that, used to an older version how i did it.

    Also, to log startup and shutdown in the server.log, use this;

    Code:java
    1.  
    2. log.info("Cuicide has been enabled!");
    3.  


    It just works better :)
    Just don't forget to add the logger

    Code:java
    1.  
    2. Logger log = Logger.getLogger("Minecraft");
    3.  
     
  28. Offline

    theguynextdoor

    Replace your command with this

    Code:java
    1. this.getCommand("die").setExecutor(new CommandExecutor() {
    2.  
    3. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    4. Player player = ((Player) sender);
    5. if (cmd.getName().equalsIgnoreCase("die")) {
    6. if (player.hasPermission("Cuicide.die")) {
    7. player.setHealth(0);
    8. Bukkit.getServer().broadcastMessage("PlayerName just did Suicide!");
    9. return true;
    10. }
    11. else {
    12. player.sendMessage("WHY YOU TRY AND DO SUICIDE?");
    13. }
    14. }
    15. return false;
    16. }
    17. });


    Tell me what happens, and if you want explaining, please ask.
     
Thread Status:
Not open for further replies.

Share This Page