summaryrefslogtreecommitdiff
path: root/misc.py
blob: 6021f468aadf6d5927b9245966fac23c67c20b8d (plain)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#pylint: disable = F0401
from helpers import *
from time import time as now
from sys import exc_info
import thread
import org.bukkit.inventory.ItemStack as ItemStack
import org.bukkit.Bukkit as Bukkit
from basecommands import simplecommand


@hook.event("player.PlayerJoinEvent", "monitor")
def on_join(event):
    """
    Welcome new players
    """
    player = event.getPlayer()

    # send welcome broadcast
    if not player.hasPlayedBefore():
        broadcast("utils.greet_new", "\n&a&lPlease welcome &f" + player.getDisplayName() + " &a&lto Redstoner!\n")

        # clear out some eventual crap before
        msg(player, " \n \n \n \n \n \n \n \n \n \n \n \n ")
        msg(player, "  &4Welcome to the Redstoner Server!")
        msg(player, "  &6Before you ask us things, take a quick")
        msg(player, "  &6look at &a&nredstoner.com/info")
        msg(player, "  \n&6thank you and happy playing ;)")
        msg(player, " \n \n")

    # teleport to spawn when spawning inside portal
    loginloc = player.getLocation().getBlock().getType()
    headloc = player.getEyeLocation().getBlock().getType()
    if "PORTAL" in [str(headloc), str(loginloc)]:
        msg(player, "&4Looks like you spawned in a portal... Let me help you out")
        msg(player, "&6You can use /back if you &nreally&6 want to go back")
        player.teleport(player.getWorld().getSpawnLocation())


@simplecommand("sudo",
        usage        = "<player> [cmd..]",
        description  = "Makes <player> write [cmd..] in chat",
        amin         = 2,
        helpNoargs   = True)
def on_sudo_command(sender, command, label, args):
    target = args[0]
    cmd    =  " ".join(args[1:])
    msg(sender, "&2[SUDO] &rRunning '&e%s&r' as &3%s" % (cmd, target))
    is_cmd     = cmd[0] == "/"
    is_console = target.lower() in ["server", "console"]
    if is_console:
        server.dispatchCommand(server.getConsoleSender(), cmd[1:] if is_cmd else cmd)
        return None
    target_player = server.getPlayer(target)
    if target_player:
        target_player.chat(cmd)
        return None
    return "&cPlayer %s not found!" % target


@simplecommand("me", 
        usage        = "[message..]",
        description  = "Sends a message in third person",
        helpNoargs   = True)
def on_me_command(sender, command, label, args):
    text = colorify("&7- %s &7%s " % (sender.getDisplayName() if isinstance(sender, Player) else "&9CONSOLE", u"\u21E6"))
    broadcast("utils.me", text + " ".join(args), usecolor = sender.hasPermission("essentials.chat.color"))
    return None

#
#@hook.command("gm")
#def on_gm_command(sender, args):
#  """
#  /gm - custom gamemode command with extra perms for greater control
#  """
#  if not is_player(sender):
#    msg(sender, "&cDerp! Can't run that from console!")
#    return True
#  if not checkargs(sender, args, 1, 2):
#    return True
#  mode = args[0]
#  target = args[1]
#  if target and not sender.hasPermission("utils.gm.other"):
#    msg(sender, "&cYou cannot change the gamemode of another player!")
#  else:
#    target = sender
#  if mode < 0 or mode > 3:
#    msg(sender, "&cThat gamemode does not exist!")
#  elif sender.hasPermission("utils.gm." % mode):
#    runas(server.getConsoleSender(), "gamemode " % mode % " " % target)
#  else:
#    msg(sender, "&cYou cannot access that gamemode!")
#  return True


last_shear = 0.0

@hook.event("player.PlayerInteractEntityEvent")
def on_player_entity_interact(event):
    """
    Clicking redstone_sheep with shears will drop redstone + wool
    also makes a moo sound for the shearer
    """
    global last_shear
    if not event.isCancelled():
        shear_time = now()
        if last_shear + 0.4 < shear_time:
            last_shear = shear_time
            sender = event.getPlayer()
            entity = event.getRightClicked()
            if is_player(entity) and uid(entity) == "ae795aa8-6327-408e-92ab-25c8a59f3ba1" and str(sender.getItemInHand().getType()) == "SHEARS" and is_creative(sender):
                for _ in range(5):
                    entity.getWorld().dropItemNaturally(entity.getLocation(), ItemStack(bukkit.Material.getMaterial("REDSTONE")))
                    entity.getWorld().dropItemNaturally(entity.getLocation(), ItemStack(bukkit.Material.getMaterial("WOOL")))
                sender.playSound(entity.getLocation(), "mob.cow.say", 1, 1)

"""
@hook.command("pluginversions")
def on_pluginversions_command(sender, command, label, args):
    ""
    /pluginversions
    print all plugins + versions; useful when updating plugins
    ""
    try:
        plugin_header(sender, "Plugin versions")
        plugins = [pl.getDescription() for pl in list(ArrayList(java_array_to_list(server.getPluginManager().getPlugins())))]
        info(type(plugins[0]).__name__)
        plugins.sort(key = lambda pl: pl.getDescription().getName())
        msg(sender, "&3Listing all " + str(len(plugins)) + " plugins and their version:")
        for plugin in plugins:
            msg(sender, "&6" + pl.getDescription().getName() + "&r: &e" + pl.getDescription().getVersion())
        return True
    except:
        error(trace())
"""


@hook.command("echo")
def on_echo_command(sender, command, label, args):
    """
    /echo
    essentials echo sucks and prints mail alerts sometimes
    """
    msg(sender, " ".join(args).replace("\\n", "\n"))


def eval_thread(sender, code):
    """
    /pyeval
    run python ingame
    """
    try:
        result = eval(code)
        msg(sender, ">>> %s: %s" % (colorify("&3") + type(result).__name__, colorify("&a") + unicode(result) + "\n "), usecolor = False)
    except:
        e = exc_info()[1]
        try:
            eclass = e.__class__
        except AttributeError:
            eclass = type(e)
        msg(sender, ">>> %s: %s" % (eclass.__name__, e) + "\n ", False, "c")
    thread.exit()

"""
def eval_argument_thread(event):
    words = event.getMessage()[5:].split(" ")
    for i in range(len(words)):
        word = words[i]
        if is_pyeval_call(word):
            code = word[5:]
            try:
                result = unicode(eval(code))
            except:
                e = exc_info()[1]
                try:
                    eclass = e.__class__
                except AttributeError:
                    eclass = type(e)
                msg(event.getPlayer(), ">>> %s: %s" % (eclass.__name__, e) + "\n ", False, "c")
                result = code
            words[i] = result
    event.setMessage(" ".join(words))
    thread.exit()
"""

@simplecommand("pyeval",
    usage       = "[code..]",
    description = "Runs python [code..] and returns the result",
    helpNoargs  = True)
def on_pyeval_command(sender, command, label, args):
    msg(sender, " ".join(args), False, "e")
    thread.start_new_thread(eval_thread, (sender, " ".join(args)))
    return None


@hook.command("modules")
def on_modules_command(sender, command, label, args):
    """
    /modules
    list all modules, unloaded modules in red
    """
    plugin_header(sender, "Modules")
    msg(sender, ", ".join([(("&a" if mod in shared["modules"] else "&c") + mod) for mod in shared["load_modules"]]))


@hook.event("player.PlayerTeleportEvent")
def on_player_teleport(event):
    """
    Disable spectator teleportation
    """
    player = event.getPlayer()
    if not event.isCancelled() and str(event.getCause()) == "SPECTATE" and not player.hasPermission("utils.tp.spectate"):
        event.setCancelled(True)
        msg(event.getPlayer(), "&cSpectator teleportation is disabled")

"""
@hook.event("player.AsyncPlayerChatEvent", "lowest")
def on_chat(event):
    user = event.getPlayer()
    if user.hasPermission("utils.pyeval"):
        thread.start_new_thread(eval_argument_thread, (event,))

@hook.event("player.PlayerCommandPreprocessEvent", "lowest")
def on_cmd(event):
    user = event.getPlayer()
    if user.hasPermission("utils.pyeval"):
        thread.start_new_thread(eval_argument_thread, (event,))

def is_pyeval_call(string):
    return len(string) > 5 and string[:5] == "EVAL:"
"""