Skip to content

Commit

Permalink
chore: Improve help command
Browse files Browse the repository at this point in the history
Signed-off-by: Awakened-Redstone <[email protected]>
  • Loading branch information
Awakened-Redstone committed May 14, 2024
1 parent c4a0e65 commit 1d763b5
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ private static void register(CommandDispatcher<ServerCommandSource> dispatcher)
}

private static void registerPublicCommands(CommandDispatcher<ServerCommandSource> dispatcher) {
//TODO: Fix soft-lock on "/help is"
MenuCommand.init(dispatcher);
CreateCommand.init(dispatcher);
HubCommands.init(dispatcher);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
package com.awakenedredstone.neoskies.command.island;

import com.awakenedredstone.neoskies.logic.IslandLogic;
import com.awakenedredstone.neoskies.util.Texts;
import com.google.common.collect.Iterables;
import com.mojang.brigadier.CommandDispatcher;
import eu.pb4.placeholders.api.TextParserUtils;
import com.mojang.brigadier.ParseResults;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.ParsedCommandNode;
import com.mojang.brigadier.tree.CommandNode;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Language;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import static com.awakenedredstone.neoskies.command.utils.CommandUtils.node;
import static com.awakenedredstone.neoskies.command.utils.CommandUtils.register;
import static net.minecraft.server.command.CommandManager.literal;
Expand All @@ -18,31 +30,46 @@ public static void init(CommandDispatcher<ServerCommandSource> dispatcher) {
.then(literal("help")
.requires(Permissions.require("neoskies.command.help", true))
.executes(context -> {
HelpCommand.run(context.getSource(), dispatcher);
ServerPlayerEntity player = context.getSource().getPlayer();
if (player != null) {
HelpCommand.run(player);

}
return 1;
})
)
);
}

static void run(ServerPlayerEntity player) {
Language lang = Language.getInstance();
StringBuilder text = new StringBuilder();
String key = "message.neoskies.help.";
private static void run(ServerCommandSource source, CommandDispatcher<ServerCommandSource> dispatcher) {
ParseResults<ServerCommandSource> parseResults = dispatcher.parse(IslandLogic.getConfig().command, source);
if (parseResults.getContext().getNodes().isEmpty()) {
source.sendError(Texts.of("commands.neoskies.error.no_commands"));
return;
}

for (int i = 0; i <= 32; i++) {
if (lang.hasTranslation(key + i)) {
text.append(lang.get(key + i));
}
if (lang.hasTranslation(key + (i + 1))) {
text.append("\n");
} else {
break;
//Map<CommandNode<ServerCommandSource>, String> nodes2 = dispatcher.getSmartUsage(Iterables.getLast(parseResults.getContext().getNodes()).getNode(), source);

CommandNode<ServerCommandSource> node = Iterables.getLast(parseResults.getContext().getNodes()).getNode();
Collection<CommandNode<ServerCommandSource>> nodes = node.getChildren().stream().sorted().toList();
source.sendFeedback(() -> Texts.of("commands.neoskies.help"), false);
sendCommands(nodes, source, "", "");
}

private static boolean sendCommands(Collection<CommandNode<ServerCommandSource>> nodes, ServerCommandSource source, String parent, String parentTranslation) {
for (CommandNode<ServerCommandSource> node : nodes) {
if (node.getChildren().isEmpty() || node.getCommand() != null) {
String command = node.getUsageText();
MutableText prefix = Texts.of("commands.neoskies.help.prefix", map -> {
map.put("prefix", IslandLogic.getConfig().command);
map.put("command", parent + command);
});
String string = "commands.description.neoskies." + parentTranslation + command;
Text description = Texts.of(string.replaceAll("\\.<.*>$", ""));
source.sendFeedback(() -> prefix.append(description), false);
}
sendCommands(node.getChildren(), source, parent + node.getUsageText() + " ", parentTranslation + node.getUsageText() + ".");
}
player.sendMessage(TextParserUtils.formatText(text.toString()));
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Style;
import net.minecraft.text.Text;

import java.util.Optional;
Expand Down Expand Up @@ -104,7 +105,7 @@ private static int execute(ServerCommandSource source) {
boolean overrideMode = protectionBypass.contains(player);
Item item = overrideMode ? Items.OAK_CHEST_BOAT : Items.OAK_BOAT;
slotHolder.setSlot(slotHolder.getSize() - 2, new CBGuiElementBuilder(item).setName(Texts.of("item_name.neoskies.protection_bypass"))
.addLoreLine(Texts.of("text.neoskies.protection_bypass", map -> map.put("value", String.valueOf(overrideMode))))
.addLoreLine(Texts.loreBase(Texts.of("text.neoskies.protection_bypass", map -> map.put("value", String.valueOf(overrideMode)))))
.setCallback((index, type, action, gui) -> {
gui.getPlayer().playSoundToPlayer(SoundEvents.UI_BUTTON_CLICK.value(), SoundCategory.MASTER, 0.3f, 1);
if (overrideMode) protectionBypass.remove(player);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public CBGuiElement buildIcon(Island island) {

public List<Text> buildLore(Island island) {
List<Text> lore = new ArrayList<>();
lore.add(Text.empty().setStyle(Style.EMPTY.withItalic(false)).append(Texts.of("island_settings." + identifier.toTranslationKey() + ".description")));
lore.add(Texts.loreBase("island_settings." + identifier.toTranslationKey() + ".description"));
lore.add(Text.empty());
int value = island.getSettings().get(identifier).getPermissionLevel().getLevel();
List<Integer> levels = new ArrayList<>();
Expand Down
62 changes: 42 additions & 20 deletions src/main/java/com/awakenedredstone/neoskies/util/Texts.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.awakenedredstone.neoskies.util;

import com.awakenedredstone.neoskies.logic.IslandLogic;
import eu.pb4.placeholders.api.ParserContext;
import eu.pb4.placeholders.api.TextParserUtils;
import eu.pb4.placeholders.api.parsers.NodeParser;
import eu.pb4.placeholders.api.parsers.ParserBuilder;
import net.minecraft.text.MutableText;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableTextContent;
import xyz.nucleoid.server.translations.api.Localization;
Expand All @@ -15,82 +20,87 @@
public class Texts {
private static final Map<String, String> EMPTY_STRING_MAP = Collections.emptyMap();
private static final Map<String, Text> EMPTY_TEXT_MAP = Collections.emptyMap();
public static final String PREFIX = "message.neoskies.prefix";
public static final String PREFIX = "neoskies.prefix";
private static final NodeParser PARSER = ParserBuilder.of()
.globalPlaceholders()
.simplifiedTextFormat()
.quickText()
.build();

public static String getTextString(Text text) {
if (text.getContent() instanceof TranslatableTextContent tanslatable) {
return Localization.text(text, ServerLanguage.getLanguage(IslandLogic.getConfig().language)).getString();
} else return text.getString();
}

public static Text prefixed(String prefixKey, Text key, Map<String, Text> placeholders) {
public static MutableText prefixed(String prefixKey, Text key, Map<String, Text> placeholders) {
Text prefix = of(Text.translatable(prefixKey));
Text text = of(key, placeholders);

return Text.empty().append(prefix).append(text);
}

public static Text prefixed(String prefixKey, Text key, Consumer<Map<String, Text>> builder) {
public static MutableText prefixed(String prefixKey, Text key, Consumer<Map<String, Text>> builder) {
Map<String, Text> placeholders = new HashMap<>();
builder.accept(placeholders);

return prefixed(prefixKey, key, placeholders);
}

public static Text prefixed(String prefixKey, Text key) {
public static MutableText prefixed(String prefixKey, Text key) {
return prefixed(prefixKey, key, EMPTY_TEXT_MAP);
}

public static Text prefixed(Text key, Consumer<Map<String, Text>> builder) {
public static MutableText prefixed(Text key, Consumer<Map<String, Text>> builder) {
return prefixed(PREFIX, key, builder);
}

public static Text prefixed(Text key, Map<String, Text> placeholders) {
public static MutableText prefixed(Text key, Map<String, Text> placeholders) {
return prefixed(PREFIX, key, placeholders);
}

public static Text prefixed(Text key) {
public static MutableText prefixed(Text key) {
return prefixed(key, EMPTY_TEXT_MAP);
}

public static Text prefixed(String prefixKey, String key, Map<String, String> placeholders) {
public static MutableText prefixed(String prefixKey, String key, Map<String, String> placeholders) {
Text prefix = of(Text.translatable(prefixKey));
Text text = of(key, placeholders);

return Text.empty().append(prefix).append(text);
}

public static Text prefixed(String prefixKey, String key, Consumer<Map<String, String>> builder) {
public static MutableText prefixed(String prefixKey, String key, Consumer<Map<String, String>> builder) {
Map<String, String> placeholders = new HashMap<>();
builder.accept(placeholders);

return prefixed(prefixKey, key, placeholders);
}

public static Text prefixed(String prefixKey, String key) {
public static MutableText prefixed(String prefixKey, String key) {
return prefixed(prefixKey, key, EMPTY_STRING_MAP);
}

public static Text prefixed(String key, Consumer<Map<String, String>> builder) {
public static MutableText prefixed(String key, Consumer<Map<String, String>> builder) {
return prefixed(PREFIX, key, builder);
}

public static Text prefixed(String key, Map<String, String> placeholders) {
public static MutableText prefixed(String key, Map<String, String> placeholders) {
return prefixed(PREFIX, key, placeholders);
}

public static Text prefixed(String key) {
public static MutableText prefixed(String key) {
return prefixed(key, EMPTY_STRING_MAP);
}

public static Text of(String key, Map<String, String> placeholders1) {
public static MutableText of(String key, Map<String, String> placeholders1) {
Map<String, Text> placeholders = new HashMap<>();
placeholders1.forEach((k, v) -> placeholders.put(k, Text.literal(v)));

return of(Text.translatable(key), placeholders);
}

public static Text of(String key, Consumer<Map<String, String>> builder) {
public static MutableText of(String key, Consumer<Map<String, String>> builder) {
Map<String, String> placeholders1 = new HashMap<>();
builder.accept(placeholders1);

Expand All @@ -100,23 +110,35 @@ public static Text of(String key, Consumer<Map<String, String>> builder) {
return of(Text.translatable(key), placeholders);
}

public static Text of(String key) {
public static MutableText of(String key) {
return of(key, EMPTY_STRING_MAP);
}

public static Text of(Text key, Map<String, Text> placeholders) {
public static MutableText of(Text key, Map<String, Text> placeholders) {
Text text = DynamicPlaceholders.parseText(key, placeholders);
return TextParserUtils.formatText(text.getString());
return (MutableText) PARSER.parseText(text.getString(), ParserContext.of());
}

public static Text of(Text key, Consumer<Map<String, Text>> builder) {
public static MutableText of(Text key, Consumer<Map<String, Text>> builder) {
Map<String, Text> placeholders = new HashMap<>();
builder.accept(placeholders);

return of(key, placeholders);
}

public static Text of(Text key) {
public static MutableText of(Text key) {
return of(key, EMPTY_TEXT_MAP);
}

public static MutableText loreBase() {
return Text.empty().setStyle(Style.EMPTY.withItalic(false));
}

public static MutableText loreBase(Text text) {
return loreBase().append(text);
}

public static MutableText loreBase(String text) {
return loreBase(Texts.of(text));
}
}
26 changes: 26 additions & 0 deletions src/main/resources/data/neoskies/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
{
"neoskies.prefix": "<color:#f4d402><bold>NeoSkies</bold></color> <dark_gray>:</dark_gray> <reset>",

"commands.neoskies.admin.bypass.enable": "<green>Enabled</green> protection bypass",
"commands.neoskies.admin.bypass.disable": "<red>Disabled</red> protection bypass",
"commands.neoskies.reload": "Reloading configs!",
"commands.neoskies.error.player_only": "<color:#ff816a>Only players can execute this!",
"commands.neoskies.error.no_commands": "<color:#ff816a>Couldn't find any command!",

"commands.neoskies.help.prefix": " <color:#98f9ff>/%prefix% %command%</color> → ",
"commands.neoskies.help": "<color:#f4d402>NeoSkies</color> <grey>user commands:</grey>",
"commands.description.neoskies.accept": "Accept island join invite request.",
"commands.description.neoskies.ban": "Ban player from visiting your island.",
"commands.description.neoskies.create": "Creates an Island.",
"commands.description.neoskies.delete": "Deletes your island.",
"commands.description.neoskies.help": "Sends this list.",
"commands.description.neoskies.home": "Teleport to an island you are member of.",
"commands.description.neoskies.hub": "Teleport to the Hub.",
"commands.description.neoskies.kick": "Kick player from your island.",
"commands.description.neoskies.level": "Check your island level",
"commands.description.neoskies.level.scan": "Scan the island",
"commands.description.neoskies.level.view": "View your island points",
"commands.description.neoskies.members.invite": "Invite player to your island.",
"commands.description.neoskies.members.remove": "Remove player from your island.",
"commands.description.neoskies.menu": "Open the NeoSkies island menu.",
"commands.description.neoskies.settings": "Opens the island settings menu.",
"commands.description.neoskies.settings.lock": "Enables/disables ability to visit your Island.",
"commands.description.neoskies.settings.position.spawn": "Changes home position.",
"commands.description.neoskies.settings.position.visit": "Changes visits position.",
"commands.description.neoskies.unban": "Unban player to allow visiting your Island.",
"commands.description.neoskies.visit": "Visit someone's Island.",

"island_protection.neoskies.place/blocks": "You can't place blocks out here!",
"island_protection.neoskies.place/minecarts": "You can't place minecarts out here!",
Expand Down

0 comments on commit 1d763b5

Please sign in to comment.