Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.infogroup.infoboard.scoreboard;
- import com.infogroup.infoboard.InfoBoardReborn;
- import org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- import java.io.*;
- import java.util.HashMap;
- import java.util.Map;
- public class InfoBoardManager {
- private InfoBoardReborn plugin;
- private Map<String, InfoBoard> boards;
- private Map<String, Ladder> ladders;
- private File folder;
- public InfoBoardManager(InfoBoardReborn plugin){
- this.plugin = plugin;
- //create new maps for boards and ladders.
- this.boards = new HashMap<>();
- this.ladders = new HashMap<>();
- //Create Board folder, if it doesn't exist and it's default files.
- this.folder = new File(plugin.getDataFolder(), "boards");
- if(!folder.exists()){
- folder.mkdirs();
- this.setup();
- }
- loadAllBoards();
- //Load the ladders
- for (String name : plugin.getConfig().getConfigurationSection("Ladders").getKeys(true)) {
- if (!name.contains(".")) {
- ladders.put(name,
- new Ladder(this.plugin, name, this.plugin.getConfig().getStringList("Ladders." + name)));
- }
- }
- }
- /**
- * Load all the Boards that are located in plugins/InfoBoardReborn/boards
- */
- public void loadAllBoards(){
- if (plugin == null){
- return;
- }
- for (File sub : this.folder.listFiles()){
- Bukkit.getConsoleSender().sendMessage(sub.getName());
- boards.put(sub.getName().replace(".yml", ""), new InfoBoard(sub));
- }
- for (String s : boards.keySet()){
- Bukkit.getConsoleSender().sendMessage("Loaded: " + s);
- }
- }
- /**
- * Load the given board if it isn't already.
- * @param name
- */
- public void loadBoard(String name){
- if (plugin == null) {
- return;
- }
- for (File sub: this.folder.listFiles()){
- if (sub.getName().equals(name + ".yml") && !(boards.containsKey(name))) {
- boards.put(name, new InfoBoard( sub));
- }
- }
- }
- /**
- * Reload the given board File
- * @param name
- */
- public void reloadBoard(String name){
- for (String boardname : this.boards.keySet()){
- if (name.equals(boardname)){
- boards.get(name).reloadFile();
- }
- }
- }
- /**
- * Reload All Board Files
- */
- public void reloadAllBoards(){
- for (InfoBoard board : this.boards.values()){
- board.reloadFile();
- }
- }
- /**
- * Crate the Default files if they do not exist
- */
- public void setup(){
- File defaultF = new File(folder.getPath(), "Default.yml");
- File pStatsF = new File(folder.getPath(), "PlayerStats.yml");
- File sStatsF = new File(folder.getPath(), "ServerStats.yml");
- File config = new File(plugin.getDataFolder(),"config.yml");
- //Default file
- if (!defaultF.exists()){
- try{
- defaultF.createNewFile();
- copy(plugin.getResource("Default.yml"), defaultF);
- if (plugin.getSettings().debug()){
- Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "Default.yml was successfully made!");
- }
- } catch (IOException ex){
- if (plugin.getSettings().debug()){
- Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.RED + "Could not make Default.yml, because:" + ex);
- }
- }
- }
- //Default Player Stats file
- if (!pStatsF.exists()){
- try{
- pStatsF.createNewFile();
- copy(plugin.getResource("PlayerStats.yml"), pStatsF);
- if (plugin.getSettings().debug()){
- Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "PlayerStats.yml was successfully made!");
- }
- } catch (IOException ex){
- if (plugin.getSettings().debug()){
- Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.RED + "Could not make PlayerStats.yml, because:" + ex);
- }
- }
- }
- //Default Server Stats file
- if (!sStatsF.exists()){
- try{
- sStatsF.createNewFile();
- copy(plugin.getResource("ServerStats.yml"), sStatsF);
- if (plugin.getSettings().debug()){
- Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "ServerStats.yml was successfully made!");
- }
- } catch (IOException ex){
- if (plugin.getSettings().debug()){
- Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.RED + "Could not make ServerStats.yml, because:" + ex);
- }
- }
- }
- //Default config file
- if (!config.exists()){
- try{
- config.createNewFile();
- copy(plugin.getResource("config.yml"), config);
- if (plugin.getSettings().debug()){
- Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "config.yml was successfully made!");
- }
- } catch (IOException ex){
- if (plugin.getSettings().debug()){
- Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.RED + "Could not make config.yml, because:" + ex);
- }
- }
- }
- }
- /**
- * @param in
- * @param file
- */
- private void copy(InputStream in, File file){
- try {
- OutputStream out = new FileOutputStream(file);
- byte[] buf = new byte[1024];
- int len;
- while ((len = in.read(buf)) > 0) {
- out.write(buf, 0, len);
- }
- out.close();
- in.close();
- } catch (Exception ex) {
- Bukkit.getConsoleSender().sendMessage("Could not copy " + file.getName()+ "because: ex");
- ex.printStackTrace();
- }
- }
- /**
- * Get the given InfoBoard
- * @param name
- * @return InfoBoard
- */
- public InfoBoard getInfoBoard(String name) {
- return this.boards.get(name.replaceAll(".yml", ""));
- }
- /**
- * Get the Ladders Map
- * @return Map<String, Ladder>
- */
- public Map<String, Ladder> getLadders() {
- return this.ladders;
- }
- /**
- * Get the given Ladder
- * @param name
- * @return Ladder
- */
- public Ladder getLadder(String name){
- return this.ladders.get(name);
- }
- /**
- * Get the Boards map
- *
- * @return Map<String, InfoBoard>
- */
- public Map<String, InfoBoard> getBoards() {
- return this.boards;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement