Solidify imbusy blocking pms, tpas, etc. #42

Merged
Dico200 merged 8 commits from imbusy-reply-patch into dev 2016-05-28 20:22:01 +00:00
Showing only changes of commit 5592e16200 - Show all commits

141
imbusy.py
View File

@ -13,7 +13,9 @@
from helpers import * from helpers import *
from basecommands import simplecommand from basecommands import simplecommand
import org.bukkit.command.Command as Command
from traceback import format_exc as trace from traceback import format_exc as trace
busy_players = [] busy_players = []
@ -85,19 +87,138 @@ def on_busy_command(sender, cmd, label, args):
return True return True
@hook.event("player.PlayerCommandPreprocessEvent", "monitor")
def on_cmd_preprocess_event(event):
message = event.getMessage().split(" ")
if message[0] == "/msg" or message[0] == "/w" or message[0] == "/m" or \
message[0] == "/tell" or message[0] == "/tpa" or message[0] == "/tpahere":
if message[1] in busy_players:
plugin_header(recipient = event.getPlayer(), name = "I'M BUSY!")
msg(event.getPlayer(), "We are sorry, but %s is currently busy. Please try again later." % message[1])
event.setCancelled(True)
@hook.event("player.PlayerQuitEvent", "lowest") @hook.event("player.PlayerQuitEvent", "lowest")
def on_player_leave(event): def on_player_leave(event):
try: try:
busy_players.remove(event.getPlayer().getName()) busy_players.remove(event.getPlayer().getName())
except: except:
pass pass
#---- Dicode for catching any bothering of busy people ----
reply_targets = {}
override_perm = "utils.imbusy.override"
def whisper(sender, target_name):
target = server.getPlayer(target_name)
if target is not None:
if target is not sender and not sender.hasPermission(override_perm) and target.getName() in busy_players:
msg(sender, "&c[&fBUSY&c] %s&r is busy!" % target.getDisplayName())
return False
reply_targets[sender.getName()] = target.getName()
# allow the target to reply regardless of sender being busy
if target.getName() in reply_targets:
del reply_targets[target.getName()]
return True
def reply(sender):
if sender.getName() in reply_targets:
target = server.getPlayer(reply_targets[sender.getName()])
if target is not None:
if target is not sender and not sender.hasPermission(override_perm) and target.getName() in busy_players:
msg(sender, "&c[&fBUSY&c] %s&r is busy!" % target.getDisplayName())
return False
# allow the target to reply regardless of sender being busy
if target.getName() in reply_targets:
del reply_targets[target.getName()]
return True
class CommandWrapper(Command):
def __init__(self, wrapped, checker):
Command.__init__(self, wrapped.getName())
self.setDescription(wrapped.getDescription())
self.setPermission(wrapped.getPermission())
self.setUsage(wrapped.getUsage())
self.setAliases(wrapped.getAliases())
self.wrapped = wrapped
self.checker = checker
def execute(self, sender, label, args):
try:
if self.checker(sender, args):
return self.wrapped.execute(sender, label, args)
except:
error(trace())
return True
def tabComplete(self, sender, alias, args):
return self.wrapped.tabComplete(sender, alias, args)
def msg_command_checker(sender, args):
return len(args) <= 1 or whisper(sender, args[0])
def reply_command_checker(sender, args):
return len(args) == 0 or reply(sender)
def tpa_command_checker(sender, args):
if len(args) == 0:
return True
target = server.getPlayer(args[0])
if target is not None and target is not sender and not sender.hasPermission(override_perm) and target.getName() in busy_players:
msg(sender, "&c[&fBUSY&c] %s&r is busy!" % target.getDisplayName())
return False
return True
def tpahere_command_checker(sender, args):
return tpa_command_checker(sender, args)
@hook.event("player.PlayerCommandPreprocessEvent", "monitor")
def on_player_command_preprocess(event):
message = event.getMessage().split(" ")
if len(message) > 1 and message[0].lower() in ("/tell", "/minecraft:tell") and not whisper(event.getPlayer(), message[1]):
event.setCancelled(True)
@hook.enable
def replace_ess_commands():
try:
mapField = server.getPluginManager().getClass().getDeclaredField("commandMap")
mapField.setAccessible(True)
commandMap = mapField.get(server.getPluginManager())
commandsField = commandMap.getClass().getDeclaredField("knownCommands")
commandsField.setAccessible(True)
map = commandsField.get(commandMap)
essMsgCmd = map.get("essentials:msg")
essReplyCmd = map.get("essentials:reply")
essTpaCmd = map.get("essentials:tpa")
essTpahereCmd = map.get("essentials:tpahere")
msgCmdWrapper = CommandWrapper(essMsgCmd, msg_command_checker)
replyCmdWrapper = CommandWrapper(essReplyCmd, reply_command_checker)
tpaCmdWrapper = CommandWrapper(essTpaCmd, tpa_command_checker)
tpahereCmdWrapper = CommandWrapper(essTpahereCmd, tpahere_command_checker)
iterator = map.entrySet().iterator()
while iterator.hasNext():
entry = iterator.next()
value = entry.getValue()
if value is essMsgCmd:
entry.setValue(msgCmdWrapper)
info("[imbusy] wrapped /" + entry.getKey())
elif value is essReplyCmd:
entry.setValue(replyCmdWrapper)
info("[imbusy] wrapped /" + entry.getKey())
elif value is essTpaCmd:
entry.setValue(tpaCmdWrapper)
info("[imbusy] wrapped /" + entry.getKey())
elif value is essTpahereCmd:
entry.setValue(tpahereCmdWrapper)
info("[imbusy] wrapped /" + entry.getKey())
except:
error("[Imbusy] Failed to wrap essentials commands")
error(trace())