Skip to content

Commit c31b1a6

Browse files
committed
major cleanup
1 parent 9ff1221 commit c31b1a6

4 files changed

Lines changed: 125 additions & 29 deletions

File tree

src/main/java/lol/hyper/velocityblockversion/VelocityBlockVersion.java

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@
1818
package lol.hyper.velocityblockversion;
1919

2020
import com.google.inject.Inject;
21-
import com.velocitypowered.api.event.PostOrder;
22-
import com.velocitypowered.api.event.connection.PreLoginEvent;
21+
import com.velocitypowered.api.command.CommandManager;
22+
import com.velocitypowered.api.command.CommandMeta;
2323
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
2424
import com.velocitypowered.api.event.Subscribe;
2525
import com.velocitypowered.api.plugin.Plugin;
2626
import com.velocitypowered.api.proxy.ProxyServer;
2727
import lol.hyper.githubreleaseapi.GitHubRelease;
2828
import lol.hyper.githubreleaseapi.GitHubReleaseAPI;
29+
import lol.hyper.velocityblockversion.commands.CommandReload;
30+
import lol.hyper.velocityblockversion.events.JoinEvent;
2931
import lol.hyper.velocityblockversion.tools.ConfigHandler;
30-
import lol.hyper.velocityblockversion.tools.VersionToStrings;
31-
import net.kyori.adventure.text.Component;
3232
import net.kyori.adventure.text.minimessage.MiniMessage;
3333
import org.bstats.velocity.Metrics;
3434
import org.slf4j.Logger;
@@ -46,45 +46,36 @@
4646
public class VelocityBlockVersion {
4747

4848
public ConfigHandler configHandler;
49+
public JoinEvent joinEvent;
50+
public CommandReload commandReload;
4951
public final String VERSION = "1.0.5";
5052

5153
public final Logger logger;
5254
private final Metrics.Factory metricsFactory;
5355
private final ProxyServer server;
56+
private final CommandManager commandManager;
5457
public final MiniMessage miniMessage = MiniMessage.miniMessage();
5558

5659
@Inject
57-
public VelocityBlockVersion(ProxyServer server, Logger logger, Metrics.Factory metricsFactory) {
60+
public VelocityBlockVersion(ProxyServer server, Logger logger, Metrics.Factory metricsFactory, CommandManager commandManager) {
5861
this.server = server;
5962
this.logger = logger;
6063
this.metricsFactory = metricsFactory;
64+
this.commandManager = commandManager;
6165
}
6266

6367
@Subscribe
6468
public void onProxyInitialization(ProxyInitializeEvent event) {
6569
configHandler = new ConfigHandler(this);
70+
joinEvent = new JoinEvent(this);
71+
commandReload = new CommandReload(this);
6672
configHandler.loadConfig();
6773
metricsFactory.make(this, 13308);
6874
server.getScheduler().buildTask(this, this::checkForUpdates).schedule();
69-
}
75+
server.getEventManager().register(this, joinEvent);
7076

71-
@Subscribe(order = PostOrder.FIRST)
72-
public void onPlayerLogin(PreLoginEvent event) {
73-
int version = event.getConnection().getProtocolVersion().getProtocol();
74-
if (ConfigHandler.versions.contains(version)) {
75-
String allowedVersions = VersionToStrings.allowedVersions(ConfigHandler.versions);
76-
String blockedMessage = configHandler.config.getString("disconnect_message");
77-
if (allowedVersions == null) {
78-
blockedMessage = "<red>All versions are currently blocked from playing.</red>";
79-
}
80-
if (blockedMessage.contains("{VERSIONS}")) {
81-
blockedMessage = blockedMessage.replace("{VERSIONS}", allowedVersions);
82-
}
83-
Component message = miniMessage.deserialize(blockedMessage);
84-
event.setResult(PreLoginEvent.PreLoginComponentResult.denied(message));
85-
logger.info("Blocking player " + event.getUsername() + " because they are playing on version "
86-
+ VersionToStrings.versionStrings.get(version) + " which is blocked!");
87-
}
77+
CommandMeta meta = commandManager.metaBuilder("vbvreload").build();
78+
commandManager.register(meta, commandReload);
8879
}
8980

9081
public void checkForUpdates() {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* This file is part of VelocityBlockVersion.
3+
*
4+
* VelocityBlockVersion is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* VelocityBlockVersion is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with VelocityBlockVersion. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package lol.hyper.velocityblockversion.commands;
19+
20+
import com.velocitypowered.api.command.CommandSource;
21+
import com.velocitypowered.api.command.SimpleCommand;
22+
import lol.hyper.velocityblockversion.VelocityBlockVersion;
23+
import net.kyori.adventure.text.Component;
24+
import net.kyori.adventure.text.format.NamedTextColor;
25+
26+
public class CommandReload implements SimpleCommand{
27+
28+
private final VelocityBlockVersion velocityBlockVersion;
29+
30+
public CommandReload(VelocityBlockVersion velocityBlockVersion) {
31+
this.velocityBlockVersion = velocityBlockVersion;
32+
}
33+
34+
@Override
35+
public void execute(SimpleCommand.Invocation invocation) {
36+
CommandSource source = invocation.source();
37+
if (source.hasPermission("velocityBlockVersion.reload")) {
38+
velocityBlockVersion.configHandler.loadConfig();
39+
source.sendMessage(Component.text("Config reloaded!").color(NamedTextColor.GREEN));
40+
} else {
41+
source.sendMessage(Component.text("You do not have permission for this command.").color(NamedTextColor.RED));
42+
}
43+
}
44+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* This file is part of VelocityBlockVersion.
3+
*
4+
* VelocityBlockVersion is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* VelocityBlockVersion is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with VelocityBlockVersion. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package lol.hyper.velocityblockversion.events;
19+
20+
import com.velocitypowered.api.event.PostOrder;
21+
import com.velocitypowered.api.event.Subscribe;
22+
import com.velocitypowered.api.event.connection.PreLoginEvent;
23+
import lol.hyper.velocityblockversion.VelocityBlockVersion;
24+
import lol.hyper.velocityblockversion.tools.ConfigHandler;
25+
import lol.hyper.velocityblockversion.tools.VersionToStrings;
26+
import net.kyori.adventure.text.Component;
27+
28+
public class JoinEvent {
29+
30+
private final VelocityBlockVersion velocityBlockVersion;
31+
private final ConfigHandler configHandler;
32+
33+
public JoinEvent(VelocityBlockVersion velocityBlockVersion) {
34+
this.velocityBlockVersion = velocityBlockVersion;
35+
this.configHandler = velocityBlockVersion.configHandler;
36+
}
37+
38+
@Subscribe(order = PostOrder.FIRST)
39+
public void onPlayerLogin(PreLoginEvent event) {
40+
int version = event.getConnection().getProtocolVersion().getProtocol();
41+
velocityBlockVersion.logger.info("Player is connecting with protocol version: " + version);
42+
if (configHandler.blockVersions.contains(version)) {
43+
String allowedVersions = VersionToStrings.allowedVersions(configHandler.blockVersions);
44+
String blockedMessage = configHandler.config.getString("disconnect_message");
45+
if (allowedVersions == null) {
46+
blockedMessage = "<red>All versions are currently blocked from playing.</red>";
47+
}
48+
if (blockedMessage.contains("{VERSIONS}")) {
49+
blockedMessage = blockedMessage.replace("{VERSIONS}", allowedVersions);
50+
}
51+
Component message = velocityBlockVersion.miniMessage.deserialize(blockedMessage);
52+
event.setResult(PreLoginEvent.PreLoginComponentResult.denied(message));
53+
velocityBlockVersion.logger.info("Blocking player " + event.getUsername() + " because they are playing on version "
54+
+ VersionToStrings.versionStrings.get(version) + " which is blocked!");
55+
}
56+
}
57+
}

src/main/java/lol/hyper/velocityblockversion/tools/ConfigHandler.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public class ConfigHandler {
3030

3131
public Toml config;
3232
private final VelocityBlockVersion velocityBlockVersion;
33-
public static final List<Integer> versions = new ArrayList<>();
34-
public static final double CONFIG_VERSION = 3;
33+
public final List<Integer> blockVersions = new ArrayList<>();
34+
public final double CONFIG_VERSION = 3;
3535

3636
public ConfigHandler(VelocityBlockVersion velocityBlockVersion) {
3737
this.velocityBlockVersion = velocityBlockVersion;
@@ -41,6 +41,10 @@ public void loadConfig() {
4141
File configFile = new File("plugins" + File.separator + "VelocityBlockVersion", "config.toml");
4242
if (!configFile.exists()) {
4343
InputStream is = velocityBlockVersion.getClass().getResourceAsStream( "/config.toml");
44+
if (is == null) {
45+
velocityBlockVersion.logger.error("Unable to load \"config.toml\" from the plugin jar!");
46+
return;
47+
}
4448
File path = new File("plugins" + File.separator + "VelocityBlockVersion");
4549
try {
4650
if (path.mkdir()) {
@@ -72,15 +76,15 @@ public void loadConfig() {
7276
// we have to convert them this ugly way
7377
for (Object obj : config.getList("versions")) {
7478
long t = (long) obj;
75-
versions.add((int) t);
79+
blockVersions.add((int) t);
7680
}
77-
if (versions.size() == 0) {
81+
if (blockVersions.size() == 0) {
7882
velocityBlockVersion.logger.warn("There are no versions listed in the config!");
7983
} else {
80-
velocityBlockVersion.logger.info("Loaded " + versions.size() + " versions!");
84+
velocityBlockVersion.logger.info("Loaded " + blockVersions.size() + " versions!");
8185
}
8286
// use an iterator here so we can remove stuff
83-
Iterator<Integer> iter = versions.iterator();
87+
Iterator<Integer> iter = blockVersions.iterator();
8488
while (iter.hasNext()) {
8589
int version = iter.next();
8690
if (!VersionToStrings.versionStrings.containsKey(version)) {

0 commit comments

Comments
 (0)