This script is a tool to help minecraft mod development. When you want to add a new block to the game you have to create three json files. This script generates three JSON files in specific locations. It's intended to be run in a certain directory so that the files go into the right location. Also it's for my own personal use.
The reason I didn't use the json module is simply because I didn't feel like. If it would make this script much better then I am all for switching to that although I did enjoy writing my json methods and found it educational which is another reason I'm making this, to learn a bit of python.
It's quite long and I think the functions could be taken out into a new file but then I'll need to make sure I have both files just to run this which I don't like.
I'm not concerned about efficiency or speed, just readability and maintainability, and general best practice.
import os
# Declare Functions
def jsonStart():
return "{\n"
def jsonEnd(indent):
return "\n" + jsonIndent(indent) + "}"
def jsonIndent(amount):
amount = amount * 4
return " " * amount
def jsonKeyValue(key, value, indent = 0):
return jsonIndent(indent) + jsonKey(key) + jsonValue(value)
def jsonKey(key, indent = 0):
return jsonIndent(indent) + "\"" + key + "\"" + ": "
def jsonValue(value):
return "\"" + value + "\""
def deleteCreatedFiles():
print("\nSomething went wrong")
for file in createdFiles:
print("Deleting: " + file)
os.remove(file)
print("\n")
def createFile(filename, data):
try:
os.makedirs(os.path.dirname(filename), exist_ok=True)
with open(filename, "w+")as newFile:
newFile.write(data)
except:
deleteCreatedFiles()
raise
else:
createdFiles.append(os.path.relpath(newFile.name))
print("Created" + os.path.relpath(newFile.name))
def blockStatesFile():
data = jsonStart()
data += jsonKey("variants", 1) + jsonStart()
data += jsonKey("normal", 2) + "{ " + jsonKeyValue("model", modid + ":" + blockName) + "}"
data += jsonEnd(1)
data += jsonEnd(0)
return data
def modelsItemFile():
data = jsonStart()
data += jsonKeyValue("parent", modid + ":block/" + blockName, 1) + ",\n"
data += jsonKey("textures", 1) + jsonStart()
data += jsonKeyValue("layer0", modid + ":items/" + blockName, 2)
data += jsonEnd(1)
data += jsonEnd(0)
return data
def modelsBlockFile():
data = jsonStart()
data += jsonKeyValue("parent", "block/cube_all", 1) + ",\n"
data += jsonKey("textures", 1) + jsonStart()
data += jsonKeyValue("all", modid + ":blocks/" + blockName, 2)
data += jsonEnd(1)
data += jsonEnd(0)
return data
# Run Script
createdFiles = []
blockName = input("block name: ")
modid = input("modid: ")
createFile("blockstates/" + blockName + ".json", blockStatesFile())
createFile("models/item/" + blockName + ".json", modelsItemFile())
createFile("models/block/" + blockName + ".json", modelsBlockFile())