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

EffBan - add option to kick #6835

Merged
merged 3 commits into from
Jul 1, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 additions & 13 deletions src/main/java/ch/njol/skript/effects/EffBan.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.Description;
Expand All @@ -38,6 +37,7 @@
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.util.Timespan;
import ch.njol.util.Kleenean;
import org.jetbrains.annotations.Nullable;

@Name("Ban")
@Description({"Bans or unbans a player or an IP address.",
Expand All @@ -46,19 +46,23 @@
"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.",
"Consider using the <a href='effects.html#EffKick'>kick effect</a> after applying a ban."})
"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"})
@Since("1.4, 2.1.1 (ban reason), 2.5 (timespan)")
"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), INSERT VERSION (kick)")
public class EffBan extends Effect {

static {
Skript.registerEffect(EffBan.class,
"ban %strings/offlineplayers% [(by reason of|because [of]|on account of|due to) %-string%] [for %-timespan%]", "unban %strings/offlineplayers%",
"ban %players% by IP [(by reason of|because [of]|on account of|due to) %-string%] [for %-timespan%]", "unban %players% by IP",
"IP(-| )ban %players% [(by reason of|because [of]|on account of|due to) %-string%] [for %-timespan%]", "(IP(-| )unban|un[-]IP[-]ban) %players%");
"ban [kick:and kick] %strings/offlineplayers% [(by reason of|because [of]|on account of|due to) %-string%] [for %-timespan%]",
"unban %strings/offlineplayers%",
"ban [kick:and kick] %players% by IP [(by reason of|because [of]|on account of|due to) %-string%] [for %-timespan%]",
"unban %players% by IP",
"IP(-| )ban [kick:and kick] %players% [(by reason of|because [of]|on account of|due to) %-string%] [for %-timespan%]",
"(IP(-| )unban|un[-]IP[-]ban) %players%");
}

@SuppressWarnings("null")
Expand All @@ -70,6 +74,7 @@ public class EffBan extends Effect {

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

@SuppressWarnings({"null", "unchecked"})
@Override
Expand All @@ -79,6 +84,7 @@ public boolean init(final Expression<?>[] exprs, final int matchedPattern, final
expires = exprs.length > 1 ? (Expression<Timespan>) exprs[2] : null;
ban = matchedPattern % 2 == 0;
ipBan = matchedPattern >= 2;
kick = parseResult.hasTag("kick");
return true;
}

Expand All @@ -91,8 +97,9 @@ protected void execute(final Event e) {
final String source = "Skript ban effect";
for (final Object o : players.getArray(e)) {
if (o instanceof Player) {
Player player = (Player) o;
if (ipBan) {
InetSocketAddress addr = ((Player) o).getAddress();
InetSocketAddress addr = player.getAddress();
if (addr == null)
return; // Can't ban unknown IP
final String ip = addr.getAddress().getHostAddress();
Expand All @@ -102,10 +109,12 @@ protected void execute(final Event e) {
Bukkit.getBanList(BanList.Type.IP).pardon(ip);
} else {
if (ban)
Bukkit.getBanList(BanList.Type.NAME).addBan(((Player) o).getName(), reason, expires, source); // FIXME [UUID] ban UUID
Bukkit.getBanList(BanList.Type.NAME).addBan(player.getName(), reason, expires, source); // FIXME [UUID] ban UUID
ShaneBeee marked this conversation as resolved.
Show resolved Hide resolved
else
Bukkit.getBanList(BanList.Type.NAME).pardon(((Player) o).getName());
Bukkit.getBanList(BanList.Type.NAME).pardon(player.getName());
}
if (kick)
player.kickPlayer(reason);
} else if (o instanceof OfflinePlayer) {
String name = ((OfflinePlayer) o).getName();
if (name == null)
Expand All @@ -130,9 +139,14 @@ protected void execute(final Event e) {
}

@Override
public String toString(final @Nullable Event e, final boolean debug) {
return (ipBan ? "IP-" : "") + (ban ? "" : "un") + "ban " + players.toString(e, debug) +
(reason != null ? " on account of " + reason.toString(e, debug) : "") + (expires != null ? " for " + expires.toString(e, debug) : "");
public String toString(final @Nullable Event event, final boolean debug) {
String ip = ipBan ? "IP-" : "";
String ban = this.ban ? "ban " : "unban ";
String andKick = kick ? "and kick " : "";
String players = this.players.toString(event, debug);
String reason = this.reason != null ? " on account of " + this.reason.toString(event, debug) : "";
String time = expires != null ? " for " + expires.toString(event, debug) : "";
return ip + ban + andKick + players + reason + time;
ShaneBeee marked this conversation as resolved.
Show resolved Hide resolved
}

}