From 25f24d29355ed400e3010b7cd48141beb1efa185 Mon Sep 17 00:00:00 2001 From: Aitor Date: Mon, 26 Feb 2024 23:26:08 +0100 Subject: [PATCH] Add new commands and improve codebase --- CHANGELOG.md | 23 +++-- README.md | 42 +++++++- pom.xml | 6 +- .../botblockerminecraft}/BotBlocker.java | 96 ++++++++++++++---- .../botblockerminecraft/CommandHandler.java | 99 +++++++++++++++++++ src/main/resources/config.yml | 4 +- src/main/resources/plugin.yml | 14 ++- 7 files changed, 244 insertions(+), 40 deletions(-) rename src/main/java/{eus/aichan/Blocker => ovh/aichan/botblockerminecraft}/BotBlocker.java (54%) create mode 100644 src/main/java/ovh/aichan/botblockerminecraft/CommandHandler.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d9e8f9..934eca4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,15 +1,20 @@ # Changelog -## v1.0.0 +## 1.3.0 ### Added or Changed -- Added this changelog :) -- Fixed typos in both templates -- Back to top links -- Added more "Built With" frameworks/libraries -- Changed table of contents to start collapsed -- Added checkboxes for major features on roadmap +- Added `ban-message` to the `config.yml` +- Lowered time limit to 5 seconds +- Added the following commands: + * `/BotBlocker status` - Show wether BotBlocker is enabled or disabled. + * `/BotBlocker getTimeLimit` - Display the configured time limit for detecting bots. + * `/BotBlocker setBanMessage [message]` - Set the ban message. + * `/BotBlocker getBanMessage` - Display the configured ban message. +- Added brief instructions for compiling this Bukkit/Spigot plugin using Maven (so I won't forget next time). +- Fixed package names to be compliant with naming conventions. +- Added usage hint to the setTimeLimit command. +- Created `CommandHandler` class to improve readability. +- Updated `README.md` and this `CHANGELOG.md` :) ### Removed - -- Some packages/libraries from acknowledgements I no longer use \ No newline at end of file +- Maven target \ No newline at end of file diff --git a/README.md b/README.md index e39041e..20f9c0e 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,7 @@
  • Usage
  • +
  • Compiling this plugin
  • Contributing
  • License
  • Contact
  • @@ -103,9 +104,13 @@ When a player joins, if BotBlocker is enabled and the player is not already exem When a player quits, BotBlocker calculates the duration of their connection. If this time is less than the set time limit and BotBlocker is enabled, the player is considered a bot. They get banned and a ban entry is added to the `players.yml` file. A disconnect message is sent to the player and a message is logged to the console. If the player stays longer than the time limit, they are deemed a legitimate player. Their UUID is added to the `players.yml` file as exempt. ### Commands -* `/BotBlocker enable` - Enables BotBlocker. -* `/BotBlocker disable` - Disables BotBlocker. -* `/BotBlocker setTimeLimit [seconds]` - Sets the time limit for detecting bots. +* `/BotBlocker enable` - Enable the BotBlocker plugin. +* `/BotBlocker disable` - Disable the BotBlocker plugin. +* `/BotBlocker status` - Show wether BotBlocker is enabled or disabled. +* `/BotBlocker setTimeLimit [seconds]` - Set the time limit for detecting bots. Default is 5 seconds. +* `/BotBlocker getTimeLimit` - Display the configured time limit for detecting bots. +* `/BotBlocker setBanMessage [message]` - Set the ban message. +* `/BotBlocker getBanMessage` - Display the configured ban message. ### Configuration Files BotBlocker maintains its configuration and the list of player UUIDs in `config.yml` and `players.yml` files, respectively. @@ -143,6 +148,37 @@ Once installed and enabled, BotBlocker works in the background without any inter

    (back to top)

    +## Compiling this plugin +To compile BotBlocker for Bukkit/Spigot yourself, follow these steps: + +1. Make sure you have Maven installed on your system. You can download it from https://maven.apache.org/download.cgi and find installation instructions at https://maven.apache.org/install.html. + +2. Clone this repository. + +3. Open your terminal or command prompt. + +4. Navigate to the root directory of the cloned repository. + + ``` + cd path/to/BotBlocker + ``` + +5. Execute the Maven build command: + + ``` + mvn clean package + ``` + + - The `clean` command will remove any previous build outputs to ensure a fresh build. + - The `package` command will compile your code and package it into a JAR file. + +6. After the build completes, you can find the compiled JAR file in the `target` directory of the project. + +7. The JAR file will be named following the convention `BotBlocker-M.m.p.jar`, where `M.m.p` is the version number following [Semantic Versioning](https://semver.org/). + +8. You can now deploy this JAR file to your Bukkit/Spigot server's `plugins` folder. + + ## Contributing diff --git a/pom.xml b/pom.xml index 06e007b..9c3565a 100644 --- a/pom.xml +++ b/pom.xml @@ -3,9 +3,9 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - eus.aichan + ovh.aichan BotBlocker - 1.2-SNAPSHOT + 1.3.0 jar @@ -61,7 +61,7 @@ true lib/ - com.tuDominio.BotBlocker + ovh.aichan.botblockerminecraft.BotBlocker diff --git a/src/main/java/eus/aichan/Blocker/BotBlocker.java b/src/main/java/ovh/aichan/botblockerminecraft/BotBlocker.java similarity index 54% rename from src/main/java/eus/aichan/Blocker/BotBlocker.java rename to src/main/java/ovh/aichan/botblockerminecraft/BotBlocker.java index e04d9ab..04160c6 100644 --- a/src/main/java/eus/aichan/Blocker/BotBlocker.java +++ b/src/main/java/ovh/aichan/botblockerminecraft/BotBlocker.java @@ -1,4 +1,4 @@ -package eus.aichan.Blocker; +package ovh.aichan.botblockerminecraft; import org.bukkit.Bukkit; import org.bukkit.BanList.Type; @@ -21,14 +21,26 @@ public class BotBlocker extends JavaPlugin implements Listener { private boolean pluginEnabled = true; private int timeLimit; // In seconds + private String banMessage; private HashMap joinTimes = new HashMap<>(); private FileConfiguration playersCfg; private File playersFile; + private CommandHandler commandHandler; @Override public void onEnable() { + initialize(); + commandHandler = new CommandHandler(this); + } + + /** + * Initialize the plugin. + */ + private void initialize() { saveDefaultConfig(); + pluginEnabled = getConfig().getBoolean("enabled", true); timeLimit = getConfig().getInt("time-limit", 5); // Default to 5 seconds + banMessage = getConfig().getString("ban-message", "Bot detected. If you are a legitimate user, please contact the admin."); Bukkit.getPluginManager().registerEvents(this, this); playersFile = new File(getDataFolder(), "players.yml"); @@ -36,6 +48,7 @@ public void onEnable() { saveResource("players.yml", false); } playersCfg = YamlConfiguration.loadConfiguration(playersFile); + saveConfig(); } @Override @@ -47,23 +60,31 @@ public void onDisable() { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { if (command.getName().equalsIgnoreCase("botblocker")) { if (args.length > 0) { - if (args[0].equalsIgnoreCase("enable")) { - pluginEnabled = true; - sender.sendMessage("BotBlocker enabled."); - } else if (args[0].equalsIgnoreCase("disable")) { - pluginEnabled = false; - sender.sendMessage("BotBlocker disabled."); - } else if (args[0].equalsIgnoreCase("setTimeLimit") && args.length > 1) { - try { - timeLimit = Integer.parseInt(args[1]); - getConfig().set("time-limit", timeLimit); - saveConfig(); - sender.sendMessage("Time limit set to " + timeLimit + " seconds."); - } catch (NumberFormatException e) { - sender.sendMessage("Invalid number format. Please enter a valid integer."); - } - } else { - return false; + String botblockerCommand = args[0].toLowerCase(); + switch (botblockerCommand) { + case "enable": + commandHandler.enable(sender); + break; + case "disable": + commandHandler.disable(sender); + break; + case "status": + commandHandler.status(sender); + break; + case "settimelimit": + commandHandler.setTimeLimit(sender, args); + break; + case "gettimelimit": + commandHandler.getTimeLimit(sender); + break; + case "setbanmessage": + commandHandler.setBanMessage(sender, args); + break; + case "getbanmessage": + commandHandler.getBanMessage(sender); + break; + default: + return false; } return true; } @@ -93,7 +114,7 @@ public void onPlayerQuit(PlayerQuitEvent event) { long timeConnected = (System.currentTimeMillis() - joinTime) / 1000; if (timeConnected < timeLimit) { String playerName = event.getPlayer().getName(); - Bukkit.getBanList(Type.NAME).addBan(playerName, "Bot detected. If you are a legitimate user, please contact the admin.", null, "Sistema anti-bot"); + Bukkit.getBanList(Type.PROFILE).addBan(playerName, banMessage, null, "BotBlocker"); getLogger().info("Player '" + playerName + "' was banned for disconnecting within " + timeLimit + " seconds of joining for the first time - suspected bot."); } else { // Add the player to players.yml if it is not banned @@ -104,6 +125,43 @@ public void onPlayerQuit(PlayerQuitEvent event) { } } + /** + * Set the ban message. + * @param message Ban message + */ + public void setPluginEnabled(boolean enabled) { + pluginEnabled = enabled; + } + + /** + * Show wether BotBlocker is enabled or disabled. + * @return true if BotBlocker is enabled, false otherwise + */ + public boolean isPluginEnabled() { + return pluginEnabled; + } + + /** + * Set the time limit for detecting bots. Default is 5 seconds. + * @param timeLimit Time limit in seconds + */ + public void setTimeLimit(int timeLimit) { + this.timeLimit = timeLimit; + getConfig().set("time-limit", timeLimit); + saveConfig(); + } + + /** + * Get the configured time limit for detecting bots. + * @return Time limit in seconds + */ + public int getTimeLimit() { + return timeLimit; + } + + /** + * Save the players configuration in the players.yml file. + */ private void savePlayers() { try { playersCfg.save(playersFile); diff --git a/src/main/java/ovh/aichan/botblockerminecraft/CommandHandler.java b/src/main/java/ovh/aichan/botblockerminecraft/CommandHandler.java new file mode 100644 index 0000000..70a747d --- /dev/null +++ b/src/main/java/ovh/aichan/botblockerminecraft/CommandHandler.java @@ -0,0 +1,99 @@ +package ovh.aichan.botblockerminecraft; + +import org.bukkit.command.CommandSender; + +public class CommandHandler { + + private final BotBlocker plugin; + + /** + * Constructor for CommandHandler. + * @param plugin BotBlocker plugin + */ + public CommandHandler(BotBlocker plugin) { + this.plugin = plugin; + } + + /** + * Enable the BotBlocker plugin. + * @param sender Command sender + */ + public void enable(CommandSender sender) { + plugin.setPluginEnabled(true); + sender.sendMessage("BotBlocker enabled."); + plugin.saveConfig(); + } + + /** + * Disable the BotBlocker plugin. + * @param sender Command sender + */ + public void disable(CommandSender sender) { + plugin.setPluginEnabled(false); + sender.sendMessage("BotBlocker disabled."); + plugin.saveConfig(); + } + + /** + * Show wether BotBlocker is enabled or disabled. + * @param sender Command sender + */ + public void status(CommandSender sender) { + sender.sendMessage("BotBlocker status: " + (plugin.isEnabled() ? "§a§lENABLED" : "§c§lDISABLED")); + } + + /** + * Set the time limit for detecting bots. Default is 5 seconds. + * @param sender Command sender + * @param args Command arguments + */ + public void setTimeLimit(CommandSender sender, String[] args) { + if(args.length > 1) { + try { + int timeLimit = Integer.parseInt(args[1]); + plugin.setTimeLimit(timeLimit); + sender.sendMessage("Time limit set to " + timeLimit + " seconds."); + } catch (NumberFormatException e) { + sender.sendMessage("Invalid number format. Please enter a valid integer."); + } + } else { + sender.sendMessage("Usage: /botblocker setTimeLimit "); + } + } + + /** + * Display the configured time limit for detecting bots. + * @param sender Command sender + */ + public void getTimeLimit(CommandSender sender) { + sender.sendMessage("Time limit set to " + plugin.getTimeLimit() + " seconds."); + } + + /** + * Set the ban message. + * @param sender Command sender + * @param args Command arguments + */ + public void setBanMessage(CommandSender sender, String[] args) { + if(args.length > 1) { + StringBuilder message = new StringBuilder(); + for(int i = 1; i < args.length; i++) { + message.append(args[i]).append(" "); + } + plugin.getConfig().set("ban-message", message.toString()); + plugin.saveConfig(); + sender.sendMessage("Ban message set to: " + message.toString()); + } else { + sender.sendMessage("Usage: /botblocker setBanMessage "); + } + } + + /** + * Display the configured ban message. + * @param sender Command sender + */ + public void getBanMessage(CommandSender sender) { + sender.sendMessage("Ban message: " + plugin.getConfig().getString("ban-message")); + } + +} \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index de47211..77400b8 100755 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,2 +1,4 @@ # Time limit in seconds -time-limit: 60 +enabled: true +time-limit: 5 +ban-message: "Bot detected. If you are a legitimate user, please contact the admin." diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index ddb57a1..d51878b 100755 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,6 +1,6 @@ name: BotBlocker -main: eus.aichan.Blocker.BotBlocker -version: 1.2 +main: ovh.aichan.botblockerminecraft.BotBlocker +version: 1.3.0 api-version: '1.20' author: aichan description: A plugin to ban users who connect for the first time and disconnect within a certain time limit. @@ -8,6 +8,10 @@ commands: botblocker: description: Manages the BotBlocker plugin usage: | - / enable - Enables the BotBlocker plugin - / disable - Disables the BotBlocker plugin - / setTimeLimit [seconds] - Sets the time limit + / enable - Enable the BotBlocker plugin. + / disable - Disable the BotBlocker plugin. + / status - Show wether BotBlocker is enabled or disabled. + / setTimeLimit [seconds] - Set the time limit for detecting bots. Default is 5 seconds. + / getTimeLimit - Display the configured time limit for detecting bots. + / setBanMessage [message] - Set the ban message. + / getBanMessage - Display the configured ban message.