Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@ Currently the default value is not configurable, as this was made for a specific

### Regular Commands

| Command | Description |
|---------|-------------|
| `/clan` | Base command. |
| `/clan create <clanName>` | Creates a clan if it doesn't already exist, the name is allowed, and you are not in a clan. |
| `/clan info <clanName>` | Shows info about a certain clan. If blank, attempts to use your clan. |
| `/clan invites` | Views your current clan invites in a neat list to accept/decline. |
| `/clan accept <clanName>` | Alternative to clicking in chat to accept an invite. |
| `/clan decline <clanName>` | Alternative to clicking in chat to decline an invite. |
| `/clan join <clanName>` | Joins a clan if it is open to joins. |
| `/clan leave` | Leaves your current clan. If you are the leader with no other members, disbands the clan. |
| Command | Description |
|----------------------------|---------------------------------------------------------------------------------------------|
| `/clan` | Base command. |
| `/clan help [pageName]` | Shows a help menu page (defaults to this page if pageName isn't chosen). |
| `/clan create <clanName>` | Creates a clan if it doesn't already exist, the name is allowed, and you are not in a clan. |
| `/clan info <clanName>` | Shows info about a certain clan. If blank, attempts to use your clan. |
| `/clan invites` | Views your current clan invites in a neat list to accept/decline. |
| `/clan accept <clanName>` | Alternative to clicking in chat to accept an invite. |
| `/clan decline <clanName>` | Alternative to clicking in chat to decline an invite. |
| `/clan join <clanName>` | Joins a clan if it is open to joins. |
| `/clan leave` | Leaves your current clan. If you are the leader with no other members, disbands the clan. |

### Clan Officer Commands - Restricted to Officers and Above

Expand All @@ -52,10 +53,9 @@ Currently the default value is not configurable, as this was made for a specific
|------------------------------------------------|---------------------------------------------------------|
| `/clan promote <playerName>` | Promotes a player to Officer rank. |
| `/clan demote <playerName>` | Demotes a player to Member rank. |
| `/clan disband` | Sends a confirmation message for deletion. |
| `/clan disband confirm` | Deletes your clan directly. |
| `/clan disband [confirm]` | Deletes a clan (if confirmed). |
| `/clan set color <colorName/hexCode>` | Changes the color code of your clan. |
| `/clan set access <open\|invite_only\|toggle>` | Changes your clan’s access state. |
| `/clan set access <open\|invite_only\|toggle>` | Changes your clan’s access state. |
| `/clan set name <newClanName>` | Changes the name of your clan if allowed and available. |
| `/clan transfer <playerName>` | Transfers clan ownership to a member. |

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/mnfu/clantag/ClanManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ public Collection<Clan> getAllClans() {
return clans.values();
}

public Collection<String> getAllClansCanonicalNames() {
return clans.keySet();
}

public boolean load() {
if (!file.exists()) return false;

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/mnfu/clantag/ClanTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public void onInitialize() {
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {

var baseCommand = CommandManager.literal("clan");
HelpCommand helpCommandClass = new HelpCommand();
var helpCommand = new HelpCommand().build();
var adminCommand = new AdminCommand(clanManager).build();
var infoCommand = new InfoCommand(clanManager).build();

Expand All @@ -96,6 +98,7 @@ public void onInitialize() {
var demoteCommand = new DemoteCommand(clanManager).build();

dispatcher.register(baseCommand
.then(helpCommand)
.then(adminCommand)
.then(setCommand)
.then(infoCommand)
Expand All @@ -111,10 +114,7 @@ public void onInitialize() {
.then(transferCommand)
.then(promoteCommand)
.then(demoteCommand)
.executes(context -> {
context.getSource().sendFeedback(()->Text.literal("Command help message not implemented, WIP!"), false);
return 1;
})
.executes(helpCommandClass::executeGeneral)
);
});
}
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/mnfu/clantag/commands/AdminCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public LiteralArgumentBuilder<ServerCommandSource> build() {
})
.then(CommandManager.argument("clanName", StringArgumentType.greedyString())
.suggests((context, builder) -> {
for (Clan c : clanManager.getAllClans()) {
builder.suggest(c.name());
for (String canonicalName : clanManager.getAllClansCanonicalNames()) {
builder.suggest(canonicalName);
}
return builder.buildFuture();
})
Expand Down Expand Up @@ -82,8 +82,8 @@ public LiteralArgumentBuilder<ServerCommandSource> build() {
})
.then(CommandManager.argument("clanName", StringArgumentType.greedyString())
.suggests((context, builder) -> {
for (Clan c : clanManager.getAllClans()) {
builder.suggest(c.name());
for (String canonicalName : clanManager.getAllClansCanonicalNames()) {
builder.suggest(canonicalName);
}
return builder.buildFuture();
})
Expand Down Expand Up @@ -112,8 +112,8 @@ public LiteralArgumentBuilder<ServerCommandSource> build() {
})
.then(CommandManager.argument("clanName", StringArgumentType.greedyString())
.suggests((context, builder) -> {
for (Clan c : clanManager.getAllClans()) {
builder.suggest(c.name());
for (String canonicalName : clanManager.getAllClansCanonicalNames()) {
builder.suggest(canonicalName);
}
return builder.buildFuture();
})
Expand All @@ -134,8 +134,8 @@ public LiteralArgumentBuilder<ServerCommandSource> build() {
.requires(source -> hasPermission(source, "clantag.admin.rename"))
.then(CommandManager.argument("clanName", StringArgumentType.string())
.suggests((context, builder) -> {
for (Clan c : clanManager.getAllClans()) {
builder.suggest(c.name());
for (String canonicalName : clanManager.getAllClansCanonicalNames()) {
builder.suggest(canonicalName);
}
return builder.buildFuture();
})
Expand All @@ -158,8 +158,8 @@ public LiteralArgumentBuilder<ServerCommandSource> build() {
.requires(source -> hasPermission(source, "clantag.admin.delete"))
.then(CommandManager.argument("clanName", StringArgumentType.greedyString())
.suggests((context, builder) -> {
for (Clan c : clanManager.getAllClans()) {
builder.suggest(c.name());
for (String canonicalName : clanManager.getAllClansCanonicalNames()) {
builder.suggest(canonicalName);
}
return builder.buildFuture();
})
Expand Down
148 changes: 148 additions & 0 deletions src/main/java/mnfu/clantag/commands/HelpCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package mnfu.clantag.commands;

import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.ClickEvent;
import net.minecraft.text.HoverEvent;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class HelpCommand {

private final Map<UUID, Long> cooldowns = new HashMap<>();
private static final long COOLDOWN_MS = 150; // 0.15 second cooldown
private static final long CLEANUP_MS = 60000; // 60 second cleanup

private boolean cooldown(ServerCommandSource source) {
ServerPlayerEntity player = source.getPlayer();
if (player == null) return false;
UUID playerId = player.getUuid();
long now = System.currentTimeMillis();

cooldowns.entrySet().removeIf(entry -> now - entry.getValue() > CLEANUP_MS);

Long lastClick = cooldowns.get(playerId);
if (lastClick != null && now - lastClick < COOLDOWN_MS) {
source.sendError(Text.literal("Please wait before clicking again"));
return true;
}

cooldowns.put(playerId, now);
return false;
}

public LiteralArgumentBuilder<ServerCommandSource> build() {
return CommandManager.literal("help")
.executes(this::executeGeneral) // default to "general commands" page
.then(CommandManager.literal("general").executes(this::executeGeneral))
.then(CommandManager.literal("manage").executes(this::executeManage))
.then(CommandManager.literal("admin").executes(this::executeAdmin));
}

// HELP PAGE FOR GENERAL COMMANDS
public int executeGeneral(CommandContext<ServerCommandSource> context) {
if (cooldown(context.getSource())) return 0;
MutableText message = Text.empty();

message.append(Text.literal("[ General Commands ]").formatted(Formatting.WHITE)).append("\n");
message.append(Text.literal("/clan help [pageName]").formatted(Formatting.YELLOW)).append(" - Shows a help menu page (defaults to this page if [pageName] isn't chosen)").formatted(Formatting.GRAY).append("\n");
message.append(Text.literal("/clan create <clanName>").formatted(Formatting.YELLOW)).append(" - Creates a clan if it doesn't already exist").formatted(Formatting.GRAY).append("\n");
message.append(Text.literal("/clan info <clanName>").formatted(Formatting.YELLOW)).append(" - Shows info about a clan, and if <clanName> is blank, it shows your clan").formatted(Formatting.GRAY).append("\n");
message.append(Text.literal("/clan invites").formatted(Formatting.YELLOW)).append(" - Displays your current clan invites to accept/decline").formatted(Formatting.GRAY).append("\n");
message.append(Text.literal("/clan accept <clanName>").formatted(Formatting.YELLOW)).append(" - Accepts a clan invite").formatted(Formatting.GRAY).append("\n");
message.append(Text.literal("/clan decline <clanName>").formatted(Formatting.YELLOW)).append(" - Declines a clan invite").formatted(Formatting.GRAY).append("\n");
message.append(Text.literal("/clan join <clanName>").formatted(Formatting.YELLOW)).append(" - Joins a clan if it is open").formatted(Formatting.GRAY).append("\n");
message.append(Text.literal("/clan leave").formatted(Formatting.YELLOW)).append(" - Leaves your current clan. If you are the last remaining member, it disbands the clan as well").formatted(Formatting.GRAY).append("\n");
message.append(Text.literal("[Management Help Page]")
.styled(style -> style
.withColor(Formatting.GOLD)
.withClickEvent(new ClickEvent.RunCommand("/clan help manage"))
.withHoverEvent(new HoverEvent.ShowText(Text.literal("Click to view management commands"))))
);
message.append(Text.literal(" | ").formatted(Formatting.GRAY));
message.append(Text.literal("[Admin Help Page]")
.styled(style -> style
.withColor(Formatting.GOLD)
.withClickEvent(new ClickEvent.RunCommand("/clan help admin"))
.withHoverEvent(new HoverEvent.ShowText(Text.literal("Click to view admin commands"))))
);
message.append("\n");
context.getSource().sendMessage(message);
return 1;
}

// HELP PAGE FOR MANAGEMENT COMMANDS
private int executeManage(CommandContext<ServerCommandSource> context) {
if (cooldown(context.getSource())) return 0;
MutableText message = Text.empty();

message.append(Text.literal("[ Management Commands ]").formatted(Formatting.WHITE)).append("\n");
message.append(Text.literal("/clan invite <playerName>").formatted(Formatting.YELLOW)).append(" - Invites <playerName> to your clan").formatted(Formatting.GRAY).append("\n");
message.append(Text.literal("/clan kick <playerName>").formatted(Formatting.YELLOW)).append(" - Kicks <playerName> from your clan").formatted(Formatting.GRAY).append("\n");
message.append(Text.literal("/clan promote <playerName>").formatted(Formatting.YELLOW)).append(" - Promotes a player in the clan rank hierarchy").formatted(Formatting.GRAY).append("\n");
message.append(Text.literal("/clan demote <playerName>").formatted(Formatting.YELLOW)).append(" - Demotes a player in the clan rank hierarchy").formatted(Formatting.GRAY).append("\n");
message.append(Text.literal("/clan disband [confirm]").formatted(Formatting.YELLOW)).append(" - Facilitates the deletion of your clan").formatted(Formatting.GRAY).append("\n");
message.append(Text.literal("/clan set color <colorName|hexCode>").formatted(Formatting.YELLOW)).append(" - Sets your clan color (supports \"WHITE\" or #FFFFFF or FFFFFF formats)").formatted(Formatting.GRAY).append("\n");
message.append(Text.literal("/clan set access <open|invite_only|toggle>").formatted(Formatting.YELLOW)).append(" - Sets your clan access to open or invite only").formatted(Formatting.GRAY).append("\n");
message.append(Text.literal("/clan set name <newClanName>").formatted(Formatting.YELLOW)).append(" - Sets your clan name to <newClanName> if it is available").formatted(Formatting.GRAY).append("\n");
message.append(Text.literal("/clan transfer <playerName>").formatted(Formatting.YELLOW)).append(" - Transfers clan ownership to <playerName>").formatted(Formatting.GRAY).append("\n");

message.append(Text.literal("[General Help Page]")
.styled(style -> style
.withColor(Formatting.GOLD)
.withClickEvent(new ClickEvent.RunCommand("/clan help general"))
.withHoverEvent(new HoverEvent.ShowText(Text.literal("Click to view general commands"))))
);
message.append(Text.literal(" | ").formatted(Formatting.GRAY));
message.append(Text.literal("[Admin Help Page]")
.styled(style -> style
.withColor(Formatting.GOLD)
.withClickEvent(new ClickEvent.RunCommand("/clan help admin"))
.withHoverEvent(new HoverEvent.ShowText(Text.literal("Click to view admin commands"))))
);
message.append("\n");
context.getSource().sendMessage(message);
return 1;
}

// HELP PAGE FOR ADMIN COMMANDS
private int executeAdmin(CommandContext<ServerCommandSource> context) {
if (cooldown(context.getSource())) return 0;
MutableText message = Text.empty();

message.append(Text.literal("[ Admin Commands ]").formatted(Formatting.WHITE)).append("\n");
message.append(Text.literal("/clan admin add <playerName> <clanName>").formatted(Formatting.YELLOW)).append(" - Adds <playerName> to a clan").formatted(Formatting.GRAY).append("\n");
message.append(Text.literal("/clan admin remove <playerName> <clanName>").formatted(Formatting.YELLOW)).append(" - Removes <playerName> from a clan").formatted(Formatting.GRAY).append("\n");
message.append(Text.literal("/clan admin delete <clanName>").formatted(Formatting.YELLOW)).append(" - Deletes a clan").formatted(Formatting.GRAY).append("\n");
message.append(Text.literal("/clan admin rename <\"clanName\"> <newClanName>").formatted(Formatting.YELLOW)).append(" - Renames a clan (Overrides length limitations)").formatted(Formatting.GRAY).append("\n");
message.append(Text.literal("/clan admin transfer <playerName> <clanName>").formatted(Formatting.YELLOW)).append(" - Transfers clan ownership to <playerName>").formatted(Formatting.GRAY).append("\n");
message.append(Text.literal("/clan admin reload").formatted(Formatting.YELLOW)).append(" - Reloads clans.json from disk").formatted(Formatting.GRAY).append("\n");
message.append(Text.literal("/clan admin cache clear").formatted(Formatting.YELLOW)).append(" - Clears the MojangAPI Cache").formatted(Formatting.GRAY).append("\n");

message.append(Text.literal("[General Help Page]")
.styled(style -> style
.withColor(Formatting.GOLD)
.withClickEvent(new ClickEvent.RunCommand("/clan help general"))
.withHoverEvent(new HoverEvent.ShowText(Text.literal("Click to view general commands"))))
);
message.append(Text.literal(" | ").formatted(Formatting.GRAY));
message.append(Text.literal("[Management Help Page]")
.styled(style -> style
.withColor(Formatting.GOLD)
.withClickEvent(new ClickEvent.RunCommand("/clan help manage"))
.withHoverEvent(new HoverEvent.ShowText(Text.literal("Click to view management commands"))))
);
message.append("\n");
context.getSource().sendMessage(message);
return 1;
}

}
Loading
Loading