add guild databasing and caching + guild creation and information cmds

This commit is contained in:
Taah 2022-05-05 19:28:39 -07:00
parent 21ddd9d361
commit 2398d44074
16 changed files with 545 additions and 66 deletions

View File

@ -19,12 +19,14 @@ dependencies {
compileOnly("org.projectlombok:lombok:1.18.22")
annotationProcessor("org.projectlombok:lombok:1.18.22")
compileOnly("io.papermc.paper:paper-api:1.18.2-R0.1-SNAPSHOT")
compileOnly("dev.plex:Plex:0.9")
compileOnly("dev.plex:server:1.1-SNAPSHOT")
compileOnly("dev.morphia.morphia:morphia-core:2.2.6")
compileOnly("org.json:json:20220320")
}
group = "dev.plex"
version = "1.0"
description = "ExampleModule"
description = "The guilds module for Plex"
java {
toolchain.languageVersion.set(JavaLanguageVersion.of(17))
@ -39,7 +41,7 @@ publishing {
}
tasks.getByName<Jar>("jar") {
archiveBaseName.set("Plex-ExampleModule")
archiveBaseName.set("Plex-Guilds")
archiveVersion.set("")
}

View File

@ -1,2 +1,2 @@
rootProject.name = "plexmodule-template"
rootProject.name = "module-guilds"

View File

@ -1,21 +0,0 @@
package dev.plex;
import dev.plex.command.ExampleCommand;
import dev.plex.listener.ExampleListener;
import dev.plex.module.PlexModule;
public class ExampleModule extends PlexModule
{
@Override
public void enable()
{
registerCommand(new ExampleCommand());
registerListener(new ExampleListener());
}
@Override
public void disable()
{
// Unregistering listeners / commands is handled by Plex
}
}

View File

@ -0,0 +1,90 @@
package dev.plex;
import dev.plex.command.GuildCommand;
import dev.plex.data.SQLGuildManager;
import dev.plex.data.SQLManager;
import dev.plex.guild.Guild;
import dev.plex.guild.GuildHolder;
import dev.plex.module.PlexModule;
import dev.plex.storage.StorageType;
import dev.plex.util.PlexLog;
import lombok.Getter;
import java.util.Arrays;
//TODO: Implement mongodb
@Getter
public class Guilds extends PlexModule
{
private static Guilds module;
private GuildHolder guildHolder = new GuildHolder();
private SQLGuildManager sqlGuildManager;
@Override
public void load()
{
module = this;
}
@Override
public void enable()
{
if (getPlex().getStorageType() == StorageType.MONGODB)
{
getPlex().getMongoConnection().getDatastore().getMapper().map(Guild.class);
getPlex().getMongoConnection().getDatastore().ensureIndexes();
} else {
SQLManager.makeTables();
sqlGuildManager = new SQLGuildManager();
sqlGuildManager.getGuilds().whenComplete((guilds, throwable) -> {
PlexLog.debug("Finished loading {0} guilds", guilds.size());
guilds.forEach(guildHolder::addGuild);
this.registerCommand(new GuildCommand());
});
}
this.addDefaultMessage("guildsHelpCommand", "<gradient:gold:yellow>======</gradient>Guild Menu<gradient:gold:yellow>======</gradient><newline>\n" +
"<newline><gold>/guild <gray>Returns this menu\n" +
"<newline><gold>/guild help");
this.addDefaultMessage("guildCommandNotFound", "<red>'{0}'<gold> is not a valid sub command!", "{0} - The sub command");
this.addDefaultMessage("guildNotFound", "<red>You're currently not a part of a guild!");
this.addDefaultMessage("alreadyInGuild", "<red>You're currently in a guild. Please do <gold>/guild leave<red> if you're a member, or if you're an owner with members, <gold>/guild promote <player><red> then <gold>/guild leave<red>, or just an owner, <gold>/guild disband<red>.");
}
@Override
public void disable()
{
// Unregistering listeners / commands is handled by Plex
}
private void addDefaultMessage(String message, Object initValue)
{
if (Plex.get().messages.getString(message) == null)
{
Plex.get().messages.set(message, initValue);
Plex.get().messages.save();
PlexLog.debug("'{0}' message added from TFMExtras module", message);
}
}
private void addDefaultMessage(String message, Object initValue, String... comments)
{
if (Plex.get().messages.getString(message) == null)
{
Plex.get().messages.set(message, initValue);
Plex.get().messages.save();
Plex.get().messages.setComments(message, Arrays.asList(comments));
Plex.get().messages.save();
PlexLog.debug("'{0}' message added from Plex-Guilds module", message);
}
}
public static Guilds get()
{
return module;
}
}

View File

@ -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");
}
}

View 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();
}
}

View 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;
}
}

View 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;
}
}

View File

@ -0,0 +1,91 @@
package dev.plex.data;
import com.google.common.collect.Lists;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import dev.plex.Plex;
import dev.plex.guild.Guild;
import dev.plex.util.CustomLocation;
import dev.plex.util.GuildUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
public class SQLGuildManager
{
private static final Gson GSON = new Gson();
private static final String SELECT_GUILD = "SELECT * FROM `guilds`";
private static final String SELECT_GUILD_OWNER = "SELECT * FROM `guilds` WHERE owner=?";
private static final String SELECT_GUILD_MEMBER_MYSQL = "SELECT * FROM `guilds` WHERE json_search(members, ?, ?) IS NOT NULL LIMIT 1";
private static final String INSERT_GUILD = "INSERT INTO `guilds` (`name`, `owner`, `createdAt`, `members`, `moderators`, `prefix`, `motd`, `home`, `tagEnabled`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
private static final String DELETE_GUILD = "DELETE FROM `guilds` WHERE owner=?";
public CompletableFuture<Guild> insertGuild(Guild guild)
{
return CompletableFuture.supplyAsync(() ->
{
try (Connection connection = Plex.get().getSqlConnection().getCon())
{
PreparedStatement statement = connection.prepareStatement(INSERT_GUILD);
statement.setString(1, guild.getName());
statement.setString(2, guild.getOwner().toString());
statement.setLong(3, guild.getCreatedAt().toInstant().toEpochMilli());
statement.setString(4, GSON.toJson(guild.getMembers().stream().map(UUID::toString).collect(Collectors.toList())));
statement.setString(5, GSON.toJson(guild.getModerators().stream().map(UUID::toString).collect(Collectors.toList())));
statement.setString(6, guild.getPrefix());
statement.setString(7, guild.getMotd());
statement.setString(8, GSON.toJson(guild.getHome()));
statement.setBoolean(9, guild.isTagEnabled());
statement.execute();
return guild;
} catch (SQLException e)
{
GuildUtil.throwExceptionSync(e);
return null;
}
});
}
public CompletableFuture<List<Guild>> getGuilds()
{
return CompletableFuture.supplyAsync(() ->
{
List<Guild> guilds = Lists.newArrayList();
try (Connection connection = Plex.get().getSqlConnection().getCon())
{
PreparedStatement statement = connection.prepareStatement(SELECT_GUILD);
ResultSet set = statement.executeQuery();
while (set.next())
{
Guild guild = new Guild(
set.getString("name"),
UUID.fromString(set.getString("owner")),
ZonedDateTime.ofInstant(Instant.ofEpochMilli(set.getLong("createdAt")), ZoneId.of(Plex.get().config.getString("server.timezone")).getRules().getOffset(Instant.now())));
guild.getMembers().addAll(new Gson().fromJson(set.getString("members"), new TypeToken<List<String>>(){}.getType()));
guild.getModerators().addAll(new Gson().fromJson(set.getString("moderators"), new TypeToken<List<String>>(){}.getType()));
guild.setPrefix(set.getString("prefix"));
guild.setMotd(set.getString("motd"));
guild.setHome(GSON.fromJson(set.getString("home"), CustomLocation.class));
guild.setTagEnabled(set.getBoolean("tagEnabled"));
guilds.add(guild);
}
} catch (SQLException e)
{
GuildUtil.throwExceptionSync(e);
}
return guilds;
});
}
}

View File

@ -0,0 +1,39 @@
package dev.plex.data;
import dev.plex.Plex;
import dev.plex.storage.StorageType;
import java.sql.Connection;
import java.sql.SQLException;
public class SQLManager
{
public static void makeTables()
{
if (Plex.get().getStorageType() == StorageType.MONGODB)
{
return;
}
try (Connection connection = Plex.get().getSqlConnection().getCon())
{
connection.prepareStatement(
"CREATE TABLE IF NOT EXISTS `guilds` (" +
"`name` VARCHAR(2000) NOT NULL, " +
"`owner` VARCHAR(46) NOT NULL, " +
"`createdAt` BIGINT NOT NULL, " +
"`prefix` VARCHAR(2000), " +
"`motd` VARCHAR(3000), " +
"`home` VARCHAR(1000)," +
"`members` LONGTEXT, " +
"`moderators` LONGTEXT, " +
"`tagEnabled` BOOLEAN" +
");"
).execute();
} catch (SQLException e)
{
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,39 @@
package dev.plex.guild;
import com.google.common.collect.Lists;
import dev.morphia.annotations.Entity;
import dev.morphia.annotations.Transient;
import dev.plex.Plex;
import dev.plex.util.CustomLocation;
import lombok.Data;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.json.JSONObject;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.UUID;
@Data
@Entity("guilds")
public class Guild
{
private final String name;
private final UUID owner;
private final ZonedDateTime createdAt;
@Transient
private final List<UUID> outgoingInvitations = Lists.newArrayList();
private final List<UUID> members = Lists.newArrayList();
private final List<UUID> moderators = Lists.newArrayList();
private String prefix;
private String motd;
private CustomLocation home;
private boolean tagEnabled;
public static Guild create(Player player, String guildName)
{
return new Guild(guildName, player.getUniqueId(), ZonedDateTime.now(ZoneId.of(Plex.get().config.getString("server.timezone"))));
}
}

View File

@ -0,0 +1,29 @@
package dev.plex.guild;
import com.google.common.collect.Lists;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
public class GuildHolder
{
private static final List<Guild> GUILDS = Lists.newArrayList();
public void addGuild(Guild guild)
{
GUILDS.add(guild);
}
public void deleteGuild(UUID owner)
{
GUILDS.removeIf(guild -> guild.getOwner().equals(owner));
}
public Optional<Guild> getGuild(UUID uuid)
{
return GUILDS.stream().filter(guild -> guild.getOwner().equals(uuid) || guild.getMembers().contains(uuid)).findFirst();
}
}

View File

@ -1,17 +0,0 @@
package dev.plex.listener;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerJoinEvent;
public class ExampleListener extends PlexListener
{
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event)
{
Player player = event.getPlayer();
player.sendMessage(Component.text("This is a message from Plex's example module!").color(NamedTextColor.GOLD));
}
}

View File

@ -0,0 +1,7 @@
package dev.plex.util;
import lombok.Data;
public record CustomLocation(String worldName, double x, double y, double z, float yaw, float pitch)
{
}

View File

@ -0,0 +1,14 @@
package dev.plex.util;
import dev.plex.Plex;
import org.bukkit.Bukkit;
public class GuildUtil
{
public static void throwExceptionSync(Throwable throwable)
{
Bukkit.getScheduler().runTask(Plex.get(), () -> throwable.printStackTrace());
}
}

View File

@ -1,4 +1,4 @@
name: ExampleModule
main: dev.plex.ExampleModule
description: An example module for Plex
name: Plex-Guilds
main: dev.plex.Guilds
description: The guilds module for Plex
version: 1.0