Multiple command classes

Discussion in 'Plugin Development' started by amitlin14, Aug 8, 2012.

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

    amitlin14

    I am currently making a fairly big plugin, and i want each command to have its own class because each command is about 100 lines, and i dont want it to get messy. currently i have the core(the onenable and ondisable) and another class, but when i open the server' it just shows me the plugin is enabled, but the command doesnt work, it does nothing at all!

    how do i fix this? D:
     
  2. Offline

    zajacmp3

  3. Offline

    bitfreeze

    Hey there!
    It's hard to tell for sure what the problem is since no source was provided.
    I can suppose what's wrong and raise some questions for you to check:
    - have you listed the commands in your plugin.yml file?
    - have you assigned the executors to new instances of your subclasses inside onEnable()?
    Code:
    getCommand("<one cmd>").setExecutor(new <executor class>(<optional parameters>));
     
  4. Offline

    amitlin14

    can you please give me an example on how to use that? cus its giving me an internal problem

    EDIT: never mind i got it! THANKS SO MUCH!

    do you perhaps know why when i do /tp(just /tp, no arguments) it gives me an internal error?

    Code:
    package me.amitlin14.ExtraRPG;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.EventHandler;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class TeleportListener extends JavaPlugin implements CommandExecutor,
    Listener {
     
    @Override
    public boolean onCommand(CommandSender sender, Command cmd,
    String commandLabel, String[] args) {
    final Player player = (Player) sender;
    final Player targetPlayer = player.getServer().getPlayer(args[0]);
     
    if (cmd.getName().equalsIgnoreCase("tp")) { // If the player typed /tp
    // then do the following...
    if (player.hasPermission("extrarpg.tp.*")) {
    if (args.length == 0) {
    player.sendMessage(ChatColor.WHITE + "----"
    + ChatColor.GREEN + " Teleport " + ChatColor.WHITE
    + "----");
    player.sendMessage(ChatColor.AQUA + "/tp" + "\t"
    + ChatColor.WHITE + "Displays this menu");
    player.sendMessage(ChatColor.AQUA + "/tp <player>" + "\t"
    + ChatColor.WHITE + "Teleport to a player.");
    player.sendMessage(ChatColor.AQUA + "/tphere" + "\t"
    + ChatColor.WHITE + "Teleport a player to you.");
    } else if (args.length == 1) {
    player.sendMessage(ChatColor.GRAY + "Teleporting...");
    teleportHunger(player, targetPlayer);
    } else {
    player.sendMessage(ChatColor.RED + "Usage: /tp <player>");
    }
    } else if (player.hasPermission("extrarpg.tp")) {
    if (args.length == 0) {
    player.sendMessage(ChatColor.WHITE + "----"
    + ChatColor.GREEN + " Teleport " + ChatColor.WHITE
    + "----");
    player.sendMessage(ChatColor.AQUA + "/tp" + "\t"
    + ChatColor.WHITE + "Displays this menu");
    player.sendMessage(ChatColor.AQUA + "/tp <player>" + "\t"
    + ChatColor.WHITE + "Teleport to a player.");
    player.sendMessage(ChatColor.AQUA + "/tphere" + "\t"
    + ChatColor.WHITE + "Teleport a player to you.");
    } else if (args.length == 1) {
    if (player.getFoodLevel() < 3) {
    player.sendMessage(ChatColor.RED + "Not enough food!");
    return false;
    } else {
    final Location commenceLocation = player.getLocation();
    player.sendMessage(ChatColor.GRAY
    + "Teleportation will commence in 3 seconds...");
    this.getServer().getScheduler()
    .scheduleSyncDelayedTask(this, new Runnable() {
    public void run() {
    if (player.getLocation() != commenceLocation) {
    player.sendMessage(ChatColor.RED
    + "Teleportation cancled.");
    return;
    } else {
    player.setFoodLevel(player
    .getFoodLevel() - 3);
    player.sendMessage(ChatColor.GRAY
    + "Teleporting... 3 food is taken.");
    teleportHunger(player, targetPlayer);
    }
    }
    }, 60L);
    }
    } else {
    player.sendMessage(ChatColor.RED + "Usage: /tp <player>");
    }
    } else if (player.hasPermission("extrarpg.tp.nohunger")) {
    if (args.length == 0) {
    player.sendMessage(ChatColor.WHITE + "----"
    + ChatColor.GREEN + " Teleport " + ChatColor.WHITE
    + "----");
    player.sendMessage(ChatColor.AQUA + "/tp" + "\t"
    + ChatColor.WHITE + "Displays this menu");
    player.sendMessage(ChatColor.AQUA + "/tp <player>" + "\t"
    + ChatColor.WHITE + "Teleport to a player.");
    player.sendMessage(ChatColor.AQUA + "/tphere" + "\t"
    + ChatColor.WHITE + "Teleport a player to you.");
    } else if (args.length == 1) {
    final Location location = targetPlayer.getLocation();
    final Location commenceLocation = player.getLocation();
    player.sendMessage(ChatColor.GRAY
    + "Teleportation will commence in 3 seconds...");
    this.getServer().getScheduler()
    .scheduleSyncDelayedTask(this, new Runnable() {
     
    public void run() {
    if (player.getLocation() != commenceLocation) {
    player.sendMessage(ChatColor.RED
    + "Teleportation cancled.");
    return;
    } else {
    player.sendMessage(ChatColor.GRAY
    + "Teleporting... 3 food is taken.");
    teleportHunger(player, targetPlayer);
    }
    }
    }, 60L);
    } else {
    player.sendMessage(ChatColor.RED + "Usage: /tp <player>");
    }
    }
    }
    return false;
    }
     
    @EventHandler
    public void teleportHunger(Player player, Player targetPlayer) {
    Location targetLocation = targetPlayer.getLocation();
    player.teleport(targetLocation);
    }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  5. Offline

    bitfreeze

    What's causing the exception? Are you casting /tp as an in-game player or on the console?
    Can you paste the error stack here? "Internal error" is vague, and the error stack usually shows exactly where the problem lies.

    Also, you'll want to make sure that it's a player running the command. If you try /tp from the console, it may raise an exception when trying to cast sender to player.

    It may not be a good practice to mix an executor and a listener all in the same class.
    Anyway, teleportHunger is a simple subroutine, right? I see no need to prefix it with @EventHandler.
    PS: Do you really need having it as a separate routine, since it only performs a player teleport?
     
  6. Offline

    amitlin14

    Nevermind, i threw that project away, not worth 3 days of work
     
Thread Status:
Not open for further replies.

Share This Page