Skip to content

Commit 47eb133

Browse files
committed
💥 Rework status management in event
A manager now handles status requests and triggers the status event. The status event has been reworked to support get/set response, and disable automatic sending.
1 parent a2bb272 commit 47eb133

11 files changed

Lines changed: 207 additions & 33 deletions

File tree

api/src/main/java/net/transferproxy/api/event/EventType.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
import net.transferproxy.api.event.listener.EmptyListener;
2929
import net.transferproxy.api.event.listener.EventListener;
3030
import net.transferproxy.api.event.login.PreLoginEvent;
31+
import net.transferproxy.api.event.status.StatusRequestEvent;
3132
import net.transferproxy.api.network.connection.PlayerConnection;
32-
import net.transferproxy.api.status.listener.DefaultStatusListener;
3333
import org.jetbrains.annotations.Contract;
3434
import org.jetbrains.annotations.NotNull;
3535

@@ -40,7 +40,7 @@ public enum EventType {
4040

4141
HANDSHAKE(PlayerConnection.class, EmptyListener::getInstance),
4242
PRE_LOGIN(PreLoginEvent.class, EmptyListener::getInstance),
43-
STATUS(PlayerConnection.class, DefaultStatusListener::new),
43+
STATUS(StatusRequestEvent.class, EmptyListener::getInstance),
4444
READY(PlayerConnection.class, DefaultReadyListener::new);
4545

4646
private final Class<?> eventClass;

api/src/main/java/net/transferproxy/api/event/listener/StatusListener.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,21 @@
2424

2525
package net.transferproxy.api.event.listener;
2626

27-
import net.transferproxy.api.network.connection.PlayerConnection;
27+
import net.transferproxy.api.event.status.StatusRequestEvent;
2828
import org.jetbrains.annotations.NotNull;
2929

3030
/**
3131
* The listener used to handle every status request
3232
*/
3333
@FunctionalInterface
34-
public interface StatusListener extends EventListener<PlayerConnection> {
34+
public interface StatusListener extends EventListener<StatusRequestEvent> {
3535

3636
/**
3737
* Handle the status request
3838
*
39-
* @param connection The {@link PlayerConnection} who request
39+
* @param event The called event
4040
*/
4141
@Override
42-
void handle(final @NotNull PlayerConnection connection);
42+
void handle(final @NotNull StatusRequestEvent event);
4343

4444
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2025 Yvan Mazy
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package net.transferproxy.api.event.status;
26+
27+
import net.transferproxy.api.network.connection.PlayerConnection;
28+
import net.transferproxy.api.status.StatusResponse;
29+
import org.jetbrains.annotations.Contract;
30+
import org.jetbrains.annotations.NotNull;
31+
import org.jetbrains.annotations.Nullable;
32+
33+
import java.util.Objects;
34+
35+
public class StatusRequestEvent {
36+
37+
private final PlayerConnection connection;
38+
private StatusResponse response;
39+
private boolean canSendResponsePacket;
40+
41+
public StatusRequestEvent(final @NotNull PlayerConnection connection) {
42+
this(connection, true);
43+
}
44+
45+
public StatusRequestEvent(final @NotNull PlayerConnection connection, final boolean canSendResponsePacket) {
46+
this.connection = Objects.requireNonNull(connection, "connection must not be null");
47+
this.canSendResponsePacket = canSendResponsePacket;
48+
}
49+
50+
@Contract(pure = true)
51+
public @NotNull PlayerConnection getConnection() {
52+
return this.connection;
53+
}
54+
55+
@Contract(pure = true)
56+
public @Nullable StatusResponse getResponse() {
57+
return this.response;
58+
}
59+
60+
public void setResponse(final @Nullable StatusResponse response) {
61+
this.response = response;
62+
}
63+
64+
@Contract(pure = true)
65+
public boolean canSendResponsePacket() {
66+
return this.canSendResponsePacket;
67+
}
68+
69+
public void setCanSendResponsePacket(final boolean canSendSuccessPacket) {
70+
this.canSendResponsePacket = canSendSuccessPacket;
71+
}
72+
73+
}

api/src/main/java/net/transferproxy/api/module/ModuleManager.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import net.transferproxy.api.event.EventManager;
2828
import net.transferproxy.api.network.packet.provider.PacketProviderGroup;
2929
import net.transferproxy.api.plugin.PluginManager;
30+
import net.transferproxy.api.status.StatusManager;
3031
import net.transferproxy.api.terminal.TerminalExecutor;
3132
import org.jetbrains.annotations.Contract;
3233
import org.jetbrains.annotations.NotNull;
@@ -51,6 +52,14 @@ public interface ModuleManager {
5152
@Contract(pure = true)
5253
@NotNull EventManager getEventManager();
5354

55+
/**
56+
* Gets the current {@link StatusManager} implementation.
57+
*
58+
* @return the status manager
59+
*/
60+
@Contract(pure = true)
61+
@NotNull StatusManager getStatusManager();
62+
5463
/**
5564
* Gets the current {@link PluginManager} implementation.
5665
*
@@ -84,6 +93,13 @@ public interface ModuleManager {
8493
*/
8594
void setEventManager(final @NotNull EventManager eventManager);
8695

96+
/**
97+
* Sets the {@link StatusManager} implementation to use.
98+
*
99+
* @param statusManager the status manager
100+
*/
101+
void setStatusManager(final @NotNull StatusManager statusManager);
102+
87103
/**
88104
* Sets the {@link PluginManager} implementation to use.
89105
*
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2025 Yvan Mazy
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package net.transferproxy.api.status;
26+
27+
import net.transferproxy.api.network.connection.PlayerConnection;
28+
import org.jetbrains.annotations.Contract;
29+
import org.jetbrains.annotations.NotNull;
30+
31+
public interface StatusManager {
32+
33+
void process(final @NotNull PlayerConnection connection);
34+
35+
@Contract("_ -> new")
36+
@NotNull StatusResponse buildDefaultResponse(final int protocol);
37+
38+
}

api/src/main/java/net/transferproxy/api/status/listener/CachedStatusListener.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
package net.transferproxy.api.status.listener;
2626

2727
import net.transferproxy.api.event.listener.StatusListener;
28-
import net.transferproxy.api.network.connection.PlayerConnection;
28+
import net.transferproxy.api.event.status.StatusRequestEvent;
2929
import net.transferproxy.api.status.StatusResponse;
3030
import org.jetbrains.annotations.NotNull;
3131

@@ -40,8 +40,8 @@ public CachedStatusListener(final @NotNull StatusResponse response) {
4040
}
4141

4242
@Override
43-
public void handle(final @NotNull PlayerConnection connection) {
44-
connection.sendStatusResponse(this.response);
43+
public void handle(final @NotNull StatusRequestEvent event) {
44+
event.setResponse(this.response);
4545
}
4646

4747
}

api/src/main/java/net/transferproxy/api/status/listener/DynamicStatusListener.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,16 @@
2525
package net.transferproxy.api.status.listener;
2626

2727
import net.transferproxy.api.event.listener.StatusListener;
28+
import net.transferproxy.api.event.status.StatusRequestEvent;
2829
import net.transferproxy.api.network.connection.PlayerConnection;
2930
import net.transferproxy.api.status.StatusResponse;
3031
import org.jetbrains.annotations.NotNull;
3132

3233
public abstract class DynamicStatusListener implements StatusListener {
3334

3435
@Override
35-
public void handle(final @NotNull PlayerConnection connection) {
36-
connection.sendStatusResponse(this.buildResponse(connection));
36+
public void handle(final @NotNull StatusRequestEvent event) {
37+
event.setResponse(this.buildResponse(event.getConnection()));
3738
}
3839

3940
protected abstract StatusResponse buildResponse(final @NotNull PlayerConnection connection);

core/src/main/java/net/transferproxy/module/ModuleManagerImpl.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@
2828
import net.transferproxy.api.module.ModuleManager;
2929
import net.transferproxy.api.network.packet.provider.PacketProviderGroup;
3030
import net.transferproxy.api.plugin.PluginManager;
31-
import net.transferproxy.api.terminal.TerminalExecutor;
31+
import net.transferproxy.api.status.StatusManager;
3232
import net.transferproxy.api.terminal.DefaultTerminalExecutor;
33+
import net.transferproxy.api.terminal.TerminalExecutor;
3334
import net.transferproxy.event.EventManagerImpl;
3435
import net.transferproxy.network.packet.provider.PacketProviderGroups;
3536
import net.transferproxy.plugin.PluginManagerImpl;
37+
import net.transferproxy.status.StatusManagerImpl;
3638
import org.jetbrains.annotations.NotNull;
3739
import org.jetbrains.annotations.VisibleForTesting;
3840

@@ -42,6 +44,7 @@
4244
public class ModuleManagerImpl implements ModuleManager {
4345

4446
private EventManager eventManager;
47+
private StatusManager statusManager;
4548
private PluginManager pluginManager;
4649
private IntFunction<PacketProviderGroup> packetProviderGroupFunction;
4750
private TerminalExecutor terminalExecutor;
@@ -56,6 +59,11 @@ public void initializeDefaults() {
5659
return this.eventManager;
5760
}
5861

62+
@Override
63+
public @NotNull StatusManager getStatusManager() {
64+
return this.statusManager;
65+
}
66+
5967
@Override
6068
public @NotNull PluginManager getPluginManager() {
6169
return this.pluginManager;
@@ -81,6 +89,11 @@ public void setEventManager(final @NotNull EventManager eventManager) {
8189
this.eventManager = Objects.requireNonNull(eventManager, "eventManager must not be null");
8290
}
8391

92+
@Override
93+
public void setStatusManager(final @NotNull StatusManager statusManager) {
94+
this.statusManager = Objects.requireNonNull(statusManager, "statusManager must not be null");
95+
}
96+
8497
@Override
8598
public void setPacketProviderGroupFunction(final @NotNull IntFunction<PacketProviderGroup> packetProviderGroupFunction) {
8699
this.packetProviderGroupFunction =
@@ -97,6 +110,9 @@ public void initializeDefaults(final boolean force) {
97110
if (force || this.eventManager == null) {
98111
this.eventManager = new EventManagerImpl();
99112
}
113+
if (force || this.statusManager == null) {
114+
this.statusManager = new StatusManagerImpl();
115+
}
100116
if (force || this.pluginManager == null) {
101117
this.pluginManager = new PluginManagerImpl();
102118
}

core/src/main/java/net/transferproxy/network/packet/status/serverbound/StatusRequestPacket.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
import io.netty.buffer.ByteBuf;
2828
import net.transferproxy.api.TransferProxy;
29-
import net.transferproxy.api.event.EventType;
3029
import net.transferproxy.api.network.connection.PlayerConnection;
3130
import net.transferproxy.api.network.packet.serverbound.ServerboundPacket;
3231
import net.transferproxy.api.network.protocol.Protocolized;
@@ -40,7 +39,7 @@ public StatusRequestPacket(final @SuppressWarnings("unused") @NotNull ByteBuf bu
4039

4140
@Override
4241
public void handle(final @NotNull PlayerConnection connection) {
43-
TransferProxy.getInstance().getModuleManager().getEventManager().call(EventType.STATUS, connection);
42+
TransferProxy.getInstance().getModuleManager().getStatusManager().process(connection);
4443
}
4544

4645
@Override

core/src/main/java/net/transferproxy/status/SnapshotStatusListener.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
package net.transferproxy.status;
2626

2727
import net.transferproxy.api.event.listener.StatusListener;
28-
import net.transferproxy.api.network.connection.PlayerConnection;
28+
import net.transferproxy.api.event.status.StatusRequestEvent;
2929
import net.transferproxy.api.network.packet.built.ProtocolizedBuiltPacket;
3030
import net.transferproxy.api.status.StatusResponse;
3131
import net.transferproxy.api.util.ComponentProtocolUtil;
@@ -46,8 +46,9 @@ public SnapshotStatusListener(final @NotNull StatusResponse response) {
4646
}
4747

4848
@Override
49-
public void handle(final @NotNull PlayerConnection connection) {
50-
connection.sendPacket(this.builtPacket);
49+
public void handle(final @NotNull StatusRequestEvent event) {
50+
event.setCanSendResponsePacket(false);
51+
event.getConnection().sendPacket(this.builtPacket);
5152
}
5253

5354
}

0 commit comments

Comments
 (0)