-
-
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
- Loading branch information
Showing
5 changed files
with
89 additions
and
76 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
72 changes: 0 additions & 72 deletions
72
litecommands-core/src/dev/rollczi/litecommands/schematic/SchematicGeneratorSimpleImpl.java
This file was deleted.
Oops, something went wrong.
83 changes: 83 additions & 0 deletions
83
litecommands-core/src/dev/rollczi/litecommands/schematic/SimpleSchematicGenerator.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,83 @@ | ||
package dev.rollczi.litecommands.schematic; | ||
|
||
import dev.rollczi.litecommands.argument.Argument; | ||
import dev.rollczi.litecommands.command.CommandRoute; | ||
import dev.rollczi.litecommands.command.executor.CommandExecutor; | ||
import dev.rollczi.litecommands.validator.ValidatorService; | ||
import dev.rollczi.litecommands.wrapper.WrapperRegistry; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
public class SimpleSchematicGenerator<SENDER> implements SchematicGenerator<SENDER> { | ||
|
||
private static final String SEPARATOR = " "; | ||
|
||
protected final SchematicFormat format; | ||
protected final ValidatorService<SENDER> validatorService; | ||
protected final WrapperRegistry wrapperRegistry; | ||
|
||
public SimpleSchematicGenerator(SchematicFormat format, ValidatorService<SENDER> validatorService, WrapperRegistry wrapperRegistry) { | ||
this.format = format; | ||
this.validatorService = validatorService; | ||
this.wrapperRegistry = wrapperRegistry; | ||
} | ||
|
||
@Override | ||
public Schematic generate(SchematicInput<SENDER> schematicInput) { | ||
List<String> schematics = generateRaw(schematicInput) | ||
.map(schematic -> format.prefix() + schematic.trim() + format.suffix()) | ||
.collect(Collectors.toList()); | ||
|
||
return new Schematic(schematics); | ||
} | ||
|
||
protected Stream<String> generateRaw(SchematicInput<SENDER> schematicInput) { | ||
CommandExecutor<SENDER> executor = schematicInput.getExecutor(); | ||
String base = schematicInput.collectRoutes().stream() | ||
.map(route -> route.getName()) | ||
.collect(Collectors.joining(SEPARATOR)) | ||
+ SEPARATOR; | ||
|
||
if (executor != null) { | ||
return Stream.of(base + generateExecutor(schematicInput, executor)); | ||
} | ||
|
||
return generateRoute(schematicInput, schematicInput.getLastRoute(), base); | ||
} | ||
|
||
protected Stream<String> generateRoute(SchematicInput<SENDER> input, CommandRoute<SENDER> route, String base) { | ||
Stream<String> children = route.getChildren().stream() | ||
.flatMap(subRoute -> generateRoute(input, subRoute, base + subRoute.getName() + SEPARATOR)); | ||
|
||
Stream<String> executors = route.getExecutors().stream() | ||
.filter(executor -> isVisible(input, executor)) | ||
.map(executor -> base + generateExecutor(input, executor)); | ||
|
||
return Stream.concat(executors, children); | ||
} | ||
|
||
protected String generateExecutor(SchematicInput<SENDER> input, CommandExecutor<SENDER> executor) { | ||
return executor.getArguments().stream() | ||
.map(argument -> String.format(generateArgumentFormat(input, argument), generateArgumentName(input, argument))) | ||
.collect(Collectors.joining(SEPARATOR)); | ||
} | ||
|
||
protected String generateArgumentFormat(SchematicInput<SENDER> input, Argument<?> argument) { | ||
return this.isOptional(input, argument) ? format.optionalArgumentFormat() : format.argumentFormat(); | ||
} | ||
|
||
protected String generateArgumentName(SchematicInput<SENDER> input, Argument<?> argument) { | ||
return argument.getName(); | ||
} | ||
|
||
protected boolean isVisible(SchematicInput<SENDER> input, CommandExecutor<SENDER> executor) { | ||
return validatorService.validate(input.getInvocation(), executor).isContinue(); | ||
} | ||
|
||
protected boolean isOptional(SchematicInput<SENDER> input, Argument<?> argument) { | ||
return wrapperRegistry.getWrappedExpectedFactory(argument.getWrapperFormat()).canCreateEmpty() || argument.hasDefaultValue(); | ||
} | ||
|
||
} |
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