Skip to content

Commit

Permalink
Add main class and configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
AitorAstorga committed Jul 19, 2023
1 parent e419c50 commit 25cfe34
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 0 deletions.
57 changes: 57 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>
<artifactId>BotBlocker</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.17.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<defaultGoal>clean package</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.tuDominio.BotBlocker</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
100 changes: 100 additions & 0 deletions src/main/java/eus/aichan/Blocker/BotBlocker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package eus.aichan.Blocker;

import org.bukkit.Bukkit;
import org.bukkit.BanList.Type;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.util.HashMap;
import java.util.UUID;

public class BotBlocker extends JavaPlugin implements Listener {

private boolean pluginEnabled = true;
private int timeLimit; // In seconds
private HashMap<UUID, Long> joinTimes = new HashMap<>();
private FileConfiguration playersCfg;

@Override
public void onEnable() {
saveDefaultConfig();
timeLimit = getConfig().getInt("time-limit", 60); // Default to 60 seconds
Bukkit.getPluginManager().registerEvents(this, this);

File playersFile = new File(getDataFolder(), "players.yml");
if (!playersFile.exists()) {
saveResource("players.yml", false);
}
playersCfg = YamlConfiguration.loadConfiguration(playersFile);
}

@Override
public void onDisable() {
saveConfig();
}

@Override
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;
}
return true;
}
}
return false;
}

@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
if (!pluginEnabled) return;

UUID playerId = event.getPlayer().getUniqueId();
if (!playersCfg.contains(playerId.toString())) {
joinTimes.put(playerId, System.currentTimeMillis());
playersCfg.set(playerId.toString(), true);
saveConfig();
}
}

@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
if (!pluginEnabled) return;

UUID playerId = event.getPlayer().getUniqueId();
if (joinTimes.containsKey(playerId)) {
long joinTime = joinTimes.get(playerId);
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");
getLogger().info("Player '" + playerName + "' was banned for disconnecting within " + timeLimit + " seconds of joining for the first time - suspected bot.");
}
joinTimes.remove(playerId);
}
}
}
2 changes: 2 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Time limit in seconds
time-limit: 60
11 changes: 11 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: BotBlocker
main: eus.aichan.BotBlocker
version: 1.0
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
2 changes: 2 additions & 0 deletions target/classes/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Time limit in seconds
time-limit: 60
Binary file not shown.
11 changes: 11 additions & 0 deletions target/classes/plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: BotBlocker
main: eus.aichan.BotBlocker
version: 1.0
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

0 comments on commit 25cfe34

Please sign in to comment.