Solved Code works only works without variables

Discussion in 'Plugin Development' started by FireRoz, Mar 21, 2021.

  1. Offline

    FireRoz

    Hi, I have this piece of code
    Code:
    package commands;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class CommandTpHere implements CommandExecutor {
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if(!sender.hasPermission("savage.tphere")) {
                sender.sendMessage(ChatColor.RED + "I'm sorry, but you do not have permission to perform this command.");
            } else {
                if(args.length < 1) {
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&dUsage: &5/tphere &f<player>"));
                } else {
                    Player target = Bukkit.getServer().getPlayerExact(args[0]);
                    if(target != null) {
                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&dYou've teleported to &5" + target.getName() + "&d to &5you&d."));
                        Player p = (Player) sender;
                        target.teleport(p);
                        Bukkit.broadcast(ChatColor.translateAlternateColorCodes('&', "&7&o[" + p.getName() + "&7&o: &dteleported " + target.getName() + "&d to " + p.getName() + "&7&o]"), "savage.tp");
                    } else {
                        sender.sendMessage(ChatColor.RED + "No player matching " + ChatColor.YELLOW + target.getName() + ChatColor.RED + " online.");
                    }
                }
            }
    
    
    
    
    
            return true;
        }
    }
    
    and it doesn't work but in the other hand

    I have this code
    Code:
    package commands;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class CommandTpHere implements CommandExecutor {
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if(!sender.hasPermission("savage.tphere")) {
                sender.sendMessage(ChatColor.RED + "I'm sorry, but you do not have permission to perform this command.");
            } else {
                if(args.length < 1) {
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&dUsage: &5/tphere &f<player>"));
                } else {
                    Player target = Bukkit.getServer().getPlayerExact(args[0]);
                    if(target != null) {
                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&dYou've teleported to &5" + target.getName() + "&d to &5you&d."));
                        Player p = (Player) sender;
                        target.teleport(p);
                        Bukkit.broadcast(ChatColor.translateAlternateColorCodes('&', "&7&o[" + p.getName() + "&7&o: &dteleported " + target.getName() + "&d to " + p.getName() + "&7&o]"), "savage.tp");
                    } else {
                        sender.sendMessage(ChatColor.RED + "Target is offline.");
                    }
                }
            }
    
    
    
    
    
            return true;
        }
    }
    
    and it does work. The difference between
    sender.sendMessage(ChatColor.RED + "Target is offline");
    and sender.sendMessage(ChatColor.RED + "No player matching " + ChatColor.YELLOW + target.getName() + ChatColor.RED + " online."); is that the first one doesn't contain target.getName();

    Any idea why?

    EDIT: solved
    OfflinePlayer offp = (OfflinePlayer) Bukkit.getServer().getOfflinePlayer(args[0]); sender.sendMessage(ChatColor.RED + "No player matching " + ChatColor.YELLOW + offp.getName() + ChatColor.RED + " online.");
     
    Last edited: Mar 21, 2021

Share This Page