Help With A Extra Class To Do Commands.

Discussion in 'Plugin Development' started by hurleyboarder, Feb 9, 2014.

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

    hurleyboarder

    Hi I want to add a extra class file to make my plugin more organized. But the command I put in there gives me "an internal error occurred while performing this command".

    Main Class:
    Code:java
    1. package me.hurleyboarder;
    2.  
    3.  
    4.  
    5.  
    6.  
    7. import java.io.File;
    8. import java.io.IOException;
    9.  
    10. import org.bukkit.ChatColor;
    11. import org.bukkit.GameMode;
    12. import org.bukkit.Location;
    13. import org.bukkit.command.Command;
    14. import org.bukkit.command.CommandSender;
    15. import org.bukkit.configuration.file.FileConfiguration;
    16. import org.bukkit.configuration.file.YamlConfiguration;
    17. import org.bukkit.entity.Player;
    18. import org.bukkit.plugin.java.JavaPlugin;
    19.  
    20. public class EmeraldAvanced extends JavaPlugin {
    21. File Warps;
    22. FileConfiguration newConfigz;
    23.  
    24.  
    25.  
    26. @Override
    27. public void onEnable() {
    28. getLogger().info("EmeraldAvanced Enabled!");
    29. new EmeraldAvanced ();
    30. saveDefaultConfig();
    31. Warps = new File(getDataFolder(), "warps.yml");
    32. newConfigz = YamlConfiguration.loadConfiguration(Warps);
    33. getCommand("warp").setExecutor(new MyPluginCommandExecutor(this));
    34. }
    35. public void saveNewConfig(){
    36.  
    37. }
    38. @Override
    39. public void onDisable() {
    40. getLogger().info("EmeraldAvanced Disabled!");
    41. saveDefaultConfig();
    42.  
    43. try{
    44. newConfigz.save(Warps);
    45.  
    46. }catch(Exception e){
    47. e.printStackTrace();
    48. }
    49. }


    Then my second class:
    Code:java
    1. package me.hurleyboarder;
    2.  
    3. import java.io.File;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Location;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandExecutor;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.configuration.file.FileConfiguration;
    11. import org.bukkit.entity.Player;
    12.  
    13. public class MyPluginCommandExecutor implements CommandExecutor {
    14. File Warps;
    15. FileConfiguration newConfigz;
    16.  
    17.  
    18. public MyPluginCommandExecutor(EmeraldAvanced plugin) {
    19.  
    20. }
    21.  
    22. @Override
    23. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    24. if(cmd.getName().equalsIgnoreCase("warp") && sender instanceof Player){
    25. Player Player = (Player) sender;
    26. if(sender.hasPermission("emeraldavanced.warp"))
    27. if(args.length == 0){
    28. }else if(args.length == 1){
    29. int x = newConfigz.getInt(args[0] + ".x"), y = newConfigz.getInt(args[0] + ".y"), z = newConfigz.getInt(args[0] + ".z");
    30. Player.teleport(new Location(Player.getWorld(), x, y, z));
    31. Player.sendMessage(ChatColor.BLUE + "[EmeraldAvanced] " + ChatColor.GREEN + "Warped To: " + args[0] + "!");
    32. return true;
    33.  
    34. }
    35. }
    36. return false;}
    37.  
    38. }


    Also my first class does have commands in it also.

    bump.

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

    xTrollxDudex

    hurleyboarder
    It's been less than 24 hours -_-

    Your problem is you didn't assign your fields yet.
     
  3. Offline

    hurleyboarder

    How would I do that? The only reason im making a second class because every other command after the /warp command wont work, then when I put it at the bottem of the code the /warp command wont work any more
     
  4. Offline

    xTrollxDudex

  5. Offline

    Icelaunche

    Here is an example of what you want to to:

    MainClass:
    Code:java
    1. package me.Icelaunche.thecakeisalie;
    2.  
    3. public class TheCakeIsALie extends JavaPlugin{
    4.  
    5. public void onEnable(){
    6.  
    7. this.getCommand("COMMAND_NAME_HERE").setExecutor(new CakeCommandClass()); // commands name
    8.  
    9. }
    10.  
    11. public void onDisable(){
    12. // On Disable Stuff Here
    13. }
    14. }
    15.  


    Command Class:

    Code:java
    1. package me.Icelaunche.thecakeisalie;
    2.  
    3. public class CakeCommandClass implements CommandExecutor { // command class
    4.  
    5. public static TheCakeIsALie plugin;
    6.  
    7. @Override
    8. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    9. Player player = (Player) sender;
    10. if(label.equalsIgnoreCase("COMMAND_NAME_HERE")){
    11. // Command here
    12. return true;
    13. }
    14.  
    15. return false;
    16. }
    17.  
    18. }
    19.  


    That what I do and it works. I might of missed a few brackets, imports or done stuff like that so you might need to add/delete a few things to use it.
     
  6. Offline

    hurleyboarder

    I
    How would I change that?
     
  7. Offline

    Icelaunche

    Look for a tutorial about multiple commands.
     
Thread Status:
Not open for further replies.

Share This Page