Please Help Player Variable Issues!

Discussion in 'Plugin Development' started by Mortal_Wombat, Jul 12, 2014.

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

    Mortal_Wombat

    Hello everybody! I am fairly new at bukkit development! And i need some help.


    package Me.Mortal;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Kits extends JavaPlugin {

    public void onEnable(){
    Bukkit.getServer().getLogger().info("Kits On");

    }

    public void onDisable() {
    Bukkit.getServer().getLogger().info("Kits Off");
    }
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String args[]) { //Only need 1 boolean for commands
    Player player = (Player) sender;
    if(commandLabel.equalsIgnoreCase("basic")){
    ItemStack Apple = new ItemStack(Material.APPLE, 5);
    ItemStack Stonesword = new ItemStack(Material.STONE_SWORD, 1);
    ItemStack StoneAxe = new ItemStack(Material.STONE_AXE, 1);
    PlayerInventory pi = new player.getInventory(); <-- error is here read below
    pi.setItem(0,Apple);
    pi.setItem(1,Stonesword);
    pi.setItem(2,StoneAxe);
    player.sendMessage(ChatColor.AQUA + "You got your basic kit!");
    }
    return true;
    }
    }

    It says and i quote"player cannot be resolved to a type". Please help me <3 for anyone who does :DDDD.
     
  2. Offline

    simolus3

    Found the mistake:
    "new" creates a new object into the servers ram, but, because the player is already existing, you probably don't want to do that. I assume you want to get the players inventory (which also exists, no need to create something). You have casted the sender to a player before (Player player = (Player)sender), so you can use that: PlayerInventory pi = player.getInventory(); There you go, ecplise shouldn't give you an error anymore. But if you want to chare your plugin, some admins might tell you that they got an error because you don't know if the sender is a player. To find that out, put this before your Player player = (Player)sender;
    Code:java
    1. if (!(sender instanceof Player)) {
    2. //The sender isn't a player...
    3. sender.sendMessage("Only players can get items!");
    4. }
     
  3. Offline

    TheMcScavenger

    To expand on what simolus3 says, you have to check if the sender is a player before actually casting it to a player. If you just assume something is a player, and it's not, it'll give you an error. This is the same for many other things, such as Entities / Players (in Events), Strings / Integers (when casting a string to an integer, and it isn't an integer), etc.

    Before adding any casts anywhere, ensure the object you're getting is the right type. You can do this by either looking at what the method returns (in Eclipse / JavaDocs), or by simply using an if-statement, and checking if it is the right type.
     
  4. Offline

    Mortal_Wombat

    Ok thank you guys I was used to typing new lol making all those ItemStacks and all that i just typed new again and for some reason was like yeah that's right. So thanks so much! :D <3
     
Thread Status:
Not open for further replies.

Share This Page