Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- =============================================
- =============================================
- NEWEST VERSION: https://pastebin.com/0KHCV7LU
- =============================================
- =============================================
- next iteration of still very simple skript: reworked file structure
- you now need in "wordsaveXYZ/data/functions/src" subdirectory your ".mcfun" files
- i.e. in ".../src/testNamespace/testFunction.mcfun" the example from last time -- compare https://pastebin.com/8Xcv3M7b
- examples for "testFunction.mcfun":
- ==============================================================================================================
- # simple introdution example
- say 123
- execute @s ~ ~ ~ #{
- say 1
- say 2
- say 3
- execute @a ~ ~ ~ #{ say hi
- say ho }# if @s[type=Player]
- }#
- say hello bye
- ======================================= OUTPUT:
- Successfully reloaded loot tables, advancements and
- functions
- [staffehn] 123
- [staffehn] 1
- [staffehn] 2
- [staffehn] 3
- [staffehn] hi
- [staffehn] ho
- [staffehn] hello bye
- Executed 11 command(s) from function 'testNamespace:testFunction'
- ==============================================================================================================
- # the old example - for reference
- say hi
- execute @a ~ ~ ~ #{
- say ho
- tp ~ ~1 ~
- execute @e[r=10,type=!Player] ~ ~ ~ #{ #nesting is okay
- say I AM CLOSE
- scoreboard players add @s objxyz 1
- }#
- # Basically every "#{...}#" will be replaced by "function namespaceABC:compiled/XYZ" and
- # the file XYZ will be created, containing all of "...", but processed the same
- # way recursively. "function ... if/unless @.." is not supported yet.
- }#
- ==============================================================================================================
- then you compile.py from the same location as before (I mean the location of compile.py, namely "%appdata%/.minecraft/saves/wordsaveXYZ/data/functions/compile.py"
- then the converted files will for example be "wordsaveXYZ/data/functions/testNamespace/testFunction.mcfunction"
- so you can call it by "function testNamespace:testFunction"
- also the compiled extra files will now be in their namespaces.. very important for sharing your compiled results as a library
- with other mapmakers: for example if theres at least one "#{...}#" block in the testFunction the file
- "wordsaveXYZ/data/functions/testNamespace/compiled/0.mcfunction" will be created and used.
- Have fun, give me feedback ;-)
- I'm planning on changing the syntax in the future and add features, probably the "#"s will go somehow and we'll get first class "if/unless" support
- right now you can already do "#{...}# unless @s[...]" constructs, as came to mind to me today
- now onto the script:
- ===============================================================================================================
- '''
- import os
- import errno
- import collections
- import shutil
- nameCounter = collections.defaultdict(lambda:0)
- nextToWrite = collections.defaultdict(lambda:0)
- funsToWrite = collections.defaultdict(list)
- def match(str):
- res = [-1]
- depth = 0
- for i, c in enumerate(str):
- if c == "#":
- if i+1 < len(str) and str[i+1] == "{":
- if depth == 0: res.append(i)
- depth += 1
- elif i > 0 and str[i-1] == "}":
- depth -= 1
- if depth == 0: res.append(i)
- res.append(len(str))
- return res
- def createFile(k,name,content):
- global nameCounter
- global funsToWrite
- try:
- os.makedirs(os.path.dirname(name))
- except OSError as e:
- if e.errno != errno.EEXIST:
- raise
- with open(name, "w") as f:
- f.write("# DON'T EDIT TIS FILE, OR CHANGES MIGHT BE LOST ON RECOMPILATION!\n\n")
- ixs = match(content)
- out = 1
- for i,j in zip(ixs, ixs[1:]):
- if out == 1:
- out = 0
- f.write(content[i+1:j])
- elif out == 0:
- out = 1
- funsToWrite[k].append(content[i+2:j-1])
- f.write("function "+k+":compiled/"+str(nameCounter[k]))
- nameCounter[k] += 1
- top = os.getcwd()
- dirs = [f for f in os.listdir(top) if not os.path.isfile(f)]
- for d in dirs:
- for d1 in [d2 for d2 in os.listdir(os.path.join(top,d)) if (not os.path.isfile(d2)) and d2 == "compiled"]:
- shutil.rmtree(os.path.join(top,d,d1))
- if "src" in dirs:
- for root, dirs, filenames in os.walk(os.path.join(top,"src")):
- for name in filenames:
- space = ""
- rest, next = os.path.split(root)
- pathnew = next
- while next != "src":
- path = pathnew
- space = next
- rest, next = os.path.split(rest)
- pathnew = os.path.join(next,pathnew)
- sth, ext = os.path.splitext(name)
- if ext == ".mcfun":
- file = open(os.path.join(root, name),'r')
- content=file.read()
- file.close()
- newname = os.path.join(top,path, name+"ction") # TODO
- createFile(space,newname,content)
- for k in funsToWrite:
- while (len(funsToWrite[k]) != 0):
- fun = funsToWrite[k][0]
- funsToWrite[k] = funsToWrite[k][1:]
- createFile(k,os.path.join(top,space,"compiled",str(nextToWrite[k])+".mcfunction"),fun)
- nextToWrite[k] += 1
Advertisement
Add Comment
Please, Sign In to add comment