Simple Heal Command

Discussion in 'Plugin Development' started by blt, Mar 4, 2018.

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

    blt

    So I just got started in Java about a week ago and I'm having a bit of trouble with this simple heal command I'm trying to make. Don't worry about the package first line.

    Code:
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import net.md_5.bungee.api.ChatColor;
    
    public class MyFirstPlugin extends JavaPlugin {
       
        public void onEnable() {
            getLogger().info("Plugin has been enabled");
        }
    
        public void onDisable() {
            getLogger().info("Plugin has been disabled");
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String [] args) {
           
            if (cmd.getName().equalsIgnoreCase("heal") && sender instanceof Player) {
                if (sender instanceof Player) {
                    if(cmd.getName().equalsIgnoreCase("heal"))
                    if (!sender.hasPermission("core.heal"));
                        Player player = (Player) sender;
                        // DO NOT player.setHealth(20.0);
                        player.sendMessage(ChatColor.DARK_GRAY + "[*]" + ChatColor.GRAY + "No permission.");
                }
                    else if (sender.hasPermission("core.heal"));
                        Player player = (Player) sender;
                        player.setHealth(20.0);
                        player.sendMessage(ChatColor.DARK_GREEN + "[*]" + ChatColor.GREEN + "Your health has been restored");
                    return true;
        }
    }
     
  2. Online

    timtower Administrator Administrator Moderator

    @blt Remove the onEnable and onDisable.
    And what is the exact issue?
    And please use the Bukkit Chatcolor, not the spigot one.
     
  3. Offline

    blt

    When I try to load this into my server, it does not appear to be in the list of /plugins.

    It may have something to do with the code, I'm just not entirely sure.

    What is the Bukkit version of the ChatColor importation?
     
  4. Online

    timtower Administrator Administrator Moderator

    @blt Then please post your full server log using http://pastebin.com
    And remove the import, it will complain that it needs an import, then use the bukkit one
     
  5. Offline

    blt

    https://pastebin.com/zg6gr75c

    Note: TutorialPlugin or Tutorial1.jar is not the plugin in this case. Tutorial2 is what I am in the need to work.
     
  6. Online

    timtower Administrator Administrator Moderator

    @blt You are not exporting the plugin.yml
     
  7. Offline

    blt

    Ah, yeah realized that before. Thanks though!

    I added a plugin.yml and it still doesn't seem to work.

    Code:
    name: Heal
    version: 1.0
    main: me.bukkit.[censored].Main
    description: Heal!
    
    commands:
        heal:
            usage: /<command>
     
  8. Offline

    AntonioC94

    @blt You need to create the plugin.yml with the name, version, commands, author , etc
     
  9. Online

    timtower Administrator Administrator Moderator

    @blt Is it in the jar root?
    Please post your new log.
     
  10. Offline

    blt

    upload_2018-3-5_16-30-16.png


    https://hastebin.com/homovabose.md

    upload_2018-3-5_16-32-27.png

    Would you be so kind to send me a proper plugin.yml? Not exactly sure how I would set something like that up. Had a friend make my plugin.yml
     
  11. Online

    timtower Administrator Administrator Moderator

    @blt Your plugin.yml is fine. You are just not exporting it.
    Do you know Java?
     
  12. Offline

    blt

    I'm very new to it, which is probably why I'm not correctly exporting it. What are the steps to export the plugin.yml?
     
  13. Offline

    Concube

    He says in his post he just got started a week ago.
     
  14. Offline

    blt

    Nevermind, figured out how.

    Changed my main class to Main and fixed code errors.

    The only issue I am experiencing now is this permission thing, where if I enter /heal it will heal me, say I have access and then continue with "No permission".

    For this, installed PermissionsEx and am trying this now, still doesn't work. What must I be doing wrong?
     
  15. Online

    timtower Administrator Administrator Moderator

    You also have dead if statements, those are worse.
    Could you follow a couple java tutoriala first? Will help you a lot when making plugins.
     
    Zombie_Striker likes this.
  16. Offline

    blt

    I've looked almost everywhere and can't really find the perfect tutorial for what I need.

    To sum it up now, when I enter /heal regardless if I have core.heal (permission needed to /heal), it always says that my health has been restored (which is what happens if you do have the permission, if you don't it shouldn't heal you).

    upload_2018-3-6_21-36-55.png

    Code:
    package me.bukkit.pratal;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin {
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String [] args) {
           
            if (cmd.getName().equalsIgnoreCase("heal") && sender instanceof Player) {
                if (sender instanceof Player) {
                    if(cmd.getName().equalsIgnoreCase("heal"))
                        if (sender.hasPermission("core.heal"));
                            Player player = (Player) sender;
                            player.setHealth(20.0);
                            player.sendMessage(ChatColor.DARK_GREEN + "[*]" + ChatColor.GREEN + " Your health has been restored");
                            return true;
                } 
           
                        else if (!sender.hasPermission("core.heal"));
                            Player player = (Player) sender;
                            player.sendMessage(ChatColor.DARK_GRAY + "[*]" + ChatColor.GRAY + " No permission.");
                            return false;
                }
            return true;
        }
    }
    upload_2018-3-6_21-39-48.png

    upload_2018-3-6_21-41-44.png

    At this point I've been sending so much code to others for help so it's not even worth censoring my in-game name out anymore.
     
    Enderkries2011 likes this.
  17. Online

    timtower Administrator Administrator Moderator

    @blt You don't need specific tutorials, you just need a bunch.
    You have a semicolon after the condition of an if statement what makes it useless
     
  18. Offline

    AdamDev

    @blt
    Also your missing a { where it's testing for the heal command. I'm pretty sure it doesn't matter but it would be recommended to get used to adding those.

    Make sure that in plugin.yml that each line that you add is three spaces from the previous line:
    Code:
    commands:
    123commandname:
          123usage: /<command>
    etc...
    
    Permissions are not needed in the plugin.yml but that's totally up to you but make sure its not spaced out like that. Make it like "commands:"

    Here's a little java for you:
    - If statements don't have a ";" at the end of them. They have a set of braces ({} <- these)after them.
    - Make an onEnable() since this is in your main class and register your command.
     
Thread Status:
Not open for further replies.

Share This Page