Skip to content

Commit

Permalink
Fix & Enhance Whitelist (#5422)
Browse files Browse the repository at this point in the history
* Rework + support whitelist enforcement

* Simple reformat

* Requested changes + new effect

* Requested changes

* Requested changes

* Fix pattern

* Requested change

* Fix docs

* Fix docs

* Requested change

* Fix patterns for server whitelist

* Fix examples & requested changes

* Improve pattern

* Add tests

* Improve formatting of test and ExprWhitelist description

---------

Co-authored-by: Ayham Al Ali <[email protected]>
Co-authored-by: DelayedGaming <[email protected]>
Co-authored-by: sovdee <[email protected]>
Co-authored-by: Moderocky <[email protected]>
  • Loading branch information
5 people authored May 8, 2024
1 parent cecf0b4 commit 96ea3d5
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 82 deletions.
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
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({
"An expression for obtaining and modifying the server's whitelist.",
"Players may be added and removed from the whitelist.",
"The whitelist can be enabled or disabled by setting the whitelist to true or false respectively."
})
@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)
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"

0 comments on commit 96ea3d5

Please sign in to comment.