diff --git a/src/main/java/lol/hyper/bungeeblockversion/BungeeBlockVersion.java b/src/main/java/lol/hyper/bungeeblockversion/BungeeBlockVersion.java index c9aaf96..7557f7d 100644 --- a/src/main/java/lol/hyper/bungeeblockversion/BungeeBlockVersion.java +++ b/src/main/java/lol/hyper/bungeeblockversion/BungeeBlockVersion.java @@ -1,90 +1,90 @@ -/* - * This file is part of BungeeBlockVersion. - * - * BungeeBlockVersion 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. - * - * BungeeBlockVersion 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 BungeeBlockVersion. If not, see . - */ - -package lol.hyper.bungeeblockversion; - -import lol.hyper.bungeeblockversion.commands.CommandReload; -import lol.hyper.bungeeblockversion.events.JoinEvent; -import lol.hyper.bungeeblockversion.tools.ConfigHandler; -import lol.hyper.githubreleaseapi.GitHubRelease; -import lol.hyper.githubreleaseapi.GitHubReleaseAPI; -import net.kyori.adventure.platform.bungeecord.BungeeAudiences; -import net.kyori.adventure.text.minimessage.MiniMessage; -import net.md_5.bungee.api.ProxyServer; -import net.md_5.bungee.api.plugin.Listener; -import net.md_5.bungee.api.plugin.Plugin; -import org.bstats.bungeecord.Metrics; - -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; -import java.util.logging.Logger; - -public final class BungeeBlockVersion extends Plugin implements Listener { - - public final Logger logger = this.getLogger(); - - public ConfigHandler configHandler; - public JoinEvent joinEvent; - public final MiniMessage miniMessage = MiniMessage.miniMessage(); - private BungeeAudiences adventure; - - public Map versions = new HashMap<>(); - - @Override - public void onEnable() { - this.adventure = BungeeAudiences.create(this); - joinEvent = new JoinEvent(this); - configHandler = new ConfigHandler(this); - configHandler.loadConfig(); - getProxy().getPluginManager().registerCommand(this, new CommandReload("bbvreload", this)); - ProxyServer.getInstance().getPluginManager().registerListener(this, joinEvent); - - new Metrics(this, 9392); - ProxyServer.getInstance().getScheduler().runAsync(this, this::checkForUpdates); - } - - public void checkForUpdates() { - GitHubReleaseAPI api; - try { - api = new GitHubReleaseAPI("BungeeBlockVersion", "hyperdefined"); - } catch (IOException e) { - logger.warning("Unable to check updates!"); - e.printStackTrace(); - return; - } - GitHubRelease current = api.getReleaseByTag(this.getDescription().getVersion()); - GitHubRelease latest = api.getLatestVersion(); - if (current == null) { - logger.warning("You are running a version that does not exist on GitHub. If you are in a dev environment, you can ignore this. Otherwise, this is a bug!"); - return; - } - int buildsBehind = api.getBuildsBehind(current); - if (buildsBehind == 0) { - logger.info("You are running the latest version."); - } else { - logger.warning("A new version is available (" + latest.getTagVersion() + ")! You are running version " + current.getTagVersion() + ". You are " + buildsBehind + " version(s) behind."); - } - } - - public BungeeAudiences getAdventure() { - if (this.adventure == null) { - throw new IllegalStateException("Tried to access Adventure when the plugin was disabled!"); - } - return this.adventure; - } -} +/* + * This file is part of BungeeBlockVersion. + * + * BungeeBlockVersion 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. + * + * BungeeBlockVersion 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 BungeeBlockVersion. If not, see . + */ + +package lol.hyper.bungeeblockversion; + +import lol.hyper.bungeeblockversion.commands.CommandReload; +import lol.hyper.bungeeblockversion.events.JoinEvent; +import lol.hyper.bungeeblockversion.tools.ConfigHandler; +import lol.hyper.githubreleaseapi.GitHubRelease; +import lol.hyper.githubreleaseapi.GitHubReleaseAPI; +import net.kyori.adventure.platform.bungeecord.BungeeAudiences; +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.md_5.bungee.api.ProxyServer; +import net.md_5.bungee.api.plugin.Listener; +import net.md_5.bungee.api.plugin.Plugin; +import org.bstats.bungeecord.Metrics; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.logging.Logger; + +public final class BungeeBlockVersion extends Plugin implements Listener { + + public final Logger logger = this.getLogger(); + + public ConfigHandler configHandler; + public JoinEvent joinEvent; + public final MiniMessage miniMessage = MiniMessage.miniMessage(); + private BungeeAudiences adventure; + + public Map versions = new HashMap<>(); + + @Override + public void onEnable() { + this.adventure = BungeeAudiences.create(this); + joinEvent = new JoinEvent(this); + configHandler = new ConfigHandler(this); + configHandler.loadConfig(); + getProxy().getPluginManager().registerCommand(this, new CommandReload("bbvreload", this)); + ProxyServer.getInstance().getPluginManager().registerListener(this, joinEvent); + + new Metrics(this, 9392); + ProxyServer.getInstance().getScheduler().runAsync(this, this::checkForUpdates); + } + + public void checkForUpdates() { + GitHubReleaseAPI api; + try { + api = new GitHubReleaseAPI("BungeeBlockVersion", "hyperdefined"); + } catch (IOException e) { + logger.warning("Unable to check updates!"); + e.printStackTrace(); + return; + } + GitHubRelease current = api.getReleaseByTag(this.getDescription().getVersion()); + GitHubRelease latest = api.getLatestVersion(); + if (current == null) { + logger.warning("You are running a version that does not exist on GitHub. If you are in a dev environment, you can ignore this. Otherwise, this is a bug!"); + return; + } + int buildsBehind = api.getBuildsBehind(current); + if (buildsBehind == 0) { + logger.info("You are running the latest version."); + } else { + logger.warning("A new version is available (" + latest.getTagVersion() + ")! You are running version " + current.getTagVersion() + ". You are " + buildsBehind + " version(s) behind."); + } + } + + public BungeeAudiences getAdventure() { + if (this.adventure == null) { + throw new IllegalStateException("Tried to access Adventure when the plugin was disabled!"); + } + return this.adventure; + } +} diff --git a/src/main/java/lol/hyper/bungeeblockversion/commands/CommandReload.java b/src/main/java/lol/hyper/bungeeblockversion/commands/CommandReload.java index 537650d..45d7b4c 100644 --- a/src/main/java/lol/hyper/bungeeblockversion/commands/CommandReload.java +++ b/src/main/java/lol/hyper/bungeeblockversion/commands/CommandReload.java @@ -1,44 +1,44 @@ -/* - * This file is part of BungeeBlockVersion. - * - * BungeeBlockVersion 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. - * - * BungeeBlockVersion 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 BungeeBlockVersion. If not, see . - */ - -package lol.hyper.bungeeblockversion.commands; - -import lol.hyper.bungeeblockversion.BungeeBlockVersion; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.format.NamedTextColor; -import net.md_5.bungee.api.CommandSender; -import net.md_5.bungee.api.plugin.Command; - -public class CommandReload extends Command { - - private final BungeeBlockVersion bungeeBlockVersion; - - public CommandReload(String name, BungeeBlockVersion bungeeBlockVersion) { - super(name); - this.bungeeBlockVersion = bungeeBlockVersion; - } - - @Override - public void execute(CommandSender sender, String[] args) { - if (sender.hasPermission("bbv.reload")) { - bungeeBlockVersion.getAdventure().sender(sender).sendMessage(Component.text("Config reloaded!").color(NamedTextColor.GREEN)); - bungeeBlockVersion.configHandler.loadConfig(); - } else { - bungeeBlockVersion.getAdventure().sender(sender).sendMessage(Component.text("You do not have permission for this command.").color(NamedTextColor.RED)); - } - } -} +/* + * This file is part of BungeeBlockVersion. + * + * BungeeBlockVersion 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. + * + * BungeeBlockVersion 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 BungeeBlockVersion. If not, see . + */ + +package lol.hyper.bungeeblockversion.commands; + +import lol.hyper.bungeeblockversion.BungeeBlockVersion; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; +import net.md_5.bungee.api.CommandSender; +import net.md_5.bungee.api.plugin.Command; + +public class CommandReload extends Command { + + private final BungeeBlockVersion bungeeBlockVersion; + + public CommandReload(String name, BungeeBlockVersion bungeeBlockVersion) { + super(name); + this.bungeeBlockVersion = bungeeBlockVersion; + } + + @Override + public void execute(CommandSender sender, String[] args) { + if (sender.hasPermission("bbv.reload")) { + bungeeBlockVersion.getAdventure().sender(sender).sendMessage(Component.text("Config reloaded!").color(NamedTextColor.GREEN)); + bungeeBlockVersion.configHandler.loadConfig(); + } else { + bungeeBlockVersion.getAdventure().sender(sender).sendMessage(Component.text("You do not have permission for this command.").color(NamedTextColor.RED)); + } + } +} diff --git a/src/main/java/lol/hyper/bungeeblockversion/events/JoinEvent.java b/src/main/java/lol/hyper/bungeeblockversion/events/JoinEvent.java index 28464ae..2788132 100644 --- a/src/main/java/lol/hyper/bungeeblockversion/events/JoinEvent.java +++ b/src/main/java/lol/hyper/bungeeblockversion/events/JoinEvent.java @@ -1,89 +1,89 @@ -/* - * This file is part of BungeeBlockVersion. - * - * BungeeBlockVersion 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. - * - * BungeeBlockVersion 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 BungeeBlockVersion. If not, see . - */ - -package lol.hyper.bungeeblockversion.events; - -import lol.hyper.bungeeblockversion.BungeeBlockVersion; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer; -import net.md_5.bungee.api.chat.BaseComponent; -import net.md_5.bungee.api.chat.TextComponent; -import net.md_5.bungee.api.event.LoginEvent; -import net.md_5.bungee.api.plugin.Listener; -import net.md_5.bungee.event.EventHandler; -import net.md_5.bungee.protocol.ProtocolConstants; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class JoinEvent implements Listener { - - private final BungeeBlockVersion bungeeBlockVersion; - - public JoinEvent(BungeeBlockVersion bungeeBlockVersion) { - this.bungeeBlockVersion = bungeeBlockVersion; - } - - @EventHandler - public void onPreConnect(LoginEvent event) { - if (event.isCancelled()) { - return; - } - int version = event.getConnection().getVersion(); - String playerName = event.getConnection().getName(); - if (bungeeBlockVersion.configHandler.configuration.getBoolean("log-connection-versions")) { - bungeeBlockVersion.logger.info("Player is connecting with protocol version: " + version); - } - - if (!bungeeBlockVersion.configHandler.blockedVersions.contains(version)) { - return; - } - - event.setCancelled(true); - String blockedMessage = bungeeBlockVersion.configHandler.configuration.getString("disconnect-message"); - String allowedVersions = allowedVersions(bungeeBlockVersion.configHandler.blockedVersions); - if (allowedVersions == null) { - blockedMessage = "All versions are currently blocked from playing."; - } - if (blockedMessage.contains("{VERSIONS}")) { - blockedMessage = blockedMessage.replace("{VERSIONS}", allowedVersions); - } - Component blockedMessageComponent = bungeeBlockVersion.miniMessage.deserialize(blockedMessage); - BaseComponent blockedMessageBaseComponent = new TextComponent(BungeeComponentSerializer.get().serialize(blockedMessageComponent)); - event.setReason(blockedMessageBaseComponent); - bungeeBlockVersion.logger.info("Blocking player " + playerName + " because they are playing on version " + bungeeBlockVersion.configHandler.versionMap.get(version) + " which is blocked!"); - } - - /** - * Builds a string that will show what versions the server supports. Example: 1.8 to 1.14.4 - * - * @param deniedVersions Versions to deny. - * @return Returns the string of versions. - */ - public String allowedVersions(List deniedVersions) { - List allVersions = new ArrayList<>(ProtocolConstants.SUPPORTED_VERSION_IDS); - allVersions.removeAll(deniedVersions); - if (allVersions.isEmpty()) { - return null; - } - int minVersion = Collections.min(allVersions); - int maxVersion = Collections.max(allVersions); - - return bungeeBlockVersion.configHandler.versionMap.get(minVersion) + " to " + bungeeBlockVersion.configHandler.versionMap.get(maxVersion); - } +/* + * This file is part of BungeeBlockVersion. + * + * BungeeBlockVersion 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. + * + * BungeeBlockVersion 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 BungeeBlockVersion. If not, see . + */ + +package lol.hyper.bungeeblockversion.events; + +import lol.hyper.bungeeblockversion.BungeeBlockVersion; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer; +import net.md_5.bungee.api.chat.BaseComponent; +import net.md_5.bungee.api.chat.TextComponent; +import net.md_5.bungee.api.event.LoginEvent; +import net.md_5.bungee.api.plugin.Listener; +import net.md_5.bungee.event.EventHandler; +import net.md_5.bungee.protocol.ProtocolConstants; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class JoinEvent implements Listener { + + private final BungeeBlockVersion bungeeBlockVersion; + + public JoinEvent(BungeeBlockVersion bungeeBlockVersion) { + this.bungeeBlockVersion = bungeeBlockVersion; + } + + @EventHandler + public void onPreConnect(LoginEvent event) { + if (event.isCancelled()) { + return; + } + int version = event.getConnection().getVersion(); + String playerName = event.getConnection().getName(); + if (bungeeBlockVersion.configHandler.configuration.getBoolean("log-connection-versions")) { + bungeeBlockVersion.logger.info("Player is connecting with protocol version: " + version); + } + + if (!bungeeBlockVersion.configHandler.blockedVersions.contains(version)) { + return; + } + + event.setCancelled(true); + String blockedMessage = bungeeBlockVersion.configHandler.configuration.getString("disconnect-message"); + String allowedVersions = allowedVersions(bungeeBlockVersion.configHandler.blockedVersions); + if (allowedVersions == null) { + blockedMessage = "All versions are currently blocked from playing."; + } + if (blockedMessage.contains("{VERSIONS}")) { + blockedMessage = blockedMessage.replace("{VERSIONS}", allowedVersions); + } + Component blockedMessageComponent = bungeeBlockVersion.miniMessage.deserialize(blockedMessage); + BaseComponent blockedMessageBaseComponent = new TextComponent(BungeeComponentSerializer.get().serialize(blockedMessageComponent)); + event.setReason(blockedMessageBaseComponent); + bungeeBlockVersion.logger.info("Blocking player " + playerName + " because they are playing on version " + bungeeBlockVersion.configHandler.versionMap.get(version) + " which is blocked!"); + } + + /** + * Builds a string that will show what versions the server supports. Example: 1.8 to 1.14.4 + * + * @param deniedVersions Versions to deny. + * @return Returns the string of versions. + */ + public String allowedVersions(List deniedVersions) { + List allVersions = new ArrayList<>(ProtocolConstants.SUPPORTED_VERSION_IDS); + allVersions.removeAll(deniedVersions); + if (allVersions.isEmpty()) { + return null; + } + int minVersion = Collections.min(allVersions); + int maxVersion = Collections.max(allVersions); + + return bungeeBlockVersion.configHandler.versionMap.get(minVersion) + " to " + bungeeBlockVersion.configHandler.versionMap.get(maxVersion); + } } \ No newline at end of file diff --git a/src/main/java/lol/hyper/bungeeblockversion/tools/ConfigHandler.java b/src/main/java/lol/hyper/bungeeblockversion/tools/ConfigHandler.java index 7ab4e71..109a340 100644 --- a/src/main/java/lol/hyper/bungeeblockversion/tools/ConfigHandler.java +++ b/src/main/java/lol/hyper/bungeeblockversion/tools/ConfigHandler.java @@ -1,118 +1,122 @@ -/* - * This file is part of BungeeBlockVersion. - * - * BungeeBlockVersion 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. - * - * BungeeBlockVersion 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 BungeeBlockVersion. If not, see . - */ - -package lol.hyper.bungeeblockversion.tools; - -import lol.hyper.bungeeblockversion.BungeeBlockVersion; -import net.md_5.bungee.config.Configuration; -import net.md_5.bungee.config.ConfigurationProvider; -import net.md_5.bungee.config.YamlConfiguration; -import net.md_5.bungee.protocol.ProtocolConstants; -import org.json.JSONObject; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - -public class ConfigHandler { - - public Configuration configuration; - public List blockedVersions; - public Map versionMap = new HashMap<>(); - private final BungeeBlockVersion bungeeBlockVersion; - - public ConfigHandler(BungeeBlockVersion bungeeBlockVersion) { - this.bungeeBlockVersion = bungeeBlockVersion; - } - - public void loadConfig() { - File configFile = new File("plugins" + File.separator + "BungeeBlockVersion", "config.yml"); - if (!configFile.exists()) { - InputStream is = bungeeBlockVersion.getResourceAsStream("config.yml"); - try { - File path = new File("plugins" + File.separator + "BungeeBlockVersion"); - if (path.mkdir()) { - Files.copy(is, configFile.toPath()); - bungeeBlockVersion.logger.info("Copying default config..."); - } else { - bungeeBlockVersion.logger.warning("Unable to create config folder!"); - } - } catch (IOException exception) { - throw new RuntimeException(exception); - } - } - try { - configuration = ConfigurationProvider.getProvider(YamlConfiguration.class).load(configFile); - blockedVersions = configuration.getIntList("versions"); - int CONFIG_VERSION = 5; - if (configuration.getInt("config-version") != CONFIG_VERSION) { - bungeeBlockVersion.logger.warning("Your config is outdated. We will attempt to load your current config. However, things might not work!"); - bungeeBlockVersion.logger.warning("To fix this, delete your current config and let the server remake it."); - } - if (blockedVersions.isEmpty()) { - bungeeBlockVersion.logger.warning("There are no versions listed in the config! There will be no attempts to block connections."); - return; - } else { - bungeeBlockVersion.logger.info("Loaded " + blockedVersions.size() + " versions!"); - } - bungeeBlockVersion.logger.info("Loaded versions: " + blockedVersions.stream().map(String::valueOf).collect(Collectors.joining(", "))); - // use an iterator here so we can remove stuff - Iterator iter = blockedVersions.iterator(); - while (iter.hasNext()) { - int version = iter.next(); - // make sure the versions the user entered exist - if (!ProtocolConstants.SUPPORTED_VERSION_IDS.contains(version)) { - bungeeBlockVersion.logger.warning("Version " + version + " is NOT a valid version number! Ignoring this version."); - iter.remove(); - } - } - } catch (IOException exception) { - exception.printStackTrace(); - bungeeBlockVersion.logger.severe("Unable to load configuration file!"); - } - - fetchVersions(); - } - - private void fetchVersions() { - bungeeBlockVersion.logger.info("Loading versions from GitHub..."); - JSONUtils jsonUtils = new JSONUtils(bungeeBlockVersion); - JSONObject versions = jsonUtils.requestJSON("https://raw.githubusercontent.com/hyperdefined/BungeeBlockVersion/master/versions.json"); - if (versions == null) { - bungeeBlockVersion.logger.severe("Unable to fetch versions from GitHub!"); - bungeeBlockVersion.logger.severe("The plugin is unable to function normally."); - return; - } - bungeeBlockVersion.logger.info("Loaded " + versions.length() + " version(s) from GitHub!"); - // key is the protocol version - // value is the name of the version - versions.keys().forEachRemaining(key -> { - int protocolVersion = Integer.parseInt(key); - String namedVersion = versions.getString(key); - // make sure the version exists before saving it - if (ProtocolConstants.SUPPORTED_VERSION_IDS.contains(protocolVersion)) { - versionMap.put(protocolVersion, namedVersion); - } - }); - } -} +/* + * This file is part of BungeeBlockVersion. + * + * BungeeBlockVersion 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. + * + * BungeeBlockVersion 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 BungeeBlockVersion. If not, see . + */ + +package lol.hyper.bungeeblockversion.tools; + +import lol.hyper.bungeeblockversion.BungeeBlockVersion; +import net.md_5.bungee.config.Configuration; +import net.md_5.bungee.config.ConfigurationProvider; +import net.md_5.bungee.config.YamlConfiguration; +import net.md_5.bungee.protocol.ProtocolConstants; +import org.json.JSONArray; +import org.json.JSONObject; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class ConfigHandler { + + public Configuration configuration; + public List blockedVersions; + public Map versionMap = new HashMap<>(); + private final BungeeBlockVersion bungeeBlockVersion; + + public ConfigHandler(BungeeBlockVersion bungeeBlockVersion) { + this.bungeeBlockVersion = bungeeBlockVersion; + } + + public void loadConfig() { + File configFile = new File("plugins" + File.separator + "BungeeBlockVersion", "config.yml"); + if (!configFile.exists()) { + InputStream is = bungeeBlockVersion.getResourceAsStream("config.yml"); + try { + File path = new File("plugins" + File.separator + "BungeeBlockVersion"); + if (path.mkdir()) { + Files.copy(is, configFile.toPath()); + bungeeBlockVersion.logger.info("Copying default config..."); + } else { + bungeeBlockVersion.logger.warning("Unable to create config folder!"); + } + } catch (IOException exception) { + throw new RuntimeException(exception); + } + } + try { + configuration = ConfigurationProvider.getProvider(YamlConfiguration.class).load(configFile); + blockedVersions = configuration.getIntList("versions"); + int CONFIG_VERSION = 5; + if (configuration.getInt("config-version") != CONFIG_VERSION) { + bungeeBlockVersion.logger.warning("Your config is outdated. We will attempt to load your current config. However, things might not work!"); + bungeeBlockVersion.logger.warning("To fix this, delete your current config and let the server remake it."); + } + if (blockedVersions.isEmpty()) { + bungeeBlockVersion.logger.warning("There are no versions listed in the config! There will be no attempts to block connections."); + return; + } else { + bungeeBlockVersion.logger.info("Loaded " + blockedVersions.size() + " versions!"); + } + bungeeBlockVersion.logger.info("Loaded versions: " + blockedVersions.stream().map(String::valueOf).collect(Collectors.joining(", "))); + // use an iterator here so we can remove stuff + Iterator iter = blockedVersions.iterator(); + while (iter.hasNext()) { + int version = iter.next(); + // make sure the versions the user entered exist + if (!ProtocolConstants.SUPPORTED_VERSION_IDS.contains(version)) { + bungeeBlockVersion.logger.warning("Version " + version + " is NOT a valid version number! Ignoring this version."); + iter.remove(); + } + } + } catch (IOException exception) { + exception.printStackTrace(); + bungeeBlockVersion.logger.severe("Unable to load configuration file!"); + } + + fetchVersions(); + } + + private void fetchVersions() { + bungeeBlockVersion.logger.info("Loading versions from the web..."); + JSONUtils jsonUtils = new JSONUtils(bungeeBlockVersion); + JSONArray versions = jsonUtils.requestJSONArray("https://raw.githubusercontent.com/PrismarineJS/minecraft-data/refs/heads/master/data/pc/common/protocolVersions.json"); + if (versions == null) { + bungeeBlockVersion.logger.severe("Unable to fetch versions from GitHub!"); + bungeeBlockVersion.logger.severe("The plugin is unable to function normally."); + return; + } + + // load versions from the JSON + for (int i = 0; i < versions.length(); i++) { + JSONObject jsonObject = versions.getJSONObject(i); + String minecraftVersion = jsonObject.getString("minecraftVersion"); + int version = jsonObject.getInt("version"); + if (ProtocolConstants.SUPPORTED_VERSION_IDS.contains(version)) { + // only add the last version with the same id + if (!versionMap.containsKey(version)) { + versionMap.put(version, minecraftVersion); + } + } + } + bungeeBlockVersion.logger.info("Loaded " + versionMap.size() + " version(s)!"); + } +} diff --git a/src/main/java/lol/hyper/bungeeblockversion/tools/JSONUtils.java b/src/main/java/lol/hyper/bungeeblockversion/tools/JSONUtils.java index 624a96c..94687d7 100644 --- a/src/main/java/lol/hyper/bungeeblockversion/tools/JSONUtils.java +++ b/src/main/java/lol/hyper/bungeeblockversion/tools/JSONUtils.java @@ -1,69 +1,71 @@ -/* - * This file is part of BungeeBlockVersion. - * - * BungeeBlockVersion 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. - * - * BungeeBlockVersion 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 BungeeBlockVersion. If not, see . - */ - -package lol.hyper.bungeeblockversion.tools; - -import lol.hyper.bungeeblockversion.BungeeBlockVersion; -import org.json.JSONObject; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.URL; -import java.net.URLConnection; -import java.util.stream.Collectors; - -public class JSONUtils { - - private final BungeeBlockVersion bungeeBlockVersion; - - public JSONUtils(BungeeBlockVersion bungeeBlockVersion) { - this.bungeeBlockVersion = bungeeBlockVersion; - } - - /** - * Get a JSONObject from a URL. - * - * @param url The URL to get JSON from. - * @return The response JSONObject. Returns null if there was some issue. - */ - public JSONObject requestJSON(String url) { - String rawJSON; - try { - URLConnection conn = new URL(url).openConnection(); - conn.setRequestProperty("User-Agent", "BungeeBlockVersion " + bungeeBlockVersion.getDescription().getVersion() + " (+https://github.com/hyperdefined/BungeeBlockVersion)"); - conn.connect(); - - InputStream in = conn.getInputStream(); - BufferedReader reader = new BufferedReader(new InputStreamReader(in)); - rawJSON = reader.lines().collect(Collectors.joining(System.lineSeparator())); - reader.close(); - - } catch (IOException exception) { - exception.printStackTrace(); - bungeeBlockVersion.logger.severe("Unable to read URL " + url); - return null; - } - - if (rawJSON.isEmpty()) { - bungeeBlockVersion.logger.severe("Read JSON from " + url + " returned an empty string!"); - return null; - } - return new JSONObject(rawJSON); - } -} +/* + * This file is part of BungeeBlockVersion. + * + * BungeeBlockVersion 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. + * + * BungeeBlockVersion 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 BungeeBlockVersion. If not, see . + */ + +package lol.hyper.bungeeblockversion.tools; + +import lol.hyper.bungeeblockversion.BungeeBlockVersion; +import org.json.JSONArray; +import org.json.JSONObject; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; +import java.net.URLConnection; +import java.util.stream.Collectors; + +public class JSONUtils { + + private final BungeeBlockVersion bungeeBlockVersion; + + public JSONUtils(BungeeBlockVersion bungeeBlockVersion) { + this.bungeeBlockVersion = bungeeBlockVersion; + } + + /** + * Get a JSONArray from a URL. + * + * @param url The URL to get JSON from. + * @return The response JSONArray. Returns null if there was some issue. + */ + public JSONArray requestJSONArray(String url) { + String rawJSON; + try { + URLConnection conn = new URL(url).openConnection(); + conn.setRequestProperty("User-Agent", "BungeeBlockVersion " + bungeeBlockVersion.getDescription().getVersion() + " (+https://github.com/hyperdefined/BungeeBlockVersion)"); + conn.connect(); + + InputStream in = conn.getInputStream(); + BufferedReader reader = new BufferedReader(new InputStreamReader(in)); + rawJSON = reader.lines().collect(Collectors.joining(System.lineSeparator())); + reader.close(); + + } catch (IOException exception) { + exception.printStackTrace(); + bungeeBlockVersion.logger.severe("Unable to read URL " + url); + return null; + } + + if (rawJSON.isEmpty()) { + bungeeBlockVersion.logger.severe("Read JSON from " + url + " returned an empty string!"); + return null; + } + + return new JSONArray(rawJSON); + } +} diff --git a/src/main/resources/bungee.yml b/src/main/resources/bungee.yml index f4ab601..073dc02 100644 --- a/src/main/resources/bungee.yml +++ b/src/main/resources/bungee.yml @@ -1,6 +1,6 @@ -name: BungeeBlockVersion -version: ${project.version} -main: lol.hyper.bungeeblockversion.BungeeBlockVersion -author: hyperdefined -website: https://www.spigotmc.org/resources/bungeeblockversion.84685/ +name: BungeeBlockVersion +version: ${project.version} +main: lol.hyper.bungeeblockversion.BungeeBlockVersion +author: hyperdefined +website: https://www.spigotmc.org/resources/bungeeblockversion.84685/ description: Block certain Minecraft versions from connecting to your network. \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 5e8e3ed..f7e565c 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,54 +1,54 @@ -# These versions will NOT be allowed to connect. -# These versions MUST be the version number. You can check the numbers here: https://wiki.vg/Protocol_version_numbers -# By default, all versions are listed here. -versions: - - 47 - - 107 - - 108 - - 109 - - 110 - - 210 - - 315 - - 316 - - 335 - - 338 - - 340 - - 393 - - 401 - - 404 - - 477 - - 480 - - 485 - - 490 - - 498 - - 573 - - 575 - - 578 - - 735 - - 736 - - 751 - - 753 - - 754 - - 755 - - 756 - - 757 - - 758 - - 759 - - 760 # will show up as 1.19.2 because 1.19.1 and 1.19.2 use the same version - - 761 - - 762 - - 763 # will show up as 1.20.1 because 1.20 and 1.20.1 use the same version - - 764 - - 765 # will show up as 1.20.4 because 1.20.3 and 1.20.4 use the same version - -# Send this message if someone connects with a blocked version. -# Use {VERSIONS} to show what versions your server uses. It will display like "1.8 to 1.16.3" or whatever. -# If you don't want to use {VERSIONS}, just remove it. -disconnect-message: "You cannot connect with this version! We only allow version(s) {VERSIONS}." - -# This will say "Player is connecting with protocol version" when someone joins. -# This is off by default to not spam your console, but you can enable it for debug reasons. -log-connection-versions: false - -# No touch please :) -config-version: 5 +# These versions will NOT be allowed to connect. +# These versions MUST be the version number. You can check the numbers here: https://wiki.vg/Protocol_version_numbers +# By default, all versions are listed here. +versions: + - 47 + - 107 + - 108 + - 109 + - 110 + - 210 + - 315 + - 316 + - 335 + - 338 + - 340 + - 393 + - 401 + - 404 + - 477 + - 480 + - 485 + - 490 + - 498 + - 573 + - 575 + - 578 + - 735 + - 736 + - 751 + - 753 + - 754 + - 755 + - 756 + - 757 + - 758 + - 759 + - 760 # will show up as 1.19.2 because 1.19.1 and 1.19.2 use the same version + - 761 + - 762 + - 763 # will show up as 1.20.1 because 1.20 and 1.20.1 use the same version + - 764 + - 765 # will show up as 1.20.4 because 1.20.3 and 1.20.4 use the same version + +# Send this message if someone connects with a blocked version. +# Use {VERSIONS} to show what versions your server uses. It will display like "1.8 to 1.16.3" or whatever. +# If you don't want to use {VERSIONS}, just remove it. +disconnect-message: "You cannot connect with this version! We only allow version(s) {VERSIONS}." + +# This will say "Player is connecting with protocol version" when someone joins. +# This is off by default to not spam your console, but you can enable it for debug reasons. +log-connection-versions: false + +# No touch please :) +config-version: 5 diff --git a/versions.json b/versions.json deleted file mode 100644 index 9151451..0000000 --- a/versions.json +++ /dev/null @@ -1,42 +0,0 @@ -{ - "47": "1.8", - "107": "1.9", - "108": "1.9.1", - "109": "1.9.2", - "110": "1.9.4", - "210": "1.10", - "315": "1.11", - "316": "1.11.1", - "335": "1.12", - "338": "1.12.1", - "340": "1.12.2", - "393": "1.13", - "401": "1.13.1", - "404": "1.13.2", - "477": "1.14", - "480": "1.14.1", - "485": "1.14.2", - "490": "1.14.3", - "498": "1.14.4", - "573": "1.15", - "575": "1.15.1", - "578": "1.15.2", - "735": "1.16", - "736": "1.16.1", - "751": "1.16.2", - "753": "1.16.3", - "754": "1.16.4", - "755": "1.17", - "756": "1.17.1", - "757": "1.18", - "758": "1.18.2", - "759": "1.19", - "760": "1.19.2", - "761": "1.19.3", - "762": "1.19.4", - "763": "1.20.1", - "764": "1.20.2", - "765": "1.20.4", - "766": "1.20.5", - "767": "1.21" -}