Solved Getting the name of a user into a message

Discussion in 'Spigot Plugin Development' started by Aleksander_043, Jul 15, 2018.

  1. Hi, im currently learning how to code spigot plugins. Currently im making a hello plugin. Basically the plugins just says hello, when you write /hello. I've managed to make the text in color, but i want it to get the name of the user.

    Right now it just says: Hello

    I want it to say Hello, <player>

    Here is the code:

    package me.Aleksander.SpigotPlugin.commands;

    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 HelloCommand implements CommandExecutor {

    public HelloCommand(me.Aleksander.SpigotPlugin.Main main){
    main.getCommand("hello").setExecutor(this);
    }

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if (!(sender instanceof Player)){
    sender.sendMessage("Only players can use this command!");
    return true;
    }
    Player p = (Player) sender;

    if (p.hasPermission("hello.use")){
    p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&bhello"));
    return true;
    } else{
    p.sendMessage("You do not have permission to use the hello command");
    }
    return false;
    }

    }
     
  2. Code (Java):
    p.sendMessage(ChatColor.AQUA+"Hello, "+p.getName());
    You can use sender.getName(), as well.
     
  3. ummmm p.getName()
     
  4. if (!(sender instanceof Player)){
    sender.sendMessage("Only players can use this command!");
    return true;
    }
    why not just use player because he already casted it
     
  5. Not that he really needs to cast it to begin with.
     
  6. Thanks for the help :), now for some reason it can't find the main class, but the main is right :/
     
  7. ummm main+ plugin.yml + console errors?

    1. plugin.yml is invalid
    2. Main doesn't extend JavaPlugin
     
  8. plugin.yml is valid

    name: hello
    version: 1.0
    main: me.Aleksander.SpigotPlugin.Main
    commands:
    hello:
    description: says hello
    usage: /hello

    Main extends JavaPlugin

    package me.Aleksander.SpigotPlugin;

    import org.bukkit.plugin.java.JavaPlugin;

    import me.Aleksander.SpigotPlugin.commands.HelloCommand;

    public class Main extends JavaPlugin {

    @Override
    public void onEnable() {
    new HelloCommand(this);

    }
    }


    Console errors

    [04:48:01 ERROR]: Could not load 'plugins\Hello.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: Cannot find main class `me.Aleksander.SpigotPlugin.Main'
    at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:80) ~[spigot.jar:git-Spigot-642f6d2-fbe3046]
    at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:129) ~[spigot.jar:git-Spigot-642f6d2-fbe3046]
    at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:327) ~[spigot.jar:git-Spigot-642f6d2-fbe3046]
    at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:248) [spigot.jar:git-Spigot-642f6d2-fbe3046]
    at org.bukkit.craftbukkit.v1_12_R1.CraftServer.loadPlugins(CraftServer.java:305) [spigot.jar:git-Spigot-642f6d2-fbe3046]
    at net.minecraft.server.v1_12_R1.DedicatedServer.init(DedicatedServer.java:205) [spigot.jar:git-Spigot-642f6d2-fbe3046]
    at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:545) [spigot.jar:git-Spigot-642f6d2-fbe3046]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_162]
    Caused by: java.lang.ClassNotFoundException: me.Aleksander.SpigotPlugin.Main
    at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.8.0_162]
    at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:152) ~[spigot.jar:git-Spigot-642f6d2-fbe3046]
    at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:100) ~[spigot.jar:git-Spigot-642f6d2-fbe3046]
    at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_162]
    at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_162]
    at java.lang.Class.forName0(Native Method) ~[?:1.8.0_162]
    at java.lang.Class.forName(Unknown Source) ~[?:1.8.0_162]
    at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:78) ~[spigot.jar:git-Spigot-642f6d2-fbe3046]
    ... 7 more
     
  9. After just re-exporting it a few times it somehow worked. Thanks alot for the help, really appreciated.