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

Fix & Enhance Whitelist #5422

Merged
merged 22 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
90 changes: 47 additions & 43 deletions src/main/java/ch/njol/skript/conditions/CondIsWhitelisted.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,73 +18,77 @@
*/
package ch.njol.skript.conditions;

import org.bukkit.Bukkit;
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;
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.lang.Condition;
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.OfflinePlayer;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

@Name("Is Whitelisted")
@Description("Whether or not the server or a player is whitelisted.")
@Examples({"if server is whitelisted:", "if player is whitelisted"})
@Since("2.5.2")
@Description("Whether or not the server or a player is whitelisted, or the server is whitelist enforced.")
@Examples({
"if the player is whitelisted:",
"if the server is whitelisted:",
"if the server whitelist is enforced:"
})
@Since("2.5.2, INSERT VERSION (enforce, offline players)")
@RequiredPlugins("MC 1.17+ (enforce)")
public class CondIsWhitelisted extends Condition {


private static final boolean ENFORCE_SUPPORT = Skript.methodExists(Bukkit.class, "isWhitelistEnforced");

static {
Skript.registerCondition(CondIsWhitelisted.class,
"[the] server (is|1¦is(n't| not)) white[ ]listed",
"%players% (is|are)(|1¦(n't| not)) white[ ]listed");
String[] patterns = new String[ENFORCE_SUPPORT ? 3 : 2];
patterns[0] = "[the] server (is|not:(isn't|is not)) (in white[ ]list mode|white[ ]listed)";
patterns[1] = "%offlineplayers% (is|are|not:(isn't|is not|aren't|are not)) white[ ]listed";
if (ENFORCE_SUPPORT)
patterns[2] = "[the] server white[ ]list (is|not:(isn't|is not)) enforced";
Skript.registerCondition(CondIsWhitelisted.class, patterns);
}

@Nullable
private Expression<Player> player;
private Expression<OfflinePlayer> players;

private boolean isServer;

private boolean isEnforce;

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
setNegated(parseResult.mark == 1);
isServer = matchedPattern == 0;
setNegated(parseResult.hasTag("not"));
isServer = matchedPattern != 1;
isEnforce = matchedPattern == 2;
if (matchedPattern == 1)
player = (Expression<Player>) exprs[0];
players = (Expression<OfflinePlayer>) exprs[0];
return true;
}

@Override
@SuppressWarnings("null")
public boolean check(Event e) {
public boolean check(Event event) {
if (isServer)
return Bukkit.hasWhitelist() == isNegated();
Player[] players = player.getAll(e);
if (player.getAnd() && isNegated()) {
for (Player player : players)
if (player.isWhitelisted())
return false;
} else if(player.getAnd()){
for (Player player : players)
if (!player.isWhitelisted())
return false;
} else {
for (Player player: players)
if(player.isWhitelisted())
return !isNegated();
}
return !isNegated();
return (isEnforce ? Bukkit.isWhitelistEnforced() : Bukkit.hasWhitelist()) ^ isNegated();
return players.check(event, OfflinePlayer::isWhitelisted, isNegated());
}

@Override
@SuppressWarnings("null")
public String toString(@Nullable Event e, boolean debug) {
return (player.getSingle(e) != null ? "player" : "server") + (isNegated() ? "not" : "") + " whitelisted";
public String toString(@Nullable Event event, boolean debug) {
String negation = isNegated() ? "not" : "";
if (isServer) {
if (isEnforce) {
return "the server whitelist is " + negation + " enforced";
}
return "the server is " + negation + " whitelisted";
}
return players.toString(event, debug) + " is " + negation + " whitelisted";
}

}
97 changes: 97 additions & 0 deletions src/main/java/ch/njol/skript/effects/EffEnforceWhitelist.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/**
* 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 Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.effects;

import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

import java.io.File;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Since;
import ch.njol.skript.doc.RequiredPlugins;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.util.Utils;
import ch.njol.util.Kleenean;

@Name("Enforce Whitelist")
@Description({
"Enforces or un-enforce a server's whitelist.",
"All non-whitelisted players will be kicked upon enforcing the whitelist."
})
@Examples({
"enforce the whitelist",
"unenforce the whitelist"
})
@Since("INSERT VERSION")
@RequiredPlugins("MC 1.17+")
public class EffEnforceWhitelist extends Effect {

private static String NOT_WHITELISTED_MESSAGE = "You are not whitelisted on this server!";

static {
if (Skript.methodExists(Bukkit.class, "setWhitelistEnforced", boolean.class)) {
try {
YamlConfiguration spigotYml = YamlConfiguration.loadConfiguration(new File("spigot.yml"));
NOT_WHITELISTED_MESSAGE = spigotYml.getString("messages.whitelist", NOT_WHITELISTED_MESSAGE);
} catch (Exception ignored) {}
Skript.registerEffect(EffEnforceWhitelist.class, "[:un]enforce [the] [server] white[ ]list");
}
}

private boolean enforce;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
enforce = !parseResult.hasTag("un");
return true;
}

@Override
protected void execute(Event event) {
Bukkit.setWhitelistEnforced(enforce);
reloadWhitelist();
}

// A workaround for Bukkit's not kicking non-whitelisted players upon enforcement
APickledWalrus marked this conversation as resolved.
Show resolved Hide resolved
public static void reloadWhitelist() {
Bukkit.reloadWhitelist();
if (!Bukkit.hasWhitelist() || !Bukkit.isWhitelistEnforced())
return;
for (Player player : Bukkit.getOnlinePlayers()) {
if (!player.isWhitelisted() && !player.isOp())
player.kickPlayer(Utils.replaceChatStyles(NOT_WHITELISTED_MESSAGE));
}
}

@Override
public String toString(@Nullable Event e, boolean debug) {
return (!enforce ? "un" : "") + "enforce the whitelist";
}

}
84 changes: 45 additions & 39 deletions src/main/java/ch/njol/skript/expressions/ExprWhitelist.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package ch.njol.skript.expressions;

import ch.njol.skript.effects.EffEnforceWhitelist;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.event.Event;
Expand All @@ -38,83 +39,88 @@
import ch.njol.util.coll.CollectionUtils;

@Name("Whitelist")
@Description("A server's whitelist." +
"This expression can be used to add/remove players to/from the whitelist," +
" to enable it and disable it (set whitelist to true / set whitelist to false)," +
" and to empty it (reset whitelist)")
@Examples({"set whitelist to false",
@Description({
"The server's whitelist.",
"This expression can be used to add/remove players to/from the whitelist.",
"To enable and disable it (set whitelist to true/false), and to empty it (reset whitelist)"
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
})
@Examples({
"set the whitelist to false",
"add all players to whitelist",
"reset the whitelist"})
@Since("2.5.2")
"reset the whitelist"
})
@Since("2.5.2, INSERT VERSION (delete)")
public class ExprWhitelist extends SimpleExpression<OfflinePlayer> {

static {
Skript.registerExpression(ExprWhitelist.class, OfflinePlayer.class, ExpressionType.SIMPLE, "[the] white[ ]list");
}

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
return true;
}

@Nullable

@Override
protected OfflinePlayer[] get(Event e) {
protected OfflinePlayer[] get(Event event) {
return Bukkit.getServer().getWhitelistedPlayers().toArray(new OfflinePlayer[0]);
}

@Nullable

@Override
public Class<?>[] acceptChange(ChangeMode mode) {
if (mode == ChangeMode.ADD || mode == ChangeMode.REMOVE)
return CollectionUtils.array(OfflinePlayer[].class);
else if (mode == ChangeMode.SET || mode == ChangeMode.RESET)
return CollectionUtils.array(Boolean.class);
else
return null;
switch (mode) {
case ADD:
case REMOVE:
return CollectionUtils.array(OfflinePlayer.class);
case DELETE:
case RESET:
case SET:
return CollectionUtils.array(Boolean.class);
}
return null;
}

@Override
public void change(Event e, @Nullable Object[] delta, ChangeMode mode) {
public void change(Event event, Object @Nullable [] delta, ChangeMode mode) {
switch (mode) {
case SET:
if (delta != null)
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
Bukkit.setWhitelist((Boolean) delta[0]);
boolean toggle = (Boolean) delta[0];
Bukkit.setWhitelist(toggle);
if (toggle)
EffEnforceWhitelist.reloadWhitelist();
break;
case ADD:
if (delta != null) {
for (Object p : delta)
((OfflinePlayer) p).setWhitelisted(true);
}
for (Object player : delta)
((OfflinePlayer) player).setWhitelisted(true);
break;
case REMOVE:
if (delta != null) {
for (Object p : delta)
((OfflinePlayer) p).setWhitelisted(false);
}
for (Object player : delta)
((OfflinePlayer) player).setWhitelisted(false);
EffEnforceWhitelist.reloadWhitelist();
break;
case DELETE:
case RESET:
for (OfflinePlayer p : Bukkit.getWhitelistedPlayers())
p.setWhitelisted(false);
for (OfflinePlayer player : Bukkit.getWhitelistedPlayers())
player.setWhitelisted(false);
break;
default:
assert false;
}
}

@Override
public boolean isSingle() {
return false;
}

@Override
public Class<? extends OfflinePlayer> getReturnType() {
return OfflinePlayer.class;
}

@Override
public String toString(@Nullable Event e, boolean debug) {
public String toString(@Nullable Event event, boolean debug) {
return "whitelist";
}

}
18 changes: 18 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprWhitelist.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
test "whitelist":
reset whitelist
set {_player} to "Njol" parsed as offline player
add {_player} to whitelist
assert {_player} is whitelisted with "Failed to whitelist a player"

remove {_player} from whitelist
assert {_player} is not whitelisted with "Failed to remove a player from whitelist"

add {_player} to whitelist
reset whitelist
assert whitelist is not set with "Failed to empty whitelist"

test "enforce whitelist" when running minecraft "1.17":
enforce whitelist
assert server whitelist is enforced with "Failed to enforce server whitelist"
unenforce whitelist
assert server whitelist is not enforced with "Failed to unenforce whitelist"
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved