Record if a player has EVER executed a command?

Discussion in 'Plugin Development' started by CarPet, Oct 18, 2012.

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

    CarPet

    Today I was trying to work on a plugin when I ran into a huge problem. I need it to store whether they have executed the command no matter if the server restarts or reloads. So if a player does /red it changes their name to red and stays that way until the plugin is completely taken off the server of if they execute /blank that would reset their name...

    Any help is awesome!

    Thanks, If you do not mind please paste your code if you have any ideas/suggestions.
     
  2. Offline

    JazzaG

    CarPet

    So you want to save their state? Well, there's this, database solutions, YAML files, and anything that saves data to disk.
     
    kroltan likes this.
  3. i think a hashmap would be a good idea, unless they are only allowed to use it one time and never again, then i'd use yaml files.
    But since hashmaps gets reset on restart, i'd go for some file solution.
     
  4. A simple SLAPI will save the hashmaps easily, wouldn't be any trouble doing that.

    EDIT: didn't click the link Jazza posted, sorry :p

    Code:
    public class SLAPI {
    public static void save(Object obj,String path) throws Exception {
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path));
    oos.writeObject(obj);
    oos.flush();
    oos.close();
    }
    public static Object load(String path) throws Exception {
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path));
    Object result = ois.readObject();
    ois.close();
    return result;
    }
    }
    call these methods to save and load them, such as load in onEnable and save whenever it updates.

    For example, your hashmap:

    Code:
    static HashMap<String, Boolean> yourHashMap = new HashMap<String, Boolean>();
    
    Code:
    public void onDisable() {
    System.out.println(this + " is now disabled!");
    try {
    SLAPI.save(yourHashMap, "fileNameOfHashMap.bin");
    } catch (Exception ex) {
    }
    }
    
    Code:
    public void onEnable() {
    getServer().getPluginManager().registerEvents(new PluginListener(), this);
    getLogger().info("Thank you for using this plugin.");
    try {
    yourHashMap = (HashMap<String, Boolean>)SLAPI.load("fileNameOfHashMap.bin");
    } catch (Exception ex) {
             
    }
    }
    
     
  5. Offline

    CarPet

    Could somebody please go over how to have 3 commands:
    /green
    /red
    /blue

    Each command setting your displayName to the appropriate color...

    What I would like to happen is it removing the player from the other 2 hashmaps when they execute a command.
    Also, I would still like it to stay the same whether the server restarts or reloads...

    Thanks, Also, when it says fileNameOfHashMap.bin, would it just be yourhashmap.bin if I kept it like you have above?

    Nevermind I figured that out but it resets when they leave the server and then rejoin it :/

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

    lenis0012

    save it in the config and put some code in an OnJoin event
    example:
    Code:
    private MainCLass plugin
    public ListenerClass(MainCLass i) { plugin = i; }
     
    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event)
    {
        Player player = event.getPlayer
        if(plugin.getConfig.getString("disPlayerName." + player.getName()) != null)
        {
            String disName = plugin.getConfig.getString("disPlayerName." + player.getName());
            disName = disName.replace("&a", ChatColor.GREEN.toString());
            disName = disName.replace("&c", ChatColor.RED.toString());
            disName = disName.replace("&b", ChatColor.AQUA.toString());
            player.setDisplayerName(disName);
        }
    }
    
     
  7. Offline

    CarPet

    Thank you to everyone who has helped find the answer!
     
Thread Status:
Not open for further replies.

Share This Page