apply file read/save helpers

This commit is contained in:
jomo
2014-07-27 19:42:33 +02:00
parent 2e7e021001
commit aad63e5416
10 changed files with 42 additions and 121 deletions

View File

@@ -1,17 +1,12 @@
import json
from helpers import *
from re import compile as reg_compile
answers_filename = "plugins/redstoner-utils.py.dir/files/abot.json"
answers = []
answers = []
def load_answers():
global answers
try:
answers = json.loads(open(answers_filename).read())
except Exception, e:
error("Failed to load answers: %s" % e)
answers = open_json_file("abot", [])
# compile answers
for answer in answers:

View File

@@ -1,17 +1,11 @@
#pylint: disable = F0401
from helpers import *
from java.util.UUID import fromString as juuid
import json
chatgroups_filename = "plugins/redstoner-utils.py.dir/files/chatgroups.json"
groups = {}
cg_key = ":"
cg_toggle_list = []
groups = open_json_file("chatgroups", {})
cg_key = ":"
cg_toggle_list = []
try:
groups = json.loads(open(chatgroups_filename).read())
except Exception, e:
error("Failed to load chatgroups: %s" % e)
@hook.command("chatgroup")
@@ -86,12 +80,7 @@ def groupchat(sender, message, ann = False):
def save_groups():
try:
chatgroups_file = open(chatgroups_filename, "w")
chatgroups_file.write(json.dumps(groups))
chatgroups_file.close()
except Exception, e:
error("Failed to write reports: " + str(e))
save_json_file("chatgroups", groups)
@hook.event("player.AsyncPlayerChatEvent", "normal")

View File

@@ -1,12 +1,6 @@
import json
from helpers import *
cyclers_file = "plugins/redstoner-utils.py.dir/files/cycle.json"
no_cyclers = [] # opt-out
try:
no_cyclers = json.loads(open(cyclers_file).read())
except Exception, e:
error("Failed to load no_cyclers: %s" % e)
no_cyclers = open_json_file("cycle", [])
@hook.command("cycle")
@@ -73,9 +67,4 @@ def do_cycle(player, down):
inv.setContents(items)
def save_cyclers():
try:
chatgroups_file = open(cyclers_file, "w")
chatgroups_file.write(json.dumps(no_cyclers))
chatgroups_file.close()
except Exception, e:
error("Failed to write reports: " + str(e))
save_json_file("cycle", no_cyclers)

View File

@@ -2,25 +2,13 @@
from helpers import *
from time import time as now
import org.bukkit.event.block.BlockBreakEvent as BlockBreakEvent
import json
spam_filename = "plugins/redstoner-utils.py.dir/files/damnspam.json"
inputs = {} # format "x;y;z;World"
inputs = open_json_file("damnspam", {}) # format "x;y;z;World"
accepted_inputs = ["WOOD_BUTTON", "STONE_BUTTON", "LEVER"]
try:
inputs = json.loads(open(spam_filename).read())
except Exception, e:
error("Failed to load buttons and levers: %s" % e)
def save_inputs():
try:
spam_file = open(spam_filename, "w")
spam_file.write(json.dumps(inputs))
spam_file.close()
except Exception, e:
error("Failed to save damnspam: " + str(e))
save_json_file("damnspam", inputs)
def location_str(block):
@@ -28,7 +16,6 @@ def location_str(block):
def add_input(creator, block, timeout_off, timeout_on):
global inputs
inputs[location_str(block)] = {
"creator" : uid(creator),
"timeout_off" : timeout_off,
@@ -39,8 +26,6 @@ def add_input(creator, block, timeout_off, timeout_on):
@hook.command("damnspam")
def on_dammnspam_command(sender, args):
global inputs
plugin_header(sender, "DamnSpam")
if len(args) in [1,2]:
@@ -101,8 +86,6 @@ def on_dammnspam_command(sender, args):
@hook.event("block.BlockBreakEvent", "normal")
def on_block_break(event):
global inputs
sender = event.getPlayer()
block = event.getBlock()
if str(block.getType()) in accepted_inputs and not event.isCancelled():

View File

@@ -3,14 +3,13 @@ from helpers import *
from org.bukkit.util import Vector
from math import sin
ff_perm = "utils.forcefield"
pass_perm = "utils.forcefield.ignore"
ff_prefix = "&8[&bFF&8] "
ff_users = []
fd = 6 # forcefield distance
Xv = 2.95 / fd # used in move_away(), this is more efficient.
whitelists_filename = "plugins/redstoner-utils.py.dir/files/forcefield.json"
whitelists = {} # {ff_owner_id: [white, listed, ids]} (Adding file usage later, should be simple but just not yet.)
ff_perm = "utils.forcefield"
pass_perm = "utils.forcefield.ignore"
ff_prefix = "&8[&bFF&8] "
ff_users = []
fd = 6 # forcefield distance
Xv = 2.95 / fd # used in move_away(), this is more efficient.
whitelists = {} # {ff_owner_id: [white, listed, ids]} (Adding file usage later, should be simple but just not yet.)
# /ff admin is a future option I might implement

View File

@@ -1,18 +1,12 @@
import json
from helpers import *
from re import compile as reg_compile
from traceback import format_exc as print_traceback
mentio_filename = "plugins/redstoner-utils.py.dir/files/mentio.json"
mentions = {}
max_amount = 3
arrow = colorify(u"&r&7\u2192&r")
regex = reg_compile(u"\u00A7[\\da-fk-or]")
try:
mentions = json.loads(open(mentio_filename).read())
except Exception, e:
error("Failed to load mentions: %s" % e)
mentions = open_json_file("mentio", {})
max_amount = 3
arrow = colorify(u"&r&7\u2192&r")
regex = reg_compile(u"\u00A7[\\da-fk-or]")
@hook.event("player.AsyncPlayerChatEvent", "high")
@@ -54,6 +48,7 @@ def onChat(event):
error("Failed to handle PlayerChatEvent:")
error(print_traceback())
@hook.command("listen")
def onListenCommand(sender, args):
try:
@@ -110,10 +105,6 @@ def onListenCommand(sender, args):
except Exception, e:
error(e)
def saveMentions():
try:
mentio_file = open(mentio_filename, "w")
mentio_file.write(json.dumps(mentions))
mentio_file.close()
except Exception, e:
error("Failed to write mentions: " + str(e))
save_json_file("mentio", mentions)

View File

@@ -1,19 +1,14 @@
from helpers import *
from java.util.UUID import fromString as id_to_player
import json
import time
import thread
reports_filename = "plugins/redstoner-utils.py.dir/files/reports.json"
time_format = "%Y.%m.%d %H:%M"
reports = []
check_reports = True
check_delay = 60 * 10 # Every 10 minutes, staff will be notified about pending reports.
rp_permission = "utils.rp"
try:
reports = json.loads(open(reports_filename).read())
except Exception, e:
error("Failed to load reports: %s" % e)
time_format = "%Y.%m.%d %H:%M"
reports = open_json_file("reports", [])
check_reports = True
check_delay = 60 * 10 # Every 10 minutes, staff will be notified about pending reports.
rp_permission = "utils.rp"
def print_help(sender):
@@ -28,6 +23,7 @@ def print_list(sender):
for i, report in enumerate(reports):
msg(sender, "&8[&e" + str(i) + " &c" + report["time"] + "&8] &3" + server.getOfflinePlayer(id_to_player(report["uuid"])).getName() + "&f: &a" + report["msg"])
def tp_report(sender, rep_id):
if rep_id >= len(reports) or rep_id < 0:
msg(sender, "&cReport &3#" + str(rep_id) + "&c does not exist!")
@@ -52,12 +48,7 @@ def delete_report(sender, rep_id):
def save_reports():
try:
reports_file = open(reports_filename, "w")
reports_file.write(json.dumps(reports))
reports_file.close()
except Exception, e:
error("Failed to write reports: " + str(e))
save_json_file("reports", reports)
@hook.command("rp")
@@ -139,4 +130,5 @@ def stop_reporting():
log("Ending reports reminder thread")
check_reports = False
thread.start_new_thread(reports_reminder, ())

View File

@@ -1,27 +1,15 @@
import json
from time import time
from helpers import *
from random import randrange
lol_filename = "plugins/redstoner-utils.py.dir/files/lol.json"
lols = []
timeout = 15
last_msg = 0
try:
lols = json.loads(open(lol_filename).read())
except Exception, e:
error("Failed to load lols: %s" % e)
lols = open_json_file("lol", [])
timeout = 15
last_msg = 0
def save_lols():
try:
lolfile = open(lol_filename, "w")
lolfile.write(json.dumps(lols))
lolfile.close()
except Exception, e:
error("Failed to write lols: " + str(e))
save_json_file("lol", lols)
def add_lol(txt):

View File

@@ -4,6 +4,7 @@ import net.minecraft.server.v1_7_R1.PlayerInteractManager as PlayerInteractManag
import net.minecraft.util.com.mojang.authlib.GameProfile as GameProfile
import org.bukkit.Bukkit as bukkit
# players.txt contains 1 name per line
players = [line.strip() for line in open("players.txt").readlines()]

View File

@@ -3,16 +3,10 @@ import org.bukkit.event.block.BlockPlaceEvent as BlockPlaceEvent
import org.bukkit.event.block.BlockBreakEvent as BlockBreakEvent
import org.bukkit.event.player.PlayerInteractEvent as PlayerInteractEvent
import thread
import json
from time import sleep
from helpers import *
tilehelpers_filename = "plugins/redstoner-utils.py.dir/files/tilehelpers.json"
tilehelpers = []
try:
tilehelpers = json.loads(open(tilehelpers_filename).read())
except Exception, e:
error("Failed to load tile helpers: %s" % e)
tilehelpers = open_json_file("tilehelpers", [])
dirmap = {
# [x, y, z]