ch.njol.skript.classes.ClassInfo Java Examples

The following examples show how to use ch.njol.skript.classes.ClassInfo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Classes.java    From Skript with GNU General Public License v3.0 6 votes vote down vote up
private static byte[] getYggdrasilStart(final ClassInfo<?> c) throws NotSerializableException {
	assert Enum.class.isAssignableFrom(Kleenean.class) && Tag.getType(Kleenean.class) == Tag.T_ENUM : Tag.getType(Kleenean.class); // TODO why is this check here?
	final Tag t = Tag.getType(c.getC());
	assert t.isWrapper() || t == Tag.T_STRING || t == Tag.T_OBJECT || t == Tag.T_ENUM;
	final byte[] cn = t == Tag.T_OBJECT || t == Tag.T_ENUM ? Variables.yggdrasil.getID(c.getC()).getBytes(UTF_8) : null;
	final byte[] r = new byte[YGGDRASIL_START.length + 1 + (cn == null ? 0 : 1 + cn.length)];
	int i = 0;
	for (; i < YGGDRASIL_START.length; i++)
		r[i] = YGGDRASIL_START[i];
	r[i++] = t.tag;
	if (cn != null) {
		r[i++] = (byte) cn.length;
		for (int j = 0; j < cn.length; j++)
			r[i++] = cn[j];
	}
	assert i == r.length;
	return r;
}
 
Example #2
Source File: Classes.java    From Skript with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Parses without trying to convert anything.
 * <p>
 * Can log an error xor other log messages.
 * 
 * @param s
 * @param c
 * @return The parsed object
 */
@Nullable
public static <T> T parseSimple(final String s, final Class<T> c, final ParseContext context) {
	final ParseLogHandler log = SkriptLogger.startParseLogHandler();
	try {
		for (final ClassInfo<?> info : getClassInfos()) {
			final Parser<?> parser = info.getParser();
			if (parser == null || !parser.canParse(context) || !c.isAssignableFrom(info.getC()))
				continue;
			log.clear();
			@SuppressWarnings("unchecked")
			final T t = (T) parser.parse(s, context);
			if (t != null) {
				log.printLog();
				return t;
			}
		}
		log.printError();
	} finally {
		log.stop();
	}
	return null;
}
 
Example #3
Source File: SkriptParser.java    From Skript with GNU General Public License v3.0 6 votes vote down vote up
public static String notOfType(final ClassInfo<?>... cs) {
	if (cs.length == 1) {
		return Language.get("not") + " " + cs[0].getName().withIndefiniteArticle();
	} else {
		final StringBuilder b = new StringBuilder(Language.get("neither") + " ");
		for (int k = 0; k < cs.length; k++) {
			if (k != 0) {
				if (k != cs.length - 1)
					b.append(", ");
				else
					b.append(" " + Language.get("nor") + " ");
			}
			b.append(cs[k].getName().withIndefiniteArticle());
		}
		return "" + b.toString();
	}
}
 
Example #4
Source File: Classes.java    From Skript with GNU General Public License v3.0 6 votes vote down vote up
@Nullable
public static Object deserialize(final ClassInfo<?> type, InputStream value) {
	Serializer<?> s;
	assert (s = type.getSerializer()) != null && (s.mustSyncDeserialization() ? Bukkit.isPrimaryThread() : true) : type + "; " + s + "; " + Bukkit.isPrimaryThread();
	YggdrasilInputStream in = null;
	try {
		value = new SequenceInputStream(new ByteArrayInputStream(getYggdrasilStart(type)), value);
		in = Variables.yggdrasil.newInputStream(value);
		return in.readObject();
	} catch (final IOException e) { // i.e. invalid save
		if (Skript.testing())
			e.printStackTrace();
		return null;
	} finally {
		if (in != null) {
			try {
				in.close();
			} catch (final IOException e) {}
		}
		try {
			value.close();
		} catch (final IOException e) {}
	}
}
 
Example #5
Source File: Classes.java    From Skript with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @param info info about the class to register
 */
public static <T> void registerClass(final ClassInfo<T> info) {
	try {
		Skript.checkAcceptRegistrations();
		if (classInfosByCodeName.containsKey(info.getCodeName()))
			throw new IllegalArgumentException("Can't register " + info.getC().getName() + " with the code name " + info.getCodeName() + " because that name is already used by " + classInfosByCodeName.get(info.getCodeName()));
		if (exactClassInfos.containsKey(info.getC()))
			throw new IllegalArgumentException("Can't register the class info " + info.getCodeName() + " because the class " + info.getC().getName() + " is already registered");
		if (info.getCodeName().length() > DatabaseStorage.MAX_CLASS_CODENAME_LENGTH)
			throw new IllegalArgumentException("The codename '" + info.getCodeName() + "' is too long to be saved in a database, the maximum length allowed is " + DatabaseStorage.MAX_CLASS_CODENAME_LENGTH);
		exactClassInfos.put(info.getC(), info);
		classInfosByCodeName.put(info.getCodeName(), info);
		tempClassInfos.add(info);
	} catch (RuntimeException e) {
		if (SkriptConfig.apiSoftExceptions.value())
			Skript.warning("Ignored an exception due to user configuration: " + e.getMessage());
		else
			throw e;
	}
}
 
Example #6
Source File: HTMLGenerator.java    From Skript with GNU General Public License v3.0 6 votes vote down vote up
@Override
public int compare(@Nullable ClassInfo<?> o1, @Nullable ClassInfo<?> o2) {
	// Nullness check
	if (o1 == null || o2 == null) {
		assert false;
		throw new NullPointerException();
	}
	
	String name1 = o1.getDocName();
	if (name1 == null)
		name1 = o1.getCodeName();
	String name2 = o2.getDocName();
	if (name2 == null)
		name2 = o2.getCodeName();
	
	return name1.compareTo(name2);
}
 
Example #7
Source File: Documentation.java    From Skript with GNU General Public License v3.0 6 votes vote down vote up
private static void insertClass(final PrintWriter pw, final ClassInfo<?> info) {
	if (info.getDocName() == ClassInfo.NO_DOC)
		return;
	if (info.getDocName() == null || info.getDescription() == null || info.getUsage() == null || info.getExamples() == null || info.getSince() == null) {
		Skript.warning("Class " + info.getCodeName() + " is missing information");
		return;
	}
	final String desc = validateHTML(StringUtils.join(info.getDescription(), "<br/>"), "classes");
	final String usage = validateHTML(StringUtils.join(info.getUsage(), "<br/>"), "classes");
	final String since = info.getSince() == null ? "" : validateHTML(info.getSince(), "classes");
	if (desc == null || usage == null || since == null) {
		Skript.warning("Class " + info.getCodeName() + "'s description, usage or 'since' is invalid");
		return;
	}
	final String patterns = info.getUserInputPatterns() == null ? "" : convertRegex(StringUtils.join(info.getUserInputPatterns(), "\n"));
	insertOnDuplicateKeyUpdate(pw, "classes",
			"id, name, description, patterns, `usage`, examples, since",
			"patterns = TRIM(LEADING '\n' FROM CONCAT(patterns, '\n', '" + escapeSQL(patterns) + "'))",
			escapeHTML(info.getCodeName()),
			escapeHTML(info.getDocName()),
			desc,
			patterns,
			usage,
			escapeHTML(StringUtils.join(info.getExamples(), "\n")),
			since);
}
 
Example #8
Source File: Classes.java    From Skript with GNU General Public License v3.0 6 votes vote down vote up
/**
 * As the name implies
 * 
 * @param name
 * @return the class info or null if the name was not recognised
 */
@Nullable
public static ClassInfo<?> getClassInfoFromUserInput(String name) {
	checkAllowClassInfoInteraction();
	name = "" + name.toLowerCase();
	for (final ClassInfo<?> ci : getClassInfos()) {
		final Pattern[] uip = ci.getUserInputPatterns();
		if (uip == null)
			continue;
		for (final Pattern pattern : uip) {
			if (pattern.matcher(name).matches())
				return ci;
		}
	}
	return null;
}
 
Example #9
Source File: Classes.java    From Skript with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Gets the class info of the given class or its closest registered superclass. This method will never return null unless <tt>c</tt> is null.
 * 
 * @param c
 * @return The closest superclass's info
 */
@SuppressWarnings({"unchecked", "null"})
public static <T> ClassInfo<? super T> getSuperClassInfo(final Class<T> c) {
	assert c != null;
	checkAllowClassInfoInteraction();
	final ClassInfo<?> i = superClassInfos.get(c);
	if (i != null)
		return (ClassInfo<? super T>) i;
	for (final ClassInfo<?> ci : getClassInfos()) {
		if (ci.getC().isAssignableFrom(c)) {
			if (!Skript.isAcceptRegistrations())
				superClassInfos.put(c, ci);
			return (ClassInfo<? super T>) ci;
		}
	}
	assert false;
	return null;
}
 
Example #10
Source File: Classes.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@Nullable
public static Object deserialize(final String type, final byte[] value) {
	final ClassInfo<?> ci = getClassInfoNoError(type);
	if (ci == null)
		return null;
	return deserialize(ci, new ByteArrayInputStream(value));
}
 
Example #11
Source File: ConvertedExpression.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@Override
@Nullable
public Class<?>[] acceptChange(final ChangeMode mode) {
	final Class<?>[] r = source.acceptChange(mode);
	if (r == null) {
		ClassInfo<? super T> rti = returnTypeInfo;
		returnTypeInfo = rti = Classes.getSuperClassInfo(getReturnType());
		final Changer<?> c = rti.getChanger();
		return c == null ? null : c.acceptChange(mode);
	}
	return r;
}
 
Example #12
Source File: EffReturn.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
	final ScriptFunction<?> f = Functions.currentFunction;
	if (f == null) {
		Skript.error("The return statement can only be used in a function");
		return false;
	}
	if (!isDelayed.isFalse()) {
		Skript.error("A return statement after a delay is useless, as the calling trigger will resume when the delay starts (and won't get any returned value)");
		return false;
	}
	function = f;
	final ClassInfo<?> rt = function.getReturnType();
	if (rt == null) {
		Skript.error("This function doesn't return any value. Please use 'stop' or 'exit' if you want to stop the function.");
		return false;
	}
	final RetainingLogHandler log = SkriptLogger.startRetainingLog();
	final Expression<?> v;
	try {
		v = exprs[0].getConvertedExpression(rt.getC());
		if (v == null) {
			log.printErrors("This function is declared to return " + rt.getName().withIndefiniteArticle() + ", but " + exprs[0].toString(null, false) + " is not of that type.");
			return false;
		}
		log.printLog();
	} finally {
		log.stop();
	}
	if (f.isSingle() && !v.isSingle()) {
		Skript.error("This function is defined to only return a single " + rt.toString() + ", but this return statement can return multiple values.");
		return false;
	}
	value = v;
	return true;
}
 
Example #13
Source File: Signature.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
public Signature(String script, String name, Parameter<?>[] parameters, @Nullable final ClassInfo<T> returnType, boolean single) {
	this.script = script;
	this.name = name;
	this.parameters = parameters;
	this.returnType = returnType;
	this.single = single;
	
	calls = new ArrayList<>();
}
 
Example #14
Source File: Parameter.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("null")
public Parameter(final String name, final ClassInfo<T> type, final boolean single, final @Nullable Expression<? extends T> def) {
	this.name = name != null ? name.toLowerCase() : null;
	this.type = type;
	this.def = def;
	this.single = single;
}
 
Example #15
Source File: Option.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
public Option(final String key, final T defaultValue) {
	this.key = "" + key.toLowerCase(Locale.ENGLISH);
	this.defaultValue = defaultValue;
	parsedValue = defaultValue;
	@SuppressWarnings("unchecked")
	final Class<T> c = (Class<T>) defaultValue.getClass();
	if (c == String.class) {
		parser = new Converter<String, T>() {
			@SuppressWarnings("unchecked")
			@Override
			public T convert(final String s) {
				return (T) s;
			}
		};
	} else {
		final ClassInfo<T> ci = Classes.getExactClassInfo(c);
		final Parser<? extends T> p;
		if (ci == null || (p = ci.getParser()) == null)
			throw new IllegalArgumentException(c.getName());
		this.parser = new Converter<String, T>() {
			@Override
			@Nullable
			public T convert(final String s) {
				final T t = p.parse(s, ParseContext.CONFIG);
				if (t != null)
					return t;
				Skript.error("'" + s + "' is not " + ci.getName().withIndefiniteArticle());
				return null;
			}
		};
	}
}
 
Example #16
Source File: SimpleLiteral.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void change(final Event e, final @Nullable Object[] delta, final ChangeMode mode) throws UnsupportedOperationException {
	final ClassInfo<? super T> rti = returnTypeInfo;
	if (rti == null)
		throw new UnsupportedOperationException();
	final Changer<? super T> c = rti.getChanger();
	if (c == null)
		throw new UnsupportedOperationException();
	c.change(getArray(), delta, mode);
}
 
Example #17
Source File: SimpleLiteral.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@Override
@Nullable
public Class<?>[] acceptChange(final ChangeMode mode) {
	ClassInfo<? super T> rti = returnTypeInfo;
	if (rti == null)
		returnTypeInfo = rti = Classes.getSuperClassInfo(getReturnType());
	final Changer<? super T> c = rti.getChanger();
	return c == null ? null : c.acceptChange(mode);
}
 
Example #18
Source File: ConvertedExpression.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void change(final Event e, final @Nullable Object[] delta, final ChangeMode mode) {
	final ClassInfo<? super T> rti = returnTypeInfo;
	if (rti != null) {
		final Changer<? super T> c = rti.getChanger();
		if (c != null)
			c.change(getArray(e), delta, mode);
	} else {
		source.change(e, delta, mode);
	}
}
 
Example #19
Source File: SimpleExpression.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@Override
@Nullable
public Class<?>[] acceptChange(final ChangeMode mode) {
	ClassInfo<?> rti = returnTypeInfo;
	if (rti == null)
		returnTypeInfo = rti = Classes.getSuperClassInfo(getReturnType());
	final Changer<?> c = rti.getChanger();
	if (c == null)
		return null;
	return c.acceptChange(mode);
}
 
Example #20
Source File: SimpleExpression.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void change(final Event e, final @Nullable Object[] delta, final ChangeMode mode) {
	final ClassInfo<?> rti = returnTypeInfo;
	if (rti == null)
		throw new UnsupportedOperationException();
	final Changer<?> c = rti.getChanger();
	if (c == null)
		throw new UnsupportedOperationException();
	((Changer<T>) c).change(getArray(e), delta, mode);
}
 
Example #21
Source File: Classes.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Gets the name a class was registered with.
 * 
 * @param c The exact class
 * @return The name of the class or null if the given class wasn't registered.
 */
@Nullable
public static String getExactClassName(final Class<?> c) {
	checkAllowClassInfoInteraction();
	final ClassInfo<?> ci = exactClassInfos.get(c);
	return ci == null ? null : ci.getCodeName();
}
 
Example #22
Source File: Classes.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Gets the default expression of a class
 * 
 * @param c The class
 * @return The expression holding the default value or null if this class doesn't have one
 */
@Nullable
public static <T> DefaultExpression<T> getDefaultExpression(final Class<T> c) {
	checkAllowClassInfoInteraction();
	final ClassInfo<T> ci = getExactClassInfo(c);
	return ci == null ? null : ci.getDefaultExpression();
}
 
Example #23
Source File: Classes.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
/**
 * As the name implies
 * 
 * @param name
 * @return the class or null if the name was not recognized
 */
@Nullable
public static Class<?> getClassFromUserInput(final String name) {
	checkAllowClassInfoInteraction();
	final ClassInfo<?> ci = getClassInfoFromUserInput(name);
	return ci == null ? null : ci.getC();
}
 
Example #24
Source File: Argument.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
private Argument(@Nullable final String name, final @Nullable Expression<? extends T> def, final ClassInfo<T> type, final boolean single, final int index, final boolean optional) {
	this.name = name;
	this.def = def;
	this.type = type;
	this.single = single;
	this.index = index;
	this.optional = optional;
}
 
Example #25
Source File: Classes.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("null")
public static List<ClassInfo<?>> getClassInfos() {
	checkAllowClassInfoInteraction();
	final ClassInfo<?>[] ci = classInfos;
	if (ci == null)
		return Collections.emptyList();
	return Collections.unmodifiableList(Arrays.asList(ci));
}
 
Example #26
Source File: Classes.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings({"null", "unused"})
private static void removeNullElements() {
	Iterator<ClassInfo<?>> it = tempClassInfos.iterator();
	while (it.hasNext()) {
		ClassInfo<?> ci = it.next();
		if (ci.getC() == null)
			it.remove();
	}
}
 
Example #27
Source File: ExprEventExpression.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parser) {
	@SuppressWarnings("unchecked")
	final ClassInfo<?> ci = ((Literal<ClassInfo<?>>) exprs[0]).getSingle();
	final EventValueExpression<?> e = new EventValueExpression<Object>(ci.getC());
	setExpr(e);
	return e.init();
}
 
Example #28
Source File: ExprRandom.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
	final Expression<?> expr = exprs[1].getConvertedExpression((((Literal<ClassInfo<?>>) exprs[0]).getSingle()).getC());
	if (expr == null)
		return false;
	this.expr = expr;
	return true;
}
 
Example #29
Source File: SimpleExpressionFork.java    From skript-yaml with MIT License 5 votes vote down vote up
@Override
@Nullable
public Class<?>[] acceptChange(final ChangeMode mode) {
	ClassInfo<?> rti = returnTypeInfo;
	if (rti == null)
		returnTypeInfo = rti = Classes.getSuperClassInfo(getReturnType());
	final Changer<?> c = rti.getChanger();
	if (c == null)
		return null;
	return c.acceptChange(mode);
}
 
Example #30
Source File: ExprFilter.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
	parent = ExprFilter.getParsing();
	if (parent == null) {
		return false;
	}
	parent.addChild(this);
	inputType = matchedPattern == 0 ? null : ((Literal<ClassInfo<?>>) exprs[0]).getSingle();
	return true;
}