|
1 | | -import zipfile |
2 | | -import json |
3 | 1 | import argparse |
4 | 2 | import sys |
| 3 | +from build_datapack import * |
5 | 4 |
|
6 | 5 | isCompiled = getattr(sys, 'frozen', False) |
7 | 6 | isUsingDefaults = (len(sys.argv) < 2) |
8 | 7 | validLootTableList = ['fishing', 'village', 'mansion', 'stronghold', 'zombie'] |
9 | | -greeting = "Babel Book Loot Generator, v0.5%s" % (' (Windows)' if isCompiled else '') |
| 8 | +greeting = "Babel Book Loot Generator, v1.0%s" % (' (Windows)' if isCompiled else '') |
10 | 9 |
|
11 | | -print("\n"+greeting) |
12 | | -print("="*len(greeting)+"\n") |
| 10 | +def restricted_float(x): |
| 11 | + error = "%r not a value between 0.0 and 1.0" |
| 12 | + try: |
| 13 | + x = float(x) |
| 14 | + except ValueError: |
| 15 | + raise argparse.ArgumentTypeError(error % x) |
| 16 | + |
| 17 | + if x < 0.0 or x > 1.0: |
| 18 | + raise argparse.ArgumentTypeError(error % x) |
| 19 | + return x |
13 | 20 |
|
14 | 21 | parser = argparse.ArgumentParser() |
15 | | -parser.add_argument('filename', help='Optional output zip filename. Default: babel.zip', nargs='?', default='babel.zip') |
16 | | -parser.add_argument('-d', dest="loottable", default=[], action='append', help='Disable adding books to one of the following loot tables: '+ |
17 | | - ', '.join(validLootTableList)+'. Can be repeated to disable more than one.') |
18 | | -parser.add_argument('-c', '--clean', help='Indent output JSON files.', action='store_true') |
| 22 | +parser.add_argument('filename', help='Optional output zip filename. (default: %(default)s)', nargs='?', default='babel.zip') |
| 23 | +parser.add_argument('-v', '--version', action='version', version=greeting) |
| 24 | +parser.add_argument('-d', dest="loottable", default=[], action='append', choices=validLootTableList, |
| 25 | + help='Disable adding books to the given loot tables. Can be repeated to disable more than one.') |
| 26 | +# parser.add_argument('--gen2', action='store', type=restricted_float, default=0.3, metavar="CHANCE", |
| 27 | + # help="Chance a book will be marked as a 'Copy of a copy', between 0.0 and 1.0. (default: %(default)s)") |
| 28 | +# parser.add_argument('--gen1', action='store', type=restricted_float, default=0.01, metavar="CHANCE", |
| 29 | + # help="Chance a book will be marked as a 'Copy of original', between 0.0 and 1.0. (default: %(default)s)") |
| 30 | +# parser.add_argument('--gen0', action='store', type=restricted_float, default=0.003, metavar="CHANCE", |
| 31 | + # help="Chance a book will be marked as an 'Original', between 0.0 and 1.0. (default: %(default)s)") |
| 32 | +#parser.add_argument('-l', '--loottable', action='store_true', |
| 33 | + # help="Don't build the datapack, instead just output the loot table. The default filename is books.json.") |
| 34 | +parser.add_argument('-i', '--indent', help='Indent output JSON files.', action='store_true') |
19 | 35 | if isCompiled: |
20 | | - parser.add_argument('-!', '--no-wait', help="Don't wait for user input at the end of the build. Triggered automatically by using any other argument.", |
21 | | - action='store_true') |
| 36 | + parser.add_argument('-!', '--no-wait', action='store_true', |
| 37 | + help="Don't wait for user input when finished. Triggered automatically by using any other argument.") |
22 | 38 | args = parser.parse_args() |
23 | 39 |
|
24 | | -if set(args.loottable).issubset(validLootTableList) == False: |
25 | | - print("ERROR: Invalid input on -d argument\n") |
26 | | - parser.print_help() |
27 | | - exit(1) |
28 | | - |
29 | | -if args.clean: |
| 40 | +if args.indent: |
30 | 41 | indent = 4 |
31 | 42 | else: |
32 | 43 | indent = None |
33 | 44 |
|
| 45 | +print("\n"+greeting) |
| 46 | +print("="*len(greeting)+"\n") |
| 47 | + |
34 | 48 | if isUsingDefaults: |
35 | 49 | print("Using default configuration, for more options try %s -h\n" % sys.argv[0]) |
36 | 50 |
|
37 | | -print ("Importing Books... (This may take a while.)") |
38 | | -from build_loottable import loottable |
39 | | -print ("Found %d books." % len(loottable['pools'][0]['entries'])) |
| 51 | +try: |
| 52 | + from build_loottable import loottable |
| 53 | + print ("Found %d books." % len(loottable['pools'][0]['entries'])) |
40 | 54 |
|
41 | | -def addToLootTable(lootfilename, weight = 1, pool = 0): |
42 | | - global indent |
43 | | - with open('base_loot_tables/'+lootfilename, 'r') as lootfile: |
44 | | - lootjson = json.loads(lootfile.read()) |
45 | | - lootjson['pools'][pool]['entries'].append({ |
46 | | - 'type': 'loot_table', |
47 | | - 'weight': weight, |
48 | | - "name": "babel:books" |
49 | | - }) |
50 | | - return json.dumps(lootjson, indent=indent, ensure_ascii=False) |
| 55 | + print ("Building datapack...") |
| 56 | + buildDatapack(args.filename, args.loottable, loottable, indent=indent) |
| 57 | + print ("\nDatapack build complete! Copy %s to your world's datapack directory." % args.filename) |
51 | 58 |
|
52 | | -print ("Building datapack...") |
53 | | -zf = zipfile.ZipFile(args.filename, mode='w') |
54 | | -zf.writestr('pack.mcmeta', json.dumps({ |
55 | | - "pack": { |
56 | | - "pack_format": 9, |
57 | | - "description": "Add pre-written books to your vanilla world" |
58 | | - } |
59 | | -}, indent=indent, ensure_ascii=False)) |
60 | | -zf.writestr('data/babel/loot_tables/books.json', json.dumps(loottable, indent=indent, ensure_ascii=False)) |
61 | | -if 'stronghold' not in args.loottable: |
62 | | - print ("Adding to Stronghold Library loot table.") |
63 | | - zf.writestr('data/minecraft/loot_tables/chests/stronghold_library.json', addToLootTable('stronghold_library.json',15)) |
64 | | -if 'mansion' not in args.loottable: |
65 | | - print ("Adding to Woodland Mansion loot table.") |
66 | | - zf.writestr('data/minecraft/loot_tables/chests/woodland_mansion.json', addToLootTable('woodland_mansion.json',5)) |
67 | | -if 'village' not in args.loottable: |
68 | | - print ("Adding to Village loot table.") |
69 | | - zf.writestr('data/minecraft/loot_tables/chests/village/village_desert_house.json', addToLootTable('village_desert_house.json',3)) |
70 | | - zf.writestr('data/minecraft/loot_tables/chests/village/village_plains_house.json', addToLootTable('village_plains_house.json',3)) |
71 | | - zf.writestr('data/minecraft/loot_tables/chests/village/village_savanna_house.json', addToLootTable('village_savanna_house.json',3)) |
72 | | - zf.writestr('data/minecraft/loot_tables/chests/village/village_snowy_house.json', addToLootTable('village_snowy_house.json',3)) |
73 | | - zf.writestr('data/minecraft/loot_tables/chests/village/village_taiga_house.json', addToLootTable('village_taiga_house.json',3)) |
74 | | -if 'fishing' not in args.loottable: |
75 | | - print ("Adding to Fishing Treasure loot table.") |
76 | | - zf.writestr('data/minecraft/loot_tables/gameplay/fishing/treasure.json', addToLootTable('treasure.json',1)) |
77 | | -if 'zombie' not in args.loottable: |
78 | | - print ("Adding to Zombie drop loot table.") |
79 | | - zf.writestr('data/minecraft/loot_tables/entities/zombie.json', addToLootTable('zombie.json',1,1)) |
80 | | -zf.close() |
81 | | -print ("\nDatapack build complete! Copy %s to your world's datapack directory." % args.filename) |
| 59 | +except Exception as e: |
| 60 | + print("\nError: "+str(e)) |
82 | 61 |
|
83 | | -if isCompiled and isUsingDefaults: |
84 | | - input("\nPress ENTER or close this window.") |
| 62 | +finally: |
| 63 | + if isCompiled and isUsingDefaults: |
| 64 | + input("\nPress ENTER or close this window.") |
0 commit comments