Implement ban functionality

- Added ban module
- Added Ban and Unban commands, supporting banning and unbanning players through various command parameters.
- Added timestamp formatting method in Utils utility class for displaying ban expiration times
- Introduced PlayerErrorCode enum defining various error codes including ErrBan
- Added dual ban checking for both IP and user during player login
- Optimized login failure responses to provide specific error reasons and parameters
This commit is contained in:
HongchengQ
2025-12-10 16:33:41 -08:00
committed by Melledy
parent 6483e8a5a7
commit 3a6387c2bd
11 changed files with 750 additions and 9 deletions
@@ -0,0 +1,115 @@
package emu.nebula.command.commands;
import emu.nebula.Nebula;
import emu.nebula.command.Command;
import emu.nebula.command.CommandArgs;
import emu.nebula.command.CommandHandler;
import emu.nebula.game.player.Player;
import emu.nebula.util.Utils;
import java.util.Locale;
@Command(label = "ban",
permission = "admin.ban",
desc = """
!ban {all | ip | uid} [player uid | ip] (end timestamp) (reason) - Ban a player\
- all mode bans both the player object and their IP address by UID, so the next parameter should be UID instead of IP
- ip mode can only ban IP addresses, so the next parameter should be an IP
- uid mode can only ban UIDs, so the next parameter should be a UID
- If you don't fill in the end timestamp, it will be permanently banned by default\
""")
public class BanCommand implements CommandHandler {
@Override
public String execute(CommandArgs args) {
if (args.size() < 2) {
return "Invalid amount of args";
}
int bannedUid = 0;
long banEndTime = 0;
String bannedIp = null;
String reason = null;
String mode = args.get(0).toLowerCase(Locale.ROOT);
switch (args.size()) {
case 4:
reason = args.get(3);
case 3: {
try {
banEndTime = Long.parseLong(args.get(2));
} catch (NumberFormatException ignored) {
return "Unable to parse timestamp.";
}
}
case 2: {
if (mode.equals("ip")) {
bannedIp = args.get(1);
} else {
try {
bannedUid = Integer.parseInt(args.get(1));
} catch (NumberFormatException ignored) {
return "Unable to parse uid.";
}
}
}
case 1: {
if (!mode.equals("all") && !mode.equals("uid") && !mode.equals("ip"))
return "Unable to parse mode.";
}
default: break;
}
if (banEndTime != 0 && banEndTime < System.currentTimeMillis()) {
return "Failed, the end timestamp must be greater than the current time";
}
var banModule = Nebula.getGameContext().getBanModule();
Player player;
if (!mode.equals("ip")) {
player = Nebula.getGameContext().getPlayerModule().getPlayer(bannedUid);
if (player == null) {
return "Failed, player not found.";
}
}
switch (mode) {
case "all" -> {
banModule.banPlayer(
bannedUid,
banEndTime,
reason,
true,
args.getSender() != null ? String.valueOf(args.getSender().getUid()) : "Console");
return "Banned player all mode " + bannedUid + " until " + Utils.formatTimestamp(banEndTime) +
(reason != null ? " (" + reason + ")" : "");
}
case "ip" -> {
banModule.banIp(
bannedIp,
banEndTime,
reason,
args.getSender() != null ? String.valueOf(args.getSender().getUid()) : "Console");
return "Banned ip " + bannedIp + " until " + Utils.formatTimestamp(banEndTime) +
(reason != null ? " (" + reason + ")" : "");
}
case "uid" -> {
banModule.banPlayer(
bannedUid,
banEndTime,
reason,
false,
args.getSender() != null ? String.valueOf(args.getSender().getUid()) : "Console");
return "Banned player " + bannedUid + " until " + Utils.formatTimestamp(banEndTime) +
(reason != null ? " (" + reason + ")" : "");
}
default -> {
return "Ban sub command not found";
}
}
}
}
@@ -0,0 +1,81 @@
package emu.nebula.command.commands;
import emu.nebula.Nebula;
import emu.nebula.command.Command;
import emu.nebula.command.CommandArgs;
import emu.nebula.command.CommandHandler;
import emu.nebula.game.ban.BanInfo;
import emu.nebula.game.player.Player;
import java.util.Locale;
@Command(label = "unban", permission = "admin.ban", desc = """
!unban {all | ip | uid} [player uid | ip]\
- all mode unbans both the player object and their IP address by UID, so the next parameter should be UID instead of IP
- ip mode can only unban IP addresses, so the next parameter should be an IP
- uid mode can only unban UIDs, so the next parameter should be a UID\
""")
public class UnbanCommand implements CommandHandler {
@Override
public String execute(CommandArgs args) {
if (args.size() < 2) {
return "Invalid amount of args";
}
int unbannedUid = 0;
String unbannedIp = null;
String mode = args.get(0).toLowerCase(Locale.ROOT);
if (!mode.equals("all") && !mode.equals("uid") && !mode.equals("ip"))
return "Unable to parse mode.";
if (mode.equals("ip")) {
unbannedIp = args.get(1);
} else {
try {
unbannedUid = Integer.parseInt(args.get(1));
} catch (NumberFormatException ignored) {
return "Unable to parse uid.";
}
}
var banModule = Nebula.getGameContext().getBanModule();
Player player = null;
if (!mode.equals("ip")) {
player = Nebula.getGameContext().getPlayerModule().getPlayer(unbannedUid);
if (player == null) {
return "Failed, player not found.";
}
}
switch (mode) {
case "all" -> {
BanInfo banInfo = banModule.getPlayerBanInfo(player.getUid());
banModule.unbanPlayer(player.getUid());
if (banInfo != null) {
unbannedIp = banInfo.getIpAddress();
if (unbannedIp != null)
banModule.unbanIp(unbannedIp);
}
return "Unban a player all mode " + unbannedUid;
} case "uid" -> {
banModule.unbanPlayer(player.getUid());
return "Unban a player " + unbannedUid;
} case "ip" -> {
banModule.unbanIp(unbannedIp);
return "Unban a ip " + unbannedIp;
}
default -> {
// Fallback
return "Unban sub command not found";
}
}
}
}