forked from neoforged/NeoForge
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ClientInformationUpdatedEvent (neoforged#1365)
- Loading branch information
Showing
4 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/net/neoforged/neoforge/event/entity/player/ClientInformationUpdatedEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
.../main/java/net/neoforged/neoforge/oldtest/entity/player/ClientInformationUpdatedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters