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

View File

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

View File

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

View File

@@ -2,25 +2,13 @@
from helpers import * from helpers import *
from time import time as now from time import time as now
import org.bukkit.event.block.BlockBreakEvent as BlockBreakEvent import org.bukkit.event.block.BlockBreakEvent as BlockBreakEvent
import json
spam_filename = "plugins/redstoner-utils.py.dir/files/damnspam.json" inputs = open_json_file("damnspam", {}) # format "x;y;z;World"
inputs = {} # format "x;y;z;World"
accepted_inputs = ["WOOD_BUTTON", "STONE_BUTTON", "LEVER"] 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(): def save_inputs():
try: save_json_file("damnspam", inputs)
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))
def location_str(block): def location_str(block):
@@ -28,7 +16,6 @@ def location_str(block):
def add_input(creator, block, timeout_off, timeout_on): def add_input(creator, block, timeout_off, timeout_on):
global inputs
inputs[location_str(block)] = { inputs[location_str(block)] = {
"creator" : uid(creator), "creator" : uid(creator),
"timeout_off" : timeout_off, "timeout_off" : timeout_off,
@@ -39,8 +26,6 @@ def add_input(creator, block, timeout_off, timeout_on):
@hook.command("damnspam") @hook.command("damnspam")
def on_dammnspam_command(sender, args): def on_dammnspam_command(sender, args):
global inputs
plugin_header(sender, "DamnSpam") plugin_header(sender, "DamnSpam")
if len(args) in [1,2]: if len(args) in [1,2]:
@@ -101,8 +86,6 @@ def on_dammnspam_command(sender, args):
@hook.event("block.BlockBreakEvent", "normal") @hook.event("block.BlockBreakEvent", "normal")
def on_block_break(event): def on_block_break(event):
global inputs
sender = event.getPlayer() sender = event.getPlayer()
block = event.getBlock() block = event.getBlock()
if str(block.getType()) in accepted_inputs and not event.isCancelled(): 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 org.bukkit.util import Vector
from math import sin from math import sin
ff_perm = "utils.forcefield" ff_perm = "utils.forcefield"
pass_perm = "utils.forcefield.ignore" pass_perm = "utils.forcefield.ignore"
ff_prefix = "&8[&bFF&8] " ff_prefix = "&8[&bFF&8] "
ff_users = [] ff_users = []
fd = 6 # forcefield distance fd = 6 # forcefield distance
Xv = 2.95 / fd # used in move_away(), this is more efficient. 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.)
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 # /ff admin is a future option I might implement

View File

@@ -1,18 +1,12 @@
import json
from helpers import * from helpers import *
from re import compile as reg_compile from re import compile as reg_compile
from traceback import format_exc as print_traceback 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 = open_json_file("mentio", {})
mentions = json.loads(open(mentio_filename).read()) max_amount = 3
except Exception, e: arrow = colorify(u"&r&7\u2192&r")
error("Failed to load mentions: %s" % e) regex = reg_compile(u"\u00A7[\\da-fk-or]")
@hook.event("player.AsyncPlayerChatEvent", "high") @hook.event("player.AsyncPlayerChatEvent", "high")
@@ -39,7 +33,7 @@ def onChat(event):
if isMentioned: if isMentioned:
colors = "".join(regex.findall("".join(words[:i+1]))) # join all color codes used upto this word 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 rec_words[i] = colorify("&r&a<&6") + stripcolors(word) + colorify("&r&a>&r") + colors # extra fancy highlight
# player was mentioned # player was mentioned
if rec_words != words: if rec_words != words:
try: try:
@@ -54,13 +48,14 @@ def onChat(event):
error("Failed to handle PlayerChatEvent:") error("Failed to handle PlayerChatEvent:")
error(print_traceback()) error(print_traceback())
@hook.command("listen") @hook.command("listen")
def onListenCommand(sender, args): def onListenCommand(sender, args):
try: try:
currWords = [] currWords = []
if str(sender.getUniqueId()) in mentions.keys(): if str(sender.getUniqueId()) in mentions.keys():
currWords = mentions[str(sender.getUniqueId())] currWords = mentions[str(sender.getUniqueId())]
# /listen add <word> # /listen add <word>
if len(args) == 2 and args[0].lower() == "add": if len(args) == 2 and args[0].lower() == "add":
@@ -110,10 +105,6 @@ def onListenCommand(sender, args):
except Exception, e: except Exception, e:
error(e) error(e)
def saveMentions(): def saveMentions():
try: save_json_file("mentio", mentions)
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))

View File

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

View File

@@ -1,27 +1,15 @@
import json
from time import time from time import time
from helpers import * from helpers import *
from random import randrange from random import randrange
lol_filename = "plugins/redstoner-utils.py.dir/files/lol.json" lols = open_json_file("lol", [])
lols = [] timeout = 15
timeout = 15 last_msg = 0
last_msg = 0
try:
lols = json.loads(open(lol_filename).read())
except Exception, e:
error("Failed to load lols: %s" % e)
def save_lols(): def save_lols():
try: save_json_file("lol", lols)
lolfile = open(lol_filename, "w")
lolfile.write(json.dumps(lols))
lolfile.close()
except Exception, e:
error("Failed to write lols: " + str(e))
def add_lol(txt): 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 net.minecraft.util.com.mojang.authlib.GameProfile as GameProfile
import org.bukkit.Bukkit as bukkit import org.bukkit.Bukkit as bukkit
# players.txt contains 1 name per line # players.txt contains 1 name per line
players = [line.strip() for line in open("players.txt").readlines()] 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.block.BlockBreakEvent as BlockBreakEvent
import org.bukkit.event.player.PlayerInteractEvent as PlayerInteractEvent import org.bukkit.event.player.PlayerInteractEvent as PlayerInteractEvent
import thread import thread
import json
from time import sleep from time import sleep
from helpers import * from helpers import *
tilehelpers_filename = "plugins/redstoner-utils.py.dir/files/tilehelpers.json" tilehelpers = open_json_file("tilehelpers", [])
tilehelpers = []
try:
tilehelpers = json.loads(open(tilehelpers_filename).read())
except Exception, e:
error("Failed to load tile helpers: %s" % e)
dirmap = { dirmap = {
# [x, y, z] # [x, y, z]