First Plugin Help.

Discussion in 'Plugin Development' started by moonjokes, May 28, 2012.

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

    moonjokes

    Hi. I need help with this plugin I'm making.

    I'm new to java and this is my first plugin, but it has some problems.

    First of all when I type /heal /heal shows as well as the other text that should show. Confused as to why.

    Second I want to display a message when a player is not online and someone tries to heal them (not sure where else statement would go?)

    Third I want to display a different message when a player is > 18 health.

    Thanks in advance for any help.

    Here is the code.

    Yml
    Code:
    name: Test
    main: com.yourName.Test.Test
    version: 0.1
    depend: [Vault]
    commands:
      heal:
        description: Heals player for money
        usage: /heal
    
    Main class
    Code:
    package com.yourName.Test;
     
    import java.util.logging.Logger;
     
    import net.milkbowl.vault.Vault;
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.economy.EconomyResponse;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Test extends JavaPlugin {
     
        private static final Logger log = Logger.getLogger("Minecraft");
        public static Economy econ = null;
     
        @Override
        public void onDisable() {
            log.info(String.format("[%s] Disabled Version %s", getDescription().getName(), getDescription().getVersion()));
        }
     
        @Override
        public void onEnable() {
            if (!setupEconomy()) {
                log.severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
                getServer().getPluginManager().disablePlugin(this);
                return;
            }
        }
     
        private boolean setupEconomy() {
            if (getServer().getPluginManager().getPlugin("Vault") == null) {
                return false;
            }
            RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
            if (rsp == null) {
                return false;
            }
            econ = rsp.getProvider();
            return econ != null;
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            Player player = (Player) sender;
     
            if (commandLabel.equalsIgnoreCase("heal") && player.getHealth() <= 18) {
                if (args.length == 0) {
                    player.setHealth(player.getHealth() + 4);
                    EconomyResponse r = econ.withdrawPlayer(player.getName(), 20.00);
                    if (r.transactionSuccess()) {
                        sender.sendMessage(String.format("You spent %s to heal", econ.format(r.amount)));
                    }
                } else if (args.length == 1) {
                    if (player.getServer().getPlayer(args[0]) != null) {
                        Player targetPlayer = player.getServer().getPlayer(args[0]);
                        targetPlayer.setHealth(targetPlayer.getHealth() + 4);
                        player.sendMessage(ChatColor.GREEN + "Healed");
                        EconomyResponse r = econ.withdrawPlayer(player.getName(), 20.00);
                        if (r.transactionSuccess()) {
                            sender.sendMessage(String.format("You spent %s to heal %s", econ.format(r.amount), targetPlayer));
                        }else player.sendMessage(ChatColor.RED + "Player is not online!");
                    }
                } 
            }
            return false;
        }
    }
    
     
  2. Offline

    Nitnelave

    When you return false on a command, it means that the user entered wrong arguments, so it will display the usage section of your command, in this case "/heal".
    Be careful how you nest your tests, because as I see it, for now a player cannot heal someone else if he is himself healthy. You should put the first player.getHealth <= 18 with the args.length == 0
    To see if a player is online, I think you can do targetPlayer.isOnline().

    Actually, to answer your third question, a better way would be to put the player.getHealth <= 18 inside the args.length == 0, and then you can do an "else" for that test, to tell him he is already healthy.

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

    moonjokes

    Thanks. I will work on fixing some of this in a bit. Any more suggestions are very welcome. As I said I'm new to java.

    Got the already healthy part working but I still need to figure out how to do the player is not online.

    Code:
    package com.yourName.Test;
     
    import java.util.logging.Logger;
     
    import net.milkbowl.vault.Vault;
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.economy.EconomyResponse;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Test extends JavaPlugin {
     
        private static final Logger log = Logger.getLogger("Minecraft");
        public static Economy econ = null;
     
        @Override
        public void onDisable() {
            log.info(String.format("[%s] Disabled Version %s", getDescription().getName(), getDescription().getVersion()));
        }
     
        @Override
        public void onEnable() {
            if (!setupEconomy()) {
                log.severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
                getServer().getPluginManager().disablePlugin(this);
                return;
            }
        }
     
        private boolean setupEconomy() {
            if (getServer().getPluginManager().getPlugin("Vault") == null) {
                return false;
            }
            RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
            if (rsp == null) {
                return false;
            }
            econ = rsp.getProvider();
            return econ != null;
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            Player player = (Player) sender;
            
            if (commandLabel.equalsIgnoreCase("heal") ) {
                if (args.length == 0 && player.getHealth() <= 18) {
                    player.setHealth(player.getHealth() + 4);
                    EconomyResponse r = econ.withdrawPlayer(player.getName(), 20.00);
                    if (r.transactionSuccess()) {
                        sender.sendMessage(String.format("You spent %s to heal", econ.format(r.amount)));
                    }
                } else if (args.length == 1 ) {
                    if (player.getServer().getPlayer(args[0]) != null) {
                        Player targetPlayer = player.getServer().getPlayer(args[0]);
                        if (targetPlayer.getHealth() <= 18){
                        targetPlayer.setHealth(targetPlayer.getHealth() + 4);
                        player.sendMessage(ChatColor.GREEN + "Healed");
                        EconomyResponse r = econ.withdrawPlayer(player.getName(), 20.00);
                        if (r.transactionSuccess()) {
                            sender.sendMessage(String.format("You spent %s to heal %s", econ.format(r.amount), targetPlayer));
                        }else player.sendMessage(ChatColor.RED + "Player is not online!");
                    } 
                }   
            }else player.sendMessage(ChatColor.RED + "Already Healthy");
            
        }return true;
    }
    }
    [\Code]
    Got the player is not online but I want to send a message to the player trying to heal the other player saying that this player is already healthy. Please help.

    Updated code
    Code:
    package com.yourName.Test;
     
    import java.util.logging.Logger;
     
    import net.milkbowl.vault.Vault;
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.economy.EconomyResponse;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Test extends JavaPlugin {
     
        private static final Logger log = Logger.getLogger("Minecraft");
        public static Economy econ = null;
     
        @Override
        public void onDisable() {
            log.info(String.format("[%s] Disabled Version %s", getDescription().getName(), getDescription().getVersion()));
        }
     
        @Override
        public void onEnable() {
            if (!setupEconomy()) {
                log.severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
                getServer().getPluginManager().disablePlugin(this);
                return;
            }
        }
     
        private boolean setupEconomy() {
            if (getServer().getPluginManager().getPlugin("Vault") == null) {
                return false;
            }
            RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
            if (rsp == null) {
                return false;
            }
            econ = rsp.getProvider();
            return econ != null;
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            Player player = (Player) sender;
           
            if (commandLabel.equalsIgnoreCase("heal") ) {
                if (args.length == 0 && player.getHealth() <= 18) {
                    player.setHealth(player.getHealth() + 4);
                    EconomyResponse r = econ.withdrawPlayer(player.getName(), 20.00);
                    if (r.transactionSuccess()) {
                        sender.sendMessage(String.format("You spent %s to heal", econ.format(r.amount)));
                    }
                } else if (args.length == 1 ) {
                    if (player.getServer().getPlayer(args[0]) != null) {
                        Player targetPlayer = player.getServer().getPlayer(args[0]);
                        if (targetPlayer.getHealth() <= 18){
                        targetPlayer.setHealth(targetPlayer.getHealth() + 4);
                        player.sendMessage(ChatColor.GREEN + "Healed");
                        EconomyResponse r = econ.withdrawPlayer(player.getName(), 20.00);
                        if (r.transactionSuccess()) {
                            sender.sendMessage(String.format("You spent %s to heal %s", econ.format(r.amount), targetPlayer));
                        }
                    }
                } else player.sendMessage(ChatColor.RED + "Player is not online!");
            }else player.sendMessage(ChatColor.RED + "Already Healthy");
           
        }return true;
    }
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  4. Offline

    SnRolls

    if(targetPlayer.getHealth() == 20){
    player.sendMessage(ChatColor.RED+"Player is already healthy!");
    }

    this is what you meant to ?
     
  5. Offline

    Nitnelave

    add a "else player.sendMessage("Player is already healthy") " on the line above your "player is not online"
     
  6. Offline

    moonjokes

    This is my final code if anyone is interested in looking at it.

    It charges 5 dollars for each 1/2 heart healed and broadcasts a message to the player and the target how much was healed and how much was spent. It heals to full hearts no matter what. If the player has no money it will send a message telling them they don't have enough money to heal/heal a player.

    Going to learn how to do config next.
    If you have any helpful advice for configuration please let me know.

    Code:
    package com.yourName.Test;
     
    import java.util.logging.Logger;
     
    import net.milkbowl.vault.Vault;
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.economy.EconomyResponse;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Test extends JavaPlugin 
    {
     
        private static final Logger log = Logger.getLogger("Minecraft");
        public static Economy econ = null;
     
        @Override
        
        public void onDisable() 
        {
            log.info(String.format("[%s] Disabled Version %s", getDescription().getName(), getDescription().getVersion()));
        }
     
        @Override
        
        public void onEnable() 
        {
            if (!setupEconomy()) 
            {
                log.severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
                
                getServer().getPluginManager().disablePlugin(this);
                
                return;
            }
        }
     
        private boolean setupEconomy() 
        {
            if (getServer().getPluginManager().getPlugin("Vault") == null) 
            {
                return false;
            }
            RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
            
            if (rsp == null) 
            {
                
                return false;
            }
            econ = rsp.getProvider();
            
            return econ != null;
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) 
        {
            
            Player player = (Player) sender;
            
            if (commandLabel.equalsIgnoreCase("heal") ) 
            {
                if (args.length == 0 && player.getHealth() != 20) 
                {
                    EconomyResponse r = econ.withdrawPlayer(player.getName(), 5 * (20 - player.getHealth()));
                    
                    if (r.transactionSuccess()) 
                    {
                        sender.sendMessage(String.format("You spent %s to heal %s hearts", econ.format(r.amount), (20 - player.getHealth()) / 2));
                        
                        player.setHealth(player.getHealth() + 20 - player.getHealth());
                        
                    }else player.sendMessage("You don't have enough money to heal!");
                    
                }
                else if (args.length == 1 ) 
                {
                    if (player.getServer().getPlayer(args[0]) != null) 
                    {
                        Player targetPlayer = player.getServer().getPlayer(args[0]);
                        
                        if (targetPlayer.getHealth() !=20)
                        {
                        
                        EconomyResponse r = econ.withdrawPlayer(player.getName(), 5 * (20 - targetPlayer.getHealth()));
                        
                        if (r.transactionSuccess()) 
                        {
                            sender.sendMessage(String.format("You spent %s to heal %s", econ.format(r.amount), targetPlayer.getName()));
                            
                            targetPlayer.sendMessage(String.format("%s spent %s to heal you %s hearts! Make sure to thank them!",player.getName(), econ.format(r.amount), (20 - targetPlayer.getHealth()) / 2));
                            
                            targetPlayer.setHealth(targetPlayer.getHealth() + 20 - targetPlayer.getHealth());
                            
                        }
                           else player.sendMessage("You don't have enough money to heal another player!");
                    }
                        else player.sendMessage(ChatColor.RED + "That player is already healthy!");
                }
                    else player.sendMessage(ChatColor.RED + "Player is not online!");   
            }
                else player.sendMessage(ChatColor.RED + "You are already healthy!");
            
        }return true;
     
        }
     
    }
    
     
Thread Status:
Not open for further replies.

Share This Page