|
4 | 4 | import net.badlion.modapicommon.AbstractBadlionApi; |
5 | 5 | import net.md_5.bungee.api.connection.ProxiedPlayer; |
6 | 6 | import net.md_5.bungee.api.event.PostLoginEvent; |
| 7 | +import net.md_5.bungee.api.event.ServerSwitchEvent; |
7 | 8 | import net.md_5.bungee.api.plugin.Listener; |
8 | 9 | import net.md_5.bungee.event.EventHandler; |
9 | 10 | import net.md_5.bungee.protocol.packet.PluginMessage; |
10 | 11 |
|
| 12 | +import java.lang.reflect.InvocationTargetException; |
| 13 | +import java.lang.reflect.Method; |
| 14 | +import java.util.logging.Level; |
| 15 | + |
11 | 16 | public class PlayerListener implements Listener { |
12 | 17 |
|
| 18 | + private final Method serverSwitchEventGetFromMethod; |
13 | 19 | private final BungeeBadlionPlugin plugin; |
14 | 20 |
|
15 | 21 | public PlayerListener(BungeeBadlionPlugin plugin) { |
16 | 22 | this.plugin = plugin; |
| 23 | + |
| 24 | + Method method; |
| 25 | + |
| 26 | + try { |
| 27 | + //noinspection JavaReflectionMemberAccess |
| 28 | + method = ServerSwitchEvent.class.getDeclaredMethod("getFrom"); |
| 29 | + } catch (NoSuchMethodException ignored) { |
| 30 | + method = null; |
| 31 | + |
| 32 | + this.plugin.getLogger().warning("ServerSwitchEvent.getFrom() was not found, this BungeeCord version is maybe outdated and might not support 1.20.3+ properly"); |
| 33 | + } |
| 34 | + |
| 35 | + this.serverSwitchEventGetFromMethod = method; |
17 | 36 | } |
18 | 37 |
|
19 | 38 | @EventHandler |
20 | 39 | public void onLogin(PostLoginEvent event) { |
21 | | - // Send the disallowed mods to players when they login to the proxy. A notification will appear on the Badlion Client so they know the mod was disabled |
22 | | - ProxiedPlayer player = event.getPlayer(); |
| 40 | + // On 1.20.3+, the player is still in LOGIN protocol at this point, we can't send that packet yet |
| 41 | + if (event.getPlayer().getPendingConnection().getVersion() < 764) { |
| 42 | + this.sendModPacket(event.getPlayer()); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @EventHandler |
| 47 | + public void onServerSwitch(ServerSwitchEvent event) { |
| 48 | + // Send it on the initial server switch instead |
| 49 | + if (event.getPlayer().getPendingConnection().getVersion() >= 764 && this.serverSwitchEventGetFromMethod != null) { |
| 50 | + boolean initialLogin; |
| 51 | + |
| 52 | + try { |
| 53 | + initialLogin = this.serverSwitchEventGetFromMethod.invoke(event) == null; |
| 54 | + } catch (IllegalAccessException | InvocationTargetException ignored) { |
| 55 | + initialLogin = false; |
| 56 | + } |
| 57 | + |
| 58 | + if (initialLogin) { |
| 59 | + this.sendModPacket(event.getPlayer()); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private void sendModPacket(ProxiedPlayer player) { |
| 65 | + this.plugin.getLogger().severe(AbstractBadlionApi.GSON_NON_PRETTY.toJson(this.plugin.getBadlionApi().getBadlionConfig())); |
| 66 | + // Send the disallowed mods to players when they log in to the proxy. A notification will appear on the Badlion Client, so they know the mod was disabled |
23 | 67 | player.unsafe().sendPacket(new PluginMessage("badlion:modapi", AbstractBadlionApi.GSON_NON_PRETTY.toJson(this.plugin.getBadlionApi().getBadlionConfig()).getBytes(), false)); |
24 | 68 | } |
25 | 69 | } |
0 commit comments