mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-17 02:55:17 +00:00
Forge 1.19.1
Load natives from classpath Make command argument types a forge registry Add `EntityMobGriefingEvent` to `Allay#wantsToPickUp` Overhaul `ServerChatEvent` to use `ChatDecorator` system Remove `ClientChatEvent#setMessage` for now Gradle 7.5 Signed-off-by: SizableShrimp <[email protected]>
This commit is contained in:
1 parent
3c1126a7b4
commit
6845c094ab
98 files changed
+1025
-733
No files matched your search
@@ -5,63 +5,170 @@
|
||||
|
||||
package net.minecraftforge.event;
|
||||
|
||||
import net.minecraft.network.protocol.game.ServerboundChatPacket;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import java.util.Objects;
|
||||
import net.minecraft.client.Options;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.server.network.ServerGamePacketListenerImpl;
|
||||
import net.minecraftforge.common.ForgeHooks;
|
||||
import net.minecraft.network.protocol.game.ServerboundChatPacket;
|
||||
import net.minecraft.network.protocol.game.ServerboundChatPreviewPacket;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraftforge.common.ForgeMod;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.eventbus.api.Cancelable;
|
||||
import net.minecraftforge.eventbus.api.Event;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import net.minecraftforge.fml.LogicalSide;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* ServerChatEvent is fired whenever a C01PacketChatMessage is processed. <br>
|
||||
* This event is fired via {@link ForgeHooks#onServerChatEvent(ServerPlayer, String, Component)},
|
||||
* which is executed by the {@link ServerGamePacketListenerImpl#handleChat(ServerboundChatPacket)}<br>
|
||||
* <br>
|
||||
* {@link #username} contains the username of the player sending the chat message, unless fired from the server, where it is null.<br>
|
||||
* {@link #message} contains the message being sent.<br>
|
||||
* {@link #player} the instance of ServerPlayer for the player sending the chat message, or null when fired from the server.<br>
|
||||
* {@link #component} contains the instance of Component for the sent message.<br>
|
||||
* <br>
|
||||
* This event is {@link Cancelable}. <br>
|
||||
* If this event is canceled, the chat message is never distributed to all clients.<br>
|
||||
* <br>
|
||||
* This event does not have a result. {@link HasResult}<br>
|
||||
* <br>
|
||||
* This event is fired on the {@link MinecraftForge#EVENT_BUS}.
|
||||
* This event is fired whenever a {@link ServerboundChatPreviewPacket} or {@link ServerboundChatPacket} is received.
|
||||
* <p>
|
||||
* Mods that modify chat messages from this event should call {@link ForgeMod#enableServerChatPreview()}
|
||||
* from their mod constructor to enable the chat preview and allow signing by clients.
|
||||
* It is recommended to listen to this event class when modifying chat messages
|
||||
* rather than a subclass to ensure clients with {@link Options#onlyShowSecureChat()}
|
||||
* enabled can see the modified messages.
|
||||
* <p>
|
||||
* This event is {@linkplain Cancelable cancellable}, and does not {@linkplain HasResult have a result}.
|
||||
* If the event is cancelled, the message will not be sent to clients.
|
||||
* <p>
|
||||
* This event is fired on the {@linkplain MinecraftForge#EVENT_BUS main Forge event bus},
|
||||
* only on the {@linkplain LogicalSide#SERVER logical server}.
|
||||
*
|
||||
* @see ForgeMod#enableServerChatPreview()
|
||||
**/
|
||||
@Cancelable
|
||||
public class ServerChatEvent extends Event
|
||||
{
|
||||
private final String message;
|
||||
@Nullable
|
||||
private final String username;
|
||||
@Nullable
|
||||
private final ServerPlayer player;
|
||||
private Component component;
|
||||
private final String username;
|
||||
private final String rawText;
|
||||
private final boolean canChangeMessage;
|
||||
private Component message;
|
||||
|
||||
public ServerChatEvent(@Nullable ServerPlayer player, String message, Component component)
|
||||
@ApiStatus.Internal
|
||||
protected ServerChatEvent(ServerPlayer player, String rawText, Component message, boolean canChangeMessage)
|
||||
{
|
||||
super();
|
||||
this.message = message;
|
||||
this.player = player;
|
||||
this.username = player != null ? player.getGameProfile().getName() : null;
|
||||
this.component = component;
|
||||
this.username = player.getGameProfile().getName();
|
||||
this.rawText = rawText;
|
||||
this.canChangeMessage = canChangeMessage;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public void setComponent(Component e)
|
||||
/**
|
||||
* {@return the player who initiated the chat action}
|
||||
*/
|
||||
public ServerPlayer getPlayer()
|
||||
{
|
||||
this.component = e;
|
||||
return this.player;
|
||||
}
|
||||
|
||||
public Component getComponent()
|
||||
/**
|
||||
* {@return the username of the player who initiated the chat action}
|
||||
*/
|
||||
public String getUsername()
|
||||
{
|
||||
return this.component;
|
||||
return this.username;
|
||||
}
|
||||
|
||||
public String getMessage() { return this.message; }
|
||||
public @Nullable String getUsername() { return this.username; }
|
||||
public @Nullable ServerPlayer getPlayer() { return this.player; }
|
||||
/**
|
||||
* {@return the original raw text of the player chat message}
|
||||
*/
|
||||
public String getRawText()
|
||||
{
|
||||
return this.rawText;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@return whether the message can be changed or not}.
|
||||
* In vanilla, this will be false if a {@link ServerboundChatPacket} is received
|
||||
* with signed decorated content already included by the sending player.
|
||||
* Players who submit a chat message with chat previews disabled will cause this to be true.
|
||||
*/
|
||||
public boolean canChangeMessage()
|
||||
{
|
||||
return this.canChangeMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the message to be sent to the relevant clients.
|
||||
* <p>
|
||||
* If {@link #canChangeMessage()} is false, this call will be ignored.
|
||||
* <p>
|
||||
* It is recommended to listen to {@link ServerChatEvent} when modifying chat messages
|
||||
* rather than a subclass to ensure clients with {@link Options#onlyShowSecureChat()}
|
||||
* enabled can see the modified messages.
|
||||
*
|
||||
* @see ForgeMod#enableServerChatPreview()
|
||||
*/
|
||||
public void setMessage(Component message)
|
||||
{
|
||||
if (!this.canChangeMessage)
|
||||
return;
|
||||
|
||||
this.message = Objects.requireNonNull(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@return the message that will be sent to the relevant clients, if the event is not cancelled}
|
||||
*/
|
||||
public Component getMessage()
|
||||
{
|
||||
return this.message;
|
||||
}
|
||||
|
||||
/**
|
||||
* This event is fired whenever a {@link ServerboundChatPreviewPacket} is received from a client typing a message.
|
||||
* <p>
|
||||
* This event is {@linkplain Cancelable cancellable}, and does not {@linkplain HasResult have a result}.
|
||||
* If the event is cancelled, the chat preview sent back to the client will match the original message.
|
||||
* <p>
|
||||
* This event is fired on the {@linkplain MinecraftForge#EVENT_BUS main Forge event bus},
|
||||
* only on the {@linkplain LogicalSide#SERVER logical server}.
|
||||
**/
|
||||
public static class Preview extends ServerChatEvent
|
||||
{
|
||||
@ApiStatus.Internal
|
||||
public Preview(ServerPlayer player, String rawText, Component message)
|
||||
{
|
||||
super(player, rawText, message, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the chat preview to be sent back to the client.
|
||||
*/
|
||||
@Override
|
||||
public void setMessage(Component message)
|
||||
{
|
||||
super.setMessage(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@return the chat preview that will be sent back to the client, if the event is not cancelled}
|
||||
*/
|
||||
@Override
|
||||
public Component getMessage()
|
||||
{
|
||||
return super.getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This event is fired whenever a {@link ServerboundChatPacket} is received from a client
|
||||
* who has submitted their chat message.
|
||||
* <p>
|
||||
* This event is {@linkplain Cancelable cancellable}, and does not {@linkplain HasResult have a result}.
|
||||
* If the event is cancelled, the message will not be sent to clients.
|
||||
* <p>
|
||||
* This event is fired on the {@linkplain MinecraftForge#EVENT_BUS main Forge event bus},
|
||||
* only on the {@linkplain LogicalSide#SERVER logical server}.
|
||||
**/
|
||||
public static class Submitted extends ServerChatEvent
|
||||
{
|
||||
@ApiStatus.Internal
|
||||
public Submitted(ServerPlayer player, String rawText, Component message, boolean canChangeMessage)
|
||||
{
|
||||
super(player, rawText, message, canChangeMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user