Advertisement
Gamebuster

Untitled

Apr 5th, 2016
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.19 KB | None | 0 0
  1. package ch.njol.skript.lang.util;
  2.  
  3. import ch.njol.skript.ScriptLoader;
  4. import ch.njol.skript.Skript;
  5. import ch.njol.skript.SkriptAPIException;
  6. import ch.njol.skript.classes.Changer;
  7. import ch.njol.skript.classes.ClassInfo;
  8. import ch.njol.skript.lang.Expression;
  9. import ch.njol.skript.lang.util.ConvertedExpression;
  10. import ch.njol.skript.registrations.Classes;
  11. import ch.njol.skript.util.Utils;
  12. import ch.njol.util.Checker;
  13. import ch.njol.util.Kleenean;
  14. import ch.njol.util.coll.CollectionUtils;
  15. import ch.njol.util.coll.iterator.ArrayIterator;
  16. import java.lang.reflect.Array;
  17. import java.util.Iterator;
  18. import org.bukkit.event.Event;
  19. import org.eclipse.jdt.annotation.Nullable;
  20.  
  21. public abstract class SimpleExpression<T>
  22. implements Expression<T> {
  23. private int time = 0;
  24. @Nullable
  25. private ClassInfo<?> returnTypeInfo;
  26.  
  27. protected SimpleExpression() {
  28. }
  29.  
  30. @Nullable
  31. @Override
  32. public final T getSingle(Event e) {
  33. T[] all = this.getArray(e);
  34. if (all.length == 0) {
  35. return null;
  36. }
  37. if (all.length > 1) {
  38. throw new SkriptAPIException("Call to getSingle() on a non-single expression");
  39. }
  40. return all[0];
  41. }
  42.  
  43. @Override
  44. public T[] getAll(Event e) {
  45. T[] all = this.get(e);
  46. if (all == null) {
  47. Object[] r = (Object[])Array.newInstance(this.getReturnType(), 0);
  48. assert (r != null);
  49. return r;
  50. }
  51. if (all.length == 0) {
  52. return all;
  53. }
  54. int numNonNull = 0;
  55. T[] arrT = all;
  56. int n = arrT.length;
  57. int n2 = 0;
  58. while (n2 < n) {
  59. T t = arrT[n2];
  60. if (t != null) {
  61. ++numNonNull;
  62. }
  63. ++n2;
  64. }
  65. if (numNonNull == all.length) {
  66. return all;
  67. }
  68. Object[] r = (Object[])Array.newInstance(this.getReturnType(), numNonNull);
  69. assert (r != null);
  70. int i = 0;
  71. T[] arrT2 = all;
  72. int n3 = arrT2.length;
  73. int n4 = 0;
  74. while (n4 < n3) {
  75. T t = arrT2[n4];
  76. if (t != null) {
  77. r[i++] = t;
  78. }
  79. ++n4;
  80. }
  81. return r;
  82. }
  83.  
  84. @Override
  85. public final T[] getArray(Event e) {
  86. T[] arrT;
  87. int n;
  88. T[] all = this.get(e);
  89. if (all == null) {
  90. Object[] r = (Object[])Array.newInstance(this.getReturnType(), 0);
  91. assert (r != null);
  92. return r;
  93. }
  94. if (all.length == 0) {
  95. return all;
  96. }
  97. int numNonNull = 0;
  98. T[] arrT2 = all;
  99. int n2 = arrT2.length;
  100. int n3 = 0;
  101. while (n3 < n2) {
  102. T t = arrT2[n3];
  103. if (t != null) {
  104. ++numNonNull;
  105. }
  106. ++n3;
  107. }
  108. if (!this.getAnd()) {
  109. if (all.length == 1 && all[0] != null) {
  110. return all;
  111. }
  112. int rand = Utils.random(0, numNonNull);
  113. Object[] one = (Object[])Array.newInstance(this.getReturnType(), 1);
  114. arrT = all;
  115. n = arrT.length;
  116. int n4 = 0;
  117. while (n4 < n) {
  118. T t = arrT[n4];
  119. if (t != null) {
  120. if (rand == 0) {
  121. one[0] = t;
  122. return one;
  123. }
  124. --rand;
  125. }
  126. ++n4;
  127. }
  128. assert (false);
  129. }
  130. if (numNonNull == all.length) {
  131. return all;
  132. }
  133. Object[] r = (Object[])Array.newInstance(this.getReturnType(), numNonNull);
  134. assert (r != null);
  135. int i = 0;
  136. arrT = all;
  137. n = arrT.length;
  138. int n5 = 0;
  139. while (n5 < n) {
  140. T t = arrT[n5];
  141. if (t != null) {
  142. r[i++] = t;
  143. }
  144. ++n5;
  145. }
  146. return r;
  147. }
  148.  
  149. @Nullable
  150. protected abstract T[] get(Event var1);
  151.  
  152. @Override
  153. public final boolean check(Event e, Checker<? super T> c) {
  154. return this.check(e, c, false);
  155. }
  156.  
  157. @Override
  158. public final boolean check(Event e, Checker<? super T> c, boolean negated) {
  159. return SimpleExpression.check(this.get(e), c, negated, this.getAnd());
  160. }
  161.  
  162. public static final <T> boolean check(@Nullable T[] all, Checker<? super T> c, boolean invert, boolean and) {
  163. if (all == null) {
  164. return false;
  165. }
  166. boolean hasElement = false;
  167. T[] arrT = all;
  168. int n = arrT.length;
  169. int n2 = 0;
  170. while (n2 < n) {
  171. T t = arrT[n2];
  172. if (t != null) {
  173. hasElement = true;
  174. boolean b = c.check(t);
  175. if (and && !b) {
  176. return invert;
  177. }
  178. if (!and && b) {
  179. return invert ^ true;
  180. }
  181. }
  182. ++n2;
  183. }
  184. if (!hasElement) {
  185. return false;
  186. }
  187. return invert ^ and;
  188. }
  189.  
  190. @Nullable
  191. protected /* varargs */ <R> ConvertedExpression<T, ? extends R> getConvertedExpr(Class<R> ... to) {
  192. assert (!CollectionUtils.containsSuperclass(to, this.getReturnType()));
  193. return ConvertedExpression.newInstance(this, to);
  194. }
  195.  
  196. @Nullable
  197. @Override
  198. public final /* varargs */ <R> Expression<? extends R> getConvertedExpression(Class<R> ... to) {
  199. if (CollectionUtils.containsSuperclass(to, this.getReturnType())) {
  200. return this;
  201. }
  202. return this.getConvertedExpr(to);
  203. }
  204.  
  205. @Nullable
  206. @Override
  207. public Class<?>[] acceptChange(Changer.ChangeMode mode) {
  208. Changer<?> c;
  209. ClassInfo rti = this.returnTypeInfo;
  210. if (rti == null) {
  211. this.returnTypeInfo = rti = Classes.getSuperClassInfo(this.getReturnType());
  212. }
  213. if ((c = rti.getChanger()) == null) {
  214. return null;
  215. }
  216. return c.acceptChange(mode);
  217. }
  218.  
  219. @Override
  220. public void change(Event e, @Nullable Object[] delta, Changer.ChangeMode mode) {
  221. ClassInfo<?> rti = this.returnTypeInfo;
  222. if (rti == null) {
  223. throw new UnsupportedOperationException();
  224. }
  225. Changer<?> c = rti.getChanger();
  226. if (c == null) {
  227. throw new UnsupportedOperationException();
  228. }
  229. c.change(this.getArray(e), delta, mode);
  230. }
  231.  
  232. @Override
  233. public boolean setTime(int time) {
  234. if (ScriptLoader.hasDelayBefore == Kleenean.TRUE && time != 0) {
  235. Skript.error("Can't use time states after the event has already passed");
  236. return false;
  237. }
  238. this.time = time;
  239. return false;
  240. }
  241.  
  242. protected final /* varargs */ boolean setTime(int time, Class<? extends Event> applicableEvent, Expression<?> ... mustbeDefaultVars) {
  243. if (ScriptLoader.hasDelayBefore == Kleenean.TRUE && time != 0) {
  244. Skript.error("Can't use time states after the event has already passed");
  245. return false;
  246. }
  247. if (!ScriptLoader.isCurrentEvent(applicableEvent)) {
  248. return false;
  249. }
  250. Expression<?>[] arrexpression = mustbeDefaultVars;
  251. int n = arrexpression.length;
  252. int n2 = 0;
  253. while (n2 < n) {
  254. Expression<?> var = arrexpression[n2];
  255. if (!var.isDefault()) {
  256. return false;
  257. }
  258. ++n2;
  259. }
  260. this.time = time;
  261. return true;
  262. }
  263.  
  264. protected final /* varargs */ boolean setTime(int time, Expression<?> mustbeDefaultVar, Class<? extends Event> ... applicableEvents) {
  265. if (ScriptLoader.hasDelayBefore == Kleenean.TRUE && time != 0) {
  266. Skript.error("Can't use time states after the event has already passed");
  267. return false;
  268. }
  269. if (!mustbeDefaultVar.isDefault()) {
  270. return false;
  271. }
  272. Class<? extends Event>[] arrclass = applicableEvents;
  273. int n = arrclass.length;
  274. int n2 = 0;
  275. while (n2 < n) {
  276. Class<? extends Event> e = arrclass[n2];
  277. if (ScriptLoader.isCurrentEvent(e)) {
  278. this.time = time;
  279. return true;
  280. }
  281. ++n2;
  282. }
  283. return false;
  284. }
  285.  
  286. @Override
  287. public int getTime() {
  288. return this.time;
  289. }
  290.  
  291. @Override
  292. public boolean isDefault() {
  293. return false;
  294. }
  295.  
  296. @Override
  297. public boolean isLoopOf(String s) {
  298. return false;
  299. }
  300.  
  301. @Nullable
  302. @Override
  303. public Iterator<? extends T> iterator(Event e) {
  304. return new ArrayIterator<T>(this.getArray(e));
  305. }
  306.  
  307. @Override
  308. public String toString() {
  309. return this.toString(null, false);
  310. }
  311.  
  312. @Override
  313. public Expression<?> getSource() {
  314. return this;
  315. }
  316.  
  317. @Override
  318. public Expression<? extends T> simplify() {
  319. return this;
  320. }
  321.  
  322. @Override
  323. public boolean getAnd() {
  324. return true;
  325. }
  326. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement