Implement plugins as event listeners

This commit is contained in:
Chipmunk 2022-12-24 09:43:33 -05:00
parent b8e03415c9
commit 6feabfc10a
2 changed files with 16 additions and 16 deletions

View file

@ -5,6 +5,7 @@ import com.github.steveice10.mc.protocol.MinecraftProtocol;
import com.github.steveice10.packetlib.ProxyInfo; import com.github.steveice10.packetlib.ProxyInfo;
import com.github.steveice10.packetlib.Session; import com.github.steveice10.packetlib.Session;
import com.github.steveice10.packetlib.tcp.TcpClientSession; import com.github.steveice10.packetlib.tcp.TcpClientSession;
import com.github.steveice10.packetlib.event.session.SessionListener;
import java.util.Map; import java.util.Map;
import java.util.HashMap; import java.util.HashMap;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
@ -28,6 +29,7 @@ public class Client {
try { try {
Plugin plugin = pluginClass.newInstance(); Plugin plugin = pluginClass.newInstance();
plugin.inject(this, null); plugin.inject(this, null);
if (plugin instanceof SessionListener) session.addListener((SessionListener) plugin);
plugins.put(plugin.id(), plugin); plugins.put(plugin.id(), plugin);
} catch (Exception ignored) { } catch (Exception ignored) {
} }

View file

@ -15,7 +15,7 @@ import java.util.List;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.UUID; import java.util.UUID;
public class PlayerListPlugin implements Plugin { public class PlayerListPlugin extends SessionAdapter implements Plugin {
private Client client; private Client client;
public List<MutablePlayerListEntry> list = new ArrayList<MutablePlayerListEntry>(); public List<MutablePlayerListEntry> list = new ArrayList<MutablePlayerListEntry>();
@ -24,25 +24,23 @@ public class PlayerListPlugin implements Plugin {
@Override @Override
public void inject (Client client, JsonObject options) { public void inject (Client client, JsonObject options) {
this.client = client; this.client = client;
}
client.session().addListener(new SessionAdapter() { @Override
@Override public void packetReceived (Session session, Packet packet) {
public void packetReceived (Session session, Packet packet) { if (packet instanceof ClientboundPlayerInfoPacket) {
if (packet instanceof ClientboundPlayerInfoPacket) { ClientboundPlayerInfoPacket _packet = (ClientboundPlayerInfoPacket) packet;
ClientboundPlayerInfoPacket _packet = (ClientboundPlayerInfoPacket) packet;
PlayerListEntryAction action = _packet.getAction(); PlayerListEntryAction action = _packet.getAction();
for (PlayerListEntry entry : _packet.getEntries()) { for (PlayerListEntry entry : _packet.getEntries()) {
if (action == PlayerListEntryAction.ADD_PLAYER) addPlayer(entry); if (action == PlayerListEntryAction.ADD_PLAYER) addPlayer(entry);
else if (action == PlayerListEntryAction.UPDATE_GAMEMODE) updateGamemode(entry); else if (action == PlayerListEntryAction.UPDATE_GAMEMODE) updateGamemode(entry);
else if (action == PlayerListEntryAction.UPDATE_LATENCY) updateLatency(entry); else if (action == PlayerListEntryAction.UPDATE_LATENCY) updateLatency(entry);
else if (action == PlayerListEntryAction.UPDATE_DISPLAY_NAME) updateDisplayName(entry); else if (action == PlayerListEntryAction.UPDATE_DISPLAY_NAME) updateDisplayName(entry);
else if (action == PlayerListEntryAction.REMOVE_PLAYER) removePlayer(entry); else if (action == PlayerListEntryAction.REMOVE_PLAYER) removePlayer(entry);
}
}
} }
}); }
} }
public final MutablePlayerListEntry getEntry (UUID uuid) { public final MutablePlayerListEntry getEntry (UUID uuid) {