Skip to content

Commit 776d44c

Browse files
authored
fixed banip logic and unbanip logic
1 parent efe7f98 commit 776d44c

File tree

7 files changed

+267
-189
lines changed

7 files changed

+267
-189
lines changed

src/main/java/me/wethink/weguardian/commands/PunishmentCommands.java

Lines changed: 96 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@
1717
import java.util.Collection;
1818
import java.util.UUID;
1919

20-
2120
@CommandAlias("weguardian|wg")
2221
public class PunishmentCommands extends BaseCommand {
2322

24-
2523
private static final String PREFIX = "&8[&cWeGuardian&8] ";
2624
private static final String MSG_PLAYER_NOT_FOUND = "&cPlayer not found!";
2725
private static final String MSG_PLAYER_NOT_ONLINE = "&cPlayer is not online!";
@@ -36,7 +34,6 @@ public PunishmentCommands(WeGuardian plugin) {
3634
this.plugin = plugin;
3735
}
3836

39-
4037
@CommandAlias("ban")
4138
@CommandPermission("weguardian.ban")
4239
@CommandCompletion("@players")
@@ -86,8 +83,6 @@ public void onUnban(CommandSender sender, String targetName) {
8683
});
8784
}
8885

89-
90-
9186
@CommandAlias("mute")
9287
@CommandPermission("weguardian.mute")
9388
@CommandCompletion("@players")
@@ -143,7 +138,6 @@ public void onUnmute(CommandSender sender, String targetName) {
143138
});
144139
}
145140

146-
147141
@CommandAlias("banip")
148142
@CommandPermission("weguardian.banip")
149143
@CommandCompletion("@players")
@@ -173,22 +167,38 @@ public void onTempBanIp(CommandSender sender, String targetName, String duration
173167
@Description("Remove an IP ban for a player (auto-resolves IP)")
174168
@Syntax("<player>")
175169
public void onUnbanIp(CommandSender sender, String targetName) {
176-
Player target = Bukkit.getPlayer(targetName);
177-
if (target == null) {
178-
sender.sendMessage(MessageUtil.toComponent(MSG_PLAYER_MUST_BE_ONLINE_IP));
179-
return;
180-
}
181-
182-
String ipAddress = getPlayerIp(target);
183-
if (ipAddress == null) {
184-
sender.sendMessage(MessageUtil.toComponent(MSG_COULD_NOT_RESOLVE_IP));
185-
return;
186-
}
187-
170+
Player onlineTarget = Bukkit.getPlayer(targetName);
188171
UUID staffUUID = sender instanceof Player p ? p.getUniqueId() : null;
189172
String staffName = sender.getName();
190173

191-
plugin.getPunishmentManager().unbanIp(ipAddress, staffUUID, staffName, "Unbanned")
174+
if (onlineTarget != null) {
175+
String ipAddress = getPlayerIp(onlineTarget);
176+
if (ipAddress == null) {
177+
sender.sendMessage(MessageUtil.toComponent(MSG_COULD_NOT_RESOLVE_IP));
178+
return;
179+
}
180+
executeUnbanIp(sender, onlineTarget.getUniqueId(), targetName, ipAddress, staffUUID, staffName);
181+
} else {
182+
OfflinePlayer offlineTarget = Bukkit.getOfflinePlayer(targetName);
183+
if (!offlineTarget.hasPlayedBefore()) {
184+
sender.sendMessage(MessageUtil.toComponent(MSG_PLAYER_NOT_FOUND));
185+
return;
186+
}
187+
188+
plugin.getPunishmentDAO().getPlayerIp(offlineTarget.getUniqueId()).thenAccept(optionalIp -> {
189+
if (optionalIp.isEmpty()) {
190+
sender.sendMessage(MessageUtil.toComponent("&cNo stored IP found for " + targetName + "!"));
191+
return;
192+
}
193+
executeUnbanIp(sender, offlineTarget.getUniqueId(), targetName, optionalIp.get(), staffUUID, staffName);
194+
});
195+
}
196+
}
197+
198+
private void executeUnbanIp(CommandSender sender, UUID targetUUID, String targetName, String ipAddress,
199+
UUID staffUUID,
200+
String staffName) {
201+
plugin.getPunishmentManager().unbanIp(targetUUID, ipAddress, staffUUID, staffName, "Unbanned")
192202
.thenAccept(success -> {
193203
if (success) {
194204
broadcastStaff(
@@ -200,8 +210,6 @@ public void onUnbanIp(CommandSender sender, String targetName) {
200210
});
201211
}
202212

203-
204-
205213
@CommandAlias("muteip")
206214
@CommandPermission("weguardian.muteip")
207215
@CommandCompletion("@players")
@@ -231,22 +239,39 @@ public void onTempMuteIp(CommandSender sender, String targetName, String duratio
231239
@Description("Remove an IP mute for a player (auto-resolves IP)")
232240
@Syntax("<player>")
233241
public void onUnmuteIp(CommandSender sender, String targetName) {
234-
Player target = Bukkit.getPlayer(targetName);
235-
if (target == null) {
236-
sender.sendMessage(MessageUtil.toComponent(MSG_PLAYER_MUST_BE_ONLINE_IP));
237-
return;
238-
}
239-
240-
String ipAddress = getPlayerIp(target);
241-
if (ipAddress == null) {
242-
sender.sendMessage(MessageUtil.toComponent(MSG_COULD_NOT_RESOLVE_IP));
243-
return;
244-
}
245-
242+
Player onlineTarget = Bukkit.getPlayer(targetName);
246243
UUID staffUUID = sender instanceof Player p ? p.getUniqueId() : null;
247244
String staffName = sender.getName();
248245

249-
plugin.getPunishmentManager().unmuteIp(ipAddress, staffUUID, staffName, "Unmuted")
246+
if (onlineTarget != null) {
247+
String ipAddress = getPlayerIp(onlineTarget);
248+
if (ipAddress == null) {
249+
sender.sendMessage(MessageUtil.toComponent(MSG_COULD_NOT_RESOLVE_IP));
250+
return;
251+
}
252+
executeUnmuteIp(sender, onlineTarget.getUniqueId(), targetName, ipAddress, staffUUID, staffName);
253+
} else {
254+
OfflinePlayer offlineTarget = Bukkit.getOfflinePlayer(targetName);
255+
if (!offlineTarget.hasPlayedBefore()) {
256+
sender.sendMessage(MessageUtil.toComponent(MSG_PLAYER_NOT_FOUND));
257+
return;
258+
}
259+
260+
plugin.getPunishmentDAO().getPlayerIp(offlineTarget.getUniqueId()).thenAccept(optionalIp -> {
261+
if (optionalIp.isEmpty()) {
262+
sender.sendMessage(MessageUtil.toComponent("&cNo stored IP found for " + targetName + "!"));
263+
return;
264+
}
265+
executeUnmuteIp(sender, offlineTarget.getUniqueId(), targetName, optionalIp.get(), staffUUID,
266+
staffName);
267+
});
268+
}
269+
}
270+
271+
private void executeUnmuteIp(CommandSender sender, UUID targetUUID, String targetName, String ipAddress,
272+
UUID staffUUID,
273+
String staffName) {
274+
plugin.getPunishmentManager().unmuteIp(targetUUID, ipAddress, staffUUID, staffName, "Unmuted")
250275
.thenAccept(success -> {
251276
if (success) {
252277
broadcastStaff(
@@ -258,8 +283,6 @@ public void onUnmuteIp(CommandSender sender, String targetName) {
258283
});
259284
}
260285

261-
262-
263286
@CommandAlias("kick")
264287
@CommandPermission("weguardian.kick")
265288
@CommandCompletion("@players")
@@ -293,8 +316,6 @@ public void onKick(CommandSender sender, String targetName, @Optional String rea
293316
});
294317
}
295318

296-
297-
298319
@CommandAlias("punish")
299320
@CommandPermission("weguardian.punish")
300321
@CommandCompletion("@players")
@@ -325,7 +346,6 @@ public void onHistory(Player sender, String targetName) {
325346
HistoryGUI.openAsync(plugin, sender, target);
326347
}
327348

328-
329349
@Subcommand("reload")
330350
@CommandPermission("weguardian.admin")
331351
@Description("Reload the plugin configuration")
@@ -377,8 +397,6 @@ public void onHelp(CommandSender sender) {
377397
}
378398
}
379399

380-
381-
382400
private void executePunishment(CommandSender sender, String targetName, PunishmentType type,
383401
long durationMs, String reason) {
384402
OfflinePlayer target = Bukkit.getOfflinePlayer(targetName);
@@ -427,7 +445,6 @@ private void executePunishment(CommandSender sender, String targetName, Punishme
427445
});
428446
}
429447

430-
431448
private void broadcastStaff(String message) {
432449
String formattedMessage = PREFIX + message;
433450
Collection<? extends Player> players = Bukkit.getOnlinePlayers();
@@ -440,40 +457,58 @@ private void broadcastStaff(String message) {
440457
Bukkit.getConsoleSender().sendMessage(MessageUtil.toComponent(formattedMessage));
441458
}
442459

443-
444460
private void executeIpPunishment(CommandSender sender, String targetName, PunishmentType type,
445461
long durationMs, String reason) {
446-
Player target = Bukkit.getPlayer(targetName);
447-
if (target == null) {
448-
sender.sendMessage(MessageUtil.toComponent(MSG_PLAYER_MUST_BE_ONLINE_IP));
449-
return;
450-
}
462+
Player onlineTarget = Bukkit.getPlayer(targetName);
451463

452-
String bypassPerm = plugin.getConfig().getString("bypass.permission", "weguardian.bypass");
453-
if (target.hasPermission(bypassPerm)) {
454-
sender.sendMessage(MessageUtil.toComponent(MSG_PLAYER_BYPASS));
455-
return;
456-
}
464+
if (onlineTarget != null) {
465+
String bypassPerm = plugin.getConfig().getString("bypass.permission", "weguardian.bypass");
466+
if (onlineTarget.hasPermission(bypassPerm)) {
467+
sender.sendMessage(MessageUtil.toComponent(MSG_PLAYER_BYPASS));
468+
return;
469+
}
457470

458-
String ipAddress = getPlayerIp(target);
459-
if (ipAddress == null) {
460-
sender.sendMessage(MessageUtil.toComponent(MSG_COULD_NOT_RESOLVE_IP));
461-
return;
471+
String ipAddress = getPlayerIp(onlineTarget);
472+
if (ipAddress == null) {
473+
sender.sendMessage(MessageUtil.toComponent(MSG_COULD_NOT_RESOLVE_IP));
474+
return;
475+
}
476+
477+
applyIpPunishment(sender, onlineTarget.getUniqueId(), targetName, ipAddress, type, durationMs, reason);
478+
} else {
479+
OfflinePlayer offlineTarget = Bukkit.getOfflinePlayer(targetName);
480+
if (!offlineTarget.hasPlayedBefore()) {
481+
sender.sendMessage(MessageUtil.toComponent(MSG_PLAYER_NOT_FOUND));
482+
return;
483+
}
484+
485+
plugin.getPunishmentDAO().getPlayerIp(offlineTarget.getUniqueId()).thenAccept(optionalIp -> {
486+
if (optionalIp.isEmpty()) {
487+
sender.sendMessage(MessageUtil.toComponent("&cNo stored IP found for " + targetName + "!"));
488+
return;
489+
}
490+
491+
String ipAddress = optionalIp.get();
492+
applyIpPunishment(sender, offlineTarget.getUniqueId(), targetName, ipAddress, type, durationMs, reason);
493+
});
462494
}
495+
}
463496

497+
private void applyIpPunishment(CommandSender sender, UUID targetUUID, String targetName, String ipAddress,
498+
PunishmentType type, long durationMs, String reason) {
464499
UUID staffUUID = sender instanceof Player p ? p.getUniqueId() : null;
465500
String staffName = sender.getName();
466501
String finalReason = reason != null ? reason : "No reason specified";
467502

468503
var future = switch (type) {
469504
case BANIP -> plugin.getPunishmentManager().banIp(
470-
target.getUniqueId(), target.getName(), ipAddress, staffUUID, staffName, finalReason);
505+
targetUUID, targetName, ipAddress, staffUUID, staffName, finalReason);
471506
case TEMPBANIP -> plugin.getPunishmentManager().tempbanIp(
472-
target.getUniqueId(), target.getName(), ipAddress, staffUUID, staffName, durationMs, finalReason);
507+
targetUUID, targetName, ipAddress, staffUUID, staffName, durationMs, finalReason);
473508
case MUTEIP -> plugin.getPunishmentManager().muteIp(
474-
target.getUniqueId(), target.getName(), ipAddress, staffUUID, staffName, finalReason);
509+
targetUUID, targetName, ipAddress, staffUUID, staffName, finalReason);
475510
case TEMPMUTEIP -> plugin.getPunishmentManager().tempmuteIp(
476-
target.getUniqueId(), target.getName(), ipAddress, staffUUID, staffName, durationMs, finalReason);
511+
targetUUID, targetName, ipAddress, staffUUID, staffName, durationMs, finalReason);
477512
default -> null;
478513
};
479514

@@ -494,7 +529,6 @@ private void executeIpPunishment(CommandSender sender, String targetName, Punish
494529
});
495530
}
496531

497-
498532
private static String getPlayerIp(Player player) {
499533
InetSocketAddress address = player.getAddress();
500534
return address != null ? address.getAddress().getHostAddress() : null;

src/main/java/me/wethink/weguardian/database/DatabaseManager.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import java.util.concurrent.CompletableFuture;
1313
import java.util.logging.Level;
1414

15-
1615
public class DatabaseManager {
1716

1817
private final WeGuardian plugin;
@@ -23,7 +22,6 @@ public DatabaseManager(WeGuardian plugin) {
2322
this.plugin = plugin;
2423
}
2524

26-
2725
public void initialize() {
2826
databaseType = plugin.getConfig().getString("database.type", "sqlite").toLowerCase();
2927

@@ -51,7 +49,6 @@ public void initialize() {
5149
}
5250
}
5351

54-
5552
private HikariConfig initializeSQLite() {
5653
String dbFileName = plugin.getConfig().getString("database.sqlite.file", "punishments.db");
5754
File dbFile = new File(plugin.getDataFolder(), dbFileName);
@@ -80,7 +77,6 @@ private HikariConfig initializeSQLite() {
8077
return config;
8178
}
8279

83-
8480
private HikariConfig initializeMySQL() {
8581
String host = plugin.getConfig().getString("database.mysql.host", "localhost");
8682
int port = plugin.getConfig().getInt("database.mysql.port", 3306);
@@ -126,7 +122,6 @@ private HikariConfig initializeMySQL() {
126122
return config;
127123
}
128124

129-
130125
private void initializeTables() {
131126
String autoIncrement = databaseType.equals("mysql") ? "AUTO_INCREMENT" : "AUTOINCREMENT";
132127
String textType = databaseType.equals("mysql") ? "VARCHAR(255)" : "TEXT";
@@ -189,26 +184,34 @@ ON punishments(target_ip) WHERE target_ip IS NOT NULL
189184
""";
190185
}
191186

187+
String createPlayerIpsTable = String.format("""
188+
CREATE TABLE IF NOT EXISTS player_ips (
189+
uuid %s PRIMARY KEY,
190+
name %s NOT NULL,
191+
ip_address %s NOT NULL,
192+
last_seen %s NOT NULL
193+
)
194+
""", textType, textType, textType, bigintType);
195+
192196
executeAsync(createPunishmentsTable)
193197
.thenCompose(v -> executeAsync(createIndexTargetUuid))
194198
.thenCompose(v -> executeAsync(createIndexActive))
195199
.thenCompose(v -> executeAsync(createIndexExpires))
196200
.thenCompose(v -> executeAsync(createIndexTargetIp))
201+
.thenCompose(v -> executeAsync(createPlayerIpsTable))
197202
.exceptionally(e -> {
198203
plugin.getLogger().log(Level.SEVERE, "Failed to initialize database tables", e);
199204
return null;
200205
});
201206
}
202207

203-
204208
public Connection getConnection() throws SQLException {
205209
if (dataSource == null) {
206210
throw new SQLException("Database not initialized");
207211
}
208212
return dataSource.getConnection();
209213
}
210214

211-
212215
public CompletableFuture<Void> executeAsync(String sql) {
213216
return CompletableFuture.runAsync(() -> {
214217
try (Connection conn = getConnection();
@@ -220,17 +223,14 @@ public CompletableFuture<Void> executeAsync(String sql) {
220223
});
221224
}
222225

223-
224226
public boolean isConnected() {
225227
return dataSource != null && !dataSource.isClosed();
226228
}
227229

228-
229230
public String getDatabaseType() {
230231
return databaseType;
231232
}
232233

233-
234234
public void shutdown() {
235235
if (dataSource != null && !dataSource.isClosed()) {
236236
dataSource.close();

0 commit comments

Comments
 (0)