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.MiniMessage; 029import net.kyori.adventure.text.minimessage.tag.Tag; 030import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; 031import org.jetbrains.annotations.NotNull; 032 033public class HelpObject implements ComponentLike { 034 035 static final MiniMessage MINI_MESSAGE = MiniMessage.miniMessage(); 036 037 private final Component rendered; 038 039 public HelpObject(final Command command, final String label, final PlotPlayer<?> audience) { 040 rendered = MINI_MESSAGE.deserialize( 041 TranslatableCaption.of("help.help_item").getComponent(audience), 042 TagResolver.builder() 043 .tag("usage", Tag.inserting(Component.text(command.getUsage().replace("{label}", label)))) 044 .tag("alias", Tag.inserting(Component.text( 045 command.getAliases().isEmpty() ? "" : StringMan.join(command.getAliases(), " | ") 046 ))) 047 .tag("desc", Tag.inserting(command.getDescription().toComponent(audience))) 048 .tag("arguments", Tag.inserting(Component.text(buildArgumentList(command.getRequiredArguments())))) 049 .tag("label", Tag.inserting(Component.text(label))) 050 .build() 051 ); 052 } 053 054 private String buildArgumentList(final Argument<?>[] arguments) { 055 if (arguments == null) { 056 return ""; 057 } 058 final StringBuilder builder = new StringBuilder(); 059 for (final Argument<?> argument : arguments) { 060 builder.append("[").append(argument.getName()).append(" (") 061 .append(argument.getExample()).append(")],"); 062 } 063 return arguments.length > 0 ? builder.substring(0, builder.length() - 1) : ""; 064 } 065 066 @Override 067 public @NotNull Component asComponent() { 068 return this.rendered; 069 } 070 071}