Skip to content

Commit

Permalink
Merge branch 'feature/banmanager'
Browse files Browse the repository at this point in the history
  • Loading branch information
AitorAstorga committed Feb 28, 2024
2 parents b5d7748 + 4a193df commit d3be432
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<!-- This adds the Frostcast CI Maven repository to the build -->
<repository>
<id>confuser-repo</id>
<url>https://ci.frostcast.net/plugin/repository/everything</url>
</repository>
</repositories>

<dependencies>
Expand All @@ -30,6 +35,13 @@
<version>1.20.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<!-- This adds the BmAPI artifact to the build -->
<dependency>
<groupId>me.confuser.banmanager</groupId>
<artifactId>BanManagerCommon</artifactId>
<version>7.7.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand Down
49 changes: 46 additions & 3 deletions src/main/java/ovh/aichan/botblockerminecraft/BotBlocker.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,23 @@
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
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.Plugin;
import org.bukkit.plugin.java.JavaPlugin;

import me.confuser.banmanager.common.api.BmAPI;
import me.confuser.banmanager.common.data.PlayerBanData;
import me.confuser.banmanager.common.data.PlayerData;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;
import java.util.logging.Level;

public class BotBlocker extends JavaPlugin implements Listener {

Expand Down Expand Up @@ -114,7 +121,17 @@ public void onPlayerQuit(PlayerQuitEvent event) {
long timeConnected = (System.currentTimeMillis() - joinTime) / 1000;
if (timeConnected < timeLimit) {
String playerName = event.getPlayer().getName();
Bukkit.getBanList(Type.PROFILE).addBan(playerName, banMessage, null, "BotBlocker");

// Check if BanManager is installed and enabled
Plugin banManager = Bukkit.getServer().getPluginManager().getPlugin("BanManager");
if (banManager != null && banManager.isEnabled()) {
// Use BanManager to ban the player
banWithBanManager(event.getPlayer(), banMessage);
} else {
// Fallback to Bukkit's default banning system
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 @@ -125,6 +142,32 @@ public void onPlayerQuit(PlayerQuitEvent event) {
}
}

public void banWithBanManager(Player player, String reason) {
Bukkit.getScheduler().runTaskAsynchronously(this, () -> {
try {
UUID playerUuid = player.getUniqueId();
String playerName = player.getName();

// Creating PlayerData for the player.
PlayerData playerData = new PlayerData(playerUuid, playerName);

// Using 'BotBlocker' as the system or admin name issuing the ban
UUID actorUuid = UUID.randomUUID(); // This could be a specific UUID if you have one for the system/admin
String actorName = "BotBlocker";
PlayerData actorData = new PlayerData(actorUuid, actorName);

// Construct the PlayerBanData object
PlayerBanData ban = new PlayerBanData(playerData, actorData, reason, false);

// Attempt to ban the player using BanManager
BmAPI.ban(ban);
} catch (Exception e) {
getLogger().warning("[BotBlocker] An error occurred while trying to ban a player with BanManager. See logs for details.");
getLogger().log(Level.SEVERE, e.toString(), e);
}
});
}

/**
* Set the ban message.
* @param message Ban message
Expand Down Expand Up @@ -166,8 +209,8 @@ private void savePlayers() {
try {
playersCfg.save(playersFile);
} catch (IOException e) {
// deepcode ignore DontUsePrintStackTrace: This debug information is useful in Minecraft
e.printStackTrace();
getLogger().warning("[BotBlocker] An error occurred while trying to save the players.yml file. See logs for details.");
getLogger().log(Level.SEVERE, e.toString(), e);
}
}

Expand Down

0 comments on commit d3be432

Please sign in to comment.