diff --git a/abot.py b/abot.py index 27508a3..b728c59 100644 --- a/abot.py +++ b/abot.py @@ -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: diff --git a/chatgroups.py b/chatgroups.py index 3fd029e..d87426b 100644 --- a/chatgroups.py +++ b/chatgroups.py @@ -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") diff --git a/cycle.py b/cycle.py index 7795f23..8460a0f 100644 --- a/cycle.py +++ b/cycle.py @@ -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)) \ No newline at end of file + save_json_file("cycle", no_cyclers) \ No newline at end of file diff --git a/damnspam.py b/damnspam.py index 1a93ae4..d252fd0 100644 --- a/damnspam.py +++ b/damnspam.py @@ -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(): diff --git a/forcefield.py b/forcefield.py index d5fa1fe..bafe4a9 100644 --- a/forcefield.py +++ b/forcefield.py @@ -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 diff --git a/mentio.py b/mentio.py index f557a5f..bb065f7 100644 --- a/mentio.py +++ b/mentio.py @@ -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") @@ -39,7 +33,7 @@ def onChat(event): if isMentioned: colors = "".join(regex.findall("".join(words[:i+1]))) # join all color codes used upto this word rec_words[i] = colorify("&r&a<&6") + stripcolors(word) + colorify("&r&a>&r") + colors # extra fancy highlight - + # player was mentioned if rec_words != words: try: @@ -54,13 +48,14 @@ def onChat(event): error("Failed to handle PlayerChatEvent:") error(print_traceback()) + @hook.command("listen") def onListenCommand(sender, args): try: currWords = [] if str(sender.getUniqueId()) in mentions.keys(): currWords = mentions[str(sender.getUniqueId())] - + # /listen add if len(args) == 2 and args[0].lower() == "add": @@ -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) \ No newline at end of file diff --git a/reports.py b/reports.py index a4fb504..6cd9ace 100644 --- a/reports.py +++ b/reports.py @@ -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, ()) + +thread.start_new_thread(reports_reminder, ()) \ No newline at end of file diff --git a/saylol.py b/saylol.py index 0b7ccc3..6da18c9 100644 --- a/saylol.py +++ b/saylol.py @@ -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): diff --git a/spawnplayer.py b/spawnplayer.py index 15dafd3..374c6e3 100644 --- a/spawnplayer.py +++ b/spawnplayer.py @@ -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()] diff --git a/tilehelper.py b/tilehelper.py index e8c2af6..04e510e 100644 --- a/tilehelper.py +++ b/tilehelper.py @@ -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]