Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix changing remaining time of command cooldown #6021

Merged
merged 2 commits into from
Sep 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 49 additions & 37 deletions src/main/java/ch/njol/skript/command/ScriptCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,11 @@
*/
package ch.njol.skript.command;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;

import ch.njol.skript.ScriptLoader;
import ch.njol.skript.config.SectionNode;
import org.skriptlang.skript.lang.script.Script;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.help.GenericCommandHelpTopic;
import org.bukkit.help.HelpMap;
import org.bukkit.help.HelpTopic;
import org.bukkit.help.HelpTopicComparator;
import org.bukkit.help.IndexHelpTopic;
import org.bukkit.plugin.Plugin;
import org.eclipse.jdt.annotation.Nullable;

import ch.njol.skript.Skript;
import ch.njol.skript.SkriptConfig;
import ch.njol.skript.command.Commands.CommandAliasHelpTopic;
import ch.njol.skript.config.SectionNode;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.lang.Trigger;
Expand All @@ -77,6 +44,39 @@
import ch.njol.skript.variables.Variables;
import ch.njol.util.StringUtils;
import ch.njol.util.Validate;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.help.GenericCommandHelpTopic;
import org.bukkit.help.HelpMap;
import org.bukkit.help.HelpTopic;
import org.bukkit.help.HelpTopicComparator;
import org.bukkit.help.IndexHelpTopic;
import org.bukkit.plugin.Plugin;
import org.eclipse.jdt.annotation.Nullable;
import org.skriptlang.skript.lang.script.Script;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;

/**
* This class is used for user-defined commands.
Expand Down Expand Up @@ -274,9 +274,21 @@ public boolean execute(final CommandSender sender, final String commandLabel, fi
}

Runnable runnable = () -> {
// save previous last usage date to check if the execution has set the last usage date
Date previousLastUsage = null;
if (sender instanceof Player)
previousLastUsage = getLastUsage(((Player) sender).getUniqueId(), event);

// execute the command - may modify the last usage date
execute2(event, sender, commandLabel, rest);
if (sender instanceof Player && !event.isCooldownCancelled())
setLastUsage(((Player) sender).getUniqueId(), event, new Date());

if (sender instanceof Player && !event.isCooldownCancelled()) {
Date lastUsage = getLastUsage(((Player) sender).getUniqueId(), event);
// check if the execution has set the last usage date
// if not, set it to the current date. if it has, we leave it alone so as not to affect the remaining/elapsed time (#5862)
if (Objects.equals(lastUsage, previousLastUsage))
setLastUsage(((Player) sender).getUniqueId(), event, new Date());
}
};
if (Bukkit.isPrimaryThread()) {
runnable.run();
Expand Down Expand Up @@ -510,7 +522,7 @@ public void setRemainingMilliseconds(UUID uuid, Event event, long milliseconds)
assert cooldown != null;
long cooldownMs = cooldown.getMilliSeconds();
if (milliseconds > cooldownMs)
throw new IllegalArgumentException("Remaining time may not be longer than the cooldown");
milliseconds = cooldownMs;
setElapsedMilliSeconds(uuid, event, cooldownMs - milliseconds);
}

Expand Down