damnspam
This commit is contained in:
150
damnspam.py
150
damnspam.py
@@ -1,10 +1,11 @@
|
|||||||
#pylint: disable=F0401
|
#pylint: disable=F0401
|
||||||
from helpers import *
|
from helpers import *
|
||||||
|
from time import time as now
|
||||||
import json
|
import json
|
||||||
|
|
||||||
spam_filename = "plugins/redstoner-utils.py.dir/files/damnspam.json"
|
spam_filename = "plugins/redstoner-utils.py.dir/files/damnspam.json"
|
||||||
inputs = []
|
inputs = {} # format "x;y;z;World"
|
||||||
accepted_inputs = ["WOOD_BUTTON", "STONE_BUTTON"]
|
accepted_inputs = ["WOOD_BUTTON", "STONE_BUTTON", "LEVER"]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
inputs = json.loads(open(spam_filename).read())
|
inputs = json.loads(open(spam_filename).read())
|
||||||
@@ -12,63 +13,100 @@ except Exception, e:
|
|||||||
error("Failed to load buttons and levers: %s" % e)
|
error("Failed to load buttons and levers: %s" % e)
|
||||||
|
|
||||||
|
|
||||||
@hook.command("damnspam")
|
|
||||||
def onTimeoutCommand(sender, args):
|
|
||||||
global inputs
|
|
||||||
try:
|
|
||||||
plugHeader(sender, "DamnSpam")
|
|
||||||
if len(args) == 1:
|
|
||||||
timeout = args[0]
|
|
||||||
if not timeout.isdigit():
|
|
||||||
msg(sender, "&cThe timeout has to be a digit.")
|
|
||||||
return True
|
|
||||||
tB = sender.getTargetBlock(None, 10)
|
|
||||||
if str(tB.getType()) not in accepted_inputs:
|
|
||||||
msg(sender, "&cPlease look at a button/lever while executing this command!")
|
|
||||||
return True
|
|
||||||
data = {
|
|
||||||
"type": str(tB.getType()),
|
|
||||||
"creator": str(sender.getUniqueId()),
|
|
||||||
"timeout": int(args[0]),
|
|
||||||
"x": int(tB.getX()),
|
|
||||||
"y": int(tB.getY()),
|
|
||||||
"z": int(tB.getZ()),
|
|
||||||
"next": 'NULL',
|
|
||||||
"last": 'NULL'
|
|
||||||
}
|
|
||||||
inputs.append(data)
|
|
||||||
saveInputs()
|
|
||||||
msg(sender, "&eSuccessfully set a timeout for this button")
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
msg(sender, "&c/timeout <seconds>")
|
|
||||||
except Exception, e:
|
|
||||||
error(e)
|
|
||||||
|
|
||||||
def saveInputs():
|
def saveInputs():
|
||||||
try:
|
try:
|
||||||
spam_file = open(spam_filename, "w")
|
spam_file = open(spam_filename, "w")
|
||||||
spam_file.write(json.dumps(inputs))
|
spam_file.write(json.dumps(inputs))
|
||||||
spam_file.close()
|
spam_file.close()
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
error("Failed to save buttons and levers: " + str(e))
|
error("Failed to save damnspam: " + str(e))
|
||||||
|
|
||||||
|
|
||||||
|
def location_str(block):
|
||||||
|
return ";".join([block.getWorld().getName(),block.getX(),block.getY(),block.getZ()])
|
||||||
|
|
||||||
|
|
||||||
|
def add_input(creator, block, timeout_off, timeout_on):
|
||||||
|
global inputs
|
||||||
|
inputs[location_str(block)] = {
|
||||||
|
"creator" : str(creator.getUniqueId()),
|
||||||
|
"timeout_off" : timeout_off,
|
||||||
|
"timeout_on" : timeout_on,
|
||||||
|
"last_time" : 0
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@hook.command("damnspam")
|
||||||
|
def onDammnspamCommand(sender, args):
|
||||||
|
global inputs
|
||||||
|
|
||||||
|
try:
|
||||||
|
plugHeader(sender, "DamnSpam")
|
||||||
|
if len(args) <= 2:
|
||||||
|
|
||||||
|
if not str(sender.getGameMode()) == "CREATIVE":
|
||||||
|
msg(sender, "&cYou can only do this in Creative mode.")
|
||||||
|
return True
|
||||||
|
|
||||||
|
# /damnspam <secs>
|
||||||
|
if len(args) == 1:
|
||||||
|
timeout_on = args[0]
|
||||||
|
if not timeout_on.isdigit():
|
||||||
|
msg(sender, "&cThe timeout must be a digit.")
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
timeout_on = int(timeout_on)
|
||||||
|
timeout_off = timeout_on
|
||||||
|
if not 0 <= timeout_on <= 60:
|
||||||
|
msg(sender, "&cThe timeout must be within 0-60.")
|
||||||
|
return True
|
||||||
|
|
||||||
|
# /damnspam <off> <on>
|
||||||
|
elif len(args) == 2:
|
||||||
|
timeout_on = args[0]
|
||||||
|
timeout_off = args[1]
|
||||||
|
if not timeout_on.isdigit() or not timeout_off.isdigit():
|
||||||
|
msg(sender, "&cThe timeout must be a digit.")
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
timeout_on = int(timeout_on)
|
||||||
|
timeout_off = int(timeout_off)
|
||||||
|
if not 0 <= timeout_on <= 60 or not 0 <= timeout_off <= 60:
|
||||||
|
msg(sender, "&cThe timeout must be within 0-60.")
|
||||||
|
return True
|
||||||
|
|
||||||
|
# get the block we're looking at
|
||||||
|
target = sender.getTargetBlock(None, 10)
|
||||||
|
ttype = str(target.getType())
|
||||||
|
if ttype not in accepted_inputs:
|
||||||
|
msg(sender, "&cPlease look at a button or lever while executing this command!")
|
||||||
|
return True
|
||||||
|
|
||||||
|
# add block to inputs
|
||||||
|
add_input(sender, target, timeout_off, timeout_on)
|
||||||
|
saveInputs()
|
||||||
|
msg(sender, "&aSuccessfully set a timeout for this %s." % ttype.lower())
|
||||||
|
return True
|
||||||
|
|
||||||
|
else:
|
||||||
|
msg(sender, "&c/damnspam <seconds> &e(Buttons/Levers)")
|
||||||
|
msg(sender, "&c/damnspam <seconds after off> <seconds after on> &e(Levers only)")
|
||||||
|
except Exception, e:
|
||||||
|
error(e)
|
||||||
|
|
||||||
|
|
||||||
@hook.event("block.BlockBreakEvent", "normal")
|
@hook.event("block.BlockBreakEvent", "normal")
|
||||||
def onBreak(event):
|
def onBreak(event):
|
||||||
try:
|
global inputs
|
||||||
|
|
||||||
sender = event.getPlayer()
|
sender = event.getPlayer()
|
||||||
block = event.getBlock()
|
block = event.getBlock()
|
||||||
if str(block.getType()) in accepted_inputs:
|
if str(block.getType()) in accepted_inputs:
|
||||||
for entry in inputs:
|
pos_str = location_str(block)
|
||||||
posX = int(entry["x"])
|
if inputs.get(pos_str):
|
||||||
posY = int(entry["y"])
|
plugHeader(sender, "DamnSpam")
|
||||||
posZ = int(entry["z"])
|
|
||||||
posX2 = block.getX()
|
|
||||||
posY2 = block.getY()
|
|
||||||
posZ2 = block.getZ()
|
|
||||||
if posX == posX2 and posY == posY2 and posZ == posZ2:
|
|
||||||
if sender.isSneaking():
|
if sender.isSneaking():
|
||||||
inputs.remove(entry)
|
inputs.pop(pos_str) # remove
|
||||||
saveInputs()
|
saveInputs()
|
||||||
msg(sender, "&eSuccessfully removed the input!")
|
msg(sender, "&eSuccessfully removed the input!")
|
||||||
return True
|
return True
|
||||||
@@ -77,6 +115,20 @@ def onBreak(event):
|
|||||||
msg(sender, "&cYou cannot destroy this input!")
|
msg(sender, "&cYou cannot destroy this input!")
|
||||||
msg(sender, "&7&lSneak&7 and break if you want to remove it.")
|
msg(sender, "&7&lSneak&7 and break if you want to remove it.")
|
||||||
return True
|
return True
|
||||||
break
|
|
||||||
except Exception, e:
|
|
||||||
error("BlockBreakEvent failed: " + str(e))
|
@hook.event("player.PlayerInteractEvent", "normal")
|
||||||
|
def onInteract(event):
|
||||||
|
if (str(event.getAction()) == "RIGHT_CLICK_BLOCK") and not event.isCancelled():
|
||||||
|
sender = event.getPlayer()
|
||||||
|
block = event.getClickedBlock().getState()
|
||||||
|
btype = str(block.getType()).lower()
|
||||||
|
powered = (block.getData() & 0x8) == 0x8 if btype == "lever" else False # data > 7, but this is how bukkit does it
|
||||||
|
pos_str = location_str(block)
|
||||||
|
data = inputs.get(pos_str)
|
||||||
|
if data:
|
||||||
|
checktime = data["timeout_on"] if powered else data["timeout_off"]
|
||||||
|
if data["last_time"] + checktime < now():
|
||||||
|
event.setCancelled(True)
|
||||||
|
plugHeader("DamnSpam")
|
||||||
|
msg(sender, "&cThis %s has a timeout of %ss." % (btype, checktime))
|
||||||
Reference in New Issue
Block a user