Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loudly Failing 🗣️ Part: Effects #7417

Open
wants to merge 8 commits into
base: dev/feature
Choose a base branch
from
9 changes: 3 additions & 6 deletions src/main/java/ch/njol/skript/effects/Delay.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,25 @@ public class Delay extends Effect {
Skript.registerEffect(Delay.class, "(wait|halt) [for] %timespan%");
}

@SuppressWarnings("NotNullFieldNotInitialized")
protected Expression<Timespan> duration;

@SuppressWarnings({"unchecked", "null"})
@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
getParser().setHasDelayBefore(Kleenean.TRUE);

duration = (Expression<Timespan>) exprs[0];
if (duration instanceof Literal) { // If we can, do sanity check for delays
long millis = ((Literal<Timespan>) duration).getSingle().getAs(Timespan.TimePeriod.MILLISECOND);
if (millis < 50) {
if (millis < 50)
Skript.warning("Delays less than one tick are not possible, defaulting to one tick.");
}
}

return true;
}

@Override
@Nullable
protected TriggerItem walk(Event event) {
protected @Nullable TriggerItem walk(Event event) {
debug(event, true);
long start = Skript.debug() ? System.nanoTime() : 0;
TriggerItem next = getNext();
Expand Down
23 changes: 16 additions & 7 deletions src/main/java/ch/njol/skript/effects/EffActionBar.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package ch.njol.skript.effects;

import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

import ch.njol.skript.Skript;
import ch.njol.skript.config.Node;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
Expand All @@ -17,24 +14,29 @@
import ch.njol.util.Kleenean;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.BaseComponent;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.log.runtime.SyntaxRuntimeErrorProducer;

@Name("Action Bar")
@Description("Sends an action bar message to the given player(s).")
@Examples("send action bar \"Hello player!\" to player")
@Since("2.3")
public class EffActionBar extends Effect {
public class EffActionBar extends Effect implements SyntaxRuntimeErrorProducer {

static {
Skript.registerEffect(EffActionBar.class, "send [the] action[ ]bar [with text] %string% [to %players%]");
}

private Node node;
private Expression<String> message;

private Expression<Player> recipients;

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
node = getParser().getNode();
message = (Expression<String>) exprs[0];
recipients = (Expression<Player>) exprs[1];
return true;
Expand All @@ -44,13 +46,20 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
@SuppressWarnings("deprecation")
protected void execute(Event event) {
String msg = message.getSingle(event);
if (msg == null)
if (msg == null) {
error("The provided message was not set.", message.toString());
return;
}
BaseComponent[] components = BungeeConverter.convert(ChatMessages.parseToArray(msg));
for (Player player : recipients.getArray(event))
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, components);
}

@Override
public Node getNode() {
return node;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "send action bar " + message.toString(event, debug) + " to " + recipients.toString(event, debug);
Expand Down
40 changes: 25 additions & 15 deletions src/main/java/ch/njol/skript/effects/EffApplyBoneMeal.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,37 @@
package ch.njol.skript.effects;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.RequiredPlugins;
import ch.njol.skript.doc.Since;
import ch.njol.skript.config.Node;
import ch.njol.skript.doc.*;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;
import org.bukkit.Bukkit;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.log.runtime.SyntaxRuntimeErrorProducer;

@Name("Apply Bone Meal")
@Description("Applies bone meal to a crop, sapling, or composter")
@Examples("apply 3 bone meal to event-block")
@RequiredPlugins("MC 1.16.2+")
@RequiredPlugins("Minecraft 1.16.2+")
@Since("2.8.0")
public class EffApplyBoneMeal extends Effect {
public class EffApplyBoneMeal extends Effect implements SyntaxRuntimeErrorProducer {

static {
if (Skript.isRunningMinecraft(1, 16, 2))
Skript.registerEffect(EffApplyBoneMeal.class, "apply [%-number%] bone[ ]meal[s] [to %blocks%]");
Skript.registerEffect(EffApplyBoneMeal.class, "apply [%-number%] bone[ ]meal[s] [to %blocks%]");
}

@Nullable
private Expression<Number> amount;
private Node node;
private @Nullable Expression<Number> amount;
private Expression<Block> blocks;

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
node = getParser().getNode();
amount = (Expression<Number>) exprs[0];
blocks = (Expression<Block>) exprs[1];
return true;
Expand All @@ -42,18 +40,30 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
@Override
protected void execute(Event event) {
int times = 1;
if (amount != null)
times = amount.getOptionalSingle(event).orElse(0).intValue();
if (amount != null) {
Number amount = this.amount.getSingle(event);
if (amount == null) {
warning("The provided amount of bone meal was not set, so defaulted to 1.", this.amount.toString());
} else {
times = amount.intValue();
}
}

for (Block block : blocks.getArray(event)) {
for (int i = 0; i < times; i++) {
block.applyBoneMeal(BlockFace.UP);
}
}
}

@Override
public Node getNode() {
return node;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "apply " + (amount != null ? amount.toString(event, debug) + " " : "" + "bone meal to " + blocks.toString(event, debug));
return "apply " + (amount != null ? amount.toString(event, debug) + " " : "") + "bone meal to " + blocks.toString(event, debug);
}

}
68 changes: 34 additions & 34 deletions src/main/java/ch/njol/skript/effects/EffBan.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,28 @@
import java.util.Date;

@Name("Ban")
@Description({"Bans or unbans a player or an IP address.",
@Description({
"Bans or unbans a player or an IP address.",
"If a reason is given, it will be shown to the player when they try to join the server while banned.",
"A length of ban may also be given to apply a temporary ban. If it is absent for any reason, a permanent ban will be used instead.",
"We recommend that you test your scripts so that no accidental permanent bans are applied.",
"",
"Note that banning people does not kick them from the server.",
"You can optionally use 'and kick' or consider using the <a href='effects.html#EffKick'>kick effect</a> after applying a ban."})
@Examples({"unban player",
"You can optionally use 'and kick' or consider using the <a href='effects.html#EffKick'>kick effect</a> after applying a ban."
})
@Examples({
"unban player",
"ban \"127.0.0.1\"",
"IP-ban the player because \"he is an idiot\"",
"ban player due to \"inappropriate language\" for 2 days",
"ban and kick player due to \"inappropriate language\" for 2 days"})
@Since("1.4, 2.1.1 (ban reason), 2.5 (timespan), 2.9.0 (kick)")
"ban and kick player due to \"inappropriate language\" for 2 days"
})
@Since({
"1.4",
"2.1.1 (ban reason)",
"2.5 (timespan)",
"2.9.0 (kick)"
})
public class EffBan extends Effect {

static {
Expand All @@ -47,20 +56,14 @@ public class EffBan extends Effect {
"(IP(-| )unban|un[-]IP[-]ban) %players%");
}

@SuppressWarnings("null")
private Expression<?> players;
@Nullable
private Expression<String> reason;
@Nullable
private Expression<Timespan> expires;
private @Nullable Expression<String> reason;
private @Nullable Expression<Timespan> expires;
private boolean ban, ipBan, kick;

private boolean ban;
private boolean ipBan;
private boolean kick;

@SuppressWarnings({"null", "unchecked"})
@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
players = exprs[0];
reason = exprs.length > 1 ? (Expression<String>) exprs[1] : null;
expires = exprs.length > 1 ? (Expression<Timespan>) exprs[2] : null;
Expand All @@ -70,21 +73,19 @@ public boolean init(final Expression<?>[] exprs, final int matchedPattern, final
return true;
}

@SuppressWarnings("null")
@Override
protected void execute(final Event e) {
final String reason = this.reason != null ? this.reason.getSingle(e) : null; // don't check for null, just ignore an invalid reason
Timespan ts = this.expires != null ? this.expires.getSingle(e) : null;
final Date expires = ts != null ? new Date(System.currentTimeMillis() + ts.getAs(Timespan.TimePeriod.MILLISECOND)) : null;
final String source = "Skript ban effect";
for (final Object o : players.getArray(e)) {
if (o instanceof Player) {
Player player = (Player) o;
protected void execute(Event event) {
String reason = this.reason != null ? this.reason.getSingle(event) : null; // don't check for null, just ignore an invalid reason
Timespan timespan = this.expires != null ? this.expires.getSingle(event) : null;
Date expires = timespan != null ? new Date(System.currentTimeMillis() + timespan.getAs(Timespan.TimePeriod.MILLISECOND)) : null;
String source = "Skript ban effect";
for (Object object : players.getArray(event)) {
if (object instanceof Player player) {
if (ipBan) {
InetSocketAddress addr = player.getAddress();
if (addr == null)
return; // Can't ban unknown IP
final String ip = addr.getAddress().getHostAddress();
String ip = addr.getAddress().getHostAddress();
if (ban)
Bukkit.getBanList(BanList.Type.IP).addBan(ip, reason, expires, source);
else
Expand All @@ -97,22 +98,21 @@ protected void execute(final Event e) {
}
if (kick)
player.kickPlayer(reason);
} else if (o instanceof OfflinePlayer) {
String name = ((OfflinePlayer) o).getName();
} else if (object instanceof OfflinePlayer offlinePlayer) {
String name = offlinePlayer.getName();
if (name == null)
return; // Can't ban, name unknown
if (ban)
Bukkit.getBanList(BanList.Type.NAME).addBan(name, reason, expires, source);
else
Bukkit.getBanList(BanList.Type.NAME).pardon(name);
} else if (o instanceof String) {
final String s = (String) o;
} else if (object instanceof String string) {
if (ban) {
Bukkit.getBanList(BanList.Type.IP).addBan(s, reason, expires, source);
Bukkit.getBanList(BanList.Type.NAME).addBan(s, reason, expires, source);
Bukkit.getBanList(BanList.Type.IP).addBan(string, reason, expires, source);
Bukkit.getBanList(BanList.Type.NAME).addBan(string, reason, expires, source);
} else {
Bukkit.getBanList(BanList.Type.IP).pardon(s);
Bukkit.getBanList(BanList.Type.NAME).pardon(s);
Bukkit.getBanList(BanList.Type.IP).pardon(string);
Bukkit.getBanList(BanList.Type.NAME).pardon(string);
}
} else {
assert false;
Expand Down
24 changes: 20 additions & 4 deletions src/main/java/ch/njol/skript/effects/EffBlockUpdate.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package ch.njol.skript.effects;

import ch.njol.skript.Skript;
import ch.njol.skript.config.Node;
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.ParseResult;
import ch.njol.skript.lang.SyntaxStringBuilder;
import ch.njol.util.Kleenean;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
import org.bukkit.event.Event;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.log.runtime.SyntaxRuntimeErrorProducer;

@Name("Update Block")
@Description({
Expand All @@ -28,20 +31,22 @@
})
@Since("2.10")
// Originally sourced from SkBee by ShaneBee (https://github.com/ShaneBeee/SkBee/blob/master/src/main/java/com/shanebeestudios/skbee/elements/other/effects/EffBlockstateUpdate.java)
public class EffBlockUpdate extends Effect {
public class EffBlockUpdate extends Effect implements SyntaxRuntimeErrorProducer {

static {
Skript.registerEffect(EffBlockUpdate.class,
"update %blocks% (as|to be) %blockdata% [physics:without [neighbo[u]r[ing]|adjacent] [physic[s]] update[s]]");
}

private Node node;
private boolean physics;
private Expression<Block> blocks;
private Expression<BlockData> blockData;

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
this.node = getParser().getNode();
this.physics = !parseResult.hasTag("physics");
this.blocks = (Expression<Block>) exprs[0];
this.blockData = (Expression<BlockData>) exprs[1];
Expand All @@ -51,17 +56,28 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
@Override
protected void execute(Event event) {
BlockData data = this.blockData.getSingle(event);
if (data == null)
if (data == null) {
error("The provided blockdata was not set.", this.blockData.toString());
return;
}

for (Block block : this.blocks.getArray(event)) {
block.setBlockData(data, this.physics);
}
}

@Override
public Node getNode() {
return node;
}

@Override
public @NotNull String toString(@Nullable Event event, boolean debug) {
return "update " + this.blocks.toString(event, debug) + " as "
+ this.blockData.toString(event, debug) + (this.physics ? "without neighbour updates" : "");
SyntaxStringBuilder builder = new SyntaxStringBuilder(event, debug);
builder.append("update", blocks, "as", blockData);
if (physics)
builder.append("without adjacent updates");
return builder.toString();
}

}
Loading
Loading