forked from cayennemc/SimpleChat
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPlayerChatCallback.java
107 lines (96 loc) · 3.47 KB
/
PlayerChatCallback.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package me.vetustus.server.simplechat.api.event;
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.server.network.ServerPlayerEntity;
/**
* Callback for when a player writes something to the chat.
*
* <p>To work with the chat, you need to register a listener and implement a callback.</p>
*
* <p>All actions are performed through a special {@link ChatMessage} object.</p>
*
* <pre><code>
* // Prohibits players from writing messages by canceling an event.
* PlayerChatCallback.EVENT.register((player, message) -> {
* PlayerChatCallback.ChatMessage chatMessage = new PlayerChatCallback.ChatMessage(player, message);
* chatMessage.setCancelled(true);
* return chatMessage;
* });
* </code></pre>
*/
public interface PlayerChatCallback {
Event<PlayerChatCallback> EVENT = EventFactory.createArrayBacked(PlayerChatCallback.class,
listeners -> (player, message) -> {
for (PlayerChatCallback listener : listeners) {
return listener.result(player, message);
}
return new ChatMessage(player, message);
});
ChatMessage result(ServerPlayerEntity player, String message);
/**
* Object for determining the further behavior of the event.
*
* <p>With it, the message text can be changed or the message may not be sent at all.</p>
*
* <pre><code>
* // Example showing how to change the message text
* PlayerChatCallback.ChatMessage chatMessage = new PlayerChatCallback.ChatMessage (player, message);
* if ("Hello!".equalsIgnoreCase(chatMessage.getMessage()))
* chatMessage.setMessage("Bye!");
* </code></pre>
*/
class ChatMessage {
private final ServerPlayerEntity sender;
private String message;
private boolean isCancelled = false;
public ChatMessage(ServerPlayerEntity sender, String message) {
this.sender = sender;
this.message = message;
}
/**
* Get the player who sent the message.
*
* @return the sender of the message
*/
public ServerPlayerEntity getPlayer() {
return this.sender;
}
/**
* Get the message text.
* It should be understood that the method will return a new text if the message is changed via setMessage.
*
* @return the message text to send
*/
public String getMessage() {
return this.message;
}
/**
* Set a new message text.
* If the new text is <i>null</i>, the message text will be set as an empty string ("").
*
* @param message new message
*/
public void setMessage(String message) {
this.message = message;
if (this.message == null)
this.message = "";
}
/**
* Whether the message was canceled.
*
* @return true if the event is canceled and the message is not sent, otherwise false
*/
public boolean isCancelled() {
return this.isCancelled;
}
/**
* Whether to cancel sending the message.
* If you cancel, the message will simply not be sent to the players.
*
* @param isCancelled true or false
*/
public void setCancelled(boolean isCancelled) {
this.isCancelled = isCancelled;
}
}
}