feat: discord -> teamchat linking, custom join/leave messages, and /team home command
This commit is contained in:
parent
130e6de1d1
commit
26971e75bb
12 changed files with 531 additions and 59 deletions
Binary file not shown.
|
@ -33,6 +33,8 @@ tasks {
|
|||
|
||||
dependencies {
|
||||
compileOnly("io.papermc.paper:paper-api:1.21.4-R0.1-SNAPSHOT")
|
||||
implementation("club.minnced:discord-webhooks:0.8.4")
|
||||
implementation("net.dv8tion:JDA:5.3.2")
|
||||
implementation("com.j256.ormlite", "ormlite-jdbc", "6.1")
|
||||
implementation("org.postgresql", "postgresql", "42.7.5")
|
||||
library("com.google.code.gson", "gson", "2.10.1") // All platform plugins
|
||||
|
|
|
@ -1,18 +1,72 @@
|
|||
package rip.iwakura.civil;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents;
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import net.dv8tion.jda.api.JDABuilder;
|
||||
import net.dv8tion.jda.api.entities.User;
|
||||
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
|
||||
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder;
|
||||
import net.dv8tion.jda.api.utils.messages.MessageCreateData;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.format.TextDecoration;
|
||||
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
|
||||
import rip.iwakura.civil.events.ChatHandler;
|
||||
import rip.iwakura.civil.events.TeamChatHandler;
|
||||
import rip.iwakura.civil.commands.Team;
|
||||
import rip.iwakura.civil.events.JoinHandler;
|
||||
import rip.iwakura.civil.types.CivilPlayer;
|
||||
|
||||
public class Core extends JavaPlugin {
|
||||
public Database database;
|
||||
public Discord discord;
|
||||
|
||||
public java.util.List<Player> teamChatToggled;
|
||||
|
||||
public Component renderTeamChat(CivilPlayer p, Component message) {
|
||||
return Component.text("TEAM")
|
||||
.color(NamedTextColor.BLUE)
|
||||
.decoration(TextDecoration.BOLD, true)
|
||||
.appendSpace()
|
||||
.append(Component.text(p.getName())
|
||||
.append(Component.text(": "))
|
||||
.append(message)
|
||||
.color(NamedTextColor.WHITE).decoration(TextDecoration.BOLD, false));
|
||||
}
|
||||
|
||||
public Component renderTeamChat(User author, String message) {
|
||||
return Component.text("TEAM")
|
||||
.color(NamedTextColor.BLUE)
|
||||
.decoration(TextDecoration.BOLD, true)
|
||||
.append(Component.text(" Discord", NamedTextColor.BLUE).decoration(TextDecoration.BOLD, false)
|
||||
.appendSpace()
|
||||
.append(Component.text(author.getName())
|
||||
.append(Component.text(": "))
|
||||
.append(Component.text(message))
|
||||
.color(NamedTextColor.WHITE).decoration(TextDecoration.BOLD, false)));
|
||||
}
|
||||
|
||||
|
||||
public void sendTeamMessage(CivilPlayer author, Component message) throws SQLException {
|
||||
Component deserializedMessage = renderTeamChat(author, message);
|
||||
discord.sendMessage(author, PlainTextComponentSerializer.plainText().serialize(message));
|
||||
for (CivilPlayer member : database.getAllPlayers(author.getTeam())) {
|
||||
Player player = Bukkit.getPlayer(member.getName());
|
||||
|
||||
if (!player.isOnline())
|
||||
continue;
|
||||
|
||||
player.sendMessage(deserializedMessage);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
|
@ -20,16 +74,19 @@ public class Core extends JavaPlugin {
|
|||
|
||||
saveDefaultConfig();
|
||||
|
||||
teamChatToggled = new ArrayList<>();
|
||||
|
||||
try {
|
||||
this.database = new Database(getConfig().getString("database.url"));
|
||||
this.database = new Database(getConfig().getString("database.url"), this);
|
||||
} catch (SQLException e) {
|
||||
getLogger().log(Level.SEVERE, e.getMessage());
|
||||
Bukkit.getPluginManager().disablePlugin(this);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Team teamCommand = new Team(database);
|
||||
this.discord = new Discord(this);
|
||||
discord.connect(getConfig().getString("discord.token"));
|
||||
|
||||
Team teamCommand = new Team(this);
|
||||
|
||||
this.getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS, commands -> {
|
||||
commands.registrar().register(teamCommand.getCommand());
|
||||
|
@ -37,5 +94,6 @@ public class Core extends JavaPlugin {
|
|||
|
||||
getServer().getPluginManager().registerEvents(new JoinHandler(database), this);
|
||||
getServer().getPluginManager().registerEvents(new ChatHandler(this), this);
|
||||
getServer().getPluginManager().registerEvents(new TeamChatHandler(this), this);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,17 +4,17 @@ import java.sql.SQLException;
|
|||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.j256.ormlite.dao.Dao;
|
||||
import com.j256.ormlite.dao.DaoManager;
|
||||
import com.j256.ormlite.jdbc.JdbcConnectionSource;
|
||||
import com.j256.ormlite.logger.LoggerFactory;
|
||||
import com.j256.ormlite.support.ConnectionSource;
|
||||
import com.j256.ormlite.table.TableUtils;
|
||||
|
||||
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
|
||||
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
|
||||
import rip.iwakura.civil.exceptions.InvalidChannelException;
|
||||
import rip.iwakura.civil.types.CivilPlayer;
|
||||
import rip.iwakura.civil.types.CivilTeam;
|
||||
|
||||
|
@ -22,28 +22,27 @@ public class Database {
|
|||
private final Dao<CivilPlayer, UUID> playerDao;
|
||||
private final Dao<CivilTeam, String> teamDao;
|
||||
private ConnectionSource connectionSource;
|
||||
private Core core;
|
||||
|
||||
public Database(String uri) throws SQLException {
|
||||
public Database(String uri, Core core) throws SQLException {
|
||||
connectionSource = new JdbcConnectionSource(uri);
|
||||
|
||||
|
||||
TableUtils.createTableIfNotExists(connectionSource, CivilPlayer.class);
|
||||
TableUtils.createTableIfNotExists(connectionSource, CivilTeam.class);
|
||||
|
||||
playerDao = DaoManager.createDao(connectionSource, CivilPlayer.class);
|
||||
teamDao = DaoManager.createDao(connectionSource, CivilTeam.class);
|
||||
this.core = core;
|
||||
}
|
||||
|
||||
public void createPlayer(Player p) throws SQLException {
|
||||
playerDao.create(
|
||||
new CivilPlayer(p.getUniqueId(), p.getName())
|
||||
);
|
||||
new CivilPlayer(p.getUniqueId(), p.getName()));
|
||||
}
|
||||
|
||||
public void createTeam(String name, String prefix) throws SQLException {
|
||||
teamDao.create(
|
||||
new CivilTeam(name, prefix)
|
||||
);
|
||||
new CivilTeam(name, prefix));
|
||||
}
|
||||
|
||||
public void joinTeam(Player p, CivilTeam team) throws SQLException {
|
||||
|
@ -65,6 +64,10 @@ public class Database {
|
|||
return teamDao.queryForAll();
|
||||
}
|
||||
|
||||
public List<CivilTeam> getAllTeams(Long channel) throws SQLException {
|
||||
return teamDao.queryBuilder().where().eq("channel", channel).query();
|
||||
}
|
||||
|
||||
public CivilPlayer getPlayer(String name) throws SQLException {
|
||||
return playerDao.queryBuilder().where().eq("name", name).queryForFirst();
|
||||
}
|
||||
|
@ -80,4 +83,20 @@ public class Database {
|
|||
public List<CivilPlayer> getAllPlayers() throws SQLException {
|
||||
return playerDao.queryForAll();
|
||||
}
|
||||
|
||||
public List<CivilPlayer> getAllPlayers(CivilTeam team) throws SQLException {
|
||||
return playerDao.queryBuilder().where().eq("team_id", team.getName()).query();
|
||||
}
|
||||
|
||||
public void setHome(CivilTeam team, Location location) throws SQLException {
|
||||
team.setHome(location);
|
||||
teamDao.update(team);
|
||||
}
|
||||
|
||||
public void linkChannel(CivilTeam team, TextChannel channel) throws SQLException {
|
||||
team.setChannel(channel.getIdLong());
|
||||
|
||||
team.setWebhook(channel.createWebhook("CivilCore").complete().getUrl());
|
||||
teamDao.update(team);
|
||||
}
|
||||
}
|
||||
|
|
104
src/main/java/rip/iwakura/civil/Discord.java
Normal file
104
src/main/java/rip/iwakura/civil/Discord.java
Normal file
|
@ -0,0 +1,104 @@
|
|||
package rip.iwakura.civil;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.mojang.brigadier.arguments.StringArgumentType;
|
||||
|
||||
import club.minnced.discord.webhook.WebhookClient;
|
||||
import club.minnced.discord.webhook.WebhookClientBuilder;
|
||||
import club.minnced.discord.webhook.send.WebhookMessageBuilder;
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import net.dv8tion.jda.api.JDABuilder;
|
||||
import net.dv8tion.jda.api.Permission;
|
||||
import net.dv8tion.jda.api.entities.Guild;
|
||||
import net.dv8tion.jda.api.entities.Webhook;
|
||||
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions;
|
||||
import net.dv8tion.jda.api.interactions.commands.OptionType;
|
||||
import net.dv8tion.jda.api.interactions.commands.build.Commands;
|
||||
import net.dv8tion.jda.api.requests.GatewayIntent;
|
||||
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder;
|
||||
import rip.iwakura.civil.discord.LinkCommand;
|
||||
import rip.iwakura.civil.types.CivilPlayer;
|
||||
import rip.iwakura.civil.types.CivilTeam;
|
||||
|
||||
/**
|
||||
* Discord
|
||||
*/
|
||||
public class Discord extends ListenerAdapter {
|
||||
private Core core;
|
||||
public JDA jda;
|
||||
|
||||
public Discord(Core core) {
|
||||
this.core = core;
|
||||
}
|
||||
|
||||
public void sendMessage(CivilPlayer p, String message) {
|
||||
String webhook = p.getTeam().getWebhook();
|
||||
if (webhook != null) {
|
||||
WebhookClient client = WebhookClient.withUrl(webhook);
|
||||
client.send(new WebhookMessageBuilder().setUsername(p.getName())
|
||||
.setAvatarUrl("https://www.mc-heads.net/avatar/" + p.getName()).setContent(message).build());
|
||||
}
|
||||
}
|
||||
|
||||
public void connect(String token) {
|
||||
jda = JDABuilder.createDefault(token)
|
||||
.enableIntents(GatewayIntent.MESSAGE_CONTENT, GatewayIntent.GUILD_MESSAGES, GatewayIntent.GUILD_MEMBERS)
|
||||
.addEventListeners(this)
|
||||
.addEventListeners(new LinkCommand(core))
|
||||
.build();
|
||||
|
||||
try {
|
||||
jda.awaitReady();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
registerCommands(core.getConfig().getLong("discord.guild"));
|
||||
}
|
||||
|
||||
public void registerCommands(Long guildId) {
|
||||
Guild guild = jda.getGuildById(guildId);
|
||||
|
||||
if (guild == null) {
|
||||
core.getLogger().info("Guild ID not found.");
|
||||
return;
|
||||
}
|
||||
|
||||
guild.updateCommands().addCommands(Commands.slash("link", "Link in-game team chat to a discord channel.")
|
||||
.setDefaultPermissions(DefaultMemberPermissions.enabledFor(Permission.MANAGE_CHANNEL))
|
||||
.addOption(OptionType.STRING, "team", "Name of the team you want to link to the current channel.", true,
|
||||
true))
|
||||
.queue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessageReceived(MessageReceivedEvent event) {
|
||||
if (event.getAuthor().isBot()) return;
|
||||
|
||||
try {
|
||||
List<CivilTeam> linked_teams = core.database.getAllTeams(event.getChannel().getIdLong());
|
||||
|
||||
for (CivilTeam team : linked_teams) {
|
||||
List<CivilPlayer> members = core.database.getAllPlayers(team);
|
||||
|
||||
for (CivilPlayer member : members) {
|
||||
Player player = Bukkit.getPlayerExact(member.getName());
|
||||
|
||||
if (player == null || !player.isOnline()) continue;
|
||||
|
||||
player.sendMessage(core.renderTeamChat(event.getAuthor(),event.getMessage().getContentDisplay()));
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,79 +1,200 @@
|
|||
package rip.iwakura.civil.commands;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.text.Format;
|
||||
|
||||
import javax.xml.crypto.Data;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.mojang.brigadier.Command;
|
||||
import com.mojang.brigadier.arguments.LongArgumentType;
|
||||
import com.mojang.brigadier.arguments.StringArgumentType;
|
||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||
import com.mojang.brigadier.context.CommandContext;
|
||||
import com.mojang.brigadier.tree.LiteralCommandNode;
|
||||
|
||||
import io.papermc.paper.command.brigadier.CommandSourceStack;
|
||||
import io.papermc.paper.command.brigadier.Commands;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.format.TextDecoration;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import rip.iwakura.civil.Core;
|
||||
import rip.iwakura.civil.Database;
|
||||
import rip.iwakura.civil.exceptions.InvalidChannelException;
|
||||
import rip.iwakura.civil.types.*;
|
||||
|
||||
public class Team {
|
||||
private Database database;
|
||||
private Core core;
|
||||
|
||||
public Team(Database database) {
|
||||
this.database = database;
|
||||
public Team(Core core) {
|
||||
this.core = core;
|
||||
this.database = core.database;
|
||||
}
|
||||
|
||||
private LiteralArgumentBuilder<CommandSourceStack> createTeam() {
|
||||
private LiteralArgumentBuilder<CommandSourceStack> createTeam() {
|
||||
return Commands.literal("create")
|
||||
.then(
|
||||
Commands.argument("name", StringArgumentType.string())
|
||||
.then(
|
||||
Commands.argument("prefix", StringArgumentType.string())
|
||||
.requires(source -> source.getSender().hasPermission("civil.team.create"))
|
||||
.then(
|
||||
Commands.argument("name", StringArgumentType.string())
|
||||
.then(
|
||||
Commands.argument("prefix", StringArgumentType.string())
|
||||
.executes(ctx -> {
|
||||
String name = StringArgumentType.getString(ctx, "name");
|
||||
String prefix = StringArgumentType.getString(ctx, "prefix");
|
||||
CommandSender sender = ctx.getSource().getSender();
|
||||
|
||||
try {
|
||||
database.createTeam(name, prefix);
|
||||
sender.sendMessage(
|
||||
Component
|
||||
.text("Successfully created team ",
|
||||
NamedTextColor.GREEN)
|
||||
.append(Component
|
||||
.text(name, NamedTextColor.GOLD))
|
||||
.append(Component.text(" with prefix: \""))
|
||||
.append(MiniMessage.miniMessage()
|
||||
.deserialize(prefix))
|
||||
.append(Component.text("\"")));
|
||||
|
||||
return Command.SINGLE_SUCCESS;
|
||||
} catch (SQLException e) {
|
||||
ctx.getSource().getSender().sendMessage(
|
||||
Component.text(e.getMessage(), NamedTextColor.RED));
|
||||
e.printStackTrace();
|
||||
return 0;
|
||||
}
|
||||
})));
|
||||
}
|
||||
|
||||
// Send team chat message
|
||||
private LiteralArgumentBuilder<CommandSourceStack> homeCommand() {
|
||||
return Commands.literal("home")
|
||||
.requires(sender -> sender.getSender() instanceof Player)
|
||||
.executes(ctx -> {
|
||||
Player p = (Player) ctx.getSource().getSender();
|
||||
|
||||
try {
|
||||
CivilPlayer cPlayer = core.database.getPlayer(p);
|
||||
p.teleport(cPlayer.getTeam().getHome(core));
|
||||
p.sendRichMessage("<green>You have been teleported to your team's home.");
|
||||
return Command.SINGLE_SUCCESS;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}).then(Commands.literal("set")
|
||||
.requires(sender -> sender.getSender() instanceof Player)
|
||||
.executes(ctx -> {
|
||||
String name = StringArgumentType.getString(ctx, "name");
|
||||
String prefix = StringArgumentType.getString(ctx, "prefix");
|
||||
Player p = (Player) ctx.getSource().getSender();
|
||||
|
||||
try {
|
||||
database.createTeam(name, prefix);
|
||||
CivilPlayer cPlayer = database.getPlayer(p);
|
||||
|
||||
Location location = p.getLocation();
|
||||
|
||||
database.setHome(cPlayer.getTeam(), location);
|
||||
|
||||
p.sendRichMessage("<green>Your team's home location has been set to: "
|
||||
+ String.format("<red>%d %d %d", Math.round(location.x()),
|
||||
Math.round(location.y()), Math.round(location.z())));
|
||||
return Command.SINGLE_SUCCESS;
|
||||
|
||||
} catch (SQLException e) {
|
||||
ctx.getSource().getSender().sendMessage(
|
||||
Component.text(e.getMessage(), NamedTextColor.RED));
|
||||
return 0;
|
||||
e.printStackTrace();
|
||||
}
|
||||
})));
|
||||
|
||||
return 1;
|
||||
}));
|
||||
|
||||
}
|
||||
|
||||
// Send team chat message
|
||||
private LiteralArgumentBuilder<CommandSourceStack> teamChat() {
|
||||
return Commands.literal("chat")
|
||||
.requires(sender -> sender.getSender() instanceof Player)
|
||||
.executes(ctx -> {
|
||||
Player p = (Player) ctx.getSource().getSender();
|
||||
|
||||
if (core.teamChatToggled.contains(p)) {
|
||||
core.teamChatToggled.remove(p);
|
||||
p.sendRichMessage("<red>Team chat disabled.");
|
||||
} else {
|
||||
core.teamChatToggled.add(p);
|
||||
p.sendRichMessage("<green>Team chat enabled.");
|
||||
}
|
||||
|
||||
return Command.SINGLE_SUCCESS;
|
||||
}).then(Commands.argument("message", StringArgumentType.greedyString())
|
||||
.requires(sender -> sender.getSender() instanceof Player)
|
||||
.executes(ctx -> {
|
||||
CommandSender sender = ctx.getSource().getSender();
|
||||
String message = StringArgumentType.getString(ctx, "message");
|
||||
|
||||
try {
|
||||
CivilPlayer p = database.getPlayer((Player) sender);
|
||||
|
||||
core.sendTeamMessage(p, message);
|
||||
|
||||
return Command.SINGLE_SUCCESS;
|
||||
|
||||
} catch (SQLException e) {
|
||||
sender.sendRichMessage("<red>Uh oh, something went wrong!");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return 1;
|
||||
}));
|
||||
|
||||
}
|
||||
|
||||
// Add a player to a team.
|
||||
private LiteralArgumentBuilder<CommandSourceStack> joinTeam() {
|
||||
return Commands.literal("add")
|
||||
.then(
|
||||
Commands.argument("player", new PlayerArgument(database))
|
||||
.then(
|
||||
Commands.argument("team", new TeamArgument(database))
|
||||
.executes(ctx -> {
|
||||
CivilTeam civilTeam = ctx.getArgument("team", CivilTeam.class);
|
||||
CivilPlayer civilPlayer = ctx.getArgument("player",
|
||||
CivilPlayer.class);
|
||||
.requires(source -> source.getSender().hasPermission("civil.team.add"))
|
||||
.then(
|
||||
Commands.argument("player", new PlayerArgument(database))
|
||||
.then(
|
||||
Commands.argument("team", new TeamArgument(database))
|
||||
.executes(ctx -> {
|
||||
CivilTeam civilTeam = ctx.getArgument("team", CivilTeam.class);
|
||||
CivilPlayer civilPlayer = ctx.getArgument("player",
|
||||
CivilPlayer.class);
|
||||
CommandSender sender = ctx.getSource().getSender();
|
||||
|
||||
try {
|
||||
database.joinTeam(civilPlayer, civilTeam);
|
||||
return Command.SINGLE_SUCCESS;
|
||||
} catch (SQLException e) {
|
||||
ctx.getSource().getSender().sendMessage(
|
||||
Component.text(e.getMessage(), NamedTextColor.RED));
|
||||
return 0;
|
||||
}
|
||||
})));
|
||||
try {
|
||||
database.joinTeam(civilPlayer, civilTeam);
|
||||
|
||||
sender.sendMessage(
|
||||
Component
|
||||
.text("Successfully added ",
|
||||
NamedTextColor.GREEN)
|
||||
.append(Component
|
||||
.text(civilPlayer.getName(),
|
||||
NamedTextColor.GOLD))
|
||||
.append(Component.text(" to team "))
|
||||
.append(Component
|
||||
.text(civilTeam.getName(),
|
||||
NamedTextColor.AQUA)));
|
||||
return Command.SINGLE_SUCCESS;
|
||||
} catch (SQLException e) {
|
||||
ctx.getSource().getSender().sendMessage(
|
||||
Component.text(e.getMessage(), NamedTextColor.RED));
|
||||
e.printStackTrace();
|
||||
return 0;
|
||||
}
|
||||
})));
|
||||
};
|
||||
|
||||
public LiteralCommandNode<CommandSourceStack> getCommand() {
|
||||
public LiteralCommandNode<CommandSourceStack> getCommand() {
|
||||
return Commands.literal("team")
|
||||
.then(createTeam())
|
||||
.then(joinTeam()).build();
|
||||
.then(createTeam())
|
||||
.then(joinTeam())
|
||||
.then(teamChat())
|
||||
.then(homeCommand())
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
56
src/main/java/rip/iwakura/civil/discord/LinkCommand.java
Normal file
56
src/main/java/rip/iwakura/civil/discord/LinkCommand.java
Normal file
|
@ -0,0 +1,56 @@
|
|||
package rip.iwakura.civil.discord;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.logging.StreamHandler;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
|
||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import net.dv8tion.jda.api.interactions.commands.Command;
|
||||
import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction;
|
||||
import rip.iwakura.civil.Core;
|
||||
import rip.iwakura.civil.types.CivilTeam;
|
||||
|
||||
/**
|
||||
* LinkCommand
|
||||
*/
|
||||
public class LinkCommand extends ListenerAdapter {
|
||||
private Core core;
|
||||
|
||||
public LinkCommand(Core core) {
|
||||
this.core = core;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSlashCommandInteraction(SlashCommandInteractionEvent event) {
|
||||
try {
|
||||
if (event.getName().equals("link")) {
|
||||
core.database.linkChannel(core.database.getTeam(event.getOption("team").getAsString()),event.getChannel().asTextChannel());
|
||||
event.reply("Successfully linked channel to team chat.").queue();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCommandAutoCompleteInteraction(CommandAutoCompleteInteractionEvent event) {
|
||||
try {
|
||||
if (event.getName().equals("link") && event.getFocusedOption().getName().equals("team")) {
|
||||
List<String> teamNames = core.database.getAllTeams().stream().map(CivilTeam::getName).collect(
|
||||
Collectors.toList());
|
||||
|
||||
List<Command.Choice> options = teamNames.stream()
|
||||
.filter(name -> name.startsWith(event.getFocusedOption().getValue()))
|
||||
.map(name -> new Command.Choice(name, name)).collect(Collectors.toList());
|
||||
|
||||
event.replyChoices(options).queue();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -7,6 +7,7 @@ import org.bukkit.event.EventHandler;
|
|||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import rip.iwakura.civil.Database;
|
||||
|
||||
public class JoinHandler implements Listener {
|
||||
|
@ -17,9 +18,13 @@ public class JoinHandler implements Listener {
|
|||
}
|
||||
|
||||
@EventHandler
|
||||
public void chatHandler(PlayerJoinEvent event) throws SQLException {
|
||||
public void joinHandler(PlayerJoinEvent event) throws SQLException {
|
||||
Player p = event.getPlayer();
|
||||
|
||||
if (database.getPlayer(p) == null) database.createPlayer(p);
|
||||
event.joinMessage(MiniMessage.miniMessage()
|
||||
.deserialize("<gray>[<green>+<gray>] <green>" + p.getName() + "<white>"));
|
||||
|
||||
if (database.getPlayer(p) == null)
|
||||
database.createPlayer(p);
|
||||
}
|
||||
}
|
||||
|
|
21
src/main/java/rip/iwakura/civil/events/LeaveHandler.java
Normal file
21
src/main/java/rip/iwakura/civil/events/LeaveHandler.java
Normal file
|
@ -0,0 +1,21 @@
|
|||
package rip.iwakura.civil.events;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
|
||||
public class LeaveHandler implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void leaveHandler(PlayerQuitEvent event) throws SQLException {
|
||||
Player p = event.getPlayer();
|
||||
|
||||
event.quitMessage(MiniMessage.miniMessage()
|
||||
.deserialize("<gray>[<red>-<gray>] <red>" + p.getName() + "<white>"));
|
||||
}
|
||||
}
|
39
src/main/java/rip/iwakura/civil/events/TeamChatHandler.java
Normal file
39
src/main/java/rip/iwakura/civil/events/TeamChatHandler.java
Normal file
|
@ -0,0 +1,39 @@
|
|||
package rip.iwakura.civil.events;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
import io.papermc.paper.chat.ChatRenderer;
|
||||
import io.papermc.paper.event.player.AsyncChatEvent;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
|
||||
import rip.iwakura.civil.Core;
|
||||
import rip.iwakura.civil.Database;
|
||||
import rip.iwakura.civil.types.CivilPlayer;
|
||||
|
||||
public class TeamChatHandler implements Listener {
|
||||
private Core core;
|
||||
private Database database;
|
||||
|
||||
public TeamChatHandler(Core core) {
|
||||
this.core = core;
|
||||
this.database = core.database;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
public void chatHandler(AsyncChatEvent event) throws SQLException {
|
||||
if (core.teamChatToggled.contains(event.getPlayer())) {
|
||||
CivilPlayer p = database.getPlayer(event.getPlayer());
|
||||
|
||||
core.sendTeamMessage(p, MiniMessage.miniMessage()
|
||||
.deserialize(PlainTextComponentSerializer.plainText().serialize(event.message())));
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package rip.iwakura.civil.exceptions;
|
||||
|
||||
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
|
||||
|
||||
/**
|
||||
* InvalidChannelException
|
||||
*/
|
||||
public class InvalidChannelException extends Exception {
|
||||
public InvalidChannelException(Long channelId) {
|
||||
super("Channel with ID: " + channelId.toString() + " could not be found.");
|
||||
}
|
||||
}
|
|
@ -5,6 +5,8 @@ import org.bukkit.Location;
|
|||
import com.j256.ormlite.field.DatabaseField;
|
||||
import com.j256.ormlite.table.DatabaseTable;
|
||||
|
||||
import rip.iwakura.civil.Core;
|
||||
|
||||
@DatabaseTable(tableName = "teams")
|
||||
public class CivilTeam {
|
||||
@DatabaseField(id = true)
|
||||
|
@ -13,8 +15,23 @@ public class CivilTeam {
|
|||
@DatabaseField(canBeNull = false)
|
||||
private String prefix;
|
||||
|
||||
/*@DatabaseField(canBeNull = true)
|
||||
private Location home;*/
|
||||
@DatabaseField(canBeNull = true)
|
||||
private Long channel;
|
||||
|
||||
@DatabaseField(canBeNull = true)
|
||||
private String webhook;
|
||||
|
||||
@DatabaseField(canBeNull = true)
|
||||
private double home_x;
|
||||
|
||||
@DatabaseField(canBeNull = true)
|
||||
private double home_y;
|
||||
|
||||
@DatabaseField(canBeNull = true)
|
||||
private double home_z;
|
||||
|
||||
@DatabaseField(canBeNull = true)
|
||||
private String home_world;
|
||||
|
||||
public CivilTeam() {
|
||||
}
|
||||
|
@ -31,13 +48,31 @@ public class CivilTeam {
|
|||
public String getPrefix() {
|
||||
return prefix;
|
||||
}
|
||||
/*
|
||||
|
||||
public void setHome(Location home) {
|
||||
this.home = home;
|
||||
this.home_x = home.x();
|
||||
this.home_y = home.y();
|
||||
this.home_z = home.z();
|
||||
this.home_world = home.getWorld().getName();
|
||||
}
|
||||
|
||||
public Location getHome() {
|
||||
return home;
|
||||
public Location getHome(Core core) {
|
||||
return new Location(core.getServer().getWorld(home_world), home_x, home_y, home_z);
|
||||
}
|
||||
|
||||
public void setChannel(Long channel) {
|
||||
this.channel = channel;
|
||||
}
|
||||
|
||||
public Long getChannel() {
|
||||
return channel;
|
||||
}
|
||||
|
||||
public void setWebhook(String webhook) {
|
||||
this.webhook = webhook;
|
||||
}
|
||||
|
||||
public String getWebhook() {
|
||||
return webhook;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue