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
|
# Chat Aliasing plugin by Curs3d #
##################################
# Allows users to alias words,
# so that when they send a
# message in chat, it gets
# replaced by their specified
# word. Configuration of this
# plugin is in the "gnl"
# (general) tag of the JSON
# file named "aliases". The
# file is generated if not
# present. Set values to -1
# for "unlimited" setting.
from helpers import *
from traceback import format_exc as trace
data = None
max_entries = 10
max_alias_length = 35
# minecraft message limit is 100 so I decided to give a little tolerance
max_overall_length = 115
alias_perm = "utils.alias.allowed"
exceed_length = "utils.alias.exceedlimit"
exceed_entries = "utils.alias.exceedlimit"
exceed_overall_length = "utils.alias.exceedlimit"
def safe_open_json():
global data
if data is not None:
return data
data = open_json_file("aliases")
if data is None:
data = {}
save_json_file("aliases", data)
return data
@hook.command("alias",
usage="/<command> [to_alias] [alias...]",
desc="Aliases words in chat")
def on_alias_command(sender, cmd, label, args):
if not is_player(sender):
msg(sender, "Sorry, Console cannot alias words")
return True
if not sender.hasPermission(alias_perm):
plugin_header(recipient=sender, name="Chat Alias")
noperm(sender)
return True
if len(args) == 0:
plugin_header(recipient=sender, name="Chat Alias")
msg(sender, "&7This is a plugin that allows you to get words" +
"replaced by other ones automatically!")
msg(sender, "&7\nCommands:")
msg(sender, "&e/alias <word> &7removes <word> from your aliases. " +
"Use &e/alias * &7to remove all aliases.")
msg(sender, "&e/alias <word> <replacement> &7will change &e<word> " +
"&7to &e<replacement> &7in chat")
msg(sender, "&7\nYour Aliases:")
data = safe_open_json()
try:
for alias, value in data[str(sender.getUniqueId())].items():
msg(sender, "&7%s &7==> %s" % (alias, value))
except KeyError:
pass
return True
elif len(args) == 1:
data = safe_open_json()
if args[0] == "*":
try:
del data[str(sender.getUniqueId())]
except KeyError:
plugin_header(recipient=sender, name="Chat Alias")
msg(sender, "&7No alias data to remove!")
return True
save_json_file("aliases", data)
plugin_header(recipient=sender, name="Chat Alias")
msg(sender, "&cALL &7alias data successfuly removed!")
return True
try:
if data[str(sender.getUniqueId())].pop(args[0], None) is None:
plugin_header(recipient=sender, name="Chat Alias")
msg(sender, "&7Could not remove: alias not present!")
return True
except KeyError:
plugin_header(recipient=sender, name="Chat Alias")
msg(sender, "&7Could not remove: you do not have any aliases!")
return True
save_json_file("aliases", data)
plugin_header(recipient=sender, name="Chat Alias")
msg(sender, "&7Alias for %s &7successfuly removed" % args[0])
return True
elif len(args) >= 2:
data = safe_open_json()
alias = " ".join(args[1:])
try:
if (len(alias) > max_alias_length) and (max_alias_length >= 0) and (not sender.hasPermission(exceed_length)):
plugin_header(recipient=sender, name="Chat Alias")
msg(sender, "&7Please do not alias long words/sentences.")
return True
if (len(data[str(sender.getUniqueId())]) >= max_entries) and (max_entries >= 0) and (not sender.hasPermission(exceed_entries)):
plugin_header(recipient=sender, name="Chat Alias")
msg(sender, "&7You have reached your alias limit!")
return True
except KeyError:
data[str(sender.getUniqueId())] = {}
data[str(sender.getUniqueId())][args[0]] = alias
save_json_file("aliases", data)
plugin_header(recipient=sender, name="Chat Alias")
msg(sender, "&7Chat Alias %s &7==> %s &7successfully created!" % (args[0], alias))
return True
else:
return False
@hook.event("player.AsyncPlayerChatEvent", "high")
def on_player_chat(event):
playerid = str(event.getPlayer().getUniqueId())
data = safe_open_json()
if event.isCancelled():
return
try:
crashtest = data[playerid].items()
except KeyError:
return
for alias, value in data[playerid].items():
event.setMessage(event.getMessage().replace(alias, value))
if (event.getPlayer().hasPermission('essentials.chat.color')):
event.setMessage(colorify(event.getMessage()))
if (len(event.getMessage()) > max_overall_length) and (not sender.hasPermission(exceed_overall_length)):
event.setCancelled(True)
msg(sender, "&7The message generated was too long and was not sent " +
"but it would've looked like that:")
msg(sender, event.getMessage())
|