Skip to content

Commit

Permalink
Add new commands and improve codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
AitorAstorga committed Feb 26, 2024
1 parent 72ef2a3 commit 25f24d2
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 40 deletions.
23 changes: 14 additions & 9 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
- Maven target
42 changes: 39 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
</ul>
</li>
<li><a href="#usage">Usage</a></li>
<li><a href="#compiling-this-plugin">Compiling this plugin</a></li>
<li><a href="#contributing">Contributing</a></li>
<li><a href="#license">License</a></li>
<li><a href="#contact">Contact</a></li>
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -143,6 +148,37 @@ Once installed and enabled, BotBlocker works in the background without any inter
<p align="right">(<a href="#readme-top">back to top</a>)</p>


## 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 -->
## Contributing
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>eus.aichan</groupId>
<groupId>ovh.aichan</groupId>
<artifactId>BotBlocker</artifactId>
<version>1.2-SNAPSHOT</version>
<version>1.3.0</version>
<packaging>jar</packaging>

<properties>
Expand Down Expand Up @@ -61,7 +61,7 @@
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.tuDominio.BotBlocker</mainClass>
<mainClass>ovh.aichan.botblockerminecraft.BotBlocker</mainClass>
</manifest>
</archive>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eus.aichan.Blocker;
package ovh.aichan.botblockerminecraft;

import org.bukkit.Bukkit;
import org.bukkit.BanList.Type;
Expand All @@ -21,21 +21,34 @@ public class BotBlocker extends JavaPlugin implements Listener {

private boolean pluginEnabled = true;
private int timeLimit; // In seconds
private String banMessage;
private HashMap<UUID, Long> 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");
if (!playersFile.exists()) {
saveResource("players.yml", false);
}
playersCfg = YamlConfiguration.loadConfiguration(playersFile);
saveConfig();
}

@Override
Expand All @@ -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;
}
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down
99 changes: 99 additions & 0 deletions src/main/java/ovh/aichan/botblockerminecraft/CommandHandler.java
Original file line number Diff line number Diff line change
@@ -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 <timeLimit>");
}
}

/**
* 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 <message>");
}
}

/**
* Display the configured ban message.
* @param sender Command sender
*/
public void getBanMessage(CommandSender sender) {
sender.sendMessage("Ban message: " + plugin.getConfig().getString("ban-message"));
}

}
4 changes: 3 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -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."
14 changes: 9 additions & 5 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
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.
commands:
botblocker:
description: Manages the BotBlocker plugin
usage: |
/<command> enable - Enables the BotBlocker plugin
/<command> disable - Disables the BotBlocker plugin
/<command> setTimeLimit [seconds] - Sets the time limit
/<command> enable - Enable the BotBlocker plugin.
/<command> disable - Disable the BotBlocker plugin.
/<command> status - Show wether BotBlocker is enabled or disabled.
/<command> setTimeLimit [seconds] - Set the time limit for detecting bots. Default is 5 seconds.
/<command> getTimeLimit - Display the configured time limit for detecting bots.
/<command> setBanMessage [message] - Set the ban message.
/<command> getBanMessage - Display the configured ban message.

0 comments on commit 25f24d2

Please sign in to comment.