-
Notifications
You must be signed in to change notification settings - Fork 0
/
MarginalChat.lxl.js
189 lines (166 loc) · 6.64 KB
/
MarginalChat.lxl.js
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
class MarginalChat {
static PluginConfig = new JsonConfigFile("./plugins/MarginalChat/config.json", JSON.stringify({
Chat: {
Global: "[G] <{pl}> {msg}",
Local: "[L] <{pl}> {msg}"
},
CensoredWords: {
Enabled: false,
Words: ["niga", "niggers", "nigga"]
},
Distance: 15,
GlobalSymbol: "!",
}))
static PluginMeta = {
Name: "MarginalChat",
Introduction: "Local and Global chat for your server.",
Version: [1, 0, 0],
Other: {
License: "Apache License",
},
};
static Utils = {
GetNearPlayers(sourcePlayer) {
let nearPlayers = []
mc.getOnlinePlayers().forEach(onlinePlayer => {
if (
(sourcePlayer.distanceToPos(onlinePlayer.pos) != undefined && Math.round(sourcePlayer.distanceToPos(onlinePlayer.blockPos)) <= MarginalChat.PluginConfig.get("Distance"))
&&
sourcePlayer.pos.dimid == onlinePlayer.pos.dimid
) {
nearPlayers.push(onlinePlayer)
}
});
return nearPlayers
},
GenerateMessage(sourcePlayer, msg, isGlobal = true) {
let message;
let resultMsg;
if (isGlobal) {
message = MarginalChat.PluginConfig.get("Chat").Global
} else {
message = MarginalChat.PluginConfig.get("Chat").Local
}
if (isGlobal) {
resultMsg = msg.substring(MarginalChat.PluginConfig.get("GlobalSymbol").length)
} else {
resultMsg = msg
}
let replaceDict = [
{
placeholder: "{pl}",
result: sourcePlayer.realName
},
{
placeholder: "{plping}",
result: sourcePlayer.getDevice().avgPing
},
{
placeholder: "{plos}",
result: sourcePlayer.getDevice().os
},
{
placeholder: "{msg}",
result: resultMsg
},
{
placeholder: "{Y}",
result: system.getTimeObj().Y
},
{
placeholder: "{M}",
result: system.getTimeObj().M
},
{
placeholder: "{D}",
result: system.getTimeObj().D
},
{
placeholder: "{h}",
result: system.getTimeObj().h
},
{
placeholder: "{m}",
result: system.getTimeObj().m
},
{
placeholder: "{s}",
result: system.getTimeObj().s
},
]
replaceDict.forEach(replaceString => {
message = message.replace(replaceString.placeholder, replaceString.result)
})
return message
},
IfMessageContainIncorrectWords(message) {
let incorrectWordsArray = MarginalChat.PluginConfig.get("CensoredWords").Words
let msg = message;
let isIncorrectWord = false;
incorrectWordsArray.forEach(word => {
if (msg.includes(word)) {
isIncorrectWord = true;
}
})
return isIncorrectWord
}
}
static PreparePermissions() {
if (!Permission.permissionExists("marginalchat:chat_access")) {
Permission.registerPermission("marginalchat:chat_access", "MarginalChat: Access to chat")
}
if (!Permission.permissionExists("marginalchat:global_chat_access")) {
Permission.registerPermission("marginalchat:global_chat_access", "MarginalChat: Ability to write in Global chat")
}
if (!Permission.permissionExists("marginalchat:write_incorrect_words") && MarginalChat.PluginConfig.get("CensoredWords").Enabled) {
Permission.registerPermission("marginalchat:write_incorrect_words", "MarginalChat: Ability to write incorrect words in chat")
}
}
static Init() {
if (ll.requireVersion(2,7,0)) {
ll.registerPlugin(
this.PluginMeta.Name,
this.PluginMeta.Introduction,
this.PluginMeta.Version,
this.PluginMeta.Other
);
this.PreparePermissions();
this.Main();
logger.info("Ready to work.")
} else {
logger.error("The plugin will not work on this version of LiteLoaderBDS.")
}
}
static Main() {
mc.listen("onChat", (pl, message) => {
if (Permission.checkPermission(pl.xuid, "marginalchat:chat_access")) {
if (
!this.Utils.IfMessageContainIncorrectWords(message)
) {
if (message.startsWith(MarginalChat.PluginConfig.get("GlobalSymbol"))) {
if (Permission.checkPermission(pl.xuid, "marginalchat:global_chat_access")) {
mc.getOnlinePlayers().forEach(player => {
player.tell(this.Utils.GenerateMessage(pl, message))
})
} else {
pl.tell(`${Format.Red}You do not have permission to write to the global chat.`)
}
} else {
let nearPlayers = this.Utils.GetNearPlayers(pl)
nearPlayers.forEach(player => {
player.tell(this.Utils.GenerateMessage(pl, message, false))
})
}
} else {
if (Permission.permissionExists("marginalchat:write_incorrect_words") && !Permission.checkPermission(pl.xuid, "marginalchat:write_incorrect_words")) {
pl.tell(`${Format.Red}You do not have permission to write incorrect words to chat.`)
}
}
} else {
pl.tell(`${Format.Red}You do not have permission to write to chat.`)
}
return false;
})
}
}
MarginalChat.Init();