retrooper / packetevents

Precision meets performance—a powerful tool for mastering Minecraft packet manipulation with speed and finesse.
GNU General Public License v3.0
557 stars 151 forks source link

FEATURE REQUEST: Can get the server used by proxy #979

Closed Elikill58 closed 1 month ago

Elikill58 commented 2 months ago

Is your feature request related to a problem? Please describe. On the PacketSendEvent, the event.getPlayer() variable can be null (on velocity, for me, it's always null). So, I can't get the server concerned by the packet.

Describe the solution you'd like On the PacketSendEvent, add a variable for the server that can be:

Or maybe a custom object that summarize important things (server key, server name, amount of players and others). I just want to identify which server is it.

retrooper commented 1 month ago

Show us your code, and are you up to date?

Elikill58 commented 1 month ago

I was using the latest version, 2.4.0.

The code IS explained in the post. I'm using packet send évent on velocity

retrooper commented 1 month ago

I asked you if you are up to date, not if you were up to date. Also, no, your code and usage of the library is not explained at all. You could have missed a crucial step during initialization, thus, I asked for the code...

retrooper commented 1 month ago

The latest version is 2.5.0 now.

Elikill58 commented 1 month ago

In the main class:

@Subscribe(order = PostOrder.LATE)
public void onProxyInitialize(ProxyInitializeEvent event) {
    PacketEvents.setAPI(VelocityPacketEventsBuilder.build(getServer(), getContainer(), getLogger(), dataDirectory));
    PacketEvents.getAPI().load();
    PacketEvents.getAPI().getEventManager().registerListener(new MyListener());
}

My listener:

public class MyListener extends PacketListenerAbstract {

    @Override
    public void onPacketSend(PacketSendEvent e) {
        PacketTypeCommon packetType = e.getPacketType();
        if (packetType.equals(Server.SPAWN_ENTITY)) {
            MyPlugin.getInstance().getLogger().info("Player: " + e.getPlayer());
        }
    }
}

I always get log: Player: null.

Also, as there is event.getServerVersion(), that could be logic if there is event.getServerInfo() or event.getServerName(), mostly on proxy side.

retrooper commented 1 month ago

Are you on 2.5.0?

Elikill58 commented 1 month ago

Yes, I updated few days ago

retrooper commented 1 month ago

You need to call init() after registering your listener. Load does not suffice.

Elikill58 commented 1 month ago

Ok, now the issue is fixed.

It's sad we need so many things to enable the API, why they are not auto-init?

Also, as the original issue is fixed, you will not add server informations to the event?

retrooper commented 1 month ago

Because you chose to shade the plugin. If you depend on the plugin externally, you don't need to do any of this initialization.