Refactor BanService#ban

This commit is contained in:
P0nk
2024-10-02 06:54:55 +02:00
parent 5d81e05458
commit e38344ceae

View File

@@ -91,27 +91,41 @@ public class BanService {
private void ban(Client c, String victimName, Duration duration, byte reason, String description) { private void ban(Client c, String victimName, Duration duration, byte reason, String description) {
Character victim = c.getChannelServer().getPlayerStorage().getCharacterByName(victimName); Character victim = c.getChannelServer().getPlayerStorage().getCharacterByName(victimName);
boolean success;
if (victim == null) { if (victim == null) {
Optional<Integer> foundAccountId = accountService.getAccountIdByChrName(victimName); success = banOfflineChr(victimName, duration, reason, description);
if (foundAccountId.isEmpty()) { } else {
success = banOnlineChr(c, victim, duration, reason, description);
}
if (!success) {
c.sendPacket(PacketCreator.getGMEffect(6, (byte) 1)); c.sendPacket(PacketCreator.getGMEffect(6, (byte) 1));
return; return;
} }
c.sendPacket(PacketCreator.getGMEffect(4, (byte) 0));
Server.getInstance().broadcastMessage(c.getWorld(), PacketCreator.serverNotice(6, "%s has been banned.".formatted(victimName)));
}
private boolean banOfflineChr(String victimName, Duration duration, byte reason, String description) {
Optional<Integer> foundAccountId = accountService.getAccountIdByChrName(victimName);
if (foundAccountId.isEmpty()) {
return false;
}
saveBan(foundAccountId.get(), duration, reason, description); saveBan(foundAccountId.get(), duration, reason, description);
} else { return true;
}
private boolean banOnlineChr(Client c, Character victim, Duration duration, byte reason, String description) {
victim.setBanned(); victim.setBanned();
String readableName = Character.makeMapleReadable(victimName); String readableName = Character.makeMapleReadable(victim.getName());
String ip = victim.getClient().getRemoteAddress(); String ip = victim.getClient().getRemoteAddress();
String enrichedDescription = "[%s] %s (IP: %s)".formatted(description, readableName, ip); String enrichedDescription = "[%s] %s (IP: %s)".formatted(description, readableName, ip);
saveBan(victim.getAccountID(), duration, reason, enrichedDescription); saveBan(victim.getAccountID(), duration, reason, enrichedDescription);
victim.sendPacket(PacketCreator.sendPolice("You have been banned by %s.".formatted(c.getPlayer().getName()))); victim.sendPacket(PacketCreator.sendPolice("You have been banned by %s.".formatted(c.getPlayer().getName())));
TimerManager.getInstance().schedule(() -> transitionService.disconnect(c, false), TimerManager.getInstance().schedule(() -> transitionService.disconnect(c, false),
TimeUnit.SECONDS.toMillis(5)); TimeUnit.SECONDS.toMillis(5));
} return true;
c.sendPacket(PacketCreator.getGMEffect(4, (byte) 0));
Server.getInstance().broadcastMessage(c.getWorld(), PacketCreator.serverNotice(6, "%s has been banned.".formatted(victimName)));
} }
private void saveBan(int accountId, Duration duration, byte reason, String description) { private void saveBan(int accountId, Duration duration, byte reason, String description) {