Skip to content

Commit 2308d98

Browse files
authored
Added /fldelete <name> to delete profiles from storage (#1283)
* Added `/fldelete <name>` to delete profiles from storage by name * Implemented requested changes for pull request #1283 * Update delete command's perm default
1 parent 063b6a9 commit 2308d98

File tree

5 files changed

+128
-0
lines changed

5 files changed

+128
-0
lines changed

bukkit/src/main/java/com/github/games647/fastlogin/bukkit/FastLoginBukkit.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.comphenix.protocol.ProtocolLibrary;
2929
import com.github.games647.fastlogin.bukkit.command.CrackedCommand;
3030
import com.github.games647.fastlogin.bukkit.command.PremiumCommand;
31+
import com.github.games647.fastlogin.bukkit.command.DeleteCommand;
3132
import com.github.games647.fastlogin.bukkit.listener.ConnectionListener;
3233
import com.github.games647.fastlogin.bukkit.listener.PaperCacheListener;
3334
import com.github.games647.fastlogin.bukkit.listener.protocollib.ProtocolLibListener;
@@ -155,6 +156,7 @@ private void registerCommands() {
155156
//register commands using a unique name
156157
Optional.ofNullable(getCommand("premium")).ifPresent(c -> c.setExecutor(new PremiumCommand(this)));
157158
Optional.ofNullable(getCommand("cracked")).ifPresent(c -> c.setExecutor(new CrackedCommand(this)));
159+
Optional.ofNullable(getCommand("fldelete")).ifPresent(c -> c.setExecutor(new DeleteCommand(this)));
158160
}
159161

160162
private boolean initializeFloodgate() {
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* SPDX-License-Identifier: MIT
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2015-2024 games647 and contributors
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in all
16+
* copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
* SOFTWARE.
25+
*/
26+
package com.github.games647.fastlogin.bukkit.command;
27+
28+
import java.util.ArrayList;
29+
import java.util.List;
30+
31+
import org.bukkit.Bukkit;
32+
import org.bukkit.command.Command;
33+
import org.bukkit.command.CommandSender;
34+
import org.bukkit.command.ConsoleCommandSender;
35+
import org.bukkit.command.TabExecutor;
36+
import org.bukkit.entity.Player;
37+
38+
import com.github.games647.fastlogin.bukkit.FastLoginBukkit;
39+
40+
public class DeleteCommand implements TabExecutor {
41+
private final FastLoginBukkit plugin;
42+
43+
public DeleteCommand(FastLoginBukkit plugin) {
44+
this.plugin = plugin;
45+
}
46+
47+
/**
48+
* Handles the command to delete profiles.
49+
*/
50+
@Override
51+
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
52+
53+
if (!sender.hasPermission(command.getPermission())) {
54+
plugin.getCore().sendLocaleMessage("no-permission", sender);
55+
return true;
56+
}
57+
58+
if (plugin.getBungeeManager().isEnabled()) {
59+
sender.sendMessage("Error: Cannot delete profile entries when using BungeeCord!");
60+
return false;
61+
}
62+
63+
if (args.length < 1) {
64+
sender.sendMessage("Error: Must supply username to delete!");
65+
return false;
66+
}
67+
68+
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
69+
int count = plugin.getCore().getStorage().deleteProfile(args[0]);
70+
if (!(sender instanceof ConsoleCommandSender)) {
71+
Bukkit.getScheduler().runTask(plugin, () -> {
72+
if (count == 0) {
73+
sender.sendMessage("Error: No profile entries found!");
74+
} else {
75+
sender.sendMessage("Deleted " + count + " matching profile entries");
76+
}
77+
});
78+
}
79+
});
80+
81+
return true;
82+
}
83+
84+
@Override
85+
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
86+
List<String> list = new ArrayList<>();
87+
for (Player p : Bukkit.getOnlinePlayers()) {
88+
if (p.getName().toLowerCase().startsWith(args[0])) {
89+
list.add(p.getName());
90+
}
91+
}
92+
return null;
93+
}
94+
}

bukkit/src/main/resources/plugin.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ commands:
4545
usage: /<command> [player]
4646
permission: ${project.artifactId}.command.cracked
4747

48+
fldelete:
49+
description: 'Delete player profile data'
50+
usage: /<command> [player]
51+
permission: ${project.artifactId}.command.delete
52+
4853
permissions:
4954
${project.artifactId}.command.premium:
5055
description: 'Label themselves as premium'
@@ -63,3 +68,7 @@ permissions:
6368
description: 'Label others as cracked'
6469
children:
6570
${project.artifactId}.command.cracked: true
71+
72+
${project.artifactId}.command.delete:
73+
description: 'Delete other players profile data'
74+
default: op

core/src/main/java/com/github/games647/fastlogin/core/storage/AuthStorage.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public interface AuthStorage {
3232

3333
StoredProfile loadProfile(UUID uuid);
3434

35+
int deleteProfile(String name);
36+
3537
void save(StoredProfile playerProfile);
3638

3739
void close();

core/src/main/java/com/github/games647/fastlogin/core/storage/SQLStorage.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public abstract class SQLStorage implements AuthStorage {
6565
+ "` WHERE `Name`=? LIMIT 1";
6666
protected static final String LOAD_BY_UUID = "SELECT * FROM `" + PREMIUM_TABLE
6767
+ "` WHERE `UUID`=? LIMIT 1";
68+
protected static final String DELETE_BY_NAME = "DELETE FROM " + PREMIUM_TABLE
69+
+ " WHERE `Name` = ?";
6870
protected static final String INSERT_PROFILE = "INSERT INTO `" + PREMIUM_TABLE
6971
+ "` (`UUID`, `Name`, `Premium`, `Floodgate`, `LastIp`) " + "VALUES (?, ?, ?, ?, ?) ";
7072
// limit not necessary here, because it's unique
@@ -143,6 +145,25 @@ public StoredProfile loadProfile(UUID uuid) {
143145
return null;
144146
}
145147

148+
@Override
149+
public int deleteProfile(String name) {
150+
try (Connection con = dataSource.getConnection();
151+
PreparedStatement deleteStmt = con.prepareStatement(DELETE_BY_NAME)) {
152+
deleteStmt.setString(1, name);
153+
154+
int rowsDeleted = deleteStmt.executeUpdate();
155+
if (rowsDeleted > 0) {
156+
log.info("Deleted {}'s profile data", name);
157+
} else {
158+
log.info("No profile data found for {}", name);
159+
}
160+
return rowsDeleted;
161+
} catch (SQLException sqlEx) {
162+
log.error("Failed to query profile: {}", name, sqlEx);
163+
return 0;
164+
}
165+
}
166+
146167
private Optional<StoredProfile> parseResult(ResultSet resultSet) throws SQLException {
147168
if (resultSet.next()) {
148169
long userId = resultSet.getInt("UserID");

0 commit comments

Comments
 (0)