1
+ import json , sys , os
2
+
3
+ #Declaring Variables
4
+ spaces = ""
5
+ singlefile = True
6
+ jsonfiles = []
7
+
8
+ ### This Area is filtering through given arguments and checking file validity. ###
9
+ #Did they specify what they wanted modified?
10
+ try :
11
+ file = sys .argv [1 ]
12
+ except IndexError as err :
13
+ print ("Please define a file to modify." )
14
+ sys .exit ()
15
+
16
+ #Function to get list of valid files in the directory if they seleced "all".
17
+ def filter_files ():
18
+ for file in jsonfiles :
19
+ if ".json" not in file :
20
+ jsonfiles .remove (file )
21
+
22
+ #Makes sure it's a .json file, or they chose all files.
23
+ if ".json" not in file :
24
+ if file == "all" :
25
+ jsonfiles = os .listdir ("." )
26
+ filter_files ()
27
+ print ("Selecting all files in this directory ending in .json to edit." )
28
+ response = input ("Are you sure you want to continue? (Y/N): " )
29
+ if response .lower () == "y" :
30
+ print ("Continuing..." )
31
+ singlefile = False
32
+ else :
33
+ sys .exit ()
34
+ else :
35
+ print ("Error. Not a JSON file. This program only accepts files appended with .json." )
36
+ sys .exit ()
37
+
38
+ #Function to check if the file is technically a valid json file. Will not be called if "all" files are selected.
39
+ def try_json ():
40
+ try :
41
+ json .loads (data )
42
+ except TypeError as err :
43
+ print ("Error: Please choose a valid JSON file. If you wish to bypass this, add \' bypass\' as an argument after the file." )
44
+ sys .exit ()
45
+
46
+ ### This area does a final JSON validity check, then starts searching for missing instances of TPYE. ###
47
+ for file in jsonfiles :
48
+ newlines = []
49
+ index = []
50
+ inside = False
51
+
52
+ with open (file , 'r' ) as data :
53
+ if singlefile :
54
+ try :
55
+ file = sys .argv [2 ]
56
+ except IndexError as err :
57
+ try_json ()
58
+ else :
59
+ if sys .argv [2 ] != "bypass" :
60
+ try_json ()
61
+ else :
62
+ print ("Bypassing any possible \' Invalid JSON\' errors..." )
63
+
64
+ for line in data :
65
+ newlines .append (line )
66
+ if ('\" count\" ' in line ) & ('{' in line ) & (':' in line ):
67
+ inside = True
68
+ if inside :
69
+ if '\" type\" ' in line :
70
+ inside = False
71
+ if '}' in line :
72
+ index .append (len (newlines ) - 3 )
73
+ inside = False
74
+
75
+ #This inserts the new "type": "minecraft:uniform" line into the list of lines, with correct indentation.
76
+ index .reverse ()
77
+ for item in index :
78
+ count = 0
79
+ for letter in newlines [item ]:
80
+ if letter == " " :
81
+ count += 1
82
+ spaces = " " * (count - 1 )
83
+ newlines .insert (item , f'{ spaces } \" type\" : \" minecraft:uniform\" ,\n ' )
84
+
85
+ #Finally, writes the new lines into given json file, replacing the old text.
86
+ with open (file , 'w' ) as f_out :
87
+ f_out .writelines (newlines )
88
+ print (f"Success! Added a TYPE for { len (index )} instances of COUNT in { file } ." )
89
+
90
+
91
+ ###OLD CODE###
92
+ #count = []
93
+ #path = []
94
+
95
+ #def find_count(current):
96
+ # count.append(0)
97
+ # level = count[len(count)-1]
98
+ #
99
+ # if type(current) == dict:
100
+ # print(f'\n{current}\n')
101
+ # for k, v in current.items():
102
+ # if isinstance(k, str):
103
+ # if (k == "min") | (k == "max"):
104
+ # #current["type"] = "minecraft:uniform"
105
+ # print('')
106
+ # for item in current.items():
107
+ # find_count(item)#
108
+ #
109
+ # if type(current) == list:
110
+ # for item in current:
111
+ # find_count(item)
112
+ #
113
+ # if type(current) == tuple:
114
+ # count[level] = 0
115
+ # while count[level] <= len(current) - 1:
116
+ # if type(current[count[level]]) == str:
117
+ # count[level] += 1
118
+ # continue
119
+ # else:
120
+ # find_count(current[count[level]])
121
+ # count[level] += 1
122
+ #
123
+ # count.pop(len(count)-1)
124
+
125
+ #for item in data.items():
126
+ # if type(data) == dict:
127
+ # find_count(item)
128
+ # if type(data) == list:
129
+ # for item in data:
130
+ # find_count(item)
131
+ # if type(data) == tuple:
132
+ # if data[0] == "type":
133
+ # print("Found a type!\n\n")
134
+ # else:
135
+ # find_count(item)
0 commit comments