Skip to content

Commit

Permalink
Add ClientInformationUpdatedEvent (neoforged#1365)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla authored Jul 26, 2024
1 parent 2b44cea commit 2fe9f9b
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,22 @@
}

@Override
@@ -1753,7 +_,7 @@
@@ -1753,13 +_,15 @@
@Override
public void handlePlayerAbilities(ServerboundPlayerAbilitiesPacket p_9887_) {
PacketUtils.ensureRunningOnSameThread(p_9887_, this, this.player.serverLevel());
- this.player.getAbilities().flying = p_9887_.isFlying() && this.player.getAbilities().mayfly;
+ this.player.getAbilities().flying = p_9887_.isFlying() && this.player.mayFly();
}

@Override
public void handleClientInformation(ServerboundClientInformationPacket p_301979_) {
PacketUtils.ensureRunningOnSameThread(p_301979_, this, this.player.serverLevel());
+ net.minecraft.server.level.ClientInformation oldInfo = this.player.clientInformation();
this.player.updateOptions(p_301979_.information());
+ net.neoforged.neoforge.common.NeoForge.EVENT_BUS.post(new net.neoforged.neoforge.event.entity.player.ClientInformationUpdatedEvent(this.player, oldInfo, p_301979_.information()));
}

@Override
@@ -1812,7 +_,7 @@
this.connection
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) NeoForged and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.neoforge.event.entity.player;

import net.minecraft.server.level.ClientInformation;
import net.minecraft.server.level.ServerPlayer;
import net.neoforged.neoforge.common.NeoForge;

/**
* ClientInformationUpdatedEvent is fired when a player changes server-synced client options,
* specifically those in {@link net.minecraft.server.level.ClientInformation}.
* <p>
* This event is fired on the {@link NeoForge#EVENT_BUS}.
**/
public class ClientInformationUpdatedEvent extends PlayerEvent {
private final ClientInformation oldInformation;
private final ClientInformation updatedInformation;

public ClientInformationUpdatedEvent(ServerPlayer player, ClientInformation oldInfo, ClientInformation newInfo) {
super(player);
this.oldInformation = oldInfo;
this.updatedInformation = newInfo;
}

@Override
public ServerPlayer getEntity() {
return (ServerPlayer) super.getEntity();
}

/**
* Returns the new client info to be applied to the player.
* Sometimes the client resends unchanged options, so if that matters
* for your use case, check equality with {@link #getOldInformation()}.
*
* @return updated information
*/
public ClientInformation getUpdatedInformation() {
return this.updatedInformation;
}

/**
* Returns the existing client info from to the player.
* <p>
* May be blank or defaulted initial data on first event call for a player instance.
*
* @return updated information
*/
public ClientInformation getOldInformation() {
return this.oldInformation;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) NeoForged and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.neoforge.oldtest.entity.player;

import net.minecraft.network.chat.Component;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.entity.player.ClientInformationUpdatedEvent;

/**
* Tests the client info update event by telling a player their new and old info on updates.
* Easiest thing to test is change your language or view distance.
*/
@Mod(ClientInformationUpdatedTest.MOD_ID)
public class ClientInformationUpdatedTest {
public static final String MOD_ID = "client_information_updated_test";

public ClientInformationUpdatedTest() {
NeoForge.EVENT_BUS.addListener((ClientInformationUpdatedEvent event) -> {
event.getEntity().sendSystemMessage(Component.literal("old: " + event.getOldInformation()));
event.getEntity().sendSystemMessage(Component.literal("new: " + event.getUpdatedInformation()));
});
}
}
2 changes: 2 additions & 0 deletions tests/src/main/resources/META-INF/neoforge.mods.toml
Original file line number Diff line number Diff line change
Expand Up @@ -246,5 +246,7 @@ modId="player_spawn_phantoms_event_test"
modId="conditional_codec_test"
[[mods]]
modId="may_fly_attribute_item"
[[mods]]
modId="client_information_updated_test"

# ADD ABOVE THIS LINE

0 comments on commit 2fe9f9b

Please sign in to comment.