Skip to content

Commit 270ede9

Browse files
committed
Fixed BungeeCord ModApi on 1.20.3+
1 parent 3559585 commit 270ede9

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

bungee/src/main/java/net/badlion/bungeeapi/listener/PlayerListener.java

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,66 @@
44
import net.badlion.modapicommon.AbstractBadlionApi;
55
import net.md_5.bungee.api.connection.ProxiedPlayer;
66
import net.md_5.bungee.api.event.PostLoginEvent;
7+
import net.md_5.bungee.api.event.ServerSwitchEvent;
78
import net.md_5.bungee.api.plugin.Listener;
89
import net.md_5.bungee.event.EventHandler;
910
import net.md_5.bungee.protocol.packet.PluginMessage;
1011

12+
import java.lang.reflect.InvocationTargetException;
13+
import java.lang.reflect.Method;
14+
import java.util.logging.Level;
15+
1116
public class PlayerListener implements Listener {
1217

18+
private final Method serverSwitchEventGetFromMethod;
1319
private final BungeeBadlionPlugin plugin;
1420

1521
public PlayerListener(BungeeBadlionPlugin plugin) {
1622
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;
1736
}
1837

1938
@EventHandler
2039
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
2367
player.unsafe().sendPacket(new PluginMessage("badlion:modapi", AbstractBadlionApi.GSON_NON_PRETTY.toJson(this.plugin.getBadlionApi().getBadlionConfig()).getBytes(), false));
2468
}
2569
}

0 commit comments

Comments
 (0)