-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…496) * Create and expose cooldown API * Create platform sender factory and expose platform API. * Improve cooldown context API * Code style fix
- Loading branch information
Showing
26 changed files
with
187 additions
and
93 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
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
90 changes: 90 additions & 0 deletions
90
litecommands-core/src/dev/rollczi/litecommands/cooldown/CooldownService.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,90 @@ | ||
package dev.rollczi.litecommands.cooldown; | ||
|
||
import dev.rollczi.litecommands.command.executor.CommandExecutor; | ||
import dev.rollczi.litecommands.identifier.Identifier; | ||
import dev.rollczi.litecommands.meta.Meta; | ||
import dev.rollczi.litecommands.platform.PlatformSender; | ||
import dev.rollczi.litecommands.scheduler.Scheduler; | ||
import dev.rollczi.litecommands.scheduler.SchedulerPoll; | ||
import java.time.Instant; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
@ApiStatus.Experimental | ||
public class CooldownService { | ||
|
||
private final Scheduler scheduler; | ||
private final Map<CooldownCompositeKey, CooldownState> cooldowns = new HashMap<>(); | ||
|
||
public CooldownService(Scheduler scheduler) { | ||
this.scheduler = scheduler; | ||
} | ||
|
||
Optional<CooldownState> getState(CommandExecutor<?> executor, PlatformSender sender) { | ||
CooldownContext cooldownContext = getOperativeContext(executor, sender); | ||
|
||
if (cooldownContext == null) { | ||
return Optional.empty(); | ||
} | ||
|
||
return getState(cooldownContext.getKey(), sender.getIdentifier()); | ||
} | ||
|
||
public Optional<CooldownState> getState(String key, Identifier senderIdentifier) { | ||
CooldownCompositeKey compositeKey = new CooldownCompositeKey(senderIdentifier, key); | ||
CooldownState cooldownState = cooldowns.get(compositeKey); | ||
|
||
if (cooldownState != null && !cooldownState.isExpired()) { | ||
return Optional.of(cooldownState); | ||
} | ||
|
||
return Optional.empty(); | ||
} | ||
|
||
boolean updateState(CommandExecutor<?> executor, PlatformSender sender) { | ||
CooldownContext cooldownContext = getOperativeContext(executor, sender); | ||
|
||
if (cooldownContext == null) { | ||
return false; | ||
} | ||
|
||
return updateState(cooldownContext, sender.getIdentifier()); | ||
} | ||
|
||
public boolean updateState(CooldownContext context, Identifier senderIdentifier) { | ||
CooldownCompositeKey compositeKey = new CooldownCompositeKey(senderIdentifier, context.getKey()); | ||
|
||
Instant now = Instant.now(); | ||
Instant expirationTime = now.plus(context.getDuration()); | ||
cooldowns.put(compositeKey, new CooldownState(context, expirationTime)); | ||
scheduler.supplyLater(SchedulerPoll.MAIN, context.getDuration(), () -> cooldowns.remove(compositeKey)); | ||
return true; | ||
} | ||
|
||
public boolean clearState(String key, Identifier senderIdentifier) { | ||
CooldownCompositeKey compositeKey = new CooldownCompositeKey(senderIdentifier, key); | ||
|
||
return cooldowns.remove(compositeKey) != null; | ||
} | ||
|
||
@Nullable | ||
private CooldownContext getOperativeContext(CommandExecutor<?> executor, PlatformSender sender) { | ||
CooldownContext cooldownContext = executor.metaCollector().findFirst(Meta.COOLDOWN, null); | ||
|
||
if (cooldownContext == null) { | ||
return null; | ||
} | ||
|
||
String bypassPermission = cooldownContext.getBypassPermission(); | ||
|
||
if (!bypassPermission.isEmpty() && sender.hasPermission(bypassPermission)) { | ||
return null; | ||
} | ||
|
||
return cooldownContext; | ||
} | ||
|
||
} |
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
1 change: 0 additions & 1 deletion
1
litecommands-core/src/dev/rollczi/litecommands/meta/MetaHolderCollectorImpl.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
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
11 changes: 11 additions & 0 deletions
11
litecommands-core/src/dev/rollczi/litecommands/platform/PlatformSenderFactory.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,11 @@ | ||
package dev.rollczi.litecommands.platform; | ||
|
||
/** | ||
* Factory for a {@link PlatformSender} | ||
* @param <SENDER> - Platform related sender e.g. CommandSender, User, CommandSource | ||
*/ | ||
public interface PlatformSenderFactory<SENDER> { | ||
|
||
PlatformSender create(SENDER sender); | ||
|
||
} |
Oops, something went wrong.