Anyone can help me to learn?

Discussion in 'Spigot Plugin Development' started by relampagorojo93, May 19, 2015.

  1. I'm a new java developer, and I'm learning from the plugins that I create. I post a plugin, HeroFake, and I'm editing the code to do it more better. I want to do a new update, but I'm searching for one think. I saw in posts that (sender instanceof Player) was used to do a command only for the console. I tryed to use that but I had a problem. The command can be executed by the user, but no by the console.

    This is the code:
    package me.relampagorojo93.teletransportar;

    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    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.PotionEffect;
    import org.bukkit.potion.PotionEffectType;

    public class Main extends JavaPlugin {
    public void onEnable() {
    PluginDescriptionFile pdfFile = this.getDescription();
    getConfig().options().copyDefaults(true);
    saveConfig();
    System.out.println("[" + pdfFile.getName() + "]" + " Version: " + pdfFile.getVersion());
    System.out.println("[" + pdfFile.getName() + "]" + " is enabled now");
    }

    public void onDisable() {
    PluginDescriptionFile pdfFile = this.getDescription();
    System.out.println("[" + pdfFile.getName() + "]" + " is disabled now");
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command,String label, String[] args) {
    Player p = (Player)sender;
    if(label.equalsIgnoreCase("herofaketp")) {
    if(p.hasPermission("herofake.tp")) {
    if(args.length == 1) {
    Player targetPlayer = p.getServer().getPlayer(args[0]);
    Location targetPlayerLocation = targetPlayer.getLocation();
    p.teleport(targetPlayerLocation);
    p.sendMessage(ChatColor.RED + "[" + ChatColor.GREEN + getConfig().getString("Prefix") + ChatColor.RED + "]" + ChatColor.GRAY + " " + getConfig().getString("Teleporting"));
    targetPlayer.sendMessage(ChatColor.RED + "[" + ChatColor.GREEN + getConfig().getString("Prefix") + ChatColor.RED + "]" + ChatColor.GRAY + " " + getConfig().getString("Tp"));
    }
    else if(args.length == 2) {
    Player targetPlayer = p.getServer().getPlayer(args[0]);
    Player targetPlayer1 = p.getServer().getPlayer(args[1]);
    Location targetPlayer1Location = targetPlayer1.getLocation();
    targetPlayer.teleport(targetPlayer1Location);
    targetPlayer.sendMessage(ChatColor.RED + "[" + ChatColor.GREEN + getConfig().getString("Prefix") + ChatColor.RED + "]" + ChatColor.GRAY + " " + getConfig().getString("Tp-to-you"));
    }
    else if(args.length >= 3) {
    p.sendMessage(ChatColor.RED + "[" + ChatColor.GREEN + getConfig().getString("Prefix") + ChatColor.RED + "]" + ChatColor.GRAY + " Don't put three players in the same command");
    }
    else if(args.length == 0) {
    p.sendMessage(ChatColor.RED + "[" + ChatColor.GREEN + getConfig().getString("Prefix") + ChatColor.RED + "]" + ChatColor.GRAY + " /herofaketp <Player> [Player2]");
    }
    }
    }
    else if(label.equalsIgnoreCase("cmdherofaketp")) {
    if(sender instanceof Player) {
    if(args.length == 1) {
    System.out.println("[" + getConfig().getString("Prefix") + "] You need to put two players in the command");
    }
    else if(args.length == 2) {
    Player targetPlayer = p.getServer().getPlayer(args[0]);
    Player targetPlayer1 = p.getServer().getPlayer(args[1]);
    Location targetPlayer1Location = targetPlayer1.getLocation();
    targetPlayer.teleport(targetPlayer1Location);
    System.out.println("[" + getConfig().getString("Prefix") + "] Manipuling...");
    targetPlayer.sendMessage(ChatColor.RED + "[" + ChatColor.GREEN + getConfig().getString("Prefix") + ChatColor.RED + "]" + ChatColor.GRAY + " " + getConfig().getString("Tp-to-you"));
    }
    else if(args.length >= 3) {
    System.out.println("[" + getConfig().getString("Prefix") + "] Don't put three players in the same command");
    }
    else if(args.length == 0) {
    System.out.println("[" + getConfig().getString("Prefix") + "] /herofaketp <Player> <Player2>");
    }
    }
    else if(p.hasPermission("herofake.cmd")) {
    p.sendMessage(ChatColor.RED + "[" + ChatColor.GREEN + getConfig().getString("Prefix") + ChatColor.RED + "]" + ChatColor.GRAY + " You can't use this command in game");
    }
    }
    else if(label.equalsIgnoreCase("herofakeattack")) {
    if(p.hasPermission("herofake.attack")) {
    if(args.length == 1) {
    PotionEffectType ceguera = PotionEffectType.BLINDNESS;
    PotionEffect effect = ceguera.createEffect(4800, 1);
    Player targetPlayer = p.getServer().getPlayer(args[0]);
    targetPlayer.addPotionEffect(effect, true);
    targetPlayer.sendMessage(ChatColor.RED + "[" + ChatColor.GREEN + getConfig().getString("Prefix") + ChatColor.RED + "]" + ChatColor.GRAY + " " + getConfig().getString("Attack"));
    }
    else if(args.length >= 2) {
    p.sendMessage(ChatColor.RED + "[" + ChatColor.GREEN + getConfig().getString("Prefix") + ChatColor.RED + "]" + ChatColor.GRAY + " Don't put two players in the same command");
    }
    else if(args.length == 0) {
    p.sendMessage(ChatColor.RED + "[" + ChatColor.GREEN + getConfig().getString("Prefix") + ChatColor.RED + "]" + ChatColor.GRAY + " /herofakeattack <player>");
    }
    }
    }
    else if(label.equalsIgnoreCase("cmdherofakeattack")) {
    if(sender instanceof Player) {
    if(args.length == 1) {
    PotionEffectType ceguera = PotionEffectType.BLINDNESS;
    PotionEffect effect = ceguera.createEffect(4800, 1);
    Player targetPlayer = p.getServer().getPlayer(args[0]);
    targetPlayer.addPotionEffect(effect, true);
    System.out.println("[" + getConfig().getString("Prefix") + "] Attacking... ");
    targetPlayer.sendMessage(ChatColor.RED + "[" + ChatColor.GREEN + getConfig().getString("Prefix") + ChatColor.RED + "]" + ChatColor.GRAY + " " + getConfig().getString("Attack"));
    }
    else if(args.length >= 2) {
    System.out.println("[" + getConfig().getString("Prefix") + "] Don't put two players in the same command");
    }
    else if(args.length == 0) {
    System.out.println("[" + getConfig().getString("Prefix") + "] /herofakeattack <player>");
    }
    }
    else if(p.hasPermission("herofake.cmd")) {
    p.sendMessage(ChatColor.RED + "[" + ChatColor.GREEN + getConfig().getString("Prefix") + ChatColor.RED + "]" + ChatColor.GRAY + " You can't use this command in game");
    }
    }
    else if(label.equalsIgnoreCase("herofakereload")) {
    if(p.hasPermission("herofake.reload")) {
    this.reloadConfig();
    p.sendMessage(ChatColor.RED + "[" + ChatColor.GREEN + getConfig().getString("Prefix") + ChatColor.RED + "]" + ChatColor.GRAY + " " + getConfig().getString("Reload"));
    }
    }
    else if(label.equalsIgnoreCase("cmdherofakereload")) {
    if(sender instanceof Player) {
    this.reloadConfig();
    System.out.println("[" + getConfig().getString("Prefix") + "] " + getConfig().getString("Reload"));
    }
    else if(p.hasPermission("herofake.cmd")) {
    p.sendMessage(ChatColor.RED + "[" + ChatColor.GREEN + getConfig().getString("Prefix") + ChatColor.RED + "]" + ChatColor.GRAY + " You can't use this command in game");
    }
    }
    else if(label.equalsIgnoreCase("herofakeversion")) {
    if(p.hasPermission("herofake.version")) {
    PluginDescriptionFile pdfFile = this.getDescription();
    p.sendMessage(ChatColor.RED + "[" + ChatColor.GREEN + getConfig().getString("Prefix") + ChatColor.RED + "]" + ChatColor.GRAY + " " + pdfFile.getVersion());
    }
    }
    else if(label.equalsIgnoreCase("cmdherofakeversion")) {
    if(sender instanceof Player) {
    PluginDescriptionFile pdfFile = this.getDescription();
    System.out.println("[" + getConfig().getString("Prefix") + "] " + pdfFile.getVersion());
    }
    else if(p.hasPermission("herofake.cmd")) {
    p.sendMessage(ChatColor.RED + "[" + ChatColor.GREEN + getConfig().getString("Prefix") + ChatColor.RED + "]" + ChatColor.GRAY + " You can't use this command in game");
    }
    }
    else if(label.equalsIgnoreCase("herofake")) {
    if(p.hasPermission("herofake.commands")) {
    p.sendMessage(ChatColor.RED + "[" + ChatColor.GREEN + getConfig().getString("Prefix") + ChatColor.RED + "]" + ChatColor.GRAY + " Commands: ");
    p.sendMessage(ChatColor.RED + "/herofaketp <Player> [Player2] " + ChatColor.GRAY + "(Do tp to a player or tp the player to you as Herobrine)");
    p.sendMessage(ChatColor.RED + "/herofakeattack <Player> " + ChatColor.GRAY + "(Blind a user during 1 minute)");
    p.sendMessage(ChatColor.RED + "/herofakeversion " + ChatColor.GRAY + "(Know the plugin's version)");
    p.sendMessage(ChatColor.RED + "/herofakereload " + ChatColor.GRAY + "(Reload the plugin)");
    }
    }
    else if(label.equalsIgnoreCase("cmdherofake")) {
    if(sender instanceof Player) {
    System.out.println("[" + getConfig().getString("Prefix") + "] Commands: ");
    System.out.println("/cmdherofaketp <Player> [Player2] (Do tp to a player or tp the player to you as Herobrine)");
    System.out.println("/cmdherofakeattack <Player> (Blind a user during 1 minute)");
    System.out.println("/cmdherofakeversion (Know the plugin's version)");
    System.out.println("/cmdherofakereload (Reload the plugin)");
    }
    else if(p.hasPermission("herofake.cmd")) {
    p.sendMessage(ChatColor.RED + "[" + ChatColor.GREEN + getConfig().getString("Prefix") + ChatColor.RED + "]" + ChatColor.GRAY + " You can't use this command in game");
    }
    }
    return false;
    }
    }

    And this is the error:
    [22:26:55] [Server thread/WARN]: Unexpected exception while parsing console command "cmdherofake"
    org.bukkit.command.CommandException: Unhandled exception executing command 'cmdherofake' in plugin HeroFake v1.2
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8.3.jar:git-Spigot-870264a-0a645a2]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.3.jar:git-Spigot-870264a-0a645a2]
    at org.bukkit.craftbukkit.v1_8_R2.CraftServer.dispatchCommand(CraftServer.java:646) ~[spigot-1.8.3.jar:git-Spigot-870264a-0a645a2]
    at org.bukkit.craftbukkit.v1_8_R2.CraftServer.dispatchServerCommand(CraftServer.java:632) [spigot-1.8.3.jar:git-Spigot-870264a-0a645a2]
    at net.minecraft.server.v1_8_R2.DedicatedServer.aN(DedicatedServer.java:405) [spigot-1.8.3.jar:git-Spigot-870264a-0a645a2]
    at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:369) [spigot-1.8.3.jar:git-Spigot-870264a-0a645a2]
    at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:651) [spigot-1.8.3.jar:git-Spigot-870264a-0a645a2]
    at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java:554) [spigot-1.8.3.jar:git-Spigot-870264a-0a645a2]
    at java.lang.Thread.run(Unknown Source) [?:1.7.0_79]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R2.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
    at me.relampagorojo93.teletransportar.Main.onCommand(Main.java:29) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.3.jar:git-Spigot-870264a-0a645a2]
    ... 8 more

    I saw people saying that is better first learning how to create with java than create plugins. This is my only way to learn how to create. Thank you.
     
  2. It can't be executed by the console because you cast the sender to a Player in this line
    Code (Text):
    Player p = (Player) sender;
    Which will cause errors if 'sender' isn't an instance of Player i.e it is the console.
    Always check the sender before casting it to a player.

    Edit: Actually, later on you do this, but you have already cast it so there's no point.
     
  3. That's already an ill advised idea. Learn Java first, then learn plugins. There are many sources: https://docs.oracle.com/javase/tutorial/index.html (Oracle made tutorials: who are responsible for Java).

    The error is really clear cut: you are casting the console to Player, which you CANNOT do.
    Code (Text):
    if(sender instanceof Player) {
        .....
    }
    That checks if the command sender is a Player as both Player and ConsoleCommandSender implement CommandSender. If it is true, then you can cast (as it means the command is coming from a player in game) but otherwise you will end up with the error that you did.

    Really do yourself a favour and read Java tutorials before diving into Plugins.
     
    • Agree Agree x 1
  4. I need to create a second onCommand with the console commands, no? but without the sender p, no?
     
  5. No.
    Simply use
    when referencing p as a player, and move
    Code (Text):
    Player p = (Player) sender;
    to inside that if statement.

    Also,
     
    • Agree Agree x 1
  6. Use linda.com to learn the Java Basic's. Get that down and then start on little plugins for a test server, gradually get bigger and bigger until you have pretty much got everything down. Lookup Bukkit Coding on youtube, there are tons of tutorials.