Archived
Initial commit of chatalias.py v2
This commit is contained in:
+302
-119
@@ -1,138 +1,321 @@
|
|||||||
# Chat Aliasing plugin by Curs3d #
|
############################################
|
||||||
##################################
|
# Alias v2.0 by Pepich #
|
||||||
# Allows users to alias words,
|
# Changes to previous version from curs3d: #
|
||||||
# so that when they send a
|
# Dynamic alias limit from permissions #
|
||||||
# message in chat, it gets
|
# AC/CG/MSG support #
|
||||||
# replaced by their specified
|
# Color support #
|
||||||
# word. The JSON file for this
|
# Bugfixes #
|
||||||
# plugin is generated if not
|
# #
|
||||||
# present. Set values to -1
|
# TODO: #
|
||||||
# for "unlimited" setting.
|
# Add command support... #
|
||||||
|
############################################
|
||||||
|
|
||||||
|
import os
|
||||||
|
import org.bukkit as bukkit
|
||||||
|
from org.bukkit import *
|
||||||
from helpers import *
|
from helpers import *
|
||||||
import re
|
|
||||||
from traceback import format_exc as trace
|
from traceback import format_exc as trace
|
||||||
|
from secrets import *
|
||||||
|
|
||||||
data = None
|
# Version number and requirements
|
||||||
|
|
||||||
max_entries = 10
|
alias_version = "2.0.0"
|
||||||
max_alias_length = 35
|
helpers_versions = ["1.1.0", "2.0.0"]
|
||||||
# Minecraft message limit is 100 so I decided to give a little tolerance (and I added a bit more)
|
enabled = False
|
||||||
max_overall_length = 100 + max_alias_length
|
error = colorify("&cUnspecified error")
|
||||||
|
commands_per_page = 5
|
||||||
|
global_aliases = {"./":"/"}
|
||||||
|
data = {}
|
||||||
|
use_mysql = False
|
||||||
|
|
||||||
alias_perm = "utils.alias.allowed"
|
# Permissions:
|
||||||
exceed_length = "utils.alias.exceedlimit"
|
|
||||||
exceed_entries = "utils.alias.exceedlimit"
|
# Grants full access immediately
|
||||||
exceed_overall_length = "utils.alias.exceedlimit"
|
permission_ALL = "utils.alias.*"
|
||||||
|
# Access to the command to display the help screen
|
||||||
|
permission_BASE = "utils.alias"
|
||||||
|
# Make replacements only when the user has this permission
|
||||||
|
permission_USE = "utils.alias.use"
|
||||||
|
# Modify aliases
|
||||||
|
permission_MODIFY = "utils.alias.modify"
|
||||||
|
permission_MODIFY_OTHERS = "utils.alias.modify.others"
|
||||||
|
# List aliases
|
||||||
|
permission_LIST = "utils.alias.list"
|
||||||
|
permission_LIST_OTHERS = "utils.alias.list.others"
|
||||||
|
# Set alias amounts/length limits, e.g. utils.alias.amount.420
|
||||||
|
permission_AMOUNT = "utils.alias.amount."
|
||||||
|
permission_LENGTH = "utils.alias.length."
|
||||||
|
# See when the plugin was disabled due to version errors
|
||||||
|
permission_INFO = "utils.alias.info"
|
||||||
|
permission_FINFO = "utils.alias.finfo"
|
||||||
|
|
||||||
|
########
|
||||||
|
# CODE #
|
||||||
|
########
|
||||||
|
|
||||||
|
enabled = helpers_version in helpers_versions
|
||||||
|
if not enabled:
|
||||||
|
error = colorify("&6Incompatible versions detected (&chelpers.py&6)")
|
||||||
|
|
||||||
|
|
||||||
def safe_open_json():
|
def safe_open_json(uuid):
|
||||||
global data
|
if not os.path.exists("plugins/redstoner-utils.py.dir/files/aliases"):
|
||||||
if data is not None:
|
os.makedirs("plugins/redstoner-utils.py.dir/files/aliases")
|
||||||
return data
|
value = open_json_file("aliases/" + uuid)
|
||||||
data = open_json_file("aliases")
|
if value is None:
|
||||||
if data is None:
|
value = global_aliases
|
||||||
data = {}
|
save_json_file("aliases/" + uuid, value)
|
||||||
save_json_file("aliases", data)
|
return value
|
||||||
return data
|
|
||||||
|
|
||||||
def multiple_replace(aliases, text):
|
|
||||||
regex = re.compile("|".join(map(re.escape, aliases.keys())))
|
|
||||||
return regex.sub(lambda mo: aliases[mo.group(0)], text)
|
|
||||||
|
|
||||||
|
|
||||||
@hook.command("alias",
|
@hook.command("alias",
|
||||||
usage="/<command> [to_alias] [alias...]",
|
usage="/<command> <add, remove, list, help> [...]",
|
||||||
desc="Aliases words in chat")
|
desc="Allows aliasing of words")
|
||||||
def on_alias_command(sender, cmd, label, args):
|
def on_alias_command(sender, cmd, label, args):
|
||||||
|
try:
|
||||||
|
args = array_to_list(args)
|
||||||
|
if not enabled:
|
||||||
|
disabled_fallback(sender)
|
||||||
|
return True
|
||||||
|
if not hasPerm(sender, permission_BASE):
|
||||||
|
plugin_header(recipient=sender, name="Alias")
|
||||||
|
noperm(sender)
|
||||||
|
return True
|
||||||
|
return subcommands[args[0].lower()](sender, args[1:])
|
||||||
|
except:
|
||||||
|
print(trace())
|
||||||
|
return subcommands["help"](sender, "1")
|
||||||
|
|
||||||
if not is_player(sender):
|
|
||||||
msg(sender, "Sorry, non-players cannot alias words")
|
def help(sender, args):
|
||||||
return True
|
commands = [colorify("&e/alias help [page]")]
|
||||||
if not sender.hasPermission(alias_perm):
|
if hasPerm(sender, permission_LIST):
|
||||||
plugin_header(recipient=sender, name="Chat Alias")
|
commands += [colorify("&e/alias list &7- Lists all your aliases")]
|
||||||
noperm(sender)
|
if hasPerm(sender, permission_MODIFY):
|
||||||
return True
|
commands += [colorify("&e/alias add <word> <alias> &7- Add an alias")]
|
||||||
if len(args) == 0:
|
commands += [colorify("&e/alias remove <word> &7- Remove an alias")]
|
||||||
plugin_header(recipient=sender, name="Chat Alias")
|
if can_remote(sender):
|
||||||
msg(sender, "&7This is a plugin that allows you to get words" +
|
while len(commands) < commands_per_page:
|
||||||
"replaced by other ones automatically!")
|
commands += [""]
|
||||||
msg(sender, "&7\nCommands:")
|
commands += [colorify("&7Following commands will be executed on <player> yet all output will be redirected to you, except when you set silent to false, then <player> will see it too.")]
|
||||||
msg(sender, "&e/alias <word> &7removes <word> from your aliases. " +
|
if hasPerm(sender, permission_LIST_OTHERS):
|
||||||
"Use &e/alias * &7to remove all aliases.")
|
commands += [colorify("&e/alias player <name> list [silent]")]
|
||||||
msg(sender, "&e/alias <word> <replacement> &7will change &e<word> " +
|
if hasPerm(sender, permission_MODIFY_OTHERS):
|
||||||
"&7to &e<replacement> &7in chat")
|
commands += [colorify("&e/alias player <name> add <word> <alias> [silent]")]
|
||||||
msg(sender, "&7\nYour Aliases:")
|
commands += [colorify("&e/alias player <name> remove <word> [silent]")]
|
||||||
data = safe_open_json()
|
pages = (len(commands)-1)/commands_per_page + 1
|
||||||
try:
|
page = 1
|
||||||
for alias, value in data[str(sender.getUniqueId())].items():
|
if len(args) != 0:
|
||||||
msg(sender, "&7%s &7==> %s" % (alias, value))
|
page = int(args[0])
|
||||||
except KeyError:
|
if (page > pages):
|
||||||
pass
|
page = pages
|
||||||
return True
|
if page < 1:
|
||||||
elif len(args) == 1:
|
page = 1
|
||||||
data = safe_open_json()
|
msg(sender, colorify("&e---- &6Help &e-- &6Page &c" + str(page) + "&6/&c" + str(pages) + " &e----"))
|
||||||
if args[0] == "*":
|
page -= 1
|
||||||
try:
|
to_display = commands[5*page:5*page+5]
|
||||||
del data[str(sender.getUniqueId())]
|
for message in to_display:
|
||||||
except KeyError:
|
msg(sender, message)
|
||||||
plugin_header(recipient=sender, name="Chat Alias")
|
if page+1 < pages:
|
||||||
msg(sender, "&7No alias data to remove!")
|
msg(sender, colorify("&6To display the next page, type &c/help " + str(page+2)))
|
||||||
return True
|
return True
|
||||||
save_json_file("aliases", data)
|
|
||||||
plugin_header(recipient=sender, name="Chat Alias")
|
|
||||||
msg(sender, "&cALL &7alias data successfuly removed!")
|
@hook.event("player.PlayerJoinEvent", "high")
|
||||||
return True
|
def on_join(event):
|
||||||
try:
|
try:
|
||||||
if data[str(sender.getUniqueId())].pop(args[0], None) is None:
|
if enabled:
|
||||||
plugin_header(recipient=sender, name="Chat Alias")
|
t = threading.Thread(target=load_data, args=(uid(event.getPlayer()), ))
|
||||||
msg(sender, "&7Could not remove: alias not present!")
|
t.daemon = True
|
||||||
return True
|
t.start()
|
||||||
except KeyError:
|
else:
|
||||||
plugin_header(recipient=sender, name="Chat Alias")
|
if event.getPlayer().hasPermission(permission_FINFO):
|
||||||
msg(sender, "&7Could not remove: you do not have any aliases!")
|
disabled_fallback(event.getPlayer())
|
||||||
return True
|
except:
|
||||||
save_json_file("aliases", data)
|
print(trace())
|
||||||
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")
|
@hook.event("player.AsyncPlayerChatEvent", "high")
|
||||||
def on_player_chat(event):
|
def on_player_chat(event):
|
||||||
playerid = str(event.getPlayer().getUniqueId())
|
try:
|
||||||
data = safe_open_json()
|
if enabled:
|
||||||
if event.isCancelled():
|
if event.isCancelled():
|
||||||
return
|
return
|
||||||
if not playerid in data:
|
if not hasPerm(event.getPlayer(), permission_USE):
|
||||||
return
|
return
|
||||||
|
for alias, value in data[str(uid(event.getPlayer()))].items():
|
||||||
|
if not event.getPlayer().hasPermission(permission_ALL) and len(event.getMessage()) > int(get_permission_content(event.getPlayer(), permission_LENGTH)):
|
||||||
|
event.setCanceled(True)
|
||||||
|
plugin_header(event.getPlayer, "Alias")
|
||||||
|
msg(event.getPlayer(), "The message you wanted to generate would exceed your limit. Please make it shorter!")
|
||||||
|
return
|
||||||
|
if event.getPlayer().hasPermission("essentials.chat.color"):
|
||||||
|
event.setMessage(event.getMessage().replace(colorify(alias), colorify(value)))
|
||||||
|
else:
|
||||||
|
event.setMessage(event.getMessage().replace(alias, value))
|
||||||
|
except:
|
||||||
|
print(trace())
|
||||||
|
|
||||||
event.setMessage(multiple_replace(data[playerid], event.getMessage()))
|
def hasPerm(player, permission):
|
||||||
|
return (player.hasPermission(permission)) or (player.hasPermission(permission_ALL))
|
||||||
|
|
||||||
if (event.getPlayer().hasPermission("essentials.chat.color")):
|
|
||||||
event.setMessage(colorify(event.getMessage()))
|
def disabled_fallback(receiver):
|
||||||
if (max_overall_length >= 0) and (len(event.getMessage()) > max_overall_length) and (not event.getPlayer().hasPermission(exceed_overall_length)):
|
if not hasPerm(receiver, permission_INFO):
|
||||||
event.setCancelled(True)
|
msg(receiver, colorify("&cUnknown command. Use &e/help&c, &e/plugins &cor ask a mod."))
|
||||||
plugin_header(recipient=event.getPlayer(), name="Chat Alias")
|
else:
|
||||||
msg(event.getPlayer(), "&7The message generated was too long and was not sent. :/")
|
msg(receiver, colorify("&cPlugin alias v" + alias_version + " has experienced an &eEMERGENCY SHUTDOWN:"))
|
||||||
|
msg(receiver, error)
|
||||||
|
msg(receiver, colorify("&cPlease contact a dev/admin (especially pep :P) about this to take a look at it."))
|
||||||
|
|
||||||
|
|
||||||
|
def can_remote(player):
|
||||||
|
return hasPerm(player, permission_LIST_OTHERS) or hasPerm(player, permission_MODIFY_OTHERS)
|
||||||
|
|
||||||
|
|
||||||
|
def add(sender, args):
|
||||||
|
plugin_header(sender, "Alias")
|
||||||
|
if not sender.hasPermission(permission_ALL) and len(data[uid(sender)]) >= int(get_permission_content(sender, permission_AMOUNT)):
|
||||||
|
msg(sender, "&cCould not create alias: Max_limit reached!")
|
||||||
|
return True
|
||||||
|
args = [args[0]] + [" ".join(args[1:])]
|
||||||
|
data[str(uid(sender))][str(args[0])] = args[1]
|
||||||
|
save_data(uid(sender))
|
||||||
|
msg(sender, colorify("&7Alias: ") + args[0] + colorify("&7 -> " + args[1] + colorify("&7 was succesfully created!")), usecolor=sender.hasPermission("essentials.chat.color"))
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def radd(sender, args):
|
||||||
|
args = [args[0:1]] + [" ".join([args[2:len(args)-2]])] + [args[len(args)-1]]
|
||||||
|
plugin_header(sender, "Alias")
|
||||||
|
if args[3].lower() == "false":
|
||||||
|
plugin_header(target, "Alias")
|
||||||
|
msg(target, "&cPlayer " + sender_name + " &cis creating an alias for you!")
|
||||||
|
if not sender.hasPermission(permission_ALL) and len(data[uid(sender)]) >= int(get_permission_content(target, permission_AMOUNT)):
|
||||||
|
msg(sender, "&cCould not create alias: Max_limit reached!")
|
||||||
|
if args[3].lower() == "false":
|
||||||
|
msg(target, "&cCould not create alias: Max_limit reached!")
|
||||||
|
return True
|
||||||
|
|
||||||
|
target = get_player(args[0])
|
||||||
|
if is_player(sender):
|
||||||
|
sender_name = colorify(sender.getDisplayName)
|
||||||
|
else:
|
||||||
|
sender_name = colorify("&6Console")
|
||||||
|
if len(args) == 3:
|
||||||
|
args += ["true"]
|
||||||
|
data[str(uid(target))][str(args[1])] = str(args[2])
|
||||||
|
save_data(uid(target))
|
||||||
|
msg(sender, colorify("&7Alias: ") + args[1] + colorify("&7 -> " + args[2] + colorify("&7 was succesfully created!")), usecolor=target.hasPermission("essentials.chat.color"))
|
||||||
|
if args[3].lower() == "false":
|
||||||
|
msg(target, colorify("&7Alias: ") + args[1] + colorify("&7 -> " + args[2] + colorify("&7 was succesfully created!")), usecolor=target.hasPermission("essentials.chat.color"))
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def remove(sender, args):
|
||||||
|
plugin_header(sender, "Alias")
|
||||||
|
try:
|
||||||
|
msg(sender, colorify("&7Successfully removed alias ") + args[0] + colorify(" &7-> ") + data[uid(sender)].pop(args[0]) + colorify("&7!"), usecolor=sender.hasPermission("essentials.chat.color"))
|
||||||
|
save_data(uid(sender))
|
||||||
|
except:
|
||||||
|
msg(sender, colorify("&cCould not remove alias ") + args[0] + colorify(", it does not exist."), usecolor=sender.hasPermission("essentials.chat.color"))
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def rremove(sender, args):
|
||||||
|
plugin_header(sender, "Alias")
|
||||||
|
target = get_player(args[0])
|
||||||
|
if is_player(sender):
|
||||||
|
sender_name = colorify(sender.getDisplayName)
|
||||||
|
else:
|
||||||
|
sender_name = colorify("&6Console")
|
||||||
|
if args[2].lower() == "false":
|
||||||
|
print("WTF")
|
||||||
|
plugin_header(target, "Alias")
|
||||||
|
msg(target, "&cPlayer " + sender_name + " &cis removing an alias for you!")
|
||||||
|
try:
|
||||||
|
alias = data[uid(target)].pop(args[1])
|
||||||
|
msg(sender, colorify("&7Successfully removed alias ") + args[1] + colorify(" &7-> ") + alias + colorify("&7!"), usecolor=sender.hasPermission("essentials.chat.color"))
|
||||||
|
if args[2].lower() == "false":
|
||||||
|
msg(target, colorify("&7Successfully removed alias ") + args[1] + colorify(" &7-> ") + alias + colorify("&7!"), usecolor=sender.hasPermission("essentials.chat.color"))
|
||||||
|
save_data(uid(target))
|
||||||
|
except:
|
||||||
|
msg(sender, colorify("&cCould not remove alias ") + args[1] + colorify(", it does not exist."), usecolor=sender.hasPermission("essentials.chat.color"))
|
||||||
|
if args[2].lower() == "false":
|
||||||
|
msg(target, colorify("&cCould not remove alias ") + args[1] + colorify(", it does not exist."), usecolor=sender.hasPermission("essentials.chat.color"))
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def list_alias(sender, args):
|
||||||
|
plugin_header(sender, "Alias")
|
||||||
|
msg(sender, "&7You have a total of " + str(len(data[uid(sender)])) + " aliases:")
|
||||||
|
for word, alias in data[str(uid(sender))].items():
|
||||||
|
msg(sender, colorify("&7") + word + colorify("&7 -> ") + alias, usecolor=sender.hasPermission("essentials.chat.color"))
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def rlist_alias(sender, args):
|
||||||
|
plugin_header(sender, "Alias")
|
||||||
|
target = get_player(args[0])
|
||||||
|
if is_player(sender):
|
||||||
|
sender_name = colorify(sender.getDisplayName)
|
||||||
|
else:
|
||||||
|
sender_name = colorify("&6Console")
|
||||||
|
if len(args) == 1:
|
||||||
|
args += ["true"]
|
||||||
|
msg(sender, "Player " + args[0] + " has following aliases (" + str(len(data[uid(target)])) + " in total):")
|
||||||
|
if args[1].lower() == "false":
|
||||||
|
plugin_header(target, "Alias")
|
||||||
|
msg(target, "&cPlayer " + sender_name + " &cis listing your aliases (" + str(len(data[uid(target)])) + " in total):")
|
||||||
|
for word, alias in data[str(uid(target))].items():
|
||||||
|
msg(sender, colorify("&7") + word + colorify("&7 -> ") + alias, usecolor=target.hasPermission("essentials.chat.color"))
|
||||||
|
if args[1].lower() == "false":
|
||||||
|
msg(target, colorify("&7") + word + colorify("&7 -> ") + alias, usecolor=target.hasPermission("essentials.chat.color"))
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def remote(sender, args):
|
||||||
|
try:
|
||||||
|
return remotes[args[1].lower()](sender, [args[0]] + [args[2:]])
|
||||||
|
except:
|
||||||
|
print(trace())
|
||||||
|
return subcommands["help"](sender, ["2"])
|
||||||
|
|
||||||
|
|
||||||
|
def load_data(uuid):
|
||||||
|
if use_mysql:
|
||||||
|
conn = zxJDBC.connect(mysql_database, mysql_user, mysql_pass, "com.mysql.jdbc.Driver")
|
||||||
|
curs = conn.cursor()
|
||||||
|
curs.execute("SELECT alias FROM alias WHERE uuid = ?", (uuid, ))
|
||||||
|
results = curs.fetchall()
|
||||||
|
if len(results) == 0:
|
||||||
|
results = global_aliases
|
||||||
|
curs.execute("INSERT INTO alias VALUES (?,?)", (uuid, results, ))
|
||||||
|
data[uuid] = results
|
||||||
|
else:
|
||||||
|
data[uuid] = safe_open_json(uuid)
|
||||||
|
|
||||||
|
|
||||||
|
def save_data(uuid):
|
||||||
|
if use_mysql:
|
||||||
|
conn = zxJDBC.connect(mysql_database, mysql_user, mysql_pass, "com.mysql.jdbc.Driver")
|
||||||
|
curs = conn.cursor()
|
||||||
|
curs.execute("UPDATE alias SET alias = ? WHERE uuid = ?", (data[uuid], uuid, ))
|
||||||
|
else:
|
||||||
|
save_json_file("aliases/" + uuid, data[uuid])
|
||||||
|
|
||||||
|
# Subcommands:
|
||||||
|
|
||||||
|
subcommands = {
|
||||||
|
"help": help,
|
||||||
|
"add": add,
|
||||||
|
"remove": remove,
|
||||||
|
"player": remote,
|
||||||
|
"list": list_alias
|
||||||
|
}
|
||||||
|
|
||||||
|
remotes = {
|
||||||
|
"add": radd,
|
||||||
|
"remove": rremove,
|
||||||
|
"list": rlist_alias,
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user