String and Int Error?

Discussion in 'Plugin Development' started by nubpro, Jul 9, 2011.

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

    nubpro

    The whole source code:
    Code:
    package net.nubpro.project.plugin.hp;
    
    import java.util.logging.Logger;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Hp extends JavaPlugin{
        private Player player;
        private static final Logger log = Logger.getLogger("Minecraft");
    
        public void onDisable() { log.info("[Health]Plugin had been disabled");
        }
    
        public void onEnable() { log.info("[Health]Plugin had been enabled");
        }
        public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args) {
            int health = player.getHealth();
            if ((sender instanceof Player)){
            if (cmd.getName().equalsIgnoreCase("hp"))
            {
            sender.sendMessage(health);
            }
            return false;
        }
            return false;
            }
    }
    A error occurs when I used "int"!
    See below:
    Code:
    int health = player.getHealth();
    A error occurs when using "int"
    It keep telling me to change "int" to string due sender.sendMessage(health); cannot support int

    Code:
            int health = player.getHealth();
            if ((sender instanceof Player)){
            if (cmd.getName().equalsIgnoreCase("hp"))
            {
            sender.sendMessage(health);
    
    So I decide to change to string but getHealth(); doesnt support string
    Code:
    String health = player.getHealth();
    It's making me MAD! :mad:
    Is there anyway to fix this?
     
  2. Offline

    ItsHarry

    String health = player.getHealth().toString();

    This is like very basic stuff, you should know this O_O.
     
  3. Offline

    nubpro

    lol not working.
    -.-''
     
  4. Offline

    krinsdeath

    int doesn't have a toString method inherited from a superclass. It's a primitive data type. If you want to cast it as a string (but retain its use as an int) cast it as Integer.

    Code:
    Integer health = player.getHealth();
    sender.sendMessage(health.toString());
    
    alternatively:
    int health = player.getHealth();
    sender.sendMessage("" + health);
    
     
  5. Offline

    nubpro

    What does Integer health means?
     
  6. Offline

    krinsdeath

    Integer is a type (just like int) that stores data, it just wraps the int in a class with some methods to utilize the data in more than just one way (ie as an int)

    the expression:
    Code:
    Integer health = player.getHealth();
    
    Means (simply) to create a variable named "health" of type "Integer," and assign it the value returned by "player.getHealth()"

    java.lang.Integer
     
  7. Offline

    nubpro

    Thanks it works!
     
  8. Offline

    badbh222

    @nubpro This seems to work for me:

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args){
    					
    	if (cmd.getName().equalsIgnoreCase("hp")){
    		if (sender instanceof Player){
    			Player player = (Player) sender;
    			int health = player.getHealth();
    			player.sendMessage(String.valueOf(health));
    			return true;
    		}
    	}
    	return false;
    }
    EDIT: Nevermind, you figured it out. ;)
     
  9. Offline

    nubpro

    I know there some problems with the return statements but I already figured out. ><
     
  10. Offline

    badbh222

    @nubpro I meant the "player.sendMessage(String.valueOf(health));" part... mainly. :D
     
  11. Offline

    nubpro

    :eek:
     
Thread Status:
Not open for further replies.

Share This Page