PageRenderTime 47ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/spleef/src/SpleefData.java

https://github.com/mrsheen/minecraft-hey0-plugins
Java | 296 lines | 235 code | 56 blank | 5 comment | 56 complexity | 595c0e8b36081d63a9518c9ddf7b2f55 MD5 | raw file
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import java.util.Scanner;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11. public class SpleefData {
  12. static Server world = etc.getServer();
  13. static final Logger log = Logger.getLogger("Minecraft");
  14. static ArrayList<String> spleefList = new ArrayList<String>();
  15. static ArrayList<Integer> triggerBlocks = new ArrayList<Integer>();
  16. static ArrayList<ArrayList<Integer>> changeBlocks = new ArrayList<ArrayList<Integer>>();
  17. static ArrayList<String> playerList = new ArrayList<String>();
  18. static ArrayList<Integer> selectionStatus = new ArrayList<Integer>();
  19. static ArrayList<Integer> pointsCoordinates = new ArrayList<Integer>();
  20. private static int getPlayerIndex(String playerName){
  21. boolean inList = false;
  22. for (String p : playerList){
  23. if (p==playerName)
  24. inList = true;
  25. }
  26. if (!inList){
  27. playerList.add(playerName);
  28. selectionStatus.add(1);
  29. pointsCoordinates.add(null); // A faire : plus élégant
  30. pointsCoordinates.add(null);
  31. pointsCoordinates.add(null);
  32. pointsCoordinates.add(null);
  33. pointsCoordinates.add(null);
  34. pointsCoordinates.add(null);
  35. pointsCoordinates.add(null);
  36. pointsCoordinates.add(null);
  37. pointsCoordinates.add(null);
  38. }
  39. return playerList.indexOf(playerName);
  40. }
  41. public static int setPoint(String playerName, int X, int Y, int Z){
  42. int index = getPlayerIndex(playerName);
  43. int secondPoint = selectionStatus.get(index);
  44. if ( selectionStatus.get(index) == 1 ){
  45. pointsCoordinates.set(index*9, X);
  46. pointsCoordinates.set(index*9+1, Y);
  47. pointsCoordinates.set(index*9+2, Z);
  48. pointsCoordinates.set(index*9+3, null);
  49. pointsCoordinates.set(index*9+4, null);
  50. pointsCoordinates.set(index*9+5, null);
  51. pointsCoordinates.set(index*9+6, null);
  52. pointsCoordinates.set(index*9+7, null);
  53. pointsCoordinates.set(index*9+8, null);
  54. selectionStatus.set(index, 2);
  55. }
  56. else if ( selectionStatus.get(index) == 2){
  57. pointsCoordinates.set(index*9+3, X);
  58. pointsCoordinates.set(index*9+4, Y);
  59. pointsCoordinates.set(index*9+5, Z);
  60. selectionStatus.set(index, 3);
  61. }
  62. else {
  63. pointsCoordinates.set(index*9+6, X);
  64. pointsCoordinates.set(index*9+7, Y);
  65. pointsCoordinates.set(index*9+8, Z);
  66. selectionStatus.set(index, 1);
  67. }
  68. return selectionStatus.get(index);
  69. }
  70. public static void loadSpleefData(){
  71. File dataSource = new File("spleefdata.txt");
  72. if (!dataSource.exists()){
  73. FileWriter writer = null;
  74. try {
  75. writer = new FileWriter("spleefdata.txt", true);
  76. writer.append("#Data for the Spleef plugin is located in this file\r\n");
  77. writer.close();
  78. } catch (Exception e) {
  79. log.log(Level.SEVERE, "[Spleef] : Exception while creating spleefdata.txt");
  80. } finally {
  81. try{
  82. writer.close();
  83. }
  84. catch(IOException e){
  85. log.log(Level.SEVERE, "[Spleef] : Exception while closing loadSpleefData writer", e);
  86. }
  87. }
  88. } else {
  89. try {
  90. spleefList = new ArrayList<String>();
  91. triggerBlocks = new ArrayList<Integer>();
  92. changeBlocks = new ArrayList<ArrayList<Integer>>();
  93. Scanner scanner = new Scanner(dataSource);
  94. while (scanner.hasNextLine()) {
  95. String line = scanner.nextLine();
  96. if (line.startsWith("#") || line.equals("")) {
  97. continue;
  98. }
  99. String[] donnees = line.split(",");
  100. if (donnees.length != 6) {
  101. continue;
  102. }
  103. spleefList.add(donnees[0]);
  104. int spleefindex = spleefList.indexOf(donnees[0]);
  105. triggerBlocks.add(Integer.parseInt(donnees[1]));
  106. triggerBlocks.add(Integer.parseInt(donnees[2]));
  107. triggerBlocks.add(Integer.parseInt(donnees[3]));
  108. changeBlocks.add(new ArrayList<Integer>());
  109. changeBlocks.add(new ArrayList<Integer>());
  110. String[] Airblocks = donnees[4].split(";");
  111. for (int i = 1; i<Airblocks.length;i++) {
  112. changeBlocks.get(spleefindex*2).add(Integer.parseInt(Airblocks[i]));
  113. }
  114. String[] Solidblocks = donnees[5].split(";");
  115. for (int i = 1; i<Solidblocks.length;i++) {
  116. changeBlocks.get(spleefindex*2+1).add(Integer.parseInt(Solidblocks[i]));
  117. }
  118. }
  119. scanner.close();
  120. log.info("Spleef plugin : successfuly loaded.");
  121. } catch (Exception e) {
  122. log.log(Level.SEVERE, "Spleef plugin : Error while reading spleefdata.txt", e);
  123. }
  124. }
  125. return;
  126. }
  127. public static void addSpleefBlocks(String playerName, Integer AirID, Integer SolidID, String spleefName)
  128. {
  129. int index = getPlayerIndex(playerName);
  130. if( pointsCoordinates.get(index*9+8) != null) {
  131. spleefList.add(spleefName);
  132. int spleefindex = spleefList.indexOf(spleefName);
  133. triggerBlocks.add(pointsCoordinates.get(index*9+6));
  134. triggerBlocks.add(pointsCoordinates.get(index*9+7));
  135. triggerBlocks.add(pointsCoordinates.get(index*9+8));
  136. changeBlocks.add(new ArrayList<Integer>());
  137. changeBlocks.add(new ArrayList<Integer>());
  138. int startX = ( pointsCoordinates.get(index*9) <= pointsCoordinates.get(index*9+3) ) ? pointsCoordinates.get(index*9) : pointsCoordinates.get(index*9+3);
  139. int startY = ( pointsCoordinates.get(index*9+1) <= pointsCoordinates.get(index*9+4) ) ? pointsCoordinates.get(index*9+1) : pointsCoordinates.get(index*9+4);
  140. int startZ = ( pointsCoordinates.get(index*9+2) <= pointsCoordinates.get(index*9+5) ) ? pointsCoordinates.get(index*9+2) : pointsCoordinates.get(index*9+5);
  141. int endX = ( pointsCoordinates.get(index*9) <= pointsCoordinates.get(index*9+3) ) ? pointsCoordinates.get(index*9+3) : pointsCoordinates.get(index*9);
  142. int endY = ( pointsCoordinates.get(index*9+1) <= pointsCoordinates.get(index*9+4) ) ? pointsCoordinates.get(index*9+4) : pointsCoordinates.get(index*9+1);
  143. int endZ = ( pointsCoordinates.get(index*9+2) <= pointsCoordinates.get(index*9+5) ) ? pointsCoordinates.get(index*9+5) : pointsCoordinates.get(index*9+2);
  144. for ( int i = startX; i<= endX; i++ ){
  145. for ( int j = startY; j<= endY; j++ ){
  146. for ( int k = startZ; k<= endZ; k++ ){
  147. if(world.getBlockIdAt(i,j,k) == AirID) {
  148. changeBlocks.get(spleefindex*2).add(i);
  149. changeBlocks.get(spleefindex*2).add(j);
  150. changeBlocks.get(spleefindex*2).add(k);
  151. } else if (world.getBlockIdAt(i,j,k) == SolidID) {
  152. changeBlocks.get(spleefindex*2+1).add(i);
  153. changeBlocks.get(spleefindex*2+1).add(j);
  154. changeBlocks.get(spleefindex*2+1).add(k);
  155. }
  156. }
  157. }
  158. }
  159. //Write to file
  160. try {
  161. BufferedWriter writer = new BufferedWriter(new FileWriter("spleefdata.txt", true));
  162. String throwawaystring = spleefName+","+pointsCoordinates.get(index*9+6)+","+pointsCoordinates.get(index*9+7)+","+pointsCoordinates.get(index*9+8)+",";
  163. writer.append(throwawaystring+"0;");
  164. for(int i = 0; i<changeBlocks.get(spleefindex*2).size(); i++) {
  165. if( i == 0) {
  166. writer.append(Integer.toString(changeBlocks.get(spleefindex*2).get(i)));
  167. } else {
  168. writer.append(";"+Integer.toString(changeBlocks.get(spleefindex*2).get(i)));
  169. }
  170. }
  171. writer.append(",0;");
  172. for(int i = 0; i<changeBlocks.get(spleefindex*2+1).size(); i++) {
  173. if( i == 0) {
  174. writer.append(Integer.toString(changeBlocks.get(spleefindex*2+1).get(i)));
  175. } else {
  176. writer.append(";"+Integer.toString(changeBlocks.get(spleefindex*2+1).get(i)));
  177. }
  178. }
  179. writer.newLine();
  180. writer.close();
  181. } catch (Exception e) {
  182. log.log(Level.SEVERE, "[Spleef] : Error while writing spleefdata.txt", e);
  183. return;
  184. }
  185. } else {
  186. return;
  187. }
  188. }
  189. public static void listthespleefs(Player player)
  190. {
  191. if(spleefList.size()>0) {
  192. for (int i = 0; i<spleefList.size(); i++)
  193. {
  194. player.sendMessage(spleefList.get(i));
  195. }
  196. }
  197. return;
  198. }
  199. public static boolean trytoResetArena(Player player, Block blockClicked) {
  200. boolean returnbool = false;
  201. if( (triggerBlocks.size()%3) == 0) {
  202. for(int i = 0; i<triggerBlocks.size()/3;i++) {
  203. if((triggerBlocks.get(i*3) == blockClicked.getX()) &&
  204. (triggerBlocks.get(i*3+1) == blockClicked.getY()) &&
  205. (triggerBlocks.get(i*3+2) == blockClicked.getZ())) {
  206. //player.sendMessage("well this works");
  207. resetSpleefArena(spleefList.get(i), blockClicked,player);
  208. returnbool = true;
  209. }
  210. }
  211. } else {
  212. }
  213. return returnbool;
  214. }
  215. public static void resetSpleefArena(String SpleefName, Block blockClicked, Player player) {
  216. //player.sendMessage("well this works");
  217. int spleefindex = spleefList.indexOf(SpleefName);
  218. if(((changeBlocks.get(spleefindex*2).size()%3) == 0) && ((changeBlocks.get(spleefindex*2+1).size()%3) == 0)) {
  219. for (int i = 0; i<changeBlocks.get(spleefindex*2).size()/3;i++) {
  220. world.setBlockAt(0,
  221. changeBlocks.get(spleefindex*2).get(i*3),
  222. changeBlocks.get(spleefindex*2).get(i*3+1),
  223. changeBlocks.get(spleefindex*2).get(i*3+2));
  224. }
  225. //player.sendMessage("replaced air");
  226. for (int i = 0; i<changeBlocks.get(spleefindex*2+1).size()/3;i++) {
  227. world.setBlockAt(blockClicked.getType(),
  228. changeBlocks.get(spleefindex*2+1).get(i*3),
  229. changeBlocks.get(spleefindex*2+1).get(i*3+1),
  230. changeBlocks.get(spleefindex*2+1).get(i*3+2));
  231. }
  232. //player.sendMessage("replaced blocks");
  233. } else {
  234. }
  235. return;
  236. }
  237. public static boolean removeSpleef(String Spleeftoremove) {
  238. return true;
  239. }
  240. }