Skip to content

Commit 25f24d2

Browse files
committed
Add new commands and improve codebase
1 parent 72ef2a3 commit 25f24d2

File tree

7 files changed

+244
-40
lines changed

7 files changed

+244
-40
lines changed

CHANGELOG.md

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
# Changelog
22

3-
## v1.0.0
3+
## 1.3.0
44

55
### Added or Changed
6-
- Added this changelog :)
7-
- Fixed typos in both templates
8-
- Back to top links
9-
- Added more "Built With" frameworks/libraries
10-
- Changed table of contents to start collapsed
11-
- Added checkboxes for major features on roadmap
6+
- Added `ban-message` to the `config.yml`
7+
- Lowered time limit to 5 seconds
8+
- Added the following commands:
9+
* `/BotBlocker status` - Show wether BotBlocker is enabled or disabled.
10+
* `/BotBlocker getTimeLimit` - Display the configured time limit for detecting bots.
11+
* `/BotBlocker setBanMessage [message]` - Set the ban message.
12+
* `/BotBlocker getBanMessage` - Display the configured ban message.
13+
- Added brief instructions for compiling this Bukkit/Spigot plugin using Maven (so I won't forget next time).
14+
- Fixed package names to be compliant with naming conventions.
15+
- Added usage hint to the setTimeLimit command.
16+
- Created `CommandHandler` class to improve readability.
17+
- Updated `README.md` and this `CHANGELOG.md` :)
1218

1319
### Removed
14-
15-
- Some packages/libraries from acknowledgements I no longer use
20+
- Maven target

README.md

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
</ul>
6262
</li>
6363
<li><a href="#usage">Usage</a></li>
64+
<li><a href="#compiling-this-plugin">Compiling this plugin</a></li>
6465
<li><a href="#contributing">Contributing</a></li>
6566
<li><a href="#license">License</a></li>
6667
<li><a href="#contact">Contact</a></li>
@@ -103,9 +104,13 @@ When a player joins, if BotBlocker is enabled and the player is not already exem
103104
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.
104105

105106
### Commands
106-
* `/BotBlocker enable` - Enables BotBlocker.
107-
* `/BotBlocker disable` - Disables BotBlocker.
108-
* `/BotBlocker setTimeLimit [seconds]` - Sets the time limit for detecting bots.
107+
* `/BotBlocker enable` - Enable the BotBlocker plugin.
108+
* `/BotBlocker disable` - Disable the BotBlocker plugin.
109+
* `/BotBlocker status` - Show wether BotBlocker is enabled or disabled.
110+
* `/BotBlocker setTimeLimit [seconds]` - Set the time limit for detecting bots. Default is 5 seconds.
111+
* `/BotBlocker getTimeLimit` - Display the configured time limit for detecting bots.
112+
* `/BotBlocker setBanMessage [message]` - Set the ban message.
113+
* `/BotBlocker getBanMessage` - Display the configured ban message.
109114

110115
### Configuration Files
111116
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
143148
<p align="right">(<a href="#readme-top">back to top</a>)</p>
144149

145150

151+
## Compiling this plugin
152+
To compile BotBlocker for Bukkit/Spigot yourself, follow these steps:
153+
154+
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.
155+
156+
2. Clone this repository.
157+
158+
3. Open your terminal or command prompt.
159+
160+
4. Navigate to the root directory of the cloned repository.
161+
162+
```
163+
cd path/to/BotBlocker
164+
```
165+
166+
5. Execute the Maven build command:
167+
168+
```
169+
mvn clean package
170+
```
171+
172+
- The `clean` command will remove any previous build outputs to ensure a fresh build.
173+
- The `package` command will compile your code and package it into a JAR file.
174+
175+
6. After the build completes, you can find the compiled JAR file in the `target` directory of the project.
176+
177+
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/).
178+
179+
8. You can now deploy this JAR file to your Bukkit/Spigot server's `plugins` folder.
180+
181+
146182

147183
<!-- CONTRIBUTING -->
148184
## Contributing

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
44
<modelVersion>4.0.0</modelVersion>
55

6-
<groupId>eus.aichan</groupId>
6+
<groupId>ovh.aichan</groupId>
77
<artifactId>BotBlocker</artifactId>
8-
<version>1.2-SNAPSHOT</version>
8+
<version>1.3.0</version>
99
<packaging>jar</packaging>
1010

1111
<properties>
@@ -61,7 +61,7 @@
6161
<manifest>
6262
<addClasspath>true</addClasspath>
6363
<classpathPrefix>lib/</classpathPrefix>
64-
<mainClass>com.tuDominio.BotBlocker</mainClass>
64+
<mainClass>ovh.aichan.botblockerminecraft.BotBlocker</mainClass>
6565
</manifest>
6666
</archive>
6767
</configuration>

src/main/java/eus/aichan/Blocker/BotBlocker.java renamed to src/main/java/ovh/aichan/botblockerminecraft/BotBlocker.java

Lines changed: 77 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package eus.aichan.Blocker;
1+
package ovh.aichan.botblockerminecraft;
22

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

2222
private boolean pluginEnabled = true;
2323
private int timeLimit; // In seconds
24+
private String banMessage;
2425
private HashMap<UUID, Long> joinTimes = new HashMap<>();
2526
private FileConfiguration playersCfg;
2627
private File playersFile;
28+
private CommandHandler commandHandler;
2729

2830
@Override
2931
public void onEnable() {
32+
initialize();
33+
commandHandler = new CommandHandler(this);
34+
}
35+
36+
/**
37+
* Initialize the plugin.
38+
*/
39+
private void initialize() {
3040
saveDefaultConfig();
41+
pluginEnabled = getConfig().getBoolean("enabled", true);
3142
timeLimit = getConfig().getInt("time-limit", 5); // Default to 5 seconds
43+
banMessage = getConfig().getString("ban-message", "Bot detected. If you are a legitimate user, please contact the admin.");
3244
Bukkit.getPluginManager().registerEvents(this, this);
3345

3446
playersFile = new File(getDataFolder(), "players.yml");
3547
if (!playersFile.exists()) {
3648
saveResource("players.yml", false);
3749
}
3850
playersCfg = YamlConfiguration.loadConfiguration(playersFile);
51+
saveConfig();
3952
}
4053

4154
@Override
@@ -47,23 +60,31 @@ public void onDisable() {
4760
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
4861
if (command.getName().equalsIgnoreCase("botblocker")) {
4962
if (args.length > 0) {
50-
if (args[0].equalsIgnoreCase("enable")) {
51-
pluginEnabled = true;
52-
sender.sendMessage("BotBlocker enabled.");
53-
} else if (args[0].equalsIgnoreCase("disable")) {
54-
pluginEnabled = false;
55-
sender.sendMessage("BotBlocker disabled.");
56-
} else if (args[0].equalsIgnoreCase("setTimeLimit") && args.length > 1) {
57-
try {
58-
timeLimit = Integer.parseInt(args[1]);
59-
getConfig().set("time-limit", timeLimit);
60-
saveConfig();
61-
sender.sendMessage("Time limit set to " + timeLimit + " seconds.");
62-
} catch (NumberFormatException e) {
63-
sender.sendMessage("Invalid number format. Please enter a valid integer.");
64-
}
65-
} else {
66-
return false;
63+
String botblockerCommand = args[0].toLowerCase();
64+
switch (botblockerCommand) {
65+
case "enable":
66+
commandHandler.enable(sender);
67+
break;
68+
case "disable":
69+
commandHandler.disable(sender);
70+
break;
71+
case "status":
72+
commandHandler.status(sender);
73+
break;
74+
case "settimelimit":
75+
commandHandler.setTimeLimit(sender, args);
76+
break;
77+
case "gettimelimit":
78+
commandHandler.getTimeLimit(sender);
79+
break;
80+
case "setbanmessage":
81+
commandHandler.setBanMessage(sender, args);
82+
break;
83+
case "getbanmessage":
84+
commandHandler.getBanMessage(sender);
85+
break;
86+
default:
87+
return false;
6788
}
6889
return true;
6990
}
@@ -93,7 +114,7 @@ public void onPlayerQuit(PlayerQuitEvent event) {
93114
long timeConnected = (System.currentTimeMillis() - joinTime) / 1000;
94115
if (timeConnected < timeLimit) {
95116
String playerName = event.getPlayer().getName();
96-
Bukkit.getBanList(Type.NAME).addBan(playerName, "Bot detected. If you are a legitimate user, please contact the admin.", null, "Sistema anti-bot");
117+
Bukkit.getBanList(Type.PROFILE).addBan(playerName, banMessage, null, "BotBlocker");
97118
getLogger().info("Player '" + playerName + "' was banned for disconnecting within " + timeLimit + " seconds of joining for the first time - suspected bot.");
98119
} else {
99120
// Add the player to players.yml if it is not banned
@@ -104,6 +125,43 @@ public void onPlayerQuit(PlayerQuitEvent event) {
104125
}
105126
}
106127

128+
/**
129+
* Set the ban message.
130+
* @param message Ban message
131+
*/
132+
public void setPluginEnabled(boolean enabled) {
133+
pluginEnabled = enabled;
134+
}
135+
136+
/**
137+
* Show wether BotBlocker is enabled or disabled.
138+
* @return true if BotBlocker is enabled, false otherwise
139+
*/
140+
public boolean isPluginEnabled() {
141+
return pluginEnabled;
142+
}
143+
144+
/**
145+
* Set the time limit for detecting bots. Default is 5 seconds.
146+
* @param timeLimit Time limit in seconds
147+
*/
148+
public void setTimeLimit(int timeLimit) {
149+
this.timeLimit = timeLimit;
150+
getConfig().set("time-limit", timeLimit);
151+
saveConfig();
152+
}
153+
154+
/**
155+
* Get the configured time limit for detecting bots.
156+
* @return Time limit in seconds
157+
*/
158+
public int getTimeLimit() {
159+
return timeLimit;
160+
}
161+
162+
/**
163+
* Save the players configuration in the players.yml file.
164+
*/
107165
private void savePlayers() {
108166
try {
109167
playersCfg.save(playersFile);
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package ovh.aichan.botblockerminecraft;
2+
3+
import org.bukkit.command.CommandSender;
4+
5+
public class CommandHandler {
6+
7+
private final BotBlocker plugin;
8+
9+
/**
10+
* Constructor for CommandHandler.
11+
* @param plugin BotBlocker plugin
12+
*/
13+
public CommandHandler(BotBlocker plugin) {
14+
this.plugin = plugin;
15+
}
16+
17+
/**
18+
* Enable the BotBlocker plugin.
19+
* @param sender Command sender
20+
*/
21+
public void enable(CommandSender sender) {
22+
plugin.setPluginEnabled(true);
23+
sender.sendMessage("BotBlocker enabled.");
24+
plugin.saveConfig();
25+
}
26+
27+
/**
28+
* Disable the BotBlocker plugin.
29+
* @param sender Command sender
30+
*/
31+
public void disable(CommandSender sender) {
32+
plugin.setPluginEnabled(false);
33+
sender.sendMessage("BotBlocker disabled.");
34+
plugin.saveConfig();
35+
}
36+
37+
/**
38+
* Show wether BotBlocker is enabled or disabled.
39+
* @param sender Command sender
40+
*/
41+
public void status(CommandSender sender) {
42+
sender.sendMessage("BotBlocker status: " + (plugin.isEnabled() ? "§a§lENABLED" : "§c§lDISABLED"));
43+
}
44+
45+
/**
46+
* Set the time limit for detecting bots. Default is 5 seconds.
47+
* @param sender Command sender
48+
* @param args Command arguments
49+
*/
50+
public void setTimeLimit(CommandSender sender, String[] args) {
51+
if(args.length > 1) {
52+
try {
53+
int timeLimit = Integer.parseInt(args[1]);
54+
plugin.setTimeLimit(timeLimit);
55+
sender.sendMessage("Time limit set to " + timeLimit + " seconds.");
56+
} catch (NumberFormatException e) {
57+
sender.sendMessage("Invalid number format. Please enter a valid integer.");
58+
}
59+
} else {
60+
sender.sendMessage("Usage: /botblocker setTimeLimit <timeLimit>");
61+
}
62+
}
63+
64+
/**
65+
* Display the configured time limit for detecting bots.
66+
* @param sender Command sender
67+
*/
68+
public void getTimeLimit(CommandSender sender) {
69+
sender.sendMessage("Time limit set to " + plugin.getTimeLimit() + " seconds.");
70+
}
71+
72+
/**
73+
* Set the ban message.
74+
* @param sender Command sender
75+
* @param args Command arguments
76+
*/
77+
public void setBanMessage(CommandSender sender, String[] args) {
78+
if(args.length > 1) {
79+
StringBuilder message = new StringBuilder();
80+
for(int i = 1; i < args.length; i++) {
81+
message.append(args[i]).append(" ");
82+
}
83+
plugin.getConfig().set("ban-message", message.toString());
84+
plugin.saveConfig();
85+
sender.sendMessage("Ban message set to: " + message.toString());
86+
} else {
87+
sender.sendMessage("Usage: /botblocker setBanMessage <message>");
88+
}
89+
}
90+
91+
/**
92+
* Display the configured ban message.
93+
* @param sender Command sender
94+
*/
95+
public void getBanMessage(CommandSender sender) {
96+
sender.sendMessage("Ban message: " + plugin.getConfig().getString("ban-message"));
97+
}
98+
99+
}

src/main/resources/config.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
# Time limit in seconds
2-
time-limit: 60
2+
enabled: true
3+
time-limit: 5
4+
ban-message: "Bot detected. If you are a legitimate user, please contact the admin."

src/main/resources/plugin.yml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
name: BotBlocker
2-
main: eus.aichan.Blocker.BotBlocker
3-
version: 1.2
2+
main: ovh.aichan.botblockerminecraft.BotBlocker
3+
version: 1.3.0
44
api-version: '1.20'
55
author: aichan
66
description: A plugin to ban users who connect for the first time and disconnect within a certain time limit.
77
commands:
88
botblocker:
99
description: Manages the BotBlocker plugin
1010
usage: |
11-
/<command> enable - Enables the BotBlocker plugin
12-
/<command> disable - Disables the BotBlocker plugin
13-
/<command> setTimeLimit [seconds] - Sets the time limit
11+
/<command> enable - Enable the BotBlocker plugin.
12+
/<command> disable - Disable the BotBlocker plugin.
13+
/<command> status - Show wether BotBlocker is enabled or disabled.
14+
/<command> setTimeLimit [seconds] - Set the time limit for detecting bots. Default is 5 seconds.
15+
/<command> getTimeLimit - Display the configured time limit for detecting bots.
16+
/<command> setBanMessage [message] - Set the ban message.
17+
/<command> getBanMessage - Display the configured ban message.

0 commit comments

Comments
 (0)