Skip to content

Commit

Permalink
Fixed: Bugs in the rewritten /tg check command
Browse files Browse the repository at this point in the history
  • Loading branch information
Bram1903 committed Oct 14, 2024
1 parent bb68784 commit 7331004
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public TotemGuardCommand(TotemGuard plugin) {
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (!hasAnyPermission(sender)) {
sender.sendMessage(messageService.version());
return true;
return false;
}

if (args.length == 0) {
Expand All @@ -91,7 +91,8 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
SubCommand subCommand = subCommands.get(subCommandName);

if (subCommand != null && hasPermissionForSubCommand(sender, subCommandName)) {
return subCommand.execute(sender, args);
subCommand.execute(sender, args);
return true;
} else {
sender.sendMessage(getAvailableCommandsComponent(sender));
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@

import java.util.List;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

public class CheckCommand extends Check implements SubCommand {
private static CheckCommand instance;
private final ConcurrentHashMap<UUID, Long> cooldowns = new ConcurrentHashMap<>();

private final TotemGuard plugin;
private final ConfigManager configManager;
Expand All @@ -67,7 +69,6 @@ public static CheckCommand getInstance(TotemGuard plugin) {
return instance;
}

@Override
public boolean execute(CommandSender sender, String[] args) {
if (args.length != 2) {
sender.sendMessage(messageService.getPrefix().append(Component.text("Usage: /totemguard check <player>", NamedTextColor.RED)));
Expand All @@ -80,17 +81,35 @@ public boolean execute(CommandSender sender, String[] args) {
return false;
}

final Settings.Checks.ManualTotemA settings = configManager.getSettings().getChecks().getManualTotemA();
UUID targetUUID = target.getUniqueId();
long currentTime = System.currentTimeMillis();

if (target.getGameMode() != org.bukkit.GameMode.SURVIVAL && target.getGameMode() != org.bukkit.GameMode.ADVENTURE) {
sender.sendMessage(messageService.getPrefix().append(Component.text("This player is not in survival mode!", NamedTextColor.RED)));
return false;
}

final PlayerInventory inventory = target.getInventory();
if (cooldowns.containsKey(targetUUID)) {
long lastExecutionTime = cooldowns.get(targetUUID);
long elapsedTime = currentTime - lastExecutionTime;
long totalCooldown = settings.getCheckTime() + 1000;

if (elapsedTime < totalCooldown) {
long remainingTime = totalCooldown - elapsedTime;
sender.sendMessage(messageService.getPrefix().append(Component.text("This player is on cooldown for " + remainingTime + "ms!", NamedTextColor.RED)));
return false;
}
}

cooldowns.put(targetUUID, currentTime);


final PlayerInventory inventory = target.getInventory();
final ItemStack mainHandItem = inventory.getItemInMainHand().clone();
final ItemStack offHandItem = inventory.getItemInOffHand().clone();
final boolean totemInMainHand = mainHandItem.getType() == org.bukkit.Material.TOTEM_OF_UNDYING;
final boolean totemInOffhand = offHandItem.getType() == org.bukkit.Material.TOTEM_OF_UNDYING;
final boolean totemInMainHand = mainHandItem.getType() == Material.TOTEM_OF_UNDYING;
final boolean totemInOffhand = offHandItem.getType() == Material.TOTEM_OF_UNDYING;

if (totemInMainHand) {
if (!totemInOffhand) {
Expand All @@ -105,35 +124,29 @@ public boolean execute(CommandSender sender, String[] args) {
}

double health = target.getHealth();
final Settings.Checks.ManualTotemA settings = configManager.getSettings().getChecks().getManualTotemA();

// Declare a mutable container to hold the TaskWrapper
final TaskWrapper[] taskWrapper = new TaskWrapper[1];
target.setHealth(0.5);
target.damage(1000);

// Task scheduling and check logic
final TaskWrapper[] taskWrapper = new TaskWrapper[1];
final long startTime = System.currentTimeMillis();
target.damage(health);

// Schedule the task and store the reference in the array
taskWrapper[0] = FoliaScheduler.getAsyncScheduler().runAtFixedRate(plugin, (o) -> {
long elapsedTime = System.currentTimeMillis() - startTime;

if (elapsedTime >= settings.getCheckTime()) {
target.sendMessage(messageService.getPrefix().append(Component.text(target.getName() + " has successfully passed the check!", NamedTextColor.GREEN)));
sender.sendMessage(messageService.getPrefix().append(Component.text(target.getName() + " has successfully passed the check!", NamedTextColor.GREEN)));
resetPlayerState(target, health, mainHandItem, offHandItem);

// Cancel the task since the check is complete
taskWrapper[0].cancel();
return;
}

if (inventory.getItemInOffHand().getType() == totemMaterial) {
resetPlayerState(target, health, mainHandItem, offHandItem);

// Cancel the task and flag the player since the check has failed
taskWrapper[0].cancel();
flag(target, createDetails(sender, elapsedTime), settings);
}

}, 0, 50, TimeUnit.MILLISECONDS);

return true;
Expand All @@ -160,11 +173,13 @@ public List<String> onTabComplete(CommandSender sender, String[] args) {

@Override
public void resetData() {
cooldowns.clear();
super.resetData();
}

@Override
public void resetData(UUID uuid) {
cooldowns.remove(uuid);
super.resetData(uuid);
}

Expand Down

0 comments on commit 7331004

Please sign in to comment.