Skip to content

Commit

Permalink
#544 #531 Improved help message syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
booksaw committed Jan 31, 2024
1 parent 5867bca commit f110e1c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 9 deletions.
7 changes: 6 additions & 1 deletion changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -752,9 +752,14 @@ Features:
- Added placeholders to get members at a specific rank - Credit EarthCow (#542, #537)
- Added /teama ally and /teama neutral (#523)

4.9.0:

Features:
- Improved help message syntax (#310, #528)

Bug Fixes:
- Fixed issue with urls causing a 404 - Credit Ceymikey (#522)
- Fixed bug with /team setowner - Credit EarthCow(#544, 531)
- Fixed bug with /team setowner - Credit EarthCow(#544, #531)

Headings:
Features:
Expand Down
74 changes: 66 additions & 8 deletions src/main/java/com/booksaw/betterTeams/commands/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,31 @@
import java.io.File;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.logging.Level;

import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import com.booksaw.betterTeams.CommandResponse;
import com.booksaw.betterTeams.Main;
import com.booksaw.betterTeams.message.MessageManager;

import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ClickEvent.Action;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.chat.hover.content.Text;

public class HelpCommand extends SubCommand {

private int commandsPerPage = 7;

public static ChatColor prefix, description;
private static boolean fullyCustom = false;
final ParentCommand command;
Expand Down Expand Up @@ -52,14 +62,36 @@ public CommandResponse onCommand(CommandSender sender, String label, String[] ar
return null;
}

for (Entry<String, SubCommand> subCommand : command.getSubCommands().entrySet()) {
if (sender.hasPermission("betterTeams." + subCommand.getValue().getNode())) {
sender.sendMessage(
createHelpMessage(label, subCommand.getKey() + " " + subCommand.getValue().getArguments(),
subCommand.getValue().getHelpMessage()));
List<SubCommand> permissiveCommands = new ArrayList<>();
command.getSubCommands().values().forEach(c -> {
if (sender.hasPermission(c.getNode())) {
permissiveCommands.add(c);
}
});

int maxPage = permissiveCommands.size() / commandsPerPage
+ ((permissiveCommands.size() % commandsPerPage == 0) ? 0 : 1);
int page;
if (args.length > 0) {
page = getPage(args[0], maxPage);
} else {
page = 0;
}
MessageManager.sendMessage(sender, "help.header");

for (int i = commandsPerPage * page; i < permissiveCommands.size() && i < commandsPerPage * (page + 1); i++) {
SubCommand subCommand = permissiveCommands.get(i);
if (sender instanceof Player) {
((Player)sender).spigot().sendMessage(createClickableHelpMessage(label, subCommand.getCommand() + " " + subCommand.getArguments(), subCommand.getHelpMessage()));
} else {
MessageManager.sendFullMessage(sender, createHelpMessage(label,
subCommand.getCommand() + " " + subCommand.getArguments(), subCommand.getHelpMessage()));
}
}

MessageManager.sendMessageF(sender, "help.footer", Integer.toString(page + 1), Integer.toString(maxPage),
command.getCommand());

return null;
}

Expand All @@ -79,8 +111,9 @@ public void fullyCustom(CommandSender sender, String label) {

PrintWriter writer = new PrintWriter(f);
for (Entry<String, SubCommand> sub : command.getSubCommands().entrySet()) {
writer.println(Main.plugin.getConfig().getString("prefixFormat") + "&b/" + label + " " + sub.getKey() + " " + sub.getValue().getArguments() + "&f - &6"
+ sub.getValue().getHelpMessage());
writer.println(
Main.plugin.getConfig().getString("prefixFormat") + "&b/" + label + " " + sub.getKey() + " "
+ sub.getValue().getArguments() + "&f - &6" + sub.getValue().getHelpMessage());
}
writer.close();

Expand All @@ -93,7 +126,7 @@ public void fullyCustom(CommandSender sender, String label) {
fullyCustom = false;
}
}
try (BufferedReader reader = new BufferedReader(new FileReader(f))){
try (BufferedReader reader = new BufferedReader(new FileReader(f))) {
String line;
while ((line = reader.readLine()) != null) {
sender.sendMessage(org.bukkit.ChatColor.translateAlternateColorCodes('&', line));
Expand All @@ -117,10 +150,21 @@ public void fullyCustom(CommandSender sender, String label) {
* @return the created message relating to that command
*/
public String createHelpMessage(String label, String commandPath, String description) {

return prefix + "/" + label + " " + commandPath + ChatColor.WHITE + " - " + HelpCommand.description
+ description;
}

public TextComponent createClickableHelpMessage(String label, String commandPath, String description) {

TextComponent message = new TextComponent(MessageManager.getPrefix() + prefix + "/" + label + " " + commandPath + ChatColor.WHITE + " - " + HelpCommand.description
+ description);
message.setClickEvent(new ClickEvent(Action.SUGGEST_COMMAND, "/" + label + " " + commandPath));
message.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(prefix + "/" + label + " " + commandPath)));

return message;
}

@Override
public String getCommand() {
return "help";
Expand Down Expand Up @@ -156,4 +200,18 @@ public int getMaximumArguments() {
return 0;
}

private int getPage(String request, int maxPage) {

try {
int page = Integer.parseInt(request);
if (page > maxPage) {
return maxPage;
}
return page - 1;
} catch (NumberFormatException e) {
return 0;
}

}

}
3 changes: 3 additions & 0 deletions src/main/resources/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,9 @@ uclaim.kick: '&4You cannot kick members of your own team from your claim'
uclaim.ban: '&4You cannot ban members of your own team from your claim'
uclaim.dissolve: '&4Your team has been disbanded so your claim has been dissolved'

help.header: '&6---- &bBetterTeams Help &6----'
help.footer: '&6---- &bPage %s/%s - /%s help <page> &6----'

help.description: View and change your team's description
help.ban: Bans the specified player from your team
help.home: Teleports you to your team's home
Expand Down

0 comments on commit f110e1c

Please sign in to comment.