Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import com.eternalcode.core.feature.warp.WarpSettings;
import com.eternalcode.core.injector.annotations.Bean;
import com.eternalcode.core.injector.annotations.component.ConfigurationFile;
import com.eternalcode.core.litecommand.handler.invalidusage.InvalidUsageConfig;
import com.eternalcode.core.litecommand.handler.invalidusage.InvalidUsageSettings;
import com.eternalcode.core.translation.TranslationConfig;
import com.eternalcode.core.translation.TranslationSettings;
import eu.okaeri.configs.OkaeriConfig;
Expand Down Expand Up @@ -79,6 +81,12 @@ public class PluginConfiguration extends AbstractConfigurationFile {
@Comment("# Settings responsible for the database connection")
DatabaseConfig database = new DatabaseConfig();

@Bean(proxied = InvalidUsageSettings.class)
@Comment("")
@Comment("# Invalid usage message generation configuration")
@Comment("# Settings for command usage hinting")
InvalidUsageConfig invalidUsage = new InvalidUsageConfig();

@Bean(proxied = SpawnJoinSettings.class)
@Comment("")
@Comment("# Spawn & Join Configuration")
Expand Down Expand Up @@ -235,6 +243,7 @@ public static class Items extends OkaeriConfig {
@Comment("# Settings responsible for player vanish functionality")
VanishConfig vanish = new VanishConfig();


@Override
public File getConfigFile(File dataFolder) {
return new File(dataFolder, "config.yml");
Expand Down
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 {
Copy link
Member

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?


@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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@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({
"# Applies ONLY when usageHintMode = DETAILED.",
"# Maximum number of usages to show in the list."
})
public int detailedMaxEntries = 5;
@Comment({
"# Applies ONLY when usageHintMode = DETAILED.",
"# Whether to show the usage header before the list."
})
public boolean detailedShowHeader = true;

}
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;
Expand All @@ -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())
Expand All @@ -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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int limit = Math.max(1, this.invalidUsageSettings.detailedMaxEntries());
int limit = Math.max(1, this.invalidUsageSettings.detailedMaxEntries());
schematic.all().stream()
.limit(limit)
.forEach(schema -> this.noticeService.viewer(
viewer,
translation -> translation.argument().usageMessageEntry(),
SCHEME.toFormatter(schema)));

Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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();
}