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 2 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
74 changes: 37 additions & 37 deletions src/main/java/ch/njol/skript/conditions/CondIsWhitelisted.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,72 +19,72 @@
package ch.njol.skript.conditions;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.OfflinePlayer;
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;

@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, and server is whitelist enforced.")
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
@Examples({
"if player is whitelisted:",
"if server is whitelisted:",
"if server is whitelist enforced:"
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
})
@Since("2.5.2, INSERT VERSION (enforce)")
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
@RequiredPlugins("Minecraft 1.17+")
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
public class CondIsWhitelisted extends Condition {


private static final boolean ENFORCE_SUPPORT;

static {
ENFORCE_SUPPORT = Skript.methodExists(Bukkit.class, "isWhitelistEnforced");
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
Skript.registerCondition(CondIsWhitelisted.class,
"[the] server (is|1¦is(n't| not)) white[ ]listed",
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
"%players% (is|are)(|1¦(n't| not)) white[ ]listed");
"%offlineplayers% (is|are)(|1¦(n't| not)) white[ ]listed",
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
(ENFORCE_SUPPORT ? "[the] server (is|1¦is(n't| not)) white[ ]list enforced" : ""));
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
}

@Nullable
private Expression<Player> player;
private Expression<OfflinePlayer> player;
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved

private boolean isServer;

private boolean isEnforce;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
setNegated(parseResult.mark == 1);
isServer = matchedPattern == 0;
if (matchedPattern == 1)
player = (Expression<Player>) exprs[0];
if (matchedPattern == 0 || matchedPattern == 2) {
isServer = true;
isEnforce = matchedPattern == 2;
}
else if (matchedPattern == 1)
player = (Expression<OfflinePlayer>) exprs[0];
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
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();

NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
return player.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) {
return (isServer ? "server" : "player") + " is " + (isNegated() ? "not" : "") + " "
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
+ (isEnforce ? "whitelist enforced" : "whitelisted");
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
}

}
105 changes: 68 additions & 37 deletions src/main/java/ch/njol/skript/expressions/ExprWhitelist.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

Expand All @@ -29,6 +30,7 @@
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.Expression;
import ch.njol.skript.lang.ExpressionType;
Expand All @@ -38,83 +40,112 @@
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)")
@Description({
"A server's whitelist or whitelist enforcement.",
"This expression can be used to add/remove players to/from the whitelist.",
"To enable and disable it (set whitelist to true / set whitelist to false), and to empty it (reset whitelist)",
"To enable and disable enforcement (set whitelist enforcement to true / set whitelist enforcement to false),",
"which kicks all non-whitelisted players once set to true."
})
@Examples({"set whitelist to false",
"add all players to whitelist",
"reset the whitelist"})
@Since("2.5.2")
@Since("2.5.2, INSERT VERSION (enforce)")
@RequiredPlugins("Minecraft 1.17+")
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
public class ExprWhitelist extends SimpleExpression<OfflinePlayer> {


private static final boolean ENFORCE_SUPPORT;

static {
Skript.registerExpression(ExprWhitelist.class, OfflinePlayer.class, ExpressionType.SIMPLE, "[the] white[ ]list");
ENFORCE_SUPPORT = Skript.methodExists(Bukkit.class, "setWhitelistEnforced", boolean.class);
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
Skript.registerExpression(ExprWhitelist.class, OfflinePlayer.class, ExpressionType.SIMPLE, "[the] white[ ]list"
+ (ENFORCE_SUPPORT ? " [:enforcement]" : ""));
}


private boolean isEnforce;

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

@Nullable

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

@Nullable

@Override
public Class<?>[] acceptChange(ChangeMode mode) {
if (mode == ChangeMode.ADD || mode == ChangeMode.REMOVE)
if (mode == ChangeMode.ADD || mode == ChangeMode.REMOVE) {
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
if (isEnforce) {
if (ENFORCE_SUPPORT)
Skript.error("\"Whitelist enforcement\" can't have anything " + (mode == ChangeMode.ADD ? "added" : "removed") + "." +
" Use 'whitelist' instead.");
return null;
}
return CollectionUtils.array(OfflinePlayer[].class);
else if (mode == ChangeMode.SET || mode == ChangeMode.RESET)
} else if (mode == ChangeMode.SET || mode == ChangeMode.RESET) {
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
if (mode == ChangeMode.RESET && isEnforce)
return null;
return CollectionUtils.array(Boolean.class);
else
return null;
}
return null;
}

@Override
public void change(Event e, @Nullable Object[] delta, ChangeMode mode) {
public void change(Event event, @Nullable Object[] 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 value = (boolean) delta[0];
if (isEnforce)
Bukkit.setWhitelistEnforced(value);
else
Bukkit.setWhitelist(value);
if (ENFORCE_SUPPORT)
reloadWhitelist();
break;
case ADD:
if (delta != null) {
for (Object p : delta)
((OfflinePlayer) p).setWhitelisted(true);
}
for (Object p : delta)
((OfflinePlayer) p).setWhitelisted(true);
break;
case REMOVE:
if (delta != null) {
for (Object p : delta)
((OfflinePlayer) p).setWhitelisted(false);
}
for (Object p : delta)
((OfflinePlayer) p).setWhitelisted(false);
if (ENFORCE_SUPPORT)
reloadWhitelist();
break;
case RESET:
for (OfflinePlayer p : Bukkit.getWhitelistedPlayers())
for (OfflinePlayer p : Bukkit.getWhitelistedPlayers()) {
p.setWhitelisted(false);
}
break;
default:
assert false;
}
}


private void reloadWhitelist() {
Bukkit.reloadWhitelist();
for (Player player : Bukkit.getOnlinePlayers()) {
if (!player.isWhitelisted() && !player.isOp())
player.kickPlayer("You are not whitelisted on this server!");
}
}

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

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

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

}