-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added new help generator, started working on new java support, bump t…
…o 2.1.6
- Loading branch information
Showing
12 changed files
with
150 additions
and
66 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ | |
|
||
public interface UsageMessage { | ||
void sendTo(@NotNull BladeContext context); | ||
@NotNull String toString(); | ||
} |
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
60 changes: 60 additions & 0 deletions
60
src/main/java/me/vaperion/blade/help/impl/BukkitHelpGenerator.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,60 @@ | ||
package me.vaperion.blade.help.impl; | ||
|
||
import me.vaperion.blade.command.BladeCommand; | ||
import me.vaperion.blade.command.impl.BukkitUsageMessage; | ||
import me.vaperion.blade.context.BladeContext; | ||
import me.vaperion.blade.help.HelpGenerator; | ||
import me.vaperion.blade.utils.PaginatedOutput; | ||
import org.bukkit.ChatColor; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class BukkitHelpGenerator implements HelpGenerator { | ||
|
||
@NotNull | ||
@Override | ||
public List<String> generate(@NotNull BladeContext context, @NotNull List<BladeCommand> commands) { | ||
commands = commands.stream().distinct().filter(c -> !c.isHidden()).collect(Collectors.toList()); | ||
|
||
return new PaginatedOutput<BladeCommand>(10) { | ||
@Override | ||
public String formatErrorMessage(Error error, Object... args) { | ||
switch (error) { | ||
case NO_RESULTS: | ||
return ChatColor.RED + "No results found."; | ||
case PAGE_OUT_OF_BOUNDS: | ||
return ChatColor.RED + String.format("Page %d does not exist, valid range is 1 to %d.", args); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public String getHeader(int page, int totalPages) { | ||
return ChatColor.AQUA + "==== " + ChatColor.YELLOW + "Help for /" + context.alias() + ChatColor.AQUA + " ===="; | ||
} | ||
|
||
@Override | ||
public String getFooter(int page, int totalPages) { | ||
return ChatColor.AQUA + "==== " + ChatColor.YELLOW + "Page " + page + "/" + totalPages + ChatColor.AQUA + " ===="; | ||
} | ||
|
||
@Override | ||
public String formatLine(BladeCommand result, int index) { | ||
return ChatColor.AQUA + " - " + | ||
ChatColor.YELLOW + ChatColor.stripColor(result.getUsageMessage().ensureGetOrLoad(() -> new BukkitUsageMessage(result)).toString().replace("Usage: ", "")) + | ||
(result.getDescription().isEmpty() ? "" : (" - " + ChatColor.GRAY + result.getDescription())); | ||
} | ||
}.generatePage(commands, parsePage(context.argument(0))); | ||
} | ||
|
||
private int parsePage(String argument) { | ||
if (argument == null) return 1; | ||
try { | ||
return Integer.parseInt(argument); | ||
} catch (NumberFormatException e) { | ||
return 1; | ||
} | ||
} | ||
} |
44 changes: 0 additions & 44 deletions
44
src/main/java/me/vaperion/blade/help/impl/DefaultHelpGenerator.java
This file was deleted.
Oops, something went wrong.
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
47 changes: 47 additions & 0 deletions
47
src/main/java/me/vaperion/blade/utils/PaginatedOutput.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,47 @@ | ||
package me.vaperion.blade.utils; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
@RequiredArgsConstructor | ||
public abstract class PaginatedOutput<T> { | ||
|
||
private final int resultsPerPage; | ||
|
||
public abstract String formatErrorMessage(Error error, Object... args); | ||
|
||
public abstract String getHeader(int page, int totalPages); | ||
|
||
public abstract String getFooter(int page, int totalPages); | ||
|
||
public abstract String formatLine(T result, int index); | ||
|
||
public final List<String> generatePage(List<T> results, int page) { | ||
if (results.size() == 0) { | ||
return Collections.singletonList(formatErrorMessage(Error.NO_RESULTS)); | ||
} | ||
|
||
int totalPages = results.size() / resultsPerPage + (results.size() % resultsPerPage == 0 ? 0 : 1); | ||
if (page < 1 || page > totalPages) { | ||
return Collections.singletonList(formatErrorMessage(Error.PAGE_OUT_OF_BOUNDS, page, totalPages)); | ||
} | ||
|
||
int startIndex = (page - 1) * resultsPerPage; | ||
int endIndex = Math.min(startIndex + resultsPerPage, results.size()); | ||
|
||
List<String> lines = new ArrayList<>(); | ||
lines.add(getHeader(page, totalPages)); | ||
results.subList(startIndex, endIndex).forEach(result -> lines.add(formatLine(result, startIndex + lines.size()))); | ||
lines.add(getFooter(page, totalPages)); | ||
return lines; | ||
} | ||
|
||
public enum Error { | ||
NO_RESULTS, | ||
PAGE_OUT_OF_BOUNDS | ||
} | ||
|
||
} |