1+ import os
2+ import time
3+
4+ # Rng import, mostly just writing large strings to a few files with the namespace filled in
5+ def import_rng (ver ):
6+
7+ print (" Importing rng..." )
8+ os .makedirs (f"./{ pack_name } /data/{ pack_namespace } /functions/rng/" )
9+
10+ with open (f"{ pack_name } /data/{ pack_namespace } /functions/rng/lcg.mcfunction" , "w" ) as f :
11+
12+ f .write (f"""# LCG Seed implementation
13+ #
14+ # x_(n+1) = x_(n)*a + c
15+ #
16+ # a = 1103515245, c = 12345
17+
18+ scoreboard players operation #lcg { pack_namespace } .rng *= #lcg_constant { pack_namespace } .rng
19+ scoreboard players add #lcg { pack_namespace } .rng 12345
20+ scoreboard players operation out { pack_namespace } .rng = #lcg { pack_namespace } .rng""" )
21+
22+ with open (f"{ pack_name } /data/{ pack_namespace } /functions/rng/next_int_lcg.mcfunction" , "w" ) as f :
23+
24+ f .write (f"""###
25+ # public int nextInt(int bound) {{
26+ # if (bound <= 0)
27+ # throw new IllegalArgumentException(BadBound);
28+ #
29+ # int r = next(31);
30+ # int m = bound - 1;
31+ # if ((bound & m) == 0) // i.e., bound is a power of 2
32+ # r = (int)((bound * (long)r) >> 31);
33+ # else {{
34+ # for (int u = r; u - (r = u % bound) + m < 0; u = next(31));
35+ # }}
36+ # return r;
37+ # }}
38+
39+ function { pack_namespace } :rng/lcg
40+
41+ scoreboard players operation #temp { pack_namespace } .rng = out { pack_namespace } .rng
42+ scoreboard players operation out { pack_namespace } .rng %= #range { pack_namespace } .rng
43+ scoreboard players operation #temp { pack_namespace } .rng -= out { pack_namespace } .rng
44+ scoreboard players operation #temp { pack_namespace } .rng += #m1 { pack_namespace } .rng
45+ execute if score #temp { pack_namespace } .rng matches ..-1 run function { pack_namespace } :rng/next_int_lcg""" )
46+
47+ with open (f"{ pack_name } /data/{ pack_namespace } /functions/rng/range_lcg.mcfunction" , "w" ) as f :
48+
49+ f .write (f"""scoreboard players add in1 { pack_namespace } .rng 1
50+ scoreboard players operation #range { pack_namespace } .rng = in1 { pack_namespace } .rng
51+ scoreboard players operation #range { pack_namespace } .rng -= in { pack_namespace } .rng
52+
53+ scoreboard players operation #m1 { pack_namespace } .rng = #range { pack_namespace } .rng
54+ scoreboard players remove #m1 { pack_namespace } .rng 1
55+ function { pack_namespace } :rng/next_int_lcg
56+ scoreboard players operation out { pack_namespace } .rng += in { pack_namespace } .rng
57+
58+ scoreboard players reset #m1 { pack_namespace } .rng
59+ scoreboard players remove in1 { pack_namespace } .rng 1""" )
60+
61+ with open (f"{ pack_name } /data/{ pack_namespace } /functions/rng/setup.mcfunction" , "w" ) as f :
62+
63+ f .write (f"""scoreboard objectives add { pack_namespace } .rng dummy
64+
65+ scoreboard players set in { pack_namespace } .rng -100
66+ scoreboard players set in1 { pack_namespace } .rng 100
67+
68+ scoreboard players set #lcg_constant { pack_namespace } .rng 1103515245
69+ execute unless score #lcg { pack_namespace } .rng matches ..0 unless score #lcg { pack_namespace } .rng matches 1.. run function { pack_namespace } :rng/uuid_reset""" )
70+
71+ with open (f"{ pack_name } /data/{ pack_namespace } /functions/rng/uuid_reset.mcfunction" , "w" ) as f :
72+
73+ # UUID data storage changed in 1.16+
74+ insert = "UUID[0] 1"
75+ if ver < 6 :
76+ insert = "UUIDMost 0.00000000023283064365386962890625"
77+
78+ f .write (f"""summon area_effect_cloud ~ ~ ~ {{Tags:["get_uuid"]}}
79+ execute store result score #lcg { pack_namespace } .rng run data get entity @e[tag=get_uuid,limit=1] { insert }
80+ kill @e[tag=get_uuid]""" )
81+
82+ with open (f"{ pack_name } /data/{ pack_namespace } /functions/load.mcfunction" , "r" ) as f :
83+
84+ f_remp = f .read ()
85+
86+ with open (f"{ pack_name } /data/{ pack_namespace } /functions/load.mcfunction" , "w" ) as f :
87+
88+ f .write (f"{ f_remp } \n \n function { pack_namespace } :rng/setup" )
89+
90+
91+
92+
93+
94+
95+
96+
97+
98+
99+
100+
101+ print ("\n -- Thank you for using Datapack Generator by RoarkCats --\n " )
102+ pack_name = input (" Datapack Name: " )
103+ pack_namespace = input (" Datapack Namespace: " )
104+ pack_author = input (" Datapack Author: " )
105+ pack_ver = input (" Datapack Version: " )
106+ pack_rng = input (" Import Rng? (y/n) " )
107+
108+ try :
109+ pack_ver = int (pack_ver )
110+ except :
111+ pack_ver = 10
112+
113+ proc_time_start = time .process_time ()
114+
115+ print ("\n Generating folders..." )
116+ os .makedirs (f"./{ pack_name } /data/minecraft/tags/functions/" )
117+ os .makedirs (f"./{ pack_name } /data/{ pack_namespace } /functions/" )
118+
119+ print (" Generating files..." )
120+ with open (f"{ pack_name } /pack.mcmeta" , "w" ) as f :
121+
122+ f .write (f"""{{
123+ \" pack\" : {{
124+ \" pack_format\" : { pack_ver } ,
125+ \" description\" : \" { pack_name } by { pack_author } \"
126+ }}
127+ }}""" )
128+
129+ with open (f"{ pack_name } /data/minecraft/tags/functions/tick.json" , "w" ) as f :
130+
131+ f .write (f"""{{
132+ \" values\" :[
133+ \" { pack_namespace } :main\"
134+ ]
135+ }}""" )
136+
137+ with open (f"{ pack_name } /data/minecraft/tags/functions/load.json" , "w" ) as f :
138+
139+ f .write (f"""{{
140+ \" values\" :[
141+ \" { pack_namespace } :load\"
142+ ]
143+ }}""" )
144+
145+ with open (f"{ pack_name } /data/{ pack_namespace } /functions/main.mcfunction" , "w" ) as f :
146+
147+ f .write ("" )
148+
149+ with open (f"{ pack_name } /data/{ pack_namespace } /functions/load.mcfunction" , "w" ) as f :
150+
151+ f .write (f"tellraw @a {{\" text\" :\" Thank you for downloading { pack_name } by { pack_author } !\" ,\" color\" :\" gold\" }}" )
152+
153+ if pack_rng == "y" :
154+ import_rng (pack_ver )
155+
156+ proc_time_end = time .process_time ()
157+ print (f" Done! ({ (proc_time_end - proc_time_start )* 1000 } ms)" )
158+ input ()
0 commit comments