mirror of
https://github.com/plexusorg/Module-Guilds.git
synced 2025-07-01 23:16:42 +00:00
add guild databasing and caching + guild creation and information cmds
This commit is contained in:
@ -1,21 +0,0 @@
|
||||
package dev.plex.command;
|
||||
|
||||
import dev.plex.command.annotation.CommandParameters;
|
||||
import dev.plex.command.annotation.CommandPermissions;
|
||||
import dev.plex.rank.enums.Rank;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@CommandParameters(name = "examplemodule", description = "An example command provided by Plex's example module")
|
||||
@CommandPermissions(level = Rank.OP, permission = "plex.module.command")
|
||||
public class ExampleCommand extends PlexCommand
|
||||
{
|
||||
@Override
|
||||
protected Component execute(@NotNull CommandSender commandSender, @Nullable Player player, @NotNull String[] strings)
|
||||
{
|
||||
return Component.text("Example module command");
|
||||
}
|
||||
}
|
126
src/main/java/dev/plex/command/GuildCommand.java
Normal file
126
src/main/java/dev/plex/command/GuildCommand.java
Normal file
@ -0,0 +1,126 @@
|
||||
package dev.plex.command;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Lists;
|
||||
import dev.plex.command.annotation.CommandParameters;
|
||||
import dev.plex.command.annotation.CommandPermissions;
|
||||
import dev.plex.command.source.RequiredCommandSource;
|
||||
import dev.plex.command.sub.CreateSubCommand;
|
||||
import dev.plex.command.sub.InfoSubCommand;
|
||||
import dev.plex.rank.enums.Rank;
|
||||
import dev.plex.util.GuildUtil;
|
||||
import dev.plex.util.PlexLog;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.ConsoleCommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
@CommandParameters(name = "guild", description = "Guild menu", aliases = "guilds")
|
||||
@CommandPermissions(level = Rank.OP, permission = "plex.guilds.guild")
|
||||
public class GuildCommand extends PlexCommand
|
||||
{
|
||||
private final List<PlexCommand> subCommands = Lists.newArrayList();
|
||||
|
||||
public GuildCommand()
|
||||
{
|
||||
System.out.println("Aaa");
|
||||
PlexLog.log("Test");
|
||||
try
|
||||
{
|
||||
this.registerSubCommand(new CreateSubCommand());
|
||||
this.registerSubCommand(new InfoSubCommand());
|
||||
} catch (Exception e)
|
||||
{
|
||||
GuildUtil.throwExceptionSync(e);
|
||||
}
|
||||
PlexLog.log("Registered");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Component execute(@NotNull CommandSender commandSender, @Nullable Player player, @NotNull String[] args)
|
||||
{
|
||||
if (args.length == 0)
|
||||
{
|
||||
return messageComponent("guildsHelpCommand");
|
||||
}
|
||||
PlexCommand subCommand = getSubCommand(args[0]);
|
||||
if (subCommand == null)
|
||||
{
|
||||
return messageComponent("guildCommandNotFound", args[0]);
|
||||
}
|
||||
|
||||
CommandPermissions permissions = subCommand.getClass().getDeclaredAnnotation(CommandPermissions.class);
|
||||
if (permissions.source() == RequiredCommandSource.CONSOLE && commandSender instanceof Player)
|
||||
{
|
||||
return messageComponent("noPermissionInGame");
|
||||
}
|
||||
|
||||
if (permissions.source() == RequiredCommandSource.IN_GAME && commandSender instanceof ConsoleCommandSender)
|
||||
{
|
||||
return messageComponent("noPermissionConsole");
|
||||
}
|
||||
|
||||
checkRank(player, permissions.level(), permissions.permission());
|
||||
|
||||
return subCommand.execute(commandSender, player, Arrays.copyOfRange(args, 1, args.length));
|
||||
}
|
||||
|
||||
private PlexCommand getSubCommand(String label)
|
||||
{
|
||||
return subCommands.stream().filter(cmd ->
|
||||
{
|
||||
CommandParameters commandParameters = cmd.getClass().getDeclaredAnnotation(CommandParameters.class);
|
||||
return commandParameters.name().equalsIgnoreCase(label) || Arrays.stream(commandParameters.aliases().split(",")).anyMatch(s -> s.equalsIgnoreCase(label));
|
||||
}).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
private void registerSubCommand(PlexCommand subCommand)
|
||||
{
|
||||
if (!subCommand.getClass().isAnnotationPresent(CommandPermissions.class))
|
||||
{
|
||||
throw new RuntimeException("CommandPermissions annotation for guild sub command " + subCommand.getName() + " could not be found!");
|
||||
}
|
||||
|
||||
if (!subCommand.getClass().isAnnotationPresent(CommandParameters.class))
|
||||
{
|
||||
throw new RuntimeException("CommandParameters annotation for guild sub command " + subCommand.getName() + " could not be found!");
|
||||
}
|
||||
this.subCommands.add(subCommand);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException
|
||||
{
|
||||
if (args.length == 1)
|
||||
{
|
||||
List<String> possibleCommands = Lists.newArrayList();
|
||||
if (!args[0].isEmpty())
|
||||
{
|
||||
subCommands.forEach(plexCommand ->
|
||||
{
|
||||
plexCommand.getAliases().stream().filter(s -> s.toLowerCase(Locale.ROOT).startsWith(args[0].toLowerCase(Locale.ROOT))).forEach(possibleCommands::add);
|
||||
if (plexCommand.getName().toLowerCase(Locale.ROOT).startsWith(args[0].toLowerCase(Locale.ROOT)))
|
||||
{
|
||||
possibleCommands.add(plexCommand.getName());
|
||||
}
|
||||
});
|
||||
}
|
||||
return possibleCommands;
|
||||
}
|
||||
if (args.length == 2)
|
||||
{
|
||||
PlexCommand subCommand = getSubCommand(args[0]);
|
||||
if (subCommand != null)
|
||||
{
|
||||
return subCommand.tabComplete(sender, alias, Arrays.copyOfRange(args, 1, args.length));
|
||||
}
|
||||
}
|
||||
return ImmutableList.of();
|
||||
}
|
||||
}
|
43
src/main/java/dev/plex/command/sub/CreateSubCommand.java
Normal file
43
src/main/java/dev/plex/command/sub/CreateSubCommand.java
Normal file
@ -0,0 +1,43 @@
|
||||
package dev.plex.command.sub;
|
||||
|
||||
import dev.plex.Guilds;
|
||||
import dev.plex.command.PlexCommand;
|
||||
import dev.plex.command.annotation.CommandParameters;
|
||||
import dev.plex.command.annotation.CommandPermissions;
|
||||
import dev.plex.command.source.RequiredCommandSource;
|
||||
import dev.plex.guild.Guild;
|
||||
import dev.plex.rank.enums.Rank;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@CommandParameters(name = "create", aliases = "make", usage = "/guild <command> <name>")
|
||||
@CommandPermissions(level = Rank.OP, source = RequiredCommandSource.IN_GAME, permission = "plex.guilds.create")
|
||||
public class CreateSubCommand extends PlexCommand
|
||||
{
|
||||
public CreateSubCommand()
|
||||
{
|
||||
super(false);
|
||||
}
|
||||
@Override
|
||||
protected Component execute(@NotNull CommandSender commandSender, @Nullable Player player, @NotNull String[] args)
|
||||
{
|
||||
if (args.length == 0)
|
||||
{
|
||||
return usage();
|
||||
}
|
||||
assert player != null;
|
||||
if (Guilds.get().getGuildHolder().getGuild(player.getUniqueId()).isPresent())
|
||||
{
|
||||
return messageComponent("alreadyInGuild");
|
||||
}
|
||||
Guilds.get().getSqlGuildManager().insertGuild(Guild.create(player, StringUtils.join(args, " "))).whenComplete((guild, throwable) -> {
|
||||
Guilds.get().getGuildHolder().addGuild(guild);
|
||||
send(player, mmString("Created guild named " + guild.getName()));
|
||||
});
|
||||
return null;
|
||||
}
|
||||
}
|
58
src/main/java/dev/plex/command/sub/InfoSubCommand.java
Normal file
58
src/main/java/dev/plex/command/sub/InfoSubCommand.java
Normal file
@ -0,0 +1,58 @@
|
||||
package dev.plex.command.sub;
|
||||
|
||||
import dev.plex.Guilds;
|
||||
import dev.plex.cache.DataUtils;
|
||||
import dev.plex.command.PlexCommand;
|
||||
import dev.plex.command.annotation.CommandParameters;
|
||||
import dev.plex.command.annotation.CommandPermissions;
|
||||
import dev.plex.command.source.RequiredCommandSource;
|
||||
import dev.plex.rank.enums.Rank;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@CommandParameters(name = "info", aliases = "information", usage = "/guild <command>")
|
||||
@CommandPermissions(level = Rank.OP, source = RequiredCommandSource.IN_GAME, permission = "plex.guilds.info")
|
||||
public class InfoSubCommand extends PlexCommand
|
||||
{
|
||||
public InfoSubCommand()
|
||||
{
|
||||
super(false);
|
||||
}
|
||||
|
||||
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm:ss a");
|
||||
@Override
|
||||
protected Component execute(@NotNull CommandSender commandSender, @Nullable Player player, @NotNull String[] strings)
|
||||
{
|
||||
assert player != null;
|
||||
CompletableFuture.runAsync(() ->
|
||||
{
|
||||
Guilds.get().getGuildHolder().getGuild(player.getUniqueId()).ifPresentOrElse(guild ->
|
||||
{
|
||||
send(player, mmString("<gradient:yellow:gold>====<aqua>" + guild.getName() + "<gradient:yellow:gold>===="));
|
||||
send(player, mmString(""));
|
||||
try
|
||||
{
|
||||
send(player, mmString("<gold>Owner: <yellow>" + DataUtils.getPlayer(guild.getOwner(), false).getName()));
|
||||
} catch (NullPointerException e)
|
||||
{
|
||||
send(player, mmString("<gold>Owner: <yellow>Unable to load cache..."));
|
||||
}
|
||||
send(player, mmString("<gold>Members (" + guild.getMembers().size() + "): " + StringUtils.join(guild.getMembers().stream().map(uuid -> DataUtils.getPlayer(uuid, false).getName()).toList(), ", ")));
|
||||
send(player, mmString("<gold>Moderators (" + guild.getModerators().size() + "): " + StringUtils.join(guild.getModerators().stream().map(uuid -> DataUtils.getPlayer(uuid, false).getName()).toList(), ", ")));
|
||||
send(player, mmString("<gold>Prefix: " + (guild.getPrefix() == null ? "N/A" : guild.getPrefix())));
|
||||
send(player, mmString("<gold>Created At: " + formatter.format(guild.getCreatedAt())));
|
||||
}, () ->
|
||||
{
|
||||
send(player, messageComponent("guildNotFound"));
|
||||
});
|
||||
});
|
||||
return null;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user