feat(ban): Implementing ban.

Rough code, not based on permission.
This commit is contained in:
Yazawazi 2022-06-18 05:01:38 +09:00 committed by Melledy
parent 557298f847
commit aba4080b0e
6 changed files with 252 additions and 4 deletions

View File

@ -0,0 +1,87 @@
package emu.grasscutter.command.commands;
import java.util.List;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.game.Account;
import emu.grasscutter.game.player.Player;
import static emu.grasscutter.utils.Language.translate;
@Command(
label = "ban",
usage = "ban <player> [time] [reason]",
description = "commands.ban.description",
targetRequirement = Command.TargetRequirement.NONE
)
public final class BanCommand implements CommandHandler {
private boolean banAccount(int uid, int time, String reason) {
Player player = Grasscutter.getGameServer().getPlayerByUid(uid, true);
if (player == null) {
return false;
}
Account account = player.getAccount();
if (account == null) {
account = DatabaseHelper.getAccountByPlayerId(uid);
}
account.setBanReason(reason);
account.setBanEndTime(time);
account.setBanStartTime((int) System.currentTimeMillis() / 1000);
account.setBanned(true);
account.save();
Player banUser = Grasscutter.getGameServer().getPlayerByUid(uid);
if (banUser != null) {
banUser.getSession().close();
}
return true;
}
@Override
public void execute(Player sender, Player targetPlayer, List<String> args) {
if (args.size() < 1) {
CommandHandler.sendMessage(sender, translate(sender, "commands.ban.command_usage"));
return;
}
int uid = 0;
int time = 2051190000;
String reason = "Reason not specified.";
if (args.size() >= 1) {
try {
uid = Integer.parseInt(args.get(0));
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(sender, translate(sender, "commands.ban.invalid_player_id"));
return;
}
}
if (args.size() >= 2) {
try {
time = Integer.parseInt(args.get(1));
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(sender, translate(sender, "commands.ban.invalid_time"));
return;
}
}
if (args.size() >= 3) {
reason = args.get(2);
}
if (banAccount(uid, time, reason)) {
CommandHandler.sendMessage(sender, translate(sender, "commands.ban.success"));
} else {
CommandHandler.sendMessage(sender, translate(sender, "commands.ban.failure"));
}
}
}

View File

@ -0,0 +1,66 @@
package emu.grasscutter.command.commands;
import java.util.List;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.game.Account;
import emu.grasscutter.game.player.Player;
import static emu.grasscutter.utils.Language.translate;
@Command(
label = "unban",
usage = "unban <player>",
description = "commands.unban.description",
targetRequirement = Command.TargetRequirement.NONE
)
public final class UnBanCommand implements CommandHandler {
private boolean unBanAccount(int uid) {
Player player = Grasscutter.getGameServer().getPlayerByUid(uid, true);
if (player == null) {
return false;
}
Account account = player.getAccount();
if (account == null) {
account = DatabaseHelper.getAccountByPlayerId(uid);
}
account.setBanReason(null);
account.setBanEndTime(0);
account.setBanStartTime(0);
account.setBanned(false);
account.save();
return true;
}
@Override
public void execute(Player sender, Player targetPlayer, List<String> args) {
if (args.size() < 1) {
CommandHandler.sendMessage(sender, translate(sender, "commands.unban.command_usage"));
return;
}
int uid = 0;
try {
uid = Integer.parseInt(args.get(0));
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(sender, translate(sender, "commands.unban.invalid_player_id"));
return;
}
if (unBanAccount(uid)) {
CommandHandler.sendMessage(sender, translate(sender, "commands.unban.success"));
} else {
CommandHandler.sendMessage(sender, translate(sender, "commands.unban.failure"));
}
}
}

View File

@ -29,6 +29,11 @@ public class Account {
private List<String> permissions;
private Locale locale;
private String banReason;
private int banEndTime;
private int banStartTime;
private boolean isBanned;
@Deprecated
public Account() {
this.permissions = new ArrayList<>();
@ -105,6 +110,46 @@ public class Account {
this.locale = locale;
}
public String getBanReason() {
return banReason;
}
public void setBanReason(String banReason) {
this.banReason = banReason;
}
public int getBanEndTime() {
return banEndTime;
}
public void setBanEndTime(int banEndTime) {
this.banEndTime = banEndTime;
}
public int getBanStartTime() {
return banStartTime;
}
public void setBanStartTime(int banStartTime) {
this.banStartTime = banStartTime;
}
public boolean isBanned() {
if (banEndTime > 0 && banEndTime < System.currentTimeMillis() / 1000) {
this.isBanned = false;
this.banEndTime = 0;
this.banStartTime = 0;
this.banReason = null;
save();
}
return isBanned;
}
public void setBanned(boolean isBanned) {
this.isBanned = isBanned;
}
/**
* The collection of a player's permissions.
*/

View File

@ -1,5 +1,7 @@
package emu.grasscutter.server.packet.send;
import java.util.Base64;
import com.google.protobuf.ByteString;
import emu.grasscutter.net.packet.BasePacket;
@ -15,6 +17,23 @@ public class PacketGetPlayerTokenRsp extends BasePacket {
this.setUseDispatchKey(true);
if (session.getPlayer().getAccount().isBanned()) {
byte[] bin = Base64.getDecoder().decode("5LiN54ixIEdDIOWNgeW5tOS6hu+8jOWNgeW5tOmHjOeUqOi/h+eahOavj+S4gOS4qiBQUyDpg73lg48gR0PjgII=");
GetPlayerTokenRsp p = GetPlayerTokenRsp.newBuilder()
.setUid(session.getPlayer().getUid())
.setIsProficientPlayer(session.getPlayer().getAvatars().getAvatarCount() > 0)
.setRetcode(21)
.setExtraBinData(ByteString.copyFrom(bin))
.setMsg("FORBID_CHEATING_PLUGINS")
.setBlackUidEndTime(session.getPlayer().getAccount().getBanEndTime())
.setRegPlatform(3)
.setCountryCode("US")
.setClientIpStr(session.getAddress().getAddress().getHostAddress())
.build();
this.setData(p.toByteArray());
} else {
GetPlayerTokenRsp p = GetPlayerTokenRsp.newBuilder()
.setUid(session.getPlayer().getUid())
.setToken(session.getAccount().getToken())
@ -33,3 +52,4 @@ public class PacketGetPlayerTokenRsp extends BasePacket {
this.setData(p.toByteArray());
}
}
}

View File

@ -391,6 +391,21 @@
"success": "Changed climate type to %s with weather type %s.",
"invalid_id": "Invalid ID.",
"description": "Changes the weather"
},
"ban": {
"description": "Ban a player",
"success": "Successful.",
"failure": "Failed, player not found.",
"invalid_time": "Unable to parse timestamp.",
"invalid_player_id": "Unable to parse player ID.",
"command_usage": "Usage: ban <playerId> [timestamp] [reason]"
},
"unban": {
"description": "Unban a player",
"success": "Successful.",
"failure": "Failed, player not found.",
"invalid_player_id": "Unable to parse player ID.",
"command_usage": "Usage: unban <playerId>"
}
},
"gacha": {

View File

@ -391,6 +391,21 @@
"success": "已更改气候类型为 %s天气类型为 %s。",
"invalid_id": "无效的ID。",
"description": "更改天气"
},
"ban": {
"description": "封禁玩家",
"success": "封禁玩家成功。",
"failure": "封禁玩家失败,因为无法获取到其账户。",
"invalid_time": "无法解析时间戳。",
"invalid_player_id": "无法解析玩家 ID。",
"command_usage": "用法ban <玩家ID> [时间] [原因]"
},
"unban": {
"description": "取消玩家的封禁",
"success": "取消玩家的封禁成功。",
"failure": "取消玩家的封禁失败,因为无法获取到其账户。",
"invalid_player_id": "无法解析玩家 ID。",
"command_usage": "用法unban <玩家ID>"
}
},
"gacha": {