Skip to content

Commit

Permalink
Fixed BungeeCord ModApi on 1.20.3+
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigner committed Mar 18, 2024
1 parent 3559585 commit 270ede9
Showing 1 changed file with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,66 @@
import net.badlion.modapicommon.AbstractBadlionApi;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.PostLoginEvent;
import net.md_5.bungee.api.event.ServerSwitchEvent;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.event.EventHandler;
import net.md_5.bungee.protocol.packet.PluginMessage;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.logging.Level;

public class PlayerListener implements Listener {

private final Method serverSwitchEventGetFromMethod;
private final BungeeBadlionPlugin plugin;

public PlayerListener(BungeeBadlionPlugin plugin) {
this.plugin = plugin;

Method method;

try {
//noinspection JavaReflectionMemberAccess
method = ServerSwitchEvent.class.getDeclaredMethod("getFrom");
} catch (NoSuchMethodException ignored) {
method = null;

this.plugin.getLogger().warning("ServerSwitchEvent.getFrom() was not found, this BungeeCord version is maybe outdated and might not support 1.20.3+ properly");
}

this.serverSwitchEventGetFromMethod = method;
}

@EventHandler
public void onLogin(PostLoginEvent event) {
// 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
ProxiedPlayer player = event.getPlayer();
// On 1.20.3+, the player is still in LOGIN protocol at this point, we can't send that packet yet
if (event.getPlayer().getPendingConnection().getVersion() < 764) {
this.sendModPacket(event.getPlayer());
}
}

@EventHandler
public void onServerSwitch(ServerSwitchEvent event) {
// Send it on the initial server switch instead
if (event.getPlayer().getPendingConnection().getVersion() >= 764 && this.serverSwitchEventGetFromMethod != null) {
boolean initialLogin;

try {
initialLogin = this.serverSwitchEventGetFromMethod.invoke(event) == null;
} catch (IllegalAccessException | InvocationTargetException ignored) {
initialLogin = false;
}

if (initialLogin) {
this.sendModPacket(event.getPlayer());
}
}
}

private void sendModPacket(ProxiedPlayer player) {
this.plugin.getLogger().severe(AbstractBadlionApi.GSON_NON_PRETTY.toJson(this.plugin.getBadlionApi().getBadlionConfig()));

This comment has been minimized.

Copy link
@LaserSlime

LaserSlime Mar 21, 2024

@Rigner You forgot to remove the debug message.

This comment has been minimized.

Copy link
@Rigner

Rigner Apr 10, 2024

Author Member

Woops, thanks!

This comment has been minimized.

Copy link
@Rigner

Rigner Apr 10, 2024

Author Member

Edited & updated the relase.

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

0 comments on commit 270ede9

Please sign in to comment.