Added DiscordSender, fixing and adding things

- It allows putting the Discord name in the chat and other plugins'
commands to work with it
- It will now only forward the general chat from MC
- It will no longer echo the message sent from Discord
This commit is contained in:
Norbi Peti 2016-11-28 18:00:43 +01:00
parent cadd04ccd1
commit ec5265f054
4 changed files with 128 additions and 8 deletions

View file

@ -120,7 +120,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.9.2-R0.1-SNAPSHOT</version>
<version>1.11-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>

View file

@ -80,14 +80,14 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
public static IChannel issuechannel;
public static IChannel debugchannel;
public static IChannel botroomchannel;
public static IGuild mainServer;
public static IGuild devServer;
public static boolean Test = true;
@Override
public void handle(ReadyEvent event) {
try {
IGuild mainServer;
IGuild devServer;
do {
mainServer = event.getClient().getGuildByID("125813020357165056");
devServer = event.getClient().getGuildByID("219529124321034241");

View file

@ -0,0 +1,115 @@
package buttondevteam.discordplugin;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.permissions.PermissibleBase;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionAttachment;
import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.plugin.Plugin;
import buttondevteam.lib.TBMCCoreAPI;
import sx.blah.discord.handle.obj.IUser;
public class DiscordSender implements CommandSender {
private PermissibleBase perm = new PermissibleBase(this);
private IUser user;
public DiscordSender(IUser user) {
this.user = user;
}
@Override
public boolean isPermissionSet(String name) {
return perm.isPermissionSet(name);
}
@Override
public boolean isPermissionSet(Permission perm) {
return this.perm.isPermissionSet(perm);
}
@Override
public boolean hasPermission(String name) {
return perm.hasPermission(name);
}
@Override
public boolean hasPermission(Permission perm) {
return this.perm.hasPermission(perm);
}
@Override
public PermissionAttachment addAttachment(Plugin plugin, String name, boolean value) {
return perm.addAttachment(plugin, name, value);
}
@Override
public PermissionAttachment addAttachment(Plugin plugin) {
return perm.addAttachment(plugin);
}
@Override
public PermissionAttachment addAttachment(Plugin plugin, String name, boolean value, int ticks) {
return perm.addAttachment(plugin, name, value, ticks);
}
@Override
public PermissionAttachment addAttachment(Plugin plugin, int ticks) {
return perm.addAttachment(plugin, ticks);
}
@Override
public void removeAttachment(PermissionAttachment attachment) {
perm.removeAttachment(attachment);
}
@Override
public void recalculatePermissions() {
perm.recalculatePermissions();
}
@Override
public Set<PermissionAttachmentInfo> getEffectivePermissions() {
return perm.getEffectivePermissions();
}
@Override
public boolean isOp() {
return perm.isOp();
}
@Override
public void setOp(boolean value) {
perm.setOp(value);
}
@Override
public void sendMessage(String message) {
try {
DiscordPlugin.sendMessageToChannel(user.getOrCreatePMChannel(), message);
} catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while sending message to DiscordSender", e);
}
}
@Override
public void sendMessage(String[] messages) {
for (String message : messages)
sendMessage(message);
}
@Override
public Server getServer() {
return Bukkit.getServer();
}
@Override
public String getName() {
return user.getDisplayName(DiscordPlugin.mainServer);
}
}

View file

@ -5,6 +5,7 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.discordplugin.DiscordSender;
import buttondevteam.lib.TBMCChatEvent;
import buttondevteam.lib.chat.Channel;
import buttondevteam.lib.chat.TBMCChatAPI;
@ -12,17 +13,21 @@ import sx.blah.discord.api.events.IListener;
import sx.blah.discord.handle.impl.events.MessageReceivedEvent;
public class MCChatListener implements Listener, IListener<MessageReceivedEvent> {
@EventHandler
@EventHandler // Minecraft
public void onMCChat(TBMCChatEvent e) {
DiscordPlugin.sendMessageToChannel(DiscordPlugin.chatchannel,
"[" + e.getChannel().DisplayName + "] <" + e.getSender().getName() + "> " + e.getMessage());
if (e.getSender() instanceof DiscordSender)
return;
if (e.getChannel().equals(Channel.GlobalChat))
DiscordPlugin.sendMessageToChannel(DiscordPlugin.chatchannel,
"<" + e.getSender().getName() + "> " + e.getMessage());
}
@Override
@Override // Discord
public void handle(MessageReceivedEvent event) {
if (event.getMessage().getAuthor().isBot())
return;
if (event.getMessage().getChannel().getID().equals(DiscordPlugin.chatchannel.getID()))
TBMCChatAPI.SendChatMessage(Channel.GlobalChat, Bukkit.getConsoleSender(), event.getMessage().getContent());
TBMCChatAPI.SendChatMessage(Channel.GlobalChat, new DiscordSender(event.getMessage().getAuthor()),
event.getMessage().getContent());
}
}