Implemented new player data thingy
This commit is contained in:
parent
dfeae5de9e
commit
6ce730016d
7 changed files with 55 additions and 39 deletions
|
@ -1,13 +1,18 @@
|
||||||
package buttondevteam.discordplugin;
|
package buttondevteam.discordplugin;
|
||||||
|
|
||||||
import buttondevteam.lib.player.TBMCPlayer;
|
import buttondevteam.lib.player.ChromaGamerBase;
|
||||||
|
|
||||||
public class DiscordPlayer extends TBMCPlayer {
|
public class DiscordPlayer extends ChromaGamerBase {
|
||||||
public String getDiscordID() {
|
public String getDiscordID() {
|
||||||
return getData();
|
return plugindata.getString("id");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDiscordID(String id) {
|
public void setDiscordID(String id) {
|
||||||
setData(id);
|
plugindata.set("id", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getFileName() {
|
||||||
|
return "discord";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,10 +13,17 @@ import sx.blah.discord.handle.obj.IUser;
|
||||||
public class DiscordSender extends DiscordSenderBase implements CommandSender {
|
public class DiscordSender extends DiscordSenderBase implements CommandSender {
|
||||||
private PermissibleBase perm = new PermissibleBase(this);
|
private PermissibleBase perm = new PermissibleBase(this);
|
||||||
|
|
||||||
|
private String name = null;
|
||||||
|
|
||||||
public DiscordSender(IUser user, IChannel channel) {
|
public DiscordSender(IUser user, IChannel channel) {
|
||||||
super(user, channel);
|
super(user, channel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public DiscordSender(IUser user, IChannel channel, String name) {
|
||||||
|
super(user, channel);
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isPermissionSet(String name) {
|
public boolean isPermissionSet(String name) {
|
||||||
return perm.isPermissionSet(name);
|
return perm.isPermissionSet(name);
|
||||||
|
@ -92,7 +99,7 @@ public class DiscordSender extends DiscordSenderBase implements CommandSender {
|
||||||
public String getName() {
|
public String getName() {
|
||||||
if (user == null)
|
if (user == null)
|
||||||
return "Discord user";
|
return "Discord user";
|
||||||
return user.getDisplayName(DiscordPlugin.mainServer);
|
return name == null ? user.getDisplayName(DiscordPlugin.mainServer) : name;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ import com.google.common.collect.HashBiMap;
|
||||||
import buttondevteam.discordplugin.DiscordPlayer;
|
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.ChromaGamerBase;
|
||||||
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 +47,8 @@ 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 (DiscordPlayer pl = ChromaGamerBase.getUser(message.getAuthor().getID(), DiscordPlayer.class)) {
|
||||||
if (message.getAuthor().getID().equals(pl.asPluginPlayer(DiscordPlayer.class).getDiscordID())) {
|
if (message.getAuthor().getID().equals(pl.getDiscordID())) {
|
||||||
DiscordPlugin.sendMessageToChannel(message.getChannel(), "You already have this account connected.");
|
DiscordPlugin.sendMessageToChannel(message.getChannel(), "You already have this account connected.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,8 +6,9 @@ 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;
|
||||||
|
|
||||||
|
@ -68,20 +69,13 @@ 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()) {
|
StringBuilder uinfo = new StringBuilder("User info for ").append(target.getName()).append(":\n");
|
||||||
DiscordPlayer dp = player.asPluginPlayer(DiscordPlayer.class);
|
uinfo.append(dp.getInfo(InfoTarget.Discord));
|
||||||
if (target.getID().equals(dp.getDiscordID())) {
|
DiscordPlugin.sendMessageToChannel(message.getChannel(), uinfo.toString());
|
||||||
StringBuilder uinfo = new StringBuilder("User info for ").append(target.getName()).append(":\n");
|
} catch (Exception e) {
|
||||||
uinfo.append(player.getInfo(InfoTarget.Discord));
|
TBMCCoreAPI.SendException("Error while getting info about " + target.getName() + "!", e);
|
||||||
DiscordPlugin.sendMessageToChannel(message.getChannel(), uinfo.toString());
|
|
||||||
found = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
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) {
|
||||||
|
|
|
@ -13,6 +13,7 @@ import buttondevteam.discordplugin.*;
|
||||||
import buttondevteam.lib.*;
|
import buttondevteam.lib.*;
|
||||||
import buttondevteam.lib.chat.Channel;
|
import buttondevteam.lib.chat.Channel;
|
||||||
import buttondevteam.lib.chat.TBMCChatAPI;
|
import buttondevteam.lib.chat.TBMCChatAPI;
|
||||||
|
import buttondevteam.lib.player.ChromaGamerBase;
|
||||||
import buttondevteam.lib.player.TBMCPlayer;
|
import buttondevteam.lib.player.TBMCPlayer;
|
||||||
import sx.blah.discord.api.events.IListener;
|
import sx.blah.discord.api.events.IListener;
|
||||||
import sx.blah.discord.api.internal.json.objects.EmbedObject;
|
import sx.blah.discord.api.internal.json.objects.EmbedObject;
|
||||||
|
@ -83,20 +84,21 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
||||||
return;
|
return;
|
||||||
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
|
DiscordPlayer dp = ChromaGamerBase.getUser(author.getID(), DiscordPlayer.class);
|
||||||
DiscordPlayer dp = TBMCPlayer.getPlayerAs(p, DiscordPlayer.class); // Online player, already loaded
|
|
||||||
return author.getID().equals(dp.getDiscordID());
|
|
||||||
}).findAny();
|
|
||||||
final DiscordSenderBase dsender;
|
final DiscordSenderBase dsender;
|
||||||
if (player.isPresent()) // Connected?
|
Player mcp = null; // Offline players can't really run commands
|
||||||
{ // Execute as ingame player
|
final String cid;
|
||||||
|
if ((cid = dp.getConnectedID(TBMCPlayer.class)) != null // Connected?
|
||||||
|
&& (mcp = Bukkit.getPlayer(cid)) != null) { // Execute as ingame player, if online
|
||||||
if (!ConnectedSenders.containsKey(author.getID()))
|
if (!ConnectedSenders.containsKey(author.getID()))
|
||||||
ConnectedSenders.put(author.getID(),
|
ConnectedSenders.put(author.getID(),
|
||||||
new DiscordPlayerSender(author, event.getMessage().getChannel(), player.get()));
|
new DiscordPlayerSender(author, event.getMessage().getChannel(), mcp));
|
||||||
dsender = ConnectedSenders.get(author.getID());
|
dsender = ConnectedSenders.get(author.getID());
|
||||||
} else {
|
} else {
|
||||||
|
TBMCPlayer p = dp.getAs(TBMCPlayer.class);
|
||||||
if (!UnconnectedSenders.containsKey(author.getID()))
|
if (!UnconnectedSenders.containsKey(author.getID()))
|
||||||
UnconnectedSenders.put(author.getID(), new DiscordSender(author, event.getMessage().getChannel()));
|
UnconnectedSenders.put(author.getID(), new DiscordSender(author, event.getMessage().getChannel(),
|
||||||
|
p == null ? null : p.getPlayerName())); // Display the playername, if found
|
||||||
dsender = UnconnectedSenders.get(author.getID());
|
dsender = UnconnectedSenders.get(author.getID());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,7 +110,7 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
||||||
|
|
||||||
if (dmessage.startsWith("/")) {
|
if (dmessage.startsWith("/")) {
|
||||||
final String cmd = dmessage.substring(1).toLowerCase();
|
final String cmd = dmessage.substring(1).toLowerCase();
|
||||||
if (!player.isPresent()
|
if (mcp == null
|
||||||
&& !Arrays.stream(UnconnectedCmds).anyMatch(s -> cmd.equals(s) || cmd.startsWith(s + " "))) {
|
&& !Arrays.stream(UnconnectedCmds).anyMatch(s -> cmd.equals(s) || cmd.startsWith(s + " "))) {
|
||||||
// Command not whitelisted
|
// Command not whitelisted
|
||||||
DiscordPlugin.sendMessageToChannel(event.getMessage().getChannel(), // TODO
|
DiscordPlugin.sendMessageToChannel(event.getMessage().getChannel(), // TODO
|
||||||
|
|
|
@ -11,17 +11,15 @@ import org.bukkit.event.server.ServerCommandEvent;
|
||||||
import buttondevteam.discordplugin.DiscordPlayer;
|
import buttondevteam.discordplugin.DiscordPlayer;
|
||||||
import buttondevteam.discordplugin.DiscordPlugin;
|
import buttondevteam.discordplugin.DiscordPlugin;
|
||||||
import buttondevteam.discordplugin.commands.ConnectCommand;
|
import buttondevteam.discordplugin.commands.ConnectCommand;
|
||||||
import buttondevteam.lib.player.TBMCPlayerGetInfoEvent;
|
import buttondevteam.lib.player.*;
|
||||||
import buttondevteam.lib.player.TBMCPlayerJoinEvent;
|
import net.ess3.api.events.*;
|
||||||
import buttondevteam.lib.player.TBMCPlayerQuitEvent;
|
|
||||||
import net.ess3.api.events.AfkStatusChangeEvent;
|
|
||||||
import sx.blah.discord.handle.obj.IUser;
|
import sx.blah.discord.handle.obj.IUser;
|
||||||
import sx.blah.discord.handle.obj.Status.StatusType;
|
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().getPlayerName())) {
|
||||||
p.sendMessage("§bTo connect with the Discord account "
|
p.sendMessage("§bTo connect with the Discord account "
|
||||||
+ ConnectCommand.WaitingToConnect.get(e.GetPlayer().getPlayerName()) + " do /discord accept");
|
+ ConnectCommand.WaitingToConnect.get(e.GetPlayer().getPlayerName()) + " do /discord accept");
|
||||||
|
@ -41,7 +39,7 @@ public class MCListener implements Listener {
|
||||||
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.getDiscordID() == null || dp.getDiscordID() == "")
|
||||||
return;
|
return;
|
||||||
IUser user = DiscordPlugin.dc.getUserByID(dp.getDiscordID());
|
IUser user = DiscordPlugin.dc.getUserByID(dp.getDiscordID());
|
||||||
|
@ -71,4 +69,9 @@ public class MCListener implements Listener {
|
||||||
public void onServerCommand(ServerCommandEvent e) {
|
public void onServerCommand(ServerCommandEvent e) {
|
||||||
DiscordPlugin.Restart = !e.getCommand().equalsIgnoreCase("stop"); // The variable is always true except if stopped
|
DiscordPlugin.Restart = !e.getCommand().equalsIgnoreCase("stop"); // The variable is always true except if stopped
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*@EventHandler
|
||||||
|
public void onPlayerMute(MuteStatusChangeEvent e) {
|
||||||
|
e.getAffected()
|
||||||
|
}*/
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ 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;
|
||||||
|
|
||||||
public class AcceptMCCommand extends DiscordMCCommandBase {
|
public class AcceptMCCommand extends DiscordMCCommandBase {
|
||||||
|
@ -42,7 +43,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);
|
final TBMCPlayer mcp = ChromaGamerBase.getUser(((Player) sender).getUniqueId().toString(), TBMCPlayer.class);
|
||||||
|
final DiscordPlayer dp = ChromaGamerBase.getUser(did, DiscordPlayer.class);
|
||||||
|
mcp.connectWith(dp);
|
||||||
|
mcp.save();
|
||||||
|
dp.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.");
|
||||||
|
|
Loading…
Reference in a new issue