Skip to content

Commit

Permalink
Add revive command tab completion
Browse files Browse the repository at this point in the history
  • Loading branch information
KartoffelChipss committed Jan 2, 2025
1 parent 5f9e774 commit 5f5d9ec
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.strassburger</groupId>
<artifactId>lifestealz</artifactId>
<version>2.9.1</version>
<version>2.9.2</version>
<packaging>jar</packaging>

<name>LifeStealZ</name>
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/strassburger/lifestealz/LifeStealZ.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public final class LifeStealZ extends JavaPlugin {
private GeyserPlayerFile geyserPlayerFile;
private WebHookManager webHookManager;
private GracePeriodManager gracePeriodManager;
private EliminatedPlayersCache eliminatedPlayersCache;
private final boolean hasWorldGuard = Bukkit.getPluginManager().getPlugin("WorldGuard") != null;
private final boolean hasPlaceholderApi = Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null;
private final boolean hasGeyser = Bukkit.getPluginManager().getPlugin("Geyser-Spigot") != null;
Expand Down Expand Up @@ -71,6 +72,8 @@ public void onEnable() {
gracePeriodManager = new GracePeriodManager(this);
webHookManager = new WebHookManager(this);

eliminatedPlayersCache = new EliminatedPlayersCache(this);

new CommandManager(this).registerCommands();

new EventManager(this).registerListeners();
Expand Down Expand Up @@ -109,6 +112,10 @@ public Storage getStorage() {
return storage;
}

public EliminatedPlayersCache getEliminatedPlayersCache() {
return eliminatedPlayersCache;
}

public WorldGuardManager getWorldGuardManager() {
return worldGuardManager;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ private void eliminatePlayer(CommandSender sender, Player targetPlayer) {
Bukkit.broadcast(elimAnnouncementMsg);
}

plugin.getEliminatedPlayersCache().addEliminatedPlayer(targetPlayer.getName());

plugin.getWebHookManager().sendWebhookMessage(WebHookManager.WebHookType.ELIMINATION, targetPlayer.getName(), sender.getName());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private List<String> getSecondArgOptions(CommandSender sender, String[] args) {
if (sender.hasPermission("lifestealz.managedata")) return List.of("export", "import");
break;
case "dev":
return List.of("giveForbiddenitem", "isInGracePeriod", "setFirstJoinDate");
return List.of("giveForbiddenitem", "isInGracePeriod", "setFirstJoinDate", "refreshCaches");
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,17 @@ public boolean execute(CommandSender sender, String[] args) {
plugin.getGracePeriodManager().startGracePeriod(player);
}

if (optionTwo.equals("refreshCaches")) {
plugin.getEliminatedPlayersCache().reloadCache();
sender.sendMessage(MessageUtils.formatMsg("&7Caches reloaded!"));
}

return false;
}

@Override
public String getUsage() {
return "/lifestealz dev <giveForbiddenitem | isInGracePeriod | setFirstJoinDate>";
return "/lifestealz dev <giveForbiddenitem | isInGracePeriod | setFirstJoinDate | refreshCaches>";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
import org.strassburger.lifestealz.util.commands.CommandUtils;
import org.strassburger.lifestealz.util.storage.PlayerData;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class ReviveCommand implements CommandExecutor, TabCompleter {
private final LifeStealZ plugin;
Expand Down Expand Up @@ -53,7 +56,8 @@ private boolean handleReviveAll(CommandSender sender, String bypassOption) {
plugin.getConfig().getInt("maxRevives"),
BYPASS_OPTION.equals(bypassOption) && sender.hasPermission("lifestealz.bypassrevivelimit")
);
sender.sendMessage(MessageUtils.getAndFormatMsg(false, "massReviveSuccess",
plugin.getEliminatedPlayersCache().reloadCache();
sender.sendMessage(MessageUtils.getAndFormatMsg(true, "massReviveSuccess",
"&7You successfully revived &c%amount% &7player(s)!",
new MessageUtils.Replaceable("%amount%", Integer.toString(revivedPlayers))
));
Expand Down Expand Up @@ -139,6 +143,7 @@ private void revivePlayer(CommandSender sender, String targetPlayerName, PlayerD
playerData.setMaxHealth(plugin.getConfig().getDouble("reviveHearts") * 2);
playerData.setHasbeenRevived(playerData.getHasbeenRevived() + 1);
plugin.getStorage().save(playerData);
plugin.getEliminatedPlayersCache().removeEliminatedPlayer(targetPlayerName);

plugin.getWebHookManager().sendWebhookMessage(WebHookManager.WebHookType.REVIVE, targetPlayerName, sender.getName());
}
Expand All @@ -156,7 +161,9 @@ private void throwUsageError(CommandSender sender) {
@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, String[] args) {
if (args.length == 1) {
return CommandUtils.getPlayersTabCompletion(true, plugin);
Set<String> eliminatedPlayers = plugin.getEliminatedPlayersCache().getEliminatedPlayers();
eliminatedPlayers.add("*");
return new ArrayList<>(eliminatedPlayers);
}
if (args.length == 2 && sender.hasPermission("lifestealz.bypassrevivelimit")) {
return List.of(BYPASS_OPTION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ public void onInventoryClick(InventoryClickEvent event) {
targetPlayerData.setHasbeenRevived(targetPlayerData.getHasbeenRevived() + 1);
plugin.getStorage().save(targetPlayerData);

plugin.getEliminatedPlayersCache().removeEliminatedPlayer(targetPlayer.getName());

player.sendMessage(MessageUtils.getAndFormatMsg(true, "messages.reviveSuccess", "&7You successfully revived &c%player%&7!", new MessageUtils.Replaceable("%player%", targetPlayer.getName())));

player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 500.0f, 1.0f);
Expand Down Expand Up @@ -157,6 +159,8 @@ public void onInventoryClick(InventoryClickEvent event) {
targetBedrockPlayerData.setHasbeenRevived(targetBedrockPlayerData.getHasbeenRevived() + 1);
plugin.getStorage().save(targetBedrockPlayerData);

plugin.getEliminatedPlayersCache().removeEliminatedPlayer(targetBedrockPlayer.getName());

player.sendMessage(MessageUtils.getAndFormatMsg(true, "messages.reviveSuccess", "&7You successfully revived &c%player%&7!", new MessageUtils.Replaceable("%player%", targetBedrockPlayer.getName())));

player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 500.0f, 1.0f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ private void handleElimination(PlayerDeathEvent event, Player player, Player kil
PlayerData playerData = plugin.getStorage().load(player.getUniqueId());
playerData.setMaxHealth(0.0);
plugin.getStorage().save(playerData);
plugin.getEliminatedPlayersCache().addEliminatedPlayer(player.getName());
}

private void handleKillerHeartGain(Player player, Player killer, World world, double healthGain) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.strassburger.lifestealz.util;

import org.strassburger.lifestealz.LifeStealZ;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class EliminatedPlayersCache {
private final Set<String> eliminatedPlayers;
private final LifeStealZ plugin;

/**
* A cache for eliminated players to avoid unnecessary database queries on tab completion
*/
public EliminatedPlayersCache(LifeStealZ plugin) {
this.plugin = plugin;
this.eliminatedPlayers = Collections.synchronizedSet(new HashSet<>());
reloadCache();
}

/**
* Reload the cache from the database
*/
public void reloadCache() {
synchronized (eliminatedPlayers) {
eliminatedPlayers.clear();
eliminatedPlayers.addAll(plugin.getStorage().getEliminatedPlayerNames());
}
}

/**
* Get a set of all eliminated players
* @return A set of all eliminated players
*/
public Set<String> getEliminatedPlayers() {
synchronized (eliminatedPlayers) {
return new HashSet<>(eliminatedPlayers);
}
}

/**
* Add a player to the cache
* @param username The username of the player to add
*/
public void addEliminatedPlayer(String username) {
synchronized (eliminatedPlayers) {
eliminatedPlayers.add(username);
}
}

/**
* Remove a player from the cache
* @param username The username of the player to remove
*/
public void removeEliminatedPlayer(String username) {
synchronized (eliminatedPlayers) {
eliminatedPlayers.remove(username);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,31 @@ public List<String> getPlayerNames() {
return playerNames;
}

@Override
public List<String> getEliminatedPlayerNames() {
List<String> eliminatedPlayerNames = new ArrayList<>();

try (Connection connection = createConnection()) {
if (connection == null) return eliminatedPlayerNames;

try (Statement statement = connection.createStatement()) {
statement.setQueryTimeout(30);

ResultSet resultSet = statement.executeQuery("SELECT name FROM hearts WHERE maxhp <= 0.0");

while (resultSet.next()) {
eliminatedPlayerNames.add(resultSet.getString("name"));
}
} catch (SQLException e) {
getPlugin().getLogger().severe("Failed to load player data from SQL database: " + e.getMessage());
}
} catch (SQLException e) {
getPlugin().getLogger().severe("Failed to load player data from SQL database: " + e.getMessage());
}

return eliminatedPlayerNames;
}

private void migrateDatabase(Connection connection) {
try (Statement statement = connection.createStatement()) {
boolean hasFirstJoin = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,6 @@ protected LifeStealZ getPlugin() {
* Get all player names
*/
public abstract List<String> getPlayerNames();

public abstract List<String> getEliminatedPlayerNames();
}

0 comments on commit 5f5d9ec

Please sign in to comment.