Skip to content

Commit 49d7876

Browse files
committed
New features: Unique Rumors, Trade Support, Player Count
Restructured properties file. Fixed a nasty bug occurring with Discord service interruptions. Minor other various tweaks and fixes.
1 parent 870a42d commit 49d7876

File tree

3 files changed

+122
-68
lines changed

3 files changed

+122
-68
lines changed

build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ apply plugin: 'com.github.johnrengelman.shadow'
1313

1414
group = 'org.nyxcode.wurm'
1515

16-
version = '1.0'
16+
version = '2.0'
1717

1818
description = "A mod that acts as a discord relay between Wurm kingdom chat and a discord server"
1919

@@ -29,8 +29,8 @@ repositories {
2929
}
3030
dependencies {
3131
compile 'net.dv8tion:JDA:3.3.1_289'
32-
compile 'org.gotti.wurmunlimited:server-modlauncher:0.33-beta1'
33-
compile 'com.github.Sindusk:sindusklibrary:v1.0'
32+
compile 'org.gotti.wurmunlimited:server-modlauncher:0.35-beta1'
33+
compile 'com.github.Sindusk:sindusklibrary:v1.1'
3434
}
3535

3636
jar {
@@ -39,7 +39,7 @@ jar {
3939

4040
shadowJar {
4141
dependencies {
42-
exclude(dependency('org.gotti.wurmunlimited:server-modlauncher:0.33-beta1'))
42+
exclude(dependency('org.gotti.wurmunlimited:server-modlauncher:0.35-beta1'))
4343
exclude(dependency('org.gotti.wurmunlimited:common:2182584'))
4444
exclude(dependency('org.gotti.wurmunlimited:server:2182584'))
4545
}

mods/DiscordRelay.properties

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,32 @@ classname=org.nyxcode.wurm.discordrelay.DiscordRelay
22
classpath=*.jar,libs/*.jar
33
sharedClassLoader=true
44

5-
# Required
6-
# Discord bot token
5+
# botToken: Discord bot token. This is the client secret that can be found when reviewing the information for your bot at the following link:
6+
# https://discordapp.com/developers/applications/me
7+
# The above link is also where you would create your bot, if you're unfamiliar with Discord bot creation.
8+
# Image assistance: https://i.imgur.com/NHctYHO.png
79
botToken=
810

9-
# Required
10-
# Discord server to connect to
11+
# discordServerName: Discord server to connect to (not case sensitive).
1112
discordServerName=ServerName
1213

13-
# Required
14-
# In game relay identification
15-
# Player name in Wurm that will prefix all discord messages
16-
wurmBotName=ServerBot
14+
# useUnderscore: This will determine whether to replace white space with underscores or remove whitespace entirely.
15+
# Example: True - #ca_help. False - #cahelp
16+
useUnderscore=true
1717

18-
# Optional
19-
# true: Kingdom names that contain space will have matching discord
20-
# channels with spaces replaced by underscores (Freedom Isles -> freedom_isles)
21-
#
22-
# false: Kingdom names that contain space will have matching discord
23-
# channels without them (Freedom Isles -> freedomisles)
24-
useUnderscore=true
18+
# showConnectedPlayers: Whether or not to show the amount of players connected as the "game" description of the bot in Discord.
19+
showConnectedPlayers=true
20+
# connectedPlayerUpdateInterval: How long, in seconds, before each update of the amount of connected players.
21+
# Note that the bot can only update at maximum 5 times per minute, so this value should never be set below 12.
22+
# My personal recommendation is to ensure a minimum of 30 seconds. The default is 120.
23+
connectedPlayerUpdateInterval=120
24+
25+
# enableRumors: Whether to announce unique rumors to Discord.
26+
enableRumors=true
27+
# rumorChannel: Which channel to announce unique rumors to.
28+
#rumorChannel=rumors
29+
30+
# enableTrade: Whether or not to enable the global trade relay.
31+
# Any messages sent to trade in-game will be relayed to a global #trade channel in Discord.
32+
# Any messages sent to the #trade channel in Discord will be relayed in-game to all kingdom's trade chats.
33+
enableTrade=true

src/main/java/org/nyxcode/wurm/discordrelay/DiscordRelay.java

Lines changed: 94 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package org.nyxcode.wurm.discordrelay;
22

3-
import com.wurmonline.server.Message;
4-
import com.wurmonline.server.Players;
5-
import com.wurmonline.server.Server;
6-
import com.wurmonline.server.Servers;
3+
import com.wurmonline.server.*;
74
import com.wurmonline.server.creatures.Communicator;
85
import com.wurmonline.server.creatures.Creature;
96
import com.wurmonline.server.kingdom.Kingdom;
@@ -38,18 +35,23 @@
3835
/**
3936
* Created by whisper2shade on 22.04.2017.
4037
*/
41-
public class DiscordRelay extends ListenerAdapter implements WurmServerMod, PreInitable, Configurable, ChannelMessageListener, PlayerMessageListener {
38+
public class DiscordRelay extends ListenerAdapter implements WurmServerMod, PreInitable, Configurable, ServerPollListener, ChannelMessageListener, PlayerMessageListener {
4239
public static final Logger logger = Logger.getLogger(DiscordRelay.class.getName());
4340

44-
private static JDA jda;
45-
private static String botToken;
46-
private static String serverName;
41+
protected static JDA jda;
42+
protected static String botToken = "";
43+
protected static String serverName = "";
4744
//private String wurmBotName;
48-
private boolean useUnderscore;
45+
protected static boolean useUnderscore = false;
46+
protected static boolean showConnectedPlayers = true;
47+
protected static int connectedPlayerUpdateInterval = 120;
48+
protected static boolean enableRumors = true;
49+
protected static String rumorChannel = "rumors";
50+
protected static boolean enableTrade = true;
4951

5052

5153
public static void sendRumour(Creature creature){
52-
sendToDiscord("rumors", "Rumours of " + creature.getName() + " are starting to spread.", true);
54+
sendToDiscord(rumorChannel, "Rumours of " + creature.getName() + " are starting to spread.", true);
5355
}
5456

5557
@Override
@@ -65,49 +67,63 @@ public void preInit() {
6567

6668
// - Send rumour messages to discord - //
6769
try {
68-
CtClass ctCreature = classPool.get("com.wurmonline.server.creatures.Creature");
69-
CtClass[] params1 = {
70-
CtClass.intType,
71-
CtClass.booleanType,
72-
CtClass.floatType,
73-
CtClass.floatType,
74-
CtClass.floatType,
75-
CtClass.intType,
76-
classPool.get("java.lang.String"),
77-
CtClass.byteType,
78-
CtClass.byteType,
79-
CtClass.byteType,
80-
CtClass.booleanType,
81-
CtClass.byteType,
82-
CtClass.intType
83-
};
84-
String desc1 = Descriptor.ofMethod(ctCreature, params1);
85-
Util.setReason("Send rumour messages to Discord.");
86-
String replace = "$proceed($$);"
87-
+ DiscordRelay.class.getName()+".sendRumour(toReturn);";
88-
Util.instrumentDescribed(thisClass, ctCreature, "doNew", desc1, "broadCastSafe", replace);
70+
if(enableRumors) {
71+
CtClass ctCreature = classPool.get("com.wurmonline.server.creatures.Creature");
72+
CtClass[] params1 = {
73+
CtClass.intType,
74+
CtClass.booleanType,
75+
CtClass.floatType,
76+
CtClass.floatType,
77+
CtClass.floatType,
78+
CtClass.intType,
79+
classPool.get("java.lang.String"),
80+
CtClass.byteType,
81+
CtClass.byteType,
82+
CtClass.byteType,
83+
CtClass.booleanType,
84+
CtClass.byteType,
85+
CtClass.intType
86+
};
87+
String desc1 = Descriptor.ofMethod(ctCreature, params1);
88+
Util.setReason("Send rumour messages to Discord.");
89+
String replace = "$proceed($$);"
90+
+ DiscordRelay.class.getName() + ".sendRumour(toReturn);";
91+
Util.instrumentDescribed(thisClass, ctCreature, "doNew", desc1, "broadCastSafe", replace);
92+
}
8993
} catch (NotFoundException e) {
9094
e.printStackTrace();
9195
}
9296
}
9397

9498
@Override
9599
public void configure(Properties properties) {
96-
botToken = properties.getProperty("botToken");
97-
serverName = properties.getProperty("discordServerName");
98-
//wurmBotName = properties.getProperty("wurmBotName");
99-
useUnderscore = Boolean.parseBoolean(properties.getProperty("useUnderscore", "false"));
100+
botToken = properties.getProperty("botToken", botToken);
101+
if(botToken.equals("")){
102+
logger.warning("Discord bot token not entered for DiscordRelay. The bot will not function without this.");
103+
}
104+
serverName = properties.getProperty("discordServerName", serverName);
105+
if(serverName.equals("")){
106+
logger.warning("Server name not entered for DiscordRelay. The bot will not function without this.");
107+
}
108+
useUnderscore = Boolean.parseBoolean(properties.getProperty("useUnderscore", Boolean.toString(useUnderscore)));
109+
showConnectedPlayers = Boolean.parseBoolean(properties.getProperty("showConnectedPlayers", Boolean.toString(showConnectedPlayers)));
110+
connectedPlayerUpdateInterval = Integer.parseInt(properties.getProperty("connectedPlayerUpdateInterval", Integer.toString(connectedPlayerUpdateInterval)));
111+
pollPlayerInterval = TimeConstants.SECOND_MILLIS*connectedPlayerUpdateInterval;
112+
enableRumors = Boolean.parseBoolean(properties.getProperty("enableRumors", Boolean.toString(enableRumors)));
113+
rumorChannel = properties.getProperty("rumorChannel", rumorChannel);
114+
enableTrade = Boolean.parseBoolean(properties.getProperty("enableTrade", Boolean.toString(enableTrade)));
100115
}
101116

102117
private static final DateFormat df = new SimpleDateFormat("HH:mm:ss");
103118
public static void sendToDiscord(String channel, String message, boolean includeMap){
104119
MessageBuilder builder = new MessageBuilder();
105120
message = "[" + df.format(new Date(System.currentTimeMillis())) + "] "+message; // Add timestamp
106-
message = message + "(" + Servers.localServer.mapname + ")";
121+
if(includeMap) {
122+
message = message + " (" + Servers.localServer.mapname + ")";
123+
}
107124

108125
builder.append(message);
109126
try {
110-
jda.getPresence().setGame(Game.of(Players.getInstance().getNumberOfPlayers() + " online!"));
111127
jda.getGuildsByName(serverName, true).get(0).getTextChannelsByName(channel, true).get(0).sendMessage(builder.build()).queue();
112128
}catch(Exception e){
113129
e.printStackTrace();
@@ -117,7 +133,12 @@ public static void sendToDiscord(String channel, String message, boolean include
117133

118134
@Override
119135
public MessagePolicy onKingdomMessage(Message message) {
120-
if(message.getWindow().startsWith("GL-")){
136+
String window = message.getWindow();
137+
if(window.startsWith("Trade")){
138+
if(enableTrade) {
139+
sendToDiscord("trade", message.getMessage(), false);
140+
}
141+
}else if(window.startsWith("GL-")){
121142
byte kingdomId = message.getSender().getKingdomId();
122143
//Kingdom kingdom = Kingdoms.getKingdom(kingdomId);
123144
String kingdomName = discordifyName("GL-"+Kingdoms.getChatNameFor(kingdomId));
@@ -136,6 +157,15 @@ public MessagePolicy onKingdomMessage(Message message) {
136157
return MessagePolicy.PASS;
137158
}
138159

160+
public void sendToTradeChat(final String channel, final String message){
161+
String window = "Trade";
162+
final Message mess = new Message(null, Message.TRADE, window, message);
163+
mess.setSenderKingdom((byte) 4);
164+
if (message.trim().length() > 1) {
165+
Server.getInstance().addMessage(mess);
166+
}
167+
}
168+
139169
public void sendToGlobalKingdomChat(final String channel, final String message) {
140170
Kingdom[] kingdoms = Kingdoms.getAllKingdoms();
141171

@@ -166,16 +196,6 @@ public void sendToGlobalKingdomChat(final String channel, final String message)
166196
mess.setSenderKingdom(kingdomId);
167197
if (message.trim().length() > 1) {
168198
Server.getInstance().addMessage(mess);
169-
/*final WcKingdomChat wc = new WcKingdomChat(WurmId.getNextWCCommandId(),
170-
wurmId, "[D]", message, false, kingdomId,
171-
-1,
172-
-1,
173-
-1);*/
174-
/*if (Servers.localServer.LOGINSERVER){
175-
wc.sendFromLoginServer();
176-
}*/
177-
//else
178-
//wc.sendToLoginServer();
179199
}
180200
}
181201
}
@@ -195,7 +215,13 @@ public void onMessageReceived(MessageReceivedEvent event) {
195215
super.onMessageReceived(event);
196216
if (event.isFromType(ChannelType.TEXT) && !event.getAuthor().isBot()) {
197217
String name = event.getTextChannel().getName();
198-
sendToGlobalKingdomChat(name, "<@" + event.getMember().getEffectiveName() + "> " + event.getMessage().getContent());
218+
if(name.contains("trade")){
219+
if(enableTrade) {
220+
sendToTradeChat(name, "<@" + event.getMember().getEffectiveName() + "> " + event.getMessage().getContent());
221+
}
222+
}else {
223+
sendToGlobalKingdomChat(name, "<@" + event.getMember().getEffectiveName() + "> " + event.getMessage().getContent());
224+
}
199225
}
200226
}
201227

@@ -219,4 +245,23 @@ public MessagePolicy onPlayerMessage(Communicator communicator, String message,
219245
public boolean onPlayerMessage(Communicator var1, String var2) {
220246
return false;
221247
}
248+
249+
protected static long lastPolledPlayers = 0;
250+
protected static long pollPlayerInterval = TimeConstants.SECOND_MILLIS*120;
251+
@Override
252+
public void onServerPoll() {
253+
if(showConnectedPlayers) {
254+
if(System.currentTimeMillis() > lastPolledPlayers + pollPlayerInterval) {
255+
if (Servers.localServer.LOGINSERVER) {
256+
try {
257+
jda.getPresence().setGame(Game.of(Players.getInstance().getNumberOfPlayers() + " online!"));
258+
}catch(Exception e){
259+
e.printStackTrace();
260+
logger.info("Failed to update player count.");
261+
}
262+
}
263+
lastPolledPlayers = System.currentTimeMillis();
264+
}
265+
}
266+
}
222267
}

0 commit comments

Comments
 (0)