ch.njol.skript.registrations.Classes Java Examples

The following examples show how to use ch.njol.skript.registrations.Classes. 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: FlatFileStorage.java    From Skript with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Saves the variables.
 * <p>
 * This method uses the sorted variables map to save the variables in order.
 * 
 * @param pw
 * @param parent The parent's name with {@link Variable#SEPARATOR} at the end
 * @param map
 */
@SuppressWarnings("unchecked")
private final void save(final PrintWriter pw, final String parent, final TreeMap<String, Object> map) {
	outer: for (final Entry<String, Object> e : map.entrySet()) {
		final Object val = e.getValue();
		if (val == null)
			continue;
		if (val instanceof TreeMap) {
			save(pw, parent + e.getKey() + Variable.SEPARATOR, (TreeMap<String, Object>) val);
		} else {
			final String name = (e.getKey() == null ? parent.substring(0, parent.length() - Variable.SEPARATOR.length()) : parent + e.getKey());
			for (final VariablesStorage s : Variables.storages) {
				if (s.accept(name)) {
					if (s == this) {
						final SerializedVariable.Value value = Classes.serialize(val);
						if (value != null)
							writeCSV(pw, name, value.type, encode(value.data));
					}
					continue outer;
				}
			}
		}
	}
}
 
Example #2
Source File: VariablesStorage.java    From Skript with GNU General Public License v3.0 6 votes vote down vote up
@Nullable
protected <T> T getValue(final SectionNode n, final String key, final Class<T> type) {
	final String v = n.getValue(key);
	if (v == null) {
		Skript.error("The config is missing the entry for '" + key + "' in the database '" + databaseName + "'");
		return null;
	}
	final ParseLogHandler log = SkriptLogger.startParseLogHandler();
	try {
		final T r = Classes.parse(v, type, ParseContext.CONFIG);
		if (r == null)
			log.printError("The entry for '" + key + "' in the database '" + databaseName + "' must be " + Classes.getSuperClassInfo(type).getName().withIndefiniteArticle());
		else
			log.printLog();
		return r;
	} finally {
		log.stop();
	}
}
 
Example #3
Source File: YAMLProcessor.java    From skript-yaml with MIT License 6 votes vote down vote up
/**
 * Prepare a value for serialization, in case it's not a native type (and we
 * don't want to serialize objects as YAML represented objects).
 * 
 * @param value
 *            the value to serialize
 * @return the new object
 */
@SuppressWarnings("unchecked")
private Object serialize(Object value) {
	if (value instanceof Map) {
		for(Entry<String, Object> entry : ((Map<String, Object>) value).entrySet())
			((Map<String, Object>) value).replace(entry.getKey(), entry.getValue(), serialize(entry.getValue()));
		return value;
	} else if (value instanceof List) {
		for (int i = 0; i < ((List<Object>) value).size(); i++)
			((List<Object>) value).set(i, serialize(((List<Object>) value).get(i)));
		return value;
	} else if (!(SkriptYamlRepresenter.contains(value) || value instanceof ConfigurationSerializable || value instanceof Number || value instanceof Map || value instanceof List)) {
		SerializedVariable.Value val = Classes.serialize(value);
		if (val == null)
			return null;

		// workaround for class 'ch.njol.skript.expressions.ExprTool$1$2'
		if (val.type.equals("itemstack"))	
			return Classes.deserialize(val.type, val.data);	// returns ItemStack instead of SkriptClass

		return new SkriptClass(val.type, val.data);
	}
	return value;
}
 
Example #4
Source File: EndermanData.java    From Skript with GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("null")
@Override
@Deprecated
protected boolean deserialize(final String s) {
	if (s.isEmpty())
		return true;
	final String[] split = s.split("(?<!,),(?!,)");
	hand = new ItemType[split.length];
	for (int i = 0; i < hand.length; i++) {
		final String[] t = split[i].split("(?<!:):(?::)");
		if (t.length != 2)
			return false;
		final Object o = Classes.deserialize(t[0], t[1].replace(",,", ",").replace("::", ":"));
		if (o == null || !(o instanceof ItemType))
			return false;
		hand[i] = (ItemType) o;
	}
	return false;
}
 
Example #5
Source File: CondCompare.java    From Skript with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Attempts to parse given expression again as a literal of given type.
 * This will only work if the expression is a literal and its unparsed
 * form can be accessed.
 * @param <T> Wanted type.
 * @param type Wanted class of literal.
 * @param expr Expression we currently have.
 * @return A literal value, or null if parsing failed.
 */
@Nullable
private <T> SimpleLiteral<T> reparseLiteral(Class<T> type, Expression<?> expr) {
	if (expr instanceof SimpleLiteral) { // Only works for simple literals
		Expression<?> source = expr.getSource();
		
		// Try to get access to unparsed content of it
		if (source instanceof UnparsedLiteral) {
			String unparsed = ((UnparsedLiteral) source).getData();
			T data = Classes.parse(unparsed, type, ParseContext.DEFAULT);
			if (data != null) { // Success, let's make a literal of it
				return new SimpleLiteral<>(data, false, new UnparsedLiteral(unparsed));
			}
		}
	}
	return null; // Context-sensitive parsing failed; can't really help it
}
 
Example #6
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 #7
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 #8
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 #9
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 #10
Source File: ExprLoopYaml.java    From skript-yaml with MIT License 5 votes vote down vote up
@Override
public String toString(final @Nullable Event e, final boolean debug) {	//TODO
	if (e == null)
		return name;
	if (isYamlLoop) {
		final Object current = loop.getCurrent(e);
		Object[] objects = ((ExprYaml<?>) loop.getLoopedExpression()).get(e);
		
		if (current == null || objects == null)
			return Classes.getDebugMessage(null);

		return loopState == LoopState.INDEX ? "\"" + getIndex() + "\"" : Classes.getDebugMessage(current);
	}
	return Classes.getDebugMessage(loop.getCurrent(e));
}
 
Example #11
Source File: InventorySlot.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@Override
public String toString(@Nullable Event e, boolean debug) {
	InventoryHolder holder = invi.getHolder();
	
	if (holder instanceof BlockState)
		holder = new BlockInventoryHolder((BlockState) holder);
	
	if (invi.getHolder() != null) {
		if (invi instanceof CraftingInventory) // 4x4 crafting grid is contained in player too!
			return "crafting slot " + index + " of " + Classes.toString(holder);
		
		return "inventory slot " + index + " of " + Classes.toString(holder);
	}
	return "inventory slot " + index + " of " + Classes.toString(invi);
}
 
Example #12
Source File: EquipmentSlot.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@Override
public String toString(@Nullable Event event, boolean debug) {
	if (slotToString) // Slot to string
		return "the " + slot.name().toLowerCase(Locale.ENGLISH) + " of " + Classes.toString(e.getHolder()); // TODO localise?
	else // Contents of slot to string
		return Classes.toString(getItem());
}
 
Example #13
Source File: ExprLoopValue.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@Override
public String toString(final @Nullable Event e, final boolean debug) {
	if (e == null)
		return name;
	if (isVariableLoop) {
		@SuppressWarnings("unchecked")
		final Entry<String, Object> current = (Entry<String, Object>) loop.getCurrent(e);
		if (current == null)
			return Classes.getDebugMessage(null);
		return isIndex ? "\"" + current.getKey() + "\"" : Classes.getDebugMessage(current.getValue());
	}
	return Classes.getDebugMessage(loop.getCurrent(e));
}
 
Example #14
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 #15
Source File: DroppedItemData.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@Override
public String toString(final int flags) {
	final ItemType[] types = this.types;
	if (types == null)
		return super.toString(flags);
	final StringBuilder b = new StringBuilder();
	b.append(Noun.getArticleWithSpace(types[0].getTypes().get(0).getGender(), flags));
	b.append(m_adjective.toString(types[0].getTypes().get(0).getGender(), flags));
	b.append(" ");
	b.append(Classes.toString(types, flags & Language.NO_ARTICLE_MASK, false));
	return "" + b.toString();
}
 
Example #16
Source File: Variables.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings({"unchecked"})
@Override
@Nullable
public String getID(final @NonNull Class<?> c) {
	if (ConfigurationSerializable.class.isAssignableFrom(c) && Classes.getSuperClassInfo(c) == Classes.getExactClassInfo(Object.class))
		return configurationSerializablePrefix + ConfigurationSerialization.getAlias((Class<? extends ConfigurationSerializable>) c);
	return null;
}
 
Example #17
Source File: ThrownPotionData.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@Override
public String toString(final int flags) {
	final ItemType[] types = this.types;
	if (types == null)
		return super.toString(flags);
	final StringBuilder b = new StringBuilder();
	b.append(Noun.getArticleWithSpace(types[0].getTypes().get(0).getGender(), flags));
	b.append(m_adjective.toString(types[0].getTypes().get(0).getGender(), flags));
	b.append(" ");
	b.append(Classes.toString(types, flags & Language.NO_ARTICLE_MASK, false));
	return "" + b.toString();
}
 
Example #18
Source File: EndermanData.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@Override
public String toString(final int flags) {
	final ItemType[] hand = this.hand;
	if (hand == null)
		return super.toString(flags);
	return format.toString(super.toString(flags), Classes.toString(hand, false));
}
 
Example #19
Source File: EventValueExpression.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
@Nullable
public Class<?>[] acceptChange(final ChangeMode mode) {
	Changer<? super T> ch = changer;
	if (ch == null)
		changer = ch = (Changer<? super T>) Classes.getSuperClassInfo(c).getChanger();
	return ch == null ? null : ch.acceptChange(mode);
}
 
Example #20
Source File: FallingBlockData.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@Override
public String toString(final int flags) {
	final ItemType[] types = this.types;
	if (types == null)
		return super.toString(flags);
	final StringBuilder b = new StringBuilder();
	b.append(Noun.getArticleWithSpace(types[0].getTypes().get(0).getGender(), flags));
	b.append(m_adjective.toString(types[0].getTypes().get(0).getGender(), flags));
	b.append(" ");
	b.append(Classes.toString(types, flags & Language.NO_ARTICLE_MASK, false));
	return "" + b.toString();
}
 
Example #21
Source File: EvtEntity.java    From Skript with GNU General Public License v3.0 4 votes vote down vote up
@Override
public String toString(final @Nullable Event e, final boolean debug) {
	return "death/spawn" + (types != null ? " of " + Classes.toString(types, false) : "");
}
 
Example #22
Source File: ExprTool.java    From Skript with GNU General Public License v3.0 4 votes vote down vote up
@Override
public String toString(final @Nullable Event e, final boolean debug) {
	if (e == null)
		return "the " + (getTime() == 1 ? "future " : getTime() == -1 ? "former " : "") + "tool of " + getExpr().toString(e, debug);
	return Classes.getDebugMessage(getSingle(e));
}
 
Example #23
Source File: SimpleLiteral.java    From Skript with GNU General Public License v3.0 4 votes vote down vote up
@Override
public String toString(final @Nullable Event e, final boolean debug) {
	if (debug)
		return "[" + Classes.toString(data, getAnd(), StringMode.DEBUG) + "]";
	return Classes.toString(data, getAnd());
}
 
Example #24
Source File: EvtAtTime.java    From Skript with GNU General Public License v3.0 4 votes vote down vote up
@Override
public String toString(final @Nullable Event e, final boolean debug) {
	return "at " + Time.toString(tick) + " in worlds " + Classes.toString(worlds, true);
}
 
Example #25
Source File: EvtBlock.java    From Skript with GNU General Public License v3.0 4 votes vote down vote up
@Override
public String toString(final @Nullable Event e, final boolean debug) {
	return "break/place/burn/fade/form of " + Classes.toString(types);
}
 
Example #26
Source File: EvtMoveOn.java    From Skript with GNU General Public License v3.0 4 votes vote down vote up
@Override
	public String toString(final @Nullable Event e, final boolean debug) {
		return "walk on " + Classes.toString(types, false);
//		return "walk on " + (types != null ? Skript.toString(types, false) : "<block:" + world.getName() + ":" + x + "," + y + "," + z + ">");
	}
 
Example #27
Source File: CursorSlot.java    From Skript with GNU General Public License v3.0 4 votes vote down vote up
@Override
public String toString(@Nullable Event e, boolean debug) {
	return "cursor slot of " + Classes.toString(player);
}
 
Example #28
Source File: DroppedItemSlot.java    From Skript with GNU General Public License v3.0 4 votes vote down vote up
@Override
public String toString(@Nullable Event e, boolean debug) {
	return Classes.toString(getItem());
}
 
Example #29
Source File: Variables.java    From Skript with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private final void init() {
	// used by asserts
	info = (ClassInfo<? extends ConfigurationSerializable>) (ClassInfo) Classes.getExactClassInfo(Object.class);
}
 
Example #30
Source File: ItemFrameSlot.java    From Skript with GNU General Public License v3.0 4 votes vote down vote up
@Override
public String toString(@Nullable Event e, boolean debug) {
	return Classes.toString(getItem());
}