Solved Command Executor

Discussion in 'Plugin Development' started by Rowinvd, Mar 1, 2015.

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

    Rowinvd

    Can someone give me a little tutorial on how to create multiple classes for commands with these classes?
    Main, Command1, Command2.
    I already searched on the internet but i can figure out how it works.
     
  2. Offline

    Lazini

    There you go. This should help.
     
  3. Offline

    Rowinvd

    @Lazini 1 command is working, 1 not: /setlobby works, but /lobby not
    Main ( onEnable )
    Code:
            this.getCommand("setlobby").setExecutor(new CMDSetLobby(this));
            this.getCommand("lobby").setExecutor(new CMDLobby(this));
    CMDLobby:
    Code:
    package me.rowinvd;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    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.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class CMDLobby implements CommandExecutor {
        private final Main plugin;
    
        public CMDLobby(Main plugin) {
            this.plugin = plugin;
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if (!(sender instanceof Player)) {
                return true;
            }
            Player p = (Player) sender;
            PlayerInventory pi = p.getInventory();
            if (cmd.getName().equalsIgnoreCase("lobby")) {
                pi.clear();
                ItemStack lobbycompass = new ItemStack(Material.COMPASS, 1);
                ItemMeta getlobbycompass = lobbycompass.getItemMeta();
                getlobbycompass.setDisplayName(ChatColor.GREEN + "Game Selector");
                lobbycompass.setItemMeta(getlobbycompass);
                p.getInventory().addItem(lobbycompass);
                pi.setHelmet(new ItemStack(Material.GLASS, 1));
                pi.setChestplate(null);
                pi.setLeggings(null);
                pi.setBoots(null);
                p.setFireTicks(0);
                p.setHealth(20);
                p.setExp(0);
                p.setLevel(0);
                String worldName = plugin.getConfig().getString("spawn.world");
                if (worldName != null) {
                    World w = Bukkit.getServer().getWorld(plugin.getConfig().getString("lobby.world"));
                    double x = plugin.getConfig().getDouble("lobby.x");
                    double y = plugin.getConfig().getDouble("lobby.y");
                    double z = plugin.getConfig().getDouble("lobby.z");
                    float yaw = (float) plugin.getConfig().getDouble("lobby.yaw");
                    float pitch = (float) plugin.getConfig().getDouble("lobby.pitch");
                    p.teleport(new Location(w, x, y, z, yaw, pitch));
                    p.sendMessage(ChatColor.GREEN + "Welcome to the Lobby!");
                }
            }
            return true;
        }
    }
    CMDSetLobby:
    Code:
    package me.rowinvd;
    
    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 CMDSetLobby implements CommandExecutor {
        private final Main plugin;
    
        public CMDSetLobby(Main plugin) {
            this.plugin = plugin;
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if (!(sender instanceof Player)) {
                return true;
            }
            Player p = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("setlobby")) {
                if (sender.hasPermission("set.lobby")) {
                    plugin.getConfig().set("lobby.world", p.getLocation().getWorld().getName());
                    plugin.getConfig().set("lobby.x", p.getLocation().getX());
                    plugin.getConfig().set("lobby.y", p.getLocation().getY());
                    plugin.getConfig().set("lobby.z", p.getLocation().getZ());
                    plugin.getConfig().set("lobby.yaw", p.getLocation().getYaw());
                    plugin.getConfig().set("lobby.pitch", p.getLocation().getPitch());
                    plugin.saveConfig();
                    p.sendMessage(ChatColor.GREEN + "Lobby has been set!");
                } else
                    p.sendMessage(ChatColor.RED + "You're not allowed to do this!");
            }
            return true;
        }
    }
     
    Last edited by a moderator: Mar 1, 2015
  4. Offline

    Lazini

    In both classes, you seem to check for the same command. Also, have you wrote the commands in your plugin.yml file?
     
  5. Offline

    Rowinvd

    @Lazini That was a copy and paste mistake ( The code in eclipse was good )
    Plugin.yml:
    Code:
         setlobby:
              description: Set the lobby.
         lobby:
              description: Teleport to the lobby.
     
    Last edited by a moderator: Mar 1, 2015
  6. Offline

    Lazini

    And what do you mean by 'it's not working'? What's the error message?
     
  7. Offline

    Rowinvd

    There is no error message. i run the command, nothing happed. No message no error, nothing. @Lazini

    I don't know how but its fixed! Thanks for the help!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  8. Offline

    Lazini

    Well, your welcome! I guess :D Glad it's working.
     
Thread Status:
Not open for further replies.

Share This Page