Skip to content

Commit

Permalink
Merge pull request #3 from Ashleyz4/master
Browse files Browse the repository at this point in the history
Version 1.5
  • Loading branch information
RhysB authored Nov 6, 2019
2 parents fbca1d7 + 99966b0 commit 2d56490
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 89 deletions.
19 changes: 19 additions & 0 deletions src/com/johnymuffin/discordGameBridge/Cmd_DiscordBridge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.johnymuffin.discordGameBridge;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;

public class Cmd_DiscordBridge implements CommandExecutor {
@Override
public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
if(strings.length > 0) {
if(strings[0].equalsIgnoreCase("reload")) {
DiscordBot.getInstance().configReader.reload();
}
} else {
commandSender.sendMessage("/DiscordBridge reload");
}
return false;
}
}
158 changes: 88 additions & 70 deletions src/com/johnymuffin/discordGameBridge/ConfigReader.java
Original file line number Diff line number Diff line change
@@ -1,103 +1,121 @@
package com.johnymuffin.discordGameBridge;

import javax.annotation.Nonnull;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

class ConfigReader {
// New Config

//discord.Discord().DiscordSendToChannel(gameBridge,
//"**" + ((PlayerJoinEvent) event).getPlayer().getName() + "** Has Joined The Game ["
//+ Bukkit.getServer().getOnlinePlayers().length + "/"
//+ Bukkit.getServer().getMaxPlayers() + "]");

private String discordStartMessage = "**Server Has Started**";
private String discordChatMessage = "&f[&6Discord&f]&7 %messageAuthor%: %message%";
private String gameChatMessage = "**%messageAuthor%**: %message%";
private String joinMessage = "**%username%** Has Joined THe Game [%onlineCount%/%maxCount%]";
private String quitMessage = "**%username%** Has Quit THe Game [%onlineCount%/%maxCount%]";

private String joinMessage = "**%username%** Has Joined The Game [%onlineCount%/%maxCount%]";
private String quitMessage = "**%username%** Has Quit The Game [%onlineCount%/%maxCount%]";
private String timestampMessage = "**[%world%( - )%h%:%m%:%s%:%S% %ampm% %z%]** ";
private String serverName = "";
private String channelID = "";
private Boolean PrensencePlayercount = true;
private Boolean OnlineCommand = true;

private boolean presencePlayercount = true;
private boolean onlineCommand = true;
private boolean announceStartStop = true;
private boolean showTimestamp = true;
private boolean useInGameTime = true;
private boolean seperateChat = true;

ConfigReader() {
this.reload();
}

ConfigReader(DiscordBot instance) {
private int reloadTimes = 0;
void reload() {
Properties prop = new Properties();

DiscordBot instance = DiscordBot.getInstance();
if(reloadTimes > 3) {
reloadTimes = 0;
instance.logger.severe("[DiscordBot]: Failed to create/read properties file after 3 times at " + instance.config.getAbsolutePath());
return;
}

try {
prop.load(new FileInputStream(instance.config));
} catch (IOException var4) {
instance.logger.warning("[RetroBot] No properties found! Making new file...");
var4.printStackTrace();
instance.logger.warning("[DiscordBot]: No properties file found! Attempting to create...");
if(!instance.config.exists()) {
try {
boolean b = instance.config.createNewFile();
if(!b) {
instance.logger.warning("[DiscordBot]: Failed to create file at " + instance.config.getAbsolutePath());
} else {
reloadTimes++;
instance.logger.fine("[DiscordBot]: Successfully created new config, trying again...");
reload();
}
return;
} catch(IOException e) {
e.printStackTrace();
return;
}
} else {
if(!instance.config.canRead()) {
instance.logger.severe("[DiscordBot]: Can't read from config file at " + instance.config.getAbsolutePath());
return;
}
}
}
// New Config
serverName = prop.getProperty("serverName", serverName);
channelID = prop.getProperty("channelID", channelID);
PrensencePlayercount = Boolean.parseBoolean(prop.getProperty("PrensencePlayercount", "" + PrensencePlayercount));
OnlineCommand = Boolean.parseBoolean(prop.getProperty("OnlineCommand", "" + OnlineCommand));
announceStartStop = Boolean.parseBoolean(prop.getProperty("announceStartStop", "" + announceStartStop));
//Join Custom Message
joinMessage = prop.getProperty("joinMessage", joinMessage);
prop.setProperty("joinMessage", joinMessage);
//Quit Custom Message
quitMessage = prop.getProperty("quitMessage", quitMessage);
prop.setProperty("quitMessage", quitMessage);
//Discord Chat Message
discordChatMessage = prop.getProperty("discordChatMessage", discordChatMessage);
prop.setProperty("discordChatMessage", discordChatMessage);
//Game Chat Message
gameChatMessage = prop.getProperty("gameChatMessage", gameChatMessage);
prop.setProperty("gameChatMessage", gameChatMessage);
//Announce Server Start/Stop

prop.setProperty("OnlineCommand", "" + OnlineCommand);
prop.setProperty("PrensencePlayercount", "" + PrensencePlayercount);
prop.setProperty("channelID", channelID);
prop.setProperty("serverName", serverName);
prop.setProperty("announceStartStop", ""+announceStartStop);
reloadTimes = 0;

seperateChat = getAndSet_B(prop, "separateChat", seperateChat);
showTimestamp = getAndSet_B(prop, "showTimestamp", showTimestamp);
useInGameTime = getAndSet_B(prop, "useInGameTime", useInGameTime);
presencePlayercount = getAndSet_B(prop, "presencePlayercount", presencePlayercount);
onlineCommand = getAndSet_B(prop, "onlineCommand", onlineCommand);
announceStartStop = getAndSet_B(prop, "announceStartStop", announceStartStop);

serverName = getAndSet_S(prop, "serverName", serverName);
channelID = getAndSet_S(prop, "channelID", channelID);
joinMessage = getAndSet_S(prop, "joinMessage", joinMessage);
quitMessage = getAndSet_S(prop, "quitMessage", quitMessage);
discordStartMessage = getAndSet_S(prop, "discordStartMessage", discordStartMessage);
discordChatMessage = getAndSet_S(prop, "discordChatMessage", discordChatMessage);
gameChatMessage = getAndSet_S(prop, "gameChatMessage", gameChatMessage);
timestampMessage = getAndSet_S(prop, "timestampMessage", timestampMessage);

try {
prop.store(new FileOutputStream(instance.config), "Properties for DiscordBot");
} catch (Exception var3) {
instance.logger.severe("Failed to save properties for RetroBot!");
var3.printStackTrace();
} catch (Exception e) {
instance.logger.severe("[DiscordBot]: Failed to save properties!");
e.printStackTrace();
}
}

String getServerName() {
return this.serverName;
private String getAndSet_S(@Nonnull Properties prop, @Nonnull String get, @Nonnull Object Default) {
String s = prop.getProperty(get, String.valueOf(Default));
prop.setProperty(get, String.valueOf(s));
return s;
}

String getChannel() {
return this.channelID;
private boolean getAndSet_B(@Nonnull Properties prop, @Nonnull String get, @Nonnull Object Default) {
Boolean s = Boolean.parseBoolean(prop.getProperty(get, String.valueOf(Default)));
prop.setProperty(get, String.valueOf(s));
return s;
}

String getJoinMessage() {
return this.joinMessage;
}
String getQuitMessage() {
return this.quitMessage;
}
String getDiscordChatMessage() {
return this.discordChatMessage;
}
String getGameChatMessage() {
return this.gameChatMessage;
}
String getServerName() { return this.serverName; }
String getChannel() { return this.channelID; }
String getJoinMessage() { return this.joinMessage; }
String getQuitMessage() { return this.quitMessage; }
String getDiscordStartMessage() { return discordStartMessage; }
String getDiscordChatMessage() { return this.discordChatMessage; }
String getGameChatMessage() { return this.gameChatMessage; }
String getTimestampMessage() { return timestampMessage; }

Boolean getPrensencePlayercount() {
return this.PrensencePlayercount;
boolean getPresencePlayercount() { return this.presencePlayercount; }
boolean getOnlineCommand() { return this.onlineCommand; }

}
Boolean getOnlineCommand() {
return this.OnlineCommand;
}

boolean canAnnounceStartStop() {
return announceStartStop;
}
boolean canUseInGameTime() { return useInGameTime; }
boolean canShowTimestamp() { return showTimestamp; }
boolean canSeparateChat() { return seperateChat; }
boolean canAnnounceStartStop() { return announceStartStop; }
}
20 changes: 14 additions & 6 deletions src/com/johnymuffin/discordGameBridge/DiscordBot.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.johnymuffin.discordGameBridge;

import com.earth2me.essentials.Essentials;
import com.johnymuffin.discordcore.DiscordCore;
import net.dv8tion.jda.core.EmbedBuilder;
import net.dv8tion.jda.core.JDA;
Expand Down Expand Up @@ -31,9 +32,11 @@ public class DiscordBot extends JavaPlugin implements Listener, EventListener {
private String discordChatMessage = "";

private static Boolean botEnabled = false;
private ConfigReader configReader;
ConfigReader configReader;
private int taskId = 0;

private Essentials ess;

public static final String PLUGIN_FOLDER = "./plugins/DiscordBot";
private File pluginFolder = new File("./plugins/DiscordBot");
File config;
Expand Down Expand Up @@ -66,12 +69,14 @@ private void quickSend(String s) {

private GamePlayerEvents events;
public void onEnable() {
this.configReader = new ConfigReader(this);
this.configReader = new ConfigReader();
this.logger = this.getServer().getLogger();
this.logger.info("[DiscordBot] Enabling DiscordBot...");
discord = (DiscordCore) getServer().getPluginManager().getPlugin("DiscordCore");
JDA jda = discord.Discord().jda;

this.getCommand("discordbridge").setExecutor(new Cmd_DiscordBridge());

gameBridge = this.configReader.getChannel();
serverName = this.configReader.getServerName();
// Custom Messages
Expand All @@ -84,7 +89,7 @@ public void onEnable() {
}


if (configReader.getPrensencePlayercount()) {
if (configReader.getPresencePlayercount()) {
System.out.println("[DiscordBot] Discord Player Timer Has Started");
Bukkit.getServer().broadcastMessage("Discord Player Timer Has Started");
taskId = this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
Expand All @@ -94,7 +99,7 @@ public void run() {
if(!botEnabled) {
botEnabled = true;
if(configReader.canAnnounceStartStop())
quickSend("**SERVER HAS STARTED**");
quickSend(configReader.getDiscordStartMessage());
jda.addEventListener(instance);
}
jda.getPresence().setGame(Game.playing(serverName + " With " + Bukkit.getServer().getOnlinePlayers().length + " Players"));
Expand All @@ -114,7 +119,7 @@ public void run() {
public void onDisable() {
quickSend("**SERVER HAS STOPPED**");
this.logger.info("[DiscordBot] Successfully stopped!");
// if (configReader.getPrensencePlayercount()) {
// if (configReader.getPresencePlayercount()) {
Bukkit.getServer().getScheduler().cancelTask(taskId);
discord.Discord().jda.removeEventListener(this);
// }
Expand Down Expand Up @@ -142,7 +147,10 @@ public void onEvent(net.dv8tion.jda.core.events.Event event) {
message = message.replaceAll("%message%",
((GuildMessageReceivedEvent) event).getMessage().getContentRaw());
message = message.replaceAll("(&([a-f0-9]))", "\u00A7$2");
Bukkit.getServer().broadcastMessage(message);
Player[] players = Bukkit.getServer().getOnlinePlayers();
for(Player player : players) {
player.sendRawMessage(message);
}
}

} else if ((messageCMD[0].equalsIgnoreCase("!online")) && (configReader.getOnlineCommand())) {
Expand Down
Loading

0 comments on commit 2d56490

Please sign in to comment.