New Plugin... Code is giving an error, help.

Discussion in 'Plugin Development' started by NathanTheDragon, Feb 15, 2015.

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

    NathanTheDragon

    Hey guys!

    I want to see if anyone can help me, I'm still learning to code with bukkit, but for some reason I cannot use the /feed command. I'm guessing it's not picking it up. I can do /heal and /h, but my second command I have coded does nothing, when I type it it just brings nothing.

    What's wrong with my code? Thanks.

    Code:
    http://pastebin.com/HmTjfkSw
     
  2. Remove this "if (player.hasPermission("sethealth.heal"))"
    It does nothing,

    also your section "for (PotionEffect effect : target.getActivePotionEffects())"

    has no {}

    and also same problem with "if (player.hasPermission("sethealth.feed"))" it does nothing

    You've over complicated this a lot, give me a sec I'll try to clean it up

    This should run better: http://pastebin.com/uYkR0Wva

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
    Last edited by a moderator: Feb 15, 2015
  3. Offline

    Code0

  4. Offline

    Tecno_Wizard

    The spoonfeeding is real. Come on, guys.
     
    bwfcwalshy likes this.
  5. Offline

    NathanTheDragon

    @danieljharris_ Thanks, the code is cleaner, but Tecno_Wizard is right, I'm still confused why the feed section wasn't working. Was it because of something missing or what?

    Code does work as well too.
     
    Last edited: Feb 15, 2015
  6. Offline

    teej107

    Might be useful to tag @danieljharris_
     
    NathanTheDragon likes this.
  7. Offline

    nverdier

    *Facepalm*
     
  8. @NathanTheDragon Yeah you had some brackets '{}' missing and it was all laid out so that your second command wouldn't function properly.
     
    Last edited by a moderator: Feb 15, 2015
  9. Offline

    Tecno_Wizard

    @NathanTheDragon, let me go through the changes step by step.

    Code:
    package me.NathanTheDragon.sethealth;
    import java.util.logging.Logger;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffectType;
    public class SetHealth extends JavaPlugin
    {
        public final Logger logger = Logger.getLogger("Minecraft");
        public static SetHealth plugin;
    
        public void onDisable()
        {
                PluginDescriptionFile pdfFile = this.getDescription();
                this.logger.info("[" + pdfFile.getName() + "]" + " has been Disabled! Why u leave me D:!");
        }
    
        public void onEnable()
        {
                PluginDescriptionFile pdfFile = this.getDescription();
                this.logger.info("[" + pdfFile.getName() + "]" + " Version " + pdfFile.getVersion() + " has successfully been Enabled! Awesome!");
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
                Player player = (Player) sender;
                if (args.length == 0)
                {
                        if (cmd.getName().equalsIgnoreCase("heal") && player.hasPermission("sethealth.heal"))
                        {
                            player.setHealth(20);
                            player.setFireTicks(0);
                            player.setFoodLevel(20);
                            player.sendMessage(ChatColor.WHITE + "[" + ChatColor.GREEN + "Health" + ChatColor.WHITE + "]" + ChatColor.GOLD + " " + ChatColor.BOLD + "You have been healed!");
                   
                            return true;
                        }
                        if(cmd.getName().equalsIgnoreCase("feed") && player.hasPermission("sethealth.feed"))
                        {
                            player.setFoodLevel(20);
                            player.removePotionEffect(PotionEffectType.HUNGER);
                            player.sendMessage(ChatColor.WHITE + "[" + ChatColor.GREEN + "Health" + ChatColor.WHITE + "]" + ChatColor.GOLD + " " + ChatColor.BOLD + "You have been fed!");
                            return true;
                        }
                }
            return false;
        }
    }
    That's the old code. (I made even more changes to this, i did not agree with some of the things @danieljharris_ did, no offense. The class was still completely broken)


    First, you were getting the logger of "Minecraft". This is an extremely bad practice that many youtubers have unfortunately.
    The correct way to do this is to declare the variable at the global spec, then assign it in the onEnable by fetching the Bukkit logger.

    Logger log;

    //in onEnable
    log = Bukkit.getLogger();

    Then the concatenation. Holy crap was that bad.
    The way to prevent the foo + " " + foo + " " + foo + " " + foo is by using String.format.
    Lets say that all of these were string variables (Yes, chatcolor.white and all that is a string)
    You would do log.info(String.format("%s %s %s %s", foo, foo, foo, foo));
    This has the same output. See the JavaDoc for more explanation on that.

    Plugin manager was never used, and I have no idea why it was there. I deleted it.

    For performance, a lot of the if... if... if... were changed to else if

    If a console was to run this, it would crash.
    To make sure that before you cast a player to a sender that it is actually a sender, use the instanceof.
    Basically, if(sender instanceof Player){
    // now cast and do code
    }
    This is checking to see if the sender class inherits from the Player interface. This is heavily reliant on polymorphism. (If you're programming, you should know what that is.)

    for the check of heal, you had 2 if statements checking the same thing, not exactly sure why.

    Some of your brackets also did not line up (One was missing). This is a hard thing to check in eclipse, I understand. Personally, I switched to IntelliJ about 2 months ago and I will never use eclipse again. It gives indicators to whitespaces and indentation. All of those brackets are lined up.

    I also statically imported ChatColor so you do not need ChatColor. before every enum. Nice right?

    Now I have not directly tested this, the permission for feed might not be correct, but it should work if you fix that.
    If you have any issues, just tag me.

    Code:
    package me.NathanTheDragon.sethealth;
    
    import java.util.logging.Logger;
    import org.bukkit.Bukkit;
    import static org.bukkit.ChatColor.*;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class SetHealth extends JavaPlugin{
        public final Logger logger;
        public static SetHealth plugin;
    
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(String.format("[%s] has been Disabled! Why u leave me D:!", pdfFile.getName()));
        }
    
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(String.format("[%s] Version %s has successfully been Enabled! Awesome!", pdfFile.getName(), pdfFile.getVersion()));
             logger = Bukkit.getLogger();
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (sender instanceof Player) {
                Player player = (Player) sender;
                if (cmd.getName().equalsIgnoreCase("heal") || cmd.getName().equalsIgnoreCase("h")) {
                    if (player.hasPermission("sethealth.heal")) {
                        if (args.length == 0) {
                            player.setHealth(20);
                            player.setFireTicks(0);
                            player.setFoodLevel(20);
                            for (PotionEffect effect : player.getActivePotionEffects())
                                player.removePotionEffect(effect.getType());
                            player.sendMessage(String.format("%s[%sHealth%s]%s %sYou have been healed!", WHITE, GREEN, WHITE, GOLD, BOLD));
                            return true;
                        }
                        Player target = Bukkit.getServer().getPlayer(args[0]);
                        if (target == null) {
                            player.sendMessage(String.format("%s[%sHealth%s]%s %sThe Player isn't on the server!", WHITE, GREEN, WHITE, RED, BOLD));
                            return true;
                        }
                        // established target, applying
                        target.setHealth(20);
                        target.setFireTicks(0);
                        target.setFoodLevel(20);
                        for (PotionEffect effect : target.getActivePotionEffects())
                            target.removePotionEffect(effect.getType());
                        target.sendMessage(String.format("%s[%sHealth%s]%s %sYou have been healed by: %s%s!", WHITE, GREEN, WHITE, GOLD, BOLD, RED, player.getName()));
                        player.sendMessage(String.format("%s[%sHealth%s]%s %sYou have healed: %s%s!", WHITE, GREEN, WHITE, GOLD, BOLD, RED, target.getName()));
                        return true;
                    }
                } else if (cmd.getName().equalsIgnoreCase("feed")) {
                    if (player.hasPermission("sethealth.feed")) if (player.hasPermission("sethealth.*")) {
                        if (args.length == 0) {
                            player.setFoodLevel(20);
                            player.removePotionEffect(PotionEffectType.HUNGER);
                            player.sendMessage(String.format("%s[%sHealth%s]%s %sYou have been fed!", WHITE, GREEN, WHITE, GOLD, BOLD));
                            return true;
                        }
                        Player target = Bukkit.getServer().getPlayer(args[0]);
                        if (target == null) {
                            player.sendMessage(String.format("%s[%sHealth%s]%s %sThe Player isn't on the server!", WHITE, GREEN, WHITE, RED, BOLD));
                            return true;
                        }
                        target.setFoodLevel(20);
                        target.removePotionEffect(PotionEffectType.HUNGER);
                        target.sendMessage(String.format("%s[%sHealth%s]%s %sYou have been fed by: %s%s!", WHITE, GREEN, WHITE, GOLD, BOLD, RED, player.getName()));
                        player.sendMessage(String.format("%s[%sHealth%s]%s %sYou have fed: %s%s!", WHITE, GREEN, WHITE, GOLD, BOLD, RED, target.getName()));
                        return true;
                    }
                }
            }
            return false;
        }
    }
    Edit:
    I've edited this almost 3 times not finding mistakes, lol. Seriously, if it doesn't work, just tell me.
     
    Last edited: Feb 16, 2015
    danieljharris_ and tomudding like this.
Thread Status:
Not open for further replies.

Share This Page