How do I create an event in an onCommand?

Discussion in 'Plugin Development' started by TheDaannn, Apr 16, 2020.

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

    TheDaannn

    So I want to learn how to put an event in an onCommand.



    (I need the "speler1" & "speler2" variables in the event, so I want to learn this.)
    onCommand Class:

    Code:
    package me.thedaann.kgsumoevent.commands;
    
    import me.thedaann.kgsumoevent.Main;
    import org.bukkit.Bukkit;;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.Plugin;
    
    public class SumoJoinCommand implements Listener, CommandExecutor {
        private Plugin plugin = Main.getPlugin(Main.class);
    
        @EventHandler
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    
            Player speler1 = Bukkit.getServer().getPlayer(args[0]);
            Player speler2 = Bukkit.getServer().getPlayer(args[1]);
            Player p = (Player) sender;
    
            if (p.getPlayer().hasPermission("sumo.join")) {
                p.sendMessage("&cJe hebt geen permissie's voor dit command!");
                return true;
            }
                if (plugin.getConfig().getConfigurationSection("spawnpoint1") == null) {
                    p.sendMessage("§cJe hebt nog geen spawnpoint gezet! Doe dit doormiddel van: /spawnpoint[1/2]");
                    return true;
                }
    
                if (args.length == 0) {
                    p.sendMessage("§c§lGebruik: /sumo-join <naam1> <naam2>");
                    return true;
                }
                if (args.length == 1) {
                    p.sendMessage("§c§lGebruik: /sumo-join <naam1> <naam2>");
                    return true;
                }
                if (args.length == 2) {
                        World w1 = Bukkit.getServer().getWorld(plugin.getConfig().getString("spawnpoint1.world"));
                        World w2 = Bukkit.getServer().getWorld(plugin.getConfig().getString("spawnpoint2.world"));
                        double x1 = plugin.getConfig().getDouble("spawnpoint1.x");
                        double y1 = plugin.getConfig().getDouble("spawnpoint1.y");
                        double z1 = plugin.getConfig().getDouble("spawnpoint1.z");
                        double x2 = plugin.getConfig().getDouble("spawnpoint2.x");
                        double y2 = plugin.getConfig().getDouble("spawnpoint2.y");
                        double z2 = plugin.getConfig().getDouble("spawnpoint2.z");
                        speler1.teleport(new Location(w1, x1, y1, z1));
                        speler2.teleport(new Location(w2, x2, y2, z2));
                        return true;
                    }
    
    
    
            return true;
    
    
        }
    
    
    
    }
    
    ^^ I want a PlayerMoveEvent in this class.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Apr 16, 2020
  2. Online

    timtower Administrator Administrator Moderator

    @TheDaannn PlayerMoveEvent to tell what?
    You are already getting teleport events.
     
  3. Offline

    Strahan

    For the sake of clarity, it's better not to glue them together. I'd make a separate class for the move event.

    Also:
    Code:
    Player speler1 = Bukkit.getServer().getPlayer(args[0]);
    Player speler2 = Bukkit.getServer().getPlayer(args[1]);
    Player p = (Player) sender;
    You should not cast sender to Player without first verifying it is a Player. You also should be validating the two players that you get from the args, since if the player makes a typo and you attempt to call a method on those player objects the plugin will crash. You also are putting the cart ahead of the horse; you use two elements of the args array here, but only later check the length of the array. Checking length should be first.

    Code:
    if (p.getPlayer().hasPermission("sumo.join")) {
    p is already a Player, do not call getPlayer() against it.

    Code:
    p.sendMessage("&cJe hebt geen permissie's voor dit command!");
    Do not embed the color character, use the ChatColor enum. That's why it exists.

    Code:
    World w1 = Bukkit.getServer().getWorld(plugin.getConfig().getString("spawnpoint1.world"));
    Like the players, you need to validate that w1 is correct and not null because the config could have a typo or be missing spawnpoint1.world. For the same reason, you should be validating the config return first as well to ensure it too didn't return null.

    Code:
    double x1 = plugin.getConfig().getDouble("spawnpoint1.x");
    double y1 = plugin.getConfig().getDouble("spawnpoint1.y");
    double z1 = plugin.getConfig().getDouble("spawnpoint1.z");
    double x2 = plugin.getConfig().getDouble("spawnpoint2.x");
    double y2 = plugin.getConfig().getDouble("spawnpoint2.y");
    double z2 = plugin.getConfig().getDouble("spawnpoint2.z");
    This is a big waste of time. Location implements ConfigurationSerializable, so you can just write Location directly to the config and read it back with FileConfiguration#getLocation. Saves you a lot of typing, and stores more information than your manual serialization you have here.
     
Thread Status:
Not open for further replies.

Share This Page