Solved Adding Defaults to Config w/ Command

Discussion in 'Plugin Development' started by WeaselBuilds, Jul 30, 2013.

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

    WeaselBuilds

    Hi there! I am making a plugin where the main configuration file will hold a bunch of different names(I am making a school plugin and the names are the classes). The names will be string lists which will hold player names. I'm trying to create a command to where the 2nd argument will be the new class name. I am having much trouble making it. Also, can you explain how I can add a player's name if they want to join the class?

    Code:

    onEnable method(If needed):
    Code:java
    1. public void onEnable(){
    2. logger.info("School has been enabled!");
    3. getCommand("class").setExecutor(new ClassCommands());
    4. FileConfiguration config = this.getConfig();
    5. config.options().header("##School Configuration File Main##");
    6. config.options().copyHeader(true);
    7. config.options().copyDefaults(true);
    8. this.saveConfig();
    9. }


    Class Command:
    Code:java
    1. package net.atvci.noahtemp.schoolplugin.commands;
    2.  
    3. import java.util.Arrays;
    4.  
    5. import net.atvci.noahtemp.schoolplugin.SchoolPluginMain;
    6.  
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandExecutor;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.Player;
    12.  
    13. public class ClassCommands implements CommandExecutor {
    14.  
    15. public Player player;
    16. public SchoolPluginMain plugin;
    17.  
    18. @Override
    19. public boolean onCommand(CommandSender sender, Command cmd, String label,
    20. String[] args) {
    21. if(cmd.getName().equalsIgnoreCase("class")){
    22. if(sender instanceof Player){
    23. if(args.length != 2){
    24. sender.sendMessage(ChatColor.RED + "Usage: /class <option> [value]");
    25. return true;
    26. }
    27. if(args[0].equalsIgnoreCase("create")){
    28. String className = args[1].toString();
    29. String[] list = {"Test"};
    30. if(!(plugin.getConfig().contains("Classes." + className))){
    31. plugin.getConfig().addDefault("Classes." + className, Arrays.asList(list));
    32. plugin.getConfig().options().copyDefaults(true);
    33. plugin.saveConfig();
    34. sender.sendMessage(ChatColor.GREEN + "You have successfully created the " + className + " class!");
    35. }else{
    36. sender.sendMessage(ChatColor.RED + "That class already exists!");
    37. }
    38. }
    39. }
    40. return true;
    41. }
    42. return false;
    43. }
    44. }


    If any more code is needed I'll be happy to add it! Thanks for your help!

    Also, I see that the log might help in this!

    Log:
    Code:
    2013-07-30 20:39:23 [INFO] WeaselBuilds issued server command: /class create Red
    2013-07-30 20:39:23 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'class' in plugin School v0.0.0.1b
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:189)
        at org.bukkit.craftbukkit.v1_6_R2.CraftServer.dispatchCommand(CraftServer.java:523)
        at net.minecraft.server.v1_6_R2.PlayerConnection.handleCommand(PlayerConnection.java:962)
        at net.minecraft.server.v1_6_R2.PlayerConnection.chat(PlayerConnection.java:880)
        at net.minecraft.server.v1_6_R2.PlayerConnection.a(PlayerConnection.java:837)
        at net.minecraft.server.v1_6_R2.Packet3Chat.handle(SourceFile:49)
        at net.minecraft.server.v1_6_R2.NetworkManager.b(NetworkManager.java:296)
        at net.minecraft.server.v1_6_R2.PlayerConnection.e(PlayerConnection.java:116)
        at net.minecraft.server.v1_6_R2.ServerConnection.b(SourceFile:37)
        at net.minecraft.server.v1_6_R2.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:590)
        at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:226)
        at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:486)
        at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:419)
        at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
    Caused by: java.lang.NullPointerException
        at net.atvci.noahtemp.schoolplugin.commands.ClassCommands.onCommand(ClassCommands.java:30)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
        ... 15 more
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  2. Offline

    Taketheword

    You need to put line 27-37 in an else statement
     
  3. Offline

    xTrollxDudex

    WeaselBuilds
    You need to instantiate plugin in your Command class
     
  4. Offline

    WeaselBuilds

    What should I put?

    How can I do that? Can you give me code please?

    EDIT: I added this to my onEnable:

    Code:java
    1. plugin = this;


    And then in the ClassCommands class, removed:

    Code:java
    1. public SchoolPluginMain plugin; //This might have worked. I don't know


    And now I can use this:

    Code:java
    1. SchoolPluginMain.plugin.getConfig().set("path", value);
    2. SchoolPluginMain.plugin.saveConfig();


    Thanks for your help! I got it to work now!
     
  5. Offline

    xTrollxDudex

    WeaselBuilds
    That is really bad programming practice. You would instantiate the plugin variable by passing the plugin class through a constructor.
    This goes in the command class
    PHP:
    Main plugin;
    public Class(
    Main plugin){
    this.plugin plugin;
    }
    This will cause errors with your register line. To register it, use this in the main class
    PHP:
    getCommand("class").setExecutor(new Class(this));
    You would replace all Main 's in the above code with the main class name / all Class 's in the above post with the class you want to register.
     
  6. Offline

    Taketheword

    You're right but don't confuse him :/. I doubt he knows about constructors.
     
    xTrollxDudex likes this.
  7. Offline

    WeaselBuilds

    I'm not confused about constructors. I make mods, too, so I know how to do this stuff. :D
     
Thread Status:
Not open for further replies.

Share This Page