Minecraft Blogs / Tutorial

I edited SethBling's loot table randomizer Python script to support mods!

  • 1,711 views, 2 today
  • 2
  • 0
Crosby's Avatar Crosby
Level 26 : Expert Paladin
55
Credits to SethBling for the original Python script loot table randomizer. https://youtu.be/3JEXAZOrykQ

This technique works with both fabric and forge. It also works with other datapacks.
This only works with mods and datapacks that have loot tables (Blocks/Mobs).
You need to have downloaded Python to run the script.

How to use:

When downloading SethBling's Python script, it also comes with vanilla Minecraft loot tables. To get modded ones you need to:
1. Download the mod .jar
2. Rename the mod from a .jar to a .zip
3. Extract the .zip
4. Inside the "data" folder, copy the namespace folder and paste it inside the folder that you downloaded and that has the Python script.
5. Inside the mod's namespace folder, delete every other folder except "loot_tables"

Datapacks:
1. Download the datapack
2. Extract the datapack
3. Copy the namespace folder (and Minecraft folder if applicable) and paste it inside the folder that you downloaded and that has the Python script.
4. Inside the datapack's namespace (and Minecraft folder if applicable) folder, delete every other folder except "loot_tables"

Editing the script:
1. Near the end of the script, edit this line:
  zip.writestr(os.path.join('data/minecraft/', file_dict[​from_file]), contents)
Into:
  zip.writestr(os.path.join('data/', file_dict[​from_file]), contents)

2. Copy & paste these following lines for the amount of mods/datapacks you want added to the loot table randomizer:
for dirpath, dirnames, filenames in os.walk('minecraft'):
  for filename in filenames:
    file_list.append(os.path.join(dirpath, filename))
    remaining.append(os.path.join(dirpath, filename))

3. Replace the 'minecraft' portion of every lines of code you pasted in #2 with the namespace of the mods/datapacks you want to be added to the randomizer.

Example full edited script:
import os
import random
import io
import zipfile
import json
import sys

if len(sys.argv) >= 2:
  try:
    seed = int(sys.argv[​1])
  except Exception:
    print('The seed "{}" is not an integer.'.format(sys.argv[​1]))
    exit()
  random.seed(seed)
  datapack_name = 'random_loot_{}'.format(seed)
  datapack_desc = 'Loot Table Randomizer, Seed: {}'.format(seed)
else:
  print('If you want to use a specific randomizer seed integer, use: "python randomize.py <seed>"')
  datapack_name = 'random_loot'
  datapack_desc = 'Loot Table Randomizer'

datapack_filename = datapack_name + '.zip'

print('Generating datapack...')

file_list = []
remaining = []

for dirpath, dirnames, filenames in os.walk('minecraft'):
  for filename in filenames:
    file_list.append(os.path.join(dirpath, filename))
    remaining.append(os.path.join(dirpath, filename))

for dirpath, dirnames, filenames in os.walk('traverse'):
  for filename in filenames:
    file_list.append(os.path.join(dirpath, filename))
    remaining.append(os.path.join(dirpath, filename))

for dirpath, dirnames, filenames in os.walk('cinderscapes'):
  for filename in filenames:
    file_list.append(os.path.join(dirpath, filename))
    remaining.append(os.path.join(dirpath, filename))

file_dict = {}

for file in file_list:
  i = random.randint(0, len(remaining)-1)
  file_dict[​file] = remaining[​i]
  del remaining[​i]

zipbytes = io.BytesIO()
zip = zipfile.ZipFile(zipbytes, 'w', zipfile.ZIP_DEFLATED, False)

for from_file in file_dict:
  with open(from_file) as file:
    contents = file.read()

zip.writestr(os.path.join('data/', file_dict[​from_file]), contents)

zip.writestr('pack.mcmeta', json.dumps({'pack':{'pack_format':1, '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"}]')

zip.close()
with open(datapack_filename, 'wb') as file:
  file.write(zipbytes.getvalue())

print('Created datapack "{}"'.format(datapack_filename))


Run the script as usual and it should output a loot table randomizer datapack that supports mods & other datapacks.
CreditSethBling | https://youtu.be/3JEXAZOrykQ
Tags

Create an account or sign in to comment.

Welcome