Revert "wrapper_command WIP"

This reverts commit 522a7a99a1.
This commit is contained in:
Dico200
2015-11-25 15:24:34 +01:00
parent 522a7a99a1
commit 0172cf2feb
4 changed files with 56 additions and 290 deletions

View File

@@ -5,14 +5,6 @@ import java.util.UUID as UUID
import org.bukkit.Material as Material
import org.bukkit.block.BlockFace as BlockFace
"""
# About permissions:
# To use the command, the user needs to have utils.serversigns.
# To use ANY subcommand, the user needs to have utils.serversigns.<subcommand> IN ADDITION to the previously mentioned node.
# To be able to add commands as messages to a sign, a user will need the node utils.serversigns.command.
# To be able to claim a sign for another player or to edit signs that the user doesn't own, they will need utils.serversigns.admin.
"""
blocked_cmds = ("pex", "kick", "ban", "tempban", "pyeval", "sudo", "stop", "reload", "op", "deop", "whitelist")
def load_signs():
@@ -32,18 +24,6 @@ signs = load_signs() # {("world", x, y, z): ["owner_id", "msg1", "msg2"]}
lines = {} # Accumulated messages so players can have longer messages: {"Dico200": "Message...........", ""}
@hook.enable
def check_all_signs():
# Check if all saved signs actually represent a sign block. There are ways to break the signs without the plugin knowing.
for loc in dict(signs): # Can't change dict size during iteration, using a copy
world = server.getWorld(loc[0])
if world and world.getBlockAt(loc[1], loc[2], loc[3]).getType() in (Material.SIGN_POST, Material.WALL_SIGN):
continue
del signs[loc]
info("[Server Signs] Couldn't find a %s, removed the data for the sign that was once there." % identifySign(loc))
save_signs()
def fromLoc(bLoc):
"""
# Returns a tuple containing the (bukkit)location's world's name and its x, y and z coordinates
@@ -242,6 +222,9 @@ def svs_command(sender, command, label, args):
return signsMsg("Removed all messages and the owner from the %s, it can now be claimed" % signName, 'a')
#-------------------------------------------------------------------------------------------------------
@hook.event("player.PlayerInteractEvent")
def on_click(event):
if str(event.getAction()) != "RIGHT_CLICK_BLOCK":
@@ -271,22 +254,25 @@ faces = {
@hook.event("block.BlockBreakEvent", "monitor")
def on_break(event):
global checking_block
if checking_block or event.isCancelled():
return
try:
global checking_block
if checking_block or event.isCancelled():
return
block = event.getBlock()
if block.getType() in (Material.SIGN_POST, Material.WALL_SIGN):
check_sign(event, block, attached = False)
block = event.getBlock()
if block.getType() in (Material.SIGN_POST, Material.WALL_SIGN):
check_sign(event, block, attached = False)
for block_face, data_values in faces.iteritems():
block2 = block.getRelative(block_face)
if block2.getType() == Material.WALL_SIGN and block2.getData() in data_values:
check_sign(event, block2)
for block_face, data_values in faces.iteritems():
block2 = block.getRelative(block_face)
if block2.getType() == Material.WALL_SIGN and block2.getData() in data_values:
check_sign(event, block2)
block3 = block.getRelative(BlockFace.UP)
if block3.getType() == Material.SIGN_POST:
check_sign(event, block3)
block3 = block.getRelative(BlockFace.UP)
if block3.getType() == Material.SIGN_POST:
check_sign(event, block3)
except:
error(trace())
def check_sign(event, block, attached = True):
@@ -301,7 +287,6 @@ def check_sign(event, block, attached = True):
save_signs()
msg(player, signsMsg("Reset the %s which you just broke" % identifySign(loc)))
def can_build2(player, block):
global checking_block
event = BlockBreakEvent(block, player)

View File

@@ -13,7 +13,7 @@ def is_vanished(player):
return uid(player) in vanished
#this can be used to silently set the vanished state of a player I guess.
#this can be used to silently set the vanished state of a player
def set_state(player, state):
if state == is_vanished(player):
return
@@ -37,10 +37,6 @@ def disable_vanish(target):
player.showPlayer(target)
def get_online_vanished_players():
return (player.getPlayer() for player in (retrieve_player(uuid) for uuid in vanished) if player.isOnline())
@simplecommand("vanish",
aliases = ["v"],
usage = "[on/off]",
@@ -52,38 +48,45 @@ def get_online_vanished_players():
helpSubcmd = True
)
def vanish_command(sender, command, label, args):
current_state = is_vanished(sender)
new_state = not current_state
try:
current_state = is_vanished(sender)
new_state = not current_state
if len(args) == 1:
arg = args[0].lower()
if arg == "on":
new_state = True
elif arg == "off":
new_state = False
if len(args) == 1:
arg = args[0].lower()
if arg == "on":
new_state = True
elif arg == "off":
new_state = False
Validate.isTrue(current_state != new_state, "&cYou were %s vanished!" % ("already" if current_state else "not yet"))
set_state(sender, new_state)
return "&a%s vanish mode!" % ("Enabled" if new_state else "Disabled")
if current_state == new_state:
return "&cYou were %s vanished!" % ("already" if current_state else "not yet")
set_state(sender, new_state)
return "&a%s vanish mode!" % ("Enabled" if new_state else "Disabled")
except:
error(trace())
@hook.event("player.PlayerJoinEvent")
def on_player_join(event):
player = event.getPlayer()
if not is_authorized(player):
for vanished in get_online_vanished_players():
player.hidePlayer(vanished)
for uuid in vanished:
player.hidePlayer(retrieve_player(uuid))
elif is_vanished(player):
msg(player, "&cKeep in mind that you are still vanished! Use /vanish to disable.")
@hook.event("player.PlayerQuitEvent")
def on_player_quit(event):
player = event.getPlayer()
if not is_authorized(player):
for vanished in get_online_vanished_players():
player.showPlayer(vanished)
elif is_vanished(player):
disable_vanish(player)
if not is_authorized(player):
for uuid in vanished:
player.showPlayer(retrieve_player(uuid))
@simplecommand("vanishother",
@@ -107,7 +110,9 @@ def vanishother_command(sender, command, label, args):
elif arg == "off":
new_state = False
Validate.isTrue(current_state != new_state, "&cThat player was %s vanished!" % ("already" if current_state else "not yet"))
if current_state == new_state:
return "&cThat player was already vanished!" if current_state else "&cThat player was not yet vanished!"
set_state(target, new_state)
enabled_str = "enabled" if new_state else "disabled"

View File

@@ -1,232 +1,11 @@
from wrapper_player import *
from helpers import *
root_commands = Command_dict() # {"command": command_object}
def check_arguments(command, arguments):
prev_required = True
type_message_seen = False
prev_arg = arguments[0] if len(arguments) > 0 else None
for arg_info in arguments[1:]:
if not prev_arg.required and arg_info.required:
raise Argument_exception("Command: %s; There may not be required arguments after non-required arguments" % command)
if prev_arg.type == Argument.MESSAGE:
raise Argument_exception("Command: %s; An argument of type MESSAGE may not be followed by other arguments" % command)
prev_arg = arg_info
#--------------------------------------------------------------------------------------
class Command_dict(dict):
#{"cmd1" : cmd_object}
def get_command_object(self, alias):
for cmd_name, cmd_obj in self.iteritems():
if alias == cmd_name or alias in cmd_obj.aliases:
return cmd_obj
raise KeyError("Subcommand '%s' was not found" % alias)
#--------------------------------------------------------------------------------------
class Command(object):
def __init__(self,
command,
aliases = (),
arguments = (
Argument("target", Argument.string, "the player to teleport to"),
Argument("second target", Argument.string, "the player to teleport", False),
),
parent = None):
self.command = command.lower()
self.arguments = arguments
check_arguments(self.command, self.arguments)
prev_required = True
for arg_info in self.arguments:
if not prev_required and arg_info.required:
raise Argument_exception("Command: %s; There may not be required arguments after non-required arguments" % self.command)
self.aliases = tuple(alias.lower() for alias in aliases)
self.parent = parent
self.sub_commands = Command_dict()
if self.parent == None:
root_commands[self.command] = self
else:
def command(command = "help"):
def decorator(wrapped):
@hook.command(command)
def wrapper(sender, command, label, args):
try:
parent_route = self.parent.split(" ")
parent_sub_commands = root_commands
parent_obj = None
for cmd_name in parent_route:
parent_obj = parent_sub_commands.get_command_object(cmd_name)
parent_sub_commands = parent_obj.sub_commands
parent_obj.sub_commands[self.command] = self
except command_exception, e:
error("Error occurred while setting up command hierarchy. " + e.message + "\n" + trace())
def __call__(self, handler):
self.handler = handler
if parent == None:
@hook.command(self.command, self.aliases)
def run(sender, command, label, args):
try:
message = self.execute(sender, command, label, args)
except Command_exception as e:
message = e.message
except Exception:
error(trace())
return True
if message:
sender.sendMessage(message)
return True
return handler
def execute(self, sender, command, label, args):
try:
return self.sub_commands.get_command_object(args[0].lower()).execute(sender, command, label, args[1:])
except (KeyError, IndexError):
self.execute_checks(sender, command, label, args)
def execute_checks(self, sender, command, label, args):
#TODO
scape = Command_scape(args, self.arguments)
if is_player(sender):
sender = py_players[sender]
return self.handler(sender, self, scape)
def syntax(self):
return " ".join(tuple(arg_info.syntax() for arg_info in self.arguments))
#--------------------------------------------------------------------------------------
class Command_scape(list):
def __init__(self, args, arg_layout):
super(list, self).__init__()
self.raw = args
self.arg_layout = arg_layout
has_message = False
for i in range(len(arg_layout)):
arg_info = arg_layout[i]
given = (len(args) >= i + 1)
if arg_info.required and not given:
raise Argument_exception("You must specify the " + arg_info.name)
if not given:
self.append(None)
continue
given_arg = args[i]
arg_type = arg_info.type
if arg_type == Argument.STRING:
self.append(given_arg)
elif arg_type == Argument.INTEGER:
try:
value = int(given_arg)
except ValueError:
raise Argument_exception("The %s has to be a round number" % arg_info.name)
self.append(value)
elif arg_type == Argument.FLOAT:
try:
value = float(given_arg)
except ValueError:
raise Argument_exception("The %s has to be a number" % arg_info.name)
self.append(value)
elif arg_type == Argument.PLAYER:
target = server.getPlayer(given_arg)
if target == None:
raise Argument_exception("The %s has to be an online player" % arg_info.name)
self.append(py_players[target])
elif arg_type == Argument.OFFLINE_PLAYER:
try:
# Code to get the PY PLAYER by name. Possibly, uid(server.getOfflinePlayer(given_arg)) can be used?
pass
except KeyError:
raise Argument_exception("The %s has to be an existing player" % arg_info.name)
self.append(None)
elif arg_type == Argument.MESSAGE:
self.append(" ".join(args[i:]))
has_message = True
else:
error("Argument type not found: %d" % arg_type)
raise Argument_exception("A weird thing has happened, please contact an administrator")
if not has_message:
self.remainder = args[len(arg_layout):]
else:
self.remainder = None
def has_flag(self, flag, check_all = False):
return (("-" + flag) in self.raw) if check_all else (("-" + flag) in self.remainder)
def get_raw(self):
return self.raw
def get_arg_layout(self):
return self.arg_layout
#--------------------------------------------------------------------------------------
class Command_exception(Exception):
def __init__(self, message):
self.message = message
class Argument_exception(Exception):
def __init__(self, message):
self.message = message
#--------------------------------------------------------------------------------------
class Argument():
STRING = 0
INTEGER = 1
FLOAT = 2
PLAYER = 3
OFFLINE_PLAYER = 4
MESSAGE = 5
def __init__(self, name, type, definition, required = True):
self.name = name
self.type = type
self.definition = definition
self.required = required
def syntax(self):
syntax = self.name
if self.type == Argument.MESSAGE:
syntax += "..."
return (("<%s>" if self.required else "[%s]") % syntax)
#--------------------------------------------------------------------------------------
class Validate():
@staticmethod
def is_true(expression, fail_message):
if not expression:
raise Command_exception(fail_message)
@staticmethod
def not_none(obj, fail_message):
if obj == None:
raise Command_exception(fail_message)
return wrapped(sender = py_players[sender], command = command, label = label, args = args)
except:
print(print_traceback())
return decorator

View File

@@ -6,9 +6,6 @@ from players_secret import *
from datetime import datetime
from com.ziclix.python.sql import zxJDBC
def get_py_player(player):
class py_player:
def __init__(self,player):
self.player = player