ch.njol.skript.lang.Literal Java Examples

The following examples show how to use ch.njol.skript.lang.Literal. 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: CondCanHold.java    From Skript with GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings({"unchecked", "null"})
@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parser) {
	invis = (Expression<Inventory>) exprs[0];
	items = (Expression<ItemType>) exprs[1];
	if (items instanceof Literal) {
		for (ItemType t : ((Literal<ItemType>) items).getAll()) {
			t = t.getItem();
			if (!(t.isAll() || t.getTypes().size() == 1)) {
				Skript.error("The condition 'can hold' can currently only be used with aliases that start with 'every' or 'all', or only represent one item.", ErrorQuality.SEMANTIC_ERROR);
				return false;
			}
		}
	}
	setNegated(matchedPattern == 1);
	return true;
}
 
Example #2
Source File: TropicalFishData.java    From Skript with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected boolean init(Literal<?>[] exprs, int matchedPattern, ParseResult parseResult) {
	if (exprs.length == 0)
		return true; // FIXME aliases reloading must work
	
	if (exprs[2] != null) {
		bodyColor = ((Literal<Color>) exprs[2]).getSingle().asDyeColor();
		patternColor = bodyColor;
	}

	if (exprs[0] != null)
		bodyColor = ((Literal<Color>) exprs[0]).getSingle().asDyeColor();
	if (exprs[1] != null)
		patternColor = ((Literal<Color>) exprs[1]).getSingle().asDyeColor();

	return true;
}
 
Example #3
Source File: HorseData.java    From Skript with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected boolean init(final Literal<?>[] exprs, final int matchedPattern, final ParseResult parseResult) {
	switch (matchedPattern) { // If Variant ordering is changed, will not break
		case 0:
			variant = Variant.HORSE;
			break;
		case 1:
			variant = Variant.DONKEY;
			break;
		case 2:
			variant = Variant.MULE;
			break;
		case 3:
			variant = Variant.UNDEAD_HORSE;
			break;
		case 4:
			variant = Variant.SKELETON_HORSE;
			break;
	}
	
	return true;
}
 
Example #4
Source File: ExprTimes.java    From Skript with GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
	end = matchedPattern == 0 ? (Expression<Number>) exprs[0] : new SimpleLiteral<>(matchedPattern, false);
	
	if (end instanceof Literal) {
		int amount = ((Literal<Number>) end).getSingle().intValue();
		if (amount == 0 && isInLoop()) {
			Skript.warning("Looping zero times makes the code inside of the loop useless");
		} else if (amount == 1 & isInLoop()) {
			Skript.warning("Since you're looping exactly one time, you could simply remove the loop instead");
		} else if (amount < 0) {
			if (isInLoop())
				Skript.error("Looping a negative amount of times is impossible");
			else
				Skript.error("The times expression only supports positive numbers");
			return false;
		}
	}
	return true;
}
 
Example #5
Source File: ExprAmount.java    From Skript with GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("null")
@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
	expr = exprs[0];
	if (expr instanceof Literal)
		return false;
	if (expr.isSingle()) {
		Skript.error("'" + expr.toString(null, false) + "' can only ever have one value at most, thus the 'amount of ...' expression is useless. Use '... exists' instead to find out whether the expression has a value.");
		return false;
	}
	this.recursive = matchedPattern == 1;
	if (recursive && !(expr instanceof Variable<?>)) {
		Skript.error("Getting the recursive size of a list only applies to variables, thus the '" + expr.toString(null, false) + "' expression is useless.");
		return false;
	}
	return true;
}
 
Example #6
Source File: ExprEntities.java    From Skript with GNU General Public License v3.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
	types = (Expression<? extends EntityData<?>>) exprs[0];
	if (matchedPattern % 2 == 0) {
		for (EntityData<?> d : ((Literal<EntityData<?>>) types).getAll()) {
			if (d.isPlural().isFalse() || d.isPlural().isUnknown() && !StringUtils.startsWithIgnoreCase(parseResult.expr, "all"))
				return false;
		}
	}
	isUsingRadius = matchedPattern >= 2;
	if (isUsingRadius) {
		radius = (Expression<Number>) exprs[exprs.length - 2];
		center = (Expression<Location>) exprs[exprs.length - 1];
	} else {
		if (parseResult.mark == 1) {
			chunks = (Expression<Chunk>) exprs[2];
		} else {
			worlds = (Expression<World>) exprs[1];
		}
	}
	if (types instanceof Literal && ((Literal<EntityData<?>>) types).getAll().length == 1)
		returnType = ((Literal<EntityData<?>>) types).getSingle().getType();
	return true;
}
 
Example #7
Source File: ThrownPotionData.java    From Skript with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected boolean init(final Literal<?>[] exprs, final int matchedPattern, final ParseResult parseResult) {
	if (exprs.length > 0 && exprs[0] != null) {
		if ((Converters.convert((ItemType[]) exprs[0].getAll(), ItemType.class, new Converter<ItemType, ItemType>() {
			@Override
			@Nullable
			public ItemType convert(final ItemType t) {
				ItemType r = null;
				for (final ItemData d : t.getTypes()) {
					if (d.getType() == Material.POTION) {
						if (r == null)
							r = new ItemType(d);
						else
							r.add(d);
					}
				}
				return r;
			}
		})).length == 0) {
			return false; // no error message - other things can be thrown as well
		}
	}
	return false;
}
 
Example #8
Source File: Delay.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings({"unchecked", "null"})
@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
	duration = (Expression<Timespan>) exprs[0];
	if (duration instanceof Literal) { // If we can, do sanity check for delays
		long millis = ((Literal<Timespan>) duration).getSingle().getMilliSeconds();
		if (millis < 50) {
			Skript.warning("Delays less than one tick are not possible, defaulting to one tick.");
		}
	}
	return true;
}
 
Example #9
Source File: EvtRegionBorder.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public boolean init(final Literal<?>[] args, final int matchedPattern, final ParseResult parseResult) {
	enter = parseResult.mark == 0;
	regions = args.length == 0 ? null : (Literal<Region>) args[0];
	return true;
}
 
Example #10
Source File: EffVisualEffect.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings({"unchecked", "null"})
@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
	int base = 0;
	if (matchedPattern == 1) {
		count = (Expression<Number>) exprs[0];
		base = 1;
	}
	
	effects = (Expression<VisualEffect>) exprs[base];
	direction = (Expression<Direction>) exprs[base + 1];
	where = exprs[base + 2];
	if (exprs[base + 3] != null) {
		if (exprs[3].getReturnType() == Player.class)
			players = (Expression<Player>) exprs[base + 3];
		else
			radius = (Expression<Number>) exprs[base + 3];
	}
	if (effects instanceof Literal) {
		final VisualEffect[] effs = effects.getAll(null);
		boolean hasLocationEffect = false, hasEntityEffect = false;
		for (final VisualEffect e : effs) {
			if (e.isEntityEffect())
				hasEntityEffect = true;
			else
				hasLocationEffect = true;
		}
		if (!hasLocationEffect && players != null)
			Skript.warning("Entity effects are visible to all players");
		if (!hasLocationEffect && !direction.isDefault())
			Skript.warning("Entity effects are always played on an entity");
		if (hasEntityEffect && !Entity.class.isAssignableFrom(where.getReturnType()))
			Skript.warning("Entity effects can only be played on entities");
	}
	return true;
}
 
Example #11
Source File: EvtResourcePackResponse.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public boolean init(final Literal<?>[] args, final int matchedPattern, final ParseResult parser) {
	if (matchedPattern == 1)
		states = (Literal<Status>) args[0];
	return true;
}
 
Example #12
Source File: EvtExperienceSpawn.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean init(final Literal<?>[] args, final int matchedPattern, final ParseResult parseResult) {
	if (!Skript.isRunningMinecraft(1, 4, 5)) {
		Skript.error("The experience spawn event can only be used in Minecraft 1.4.5 and later");
		return false;
	}
	return true;
}
 
Example #13
Source File: EvtSkript.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean init(final Literal<?>[] args, final int matchedPattern, final ParseResult parser) {
	isStart = matchedPattern == 0;
	if (parser.mark == 0) {
		Skript.warning("Server start/stop events are actually called when Skript is started or stopped. It is thus recommended to use 'on Skript start/stop' instead.");
	}
	return true;
}
 
Example #14
Source File: EvtClick.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean init(final Literal<?>[] args, final int matchedPattern, final ParseResult parser) {
	click = parser.mark == 0 ? ANY : parser.mark;
	types = args[matchedPattern];
	if (types != null && !ItemType.class.isAssignableFrom(types.getReturnType())) {
		if (click == LEFT) {
			Skript.error("A leftclick on an entity is an attack and thus not covered by the 'click' event, but the 'damage' event.", ErrorQuality.SEMANTIC_ERROR);
			return false;
		} else if (click == ANY) {
			Skript.warning("A leftclick on an entity is an attack and thus not covered by the 'click' event, but the 'damage' event. Change this event to a rightclick to disable this warning message.");
		}
	}
	tools = (Literal<ItemType>) args[1 - matchedPattern];
	return true;
}
 
Example #15
Source File: EvtMoveOn.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@Override
	public boolean init(final Literal<?>[] args, final int matchedPattern, final ParseResult parser) {
//		if (parser.regexes.get(0).group().equalsIgnoreCase("<block>")/* && isValidatingInput*/)
//			return true;
//		final Matcher m = Pattern.compile("<block:(.+)>").matcher(parser.regexes.get(0).group());
//		if (m.matches()) {
//			final Block b = (Block) Skript.deserialize("block", m.group(1));
//			if (b == null)
//				return false;
//			world = b.getWorld();
//			x = b.getX();
//			y = b.getY();
//			z = b.getZ();
//		} else {
		@SuppressWarnings("unchecked")
		final Literal<? extends ItemType> l = (Literal<? extends ItemType>) args[0];//SkriptParser.parseLiteral(parser.regexes.get(0).group(), ItemType.class, ParseContext.EVENT);
		if (l == null)
			return false;
		types = l.getAll();
		for (final ItemType t : types) {
			if (t.isAll()) {
				Skript.error("Can't use an 'on walk' event with an alias that matches all blocks");
				return false;
			}
			boolean hasBlock = false;
			for (final ItemData d : t) {
				if (d.getType().isBlock() && d.getType() != Material.AIR) // don't allow air
					hasBlock = true;
			}
			if (!hasBlock) {
				Skript.error(t + " is not a block and can thus not be walked on");
				return false;
			}
		}
//		}
		return true;
	}
 
Example #16
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 #17
Source File: EvtGrow.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public boolean init(final Literal<?>[] args, final int matchedPattern, final ParseResult parser) {
	evtType = parser.mark; // ANY, STRUCTURE or BLOCK
	if (evtType == STRUCTURE)
		types = (Literal<StructureType>) args[0];
	else if (evtType == BLOCK)
		blocks = (Literal<ItemType>) args[1]; // Arg 1 may not be present... but it is in the array still, as null
	// Else: no type restrictions specified
	return true;
}
 
Example #18
Source File: EvtItem.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public boolean init(final Literal<?>[] args, final int matchedPattern, final ParseResult parser) {
	types = (Literal<ItemType>) args[0];
	entity = parser.mark == 1;
	return true;
}
 
Example #19
Source File: EvtTestCase.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings({"null", "unchecked"})
@Override
public boolean init(Literal<?>[] args, int matchedPattern, SkriptParser.ParseResult parseResult) {
	name = (Expression<String>) args[0];
	if (!parseResult.regexes.isEmpty()) { // Do not parse or run unless condition is met
		String cond = parseResult.regexes.get(0).group();
		condition = Condition.parse(cond, "Can't understand this condition: " + cond);
	}
	return true;
}
 
Example #20
Source File: EvtPlantGrowth.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public boolean init(Literal<?>[] args, int matchedPattern, ParseResult parseResult) {
	types = (Literal<ItemType>) args[0];
	
	return true;
}
 
Example #21
Source File: ConvertedLiteral.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
@Nullable
public <R> Literal<? extends R> getConvertedExpression(final Class<R>... to) {
	if (CollectionUtils.containsSuperclass(to, this.to))
		return (Literal<? extends R>) this;
	return ((Literal<F>) source).getConvertedExpression(to);
}
 
Example #22
Source File: ConvertedLiteral.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public ConvertedLiteral(final Literal<F> source, final T[] data, final Class<T> to) {
	super(source, to, new ConverterInfo<>((Class<F>) source.getReturnType(), to, new Converter<F, T>() {
		@Override
		@Nullable
		public T convert(final F f) {
			assert false;
			return Converters.convert(f, to);
		}
	}, 0));
	this.data = data;
	assert data.length > 0;
}
 
Example #23
Source File: LiteralUtils.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Checks an {@link Expression} for {@link UnparsedLiteral} objects
 * and converts them if found.
 *
 * @param expr The expression to check for {@link UnparsedLiteral} objects
 * @param <T>  {@code expr}'s type
 * @return {@code expr} without {@link UnparsedLiteral} objects
 */
@SuppressWarnings("unchecked")
public static <T> Expression<T> defendExpression(Expression<?> expr) {
	if (expr instanceof ExpressionList) {
		Expression<?>[] expressions = ((ExpressionList) expr).getExpressions();
		for (int i = 0; i < expressions.length; i++)
			expressions[i] = LiteralUtils.defendExpression(expressions[i]);
	} else if (expr instanceof UnparsedLiteral) {
		Literal<?> parsedLiteral = ((UnparsedLiteral) expr).getConvertedExpression(Object.class);
		return (Expression<T>) (parsedLiteral == null ? expr : parsedLiteral);
	}
	return (Expression<T>) expr;
}
 
Example #24
Source File: ExprXOf.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings({"unchecked", "null"})
@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
	setExpr(exprs[1]);
	amount = (Expression<Number>) exprs[0];
	if (amount instanceof Literal && getExpr() instanceof Literal)// "x of y" is also an ItemType syntax
		return false;
	return true;
}
 
Example #25
Source File: ExprItems.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("null")
@Override
protected ItemStack[] get(final Event e) {
	if (buffer != null)
		return buffer;
	final ArrayList<ItemStack> r = new ArrayList<>();
	for (final ItemStack is : new IteratorIterable<>(iterator(e)))
		r.add(is);
	if (types instanceof Literal)
		return buffer = r.toArray(new ItemStack[r.size()]);
	return r.toArray(new ItemStack[r.size()]);
}
 
Example #26
Source File: ExprItems.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public boolean init(final Expression<?>[] vars, final int matchedPattern, final Kleenean isDelayed, final ParseResult parser) {
	if (vars.length > 0)
		types = (Expression<ItemType>) vars[0];
	blocks = matchedPattern >= 2;
	if (types instanceof Literal) {
		for (final ItemType t : ((Literal<ItemType>) types).getAll())
			t.setAll(true);
	}
	return true;
}
 
Example #27
Source File: ExprClicked.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 parseResult) {
	clickable = ClickableType.getClickable(parseResult.mark);
	switch (clickable) {
		case BLOCK_AND_ITEMS:
			final Object type = exprs[0] == null ? null : ((Literal<?>) exprs[0]).getSingle();
			if (type instanceof EntityData) {
				entityType = (EntityData<?>) type;
				if (!ScriptLoader.isCurrentEvent(PlayerInteractEntityEvent.class) && !ScriptLoader.isCurrentEvent(PlayerInteractAtEntityEvent.class)) {
					Skript.error("The expression 'clicked entity' may only be used in a click event", ErrorQuality.SEMANTIC_ERROR);
					return false;
				}
			} else {
				itemType = (ItemType) type;
				if (!ScriptLoader.isCurrentEvent(PlayerInteractEvent.class)) {
					Skript.error("The expression 'clicked block' may only be used in a click event", ErrorQuality.SEMANTIC_ERROR);
					return false;
				}
			}
			break;
		case INVENTORY:
		case ACTION:
		case TYPE:
		case SLOT:
			if (!ScriptLoader.isCurrentEvent(InventoryClickEvent.class)) {
				Skript.error("The expression '" + clickable.getName() + "' may only be used in an inventory click event", ErrorQuality.SEMANTIC_ERROR);
				return false;
			}
			break;
	}
	return true;
}
 
Example #28
Source File: ExprLastSpawnedEntity.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) {
	if (parseResult.mark == 2) // It's just to make an extra expression for item only
		type = EntityData.fromClass(Item.class);
	else 
		type = ((Literal<EntityData<?>>) exprs[0]).getSingle();
	from = parseResult.mark;
	return true;
}
 
Example #29
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 #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;
}