Skip to content

Commit 661b426

Browse files
committed
Added /fldelete <name> to delete profiles from storage by name
1 parent dd2aa92 commit 661b426

5 files changed

Lines changed: 119 additions & 0 deletions

File tree

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: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 (args.length < 1) {
59+
sender.sendMessage("Error: Must supply username to delete!");
60+
return false;
61+
}
62+
63+
int count = plugin.getCore().getStorage().deleteProfile(args[0]);
64+
if (!(sender instanceof ConsoleCommandSender)) {
65+
if (count == 0) {
66+
sender.sendMessage("Error: No profile entries found!");
67+
} else {
68+
sender.sendMessage("Deleted " + count + " matching profile entries");
69+
}
70+
}
71+
72+
return true;
73+
}
74+
75+
@Override
76+
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
77+
List<String> list = new ArrayList<>();
78+
for (Player p : Bukkit.getOnlinePlayers()) {
79+
if (p.getName().toLowerCase().startsWith(args[0])) {
80+
list.add(p.getName());
81+
}
82+
}
83+
return null;
84+
}
85+
}

bukkit/src/main/resources/plugin.yml

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

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

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)