/HeyRunes/SimpleTower/HeyRune.java

https://github.com/Raphfrk/hey0-plugins · Java · 209 lines · 187 code · 18 blank · 4 comment · 42 complexity · 2069a27119d2ac6feed2db714de67919 MD5 · raw file

  1. /**
  2. * HeyRune.java - Extend this and register it to listen to specific hooks.
  3. * @author chrisinajar
  4. */
  5. import java.util.ArrayList;
  6. import java.util.Iterator;
  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;
  9. public class HeyRune
  10. {
  11. private static Logger a = Logger.getLogger("Minecraft");
  12. public static enum Direction { NORTH, SOUTH, EAST, WEST, NONE }
  13. private ArrayList<Integer> pattern = new ArrayList<Integer>();
  14. public String name;
  15. public Direction direction;
  16. public boolean north = true;
  17. public boolean south = true;
  18. public boolean east = true;
  19. public boolean west = true;
  20. public HeyRune(String in_name, int[][] in_pattern) {
  21. name = in_name;
  22. windPattern(in_pattern);
  23. }
  24. public HeyRune(String in_name, ArrayList<Integer> in_pattern)
  25. {
  26. name = in_name;
  27. pattern = in_pattern;
  28. }
  29. public String name()
  30. {
  31. return name;
  32. }
  33. public ArrayList<Integer> pattern()
  34. {
  35. return pattern;
  36. }
  37. public int size() {
  38. return pattern.size();
  39. }
  40. public int height() {
  41. return (int)Math.ceil(Math.sqrt(pattern.size()));
  42. }
  43. public int idAt(int index) {
  44. try {
  45. return pattern.get(index);
  46. } catch (IndexOutOfBoundsException ex) {
  47. return -1;
  48. }
  49. }
  50. private void windPattern(int[][] p) {
  51. double height = p.length;
  52. double width = 0;
  53. double size = height;
  54. for(int i = 0; i < p.length; ++i) {
  55. if (p[i].length > width)
  56. width = p[i].length;
  57. }
  58. int offset = -1;
  59. int curx = 0;
  60. int cury = 0;
  61. if (width > size) {
  62. size = width;
  63. offset = -2;
  64. if ((size%2) == 1) {
  65. offset = -3;
  66. }
  67. else if ((size%2) == 0) {
  68. offset = -4;
  69. }
  70. }
  71. else
  72. {
  73. if ((size%2) == 0) {
  74. offset = -4;
  75. }
  76. }
  77. cury += (height)/2;
  78. curx += (width)/2;
  79. int counter = 1;
  80. for (int i = 0; i < ((size*2) + offset); ++i) {
  81. for (int j = 0; j < ((i/2) + 1); ++j) {
  82. try {
  83. pattern.add(p[cury][curx]);
  84. } catch (Throwable t) {
  85. pattern.add(-1);
  86. }
  87. switch((i)%4) {
  88. case 1:
  89. cury++;
  90. break;
  91. case 0:
  92. curx++;
  93. break;
  94. case 3:
  95. cury--;
  96. break;
  97. case 2:
  98. curx--;
  99. break;
  100. }
  101. }
  102. }
  103. }
  104. public void reset() {
  105. north = true;
  106. south = true;
  107. east = true;
  108. west = true;
  109. }
  110. public static HeyRune match(ArrayList<HeyRune> _r, int x, int y, int z) {
  111. ArrayList<HeyRune> runes = new ArrayList<HeyRune>(_r);
  112. HeyRune victor = null;
  113. Direction victorD = null;
  114. int size = 0;
  115. for (HeyRune r : runes) {
  116. size = (size < r.size() ? r.size() : size);
  117. r.reset();
  118. }
  119. int index = 0;
  120. int xoffset = 0;
  121. int yoffset = 0;
  122. int zoffset = 0;
  123. for (int i = 0; index < size; ++i) {
  124. for (int j = 0; j < ((i/2) + 1); ++j) {
  125. try {
  126. Iterator<HeyRune> iter = runes.iterator();
  127. while (iter.hasNext()) {
  128. HeyRune rune = iter.next();
  129. if (!rune.north && !rune.south && !rune.east && !rune.west) {
  130. iter.remove();
  131. continue;
  132. }
  133. int myId = rune.idAt(index);
  134. if (myId > 0) {
  135. if(rune.north && myId != etc.getServer().getBlockIdAt(xoffset + x, y, zoffset + z)) {
  136. rune.north = false;
  137. }
  138. if(rune.east && myId != etc.getServer().getBlockIdAt(zoffset + x, y, -xoffset + z)) {
  139. rune.east = false;
  140. }
  141. if(rune.south && myId != etc.getServer().getBlockIdAt(-xoffset + x, y, -zoffset + z)) {
  142. rune.south = false;
  143. }
  144. if(rune.west && myId != etc.getServer().getBlockIdAt(-zoffset + x, y, xoffset + z)) {
  145. rune.west = false;
  146. }
  147. }
  148. if (index + 1 == rune.size()) {
  149. if(rune.north) {
  150. victorD = Direction.NORTH;
  151. victor = rune;
  152. }
  153. else if(rune.south) {
  154. victorD = Direction.SOUTH;
  155. victor = rune;
  156. }
  157. else if(rune.east) {
  158. victorD = Direction.EAST;
  159. victor = rune;
  160. }
  161. else if(rune.west) {
  162. victorD = Direction.WEST;
  163. victor = rune;
  164. }
  165. iter.remove();
  166. continue;
  167. }
  168. }
  169. } catch (Throwable t) {
  170. a.log(Level.SEVERE, "Exception while iterating a rune " + t);
  171. }
  172. switch((i)%4) {
  173. case 1:
  174. zoffset++;
  175. break;
  176. case 0:
  177. xoffset++;
  178. break;
  179. case 3:
  180. zoffset--;
  181. break;
  182. case 2:
  183. xoffset--;
  184. break;
  185. }
  186. index++;
  187. }
  188. }
  189. return victor;
  190. }
  191. }