onCommand Config Help

Discussion in 'Plugin Development' started by goldencreeper, Sep 9, 2012.

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

    goldencreeper

    Hello miencrafters, i am developing a plugin for my server(May be public). what i am trying to do is when a player joins my server it adds their name to a custom config file along with a default value which is 100. Then when the player types /$ it sends the player the default value(100). But when a player types /$ nothing happens until the server restarts. after the server restarts and after the player has already joined before and the player types /$ then it shows them the default value(100).
    Thx,
    danmw3

    Edit: Here is the code: <Edit by Moderator: Redacted mediafire url>
     
    Last edited by a moderator: Nov 9, 2016
  2. maybe you should show us some code.

    But I think you are saving the file at the wrong moment.
    You should save the file right after you changed it (right after the player joins, and you put in the 100)

    I think you only save it, in onDisable?
     
  3. Offline

    goldencreeper

    I added the code to the post
     
  4. My guess was wrong. You DO save it at the right time.

    Let me paraphrase the problem.

    When a player joins. It saves some initial data.
    When a player does /$... that data should be shown.
    But... it only works when... the player joins -> server restart -> player joins again.
    Right?

    I don't see anything wrong with your code.
    Except that you never load the file.... I mean, in onEnable, you just create the file and save it?
    I guess it's weird that you do this:
    Code:
    public FileConfiguration config = YamlConfiguration.loadConfiguration(file);
    instead of a seperate load method or something.
    (I dont think this is related to your problem though)
     
  5. Offline

    goldencreeper

    Thank you so much! I can't believe i forgot about the config loading part. I tried putting that in the onEnable method, but that didnt work. Then i put it in the /$ command and it worked! But now is that bad having that code there because now that every time someone types that command it has to load the config. Wont that use more ram than needed?
    I have been searching this problem for about 4 days and you are the first person that i have seen that actually knows what is going wrong. Here is my current CommandExecutor code:
    Code (open)

    Code:
    package com.battlenations.serverplugin;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
     
    import com.battlenations.serverplugin.Listener.ServerPluginPlayerListener;
     
    public class ServerPluginCommandExecutor implements CommandExecutor {
     
    public ServerPlugin plugin;
    public ServerPluginPlayerListener sppl = new ServerPluginPlayerListener();
    public ConfigHandler ch = new ConfigHandler();
     
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    Player player = (Player) sender;
     
    if (commandLabel.equalsIgnoreCase("$")) {
    try {
    ch.config.load(ch.file);
    } catch (Exception e) {
    e.printStackTrace();
    }
    if (ch.config.contains(player.getName())) {
    System.out.println(ch.config.get(player.getName()));
    player.sendMessage("You have: $" + ch.config.get(player.getName()));
    }else{
    player.sendMessage("Failed to find your name!");
    }
    }
    return true;
    }
    }
    
     
  6. You are right, it will load the file, everytime the command is executed.
    That's a bad idea, if there's a lot of data in that file.

    It would be better if you load the file one time in onEnable. And also add a reload-command or something.

    Orrrr.... do not put so much data in 1 file. Maybe you can make 1 file pér player. Instead of all players in 1 file.



    Sorry for late response.
     
Thread Status:
Not open for further replies.

Share This Page