Skip to content

Commit 27f796b

Browse files
committed
Add /velocitywhitelist command
resolve #1
1 parent 00e50f9 commit 27f796b

File tree

5 files changed

+103
-17
lines changed

5 files changed

+103
-17
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ uuids:
6060

6161
Require permission `velocitywhitelist.command`
6262

63+
### Whitelist / Blacklist Control
64+
6365
- `/whitelist` and `/vwhitelist` (alias) commands are for whitelist control
6466
- `/blacklist` and `/vblacklist` (alias) commands are for whitelist control
6567

@@ -78,6 +80,11 @@ For player operation commands, `<value>` has different meaning depends on the id
7880
If it's a player name, and the player is connected to the proxy, the player's UUID will be used,
7981
otherwise it will try to fetch and use the player's online UUID from mojang API
8082

83+
### Plugin Control
84+
85+
- `/velocitywhitelist`: Show plugin information
86+
- `/velocitywhitelist reload`: Reload config, whitelist and blacklist
87+
8188
## TODO
8289

8390
- [x] UUID support

src/main/java/me/fallenbreath/velocitywhitelist/VelocityWhitelistPlugin.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.velocitypowered.api.plugin.Plugin;
88
import com.velocitypowered.api.plugin.annotation.DataDirectory;
99
import com.velocitypowered.api.proxy.ProxyServer;
10+
import me.fallenbreath.velocitywhitelist.command.PluginControlCommand;
1011
import me.fallenbreath.velocitywhitelist.command.WhitelistCommand;
1112
import me.fallenbreath.velocitywhitelist.config.Configuration;
1213
import org.slf4j.Logger;
@@ -57,6 +58,7 @@ public void onProxyInitialization(ProxyInitializeEvent event)
5758

5859
this.server.getEventManager().register(this, LoginEvent.class, this.whitelistManager::onPlayerLogin);
5960
new WhitelistCommand(this.config, this.whitelistManager).register(this.server.getCommandManager());
61+
new PluginControlCommand(this.logger, this.config, this.whitelistManager).register(this.server.getCommandManager());
6062
}
6163

6264
private boolean prepareConfig()
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package me.fallenbreath.velocitywhitelist.command;
2+
3+
import com.velocitypowered.api.command.BrigadierCommand;
4+
import com.velocitypowered.api.command.CommandManager;
5+
import com.velocitypowered.api.command.CommandSource;
6+
import me.fallenbreath.velocitywhitelist.PluginMeta;
7+
import me.fallenbreath.velocitywhitelist.WhitelistManager;
8+
import me.fallenbreath.velocitywhitelist.config.Configuration;
9+
import net.kyori.adventure.text.Component;
10+
import net.kyori.adventure.text.event.ClickEvent;
11+
import net.kyori.adventure.text.format.NamedTextColor;
12+
import net.kyori.adventure.text.format.TextDecoration;
13+
import org.slf4j.Logger;
14+
15+
import static me.fallenbreath.velocitywhitelist.command.CommandUtils.literal;
16+
17+
public class PluginControlCommand
18+
{
19+
private final Logger logger;
20+
private final Configuration config;
21+
private final WhitelistManager manager;
22+
23+
public PluginControlCommand(Logger logger, Configuration config, WhitelistManager whitelistManager)
24+
{
25+
this.logger = logger;
26+
this.config = config;
27+
this.manager = whitelistManager;
28+
}
29+
30+
@SuppressWarnings("deprecation") // Next time for sure...
31+
public void register(CommandManager commandManager)
32+
{
33+
var root = literal("velocitywhitelist").
34+
requires(s -> s.hasPermission(PluginMeta.ID + ".command")).
35+
executes(c -> showPluginInfo(c.getSource())).
36+
then(literal("reload").
37+
executes(c -> reloadAll(c.getSource()))
38+
);
39+
commandManager.register(new BrigadierCommand(root.build()));
40+
}
41+
42+
private int reloadAll(CommandSource source)
43+
{
44+
try
45+
{
46+
this.config.reload();
47+
this.manager.loadLists();
48+
source.sendMessage(Component.text("Reloaded config, whitelist and blacklist"));
49+
return 1;
50+
}
51+
catch (Exception e)
52+
{
53+
this.logger.error("Failed to reload", e);
54+
source.sendMessage(Component.text("Reload failed, see console for details"));
55+
}
56+
return 0;
57+
}
58+
59+
private int showPluginInfo(CommandSource source)
60+
{
61+
source.sendMessage(Component.text(String.format("%s v%s", PluginMeta.NAME, PluginMeta.VERSION)));
62+
source.sendMessage(Component.text(PluginMeta.REPOSITORY_URL, NamedTextColor.BLUE, TextDecoration.UNDERLINED).clickEvent(ClickEvent.openUrl(PluginMeta.REPOSITORY_URL)));
63+
return 0;
64+
}
65+
}

src/main/java/me/fallenbreath/velocitywhitelist/command/WhitelistCommand.java

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@
2121
public class WhitelistCommand
2222
{
2323
private final Configuration config;
24-
private final WhitelistManager whitelistManager;
24+
private final WhitelistManager manager;
2525

2626
public WhitelistCommand(Configuration config, WhitelistManager whitelistManager)
2727
{
2828
this.config = config;
29-
this.whitelistManager = whitelistManager;
29+
this.manager = whitelistManager;
3030
}
3131

3232
@SuppressWarnings("deprecation") // Next time for sure...
33-
private void registerOne(CommandManager commandManager, String[] roots, Supplier<@Nullable PlayerList> listSupplier, Runnable listReloader)
33+
private void registerOne(CommandManager commandManager, String[] roots, Supplier<Boolean> enableStateGetter, Supplier<@Nullable PlayerList> listSupplier, Runnable listReloader)
3434
{
3535
if (roots.length == 0)
3636
{
@@ -39,15 +39,15 @@ private void registerOne(CommandManager commandManager, String[] roots, Supplier
3939

4040
var root = literal(roots[0]).
4141
requires(s -> s.hasPermission(PluginMeta.ID + ".command")).
42-
executes(c -> showPluginInfo(c.getSource())).
42+
executes(c -> showPluginInfo(c.getSource(), listSupplier.get(), enableStateGetter.get())).
4343
then(literal("add").
4444
then(argument("name", word()).
4545
executes(c -> addWhitelist(c.getSource(), listSupplier.get(), getString(c, "name")))
4646
)
4747
).
4848
then(literal("remove").
4949
then(argument("name", word()).
50-
suggests((c, sb) -> suggestMatching(this.whitelistManager.getValuesForRemovalSuggestion(listSupplier.get()), sb)).
50+
suggests((c, sb) -> suggestMatching(this.manager.getValuesForRemovalSuggestion(listSupplier.get()), sb)).
5151
executes(c -> removeWhitelist(c.getSource(), listSupplier.get(), getString(c, "name")))
5252
)
5353
).
@@ -71,33 +71,38 @@ private void registerOne(CommandManager commandManager, String[] roots, Supplier
7171

7272
public void register(CommandManager commandManager)
7373
{
74-
this.registerOne(commandManager, new String[]{"whitelist", "vwhitelist"}, this.whitelistManager::getWhitelist, this.whitelistManager::loadWhitelist);
75-
this.registerOne(commandManager, new String[]{"blacklist", "vblacklist"}, this.whitelistManager::getBlacklist, this.whitelistManager::loadBlacklist);
74+
this.registerOne(commandManager, new String[]{"whitelist", "vwhitelist"}, this.config::isWhitelistEnabled, this.manager::getWhitelist, this.manager::loadWhitelist);
75+
this.registerOne(commandManager, new String[]{"blacklist", "vblacklist"}, this.config::isBlacklistEnabled, this.manager::getBlacklist, this.manager::loadBlacklist);
7676
}
7777

78-
private int showPluginInfo(CommandSource source)
78+
private int showPluginInfo(CommandSource source, @Nullable PlayerList list, boolean enabled)
7979
{
80+
if (list == null)
81+
{
82+
source.sendMessage(Component.text("Uninitialized"));
83+
return 0;
84+
}
85+
8086
source.sendMessage(Component.text(String.format("%s v%s", PluginMeta.NAME, PluginMeta.VERSION)));
81-
source.sendMessage(Component.text(String.format("Whitelist enabled: %s", this.config.isWhitelistEnabled())));
82-
source.sendMessage(Component.text(String.format("Blacklist enabled: %s", this.config.isBlacklistEnabled())));
83-
return 0;
87+
source.sendMessage(Component.text(String.format("%s enabled: %s", list.getName(), enabled)));
88+
return enabled ? 2 : 1;
8489
}
8590

86-
private int addWhitelist(CommandSource source, @Nullable PlayerList list, String name)
91+
private int addWhitelist(CommandSource source, @Nullable PlayerList list, String name)
8792
{
88-
if (this.whitelistManager.addPlayer(source, list,name))
93+
if (this.manager.addPlayer(source, list,name))
8994
{
90-
this.whitelistManager.saveList(list);
95+
this.manager.saveList(list);
9196
return 1;
9297
}
9398
return 0;
9499
}
95100

96101
private int removeWhitelist(CommandSource source, @Nullable PlayerList list, String name)
97102
{
98-
if (this.whitelistManager.removePlayer(source, list, name))
103+
if (this.manager.removePlayer(source, list, name))
99104
{
100-
this.whitelistManager.saveList(list);
105+
this.manager.saveList(list);
101106
return 1;
102107
}
103108
return 0;
@@ -111,7 +116,7 @@ private int listWhitelist(CommandSource source, @Nullable PlayerList list)
111116
return 0;
112117
}
113118

114-
var players = this.whitelistManager.getValuesForListing(list);
119+
var players = this.manager.getValuesForListing(list);
115120
source.sendMessage(Component.text(String.format("%s size: %d", list.getName(), players.size())));
116121
source.sendMessage(Component.text(String.format("%s players: %s", list.getName(), Joiner.on(", ").join(players))));
117122
return players.size();

src/main/java/me/fallenbreath/velocitywhitelist/config/Configuration.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.yaml.snakeyaml.Yaml;
99

1010
import java.io.IOException;
11+
import java.nio.file.Files;
1112
import java.nio.file.Path;
1213
import java.util.Map;
1314
import java.util.Optional;
@@ -38,6 +39,12 @@ public void load(String yamlContent)
3839
this.identifyMode = this.makeIdentifyMode();
3940
}
4041

42+
public void reload() throws IOException
43+
{
44+
String content = Files.readString(this.configFilePath);
45+
this.load(content);
46+
}
47+
4148
private void migrate()
4249
{
4350
boolean migrated = false;

0 commit comments

Comments
 (0)