Fixed and updated plugin to the new things

This commit is contained in:
Norbi Peti 2017-04-02 01:32:37 +02:00
parent b7ef8f4048
commit ffbb9a0a91
8 changed files with 58 additions and 41 deletions

View file

@ -1,13 +1,21 @@
package buttondevteam.discordplugin; package buttondevteam.discordplugin;
import buttondevteam.lib.player.TBMCPlayer; import buttondevteam.lib.player.ChromaGamerBase;
import buttondevteam.lib.player.UserClass;
@UserClass(foldername = "discord")
public class DiscordPlayer extends ChromaGamerBase {
private String did;
public DiscordPlayer() {
}
public class DiscordPlayer extends TBMCPlayer {
public String getDiscordID() { public String getDiscordID() {
return getData(); return did;
} }
public void setDiscordID(String id) { @Override
setData(id); public String getFileName() {
return did;
} }
} }

View file

@ -99,7 +99,7 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
annchannel = botchannel; // bot-room annchannel = botchannel; // bot-room
genchannel = botchannel; // bot-room genchannel = botchannel; // bot-room
botroomchannel = botchannel;// bot-room botroomchannel = botchannel;// bot-room
chatchannel = devServer.getChannelByID("248185455508455424"); // minecraft_chat_test chatchannel = botchannel;// bot-room
officechannel = devServer.getChannelByID("219626707458457603"); // developers-office officechannel = devServer.getChannelByID("219626707458457603"); // developers-office
updatechannel = botchannel; updatechannel = botchannel;
dc.changeStatus(Status.game("testing")); dc.changeStatus(Status.game("testing"));
@ -120,6 +120,7 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
Bukkit.getPluginManager().registerEvents(new ExceptionListener(), this); Bukkit.getPluginManager().registerEvents(new ExceptionListener(), this);
TBMCCoreAPI.RegisterEventsForExceptions(new MCListener(), this); TBMCCoreAPI.RegisterEventsForExceptions(new MCListener(), this);
TBMCChatAPI.AddCommands(this, DiscordMCCommandBase.class); TBMCChatAPI.AddCommands(this, DiscordMCCommandBase.class);
TBMCCoreAPI.RegisterUserClass(DiscordPlayer.class);
Bukkit.getScheduler().runTaskAsynchronously(this, () -> sendMessageToChannel(chatchannel, "", Bukkit.getScheduler().runTaskAsynchronously(this, () -> sendMessageToChannel(chatchannel, "",
new EmbedBuilder().withColor(Color.GREEN).withTitle("Server started - chat connected.").build())); new EmbedBuilder().withColor(Color.GREEN).withTitle("Server started - chat connected.").build()));

View file

@ -10,6 +10,7 @@ import buttondevteam.discordplugin.DiscordPlayer;
import buttondevteam.discordplugin.DiscordPlugin; import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.player.TBMCPlayer; import buttondevteam.lib.player.TBMCPlayer;
import buttondevteam.lib.player.TBMCPlayerBase;
import sx.blah.discord.handle.obj.IMessage; import sx.blah.discord.handle.obj.IMessage;
public class ConnectCommand extends DiscordCommandBase { public class ConnectCommand extends DiscordCommandBase {
@ -47,8 +48,9 @@ public class ConnectCommand extends DiscordCommandBase {
DiscordPlugin.sendMessageToChannel(message.getChannel(), "The specified Minecraft player cannot be found"); DiscordPlugin.sendMessageToChannel(message.getChannel(), "The specified Minecraft player cannot be found");
return; return;
} }
try (TBMCPlayer pl = TBMCPlayer.getPlayer(p)) { try (TBMCPlayer pl = TBMCPlayerBase.getPlayer(p.getUniqueId(), TBMCPlayer.class)) {
if (message.getAuthor().getID().equals(pl.asPluginPlayer(DiscordPlayer.class).getDiscordID())) { DiscordPlayer dp = pl.getAs(DiscordPlayer.class);
if (dp != null && message.getAuthor().getID().equals(dp.getDiscordID())) {
DiscordPlugin.sendMessageToChannel(message.getChannel(), "You already have this account connected."); DiscordPlugin.sendMessageToChannel(message.getChannel(), "You already have this account connected.");
return; return;
} }

View file

@ -1,13 +1,15 @@
package buttondevteam.discordplugin.commands; package buttondevteam.discordplugin.commands;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import buttondevteam.discordplugin.DiscordPlayer; import buttondevteam.discordplugin.DiscordPlayer;
import buttondevteam.discordplugin.DiscordPlugin; import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.lib.player.TBMCPlayer; import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.player.TBMCPlayer.InfoTarget; import buttondevteam.lib.player.ChromaGamerBase;
import buttondevteam.lib.player.ChromaGamerBase.InfoTarget;
import sx.blah.discord.handle.obj.IMessage; import sx.blah.discord.handle.obj.IMessage;
import sx.blah.discord.handle.obj.IUser; import sx.blah.discord.handle.obj.IUser;
@ -29,10 +31,11 @@ public class UserinfoCommand extends DiscordCommandBase {
if (args.length() == 0) if (args.length() == 0)
target = message.getAuthor(); target = message.getAuthor();
else { else {
final Stream<IUser> mentions = message.getMentions().stream()
.filter(m -> !m.getID().equals(DiscordPlugin.dc.getOurUser().getID())); final Optional<IUser> firstmention = message.getMentions().stream()
if (mentions.findFirst().isPresent()) .filter(m -> !m.getID().equals(DiscordPlugin.dc.getOurUser().getID())).findFirst();
target = mentions.findFirst().get(); if (firstmention.isPresent())
target = firstmention.get();
else if (args.contains("#")) { else if (args.contains("#")) {
String[] targettag = args.split("#"); String[] targettag = args.split("#");
final List<IUser> targets = getUsers(message, targettag[0]); final List<IUser> targets = getUsers(message, targettag[0]);
@ -68,21 +71,15 @@ public class UserinfoCommand extends DiscordCommandBase {
target = targets.get(0); target = targets.get(0);
} }
} }
boolean found = false; try (DiscordPlayer dp = ChromaGamerBase.getUser(target.getID(), DiscordPlayer.class)) {
for (TBMCPlayer player : TBMCPlayer.getLoadedPlayers().values()) {
DiscordPlayer dp = player.asPluginPlayer(DiscordPlayer.class);
if (target.getID().equals(dp.getDiscordID())) {
StringBuilder uinfo = new StringBuilder("User info for ").append(target.getName()).append(":\n"); StringBuilder uinfo = new StringBuilder("User info for ").append(target.getName()).append(":\n");
uinfo.append(player.getInfo(InfoTarget.Discord)); uinfo.append(dp.getInfo(InfoTarget.Discord));
DiscordPlugin.sendMessageToChannel(message.getChannel(), uinfo.toString()); DiscordPlugin.sendMessageToChannel(message.getChannel(), uinfo.toString());
found = true; } catch (Exception e) {
break; DiscordPlugin.sendMessageToChannel(message.getChannel(), "An error occured while getting the user!");
TBMCCoreAPI.SendException("Error while getting DiscordPlayer info!", e);
} }
} }
if (!found)
DiscordPlugin.sendMessageToChannel(message.getChannel(), "The user (" + target.getName()
+ ") is not found in our system (player has to be on the MC server for now)!");
}
private List<IUser> getUsers(IMessage message, String args) { private List<IUser> getUsers(IMessage message, String args) {
final List<IUser> targets; final List<IUser> targets;

View file

@ -54,9 +54,9 @@ public class CommandListener {
final String mention = DiscordPlugin.dc.getOurUser().mention(false); final String mention = DiscordPlugin.dc.getOurUser().mention(false);
final String mentionNick = DiscordPlugin.dc.getOurUser().mention(true); final String mentionNick = DiscordPlugin.dc.getOurUser().mention(true);
boolean gotmention = checkanddeletemention(cmdwithargs, mention, message); boolean gotmention = checkanddeletemention(cmdwithargs, mention, message);
gotmention = checkanddeletemention(cmdwithargs, mentionNick, message); gotmention = checkanddeletemention(cmdwithargs, mentionNick, message) || gotmention;
for (String mentionRole : (Iterable<String>) message.getRoleMentions().stream().map(r -> r.mention())::iterator) for (String mentionRole : (Iterable<String>) message.getRoleMentions().stream().map(r -> r.mention())::iterator)
gotmention = checkanddeletemention(cmdwithargs, mentionRole, message); gotmention = checkanddeletemention(cmdwithargs, mentionRole, message) || gotmention; // Delete all mentions
if (mentionedonly && !gotmention) { if (mentionedonly && !gotmention) {
message.getChannel().setTypingStatus(false); message.getChannel().setTypingStatus(false);
return false; return false;

View file

@ -84,8 +84,8 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
String dmessage = event.getMessage().getContent(); String dmessage = event.getMessage().getContent();
try { try {
Optional<? extends Player> player = Bukkit.getOnlinePlayers().stream().filter(p -> { // TODO: Support offline players Optional<? extends Player> player = Bukkit.getOnlinePlayers().stream().filter(p -> { // TODO: Support offline players
DiscordPlayer dp = TBMCPlayer.getPlayerAs(p, DiscordPlayer.class); // Online player, already loaded DiscordPlayer dp = TBMCPlayer.getPlayer(p.getUniqueId(), TBMCPlayer.class).getAs(DiscordPlayer.class); // Not changing any data, don't need to save
return author.getID().equals(dp.getDiscordID()); return dp != null && author.getID().equals(dp.getDiscordID());
}).findAny(); }).findAny();
final DiscordSenderBase dsender; final DiscordSenderBase dsender;
if (player.isPresent()) // Connected? if (player.isPresent()) // Connected?

View file

@ -21,28 +21,31 @@ import sx.blah.discord.handle.obj.Status.StatusType;
public class MCListener implements Listener { public class MCListener implements Listener {
@EventHandler @EventHandler
public void onPlayerJoin(TBMCPlayerJoinEvent e) { public void onPlayerJoin(TBMCPlayerJoinEvent e) {
final Player p = Bukkit.getPlayer(e.GetPlayer().getUuid()); final Player p = Bukkit.getPlayer(e.GetPlayer().getUUID());
if (ConnectCommand.WaitingToConnect.containsKey(e.GetPlayer().getPlayerName())) { if (ConnectCommand.WaitingToConnect.containsKey(e.GetPlayer().PlayerName().get())) {
p.sendMessage("§bTo connect with the Discord account " IUser user = DiscordPlugin.dc
+ ConnectCommand.WaitingToConnect.get(e.GetPlayer().getPlayerName()) + " do /discord accept"); .getUserByID(ConnectCommand.WaitingToConnect.get(e.GetPlayer().PlayerName().get()));
p.sendMessage("§bTo connect with the Discord account @" + user.getName() + "#" + user.getDiscriminator()
+ " do /discord accept");
p.sendMessage("§bIf it wasn't you, do /discord decline"); p.sendMessage("§bIf it wasn't you, do /discord decline");
} }
DiscordPlugin.sendMessageToChannel(DiscordPlugin.chatchannel, DiscordPlugin.sendMessageToChannel(DiscordPlugin.chatchannel,
e.GetPlayer().getPlayerName() + " joined the game"); e.GetPlayer().PlayerName().get() + " joined the game");
MCChatListener.ListC = 0; MCChatListener.ListC = 0;
} }
@EventHandler @EventHandler
public void onPlayerLeave(TBMCPlayerQuitEvent e) { public void onPlayerLeave(TBMCPlayerQuitEvent e) {
DiscordPlugin.sendMessageToChannel(DiscordPlugin.chatchannel, e.GetPlayer().getPlayerName() + " left the game"); DiscordPlugin.sendMessageToChannel(DiscordPlugin.chatchannel,
e.GetPlayer().PlayerName().get() + " left the game");
} }
@EventHandler @EventHandler
public void onGetInfo(TBMCPlayerGetInfoEvent e) { public void onGetInfo(TBMCPlayerGetInfoEvent e) {
if (DiscordPlugin.SafeMode) if (DiscordPlugin.SafeMode)
return; return;
DiscordPlayer dp = e.getPlayer().asPluginPlayer(DiscordPlayer.class); DiscordPlayer dp = e.getPlayer().getAs(DiscordPlayer.class);
if (dp.getDiscordID() == null || dp.getDiscordID() == "") if (dp == null || dp.getDiscordID() == null || dp.getDiscordID() == "")
return; return;
IUser user = DiscordPlugin.dc.getUserByID(dp.getDiscordID()); IUser user = DiscordPlugin.dc.getUserByID(dp.getDiscordID());
e.addInfo("Discord tag: " + user.getName() + "#" + user.getDiscriminator()); e.addInfo("Discord tag: " + user.getName() + "#" + user.getDiscriminator());

View file

@ -6,7 +6,9 @@ import org.bukkit.entity.Player;
import buttondevteam.discordplugin.DiscordPlayer; import buttondevteam.discordplugin.DiscordPlayer;
import buttondevteam.discordplugin.commands.ConnectCommand; import buttondevteam.discordplugin.commands.ConnectCommand;
import buttondevteam.discordplugin.listeners.MCChatListener; import buttondevteam.discordplugin.listeners.MCChatListener;
import buttondevteam.lib.player.ChromaGamerBase;
import buttondevteam.lib.player.TBMCPlayer; import buttondevteam.lib.player.TBMCPlayer;
import buttondevteam.lib.player.TBMCPlayerBase;
public class AcceptMCCommand extends DiscordMCCommandBase { public class AcceptMCCommand extends DiscordMCCommandBase {
@ -42,7 +44,11 @@ public class AcceptMCCommand extends DiscordMCCommandBase {
sender.sendMessage("§cYou don't have a pending connection to Discord."); sender.sendMessage("§cYou don't have a pending connection to Discord.");
return true; return true;
} }
TBMCPlayer.getPlayerAs((Player) sender, DiscordPlayer.class).setDiscordID(did); DiscordPlayer dp = ChromaGamerBase.getUser(did, DiscordPlayer.class);
TBMCPlayer mcp = TBMCPlayerBase.getPlayer(((Player) sender).getUniqueId(), TBMCPlayer.class);
dp.connectWith(mcp); // TODO: Fix null.yml and empty playerinfos
dp.save();
mcp.save();
ConnectCommand.WaitingToConnect.remove(sender.getName()); ConnectCommand.WaitingToConnect.remove(sender.getName());
MCChatListener.UnconnectedSenders.remove(did); MCChatListener.UnconnectedSenders.remove(did);
sender.sendMessage("§bAccounts connected."); sender.sendMessage("§bAccounts connected.");