Skip to content

Commit 12c257f

Browse files
Add tellraw relay
1 parent 11fc9b4 commit 12c257f

3 files changed

Lines changed: 84 additions & 2 deletions

File tree

src/main/java/com/firecontroller1847/truediscordlink/TrueDiscordLink.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public boolean onAfterConfiguration() {
6161
pluginManager.registerEvents(new PlayerQuitListener(this), this);
6262
pluginManager.registerEvents(new PlayerDeathListener(this), this);
6363
pluginManager.registerEvents(new PlayerAdvancementDoneListener(this), this);
64+
pluginManager.registerEvents(new CommandListener(this), this);
6465

6566
// If we haven't returned already, it was a success
6667
return true;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.firecontroller1847.truediscordlink.listeners.minecraft;
2+
3+
import com.firecontroller1847.truediscordlink.DiscordManager;
4+
import com.firecontroller1847.truediscordlink.TrueDiscordLink;
5+
import net.minecraft.network.chat.Component;
6+
import net.minecraft.network.chat.MutableComponent;
7+
import org.bukkit.command.CommandSender;
8+
import org.bukkit.event.EventHandler;
9+
import org.bukkit.event.Listener;
10+
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
11+
import org.bukkit.event.server.ServerCommandEvent;
12+
13+
import java.util.Arrays;
14+
15+
public class CommandListener implements Listener {
16+
17+
// Variables
18+
private TrueDiscordLink discordlink;
19+
20+
// Constructor
21+
public CommandListener(TrueDiscordLink discordlink) {
22+
this.discordlink = discordlink;
23+
}
24+
25+
// Player Commands
26+
@EventHandler
27+
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
28+
this.onCommand(event.getMessage().substring(1), event.getPlayer());
29+
}
30+
31+
// Non-Player Commands
32+
@EventHandler
33+
public void onServerCommand(ServerCommandEvent event) {
34+
this.onCommand(event.getCommand(), event.getSender());
35+
}
36+
37+
// Event
38+
public void onCommand(String command, CommandSender sender) {
39+
// Relay Tellraw Messages
40+
if (discordlink.getConfig().getBoolean("events.relay_tellraw_messages") && command.startsWith("tellraw")) {
41+
42+
// Check for communication method
43+
DiscordManager discordManager = discordlink.getDiscordManager();
44+
if (discordManager.canCommunicateWithDiscord()) {
45+
String[] args = command.split(" ");
46+
if (args.length >= 3) {
47+
String targets = args[1];
48+
String message = String.join(" ", Arrays.copyOfRange(args, 2, args.length));
49+
if (targets.contains("@a")) {
50+
try {
51+
MutableComponent components = Component.Serializer.fromJson(message);
52+
String stringifiedTellraw = components.getString();
53+
discordManager.sendDiscordMessage(stringifiedTellraw);
54+
} catch (Exception e) {
55+
// ...
56+
}
57+
}
58+
}
59+
}
60+
61+
}
62+
}
63+
64+
}

src/main/resources/config.yml

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,16 +147,33 @@ bot:
147147
###########################
148148
# System Messages #
149149
###########################
150-
# Messages that will send on the following events. Messages configurable in lang file.
151-
# Set to false to disable the messages from sending.
150+
# Messages that will send to the Discord server when the following events occur.
151+
# The messages themselves are configurable in the lang file.
152+
# Set to false to disable them from sending.
152153
events:
154+
# Sends a message on server startup
153155
server_start: true
156+
157+
# Sends a message on server shutdown
154158
server_shutdown: true
159+
160+
# Sends a message when a player joins
155161
player_join: true
162+
163+
# Sends a message when a player leaves
156164
player_quit: true
165+
166+
# Sends a message when a player dies
157167
player_death: true
168+
169+
# Sends a message when a player receives an advancement
158170
player_advance: true
159171

172+
# Relays tellraw messages to the Discord server
173+
# Only works when targeted at @a (not @e)
174+
# Works with command blocks and console
175+
relay_tellraw_messages: true
176+
160177
###################
161178
# Tagging #
162179
###################

0 commit comments

Comments
 (0)