-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomName.js
More file actions
82 lines (76 loc) · 2.24 KB
/
CustomName.js
File metadata and controls
82 lines (76 loc) · 2.24 KB
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
const PLUGIN_DIR = "plugins/CustomName/";
const CONFIG_FILE = PLUGIN_DIR + "config.json";
const Config = {
_raw: {},
load() {
if (!File.exists(CONFIG_FILE)) {
this.save();
}
this._raw = JSON.parse(File.readFrom(CONFIG_FILE));
let keys = Object.keys(this._raw);
keys.forEach(key => {
if (this._raw[key] != null) {
this[key] = this._raw[key];
}
});
},
save() {
let keys = Object.keys(this);
for (let i = 0; i < keys.length; i++) {
let key = keys[i];
if (key.startsWith('_')) continue;
if (typeof this[key] == 'function') continue;
this._raw[key] = this[key];
}
File.writeTo(CONFIG_FILE, JSON.stringify(this._raw, null, 4));
},
maxNameLength: 20,
names: {}
};
const Command = {
setup() {
let cmd = mc.newCommand("setname", "Set your custom name!", PermType.Any);
cmd.setAlias("sn");
cmd.optional("name", ParamType.RawText);
cmd.overload(["name"]);
cmd.setCallback(Command.execute);
cmd.setup();
},
execute(_cmd, ori, out, args) {
if (ori.player == null) {
out.error("This command can only be executed by player!");
return;
}
let customName = args["name"];
let realName = ori.player?.realName;
if (realName == null) {
return;
}
if (customName.length > Config.maxNameLength) {
out.error("Your name is too long!");
return;
}
if (customName == null) {
delete Config.names[ori.player.realName];
Config.save();
ori.player.rename(realName);
out.success("Your name has been reset!");
return;
}
Config.names[realName] = customName;
Config.save();
ori.player.rename(customName);
out.success("Your name has been set to " + customName);
}
};
if (File.exists(PLUGIN_DIR)) {
File.mkdir(PLUGIN_DIR);
}
Config.load();
Command.setup();
mc.listen("onJoin", function (player) {
let name = player.realName;
if (Config.names[name] != null) {
player.rename(Config.names[name]);
}
});