-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
1,128 additions
and
1,013 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ plugins { | |
} | ||
|
||
group 'io.github.mqzn' | ||
version '1.1.5' | ||
version '1.1.6' | ||
|
||
repositories { | ||
mavenCentral() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
common/src/main/java/io/github/mqzn/commands/base/cooldown/CommandCooldown.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package io.github.mqzn.commands.base.cooldown; | ||
|
||
import java.time.Duration; | ||
import java.util.Objects; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class CommandCooldown { | ||
|
||
public static final CommandCooldown EMPTY = new CommandCooldown(0, TimeUnit.SECONDS); | ||
private final long value; | ||
private final TimeUnit unit; | ||
private final Duration duration; | ||
|
||
public CommandCooldown(long value, TimeUnit unit) { | ||
this.value = value; | ||
this.unit = unit; | ||
this.duration = Duration.of(value, unit.toChronoUnit()); | ||
} | ||
|
||
public boolean isEmpty() { | ||
return value <= 0; | ||
} | ||
|
||
public long toMillis() { | ||
return duration.toMillis(); | ||
} | ||
|
||
public long getValue() { | ||
return value; | ||
} | ||
|
||
public TimeUnit getUnit() { | ||
return unit; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (this == other) return true; | ||
if (!(other instanceof CommandCooldown that)) return false; | ||
return value == that.value && unit == that.unit; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(value, unit); | ||
} | ||
} | ||
|
||
|
38 changes: 0 additions & 38 deletions
38
common/src/main/java/io/github/mqzn/commands/base/cooldown/CommandCooldown.kt
This file was deleted.
Oops, something went wrong.
62 changes: 62 additions & 0 deletions
62
common/src/main/java/io/github/mqzn/commands/base/cooldown/CooldownCaption.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package io.github.mqzn.commands.base.cooldown; | ||
|
||
import io.github.mqzn.commands.base.caption.Caption; | ||
import io.github.mqzn.commands.base.caption.CaptionKey; | ||
import io.github.mqzn.commands.base.caption.Message; | ||
import io.github.mqzn.commands.base.context.Context; | ||
import io.github.mqzn.commands.utilities.TimeParser; | ||
import net.kyori.adventure.text.Component; | ||
import net.kyori.adventure.text.TextComponent; | ||
import net.kyori.adventure.text.format.NamedTextColor; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Locale; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public final class CooldownCaption<S> implements Caption<S> { | ||
|
||
public static String formatUnit(TimeUnit unit) { | ||
var unitName = unit.name().toLowerCase(Locale.getDefault()); | ||
unitName = unitName.substring(0, unitName.length() - 1); | ||
return unitName + "(s)"; | ||
} | ||
|
||
public static long calculateRemainingTime(long lastTime, CommandCooldown commandCooldown) { | ||
var diff = System.currentTimeMillis() - lastTime; | ||
return commandCooldown.toMillis() - diff; | ||
} | ||
|
||
@Override | ||
public @NotNull CaptionKey key() { | ||
return CaptionKey.COMMAND_IN_COOLDOWN; | ||
} | ||
|
||
@Override | ||
public @NotNull TextComponent message(S sender, Context<S> context, Throwable exception) { | ||
var command = context.commandUsed(); | ||
var manager = command.manager(); | ||
var cooldown = command.cooldown(); | ||
var lastTimeCommandExecuted = manager.getCommandCooldown(manager.getSenderWrapper().senderName(sender)); | ||
if (lastTimeCommandExecuted == null) lastTimeCommandExecuted = 0L; | ||
// Send a caption telling the user that he's in a cooldown | ||
// Calculating remaining time | ||
var parser = TimeParser.parse(calculateRemainingTime(lastTimeCommandExecuted, cooldown)); | ||
var timeData = parser.highestLogicalUnitValue(); | ||
|
||
return cooldownMessage(timeData.getLeft(), timeData.getRight(), context); | ||
} | ||
|
||
public TextComponent cooldownMessage(long time, TimeUnit unit, Context<S> context) { | ||
return Message.prefixed(Message.EXECUTION_ERROR) | ||
.append( | ||
Component.text( | ||
String.format( | ||
"Command '" + context.commandUsed().name() | ||
+ "' is in cooldown for %d %s", time, formatUnit(unit) | ||
), NamedTextColor.YELLOW | ||
) | ||
); | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.