Solved How to /smite players?

Discussion in 'Spigot Plugin Development' started by JParthur, May 26, 2018.

  1. Hello,
    I'm a beginner java developer,
    and I want to know how to create the command /smite <player> using java?
    This command summon a Lightning on the player.
    Thank you for your help!
  2. Serializator


    What you first want to do is learn how to create a command, there are plenty of resources online to help you with that.
    Once you know how to do that you can create your command "/smite" with a single argument that is the player you want to smite ("<player>").

    As you now know accessing the arguments of your command is done by using the array of strings (String[]) named args (or whatever you named it) in your CommandExecutor#onCommand(CommandSender, Command, String, String[]), but you can't do anything with just a name so you have to get the Player object that is associated with that name using Bukkit#getPlayerExact(String).

    Once you have the Player object from the name you can use this to get the World object the player is in, this is done by calling Player#getWorld() which returns a World object.

    Once you have the World object, you can use this to spawn the lightning strike in the world the player is in at the specific location the player is at using World#strikeLightning(Location).

    What the argument to World#strikeLightning(Location) means is where the lightning strike will spawn, this will in your case be the location the player is at which you can get by calling Player#getLocation().

    ^ refactored the explanation because of @Luligabi his suggestion of using World#strikeLightning(Location)

    If you don't understand most of what I talked about above you might want to consider starting with learning the basics of Java before jumping into the Spigot API and plugins, not as a roast towards you or something but just some friendly advice.

    Hopefully this is enough information for you to get started.
    If you need any more information, just say the words.

    Create a Simple Command (Wiki)
    #2 Serializator, May 26, 2018
    Last edited: May 26, 2018
    • Like Like x 1
    • Informative Informative x 1
  3. The way would be using Command Arguments.

    Check this tutorial for example usage.

    To smiting the selected player, you would do this:

    Code (Java):

    //Get player location (x, y, z, world)
    Location ploc = p.getLocation();
    //Summon an lightning bolt at ploc

    • Like Like x 1
  4. I'll try,think you all for your help! :coffee: