diff --git a/src/main/java/fr/Alphart/BAT/Configuration.java b/src/main/java/fr/Alphart/BAT/Configuration.java index 5f488ad..d21c8f9 100644 --- a/src/main/java/fr/Alphart/BAT/Configuration.java +++ b/src/main/java/fr/Alphart/BAT/Configuration.java @@ -38,6 +38,8 @@ public Configuration(){ @Comment("Make the date more readable." + "If the date correspond to today, tmw or yda, it will replace the date by the corresponding word") private boolean litteralDate = true; + @Comment("Send banned player to a specified server through BungeeCord. Leave 'none' to turn the feature off") + private String bannedServer = "none"; @Comment("Enable BETA (experimental) Redis support, requires RedisBungee") private boolean redisSupport = false; @Comment("For special setups, leave to false by default") diff --git a/src/main/java/fr/Alphart/BAT/Modules/Ban/Ban.java b/src/main/java/fr/Alphart/BAT/Modules/Ban/Ban.java index 23945cb..53c3ef0 100644 --- a/src/main/java/fr/Alphart/BAT/Modules/Ban/Ban.java +++ b/src/main/java/fr/Alphart/BAT/Modules/Ban/Ban.java @@ -1,48 +1,45 @@ package fr.Alphart.BAT.Modules.Ban; -import static fr.Alphart.BAT.I18n.I18n._; - -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.sql.Timestamp; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.UUID; -import java.util.concurrent.TimeUnit; +import com.imaginarycode.minecraft.redisbungee.RedisBungee; import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.TextComponent; +import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.connection.PendingConnection; import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.event.LoginEvent; +import net.md_5.bungee.api.event.PostLoginEvent; import net.md_5.bungee.api.event.ServerConnectEvent; import net.md_5.bungee.api.plugin.Listener; import net.md_5.bungee.api.scheduler.ScheduledTask; import net.md_5.bungee.event.EventHandler; -import com.imaginarycode.minecraft.redisbungee.RedisBungee; +import java.sql.*; +import java.util.*; +import java.util.concurrent.TimeUnit; import fr.Alphart.BAT.BAT; import fr.Alphart.BAT.Modules.BATCommand; +import fr.Alphart.BAT.Modules.Core.Core; import fr.Alphart.BAT.Modules.IModule; import fr.Alphart.BAT.Modules.ModuleConfiguration; -import fr.Alphart.BAT.Modules.Core.Core; import fr.Alphart.BAT.Utils.FormatUtils; import fr.Alphart.BAT.Utils.UUIDNotFoundException; import fr.Alphart.BAT.Utils.Utils; import fr.Alphart.BAT.database.DataSourceHandler; import fr.Alphart.BAT.database.SQLQueries; +import static fr.Alphart.BAT.I18n.I18n._; + public class Ban implements IModule, Listener { private final String name = "ban"; - private ScheduledTask task; + private ScheduledTask tempBanTask; + private ScheduledTask clearPendingTask; private BanCommand commandHandler; private final BanConfig config; + private ServerInfo banServer; + private Set pendingPlayers = new HashSet(); public Ban(){ config = new BanConfig(); @@ -89,9 +86,9 @@ public boolean load() { commandHandler = new BanCommand(this); commandHandler.loadCmds(); - // Launch tempban task + // Launch tempBanTask final BanExpirationTask banExpirationTask = new BanExpirationTask(this); - task = ProxyServer.getInstance().getScheduler().schedule(BAT.getInstance(), banExpirationTask, 0, 10, TimeUnit.SECONDS); + tempBanTask = ProxyServer.getInstance().getScheduler().schedule(BAT.getInstance(), banExpirationTask, 0, 10, TimeUnit.SECONDS); // Check if the online players are banned (if the module has been reloaded) for(final ProxiedPlayer player : ProxyServer.getInstance().getPlayers()){ @@ -110,12 +107,21 @@ public boolean load() { } } - return true; + // Set banServer is + String serverName = BAT.getInstance().getConfiguration().getBannedServer(); + banServer = serverName.equals("none") ? null : ProxyServer.getInstance().getServerInfo(serverName); + if (banServer != null) { + clearPendingTask = ProxyServer.getInstance().getScheduler().schedule(BAT.getInstance(), () -> { + pendingPlayers.clear(); + }, 0, 30, TimeUnit.SECONDS); + } + + return true; } @Override public boolean unload() { - task.cancel(); + tempBanTask.cancel(); return true; } @@ -684,14 +690,7 @@ public void run() { String uuid = null; try(Connection conn = BAT.getConnection()){ statement = conn.prepareStatement("SELECT ban_id FROM `BAT_ban` WHERE ban_state = 1 AND UUID = ? AND ban_server = '" + GLOBAL_SERVER + "';"); - // If this is an online mode server, the uuid will be already set - if(ev.getConnection().getUniqueId() != null && ProxyServer.getInstance().getConfig().isOnlineMode()){ - uuid = ev.getConnection().getUniqueId().toString().replaceAll( "-", "" ); - } - // Otherwise it's an offline mode server, so we're gonna generate the UUID using player name (hashing) - else{ - uuid = Utils.getOfflineUUID(ev.getConnection().getName()); - } + uuid = Utils.getUUID(ev.getConnection()); statement.setString(1, uuid); resultSet = statement.executeQuery(); @@ -704,13 +703,25 @@ public void run() { DataSourceHandler.close(statement, resultSet); } - if ((isBanPlayer) || (isBan(ev.getConnection().getAddress().getAddress().getHostAddress(), GLOBAL_SERVER))) { - BaseComponent[] bM = getBanMessage(ev.getConnection(), GLOBAL_SERVER); - ev.setCancelReason(TextComponent.toLegacyText(bM)); - ev.setCancelled(true); - } + if ((isBanPlayer) || (isBan(ev.getConnection().getAddress().getAddress().getHostAddress(), GLOBAL_SERVER))) { + if (banServer != null) { + pendingPlayers.add(uuid); + } + BaseComponent[] bM = getBanMessage(ev.getConnection(), GLOBAL_SERVER); + ev.setCancelReason(TextComponent.toLegacyText(bM)); + ev.setCancelled(true); + } ev.completeIntent(BAT.getInstance()); } }); } + + @EventHandler + public void onPlayerPostLogin(PostLoginEvent ev) { + String uuid = Utils.getUUID(ev.getPlayer()); + if (pendingPlayers.contains(uuid)) { + ev.getPlayer().connect(banServer); + pendingPlayers.remove(uuid); + } + } } \ No newline at end of file diff --git a/src/main/java/fr/Alphart/BAT/Utils/Utils.java b/src/main/java/fr/Alphart/BAT/Utils/Utils.java index e7a1f97..63954e9 100644 --- a/src/main/java/fr/Alphart/BAT/Utils/Utils.java +++ b/src/main/java/fr/Alphart/BAT/Utils/Utils.java @@ -1,5 +1,14 @@ package fr.Alphart.BAT.Utils; +import com.google.common.base.Charsets; +import com.google.common.reflect.TypeToken; +import com.google.gson.Gson; + +import net.md_5.bungee.api.ChatColor; +import net.md_5.bungee.api.ProxyServer; +import net.md_5.bungee.api.connection.PendingConnection; +import net.md_5.bungee.api.connection.ProxiedPlayer; + import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; @@ -11,14 +20,6 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -import net.md_5.bungee.api.ChatColor; -import net.md_5.bungee.api.ProxyServer; -import net.md_5.bungee.api.connection.ProxiedPlayer; - -import com.google.common.base.Charsets; -import com.google.common.reflect.TypeToken; -import com.google.gson.Gson; - import fr.Alphart.BAT.BAT; public class Utils { @@ -229,4 +230,26 @@ public static String getOfflineUUID(String pName){ return java.util.UUID.nameUUIDFromBytes(("OfflinePlayer:" + pName.toLowerCase()) //Dsiable case sensitivity .getBytes(Charsets.UTF_8)).toString().replaceAll( "-", "" ); } + + public static String getUUID(final PendingConnection conn) { + // If this is an online mode server, the uuid will be already set + if (conn.getUniqueId() != null && ProxyServer.getInstance().getConfig().isOnlineMode()) { + return conn.getUniqueId().toString().replaceAll("-", ""); + } + // Otherwise it's an offline mode server, so we're gonna generate the UUID using player name (hashing) + else { + return Utils.getOfflineUUID(conn.getName()); + } + } + + public static String getUUID(final ProxiedPlayer pl) { + // If this is an online mode server, the uuid will be already set + if (pl.getUniqueId() != null && ProxyServer.getInstance().getConfig().isOnlineMode()) { + return pl.getUniqueId().toString().replaceAll("-", ""); + } + // Otherwise it's an offline mode server, so we're gonna generate the UUID using player name (hashing) + else { + return Utils.getOfflineUUID(pl.getName()); + } + } } \ No newline at end of file