StatusRequestEvent

Added basic event

notes:
 - minestom will only accept the 1.16.5 protocol version, as of now there is no way to get the protocol version from the event. Assume 1.16.5

- there is also no way to currently get the address the client used to ping the server, as that is sent in another packet and minestom does not currently pass that data through.

 - favicon appears to not be working
This commit is contained in:
thiccaxe 2021-04-04 15:43:19 -07:00
parent 2b72f91909
commit cf4a8756ef
4 changed files with 89 additions and 6 deletions

View File

@ -0,0 +1,61 @@
package net.minestom.server.event.server;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.Event;
import net.minestom.server.network.player.PlayerConnection;
import net.minestom.server.ping.ResponseData;
import org.jetbrains.annotations.NotNull;
/**
* Called by a StatusRequestPacket.
* Can be used to modify the {@link ResponseData} response data.
*/
public class StatusRequestEvent extends Event implements CancellableEvent {
private final ResponseData responseData;
private final PlayerConnection connection;
private boolean cancelled;
public StatusRequestEvent(@NotNull ResponseData responseData, @NotNull PlayerConnection connection) {
this.responseData = responseData;
this.connection = connection;
}
/**
* ResponseData being returned.
*
* @return the response data being returned
*/
public ResponseData getResponseData() {
return responseData;
}
/**
* PlayerConnection of received packet.
*
* Note that the player has not joined the server at this time.
*
* @return the playerConnection.
*/
public PlayerConnection getConnection() {
return connection;
}
@Override
public boolean isCancelled() {
return cancelled;
}
/**
* Cancelling this event will cause you server to appear offline in the vanilla server list.
*
* @param cancel true if the event should be cancelled, false otherwise
*/
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}

View File

@ -2,6 +2,7 @@ package net.minestom.server.network.packet.client.status;
import net.kyori.adventure.text.Component;
import net.minestom.server.MinecraftServer;
import net.minestom.server.event.server.StatusRequestEvent;
import net.minestom.server.network.packet.client.ClientPreplayPacket;
import net.minestom.server.network.packet.server.handshake.ResponsePacket;
import net.minestom.server.network.player.PlayerConnection;
@ -29,10 +30,17 @@ public class StatusRequestPacket implements ClientPreplayPacket {
if (consumer != null)
consumer.accept(connection, responseData);
ResponsePacket responsePacket = new ResponsePacket();
responsePacket.jsonResponse = responseData.build().toString();
// Call event
StatusRequestEvent statusRequestEvent = new StatusRequestEvent(responseData, connection);
MinecraftServer.getGlobalEventHandler().callEvent(StatusRequestEvent.class, statusRequestEvent);
connection.sendPacket(responsePacket);
// Send packet only if event has not been cancelled
if (!statusRequestEvent.isCancelled()) {
ResponsePacket responsePacket = new ResponsePacket();
responsePacket.jsonResponse = responseData.build().toString();
connection.sendPacket(responsePacket);
}
}
@Override

View File

@ -8,14 +8,18 @@ import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.minestom.server.MinecraftServer;
import net.minestom.server.command.CommandManager;
import net.minestom.server.event.server.StatusRequestEvent;
import net.minestom.server.extras.optifine.OptifineSupport;
import net.minestom.server.instance.block.BlockManager;
import net.minestom.server.instance.block.rule.vanilla.RedstonePlacementRule;
import net.minestom.server.ping.ResponseData;
import net.minestom.server.storage.StorageManager;
import net.minestom.server.storage.systems.FileStorageSystem;
import net.minestom.server.utils.time.TimeUnit;
import net.minestom.server.utils.time.UpdateOption;
import java.util.UUID;
public class Main {
@ -60,6 +64,12 @@ public class Main {
MinecraftServer.getSchedulerManager().buildShutdownTask(() -> System.out.println("Good night")).schedule();
MinecraftServer.getGlobalEventHandler().addEventCallback(StatusRequestEvent.class, event -> {
ResponseData responseData = event.getResponseData();
responseData.addPlayer("IP test: " + event.getConnection().getRemoteAddress().toString(), UUID.randomUUID());
responseData.addPlayer("Use " + (char)0x00a7 + "6section characters for formatting: (char)0x00a7", UUID.randomUUID());
});
PlayerInit.init();
OptifineSupport.enable();

View File

@ -3,6 +3,7 @@ package demo;
import demo.generator.ChunkGeneratorDemo;
import demo.generator.NoiseTestGenerator;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextColor;
import net.minestom.server.MinecraftServer;
import net.minestom.server.adventure.audience.Audiences;
import net.minestom.server.benchmark.BenchmarkManager;
@ -252,9 +253,12 @@ public class PlayerInit {
return (playerConnection, responseData) -> {
responseData.setMaxPlayer(0);
responseData.setOnline(MinecraftServer.getConnectionManager().getOnlinePlayers().size());
responseData.addPlayer("A name", UUID.randomUUID());
responseData.addPlayer("Could be some message", UUID.randomUUID());
responseData.setDescription("IP test: " + playerConnection.getRemoteAddress());
responseData.addPlayer("The first line is separated from the others", UUID.randomUUID());
responseData.addPlayer("Could be a name, or a message", UUID.randomUUID());
responseData.setDescription(Component.text("You can do ")
.append(Component.text("RGB", TextColor.color(0x66b3ff)))
.append(Component.text(" color here")));
};
}