Skip to content

Commit

Permalink
2.2.0: Added Start and Stop - Cleaned up code
Browse files Browse the repository at this point in the history
  • Loading branch information
notTamion committed Jun 6, 2023
1 parent 15dd625 commit 39e54e4
Show file tree
Hide file tree
Showing 17 changed files with 131 additions and 107 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ This is a Minecraft Plugin that combines Discord and Minecraft into one!
- /setdcsyntax {syntax} - Change how messages sent on Minecraft get sent in Discord. - Example Syntax: [MC]{username}: {message}
- /setjoinsyntax {syntax} - Change how join messages get sent in Discord - Example Syntax: [MC]{username} joined the Server!
- /setleavesyntax {syntax} - Change how leave messages get sent in Discord - Example Syntax: [MC]{username} left the Server!
- /setstartsyntax {syntax} - Change how start messages get sent in Discord - Example Syntax: Bot Online!
- /setstopsyntax {syntax} - Change how stop messages get sent in Discord - Example Syntax: Bot Offline!

NOTE: {username} and {message} need to be written in lowercase.
{username} and {message} need to be written in lowercase. If you want to disable a feature just leave the syntax empty
### Channels
Every Channel that has "MCCHAT" in its Description/Topic will act as Bridge for Minecraft and Discord Chat.\
Every Channel that has "MCCONSOLE" in its Description/Topic will act as Minecraft Console. \
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>de.tamion</groupId>
<artifactId>DCChat</artifactId>
<version>2.1.0</version>
<version>2.2.0</version>
<packaging>jar</packaging>

<name>DCChat</name>
Expand Down
25 changes: 18 additions & 7 deletions src/main/java/de/tamion/discord/DCMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import de.tamion.minecraft.listeners.JoinLeave;
import de.tamion.others.DCChatConsoleAppender;
import de.tamion.others.Schedulers;
import de.tamion.others.Utils;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;
Expand All @@ -18,14 +20,15 @@
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.PluginManager;

import java.awt.*;

public class DCMain {

public static JDA jda;
static FileConfiguration config = MCMain.getPlugin().getConfig();
public static void startup() {
try {
boolean startallowed = true;
PluginManager pluginManager = Bukkit.getPluginManager();
JDABuilder jb = JDABuilder.createDefault(config.getString("Bot.token")).setStatus(OnlineStatus.ONLINE).enableIntents(GatewayIntent.MESSAGE_CONTENT);
if(!config.contains("Bot.token") || config.get("Bot.token").equals("TOKEN")) {
MCMain.getPlugin().getLogger().info("Bot token not set. Please set with /setBotToken [Token] or change it directly in the config.yml");
Expand All @@ -45,15 +48,28 @@ public static void startup() {
log.addAppender(new DCChatConsoleAppender());
Schedulers.consolescheduler();
Schedulers.updatesyntaxchannel();
PluginManager pluginManager = Bukkit.getPluginManager();
pluginManager.registerEvents(new Chat(), MCMain.getPlugin());
pluginManager.registerEvents(new JoinLeave(), MCMain.getPlugin());
EmbedBuilder eb = new EmbedBuilder();
eb.setTitle(config.getString("Bot.startsyntax"));
eb.setColor(Color.GREEN);
Utils.sendtochat(eb.build());
Utils.sendtoconsole(eb.build());
MCMain.getPlugin().getLogger().info("Bot started.");
} catch (InvalidTokenException e) {
MCMain.getPlugin().getLogger().info("Invalid Bot token.");
} catch (InterruptedException ignored) {}
} catch (InterruptedException | IllegalArgumentException ignored) {}
}

public static void shutdown() {
try {
EmbedBuilder eb = new EmbedBuilder();
eb.setTitle(config.getString("Bot.stopsyntax"));
eb.setColor(Color.RED);
Utils.sendtochat(eb.build());
Utils.sendtoconsole(eb.build());
} catch (IllegalArgumentException ignored) {}
Logger log = (Logger) LogManager.getRootLogger();
if(log.getAppenders().containsKey("DCChatConsoleAppender")) {
log.removeAppender(log.getAppenders().get("DCChatConsoleAppender"));
Expand All @@ -64,9 +80,4 @@ public static void shutdown() {
MCMain.getPlugin().getLogger().info("Can't stop bot. Bot not running.");
}
}

public static void restart() {
shutdown();
startup();
}
}
3 changes: 3 additions & 0 deletions src/main/java/de/tamion/discord/listeners/DCChat.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public void onMessageReceived(MessageReceivedEvent e) {
if(e.getMessage().getContentDisplay().startsWith("/")) {
return;
}
if(config.getString("Bot.mcsyntax").equals("")) {
return;
}
Bukkit.getServer().broadcastMessage(config.getString("Bot.mcsyntax").replaceAll("\\{username}", e.getMember().getEffectiveName()).replaceAll("\\{message}", msg));
}
}
12 changes: 12 additions & 0 deletions src/main/java/de/tamion/minecraft/MCMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import de.tamion.discord.DCMain;
import de.tamion.minecraft.commands.*;
import de.tamion.others.Utils;
import net.dv8tion.jda.api.EmbedBuilder;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

import java.awt.*;

public final class MCMain extends JavaPlugin {

static MCMain plugin;
Expand All @@ -19,6 +23,8 @@ public void onEnable() {
getCommand("setdcsyntax").setExecutor(new SetDCSyntax());
getCommand("setjoinsyntax").setExecutor(new SetJoinSyntax());
getCommand("setleavesyntax").setExecutor(new SetLeaveSyntax());
getCommand("setstartsyntax").setExecutor(new SetStartSyntax());
getCommand("setstopsyntax").setExecutor(new SetStopSyntax());
getCommand("setguildid").setExecutor(new SetGuildID());
}

Expand All @@ -45,6 +51,12 @@ public static void defaultconfig() {
if(!config.contains("Bot.leavesyntax")) {
config.set("Bot.leavesyntax", "[Minecraft] {username} left the Server!");
}
if(!config.contains("Bot.startsyntax")) {
config.set("Bot.startsyntax", "Bot started!");
}
if(!config.contains("Bot.stopsyntax")) {
config.set("Bot.stopsyntax", "Bot stopped!");
}
if(!config.contains("Bot.token")) {
config.set("Bot.token", "TOKEN");
}
Expand Down
20 changes: 2 additions & 18 deletions src/main/java/de/tamion/minecraft/commands/SetDCSyntax.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
package de.tamion.minecraft.commands;

import de.tamion.minecraft.MCMain;
import de.tamion.others.Utils;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.jetbrains.annotations.NotNull;

public class SetDCSyntax implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String label, @NotNull String[] args) {
if(!sender.hasPermission("DCChat.syntax") || !sender.hasPermission("DCChat.admin")) {
sender.sendMessage("You are not allowed to execute this Command!");
return false;
}
if(!(args.length >= 1)) {
sender.sendMessage("/setDCSyntax [Syntax]");
return false;
}
FileConfiguration config = MCMain.getPlugin().getConfig();
StringBuilder sb = new StringBuilder();
for(String wor: args) {
sb.append(wor + " ");
}
config.set("Bot.dcsyntax", sb.toString());
MCMain.getPlugin().saveConfig();
sender.sendMessage("Syntax Set!");
sender.sendMessage(Utils.setsyntax(sender, "Bot.dcsyntax", String.join(" ", args)));
return true;
}
}
20 changes: 2 additions & 18 deletions src/main/java/de/tamion/minecraft/commands/SetJoinSyntax.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
package de.tamion.minecraft.commands;

import de.tamion.minecraft.MCMain;
import de.tamion.others.Utils;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.jetbrains.annotations.NotNull;

public class SetJoinSyntax implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String label, @NotNull String[] args) {
if(!sender.hasPermission("DCChat.syntax") || !sender.hasPermission("DCChat.admin")) {
sender.sendMessage("You are not allowed to execute this Command!");
return false;
}
if(!(args.length >= 1)) {
sender.sendMessage("/setJoinSyntax [Syntax]");
return false;
}
FileConfiguration config = MCMain.getPlugin().getConfig();
StringBuilder sb = new StringBuilder();
for(String wor: args) {
sb.append(wor+ " ");
}
config.set("Bot.joinsyntax", sb.toString());
MCMain.getPlugin().saveConfig();
sender.sendMessage("Syntax Set!");
sender.sendMessage(Utils.setsyntax(sender, "Bot.joinsyntax", String.join(" ", args)));
return true;
}
}
20 changes: 2 additions & 18 deletions src/main/java/de/tamion/minecraft/commands/SetLeaveSyntax.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
package de.tamion.minecraft.commands;

import de.tamion.minecraft.MCMain;
import de.tamion.others.Utils;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.jetbrains.annotations.NotNull;

public class SetLeaveSyntax implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String label, @NotNull String[] args) {
if(!sender.hasPermission("DCChat.syntax") || !sender.hasPermission("DCChat.admin")) {
sender.sendMessage("You are not allowed to execute this Command!");
return false;
}
if(!(args.length >= 1)) {
sender.sendMessage("/setLeaveSyntax [Syntax]");
return false;
}
FileConfiguration config = MCMain.getPlugin().getConfig();
StringBuilder sb = new StringBuilder();
for(String wor: args) {
sb.append(wor+ " ");
}
config.set("Bot.leavesyntax", sb.toString());
MCMain.getPlugin().saveConfig();
sender.sendMessage("Syntax Set!");
sender.sendMessage(Utils.setsyntax(sender, "Bot.leavesyntax", String.join(" ", args)));
return true;
}
}
20 changes: 2 additions & 18 deletions src/main/java/de/tamion/minecraft/commands/SetMCSyntax.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
package de.tamion.minecraft.commands;

import de.tamion.minecraft.MCMain;
import de.tamion.others.Utils;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.jetbrains.annotations.NotNull;

public class SetMCSyntax implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String label, @NotNull String[] args) {
if(!sender.hasPermission("DCChat.syntax") || !sender.hasPermission("DCChat.admin")) {
sender.sendMessage("You are not allowed to execute this Command!");
return false;
}
if(!(args.length >= 1)) {
sender.sendMessage("/setMCSyntax [Syntax]");
return false;
}
FileConfiguration config = MCMain.getPlugin().getConfig();
StringBuilder sb = new StringBuilder();
for(String wor: args) {
sb.append(wor+ " ");
}
config.set("Bot.mcsyntax", sb.toString());
MCMain.getPlugin().saveConfig();
sender.sendMessage("Syntax Set!");
sender.sendMessage(Utils.setsyntax(sender, "Bot.mcsyntax", String.join(" ", args)));
return true;
}
}
15 changes: 15 additions & 0 deletions src/main/java/de/tamion/minecraft/commands/SetStartSyntax.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package de.tamion.minecraft.commands;

import de.tamion.others.Utils;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;

public class SetStartSyntax implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String label, @NotNull String[] args) {
sender.sendMessage(Utils.setsyntax(sender, "Bot.startsyntax", String.join(" ", args)));
return true;
}
}
15 changes: 15 additions & 0 deletions src/main/java/de/tamion/minecraft/commands/SetStopSyntax.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package de.tamion.minecraft.commands;

import de.tamion.others.Utils;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;

public class SetStopSyntax implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String label, @NotNull String[] args) {
sender.sendMessage(Utils.setsyntax(sender, "Bot.stopsyntax", String.join(" ", args)));
return true;
}
}
2 changes: 1 addition & 1 deletion src/main/java/de/tamion/minecraft/commands/SetToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N
sender.sendMessage("Token has been set to " + args[0]);
return true;
}
}
}
3 changes: 1 addition & 2 deletions src/main/java/de/tamion/minecraft/listeners/JoinLeave.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
import org.bukkit.event.player.PlayerQuitEvent;

public class JoinLeave implements Listener {
static FileConfiguration config = MCMain.getPlugin().getConfig();
@EventHandler
public void onJoin(PlayerJoinEvent e) {
FileConfiguration config = MCMain.getPlugin().getConfig();
Utils.sendtochat(config.getString("Bot.joinsyntax").replaceAll("\\{username}", e.getPlayer().getName()));
}

@EventHandler
public void onLeave(PlayerQuitEvent e) {
FileConfiguration config = MCMain.getPlugin().getConfig();
Utils.sendtochat(config.getString("Bot.leavesyntax").replaceAll("\\{username}", e.getPlayer().getName()));
}
}
26 changes: 13 additions & 13 deletions src/main/java/de/tamion/others/DCChatConsoleAppender.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ public boolean isStarted() {
@Override
public void append(LogEvent e) {
FileConfiguration config = MCMain.getPlugin().getConfig();
if(DCMain.jda != null && DCMain.jda.getGuildById(config.getString("Bot.guildid")) != null) {
if(Schedulers.sb.length()+e.getMessage().getFormattedMessage().length()>1990) {
Utils.sendtoconsole(Schedulers.sb.toString());
Schedulers.sb.setLength(0);
if(e.getMessage().getFormattedMessage().length()<1990) {
Schedulers.sb.append(e.getMessage().getFormattedMessage() + "\n");
} else {
for(String mn : e.getMessage().getFormattedMessage().split("(?<=\\G.{4})")) {
Utils.sendtoconsole(mn);
}
}
} else {
if(DCMain.jda == null || DCMain.jda.getGuildById(config.getString("Bot.guildid")) == null) {
return;
}
if(Schedulers.sb.length()+e.getMessage().getFormattedMessage().length()>1990) {
Utils.sendtoconsole(Schedulers.sb.toString());
Schedulers.sb.setLength(0);
if(e.getMessage().getFormattedMessage().length()<1990) {
Schedulers.sb.append(e.getMessage().getFormattedMessage() + "\n");
} else {
for(String mn : e.getMessage().getFormattedMessage().split("(?<=\\G.{4})")) {
Utils.sendtoconsole(mn);
}
}

} else {
Schedulers.sb.append(e.getMessage().getFormattedMessage() + "\n");
}
}
}
15 changes: 8 additions & 7 deletions src/main/java/de/tamion/others/Schedulers.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ public static void consolescheduler() {
public static void updatesyntaxchannel() {
Bukkit.getScheduler().scheduleSyncRepeatingTask(MCMain.getPlugin(), () -> {
for(TextChannel textChannel : DCMain.jda.getGuildById(MCMain.getPlugin().getConfig().getString("Bot.guildid")).getTextChannels()) {
if(textChannel.getTopic() != null && textChannel.getTopic().startsWith("MCSYNTAX:")) {
String newname = textChannel.getTopic().replace("MCSYNTAX:", "").trim().replaceAll(" ", "-")
.replaceAll("\\{players}", String.valueOf(Bukkit.getOnlinePlayers().size()));
if (!textChannel.getName().equals(newname)) {
textChannel.getManager().setName(newname).queue();
break;
}
if(textChannel.getTopic() == null || textChannel.getTopic().startsWith("MCSYNTAX:")) {
return;
}
String newname = textChannel.getTopic().replace("MCSYNTAX:", "").trim().replaceAll(" ", "-")
.replaceAll("\\{players}", String.valueOf(Bukkit.getOnlinePlayers().size()));
if (!textChannel.getName().equals(newname)) {
textChannel.getManager().setName(newname).queue();
break;
}
}
}, 20L, 6000L);
Expand Down
Loading

0 comments on commit 39e54e4

Please sign in to comment.