001/* 002 * PlotSquared, a land and world management plugin for Minecraft. 003 * Copyright (C) IntellectualSites <https://intellectualsites.com> 004 * Copyright (C) IntellectualSites team and contributors 005 * 006 * This program is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program. If not, see <https://www.gnu.org/licenses/>. 018 */ 019package com.plotsquared.core.util.helpmenu; 020 021import com.plotsquared.core.command.Argument; 022import com.plotsquared.core.command.Command; 023import com.plotsquared.core.configuration.caption.TranslatableCaption; 024import com.plotsquared.core.player.PlotPlayer; 025import com.plotsquared.core.util.StringMan; 026import net.kyori.adventure.text.Component; 027import net.kyori.adventure.text.ComponentLike; 028import net.kyori.adventure.text.minimessage.tag.Tag; 029import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; 030import org.jetbrains.annotations.NotNull; 031 032public class HelpObject implements ComponentLike { 033 034 private final Component rendered; 035 036 public HelpObject(final Command command, final String label, final PlotPlayer<?> audience) { 037 this.rendered = TranslatableCaption.of("help.help_item").toComponent(audience, TagResolver.builder() 038 .tag("usage", Tag.inserting(Component.text(command.getUsage().replace("{label}", label)))) 039 .tag("alias", Tag.inserting(Component.text( 040 command.getAliases().isEmpty() ? "" : StringMan.join(command.getAliases(), " | ") 041 ))) 042 .tag("desc", Tag.inserting(command.getDescription().toComponent(audience))) 043 .tag("arguments", Tag.inserting(Component.text(buildArgumentList(command.getRequiredArguments())))) 044 .tag("label", Tag.inserting(Component.text(label))) 045 .build()); 046 } 047 048 private String buildArgumentList(final Argument<?>[] arguments) { 049 if (arguments == null) { 050 return ""; 051 } 052 final StringBuilder builder = new StringBuilder(); 053 for (final Argument<?> argument : arguments) { 054 builder.append("[").append(argument.getName()).append(" (") 055 .append(argument.getExample()).append(")],"); 056 } 057 return arguments.length > 0 ? builder.substring(0, builder.length() - 1) : ""; 058 } 059 060 @Override 061 public @NotNull Component asComponent() { 062 return this.rendered; 063 } 064 065}