update recipes for Minecraft 1.21.3, fedora automation

This commit is contained in:
Alan O'Cull 2024-11-01 21:07:28 -04:00
parent 8041d3f2d1
commit c78974b11d
13 changed files with 183 additions and 5 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ loot_randomizer/loot_table/
loot_randomizer/*.zip
mcrecipescrambler/
build/
data/

18
Makefile Normal file
View File

@ -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

View File

@ -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

1
config/blacklist.txt Normal file
View File

@ -0,0 +1 @@
crafting_table

10
config/packnames.txt Normal file
View File

@ -0,0 +1,10 @@
smoothie
milkshake
butter
chocolate
cookie
pudding
biscuit
pancake
reindeer
doughball

View File

@ -0,0 +1,18 @@
smoothie
milkshake
butter
ice-cream
chocolate
cakedough
cookie
pudding
biscuit
brownie
orange-soda
milk
creamer
banana
syrup
pancake
reindeer
doughball

View File

@ -26,7 +26,7 @@ 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"}]')

View File

@ -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!")

51
scripts/build.sh Executable file
View File

@ -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