Advertisement
Guest User

IBM-IBR3

a guest
May 21st, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.92 KB | None | 0 0
  1. package com.infogroup.infoboard.scoreboard;
  2.  
  3. import com.infogroup.infoboard.InfoBoardReborn;
  4. import org.bukkit.Bukkit;
  5. import org.bukkit.ChatColor;
  6.  
  7. import java.io.*;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10.  
  11. public class InfoBoardManager {
  12.  
  13. private InfoBoardReborn plugin;
  14. private Map<String, InfoBoard> boards;
  15. private Map<String, Ladder> ladders;
  16. private File folder;
  17.  
  18. public InfoBoardManager(InfoBoardReborn plugin){
  19. this.plugin = plugin;
  20. //create new maps for boards and ladders.
  21. this.boards = new HashMap<>();
  22. this.ladders = new HashMap<>();
  23.  
  24. //Create Board folder, if it doesn't exist and it's default files.
  25. this.folder = new File(plugin.getDataFolder(), "boards");
  26. if(!folder.exists()){
  27. folder.mkdirs();
  28. this.setup();
  29. }
  30. loadAllBoards();
  31.  
  32. //Load the ladders
  33. for (String name : plugin.getConfig().getConfigurationSection("Ladders").getKeys(true)) {
  34. if (!name.contains(".")) {
  35.  
  36. ladders.put(name,
  37. new Ladder(this.plugin, name, this.plugin.getConfig().getStringList("Ladders." + name)));
  38. }
  39. }
  40. }
  41.  
  42. /**
  43. * Load all the Boards that are located in plugins/InfoBoardReborn/boards
  44. */
  45. public void loadAllBoards(){
  46. if (plugin == null){
  47. return;
  48. }
  49. for (File sub : this.folder.listFiles()){
  50. Bukkit.getConsoleSender().sendMessage(sub.getName());
  51. boards.put(sub.getName().replace(".yml", ""), new InfoBoard(sub));
  52. }
  53. for (String s : boards.keySet()){
  54. Bukkit.getConsoleSender().sendMessage("Loaded: " + s);
  55. }
  56. }
  57.  
  58. /**
  59. * Load the given board if it isn't already.
  60. * @param name
  61. */
  62. public void loadBoard(String name){
  63. if (plugin == null) {
  64. return;
  65. }
  66. for (File sub: this.folder.listFiles()){
  67. if (sub.getName().equals(name + ".yml") && !(boards.containsKey(name))) {
  68. boards.put(name, new InfoBoard( sub));
  69. }
  70. }
  71. }
  72.  
  73. /**
  74. * Reload the given board File
  75. * @param name
  76. */
  77. public void reloadBoard(String name){
  78. for (String boardname : this.boards.keySet()){
  79. if (name.equals(boardname)){
  80. boards.get(name).reloadFile();
  81. }
  82. }
  83. }
  84.  
  85. /**
  86. * Reload All Board Files
  87. */
  88. public void reloadAllBoards(){
  89. for (InfoBoard board : this.boards.values()){
  90. board.reloadFile();
  91. }
  92. }
  93.  
  94. /**
  95. * Crate the Default files if they do not exist
  96. */
  97. public void setup(){
  98. File defaultF = new File(folder.getPath(), "Default.yml");
  99. File pStatsF = new File(folder.getPath(), "PlayerStats.yml");
  100. File sStatsF = new File(folder.getPath(), "ServerStats.yml");
  101. File config = new File(plugin.getDataFolder(),"config.yml");
  102.  
  103. //Default file
  104. if (!defaultF.exists()){
  105. try{
  106. defaultF.createNewFile();
  107. copy(plugin.getResource("Default.yml"), defaultF);
  108. if (plugin.getSettings().debug()){
  109. Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "Default.yml was successfully made!");
  110. }
  111. } catch (IOException ex){
  112. if (plugin.getSettings().debug()){
  113. Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.RED + "Could not make Default.yml, because:" + ex);
  114. }
  115. }
  116. }
  117. //Default Player Stats file
  118. if (!pStatsF.exists()){
  119. try{
  120. pStatsF.createNewFile();
  121. copy(plugin.getResource("PlayerStats.yml"), pStatsF);
  122. if (plugin.getSettings().debug()){
  123. Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "PlayerStats.yml was successfully made!");
  124. }
  125. } catch (IOException ex){
  126. if (plugin.getSettings().debug()){
  127. Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.RED + "Could not make PlayerStats.yml, because:" + ex);
  128. }
  129. }
  130. }
  131. //Default Server Stats file
  132. if (!sStatsF.exists()){
  133. try{
  134. sStatsF.createNewFile();
  135. copy(plugin.getResource("ServerStats.yml"), sStatsF);
  136. if (plugin.getSettings().debug()){
  137. Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "ServerStats.yml was successfully made!");
  138. }
  139. } catch (IOException ex){
  140. if (plugin.getSettings().debug()){
  141. Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.RED + "Could not make ServerStats.yml, because:" + ex);
  142. }
  143. }
  144. }
  145. //Default config file
  146. if (!config.exists()){
  147. try{
  148. config.createNewFile();
  149. copy(plugin.getResource("config.yml"), config);
  150. if (plugin.getSettings().debug()){
  151. Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "config.yml was successfully made!");
  152. }
  153. } catch (IOException ex){
  154. if (plugin.getSettings().debug()){
  155. Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.RED + "Could not make config.yml, because:" + ex);
  156. }
  157. }
  158. }
  159. }
  160.  
  161. /**
  162. * @param in
  163. * @param file
  164. */
  165. private void copy(InputStream in, File file){
  166. try {
  167. OutputStream out = new FileOutputStream(file);
  168. byte[] buf = new byte[1024];
  169. int len;
  170. while ((len = in.read(buf)) > 0) {
  171. out.write(buf, 0, len);
  172. }
  173. out.close();
  174. in.close();
  175. } catch (Exception ex) {
  176. Bukkit.getConsoleSender().sendMessage("Could not copy " + file.getName()+ "because: ex");
  177. ex.printStackTrace();
  178. }
  179. }
  180.  
  181. /**
  182. * Get the given InfoBoard
  183. * @param name
  184. * @return InfoBoard
  185. */
  186. public InfoBoard getInfoBoard(String name) {
  187. return this.boards.get(name.replaceAll(".yml", ""));
  188. }
  189.  
  190. /**
  191. * Get the Ladders Map
  192. * @return Map<String, Ladder>
  193. */
  194. public Map<String, Ladder> getLadders() {
  195. return this.ladders;
  196. }
  197.  
  198. /**
  199. * Get the given Ladder
  200. * @param name
  201. * @return Ladder
  202. */
  203. public Ladder getLadder(String name){
  204. return this.ladders.get(name);
  205. }
  206.  
  207. /**
  208. * Get the Boards map
  209. *
  210. * @return Map<String, InfoBoard>
  211. */
  212. public Map<String, InfoBoard> getBoards() {
  213. return this.boards;
  214. }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement