-
-
Notifications
You must be signed in to change notification settings - Fork 368
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
cecf0b4
commit 96ea3d5
Showing
4 changed files
with
207 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
src/main/java/ch/njol/skript/effects/EffEnforceWhitelist.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/test/skript/tests/syntaxes/expressions/ExprWhitelist.sk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |