Skip to content

Commit

Permalink
Attempt to implement paid unban command
Browse files Browse the repository at this point in the history
  • Loading branch information
BoomEaro committed Apr 29, 2024
1 parent 182455c commit e7f5dc1
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 6 deletions.
6 changes: 6 additions & 0 deletions bukkit/src/main/resources/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ messages:
noExists: '&c[player] is not abanned'
notOwn: '&c[player] was not abanned by you, unable to unban'

unbanpaid:
notify: '&6[player] has been paid unbanned by [actor]'
error:
noExists: '&c[player] is not banned'
notOwn: '&c[player] was not banned by you, unable to paid unban'

unbanall:
notify: '&6[player] will be unbanned by [actor]'

Expand Down
6 changes: 6 additions & 0 deletions bungee/src/main/resources/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ messages:
noExists: '&c[player] is not abanned'
notOwn: '&c[player] was not abanned by you, unable to unban'

unbanpaid:
notify: '&6[player] has been paid unbanned by [actor]'
error:
noExists: '&c[player] is not banned'
notOwn: '&c[player] was not banned by you, unable to paid unban'

unbanall:
notify: '&6[player] will be unbanned by [actor]'

Expand Down
5 changes: 5 additions & 0 deletions bungee/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ commands:
usage: "/unban <player> [reason]"
aliases: [ bmunban, pardon ]
permission: bm.command.unban
unbanpaid:
description: "unbanpaid a player"
usage: "/unbanpaid <player> [reason]"
aliases: [ bmunbanpaid, pardonpaid ]
permission: bm.command.unbanpaid
aunban:
description: "aunban a player"
usage: "/aunban <player> [reason]"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ public CommonCommand[] getCommands() {
new TempNameBanCommand(this),
new TempWarnCommand(this),
new UnbanCommand(this),
new UnbanPaidCommand(this),
new AUnbanCommand(this),
new UnbanIpCommand(this),
new UnbanIpRangeCommand(this),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,15 @@ public boolean onCommand(CommonSender sender, CommandParser parser) {
}
}

if (getPlugin().getPlayerBanStorage().isPaidUnbanned(player.getUUID())) {
if (!sender.hasPermission("bm.unbanpaid.prevent")) {
sender.sendMessage(Message.get("sender.error.exempt").set("player", playerName).toString());
return;
}
}

final PlayerBanData ban = new PlayerBanData(player, actor, parser.getReason().getMessage(), isSilent);
boolean created;

try {
created = getPlugin().getPlayerBanStorage().ban(ban);
} catch (SQLException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package me.confuser.banmanager.common.commands;

import me.confuser.banmanager.common.BanManagerPlugin;
import me.confuser.banmanager.common.data.PlayerBanData;
import me.confuser.banmanager.common.data.PlayerData;
import me.confuser.banmanager.common.util.Message;
import me.confuser.banmanager.common.util.parsers.UnbanCommandParser;

import java.sql.SQLException;
import java.util.UUID;

public class UnbanPaidCommand extends CommonCommand {

public UnbanPaidCommand(BanManagerPlugin plugin) {
super(plugin, "unbanpaid", true, UnbanCommandParser.class, 0);
}

@Override
public boolean onCommand(final CommonSender sender, CommandParser originalParser) {
final UnbanCommandParser parser = (UnbanCommandParser) originalParser;
final boolean isDelete = parser.isDelete();

if (isDelete && !sender.hasPermission(getPermission() + ".delete")) {
sender.sendMessage(Message.getString("sender.error.noPermission"));
return true;
}

if (parser.args.length < 1) {
return false;
}

// Check if UUID vs name
final String playerName = parser.args[0];
final boolean isUUID = playerName.length() > 16;
boolean isBanned;

if (isUUID) {
try {
isBanned = getPlugin().getPlayerBanStorage().isBanned(UUID.fromString(playerName));
} catch (IllegalArgumentException e) {
sender.sendMessage(Message.get("sender.error.notFound").set("player", playerName).toString());
return true;
}
} else {
isBanned = getPlugin().getPlayerBanStorage().isBanned(playerName);
}

if (!isBanned) {
Message message = Message.get("unbanpaid.error.noExists");
message.set("player", playerName);

sender.sendMessage(message.toString());
return true;
}

final String reason = parser.getReason().getMessage();

getPlugin().getScheduler().runAsync(() -> {
PlayerBanData ban;

if (isUUID) {
ban = getPlugin().getPlayerBanStorage().getBan(UUID.fromString(playerName));
} else {
ban = getPlugin().getPlayerBanStorage().getBan(playerName);
}

if (ban == null) {
sender.sendMessage(Message.get("sender.error.notFound").set("player", playerName).toString());
return;
}

final PlayerData actor = sender.getData();

//TODO refactor if async perm check is problem
if (!actor.getUUID().equals(ban.getActor().getUUID()) && !sender.hasPermission("bm.exempt.override.aban")
&& sender.hasPermission("bm.command.unbanpaid.own")) {
Message.get("unbanpaid.error.notOwn").set("player", ban.getPlayer().getName()).sendTo(sender);
return;
}

boolean unbanned;

try {
unbanned = getPlugin().getPlayerBanStorage().unban(ban, actor, reason, isDelete, true);
} catch (SQLException e) {
sender.sendMessage(Message.get("sender.error.exception").toString());
e.printStackTrace();
return;
}

if (!unbanned) {
return;
}

Message message = Message.get("unbanpaid.notify");
message
.set("player", ban.getPlayer().getName())
.set("playerId", ban.getPlayer().getUUID().toString())
.set("actor", actor.getName())
.set("id", ban.getId())
.set("reason", reason);

if (sender.isTrueNotConsole()) {
sender.sendMessage(message);
}

if (!sender.hasPermission("bm.notify.unbanpaid") || parser.isSilent()) {
message.sendTo(sender);
}

if (!parser.isSilent()) {
getPlugin().getServer().broadcast(message.toString(), "bm.notify.unbanpaid");
}
});

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import me.confuser.banmanager.common.ipaddr.IPAddress;
import me.confuser.banmanager.common.maxmind.db.model.CountryResponse;
import me.confuser.banmanager.common.ormlite.dao.CloseableIterator;
import me.confuser.banmanager.common.storage.PlayerBanStorage;
import me.confuser.banmanager.common.util.*;

import java.io.IOException;
Expand Down Expand Up @@ -271,6 +272,14 @@ public void banCheck(UUID id, String name, IPAddress address, CommonJoinHandler

handler.handlePlayerDeny(data.getPlayer(), message);
handleJoinDeny(data.getPlayer(), data.getActor(), data.getReason());
return;
}

PlayerBanStorage.PaidUnbanData paidUnbanData = plugin.getPlayerBanStorage().getPaidUnbanData(id);
if (paidUnbanData != null) {
if (paidUnbanData.getTimeAdded() == null) {
paidUnbanData.setTimeAdded(System.currentTimeMillis());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.confuser.banmanager.common.storage;

import lombok.Data;
import me.confuser.banmanager.common.BanManagerPlugin;
import me.confuser.banmanager.common.api.events.CommonEvent;
import me.confuser.banmanager.common.data.PlayerBanData;
Expand All @@ -22,15 +23,19 @@
import me.confuser.banmanager.common.util.UUIDUtils;

import java.sql.SQLException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class PlayerBanStorage extends BaseDaoImpl<PlayerBanData, Integer> {

private BanManagerPlugin plugin;
private ConcurrentHashMap<UUID, PlayerBanData> bans = new ConcurrentHashMap<>();
private final ConcurrentMap<UUID, PlayerBanData> bans = new ConcurrentHashMap<>();
private final ConcurrentMap<UUID, PaidUnbanData> paidUnbans = new ConcurrentHashMap<>();


public PlayerBanStorage(BanManagerPlugin plugin) throws SQLException {
super(plugin.getLocalConn(), (DatabaseTableConfig<PlayerBanData>) plugin.getConfig().getLocalDb()
Expand All @@ -44,7 +49,7 @@ public PlayerBanStorage(BanManagerPlugin plugin) throws SQLException {
} else {
try {
executeRawNoArgs("ALTER TABLE " + tableConfig.getTableName() + " ADD COLUMN `silent` TINYINT(1)");
} catch (SQLException e) {
} catch (SQLException ignored) {
}

try {
Expand All @@ -53,7 +58,7 @@ public PlayerBanStorage(BanManagerPlugin plugin) throws SQLException {
+ " CHANGE `updated` `updated` BIGINT UNSIGNED,"
+ " CHANGE `expires` `expires` BIGINT UNSIGNED"
);
} catch (SQLException e) {
} catch (SQLException ignored) {
}
}

Expand Down Expand Up @@ -148,7 +153,7 @@ private void loadAll() throws SQLException {
}
}

public ConcurrentHashMap<UUID, PlayerBanData> getBans() {
public ConcurrentMap<UUID, PlayerBanData> getBans() {
return bans;
}

Expand Down Expand Up @@ -220,20 +225,47 @@ public boolean unban(PlayerBanData ban, PlayerData actor, String reason) throws
}

public boolean unban(PlayerBanData ban, PlayerData actor, String reason, boolean delete) throws SQLException {
return unban(ban, actor, reason, delete, false);
}

public boolean unban(PlayerBanData ban, PlayerData actor, String reason, boolean delete, boolean paid) throws SQLException {
CommonEvent event = plugin.getServer().callEvent("PlayerUnbanEvent", ban, actor, reason);

if (event.isCancelled()) {
return false;
}

delete(ban);
bans.remove(ban.getPlayer().getUUID());
UUID uuid = ban.getPlayer().getUUID();
bans.remove(uuid);

if (paid) {
this.paidUnbans.put(uuid, new PaidUnbanData(uuid));
}

if (!delete) plugin.getPlayerBanRecordStorage().addRecord(ban, actor, reason);

return true;
}

public PaidUnbanData getPaidUnbanData(UUID uuid) {
return this.paidUnbans.get(uuid);
}

public boolean isPaidUnbanned(UUID uuid) {
PaidUnbanData paidUnbanData = this.paidUnbans.get(uuid);
if (paidUnbanData == null) {
return false;
}

Long time = paidUnbanData.getTimeAdded();
if (time == null) {
return false;
}

return System.currentTimeMillis() - time > Duration.ofMinutes(10).toMillis();
}

public CloseableIterator<PlayerBanData> findBans(long fromTime) throws SQLException {
if (fromTime == 0) {
return iterator();
Expand Down Expand Up @@ -301,4 +333,12 @@ public boolean isRecentlyBanned(PlayerData player, long cooldown) throws SQLExce
.ge("created", (System.currentTimeMillis() / 1000L) - cooldown)
.countOf() > 0;
}

@Data
public static class PaidUnbanData {
private final UUID uuid;

private Long timeAdded;

}
}
6 changes: 6 additions & 0 deletions common/src/main/resources/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ messages:
noExists: '&c[player] is not abanned'
notOwn: '&c[player] was not abanned by you, unable to unban'

unbanpaid:
notify: '&6[player] has been paid unbanned by [actor]'
error:
noExists: '&c[player] is not banned'
notOwn: '&c[player] was not banned by you, unable to paid unban'

unbanall:
notify: '&6[player] will be unbanned by [actor]'

Expand Down
5 changes: 5 additions & 0 deletions common/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ commands:
usage: "/unban <player> [reason]"
aliases: [ bmunban, pardon ]
permission: bm.command.unban
unbanpaid:
description: "unbanpaid a player"
usage: "/unbanpaid <player> [reason]"
aliases: [ bmunbanpaid, pardonpaid ]
permission: bm.command.unbanpaid
aunban:
description: "aunban a player"
usage: "/aunban <player> [reason]"
Expand Down

0 comments on commit e7f5dc1

Please sign in to comment.