diff --git a/.gitignore b/.gitignore index ce0e680..e0ae6ed 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ loot_randomizer/loot_table/ loot_randomizer/*.zip mcrecipescrambler/ -build/ \ No newline at end of file +build/ +data/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..57043c5 --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +.PHONY: all + +all: clean setup build + +clean: + @rm -rf build/ + +setup: + @mkdir -p data/ + @mkdir -p build/ + +build: recipes + +recipes: + @./scripts/build.sh true false _recipes + +loot: + @./scripts/build.sh false true _loot diff --git a/README.md b/README.md index 66e0012..7d29d7d 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Trent keeps asking me for randomizers, so I'm automating the process now so it's ## DOING THE THING 1. First, run `$ prep.cmd` to clone the `mcrecipescrambler` repository -2. Go to `%appdata%\.minecraft\versions\1.21.1` in Windows, then use 7-Zip to open the `.jar` file as an archive +2. Go to `%appdata%\.minecraft\versions\1.21.1` in Windows (`/home/alan/.var/app/com.mojang.Minecraft/.minecraft/` linux flatpak), then use 7-Zip to open the `.jar` file as an archive 3. Copy the contents of `recipe` from the `.jar` into `mcrecipescrambler/crafting_files` (make directory as necessary) 4. Copy the contents of `loot_table` from the `.jar` into `loot_randomizer/loot_table` (can right-click directory and click **Copy to...**) 5. Run `build_double.cmd pack_name` in terminal diff --git a/config/blacklist.txt b/config/blacklist.txt new file mode 100644 index 0000000..3055baf --- /dev/null +++ b/config/blacklist.txt @@ -0,0 +1 @@ +crafting_table \ No newline at end of file diff --git a/config/packnames.txt b/config/packnames.txt new file mode 100644 index 0000000..bd4792f --- /dev/null +++ b/config/packnames.txt @@ -0,0 +1,10 @@ +smoothie +milkshake +butter +chocolate +cookie +pudding +biscuit +pancake +reindeer +doughball \ No newline at end of file diff --git a/config/packnames_extra.txt b/config/packnames_extra.txt new file mode 100644 index 0000000..9959d85 --- /dev/null +++ b/config/packnames_extra.txt @@ -0,0 +1,18 @@ +smoothie +milkshake +butter +ice-cream +chocolate +cakedough +cookie +pudding +biscuit +brownie +orange-soda +milk +creamer +banana +syrup +pancake +reindeer +doughball \ No newline at end of file diff --git a/loot_randomizer/randomize.py b/python/random_loot.py similarity index 89% rename from loot_randomizer/randomize.py rename to python/random_loot.py index 97a016a..888fd05 100644 --- a/loot_randomizer/randomize.py +++ b/python/random_loot.py @@ -22,11 +22,11 @@ else: datapack_filename = datapack_name + '.zip' print('Generating datapack...') - + file_list = [] remaining = [] -for dirpath, dirnames, filenames in os.walk('loot_table'): +for dirpath, dirnames, filenames in os.walk('../data/loot_table'): for filename in filenames: file_list.append(os.path.join(dirpath, filename)) remaining.append(os.path.join(dirpath, filename)) @@ -47,7 +47,7 @@ for from_file in file_dict: zip.writestr(os.path.join('data/minecraft/', file_dict[from_file]), contents) -zip.writestr('pack.mcmeta', json.dumps({'pack':{'pack_format':48, 'description':datapack_desc}}, indent=4)) +zip.writestr('pack.mcmeta', json.dumps({'pack':{'pack_format':57, 'description':datapack_desc}}, indent=4)) zip.writestr('data/minecraft/tags/functions/load.json', json.dumps({'values':['{}:reset'.format(datapack_name)]})) zip.writestr('data/{}/functions/reset.mcfunction'.format(datapack_name), 'tellraw @a ["",{"text":"Loot table randomizer by SethBling","color":"green"}]') diff --git a/python/scramble_recipes.py b/python/scramble_recipes.py new file mode 100644 index 0000000..75561fd --- /dev/null +++ b/python/scramble_recipes.py @@ -0,0 +1,79 @@ +import zipfile +import copy +from random import shuffle, choice +import os +import json +import sys + +# Check arguments +if len(sys.argv) < 2: + datapack_path = "scrambler.zip" + print("Using default datapack name: ./scrambler.zip") +else: + datapack_path = sys.argv[1] + +try: + blacklist = list(map(lambda s: s.strip(), open( + "../config/blacklist.txt", "r").readlines())) +except FileNotFoundError: + print("No blacklist detected, ignoring...") + blacklist = [] +all_data = {} # key is the filename, value is the object data. +for file_handler in os.scandir('../data/recipe/'): + (basename, ext) = os.path.splitext(file_handler.name) + if ext == ".json": + file_data = json.load(open(file_handler.path, "r")) + # Some special recipes don't have a result... + # And we don't files in the blacklist too! + if file_data.get("result") is None or basename in blacklist: + continue + all_data[file_handler.name] = file_data + +random_data = copy.deepcopy(all_data) + +# Create the zip file and write the pack.mcmeta +try: + zip_file = zipfile.ZipFile(datapack_path, "x", zipfile.ZIP_DEFLATED, False) +except FileExistsError: + print(f'{datapack_path} already exists.') + exit() + +data_folder = os.path.join("data", "minecraft", "recipe") +zip_file.writestr("pack.mcmeta", json.dumps({ + "pack": { + "pack_format": 57, + "description": "Scramble all the recipes!" + } +})) + +# Iterate over all the files and pop a random item from 'random_data', +# assigning its result value to the current file. +for i, (filename, obj) in enumerate(all_data.items()): + # I need to find a better way... + random_item = random_data.pop(choice(list(random_data.keys()))) + random_result = random_item.get("result") + obj_result = obj.get("result") + # Crafting recipes need an object for the result and everything else needs a string. + # We have to convert them if the types between the current file and the random item are different. + if type(random_result) == type(obj_result): + obj["result"] = random_item["result"] + # Case where we need a str but the random item is an object + elif type(random_result) == dict and type(obj_result) == str: + obj["result"] = random_item["result"]["id"] + else: # Case where we need an object but the random item is a str + obj["result"] = { + "item": random_item["result"] + } + + zip_file.writestr(os.path.join(data_folder, filename), json.dumps(obj)) + # Progress bar :D + # Thanks : https://gist.github.com/sibosutd/c1d9ef01d38630750a1d1fe05c367eb8 + percent = 100.0*(i+1)/len(all_data.items()) + sys.stdout.write('\r') + sys.stdout.write("Completed: [{:{}}] {:>3}%" + .format('='*int(percent/(100.0/20)), + 20, int(percent))) + sys.stdout.flush() +zip_file.close() +print() +print("Done!") diff --git a/scripts/build.sh b/scripts/build.sh new file mode 100755 index 0000000..1969537 --- /dev/null +++ b/scripts/build.sh @@ -0,0 +1,51 @@ +#!/usr/bin/bash + +DORECIPES=$1 +DOLOOT=$2 +APPEND=$3 + +for fn in `cat config/packnames.txt`; do + PACKNAME="$fn$APPEND" + PACK="build/$PACKNAME" + echo "generating $PACK" + mkdir -p $PACK + + PATHLOOT="python/random_loot.zip" + PATHRECIPES="python/scrambler.zip" + + # Do randomization + if [ "$DOLOOT" = true ]; then + cd python/ && python random_loot.py + cd .. + fi + if [ "$DORECIPES" = true ]; then + cd python/ && python scramble_recipes.py + cd .. + fi + + # Wait for Filesystem to catch up + echo "generated packs, waiting" + sleep 0.3 + + # Extract generated zip files + echo "extracting files" + if [ -f $PATHLOOT ]; then + 7z x $PATHLOOT -o$PACK -r -y + fi + if [ -f $PATHRECIPES ]; then + 7z x $PATHRECIPES -o$PACK -r -y + + # rename recipes to recipe, to support 1.21 format + mv $PACK/data/minecraft/recipes $PACK/data/minecraft/recipe + fi + + # Zip it all up + echo "zipping datapack" + cd build/ + 7z a -tzip $PACKNAME.zip $PACKNAME/* + cd .. + + # rm -rf $PACK + rm -rf $PATHLOOT + rm -rf $PATHRECIPES +done diff --git a/build_double.cmd b/scripts/build_double.cmd similarity index 100% rename from build_double.cmd rename to scripts/build_double.cmd diff --git a/build_loot.cmd b/scripts/build_loot.cmd similarity index 100% rename from build_loot.cmd rename to scripts/build_loot.cmd diff --git a/build_recipes.cmd b/scripts/build_recipes.cmd similarity index 100% rename from build_recipes.cmd rename to scripts/build_recipes.cmd diff --git a/prep.cmd b/scripts/prep.cmd similarity index 100% rename from prep.cmd rename to scripts/prep.cmd