2016-06-22 23:49:47 -04:00
|
|
|
/*
|
2022-04-14 03:42:00 +00:00
|
|
|
* Copyright (c) Forge Development LLC and contributors
|
2022-02-28 13:41:23 -08:00
|
|
|
* SPDX-License-Identifier: LGPL-2.1-only
|
2016-06-22 23:49:47 -04:00
|
|
|
*/
|
|
|
|
|
|
2012-10-28 06:58:23 -04:00
|
|
|
package net.minecraftforge.event;
|
|
|
|
|
|
2022-07-28 00:20:31 -05:00
|
|
|
import java.util.Objects;
|
|
|
|
|
import net.minecraft.client.Options;
|
|
|
|
|
import net.minecraft.network.chat.Component;
|
2021-11-26 05:09:56 +08:00
|
|
|
import net.minecraft.network.protocol.game.ServerboundChatPacket;
|
2022-07-28 00:20:31 -05:00
|
|
|
import net.minecraft.network.protocol.game.ServerboundChatPreviewPacket;
|
2021-07-17 00:08:49 -05:00
|
|
|
import net.minecraft.server.level.ServerPlayer;
|
2022-07-28 00:20:31 -05:00
|
|
|
import net.minecraftforge.common.ForgeMod;
|
2021-11-26 05:09:56 +08:00
|
|
|
import net.minecraftforge.common.MinecraftForge;
|
2018-06-10 21:12:46 -04:00
|
|
|
import net.minecraftforge.eventbus.api.Cancelable;
|
2022-06-07 16:03:52 -05:00
|
|
|
import net.minecraftforge.eventbus.api.Event;
|
2022-07-28 00:20:31 -05:00
|
|
|
import net.minecraftforge.fml.LogicalSide;
|
|
|
|
|
import org.jetbrains.annotations.ApiStatus;
|
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
2022-06-07 15:31:26 -05:00
|
|
|
|
2014-07-06 16:07:49 -04:00
|
|
|
/**
|
2022-07-28 00:20:31 -05:00
|
|
|
* 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()
|
2014-07-06 16:07:49 -04:00
|
|
|
**/
|
2012-10-28 06:58:23 -04:00
|
|
|
@Cancelable
|
2022-06-07 16:03:52 -05:00
|
|
|
public class ServerChatEvent extends Event
|
2013-02-15 03:22:59 -08:00
|
|
|
{
|
2022-06-07 16:03:52 -05:00
|
|
|
private final ServerPlayer player;
|
2022-07-28 00:20:31 -05:00
|
|
|
private final String username;
|
|
|
|
|
private final String rawText;
|
|
|
|
|
private final boolean canChangeMessage;
|
|
|
|
|
private Component message;
|
2022-06-07 16:03:52 -05:00
|
|
|
|
2022-07-28 00:20:31 -05:00
|
|
|
@ApiStatus.Internal
|
|
|
|
|
protected ServerChatEvent(ServerPlayer player, String rawText, Component message, boolean canChangeMessage)
|
2012-10-28 06:58:23 -04:00
|
|
|
{
|
|
|
|
|
this.player = player;
|
2022-07-28 00:20:31 -05:00
|
|
|
this.username = player.getGameProfile().getName();
|
|
|
|
|
this.rawText = rawText;
|
|
|
|
|
this.canChangeMessage = canChangeMessage;
|
|
|
|
|
this.message = message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* {@return the player who initiated the chat action}
|
|
|
|
|
*/
|
|
|
|
|
public ServerPlayer getPlayer()
|
|
|
|
|
{
|
|
|
|
|
return this.player;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* {@return the username of the player who initiated the chat action}
|
|
|
|
|
*/
|
|
|
|
|
public String getUsername()
|
|
|
|
|
{
|
|
|
|
|
return this.username;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* {@return the original raw text of the player chat message}
|
|
|
|
|
*/
|
|
|
|
|
public String getRawText()
|
|
|
|
|
{
|
|
|
|
|
return this.rawText;
|
2012-10-28 06:58:23 -04:00
|
|
|
}
|
2015-05-25 12:02:47 -07:00
|
|
|
|
2022-07-28 00:20:31 -05:00
|
|
|
/**
|
|
|
|
|
* {@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()
|
2015-05-25 12:02:47 -07:00
|
|
|
{
|
2022-07-28 00:20:31 -05:00
|
|
|
return this.canChangeMessage;
|
2015-05-25 12:02:47 -07:00
|
|
|
}
|
|
|
|
|
|
2022-07-28 00:20:31 -05:00
|
|
|
/**
|
|
|
|
|
* 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)
|
2015-05-25 12:02:47 -07:00
|
|
|
{
|
2022-07-28 00:20:31 -05:00
|
|
|
if (!this.canChangeMessage)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
this.message = Objects.requireNonNull(message);
|
2015-05-25 12:02:47 -07:00
|
|
|
}
|
2016-03-01 20:42:36 -08:00
|
|
|
|
2022-07-28 00:20:31 -05:00
|
|
|
/**
|
|
|
|
|
* {@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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-10-28 06:58:23 -04:00
|
|
|
}
|