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 3 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
73 changes: 35 additions & 38 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,69 @@
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, offline players)")
@RequiredPlugins("Minecraft 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");
"[the] server (is|not:(isn't|is not)) white[ ]listed",
"%offlineplayers% (is|are)(|not:(isn't|is not)) white[ ]listed",
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
(ENFORCE_SUPPORT ? "[the] white[ ]list (is|not:(isn't|is not)) enforced" : "")
);
}

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

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;
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) {
return (players != null ? players.toString(event, debug) : "server") + " 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
}

}
93 changes: 93 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,93 @@
/**
* 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.")
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
@Examples({
"enforce the whitelist",
"unenforce the whitelist"
})
@Since("INSERT VERSION")
@RequiredPlugins("Minecraft 1.17+")
public class EffEnforceWhitelist extends Effect {

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

static {
try {
YamlConfiguration spigotYml = YamlConfiguration.loadConfiguration(new File("spigot.yml"));
NOT_WHITELISTED_MESSAGE = (String) spigotYml.get("messages.whitelist", NOT_WHITELISTED_MESSAGE);
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
} catch (Exception ignored) {}
if (Skript.methodExists(Bukkit.class, "setWhitelistEnforced", boolean.class))
Skript.registerEffect(EffEnforceWhitelist.class, "[:un]enforce [the] 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 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";
}

}
73 changes: 37 additions & 36 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,83 @@
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({
"A server's whitelist.",
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
"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)"
})
@Examples({
"set whitelist to false",
"add all players to whitelist",
"reset the whitelist"})
"reset the whitelist"
})
@Since("2.5.2")
public class ExprWhitelist extends SimpleExpression<OfflinePlayer> {


private EffEnforceWhitelist effEnforceWhitelist = new EffEnforceWhitelist();

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)
if (mode == ChangeMode.ADD || mode == ChangeMode.REMOVE) {
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
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
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]);
Bukkit.setWhitelist((Boolean) delta[0]);
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);
break;
case RESET:
for (OfflinePlayer p : Bukkit.getWhitelistedPlayers())
p.setWhitelisted(false);
for (OfflinePlayer player : Bukkit.getWhitelistedPlayers()) {
player.setWhitelisted(false);
}
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
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";
}

}