move cycle plugin to new module; add features

This commit is contained in:
jomo
2014-06-29 15:18:25 +02:00
parent 5012696c56
commit ead78abd1c
4 changed files with 69 additions and 22 deletions

View File

@@ -121,6 +121,10 @@ If you want the server to load a file (*module*) on startup, add it to the `modu
> Adds `/listen`, highlights chat and plays a sound when your name was mentioned
* `cycler.py`
> Adds `/cycler`, swaps the hotbar with inventory when player changes slot from right->left or left->right
# Code styleguide & tips

64
cycle.py Normal file
View File

@@ -0,0 +1,64 @@
import simplejson as json
from helpers import *
cyclers_file = "plugins/redstoner-utils.py.dir/files/cycle.json"
no_cyclers = []
try:
no_cyclers = json.loads(open(cyclers_file).read())
except Exception, e:
error("Failed to load no_cyclers: %s" % e)
@hook.command("cycle")
def onCyclerCommand(sender, args):
plugHeader(sender, "Cycle")
if not isPlayer(sender):
msg(sender, "&conly players can do this")
return True
if not checkargs(sender, args, 1, 1):
return True
cmd = args[0].lower()
pid = sender.getUniqueId()
nop = pid in no_cyclers
if cmd == "on":
if nop:
no_cyclers.remove(pid)
msg(sender, "&aTurned &2on&a inventory cycling!")
else:
msg(sender, "&aAlready turned on.")
elif cmd == "off":
if not nop:
no_cyclers.append(pid)
msg(sender, "&aTurned &coff&a inventory cycling!")
else:
msg(sender, "&aAlready turned off.")
else:
msg(sender, "&cUsage: /cycle <on|off>")
return True
@hook.event("player.PlayerItemHeldEvent", "normal")
def onSlotChange(event):
player = event.getPlayer()
if player.getUniqueId() not in no_cyclers:
prev_slot = event.getPreviousSlot()
new_slot = event.getNewSlot()
if (prev_slot == 0 and new_slot == 8): # left -> right
doCycle(player, True)
elif (prev_slot == 8 and new_slot == 0): # right -> left
doCycle(player, False)
def doCycle(player, up):
inv = player.getInventory()
items = inv.getContents()
shift = -9 if up else 9
shift = shift % len(items)
for _ in range(4):
uniq_items = sorted(set(list(items)[:shift])) # crazy code
msg(player, items[:shift])
if uniq_items != [None]: # row not empty
break
items = items[shift:] + items[:shift] # shift "around"
inv.setContents(items)

View File

@@ -31,7 +31,7 @@ def onDisable():
log("Loading RedstonerUtils...")
# Import all modules
modules = ["misc", "adminchat", "lagchunks", "reports", "chatgroups", "webtoken", "saylol", "skullclick", "mentio"]
modules = ["misc", "adminchat", "lagchunks", "reports", "chatgroups", "webtoken", "saylol", "skullclick", "mentio", "cycle"]
mod = {}
for module in modules:
try:

21
misc.py
View File

@@ -92,27 +92,6 @@ def onPlayerInteractEntity(event):
sender.playSound(entity.getLocation(), "mob.cow.say", 1, 1)
#
# Cycling around the item bar will switch between rows of the inventory
#
@hook.event("player.PlayerItemHeldEvent", "normal")
def onHeldEvent(event):
player = event.getPlayer()
prev_slot = event.getPreviousSlot()
new_slot = event.getNewSlot()
if (prev_slot == 0 and new_slot == 8):
doCycle(player, -9)
elif (prev_slot == 8 and new_slot == 0):
doCycle(player, 9)
def doCycle(player, e):
inv = player.getInventory()
inv2 = inv.getContents()
e = e % len(inv2)
inv2 = inv2[e:] + inv2[:e]
inv.setContents(inv2)
#
# Various text commands
#