Skip to content

Commit ee9121e

Browse files
Show position and time for advancement and change discord library
1 parent f62334e commit ee9121e

File tree

5 files changed

+133
-30
lines changed

5 files changed

+133
-30
lines changed

pom.xml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@
7373
<id>papermc</id>
7474
<url>https://papermc.io/repo/repository/maven-public/</url>
7575
</repository>
76+
<repository>
77+
<id>jcenter</id>
78+
<name>jcenter-bintray</name>
79+
<url>https://jcenter.bintray.com</url>
80+
</repository>
7681
</repositories>
7782

7883
<dependencies>
@@ -100,9 +105,15 @@
100105
<scope>test</scope>
101106
</dependency>
102107
<dependency>
103-
<groupId>com.discord4j</groupId>
104-
<artifactId>discord4j-core</artifactId>
105-
<version>3.1.3</version>
108+
<groupId>net.dv8tion</groupId>
109+
<artifactId>JDA</artifactId>
110+
<version>4.2.0_242</version>
111+
<exclusions>
112+
<exclusion>
113+
<groupId>club.minnced</groupId>
114+
<artifactId>opus-java</artifactId>
115+
</exclusion>
116+
</exclusions>
106117
</dependency>
107118
</dependencies>
108119
</project>

src/main/java/parallelmc/parallelutils/Parallelutils.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import parallelmc.parallelutils.discordintegration.BotManager;
2626
import parallelmc.parallelutils.discordintegration.DiscordIntegrationEventRegistrar;
2727

28+
import javax.security.auth.login.LoginException;
2829
import java.sql.*;
2930
import java.util.UUID;
3031
import java.util.logging.Level;
@@ -180,9 +181,14 @@ public void onEnable() {
180181
CustomMobsEventRegistrar.registerEvents();
181182

182183
// Register Events for the DiscordIntegration Module
183-
BotManager manager = new BotManager(token);
184-
manager.addChannel("staff", staffChannel);
185-
DiscordIntegrationEventRegistrar.registerEvents();
184+
BotManager manager = null;
185+
try {
186+
manager = new BotManager(token);
187+
manager.addChannel("staff", staffChannel);
188+
DiscordIntegrationEventRegistrar.registerEvents();
189+
} catch (LoginException e) {
190+
Parallelutils.log(Level.SEVERE, "Unable to initialize BotManager. Is the token valid?");
191+
}
186192

187193
// Setup commands
188194
Commands commands = new Commands(this);

src/main/java/parallelmc/parallelutils/discordintegration/AdvancementListener.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package parallelmc.parallelutils.discordintegration;
22

3+
import org.bukkit.Location;
4+
import org.bukkit.Statistic;
35
import org.bukkit.advancement.Advancement;
46
import org.bukkit.entity.Player;
57
import org.bukkit.event.EventHandler;
68
import org.bukkit.event.Listener;
79
import org.bukkit.event.player.PlayerAdvancementDoneEvent;
810
import parallelmc.parallelutils.Parallelutils;
11+
import parallelmc.parallelutils.util.TimeTools;
912

1013
import java.util.Arrays;
1114
import java.util.List;
@@ -29,22 +32,31 @@ public class AdvancementListener implements Listener {
2932
public void onAch(PlayerAdvancementDoneEvent event) {
3033
Player player = event.getPlayer();
3134

35+
Location playerLocation = player.getLocation();
36+
37+
int playtime = player.getStatistic(Statistic.PLAY_ONE_MINUTE); // Ticks played
38+
39+
String fulltime = TimeTools.fullTime(playtime, TimeTools.TimeUnit.TICKS);
40+
3241
Advancement advancement = event.getAdvancement();
3342

34-
// player.sendMessage("Player " + player.getName() + " got advancement " + advancement.getKey().toString());
43+
Parallelutils.log(Level.INFO,"Player " + player.getName() + " got advancement " + advancement.getKey().toString());
3544

3645
if (SPECIAL_ADVANCEMENTS.contains(advancement.getKey().toString())) {
3746
if (BotManager.getInstance() != null) {
3847
if (!BotManager.getInstance().sendMessage("staff",
39-
"Player " + player.getName() + " got advancement " + advancement.getKey().getKey())) {
48+
"Player " + player.getName() + " got advancement " + advancement.getKey().getKey() + "." +
49+
" They are at " + playerLocation.toString() + " and have " + fulltime + " of playtime")) {
4050
Parallelutils.log(Level.WARNING, "Unable to send message. Unknown error.");
4151
Parallelutils.log(Level.WARNING,
42-
"Player " + player.getName() + " got advancement " + advancement.getKey().getKey());
52+
"Player " + player.getName() + " got advancement " + advancement.getKey().getKey() + "." +
53+
" They are at " + playerLocation.toString() + " and have " + fulltime + " of playtime");
4354
}
4455
} else {
4556
Parallelutils.log(Level.WARNING, "BotManager not initialized. Can't send message!");
4657
Parallelutils.log(Level.WARNING,
47-
"Player " + player.getName() + " got advancement " + advancement.getKey().getKey());
58+
"Player " + player.getName() + " got advancement " + advancement.getKey().getKey() + "." +
59+
" They are at " + playerLocation.toString() + " and have " + fulltime + " of playtime");
4860
}
4961
}
5062
}
Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package parallelmc.parallelutils.discordintegration;
22

3-
import discord4j.common.util.Snowflake;
4-
import discord4j.core.DiscordClient;
5-
import discord4j.rest.entity.RestChannel;
3+
import net.dv8tion.jda.api.JDA;
4+
import net.dv8tion.jda.api.JDABuilder;
5+
import net.dv8tion.jda.api.entities.TextChannel;
6+
import net.dv8tion.jda.api.events.ReadyEvent;
7+
import net.dv8tion.jda.api.hooks.EventListener;
68
import parallelmc.parallelutils.Parallelutils;
79

810
import javax.annotation.Nullable;
9-
import java.time.Duration;
11+
import javax.security.auth.login.LoginException;
1012
import java.util.HashMap;
1113
import java.util.logging.Level;
1214

@@ -15,19 +17,27 @@
1517
*/
1618
public class BotManager {
1719

18-
private final DiscordClient client;
20+
private final JDA client;
1921
private final HashMap<String, String> channels;
2022

2123
private final int NUM_TRIES = 5;
2224

25+
private boolean ready = false;
26+
2327
private static BotManager manager;
2428

2529
/**
2630
* Instantiate a new BotManager with the given Discord bot token
31+
*
2732
* @param token The Discord bot token
2833
*/
29-
public BotManager(String token) {
30-
client = DiscordClient.create(token);
34+
public BotManager(String token) throws LoginException {
35+
client = JDABuilder.createDefault(token).addEventListeners((EventListener) event -> {
36+
if (event instanceof ReadyEvent) {
37+
ready = true;
38+
Parallelutils.log(Level.INFO, "JDA Ready");
39+
}
40+
}).build();
3141
channels = new HashMap<>();
3242

3343
if (manager == null) {
@@ -37,6 +47,7 @@ public BotManager(String token) {
3747

3848
/**
3949
* Returns the singleton instance of BotManager
50+
*
4051
* @return The instance
4152
*/
4253
@Nullable
@@ -46,35 +57,32 @@ public static BotManager getInstance() {
4657

4758
/**
4859
* Add a channel and ID pair to the map of channels. This is used to assign useful names to channel ids in code
60+
*
4961
* @param name The assigned name for the channel id pair
50-
* @param id The id of the channel
62+
* @param id The id of the channel
5163
*/
5264
public void addChannel(String name, String id) {
5365
channels.put(name, id);
5466
}
5567

5668
/**
5769
* Sends a message in the specified channel
70+
*
5871
* @param channel The shortname of the channel to send a message to
5972
* @param message The message that is being sent
6073
* @return Returns true if the message was sent successfully, false otherwise
6174
*/
6275
public boolean sendMessage(String channel, String message) {
63-
RestChannel restChannel = client.getChannelById(Snowflake.of(channels.get(channel)));
64-
76+
Parallelutils.log(Level.INFO, "Attempting to send a message");
6577
boolean success = false;
66-
67-
for (int i = 0; i < NUM_TRIES; i++) {
68-
try {
69-
restChannel.createMessage(message).block(Duration.ofSeconds(2));
70-
} catch (Exception e) {
71-
Parallelutils.log(Level.INFO, "Failed to send message. Trying again.");
72-
continue;
78+
if (ready) {
79+
TextChannel textChannel = client.getTextChannelById(channels.get(channel));
80+
if (textChannel != null) {
81+
textChannel.sendMessage(message).queue();
82+
success = true;
7383
}
74-
success = true;
75-
break;
7684
}
7785

78-
return success;
86+
return success; // Yes this is dumb, no I'm not changing it. It's an artifact of old code
7987
}
8088
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package parallelmc.parallelutils.util;
2+
3+
public class TimeTools {
4+
5+
public enum TimeUnit {
6+
TICKS(0, 1, "t"),
7+
SECONDS(1, 20, "s"),
8+
MINUTES(2, 20*60, "m"),
9+
HOURS(3, 20*60*60, "h"),
10+
DAYS(4, 20*60*60*24, "d");
11+
12+
public int index;
13+
public int ticks;
14+
public String val;
15+
16+
TimeUnit(int index, int ticks, String s) {
17+
this.index = index;
18+
this.ticks = ticks;
19+
val = s;
20+
}
21+
}
22+
23+
/**
24+
* Converts the given time from one unit to another
25+
* @param time The time to convert
26+
* @param original The unit of {@param time}
27+
* @param end The unit to convert to
28+
* @return The converted time
29+
*/
30+
public static double convertTime(double time, TimeUnit original, TimeUnit end) {
31+
return (time*(double)original.ticks)/(double)end.ticks;
32+
}
33+
34+
/**
35+
* Converts the given time in the given unit to a full form.
36+
* Example: 64d 5h 2m 50s 10t
37+
* @param time The starting time
38+
* @param unit The unit the starting time is in
39+
* @return The full time
40+
*/
41+
public static String fullTime(double time, TimeUnit unit) {
42+
String fulltime = "";
43+
44+
// Convert to days
45+
double days = convertTime(time, unit, TimeUnit.DAYS);
46+
int daysInt = (int)days;
47+
fulltime += daysInt + TimeUnit.DAYS.val + " ";
48+
49+
double hours = convertTime(days-daysInt, TimeUnit.DAYS, TimeUnit.HOURS);
50+
int hoursInt = (int)hours;
51+
fulltime += hoursInt + TimeUnit.HOURS.val + " ";
52+
53+
double minutes = convertTime(hours-hoursInt, TimeUnit.HOURS, TimeUnit.MINUTES);
54+
int minutesInt = (int)minutes;
55+
fulltime += minutesInt + TimeUnit.MINUTES.val + " ";
56+
57+
double seconds = convertTime(minutes-minutesInt, TimeUnit.MINUTES, TimeUnit.SECONDS);
58+
int secondsInt = (int) seconds;
59+
fulltime += secondsInt + TimeUnit.SECONDS.val + " ";
60+
61+
double ticks = convertTime(seconds-secondsInt, TimeUnit.SECONDS, TimeUnit.TICKS);
62+
fulltime += (int)ticks + TimeUnit.TICKS.val;
63+
64+
return fulltime;
65+
}
66+
}

0 commit comments

Comments
 (0)