Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ch.njol.skript.lang.util;
- import ch.njol.skript.ScriptLoader;
- import ch.njol.skript.Skript;
- import ch.njol.skript.SkriptAPIException;
- import ch.njol.skript.classes.Changer;
- import ch.njol.skript.classes.ClassInfo;
- import ch.njol.skript.lang.Expression;
- import ch.njol.skript.lang.util.ConvertedExpression;
- import ch.njol.skript.registrations.Classes;
- import ch.njol.skript.util.Utils;
- import ch.njol.util.Checker;
- import ch.njol.util.Kleenean;
- import ch.njol.util.coll.CollectionUtils;
- import ch.njol.util.coll.iterator.ArrayIterator;
- import java.lang.reflect.Array;
- import java.util.Iterator;
- import org.bukkit.event.Event;
- import org.eclipse.jdt.annotation.Nullable;
- public abstract class SimpleExpression<T>
- implements Expression<T> {
- private int time = 0;
- @Nullable
- private ClassInfo<?> returnTypeInfo;
- protected SimpleExpression() {
- }
- @Nullable
- @Override
- public final T getSingle(Event e) {
- T[] all = this.getArray(e);
- if (all.length == 0) {
- return null;
- }
- if (all.length > 1) {
- throw new SkriptAPIException("Call to getSingle() on a non-single expression");
- }
- return all[0];
- }
- @Override
- public T[] getAll(Event e) {
- T[] all = this.get(e);
- if (all == null) {
- Object[] r = (Object[])Array.newInstance(this.getReturnType(), 0);
- assert (r != null);
- return r;
- }
- if (all.length == 0) {
- return all;
- }
- int numNonNull = 0;
- T[] arrT = all;
- int n = arrT.length;
- int n2 = 0;
- while (n2 < n) {
- T t = arrT[n2];
- if (t != null) {
- ++numNonNull;
- }
- ++n2;
- }
- if (numNonNull == all.length) {
- return all;
- }
- Object[] r = (Object[])Array.newInstance(this.getReturnType(), numNonNull);
- assert (r != null);
- int i = 0;
- T[] arrT2 = all;
- int n3 = arrT2.length;
- int n4 = 0;
- while (n4 < n3) {
- T t = arrT2[n4];
- if (t != null) {
- r[i++] = t;
- }
- ++n4;
- }
- return r;
- }
- @Override
- public final T[] getArray(Event e) {
- T[] arrT;
- int n;
- T[] all = this.get(e);
- if (all == null) {
- Object[] r = (Object[])Array.newInstance(this.getReturnType(), 0);
- assert (r != null);
- return r;
- }
- if (all.length == 0) {
- return all;
- }
- int numNonNull = 0;
- T[] arrT2 = all;
- int n2 = arrT2.length;
- int n3 = 0;
- while (n3 < n2) {
- T t = arrT2[n3];
- if (t != null) {
- ++numNonNull;
- }
- ++n3;
- }
- if (!this.getAnd()) {
- if (all.length == 1 && all[0] != null) {
- return all;
- }
- int rand = Utils.random(0, numNonNull);
- Object[] one = (Object[])Array.newInstance(this.getReturnType(), 1);
- arrT = all;
- n = arrT.length;
- int n4 = 0;
- while (n4 < n) {
- T t = arrT[n4];
- if (t != null) {
- if (rand == 0) {
- one[0] = t;
- return one;
- }
- --rand;
- }
- ++n4;
- }
- assert (false);
- }
- if (numNonNull == all.length) {
- return all;
- }
- Object[] r = (Object[])Array.newInstance(this.getReturnType(), numNonNull);
- assert (r != null);
- int i = 0;
- arrT = all;
- n = arrT.length;
- int n5 = 0;
- while (n5 < n) {
- T t = arrT[n5];
- if (t != null) {
- r[i++] = t;
- }
- ++n5;
- }
- return r;
- }
- @Nullable
- protected abstract T[] get(Event var1);
- @Override
- public final boolean check(Event e, Checker<? super T> c) {
- return this.check(e, c, false);
- }
- @Override
- public final boolean check(Event e, Checker<? super T> c, boolean negated) {
- return SimpleExpression.check(this.get(e), c, negated, this.getAnd());
- }
- public static final <T> boolean check(@Nullable T[] all, Checker<? super T> c, boolean invert, boolean and) {
- if (all == null) {
- return false;
- }
- boolean hasElement = false;
- T[] arrT = all;
- int n = arrT.length;
- int n2 = 0;
- while (n2 < n) {
- T t = arrT[n2];
- if (t != null) {
- hasElement = true;
- boolean b = c.check(t);
- if (and && !b) {
- return invert;
- }
- if (!and && b) {
- return invert ^ true;
- }
- }
- ++n2;
- }
- if (!hasElement) {
- return false;
- }
- return invert ^ and;
- }
- @Nullable
- protected /* varargs */ <R> ConvertedExpression<T, ? extends R> getConvertedExpr(Class<R> ... to) {
- assert (!CollectionUtils.containsSuperclass(to, this.getReturnType()));
- return ConvertedExpression.newInstance(this, to);
- }
- @Nullable
- @Override
- public final /* varargs */ <R> Expression<? extends R> getConvertedExpression(Class<R> ... to) {
- if (CollectionUtils.containsSuperclass(to, this.getReturnType())) {
- return this;
- }
- return this.getConvertedExpr(to);
- }
- @Nullable
- @Override
- public Class<?>[] acceptChange(Changer.ChangeMode mode) {
- Changer<?> c;
- ClassInfo rti = this.returnTypeInfo;
- if (rti == null) {
- this.returnTypeInfo = rti = Classes.getSuperClassInfo(this.getReturnType());
- }
- if ((c = rti.getChanger()) == null) {
- return null;
- }
- return c.acceptChange(mode);
- }
- @Override
- public void change(Event e, @Nullable Object[] delta, Changer.ChangeMode mode) {
- ClassInfo<?> rti = this.returnTypeInfo;
- if (rti == null) {
- throw new UnsupportedOperationException();
- }
- Changer<?> c = rti.getChanger();
- if (c == null) {
- throw new UnsupportedOperationException();
- }
- c.change(this.getArray(e), delta, mode);
- }
- @Override
- public boolean setTime(int time) {
- if (ScriptLoader.hasDelayBefore == Kleenean.TRUE && time != 0) {
- Skript.error("Can't use time states after the event has already passed");
- return false;
- }
- this.time = time;
- return false;
- }
- protected final /* varargs */ boolean setTime(int time, Class<? extends Event> applicableEvent, Expression<?> ... mustbeDefaultVars) {
- if (ScriptLoader.hasDelayBefore == Kleenean.TRUE && time != 0) {
- Skript.error("Can't use time states after the event has already passed");
- return false;
- }
- if (!ScriptLoader.isCurrentEvent(applicableEvent)) {
- return false;
- }
- Expression<?>[] arrexpression = mustbeDefaultVars;
- int n = arrexpression.length;
- int n2 = 0;
- while (n2 < n) {
- Expression<?> var = arrexpression[n2];
- if (!var.isDefault()) {
- return false;
- }
- ++n2;
- }
- this.time = time;
- return true;
- }
- protected final /* varargs */ boolean setTime(int time, Expression<?> mustbeDefaultVar, Class<? extends Event> ... applicableEvents) {
- if (ScriptLoader.hasDelayBefore == Kleenean.TRUE && time != 0) {
- Skript.error("Can't use time states after the event has already passed");
- return false;
- }
- if (!mustbeDefaultVar.isDefault()) {
- return false;
- }
- Class<? extends Event>[] arrclass = applicableEvents;
- int n = arrclass.length;
- int n2 = 0;
- while (n2 < n) {
- Class<? extends Event> e = arrclass[n2];
- if (ScriptLoader.isCurrentEvent(e)) {
- this.time = time;
- return true;
- }
- ++n2;
- }
- return false;
- }
- @Override
- public int getTime() {
- return this.time;
- }
- @Override
- public boolean isDefault() {
- return false;
- }
- @Override
- public boolean isLoopOf(String s) {
- return false;
- }
- @Nullable
- @Override
- public Iterator<? extends T> iterator(Event e) {
- return new ArrayIterator<T>(this.getArray(e));
- }
- @Override
- public String toString() {
- return this.toString(null, false);
- }
- @Override
- public Expression<?> getSource() {
- return this;
- }
- @Override
- public Expression<? extends T> simplify() {
- return this;
- }
- @Override
- public boolean getAnd() {
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement