-
-
Notifications
You must be signed in to change notification settings - Fork 20
GH-983 Add configurable settings for invalid usage message generation #1163
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,29 @@ | ||||||||||||||||||||||||||||||||||||||||||
package com.eternalcode.core.litecommand.handler.invalidusage; | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
import eu.okaeri.configs.OkaeriConfig; | ||||||||||||||||||||||||||||||||||||||||||
import eu.okaeri.configs.annotation.Comment; | ||||||||||||||||||||||||||||||||||||||||||
import lombok.Getter; | ||||||||||||||||||||||||||||||||||||||||||
import lombok.experimental.Accessors; | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
@SuppressWarnings({"FieldMayBeFinal", "FieldCanBeLocal"}) | ||||||||||||||||||||||||||||||||||||||||||
@Getter | ||||||||||||||||||||||||||||||||||||||||||
@Accessors(fluent = true) | ||||||||||||||||||||||||||||||||||||||||||
public class InvalidUsageConfig extends OkaeriConfig implements InvalidUsageSettings { | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
@Comment({ | ||||||||||||||||||||||||||||||||||||||||||
"# How to display invalid usage hints:", | ||||||||||||||||||||||||||||||||||||||||||
"# - MOST_RELEVANT: show only the most relevant (first) usage (default).", | ||||||||||||||||||||||||||||||||||||||||||
"# - DETAILED: show header and a list of top N usages." | ||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||
public InvalidUsageHintMode usageHintMode = InvalidUsageHintMode.MOST_RELEVANT; | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
@Comment({ | ||||||||||||||||||||||||||||||||||||||||||
"# When usageHintMode is DETAILED: maximum number of usages to show in the list." | ||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||
public int detailedMaxEntries = 5; | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
@Comment({ | ||||||||||||||||||||||||||||||||||||||||||
"# When usageHintMode is DETAILED: whether to show the usage header before the list." | ||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||
public boolean detailedShowHeader = true; | ||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+20
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,11 +1,11 @@ | ||||||||||||||||||||
package com.eternalcode.core.litecommand.handler; | ||||||||||||||||||||
package com.eternalcode.core.litecommand.handler.invalidusage; | ||||||||||||||||||||
|
||||||||||||||||||||
import com.eternalcode.core.injector.annotations.Inject; | ||||||||||||||||||||
import com.eternalcode.core.injector.annotations.lite.LiteHandler; | ||||||||||||||||||||
import com.eternalcode.core.notice.NoticeService; | ||||||||||||||||||||
import com.eternalcode.core.placeholder.Placeholders; | ||||||||||||||||||||
import com.eternalcode.core.viewer.ViewerService; | ||||||||||||||||||||
import com.eternalcode.core.viewer.Viewer; | ||||||||||||||||||||
import com.eternalcode.core.viewer.ViewerService; | ||||||||||||||||||||
import dev.rollczi.litecommands.handler.result.ResultHandlerChain; | ||||||||||||||||||||
import dev.rollczi.litecommands.invalidusage.InvalidUsage; | ||||||||||||||||||||
import dev.rollczi.litecommands.invalidusage.InvalidUsageHandler; | ||||||||||||||||||||
|
@@ -20,19 +20,30 @@ class InvalidUsageHandlerImpl implements InvalidUsageHandler<CommandSender> { | |||||||||||||||||||
|
||||||||||||||||||||
private final ViewerService viewerService; | ||||||||||||||||||||
private final NoticeService noticeService; | ||||||||||||||||||||
private final InvalidUsageSettings invalidUsageSettings; | ||||||||||||||||||||
|
||||||||||||||||||||
@Inject | ||||||||||||||||||||
InvalidUsageHandlerImpl(ViewerService viewerService, NoticeService noticeService) { | ||||||||||||||||||||
InvalidUsageHandlerImpl( | ||||||||||||||||||||
ViewerService viewerService, | ||||||||||||||||||||
NoticeService noticeService, | ||||||||||||||||||||
InvalidUsageSettings invalidUsageSettings | ||||||||||||||||||||
) { | ||||||||||||||||||||
this.viewerService = viewerService; | ||||||||||||||||||||
this.noticeService = noticeService; | ||||||||||||||||||||
this.invalidUsageSettings = invalidUsageSettings; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
@Override | ||||||||||||||||||||
public void handle(Invocation<CommandSender> invocation, InvalidUsage<CommandSender> result, ResultHandlerChain<CommandSender> chain) { | ||||||||||||||||||||
public void handle( | ||||||||||||||||||||
Invocation<CommandSender> invocation, | ||||||||||||||||||||
InvalidUsage<CommandSender> result, | ||||||||||||||||||||
ResultHandlerChain<CommandSender> chain) { | ||||||||||||||||||||
Viewer viewer = this.viewerService.any(invocation.sender()); | ||||||||||||||||||||
Schematic schematic = result.getSchematic(); | ||||||||||||||||||||
|
||||||||||||||||||||
if (schematic.isOnlyFirst()) { | ||||||||||||||||||||
InvalidUsageHintMode mode = this.invalidUsageSettings.usageHintMode(); | ||||||||||||||||||||
|
||||||||||||||||||||
if (mode == InvalidUsageHintMode.MOST_RELEVANT || schematic.isOnlyFirst()) { | ||||||||||||||||||||
this.noticeService.create() | ||||||||||||||||||||
.viewer(viewer) | ||||||||||||||||||||
.notice(translation -> translation.argument().usageMessage()) | ||||||||||||||||||||
|
@@ -41,11 +52,21 @@ public void handle(Invocation<CommandSender> invocation, InvalidUsage<CommandSen | |||||||||||||||||||
return; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
this.noticeService.viewer(viewer, translation -> translation.argument().usageMessageHead()); | ||||||||||||||||||||
if (this.invalidUsageSettings.detailedShowHeader()) { | ||||||||||||||||||||
this.noticeService.viewer(viewer, translation -> translation.argument().usageMessageHead()); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
int limit = Math.max(1, this.invalidUsageSettings.detailedMaxEntries()); | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pardon nie widziałem podpowiedzi bota xD |
||||||||||||||||||||
int shown = 0; | ||||||||||||||||||||
|
||||||||||||||||||||
for (String schema : schematic.all()) { | ||||||||||||||||||||
this.noticeService.viewer(viewer, translation -> translation.argument().usageMessageEntry(), SCHEME.toFormatter(schema)); | ||||||||||||||||||||
if (shown++ >= limit) { | ||||||||||||||||||||
break; | ||||||||||||||||||||
} | ||||||||||||||||||||
this.noticeService.viewer( | ||||||||||||||||||||
viewer, | ||||||||||||||||||||
translation -> translation.argument().usageMessageEntry(), | ||||||||||||||||||||
SCHEME.toFormatter(schema)); | ||||||||||||||||||||
} | ||||||||||||||||||||
Comment on lines
+59
to
70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The loop for sending usage hints can be simplified by using the Java Stream API. This approach is more concise, expressive, and less prone to manual indexing errors. schematic.all().stream()
.limit(Math.max(1, this.invalidUsageSettings.detailedMaxEntries()))
.forEach(schema -> this.noticeService.viewer(
viewer,
translation -> translation.argument().usageMessageEntry(),
SCHEME.toFormatter(schema)
)); |
||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.eternalcode.core.litecommand.handler.invalidusage; | ||
|
||
public enum InvalidUsageHintMode { | ||
MOST_RELEVANT, | ||
DETAILED | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.eternalcode.core.litecommand.handler.invalidusage; | ||
|
||
public interface InvalidUsageSettings { | ||
InvalidUsageHintMode usageHintMode(); | ||
|
||
int detailedMaxEntries(); | ||
|
||
boolean detailedShowHeader(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need to define standards regarding configs in future meeting - why are we having public fields despite having generated getters?