Add feature flags to AsyncPlayerConfigurationEvent

This commit is contained in:
Not Flamgop 2024-06-14 06:06:30 -07:00 committed by Matt Worzala
parent 0f81bc31ff
commit 6766e3c7ed
2 changed files with 48 additions and 0 deletions

View File

@ -1,12 +1,18 @@
package net.minestom.server.event.player;
import it.unimi.dsi.fastutil.objects.ObjectArraySet;
import it.unimi.dsi.fastutil.objects.ObjectSets;
import net.minestom.server.entity.Player;
import net.minestom.server.event.trait.PlayerEvent;
import net.minestom.server.instance.Instance;
import net.minestom.server.network.packet.server.configuration.ResetChatPacket;
import net.minestom.server.network.packet.server.configuration.UpdateEnabledFeaturesPacket;
import net.minestom.server.utils.NamespaceID;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Set;
/**
* Called when a player enters the configuration state (either on first connection, or if they are
* sent back to configuration later). The player is moved to the play state as soon as all event
@ -20,6 +26,7 @@ public class AsyncPlayerConfigurationEvent implements PlayerEvent {
private final Player player;
private final boolean isFirstConfig;
private final ObjectArraySet<NamespaceID> featureFlags = new ObjectArraySet<>();
private boolean hardcore;
private boolean clearChat;
private boolean sendRegistryData;
@ -29,6 +36,8 @@ public class AsyncPlayerConfigurationEvent implements PlayerEvent {
this.player = player;
this.isFirstConfig = isFirstConfig;
this.featureFlags.add(NamespaceID.from("minecraft:vanilla")); // Vanilla feature-set, without this you get nothing at all. Kinda wacky!
this.hardcore = false;
this.clearChat = false;
this.sendRegistryData = isFirstConfig;
@ -55,6 +64,42 @@ public class AsyncPlayerConfigurationEvent implements PlayerEvent {
this.hardcore = hardcore;
}
/**
* Add a feature flag, see <a href="https://wiki.vg/Protocol#Feature_Flags">Wiki.vg Feature Flags</a> for a list of applicable features
* Note: the flag "minecraft:vanilla" is already included by default.
*
* @param feature A minecraft feature flag
*
* @see UpdateEnabledFeaturesPacket
*/
public void addFeatureFlag(@NotNull NamespaceID feature) {
this.featureFlags.add(feature);
}
/**
* Remove a feature flag, see <a href="https://wiki.vg/Protocol#Feature_Flags">Wiki.vg Feature Flags</a> for a list of applicable features
* Note: removing the flag "minecraft:vanilla" may result in weird behavior
*
* @param feature A minecraft feature flag
* @return if the feature specified existed prior to being removed
*
* @see UpdateEnabledFeaturesPacket
*/
public boolean removeFeatureFlag(@NotNull NamespaceID feature) {
return this.featureFlags.remove(feature); // Should this have sanity checking to see if the feature was actually contained in the list?
}
/**
* The list of currently added feature flags. This is an unmodifiable copy of what will be sent to the client.
*
* @return An unmodifiable set of feature flags
*
* @see UpdateEnabledFeaturesPacket
*/
public @NotNull Set<NamespaceID> getFeatureFlags() {
return ObjectSets.unmodifiable(this.featureFlags);
}
/**
* If true, the player's chat will be cleared when exiting the configuration state, otherwise
* it will be preserved. The default is not to clear the chat.

View File

@ -18,6 +18,7 @@ import net.minestom.server.network.packet.server.common.TagsPacket;
import net.minestom.server.network.packet.server.configuration.FinishConfigurationPacket;
import net.minestom.server.network.packet.server.configuration.ResetChatPacket;
import net.minestom.server.network.packet.server.configuration.SelectKnownPacksPacket;
import net.minestom.server.network.packet.server.configuration.UpdateEnabledFeaturesPacket;
import net.minestom.server.network.packet.server.login.LoginSuccessPacket;
import net.minestom.server.network.packet.server.play.StartConfigurationPacket;
import net.minestom.server.network.player.PlayerConnection;
@ -277,6 +278,8 @@ public final class ConnectionManager {
EventDispatcher.call(event);
if (!player.isOnline()) return; // Player was kicked during config.
player.sendPacket(new UpdateEnabledFeaturesPacket(event.getFeatureFlags())); // send player features that were enabled or disabled during async config event
final Instance spawningInstance = event.getSpawningInstance();
Check.notNull(spawningInstance, "You need to specify a spawning instance in the AsyncPlayerConfigurationEvent");