Files
MinecraftForge/src/main/java/net/minecraftforge/event/ServerChatEvent.java
T

84 lines
2.3 KiB
Java
Raw Normal View History

/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.event;
2022-07-28 00:20:31 -05:00
import java.util.Objects;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.game.ServerboundChatPacket;
2021-07-17 00:08:49 -05:00
import net.minecraft.server.level.ServerPlayer;
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;
2022-06-07 15:31:26 -05:00
/**
2022-11-22 13:32:47 -06:00
* This event is fired whenever a {@link ServerboundChatPacket} is received from a client
* who has submitted their chat message.
2022-07-28 00:20:31 -05:00
* <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}.
**/
@Cancelable
2022-06-07 16:03:52 -05:00
public class ServerChatEvent extends Event
{
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 Component message;
2022-06-07 16:03:52 -05:00
2022-07-28 00:20:31 -05:00
@ApiStatus.Internal
2022-11-22 13:32:47 -06:00
public ServerChatEvent(ServerPlayer player, String rawText, Component message)
{
this.player = player;
2022-07-28 00:20:31 -05:00
this.username = player.getGameProfile().getName();
this.rawText = rawText;
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;
}
2022-07-28 00:20:31 -05:00
/**
* Set the message to be sent to the relevant clients.
*/
public void setMessage(Component message)
{
2022-07-28 00:20:31 -05:00
this.message = Objects.requireNonNull(message);
}
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;
}
}