Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public WhitelistManager(Logger logger, Configuration config, Path dataDirectory,
{
this.logger = logger;
this.config = config;
this.whitelist = new PlayerList("Whitelist", dataDirectory.resolve("whitelist.yml"), this.config::isWhitelistEnabled);
this.blacklist = new PlayerList("Blacklist", dataDirectory.resolve("blacklist.yml"), this.config::isBlacklistEnabled);
this.whitelist = new PlayerList("Whitelist", dataDirectory.resolve("whitelist.yml"), config::getMigrateWhitelistEnabled);
this.blacklist = new PlayerList("Blacklist", dataDirectory.resolve("blacklist.yml"), config::getMigrateBlacklistEnabled);
this.server = server;
}

Expand All @@ -56,6 +56,12 @@ public void loadLists()
this.loadOneList(this.blacklist);
}

public void enableList(PlayerList list, boolean enabled)
{
list.setEnabled(enabled);
saveList(list);
}

private boolean isPlayerInList(GameProfile profile, PlayerList list)
{
return switch (this.config.getIdentifyMode())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ private void registerOne(CommandManager commandManager, String[] roots, PlayerLi
var root = literal(roots[0]).
requires(s -> s.hasPermission(PluginMeta.ID + ".command")).
executes(c -> showListStatus(c.getSource(), list)).
then(literal("enable")
.executes(c -> enable(c.getSource(), list))
).
then(
literal("disable")
.executes(c -> disable(c.getSource(), list))
).
then(literal("add").
then(argument("name", word()).
executes(c -> addPlayer(c.getSource(), list, getString(c, "name")))
Expand Down Expand Up @@ -77,10 +84,30 @@ private int showListStatus(CommandSource source, PlayerList list)

protected static void showListStatus(CommandSource source, PlayerList list, String prefix)
{
source.sendMessage(Component.text(String.format("%sActivated: %s (config enabled: %s, load ok: %s)", prefix, list.isActivated(), list.isConfigEnabled(), list.isLoadOk())));
source.sendMessage(Component.text(String.format("%sActivated: %s (enabled: %s, load ok: %s)", prefix, list.isActivated(), list.isEnabled(), list.isLoadOk())));
source.sendMessage(Component.text(String.format("%sSize: %d player names, %d player UUIDs", prefix, list.getPlayerNames().size(), list.getPlayerUuidMappingEntries().size())));
}

private int enable(CommandSource source, PlayerList list) {
if (list.isEnabled()) {
source.sendMessage(Component.text(String.format("%s is already enabled.", list.getName())));
return 0;
}
manager.enableList(list, true);
source.sendMessage(Component.text(String.format("%s was enabled.", list.getName())));
return 1;
}

private int disable(CommandSource source, PlayerList list) {
if (!list.isEnabled()) {
source.sendMessage(Component.text(String.format("%s is already disabled", list.getName())));
return 0;
}
manager.enableList(list, false);
source.sendMessage(Component.text(String.format("%s was disabled.", list.getName())));
return 1;
}

private int addPlayer(CommandSource source, PlayerList list, String playerName)
{
if (!list.isActivated())
Expand Down Expand Up @@ -129,12 +156,6 @@ private int listPlayers(CommandSource source, PlayerList list)

private int reloadList(CommandSource source, PlayerList list)
{
if (!list.isConfigEnabled())
{
source.sendMessage(Component.text(String.format("%s is disabled by config", list.getName())));
return 0;
}

if (this.manager.loadOneList(list))
{
source.sendMessage(Component.text(String.format("%s reloaded", list.getName())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import me.fallenbreath.velocitywhitelist.IdentifyMode;
import me.fallenbreath.velocitywhitelist.PluginMeta;
import me.fallenbreath.velocitywhitelist.utils.FileUtils;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.yaml.snakeyaml.Yaml;

Expand All @@ -21,6 +22,9 @@ public class Configuration

private IdentifyMode identifyMode = IdentifyMode.DEFAULT;

private Boolean migrateWhitelistEnabled;
private Boolean migrateBlacklistEnabled;

public Configuration(Logger logger, Path configFilePath)
{
this.logger = logger;
Expand Down Expand Up @@ -55,15 +59,34 @@ private void migrate()
Map<String, Object> newOptions = Maps.newLinkedHashMap();
newOptions.put("_version", 1);
newOptions.put("identify_mode", Optional.ofNullable(this.options.get("identify_mode")).orElse("name"));
newOptions.put("whitelist_enabled", Optional.ofNullable(this.options.get("enabled")).orElse(true));
newOptions.put("whitelist_kick_message", Optional.ofNullable(this.options.get("kick_message")).orElse("You are not in the whitelist!"));
newOptions.put("blacklist_enabled", Optional.ofNullable(this.options.get("enabled")).orElse(true)); // it's ok to enable an empty blacklist
newOptions.put("blacklist_kick_message", "You are banned from the server!");

this.options.clear();
this.options.putAll(newOptions);
migrated = true;
}
if (this.options.get("_version") instanceof Integer version) {
if (version == 1) {
// migrate config version 1 to 2

Object whitelistEnabled = this.options.get("whitelist_enabled");
if (whitelistEnabled instanceof Boolean boolWhitelistEnabled) {
this.migrateWhitelistEnabled = boolWhitelistEnabled;
}
this.options.remove("whitelist_enabled");
Object blacklistEnabled = this.options.get("blacklist_enabled");
if (blacklistEnabled instanceof Boolean boolBlacklistEnabled) {
this.migrateBlacklistEnabled = boolBlacklistEnabled;
}
this.options.remove("blacklist_enabled");

this.options.remove("_version");
this.options.put("_version", 2);

migrated = true;
}
}

if (migrated)
{
Expand Down Expand Up @@ -100,31 +123,21 @@ private IdentifyMode makeIdentifyMode()
return IdentifyMode.DEFAULT;
}

public boolean isWhitelistEnabled()
{
Object enabled = this.options.get("whitelist_enabled");
if (enabled instanceof Boolean)
{
return (Boolean)enabled;
}
return false;
}

public boolean isBlacklistEnabled()
{
Object enabled = this.options.get("blacklist_enabled");
if (enabled instanceof Boolean)
{
return (Boolean)enabled;
}
return false;
}

public IdentifyMode getIdentifyMode()
{
return this.identifyMode;
}

public @Nullable Boolean getMigrateWhitelistEnabled()
{
return this.migrateWhitelistEnabled;
}

public @Nullable Boolean getMigrateBlacklistEnabled()
{
return this.migrateBlacklistEnabled;
}

public String getWhitelistKickMessage()
{
Object maxPlayer = this.options.get("whitelist_kick_message");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;

import java.io.IOException;
Expand All @@ -22,19 +23,20 @@

public class PlayerList
{
private final Set<String> names = Sets.newLinkedHashSet();
private final Set<String> names = Sets.newLinkedHashSet();
private final Map<UUID, @Nullable String> uuids = Maps.newLinkedHashMap();
private final String name;
private final Path filePath;
private final Supplier<Boolean> configEnableGetter;
private final Supplier<Boolean> migrateConfigEnable;
private boolean loadOk = false;
private final Object lock = new Object();
private boolean enabled;

public PlayerList(String name, Path filePath, Supplier<Boolean> configEnableGetter)
public PlayerList(String name, Path filePath, Supplier<Boolean> migrateConfigEnable)
{
this.name = name;
this.filePath = filePath;
this.configEnableGetter = configEnableGetter;
this.migrateConfigEnable = migrateConfigEnable;
}

public String getName()
Expand All @@ -55,19 +57,26 @@ public boolean isLoadOk()
}
}

public boolean isConfigEnabled()
{
public boolean isEnabled() {
synchronized (this.lock)
{
return this.configEnableGetter.get();
return this.enabled;
}
}

public void setEnabled(boolean enabled)
{
synchronized (this.lock)
{
this.enabled = enabled;
}
}

public boolean isActivated()
{
synchronized (this.lock)
{
return this.isLoadOk() && this.isConfigEnabled();
return this.isLoadOk() && this.isEnabled();
}
}

Expand Down Expand Up @@ -175,13 +184,14 @@ public void resetTo(@NotNull PlayerList newList)
this.names.addAll(newList.names);
this.uuids.clear();
this.uuids.putAll(newList.uuids);
this.enabled = newList.enabled;
this.loadOk = true;
}
}

public PlayerList createNewEmptyList()
{
return new PlayerList(this.name, this.filePath, this.configEnableGetter);
return new PlayerList(this.name, this.filePath, migrateConfigEnable);
}

@SuppressWarnings({"unchecked", "rawtypes"})
Expand All @@ -194,6 +204,19 @@ public void load(Logger logger) throws IOException

synchronized (this.lock)
{
Boolean migrateValue = migrateConfigEnable.get();
if (migrateValue != null) {
logger.info("List {} migrated enable value from configuration.", getName());
setEnabled(migrateValue);
save();
} else {
if (options.get("enabled") instanceof Boolean bool) {
setEnabled(bool);
} else {
setEnabled(false);
}
}

this.names.clear();
if (options.get("names") instanceof List list)
{
Expand Down Expand Up @@ -239,7 +262,7 @@ else if (item instanceof Map<?, ?> map)

this.loadOk = true;
logger.info("{} loaded with {} names and {} uuids", this.name, this.names.size(), this.uuids.size());
}
}
}

public void save() throws IOException
Expand All @@ -248,6 +271,7 @@ public void save() throws IOException

synchronized (this.lock)
{
options.put("enabled", this.enabled);
options.put("names", Lists.newArrayList(this.names));
List<Object> uuidList = this.uuids.entrySet().stream()
.map(e -> e.getValue() != null ? Map.of(e.getKey().toString(), e.getValue()) : e.getKey().toString())
Expand Down
6 changes: 1 addition & 5 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
# Config file version. Do not edit it
_version: 1
_version: 2

# The way to identify a player
# Options: name, uuid. Default: name
# For online servers, it's suggested to use the uuid mode
# since it can keep tracking on the mojang account of the player
identify_mode: name

# If the whitelist functionality is enabled
whitelist_enabled: true
# Message sent to those not whitelisted players
whitelist_kick_message: You are not in the whitelist!

# If the blacklist functionality is enabled
blacklist_enabled: true
# Message sent to those blacklisted players
blacklist_kick_message: You are banned from the server!