Player data things, LPInjector update start

This commit is contained in:
Norbi Peti 2020-10-25 01:58:58 +02:00
parent d3ae53cd46
commit 40fe1093e0
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
8 changed files with 44 additions and 58 deletions

View file

@ -180,7 +180,7 @@ public class DiscordPlugin extends ButtonPlugin {
CommonListeners.register(dc.getEventDispatcher()); CommonListeners.register(dc.getEventDispatcher());
TBMCCoreAPI.RegisterEventsForExceptions(new MCListener(), this); TBMCCoreAPI.RegisterEventsForExceptions(new MCListener(), this);
TBMCCoreAPI.RegisterUserClass(DiscordPlayer.class); TBMCCoreAPI.RegisterUserClass(DiscordPlayer.class, DiscordPlayer::new);
ChromaGamerBase.addConverter(sender -> Optional.ofNullable(sender instanceof DiscordSenderBase ChromaGamerBase.addConverter(sender -> Optional.ofNullable(sender instanceof DiscordSenderBase
? ((DiscordSenderBase) sender).getChromaUser() : null)); ? ((DiscordSenderBase) sender).getChromaUser() : null));

View file

@ -1,8 +1,6 @@
package buttondevteam.discordplugin.commands; package buttondevteam.discordplugin.commands;
import buttondevteam.discordplugin.DiscordPlayer; import buttondevteam.discordplugin.DiscordPlayer;
import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.chat.Command2; import buttondevteam.lib.chat.Command2;
import buttondevteam.lib.chat.CommandClass; import buttondevteam.lib.chat.CommandClass;
import buttondevteam.lib.player.TBMCPlayer; import buttondevteam.lib.player.TBMCPlayer;
@ -42,20 +40,16 @@ public class ConnectCommand extends ICommand2DC {
channel.createMessage("The specified Minecraft player cannot be found").subscribe(); channel.createMessage("The specified Minecraft player cannot be found").subscribe();
return true; return true;
} }
try (TBMCPlayer pl = TBMCPlayerBase.getPlayer(p.getUniqueId(), TBMCPlayer.class)) { TBMCPlayer pl = TBMCPlayerBase.getPlayer(p.getUniqueId(), TBMCPlayer.class);
DiscordPlayer dp = pl.getAs(DiscordPlayer.class); DiscordPlayer dp = pl.getAs(DiscordPlayer.class);
if (dp != null && author.getId().asString().equals(dp.getDiscordID())) { if (dp != null && author.getId().asString().equals(dp.getDiscordID())) {
channel.createMessage("You already have this account connected.").subscribe(); channel.createMessage("You already have this account connected.").subscribe();
return true; return true;
} }
} catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while connecting a Discord account!", e, DiscordPlugin.plugin);
channel.createMessage("An internal error occured!\n" + e).subscribe();
}
WaitingToConnect.put(p.getName(), author.getId().asString()); WaitingToConnect.put(p.getName(), author.getId().asString());
channel.createMessage( channel.createMessage(
"Alright! Now accept the connection in Minecraft from the account " + Minecraftname "Alright! Now accept the connection in Minecraft from the account " + Minecraftname
+ " before the next server restart. You can also adjust the Minecraft name you want to connect to with the same command.").subscribe(); + " before the next server restart. You can also adjust the Minecraft name you want to connect to by running this command again.").subscribe();
if (p.isOnline()) if (p.isOnline())
((Player) p).sendMessage("§bTo connect with the Discord account " + author.getUsername() + "#" ((Player) p).sendMessage("§bTo connect with the Discord account " + author.getUsername() + "#"
+ author.getDiscriminator() + " do /discord accept"); + author.getDiscriminator() + " do /discord accept");

View file

@ -2,7 +2,6 @@ package buttondevteam.discordplugin.commands;
import buttondevteam.discordplugin.DiscordPlayer; import buttondevteam.discordplugin.DiscordPlayer;
import buttondevteam.discordplugin.DiscordPlugin; import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.chat.Command2; import buttondevteam.lib.chat.Command2;
import buttondevteam.lib.chat.CommandClass; import buttondevteam.lib.chat.CommandClass;
import buttondevteam.lib.player.ChromaGamerBase; import buttondevteam.lib.player.ChromaGamerBase;
@ -67,14 +66,10 @@ public class UserinfoCommand extends ICommand2DC {
sender.sendMessage("An error occurred."); sender.sendMessage("An error occurred.");
return true; return true;
} }
try (DiscordPlayer dp = ChromaGamerBase.getUser(target.getId().asString(), DiscordPlayer.class)) { DiscordPlayer dp = ChromaGamerBase.getUser(target.getId().asString(), DiscordPlayer.class);
StringBuilder uinfo = new StringBuilder("User info for ").append(target.getUsername()).append(":\n"); StringBuilder uinfo = new StringBuilder("User info for ").append(target.getUsername()).append(":\n");
uinfo.append(dp.getInfo(InfoTarget.Discord)); uinfo.append(dp.getInfo(InfoTarget.Discord));
channel.createMessage(uinfo.toString()).subscribe(); channel.createMessage(uinfo.toString()).subscribe();
} catch (Exception e) {
channel.createMessage("An error occured while getting the user!").subscribe();
TBMCCoreAPI.SendException("Error while getting info about " + target.getUsername() + "!", e, DiscordPlugin.plugin);
}
return true; return true;
} }

View file

@ -7,21 +7,21 @@ import buttondevteam.discordplugin.mcchat.MinecraftChatModule;
import buttondevteam.discordplugin.util.DPState; import buttondevteam.discordplugin.util.DPState;
import buttondevteam.lib.TBMCCommandPreprocessEvent; import buttondevteam.lib.TBMCCommandPreprocessEvent;
import buttondevteam.lib.player.TBMCPlayerGetInfoEvent; import buttondevteam.lib.player.TBMCPlayerGetInfoEvent;
import buttondevteam.lib.player.TBMCPlayerJoinEvent;
import discord4j.common.util.Snowflake; import discord4j.common.util.Snowflake;
import discord4j.core.object.entity.Member; import discord4j.core.object.entity.Member;
import discord4j.core.object.entity.User; import discord4j.core.object.entity.User;
import lombok.val; import lombok.val;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
public class MCListener implements Listener { public class MCListener implements Listener {
@EventHandler @EventHandler
public void onPlayerJoin(TBMCPlayerJoinEvent e) { public void onPlayerJoin(PlayerJoinEvent e) {
if (ConnectCommand.WaitingToConnect.containsKey(e.GetPlayer().PlayerName().get())) { if (ConnectCommand.WaitingToConnect.containsKey(e.getPlayer().getName())) {
@SuppressWarnings("ConstantConditions") User user = DiscordPlugin.dc @SuppressWarnings("ConstantConditions") User user = DiscordPlugin.dc
.getUserById(Snowflake.of(ConnectCommand.WaitingToConnect.get(e.GetPlayer().PlayerName().get()))).block(); .getUserById(Snowflake.of(ConnectCommand.WaitingToConnect.get(e.getPlayer().getName()))).block();
if (user == null) return; if (user == null) return;
e.getPlayer().sendMessage("§bTo connect with the Discord account @" + user.getUsername() + "#" + user.getDiscriminator() e.getPlayer().sendMessage("§bTo connect with the Discord account @" + user.getUsername() + "#" + user.getDiscriminator()
+ " do /discord accept"); + " do /discord accept");

View file

@ -5,7 +5,6 @@ import buttondevteam.discordplugin.DiscordPlayer;
import buttondevteam.discordplugin.DiscordPlugin; import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.discordplugin.commands.Command2DCSender; import buttondevteam.discordplugin.commands.Command2DCSender;
import buttondevteam.discordplugin.commands.ICommand2DC; import buttondevteam.discordplugin.commands.ICommand2DC;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.chat.Command2; import buttondevteam.lib.chat.Command2;
import buttondevteam.lib.chat.CommandClass; import buttondevteam.lib.chat.CommandClass;
import discord4j.core.object.entity.channel.PrivateChannel; import discord4j.core.object.entity.channel.PrivateChannel;
@ -36,15 +35,12 @@ public class MCChatCommand extends ICommand2DC {
DPUtils.reply(message, channel, "this command can only be issued in a direct message with the bot.").subscribe(); DPUtils.reply(message, channel, "this command can only be issued in a direct message with the bot.").subscribe();
return true; return true;
} }
try (final DiscordPlayer user = DiscordPlayer.getUser(author.getId().asString(), DiscordPlayer.class)) { final DiscordPlayer user = DiscordPlayer.getUser(author.getId().asString(), DiscordPlayer.class);
boolean mcchat = !user.isMinecraftChatEnabled(); boolean mcchat = !user.isMinecraftChatEnabled();
MCChatPrivate.privateMCChat(channel, mcchat, author, user); MCChatPrivate.privateMCChat(channel, mcchat, author, user);
DPUtils.reply(message, channel, "Minecraft chat " + (mcchat // DPUtils.reply(message, channel, "Minecraft chat " + (mcchat //
? "enabled. Use '" + DiscordPlugin.getPrefix() + "mcchat' again to turn it off." // ? "enabled. Use '" + DiscordPlugin.getPrefix() + "mcchat' again to turn it off." //
: "disabled.")).subscribe(); : "disabled.")).subscribe();
} catch (Exception e) {
TBMCCoreAPI.SendException("Error while setting mcchat for user " + author.getUsername() + "#" + author.getDiscriminator(), e, module);
}
return true; return true;
} // TODO: Pin channel switching to indicate the current channel } // TODO: Pin channel switching to indicate the current channel

View file

@ -338,8 +338,8 @@ public class MCChatListener implements Listener {
if (dmessage.length() == 0 && event.getMessage().getAttachments().size() == 0 if (dmessage.length() == 0 && event.getMessage().getAttachments().size() == 0
&& !isPrivate && event.getMessage().getType() == Message.Type.CHANNEL_PINNED_MESSAGE) { && !isPrivate && event.getMessage().getType() == Message.Type.CHANNEL_PINNED_MESSAGE) {
val rtr = clmd != null ? clmd.mcchannel.getRTR(clmd.dcp) val rtr = clmd != null ? clmd.mcchannel.getRTR(clmd.dcp)
: dsender.getChromaUser().channel().get().getRTR(dsender); : dsender.getChromaUser().channel.get().getRTR(dsender);
TBMCChatAPI.SendSystemMessage(clmd != null ? clmd.mcchannel : dsender.getChromaUser().channel().get(), rtr, TBMCChatAPI.SendSystemMessage(clmd != null ? clmd.mcchannel : dsender.getChromaUser().channel.get(), rtr,
(dsender instanceof Player ? ((Player) dsender).getDisplayName() (dsender instanceof Player ? ((Player) dsender).getDisplayName()
: dsender.getName()) + " pinned a message on Discord.", TBMCSystemChatEvent.BroadcastTarget.ALL); : dsender.getName()) + " pinned a message on Discord.", TBMCSystemChatEvent.BroadcastTarget.ALL);
} else { } else {
@ -374,7 +374,7 @@ public class MCChatListener implements Listener {
} }
module.log(dsender.getName() + " ran from DC: /" + cmd); module.log(dsender.getName() + " ran from DC: /" + cmd);
if (runCustomCommand(dsender, cmdlowercased)) return true; if (runCustomCommand(dsender, cmdlowercased)) return true;
val channel = clmd == null ? user.channel().get() : clmd.mcchannel; val channel = clmd == null ? user.channel.get() : clmd.mcchannel;
val ev = new TBMCCommandPreprocessEvent(dsender, channel, dmessage, clmd == null ? dsender : clmd.dcp); val ev = new TBMCCommandPreprocessEvent(dsender, channel, dmessage, clmd == null ? dsender : clmd.dcp);
Bukkit.getScheduler().runTask(DiscordPlugin.plugin, //Commands need to be run sync Bukkit.getScheduler().runTask(DiscordPlugin.plugin, //Commands need to be run sync
() -> { () -> {

View file

@ -38,8 +38,6 @@ public class DiscordMCCommand extends ICommand2MC {
DiscordPlayer dp = ChromaGamerBase.getUser(did, DiscordPlayer.class); DiscordPlayer dp = ChromaGamerBase.getUser(did, DiscordPlayer.class);
TBMCPlayer mcp = TBMCPlayerBase.getPlayer(player.getUniqueId(), TBMCPlayer.class); TBMCPlayer mcp = TBMCPlayerBase.getPlayer(player.getUniqueId(), TBMCPlayer.class);
dp.connectWith(mcp); dp.connectWith(mcp);
dp.save();
mcp.save();
ConnectCommand.WaitingToConnect.remove(player.getName()); ConnectCommand.WaitingToConnect.remove(player.getName());
MCChatUtils.UnconnectedSenders.remove(did); //Remove all unconnected, will be recreated where needed MCChatUtils.UnconnectedSenders.remove(did); //Remove all unconnected, will be recreated where needed
player.sendMessage("§bAccounts connected."); player.sendMessage("§bAccounts connected.");

View file

@ -10,8 +10,10 @@ import me.lucko.luckperms.bukkit.inject.permissible.DummyPermissibleBase;
import me.lucko.luckperms.bukkit.inject.permissible.LuckPermsPermissible; import me.lucko.luckperms.bukkit.inject.permissible.LuckPermsPermissible;
import me.lucko.luckperms.bukkit.listeners.BukkitConnectionListener; import me.lucko.luckperms.bukkit.listeners.BukkitConnectionListener;
import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.locale.message.Message; import me.lucko.luckperms.common.locale.Message;
import me.lucko.luckperms.common.locale.TranslationManager;
import me.lucko.luckperms.common.model.User; import me.lucko.luckperms.common.model.User;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
@ -92,9 +94,9 @@ public final class LPInjector implements Listener { //Disable login event for Lu
final DiscordConnectedPlayer player = (DiscordConnectedPlayer) e.getPlayer(); final DiscordConnectedPlayer player = (DiscordConnectedPlayer) e.getPlayer();
/*if (plugin.getConfiguration().get(ConfigKeys.DEBUG_LOGINS)) { if (plugin.getConfiguration().get(ConfigKeys.DEBUG_LOGINS)) {
plugin.getLogger().info("Processing login for " + player.getUniqueId() + " - " + player.getName()); plugin.getLogger().info("Processing login for " + player.getUniqueId() + " - " + player.getName());
}*/ }
final User user = plugin.getUserManager().getIfLoaded(player.getUniqueId()); final User user = plugin.getUserManager().getIfLoaded(player.getUniqueId());
@ -102,7 +104,7 @@ public final class LPInjector implements Listener { //Disable login event for Lu
if (user == null) { if (user == null) {
deniedLogin.add(player.getUniqueId()); deniedLogin.add(player.getUniqueId());
if (!connectionListener.getUniqueConnections().contains(player.getUniqueId())) { if (!plugin.getConnectionListener().getUniqueConnections().contains(player.getUniqueId())) {
plugin.getLogger().warn("User " + player.getUniqueId() + " - " + player.getName() + plugin.getLogger().warn("User " + player.getUniqueId() + " - " + player.getName() +
" doesn't have data pre-loaded, they have never been processed during pre-login in this session." + " doesn't have data pre-loaded, they have never been processed during pre-login in this session." +
@ -111,7 +113,9 @@ public final class LPInjector implements Listener { //Disable login event for Lu
try { try {
if ((Boolean) detectedCraftBukkitOfflineMode.get(connectionListener)) { if ((Boolean) detectedCraftBukkitOfflineMode.get(connectionListener)) {
printCraftBukkitOfflineModeError.invoke(connectionListener); printCraftBukkitOfflineModeError.invoke(connectionListener);
e.disallow(PlayerLoginEvent.Result.KICK_OTHER, Message.LOADING_STATE_ERROR_CB_OFFLINE_MODE.asString(plugin.getLocaleManager()));
Component reason = TranslationManager.render(Message.LOADING_STATE_ERROR_CB_OFFLINE_MODE.build(), player.getLocale());
e.disallow(PlayerLoginEvent.Result.KICK_OTHER, LegacyComponentSerializer.legacySection().serialize(reason));
return; return;
} }
} catch (IllegalAccessException | InvocationTargetException ex) { } catch (IllegalAccessException | InvocationTargetException ex) {
@ -124,32 +128,31 @@ public final class LPInjector implements Listener { //Disable login event for Lu
" - denying login."); " - denying login.");
} }
e.disallow(PlayerLoginEvent.Result.KICK_OTHER, Message.LOADING_STATE_ERROR.asString(plugin.getLocaleManager())); Component reason = TranslationManager.render(Message.LOADING_STATE_ERROR.build(), player.getLocale());
e.disallow(PlayerLoginEvent.Result.KICK_OTHER, LegacyComponentSerializer.legacySection().serialize(reason));
return; return;
} }
// User instance is there, now we can inject our custom Permissible into the player. // User instance is there, now we can inject our custom Permissible into the player.
// Care should be taken at this stage to ensure that async tasks which manipulate bukkit data check that the player is still online. // Care should be taken at this stage to ensure that async tasks which manipulate bukkit data check that the player is still online.
try { try {
// get the existing PermissibleBase held by the player
PermissibleBase oldPermissible = player.getPerm();
// Make a new permissible for the user // Make a new permissible for the user
LuckPermsPermissible lpPermissible = new LuckPermsPermissible(player, user, plugin); LuckPermsPermissible lpPermissible = new LuckPermsPermissible(player, user, this.plugin);
// Inject into the player // Inject into the player
inject(player, lpPermissible, oldPermissible); inject(player, lpPermissible);
} catch (Throwable t) { } catch (Throwable t) {
plugin.getLogger().warn("Exception thrown when setting up permissions for " + plugin.getLogger().warn("Exception thrown when setting up permissions for " +
player.getUniqueId() + " - " + player.getName() + " - denying login."); player.getUniqueId() + " - " + player.getName() + " - denying login.");
t.printStackTrace(); t.printStackTrace();
e.disallow(PlayerLoginEvent.Result.KICK_OTHER, Message.LOADING_SETUP_ERROR.asString(plugin.getLocaleManager())); Component reason = TranslationManager.render(Message.LOADING_SETUP_ERROR.build(), player.getLocale());
//return; e.disallow(PlayerLoginEvent.Result.KICK_OTHER, LegacyComponentSerializer.legacySection().serialize(reason));
return;
} }
//this.plugin.getContextManager().signalContextUpdate(player); this.plugin.getContextManager().signalContextUpdate(player);
} }
// Wait until the last priority to unload, so plugins can still perform permission checks on this event // Wait until the last priority to unload, so plugins can still perform permission checks on this event
@ -160,14 +163,14 @@ public final class LPInjector implements Listener { //Disable login event for Lu
final DiscordConnectedPlayer player = (DiscordConnectedPlayer) e.getPlayer(); final DiscordConnectedPlayer player = (DiscordConnectedPlayer) e.getPlayer();
connectionListener.handleDisconnect(player.getUniqueId()); handleDisconnect(player.getUniqueId());
// perform unhooking from bukkit objects 1 tick later. // perform unhooking from bukkit objects 1 tick later.
// this allows plugins listening after us on MONITOR to still have intact permissions data // this allows plugins listening after us on MONITOR to still have intact permissions data
this.plugin.getBootstrap().getServer().getScheduler().runTaskLaterAsynchronously(this.plugin.getBootstrap(), () -> { this.plugin.getBootstrap().getServer().getScheduler().runTaskLater(this.plugin.getBootstrap(), () -> {
// Remove the custom permissible // Remove the custom permissible
try { try {
uninject(player); uninject(player, true);
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }