Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/SkriptLang/Skript
Browse files Browse the repository at this point in the history
  • Loading branch information
bensku committed Jul 5, 2018
2 parents 78f1b1a + 07829ff commit d77935b
Show file tree
Hide file tree
Showing 15 changed files with 381 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public Long[] execute(final FunctionEvent e, final Object[][] params) {
return new Long[] {(Long) params[0][0]};
return new Long[] {Math2.ceil(((Number) params[0][0]).doubleValue())};
}
}.description("Rounds a number down, i.e. returns the closest integer larger than or equal to the argument.")
}.description("Rounds a number up, i.e. returns the closest integer larger than or equal to the argument.")
.examples("ceil(2.34) = 3", "ceil(2) = 2", "ceil(2.99) = 3")
.since("2.2"));
Functions.registerFunction(new JavaFunction<Long>("ceiling", numberParam, longClass, true) {
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/ch/njol/skript/command/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -427,14 +427,10 @@ else if (aliases.get(0).isEmpty())

final String rawPermissionMessage = ScriptLoader.replaceOptions(node.get("permission message", ""));

Expression<String> permissionMessage = rawPermissionMessage.isEmpty() ?
VariableString permissionMessage = rawPermissionMessage.isEmpty() ?
null
: VariableString.newInstance(rawPermissionMessage);

if (permissionMessage != null && ((VariableString) permissionMessage).isSimple()) {
permissionMessage = new SimpleLiteral<>(rawPermissionMessage, false);
}

final SectionNode trigger = (SectionNode) node.get("trigger");
if (trigger == null)
return null;
Expand Down
23 changes: 16 additions & 7 deletions src/main/java/ch/njol/skript/command/ScriptCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import ch.njol.skript.lang.util.SimpleLiteral;
import ch.njol.skript.util.Date;
import ch.njol.skript.util.Timespan;
import ch.njol.skript.util.chat.BungeeConverter;
import ch.njol.skript.util.chat.MessageComponent;
import ch.njol.skript.variables.Variables;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
Expand Down Expand Up @@ -89,7 +91,7 @@ public class ScriptCommand implements CommandExecutor {
private final List<String> aliases;
private List<String> activeAliases;
private String permission;
private final Expression<String> permissionMessage;
private final VariableString permissionMessage;
private final String description;
@Nullable
private final Timespan cooldown;
Expand Down Expand Up @@ -126,15 +128,16 @@ public class ScriptCommand implements CommandExecutor {
*/
public ScriptCommand(final File script, final String name, final String pattern, final List<Argument<?>> arguments,
final String description, final String usage, final ArrayList<String> aliases,
final String permission, @Nullable final Expression<String> permissionMessage, @Nullable final Timespan cooldown,
final String permission, @Nullable final VariableString permissionMessage, @Nullable final Timespan cooldown,
@Nullable final VariableString cooldownMessage, final String cooldownBypass,
@Nullable VariableString cooldownStorage, final int executableBy, final List<TriggerItem> items) {
Validate.notNull(name, pattern, arguments, description, usage, aliases, items);
this.name = name;
label = "" + name.toLowerCase();
this.permission = permission;
assert permissionMessage != null;
this.permissionMessage = permissionMessage == null ?
new SimpleLiteral<>(Language.get("commands.no permission message"), false)
VariableString.newInstance(Language.get("commands.no permission message"))
: permissionMessage;

this.cooldown = cooldown;
Expand Down Expand Up @@ -174,9 +177,9 @@ private PluginCommand setupBukkitCommand() {
bukkitCommand.setDescription(description);
bukkitCommand.setLabel(label);
bukkitCommand.setPermission(permission);
// We can only set the message if it's available at parse time (aka a literal)
if (permissionMessage instanceof Literal)
bukkitCommand.setPermissionMessage(((Literal<String>) permissionMessage).getSingle());
// We can only set the message if it's simple (doesn't contains expressions)
if (permissionMessage.isSimple())
bukkitCommand.setPermissionMessage(permissionMessage.toString(null));
bukkitCommand.setUsage(usage);
bukkitCommand.setExecutor(this);
return bukkitCommand;
Expand Down Expand Up @@ -210,7 +213,13 @@ public boolean execute(final CommandSender sender, final String commandLabel, fi
final ScriptCommandEvent event = new ScriptCommandEvent(ScriptCommand.this, sender);

if (!permission.isEmpty() && !sender.hasPermission(permission)) {
sender.sendMessage(permissionMessage.getSingle(event));
if (sender instanceof Player) {
List<MessageComponent> components =
permissionMessage.getMessageComponents(event);
((Player) sender).spigot().sendMessage(BungeeConverter.convert(components));
} else {
sender.sendMessage(permissionMessage.getSingle(event));
}
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
" apply fire resistance to the victim for 30 seconds",
" remove 1 potion of fire resistance from the victim",
"# prevent mobs from dropping items under certain circumstances",
"on death;",
"on death:",
" entity is not a player",
" damage wasn't caused by a block explosion, an attack, a projectile, a potion, fire, burning, thorns or poison",
" clear drops"})
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ch/njol/skript/effects/EffMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected void execute(final Event e) {
for (CommandSender sender : recipients.getArray(e)) {
if (message instanceof VariableString && sender instanceof Player) { // this could contain json formatting
List<MessageComponent> components = ((VariableString) message).getMessageComponents(e);
((Player) sender).spigot().sendMessage(BungeeConverter.convert(components.toArray(new MessageComponent[components.size()])));
((Player) sender).spigot().sendMessage(BungeeConverter.convert(components));
} else {
String string = message.getSingle(e);
if (string != null)
Expand Down
108 changes: 108 additions & 0 deletions src/main/java/ch/njol/skript/effects/EffSendBlockChange.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
*
* Copyright 2011-2017 Peter Güttinger and contributors
*/
package ch.njol.skript.effects;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.inventory.ItemStack;
import org.eclipse.jdt.annotation.Nullable;

import ch.njol.skript.Skript;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.util.Kleenean;

@Name("Send Block Change")
@Description("Makes a player see a block as something it really isn't")
@Examples("make player see block at player as dirt")
@Since("INSERT VERSION")
public class EffSendBlockChange extends Effect {

private static final boolean SUPPORTED =
Skript.methodExists(
Player.class,
"sendBlockChange",
Location.class,
Material.class,
byte.class
);

static {
Skript.registerEffect(EffSendBlockChange.class,
"make %players% see %blocks% as %itemstack%"
);
}

@SuppressWarnings("null")
private Expression<Player> players;

@SuppressWarnings("null")
private Expression<Block> blocks;

@SuppressWarnings("null")
private Expression<ItemStack> as;

@Override
protected void execute(Event e) {
ItemStack as = this.as.getSingle(e);
if (as == null)
return;
for (Player player : players.getArray(e)) {
for (Block block : blocks.getArray(e)) {
player.sendBlockChange(block.getLocation(), as.getType(), (byte) as.getDurability());
}
}
}

@Override
public String toString(@Nullable Event e, boolean debug) {
return String.format(
"make %s see %s as %s",
players.toString(e, debug),
blocks.toString(e, debug),
as.toString(e, debug)
);
}

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
if (!SUPPORTED) {
Skript.error("The send block change effect is not supported on this version. " +
"If Spigot has added a replacement method without magic values " +
"please open an issue at https://github.com/SkriptLang/Skript/issues " +
"and support will be added for it.");
return false;
}
players = (Expression<Player>) exprs[0];
blocks = (Expression<Block>) exprs[1];
as = (Expression<ItemStack>) exprs[2];
return true;
}
}
5 changes: 4 additions & 1 deletion src/main/java/ch/njol/skript/effects/EffVisualEffect.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,11 @@ public boolean init(final Expression<?>[] exprs, final int matchedPattern, final
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()))
if (hasEntityEffect
&& (!Entity.class.isAssignableFrom(where.getReturnType())
|| where.getReturnType() == Object.class)) { // variables and similar
Skript.warning("Entity effects can only be played on entities");
}
}
return true;
}
Expand Down
69 changes: 23 additions & 46 deletions src/main/java/ch/njol/skript/expressions/ExprFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package ch.njol.skript.expressions;

import ch.njol.skript.Skript;
import ch.njol.skript.classes.Changer;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
Expand All @@ -31,11 +32,13 @@
import ch.njol.skript.lang.Literal;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.skript.registrations.Classes;
import ch.njol.skript.registrations.Converters;
import ch.njol.skript.util.LiteralUtils;
import ch.njol.skript.util.Utils;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

import java.lang.reflect.Array;
import java.util.ArrayList;
Expand All @@ -49,57 +52,41 @@
@Examples("send \"congrats on being staff!\" to all players where [player input has permission \"staff\"]")
@Since("2.2-dev36")
@SuppressWarnings({"null", "unchecked"})
public class ExprFilter<T> extends SimpleExpression<T> {
public class ExprFilter extends SimpleExpression<Object> {

private static ExprFilter<?> parsing;
private static ExprFilter parsing;

static {
Skript.registerExpression(ExprFilter.class, Object.class, ExpressionType.COMBINED,
"%objects% (where|that match) \\[<.+>\\]");
}

private ExprFilter<?> source;
private Object current;
private List<ExprInput<?>> children = new ArrayList<>();
private Class<T> superType;
private Condition condition;
private String rawCond;
private Expression<Object> objects;

public ExprFilter() {
this(null, (Class<? extends T>) Object.class);
}

public ExprFilter(ExprFilter<?> source, Class<? extends T>... types) {
this.source = source;
if (source != null) {
this.condition = source.condition;
this.rawCond = source.rawCond;
this.objects = source.objects;
this.children = source.children;
for (ExprInput<?> child : children) {
child.setParent(this);
}
}
this.superType = (Class<T>) Utils.getSuperType(types);
}

public static ExprFilter<?> getParsing() {
public static ExprFilter getParsing() {
return parsing;
}

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
parsing = this;
objects = LiteralUtils.defendExpression(exprs[0]);
rawCond = parseResult.regexes.get(0).group();
condition = Condition.parse(rawCond, "Can't understand this condition: " + rawCond);
parsing = null;
try {
parsing = this;
objects = LiteralUtils.defendExpression(exprs[0]);
rawCond = parseResult.regexes.get(0).group();
condition = Condition.parse(rawCond, "Can't understand this condition: " + rawCond);
} finally {
parsing = null;
}
return condition != null && LiteralUtils.canInitSafely(objects);
}

@Override
protected T[] get(Event e) {
protected Object[] get(Event e) {
List<Object> filtered = new ArrayList<>();
try {
for (Object object : objects.getArray(e)) {
Expand All @@ -112,7 +99,7 @@ protected T[] get(Event e) {
current = null;
}
try {
return Converters.convertStrictly(filtered.toArray(), superType);
return Converters.convertStrictly(filtered.toArray(), objects.getReturnType());
} catch (ClassCastException e1) {
return null;
}
Expand All @@ -122,23 +109,13 @@ public Object getCurrent() {
return current;
}

public void addChild(ExprInput<?> child) {
private void addChild(ExprInput<?> child) {
children.add(child);
}

@Override
public <R> Expression<? extends R> getConvertedExpression(Class<R>... to) {
return new ExprFilter<>(this, to);
}

@Override
public Expression<?> getSource() {
return source == null ? this : source;
}

@Override
public Class<? extends T> getReturnType() {
return superType;
public Class<?> getReturnType() {
return objects.getReturnType();
}

@Override
Expand All @@ -154,7 +131,7 @@ public String toString(Event e, boolean debug) {
@Override
public boolean isLoopOf(String s) {
for (ExprInput<?> child : children) { // if they used player input, let's assume loop-player is valid
if (child.getClassInfo() == null)
if (child.getClassInfo() == null || child.getClassInfo().getUserInputPatterns() == null)
continue;
for (Pattern pattern : child.getClassInfo().getUserInputPatterns()) {
if (pattern.matcher(s).matches()) {
Expand All @@ -181,7 +158,7 @@ public static class ExprInput<T> extends SimpleExpression<T> {

private ExprInput<?> source;
private Class<T> superType;
private ExprFilter<?> parent;
private ExprFilter parent;
private Literal<ClassInfo<?>> inputType;

public ExprInput() {
Expand Down Expand Up @@ -222,7 +199,7 @@ protected T[] get(Event e) {
}
}

public void setParent(ExprFilter<?> parent) {
public void setParent(ExprFilter parent) {
this.parent = parent;
}

Expand All @@ -241,7 +218,7 @@ public Class<? extends T> getReturnType() {
return superType;
}

public ClassInfo<?> getClassInfo() {
private ClassInfo<?> getClassInfo() {
return inputType == null ? null : inputType.getSingle();
}

Expand Down
Loading

0 comments on commit d77935b

Please sign in to comment.