Arraylists

Discussion in 'Plugin Development' started by Staartvin, Apr 9, 2012.

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

    Staartvin

    Hello everyone,

    I'm making a Vote Plugin where, players can type /vote <player> to vote for a player.
    I need to have the players there has been voted for in a config.yml.
    I use a Array called 'votedplayers' where, all the players who are voted for , are in.
    I need to set this array to an arraylist in the config.yml.
    I have tried Arrays.asList, but I can't figure it out.
    I also need to add players to the list, as soon as a player does /vote <player>

    Could you guys help me out?

    Code I have so far:

    Code:
    package me.staartvin.CMIVotePlugin;
     
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.logging.Level;
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class CMIVotePlugin extends JavaPlugin{
       
        public static JavaPlugin plugin;
        public static final Logger log = Logger.getLogger("Minecraft");
        private FileConfiguration customConfig = null;
        private File customConfigurationFile = null;
    //    private int Size = Bukkit.getServer().getMaxPlayers() * 1000;
        public String[] votedplayers;
       
       
        public void onDisable() {
            saveCustomConfig();
            log.info("[CMIVotePlugin version" + getDescription().getVersion()
                    + " by Staartvin" + " has been disabled!");
        }
       
        public void onEnable() {
            loadConfiguration();
            getConfig();
            reloadConfig();
            saveConfig();
            getServer().getPluginManager().registerEvents(new Listener() {
                 
                @EventHandler
                    public void playerJoin(PlayerJoinEvent event) {
                        if (event.getPlayer().hasPlayedBefore() == false) {
                        event.getPlayer().sendMessage(getConfig().getString("message"));
                        }
                    }
                }, this);
            log.info("[CMIVotePlugin] version " + getDescription().getVersion()
                    + " by Staartvin" + " has been enabled!");
        }
     
        public void loadConfiguration(){
    //        String voted = "VotedPlayers";
    //    Arrays[] votedplayers = {};
            String message = "message";
            getConfig().addDefault(message , "Welcome new Player to the Craft Me In Server!");
            getConfig().set("VotedPlayertjes", Arrays.asList(votedplayers));
    //        getConfig().set("PlayersWhoHaveBeenVotedFor", "");
    //        getConfig().set("PlayersWhoHaveBeenVotedFor", Arrays.asList(votedplayers));
            //See "Creating you're defaults"
            getConfig().options().copyDefaults(true); // NOTE: You do not have to use "plugin." if the class extends the java plugin
            //Save the config whenever you manipulate it
            saveConfig();
        }
       
        public boolean onCommand(CommandSender cs, Command cmnd, String commandLabel, String[] args) {
            Player player = (Player) cs;
           
            if(cmnd.getName().equalsIgnoreCase("vote")){
                if(player.hasPermission("cmi.vote") && args.length == 1) {
                    if(cs.getName() == args[0]) {
                        player.sendMessage("You cannot vote for yourself!");
                    }
                    else if (args.length == 1) {
                    player.sendMessage(ChatColor.BLUE + "You've voted for " + ChatColor.GOLD + args[0]);
                    System.out.print("[VOTING]" + " Player " + cs.getName() + " has voted for " + args[0]);
                    int Place = getConfig().getStringList("PlayersWhoHaveBeenVotedFor").size() + 1;
                    getConfig().set("PlayersWhoHaveBeenVotedFor", getStringList("PlayersWhoHaveBeenVotedFor").add(Place, args[0]));
                    saveConfig();
                }
                }
                else if(args.length > 1 ) {
                    player.sendMessage("Too many arguments!");
                    player.sendMessage("Command use: /vote <player>");
                }
                else if(args.length == 0 ) {
                    player.sendMessage("Not enough arguments!");
                    player.sendMessage("Command use: /vote <player>");
                }
                if(cmnd.getName().equalsIgnoreCase("votedplayers")) {
                    if(player.hasPermission("cmi.votedplayers")); {
                        player.sendMessage("Players who have been voted for: " + getConfig().getStringList("PlayersWhoHaveBeenVotedFor").toArray());
                    }
                }
            }
            return true;
     
                }
       
        public FileConfiguration getCustomConfig() {
            if (customConfig == null) {
                reloadConfig();
            }
            return customConfig;
        }
       
        public void saveCustomConfig() {
            if (customConfig == null || customConfigurationFile == null) {
            return;
            }
            try {
                customConfig.save(customConfigurationFile);
            } catch (IOException ex) {
                Logger.getLogger(JavaPlugin.class.getName()).log(Level.SEVERE, "Could not save config to " + customConfigurationFile, ex);
            }
        }
       
    /*    public void reloadCustomConfig() {
            if (customConfigurationFile == null) {
            customConfigurationFile = new File(getDataFolder(), "VotedPlayers.yml");
            }
            customConfig = YamlConfiguration.loadConfiguration(customConfigurationFile);
       
            // Look for defaults in the jar
            InputStream defConfigStream = getResource("VotedPlayers.yml");
            if (defConfigStream != null) {
                YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
                customConfig.setDefaults(defConfig);
            }
        }
    */
    }
    
     
  2. Offline

    Ranzdo

  3. Offline

    Staartvin

Thread Status:
Not open for further replies.

Share This Page