Skip to content
This repository has been archived by the owner on Mar 8, 2023. It is now read-only.

Commit

Permalink
Added "server for banned people" as requested by issue
Browse files Browse the repository at this point in the history
  • Loading branch information
VaiTon committed Jul 25, 2018
1 parent ddd2086 commit 7625958
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 41 deletions.
2 changes: 2 additions & 0 deletions src/main/java/fr/Alphart/BAT/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
77 changes: 44 additions & 33 deletions src/main/java/fr/Alphart/BAT/Modules/Ban/Ban.java
Original file line number Diff line number Diff line change
@@ -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<String> pendingPlayers = new HashSet<String>();

public Ban(){
config = new BanConfig();
Expand Down Expand Up @@ -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()){
Expand All @@ -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;
}

Expand Down Expand Up @@ -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();
Expand All @@ -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);
}
}
}
39 changes: 31 additions & 8 deletions src/main/java/fr/Alphart/BAT/Utils/Utils.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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());
}
}
}

0 comments on commit 7625958

Please sign in to comment.