Thought provoking problem

Discussion in 'Plugin Development' started by Tecno_Wizard, Aug 14, 2014.

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

    Tecno_Wizard

    Good evening everyone! (At least for me)
    I have an interesting problem that I do not know how to solve. My plugin has a HashMap of players (UUID, thank you Mojang) with a corresponding string specifying which ability a player has active. When a player logs in, it sets off the player join event and gives that player a value of "NA", or not applicable. The problem is, on reload, that HashMap is reset, but I want the players to lose their active ability only if they leave. What should I be doing to ensure that the files stay through reload, but don't when the server is restarted or the player leaves?
    Thank You in advance!

    Edit: I did think about using the player leave event, but I think it doesn't run if the server is shut down.
     
  2. Offline

    Skionz

    Save it to the config?
     
  3. Offline

    Tecno_Wizard

    Skionz, the problem is that if I save it to the config, it still doesn't know if the server was just reloaded or shut down...
     
  4. Offline

    Skionz

    Tecno_Wizard So you want to keep the players ability even when the server reloads?
     
  5. Offline

    Tecno_Wizard

    Yes, I want the active abilities to persist through a /reload, but not through a /stop.
     
  6. Offline

    Skionz

    Tecno_Wizard This may be an awful idea but here goes

    first save all your data to the config
    create a new hashmap or something and onEnable set like "reload" to false
    then check when a player executes a command check if the command is /reload and if it is set "reload" to true
    then on your onDisable check if "reload" equals true and if it is don't do anything, if it equals false clear the config

    There is probably a better way to do this and im not really sure if it will work.
     
  7. Offline

    Funergy

    this is how I learned it(I just have to say. since I learned MySQL I always use MySQL its faster and easier(for me)normally you get a MySQL database for free if you buy a server)
    create a SLAPI or in the full way SaveLoadAPI
    Code:
    public void savekills()
        {
            for(String p : getkillsMap().keySet())
            {
                getConfig().set("kills."+p, killsHashmap.get(p));
            }
            this.saveConfig();
        }
     
        public void loadkills()
        {
            if(!getConfig().contains("kills")) return;
            for(String s : getConfig().getConfigurationSection("kills").getKeys(false))
            {
                killsHashmap(s,getConfig().getInt("kills."+s))
            }
        }
    add the savekills(); in you're on disable
    and add the loadkills(); in the onenable

    :D hope it works for you
    EDIT: Oops I didn't read the latest posts
    in the CommandPreProcessEvent check if the command equals reload then do savekills(); and when he does stop clear the Hashmap and save it with savekills();

    Its kinda hard because you only wanted to do it for a reload.
    If you have any problems with this code feel free to tag me :p
     
  8. Offline

    Tecno_Wizard

    Funergy , Skionz , I will try those tomorrow, I need to stop for the day after spending an hour debugging and making it easier to add more commands. Thank you though. I'll let you know if that works, Fungery. Never knew that there was a commandpreprocess event either, i'll be using that more often in the future...
     
  9. Offline

    Skye

    You could log the map and the system time onDisable and compare it in onEnable. If time difference is less than x amount of seconds, then it will be apparent whether plugins were reloaded or the server restarted.
     
  10. Offline

    Comphenix

    Metadatable is probably the best fit here, as it does survive server reloads but not server restarts (download):
    Code:java
    1. package com.comphenix.example;
    2.  
    3. import java.util.Random;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.metadata.FixedMetadataValue;
    11. import org.bukkit.metadata.MetadataValue;
    12. import org.bukkit.metadata.Metadatable;
    13. import org.bukkit.plugin.Plugin;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. public class ExampleMod extends JavaPlugin implements Listener {
    17. private static String[] ABILITIES = { "BLINK", "INVISIBILITY", "FIREBALL" };
    18.  
    19. // Probably a good idea to use your namespace here
    20. private static final String ABILITY_KEY = "com.comphenix.ability";
    21.  
    22. private Random rnd = new Random();
    23.  
    24. @Override
    25. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    26. if (sender instanceof Player) {
    27. Player p = (Player) sender;
    28.  
    29. // Sub commands
    30. if (args.length == 1) {
    31. final String subCommand = args[0];
    32.  
    33. if ("set".equalsIgnoreCase(subCommand)) {
    34. String ability = ABILITIES[rnd.nextInt(ABILITIES.length)];
    35.  
    36. setMetadata(p, ABILITY_KEY, ability);
    37. sender.sendMessage(ChatColor.GOLD + "Setting ability to " + ability);
    38.  
    39. } else if ("get".equalsIgnoreCase(subCommand)) {
    40. sender.sendMessage(ChatColor.GOLD + "Current ability is: " + getMetadata(p, ABILITY_KEY));
    41.  
    42. } else {
    43. sender.sendMessage(ChatColor.RED + "Unknown sub command: " + subCommand);
    44. }
    45. } else {
    46. sender.sendMessage(ChatColor.RED + "Must specify one sub command");
    47. }
    48. }
    49. return true;
    50. }
    51.  
    52. private void setMetadata(Metadatable target, String key, String value) {
    53. target.setMetadata(key, new FixedMetadataValue(this, value));
    54. }
    55.  
    56. private String getMetadata(Metadatable source, String key) {
    57. for (MetadataValue value : source.getMetadata(key)) {
    58. Plugin owner = value.getOwningPlugin();
    59.  
    60. // The previous plugin instance might have be collected
    61. if (owner == null || owner.getName().equals(getName())) {
    62. return value.asString();
    63. }
    64. }
    65. return null;
    66. }
    67. }

    That's much simpler than having to determine if your plugin may have been reloaded or restarted from time stamps.
     
    Caprei, Skye and xTigerRebornx like this.
Thread Status:
Not open for further replies.

Share This Page