Last active
November 10, 2020 10:32
-
-
Save DmitryRendov/39189765597d41f2925fceca70e5abfa to your computer and use it in GitHub Desktop.
Export GriefPrevention claims for MCA Selector or WE commands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
$ python3 GPClaimManager.py --help | |
usage: GPClaimManager.py [-h] [-f FOLDER] [-a ACTION] [-c CLAIMTYPE] [-w WORLD] | |
Export GriefPrevention claims for MCA Selector or WE commands | |
optional arguments: | |
-h, --help show this help message and exit | |
-f FOLDER, --folder FOLDER | |
Folder name with GriefPrevention yml files (default: ClaimData) | |
-a ACTION, --action ACTION | |
Action to perform: mca, we (default: mca) | |
-c CLAIMTYPE, --claimtype CLAIMTYPE | |
Type of claims to process: all, admin, user (default: all) | |
-w WORLD, --world WORLD | |
World to process (default: survival) | |
""" | |
import os | |
import yaml | |
import argparse | |
import logging | |
files = [] | |
DEBUG = False | |
logger = logging.getLogger(__name__) | |
if DEBUG: | |
logger.setLevel(logging.DEBUG) | |
else: | |
logger.setLevel(logging.INFO) | |
# How many chunks extend your claims | |
EXTEND = 1 | |
class ClaimAdmin: | |
def __init__(self, folder, world): | |
self.folder = folder | |
self.world = world | |
self.mca_file = "mca-chunks-list.csv" | |
def calculate_mca(self, x1, z1, x2, z2, out): | |
""" Create a csv file with chunks list for MCA Selector tool | |
Add EXTEND extra chunks from all sides | |
""" | |
for x in range(1 - EXTEND, 1 + EXTEND + ((x2-x1) // 16)): | |
for z in range(1 - EXTEND, 1 + EXTEND + ((z2-z1) // 16)): | |
mca = str((x1 + x*16 ) // 512) + ';' + str((z1 + z*16) // 512) + ';' + str((x1 // 16) + x) + ';' + str((z1 // 16) + z) + '' | |
if mca not in out: | |
out.append(mca) | |
def process(self, action, claimtype): | |
""" Export all chunks from your claims to MCA Selector | |
- ignore inheritted claims | |
- ignore admin claims unless it's asked via params | |
- you can specify a world name to process | |
""" | |
out = [] | |
for f in os.listdir(self.folder): | |
if f.endswith('.yml'): | |
with open(self.folder + "/" + f, 'r') as stream: | |
try: | |
content = yaml.safe_load(stream) | |
logger.info(' %s', f) | |
except yaml.YAMLError as exc: | |
print(exc) | |
world, x1, y1, z1 = content["Lesser Boundary Corner"].split(';') | |
_, x2, y2, z2 = content["Greater Boundary Corner"].split(';') | |
if content['Parent Claim ID'] != -1: | |
print('Inherited claim, skipping...') | |
continue | |
if claimtype != "all": | |
if claimtype == "user" and not content['Owner']: | |
print('Admin claim, skipping...') | |
continue | |
if claimtype == "admin" and content['Owner']: | |
print('User claim, skipping...') | |
continue | |
if (world == self.world): | |
if action == "mca": | |
self.calculate_mca(int(x1), int(z1), int(x2), int(z2), out) | |
elif action == "we": | |
out.append('//pos1 ' + x1 + ',' + y1 + ',' + z1) | |
out.append('//pos2 ' + x2 + ',' + y2 + ',' + z2) | |
out.append('//copy -e') | |
out.append('//schem save ' + os.path.splitext(f)[0]) | |
else: | |
logger.info('Is not overworld region. Skipping.') | |
self.export_to_file('\n'.join(out)) | |
def export_to_file(self, out): | |
if out: | |
output = open(self.mca_file, 'w+') | |
output.write(out) | |
output.close() | |
print('Exported to ', os.path.abspath(self.mca_file)) | |
else: | |
print('Nothing to export') | |
if __name__ == "__main__": | |
logging.basicConfig(format='%(asctime)s %(levelname)s:%(message)s', | |
level=logging.INFO) | |
parser = argparse.ArgumentParser( | |
description="Export GriefPrevention claims for MCA Selector or WE commands", | |
formatter_class=argparse.ArgumentDefaultsHelpFormatter) | |
parser.add_argument("-f", "--folder", default="ClaimData", help="Folder name with GriefPrevention yml files") | |
parser.add_argument("-a", "--action", default="mca", help="Action to perform: mca, we") | |
parser.add_argument("-c", "--claimtype", default="all", help="Type of claims to process: all, admin, user") | |
parser.add_argument("-w", "--world", default="survival", help="World to process") | |
args = parser.parse_args() | |
ca = ClaimAdmin(args.folder, args.world) | |
ca.process(args.action, args.claimtype) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment