Seen command like essentials?

Discussion in 'Spigot Plugin Development' started by UnlimitedNinjas, Jul 19, 2018.

  1. When Player Join, add their IP to the current list (if any) of their other known ip addresses. If some other player joins on the same IP then that's the same account. Other stuff and all that, more checks, being smart, etc.
     
  2. How can i make a permenent list? Without being it in config?
     
  3. You're gonna need it to be in some sort of file if you want it to be permanent.
     
  4. You have to save information about the player somewhere. Any way of storage would work, it just so happens that config files are easy to use.
     
  5. Okay so i will use config, but how do i add a player then their ips? So under stringlist ips: i need to add
    player:
    - ip1
    - ip2
    player2:
    - ip1
    - ip2
    - ip3
     
  6. If you're going to use config files, I recommend you use a Folder then save a file for each Player.

    I use this folder and YAML file handler I wrote a while ago to help myself.
    Code (Java):
    package com.wowserman.database;

    import java.io.File;
    import java.io.IOException;

    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;

    public class YAMLFileHandler {

        protected File source;
        private FileConfiguration config;
        protected final boolean provided;
        final protected JavaPlugin plugin;
        final protected String providedName;

        public FileConfiguration getConfig() {
            return config;
        }

        protected YAMLFileHandler(File source) {
            this.source = source;
            this.load();
            this.plugin = null;
            this.providedName = null;
            this.provided = false;
        }

        public YAMLFileHandler(JavaPlugin plugin, String providedName) {
            this.plugin = plugin;
            this.providedName = providedName;
            this.provided = true;
            this.load();
        }

        public void save() {
            try {
                config.save(source);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public void load() {
            if (provided) {
                File file = new File(plugin.getDataFolder(), providedName + ".yml");
                if (file.exists() == false && plugin.getResource(providedName + ".yml") != null) {
                    plugin.saveResource(providedName + ".yml", true);
                } else if (file.exists() == false) {
                    try {
                        file.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                this.source = file;
            }
            this.config = YamlConfiguration.loadConfiguration(source);
        }
    }
     
    Code (Java):
    package com.wowserman.database;

    import java.io.File;
    import java.io.IOException;
    import java.util.Arrays;
    import java.util.List;
    import java.util.stream.Collectors;

    import org.bukkit.plugin.java.JavaPlugin;

    public class YAMLFolder {

        protected File folder;
        protected String name;
        protected JavaPlugin plugin;

        public YAMLFileHandler getFile(String name) {
            File file = new File(folder, name + (name.contains(".yml") ? "" : ".yml"));

            if (file.exists() == false)
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            return new YAMLFileHandler(file);
        }

        public List<YAMLFileHandler> getAll() {
            final List<String> names = Arrays.asList(this.folder.list()).stream().filter((file) -> {
                return file.endsWith(".yml");
            }).collect(Collectors.toList());
            return names.stream().map((fileyml) -> {
                return this.getFile(fileyml);
            }).collect(Collectors.toList());
        }

        public YAMLFolder(JavaPlugin plugin, String name) {
            this.plugin = plugin;
            if (plugin.getDataFolder().exists() == false)
                plugin.getDataFolder().mkdir();

            this.folder = new File(plugin.getDataFolder(), name);

            if (this.folder.exists() == false)
                this.folder.mkdir();
        }
    }
     

    I create a folder where all the player data will be.
    Code (Java):
    protected final YAMLFolder folder = new YAMLFolder(myPlugin, "player-data");
    Than when I need to get the file of a player I do
    Code (Text):
    Player player = Bukkit.getPlayer("Wowserman");
    YAMLFileHandler file = folder.getFile(player.getUniqueID().toString());
    Than I read / write / save / reload the file whenever I need to.
    Code (Java):
    file.getConfig().set("ip", someMethodHereToGetIP());
    String ip = file.getConfig().getString("ip", "IP has not been Saved Yet.");

    file.save(); // save file to disk
    file.load(); // re-load the file
    You could also make the files based on the IP, than in the files have a list of Player UUID's that have logged in with the IP.

    Like this:
    /player-data/someip.yml
    /player-data/someotherip.yml
    than the files might looks something like
    Code (YAML):
    player-ids:
    - some-player-uuid
    - some-other-player-uuid
    ip
    : the-ip
    last-login
    : some-date
     
    #7 Wowserman, Jul 19, 2018
    Last edited: Jul 19, 2018