add reset command + exists method for data source
This commit is contained in:
parent
9f33889604
commit
7cc9395cbc
6 changed files with 172 additions and 2 deletions
|
@ -0,0 +1,145 @@
|
|||
package me.unurled.sacredrealms.sr.commands.player;
|
||||
|
||||
import static me.unurled.sacredrealms.sr.utils.Component.comp;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import me.unurled.sacredrealms.sr.SR;
|
||||
import me.unurled.sacredrealms.sr.components.player.PlayerManager;
|
||||
import me.unurled.sacredrealms.sr.data.DataManager;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabExecutor;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class ResetAdventureCommand implements TabExecutor {
|
||||
|
||||
private static final Long COOLDOWN = 120L;
|
||||
|
||||
private final HashMap<Player, Long> cooldowns = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Executes the given command, returning its success. <br>
|
||||
* If false is returned, then the "usage" plugin.yml entry for this command (if defined) will be
|
||||
* sent to the player.
|
||||
*
|
||||
* @param sender Source of the command
|
||||
* @param command Command which was executed
|
||||
* @param label Alias of the command which was used
|
||||
* @param args Passed command arguments
|
||||
* @return true if a valid command, otherwise false
|
||||
*/
|
||||
@Override
|
||||
public boolean onCommand(
|
||||
@NotNull CommandSender sender,
|
||||
@NotNull Command command,
|
||||
@NotNull String label,
|
||||
@NotNull String[] args) {
|
||||
if (sender.hasPermission("sr.resetadventure")) {
|
||||
if (args.length > 2) {
|
||||
sender.sendMessage(comp("<red>Usage: /resetadventure <player>"));
|
||||
return true;
|
||||
}
|
||||
if (sender instanceof Player p) {
|
||||
if (args.length == 0) {
|
||||
boolean status = resetAdventure(p);
|
||||
if (status) {
|
||||
p.sendMessage(comp("<green>Your adventure has been reset."));
|
||||
} else {
|
||||
p.sendMessage(comp("<red>Failed to reset your adventure."));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
OfflinePlayer player = SR.getInstance().getServer().getOfflinePlayer(args[0]);
|
||||
|
||||
boolean status = resetAdventure(player);
|
||||
if (status) {
|
||||
sender.sendMessage(comp("<green>Successfully reset " + player.getName() + "'s adventure."));
|
||||
} else {
|
||||
sender.sendMessage(comp("<red>Failed to reset " + player.getName() + "'s adventure."));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if (args.length > 0) {
|
||||
sender.sendMessage(comp("<red>Usage: /resetadventure"));
|
||||
return true;
|
||||
}
|
||||
if (sender instanceof Player player) {
|
||||
if (cooldowns.containsKey(player)) {
|
||||
long lastUsed = cooldowns.get(player);
|
||||
long secondsLeft = ((lastUsed / 1000) + COOLDOWN) - (System.currentTimeMillis() / 1000);
|
||||
if (secondsLeft > 0) {
|
||||
player.sendMessage(
|
||||
comp(
|
||||
"<red>You must wait "
|
||||
+ secondsLeft
|
||||
+ " seconds before using this command again."));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
boolean status = resetAdventure(player);
|
||||
if (status) {
|
||||
player.sendMessage(comp("<green>Your adventure has been reset."));
|
||||
} else {
|
||||
player.sendMessage(comp("<red>Failed to reset your adventure."));
|
||||
}
|
||||
cooldowns.put(player, System.currentTimeMillis());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean resetAdventure(OfflinePlayer player) {
|
||||
DataManager dataManager = (DataManager) DataManager.getInstance(DataManager.class);
|
||||
if (dataManager != null) {
|
||||
if (dataManager.getDataHandler().exists(PlayerManager.PLAYER_KEY + player.getUniqueId())) {
|
||||
dataManager.getDataHandler().remove(PlayerManager.PLAYER_KEY + player.getUniqueId());
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Requests a list of possible completions for a command argument.
|
||||
*
|
||||
* @param sender Source of the command. For players tab-completing a command inside of a command
|
||||
* block, this will be the player, not the command block.
|
||||
* @param command Command which was executed
|
||||
* @param label Alias of the command which was used
|
||||
* @param args The arguments passed to the command, including final partial argument to be
|
||||
* completed
|
||||
* @return A List of possible completions for the final argument, or null to default to the
|
||||
* command executor
|
||||
*/
|
||||
@Override
|
||||
public @Nullable List<String> onTabComplete(
|
||||
@NotNull CommandSender sender,
|
||||
@NotNull Command command,
|
||||
@NotNull String label,
|
||||
@NotNull String[] args) {
|
||||
if (sender.hasPermission("sr.resetadventure")) {
|
||||
if (args.length == 1) {
|
||||
return null;
|
||||
}
|
||||
if (args[0].isBlank()) {
|
||||
return Arrays.stream(Bukkit.getOfflinePlayers())
|
||||
.map(OfflinePlayer::getName)
|
||||
.toList().stream().limit(10).toList();
|
||||
}
|
||||
return Arrays.stream(Bukkit.getOfflinePlayers())
|
||||
.map(OfflinePlayer::getName).filter(Objects::nonNull)
|
||||
.filter(name -> name.toLowerCase().startsWith(args[0].toLowerCase()))
|
||||
.toList().stream().limit(10).toList();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue