1010
1111import java .io .*;
1212import java .nio .charset .StandardCharsets ;
13- import java .util .ArrayList ;
14- import java .util .HashMap ;
15- import java .util .List ;
16- import java .util .Map ;
13+ import java .util .*;
1714import java .util .stream .Stream ;
1815
1916import org .json .simple .JSONArray ;
@@ -27,12 +24,13 @@ public class Config {
2724 private static final File configFile = FabricLoader .getInstance ().getConfigDir ().resolve ("SDS.json" ).toFile ();
2825
2926 public static boolean whitelist ;
30- private static HashMap <String , Boolean > properties = new HashMap <>();
27+ private static HashMap <String , List <String >> properties_allowed = new HashMap <>();
28+ private static HashMap <String , List <String >> properties_forbidden = new HashMap <>();
3129
32- private static HashMap <String , List <String >> tags_allowed = new HashMap <>();
33- private static HashMap <String , List <String >> blocks_allowed = new HashMap <>();
34- private static HashMap <String , List <String >> tags_forbidden = new HashMap <>();
35- private static HashMap <String , List <String >> blocks_forbidden = new HashMap <>();
30+ private static HashMap <String , HashMap < String , List <String > >> tags_allowed = new HashMap <>();
31+ private static HashMap <String , HashMap < String , List <String > >> blocks_allowed = new HashMap <>();
32+ private static HashMap <String , HashMap < String , List <String > >> tags_forbidden = new HashMap <>();
33+ private static HashMap <String , HashMap < String , List <String > >> blocks_forbidden = new HashMap <>();
3634
3735 public static void load () {
3836 JSONParser parser = new JSONParser ();
@@ -43,20 +41,34 @@ public static void load() {
4341 whitelist = (boolean ) jfile .get ("whitelist" );
4442
4543 JSONObject allowed = (JSONObject ) jfile .get ("allowed" );
46- JSONArray properties_allowed = (JSONArray ) allowed .get ("properties" );
4744 JSONArray tags_allowed_js = (JSONArray ) allowed .get ("tags" );
4845 JSONArray blocks_allowed_js = (JSONArray ) allowed .get ("blocks" );
4946
47+ Object ObjectProperties_allowed = allowed .get ("properties" );
48+ JSONObject properties_allowed_js = (ObjectProperties_allowed instanceof JSONArray ) ? parseOldArrayToObject ((JSONArray ) allowed .get ("properties" )) : (JSONObject ) allowed .get ("properties" );
49+
5050 JSONObject forbidden = (JSONObject ) jfile .get ("forbidden" );
51- JSONArray properties_forbidden = (JSONArray ) forbidden .get ("properties" );
5251 JSONArray tags_forbidden_js = (JSONArray ) forbidden .get ("tags" );
5352 JSONArray blocks_forbidden_js = (JSONArray ) forbidden .get ("blocks" );
5453
55- for (String property : (Iterable <String >) properties_allowed ) {
56- properties .put (property , true );
54+ Object ObjectProperties_forbidden = forbidden .get ("properties" );
55+ JSONObject properties_forbidden_js = (ObjectProperties_forbidden instanceof JSONArray ) ? parseOldArrayToObject ((JSONArray ) forbidden .get ("properties" )) : (JSONObject ) forbidden .get ("properties" );
56+
57+ for (Object property : properties_allowed_js .keySet ()) {
58+ JSONArray JSONPropertyValues = (JSONArray ) properties_allowed_js .get (property );
59+ List <String > propertyValues = new ArrayList <>();
60+ for (Object value : JSONPropertyValues ) {
61+ propertyValues .add (value .toString ());
62+ }
63+ properties_allowed .put ((String ) property , propertyValues );
5764 }
58- for (String property : (Iterable <String >) properties_forbidden ) {
59- properties .put (property , false );
65+ for (Object property : properties_forbidden_js .keySet ()) {
66+ JSONArray JSONPropertyValues = (JSONArray ) properties_forbidden_js .get (property );
67+ List <String > propertyValues = new ArrayList <>();
68+ for (Object value : JSONPropertyValues ) {
69+ propertyValues .add (value .toString ());
70+ }
71+ properties_forbidden .put ((String ) property , propertyValues );
6072 }
6173
6274 populate (tags_allowed , tags_allowed_js );
@@ -72,7 +84,8 @@ public static void load() {
7284
7385 private static void factorySettings () {
7486 whitelist = false ;
75- properties = new HashMap <>();
87+ properties_allowed = new HashMap <>();
88+ properties_forbidden = new HashMap <>();
7689 tags_allowed = new HashMap <>();
7790 tags_forbidden = new HashMap <>();
7891 blocks_allowed = new HashMap <>();
@@ -85,40 +98,31 @@ public static void save() {
8598 jfile .put ("whitelist" , whitelist );
8699
87100 JSONObject allowed = new JSONObject ();
88- JSONArray properties_allowed = new JSONArray ();
89- for (Map .Entry <String , Boolean > entry : properties .entrySet ()) {
90- if (entry .getValue ()) properties_allowed .add (entry .getKey ());
91- }
92- allowed .put ("properties" , properties_allowed );
101+ JSONObject properties_allowed_js = new JSONObject (properties_allowed );
102+ allowed .put ("properties" , properties_allowed_js );
93103
94104 JSONArray tags_allowed_js = new JSONArray ();
95- for (Map .Entry <String , List <String >> entry : tags_allowed .entrySet ()) {
105+ for (Map .Entry <String , HashMap < String , List <String > >> entry : tags_allowed .entrySet ()) {
96106 JSONObject tag = new JSONObject ();
97107 tag .put ("id" , entry .getKey ());
98108
99- List <String > list = entry .getValue ();
100- if (!list .isEmpty ()) {
101- JSONArray props = new JSONArray ();
102- for (String property : list ) {
103- props .add (property );
104- }
109+ HashMap <String , List <String >> map = entry .getValue ();
110+ if (!map .isEmpty ()) {
111+ JSONObject props = new JSONObject (map );
105112 tag .put ("properties" , props );
106113 }
107114 tags_allowed_js .add (tag );
108115 }
109116 allowed .put ("tags" , tags_allowed_js );
110117
111118 JSONArray blocks_allowed_js = new JSONArray ();
112- for (Map .Entry <String , List <String >> entry : blocks_allowed .entrySet ()) {
119+ for (Map .Entry <String , HashMap < String , List <String > >> entry : blocks_allowed .entrySet ()) {
113120 JSONObject block = new JSONObject ();
114121 block .put ("id" , entry .getKey ());
115122
116- List <String > list = entry .getValue ();
117- if (!list .isEmpty ()) {
118- JSONArray props = new JSONArray ();
119- for (String property : list ) {
120- props .add (property );
121- }
123+ HashMap <String , List <String >> map = entry .getValue ();
124+ if (!map .isEmpty ()) {
125+ JSONObject props = new JSONObject (map );
122126 block .put ("properties" , props );
123127 }
124128 blocks_allowed_js .add (block );
@@ -128,40 +132,31 @@ public static void save() {
128132 jfile .put ("allowed" , allowed );
129133
130134 JSONObject forbidden = new JSONObject ();
131- JSONArray properties_forbidden = new JSONArray ();
132- for (Map .Entry <String , Boolean > entry : properties .entrySet ()) {
133- if (!entry .getValue ()) properties_forbidden .add (entry .getKey ());
134- }
135- forbidden .put ("properties" , properties_forbidden );
135+ JSONObject properties_forbidden_js = new JSONObject (properties_forbidden );
136+ forbidden .put ("properties" , properties_forbidden_js );
136137
137138 JSONArray tags_forbidden_js = new JSONArray ();
138- for (Map .Entry <String , List <String >> entry : tags_forbidden .entrySet ()) {
139+ for (Map .Entry <String , HashMap < String , List <String > >> entry : tags_forbidden .entrySet ()) {
139140 JSONObject tag = new JSONObject ();
140141 tag .put ("id" , entry .getKey ());
141142
142- List <String > list = entry .getValue ();
143- if (!list .isEmpty ()) {
144- JSONArray props = new JSONArray ();
145- for (String property : list ) {
146- props .add (property );
147- }
143+ HashMap <String , List <String >> map = entry .getValue ();
144+ if (!map .isEmpty ()) {
145+ JSONObject props = new JSONObject (map );
148146 tag .put ("properties" , props );
149147 }
150148 tags_forbidden_js .add (tag );
151149 }
152150 forbidden .put ("tags" , tags_forbidden_js );
153151
154152 JSONArray blocks_forbidden_js = new JSONArray ();
155- for (Map .Entry <String , List <String >> entry : blocks_forbidden .entrySet ()) {
153+ for (Map .Entry <String , HashMap < String , List <String > >> entry : blocks_forbidden .entrySet ()) {
156154 JSONObject block = new JSONObject ();
157155 block .put ("id" , entry .getKey ());
158156
159- List <String > list = entry .getValue ();
160- if (!list .isEmpty ()) {
161- JSONArray props = new JSONArray ();
162- for (String property : list ) {
163- props .add (property );
164- }
157+ HashMap <String , List <String >> map = entry .getValue ();
158+ if (!map .isEmpty ()) {
159+ JSONObject props = new JSONObject (map );
165160 block .put ("properties" , props );
166161 }
167162 blocks_forbidden_js .add (block );
@@ -182,7 +177,7 @@ public static void save() {
182177 } catch (IOException e ) {SDSMod .LOGGER .error ("Error while saving:" + e .getMessage ());}
183178 }
184179
185- private static void populate (HashMap <String , List <String >> map , JSONArray source ) {
180+ private static void populate (HashMap <String , HashMap < String , List <String > >> map , JSONArray source ) {
186181 for (JSONObject entry : (Iterable <JSONObject >) source ) {
187182 String id = (String ) entry .get ("id" );
188183
@@ -191,17 +186,35 @@ private static void populate(HashMap<String, List<String>> map, JSONArray source
191186 id = "minecraft:" + id ;
192187 }
193188
194- List <String > list = new ArrayList <>();
189+ HashMap < String , List <String >> propertyEntries = new HashMap <>();
195190 if (entry .containsKey ("properties" )) {
196- JSONArray props = (JSONArray ) entry .get ("properties" );
197- for (String property : (Iterable <String >) props ) {
198- list .add (property );
191+ Object ObjectProperties = entry .get ("properties" );
192+ JSONObject JSONProperties = (ObjectProperties instanceof JSONArray ) ? parseOldArrayToObject ((JSONArray ) entry .get ("properties" )) : (JSONObject ) entry .get ("properties" );
193+ for (Object propertyName : JSONProperties .keySet ()) {
194+ JSONArray JSONPropertyValues = (JSONArray ) JSONProperties .get (propertyName );
195+ List <String > propertyValues = new ArrayList <>();
196+ for (Object value : JSONPropertyValues ) {
197+ propertyValues .add (value .toString ());
198+ }
199+ propertyEntries .put ((String ) propertyName , propertyValues );
199200 }
200201 }
201- map .put (id , list );
202+ map .put (id , propertyEntries );
202203 }
203204 }
204205
206+ private static JSONObject parseOldArrayToObject (JSONArray oldArray ) {
207+ JSONObject jsonObject = new JSONObject ();
208+
209+ for (String propertyName : (Iterable <? extends String >) oldArray ) {
210+ JSONArray jsonArray = new JSONArray ();
211+ jsonArray .add ("all" );
212+ jsonObject .put (propertyName , jsonArray );
213+ }
214+
215+ return jsonObject ;
216+ }
217+
205218 public static boolean isBlockAllowed (Block block ) {
206219 // 1) check if block has been mentioned in BLOCKS part
207220 String blockName = Registries .BLOCK .getId (block ).toString ();
@@ -226,7 +239,7 @@ public static boolean isBlockAllowed(Block block) {
226239
227240 // 3) if block has properties in allowed properties config
228241 for (Property <?> property : block .getStateManager ().getProperties ()) {
229- if (properties . getOrDefault (property .getName (), false ))
242+ if (properties_allowed . containsKey (property .getName ()))
230243 return true ;
231244 }
232245
@@ -239,42 +252,101 @@ public static boolean isPropertyAllowed(String propertyName, @Nullable Block blo
239252 String blockName = Registries .BLOCK .getId (block ).toString ();
240253 // 1) Check for exactly this block
241254 if (blocks_forbidden .containsKey (blockName )) {
242- List <String > forbidden_props = blocks_forbidden .get (blockName );
243- if (forbidden_props .contains (propertyName )) return false ;
255+ HashMap <String , List <String >> forbidden_props = blocks_forbidden .get (blockName );
256+ if (forbidden_props .containsKey ("all" ) ||
257+ forbidden_props .getOrDefault (propertyName , new ArrayList <>()).contains ("all" )) return false ;
244258 }
245259 if (blocks_allowed .containsKey (blockName )) {
246- List <String > allowed_props = blocks_allowed .get (blockName );
247- if (allowed_props .contains (propertyName )) return true ;
260+ HashMap < String , List <String > > allowed_props = blocks_allowed .get (blockName );
261+ if (allowed_props .containsKey ( "all" ) || allowed_props . containsKey (propertyName )) return true ;
248262 if (allowed_props .isEmpty ()) {
249- if (properties .containsKey (propertyName )) return properties .get (propertyName );
250- return true ;
263+ return !properties_forbidden .getOrDefault (propertyName , new ArrayList <>()).contains ("all" );
251264 }
252265 }
253266 // 2) Either block is not stated in config or
254- // allowed by itself, but does not speak about property. Check its tags
267+ // allowed by itself, but does not speak about this property. Check its tags
255268 Stream <TagKey <Block >> tagStream = block .getRegistryEntry ().streamTags ();
256269 List <TagKey <Block >> tagArray = tagStream .toList ();
257270 for (TagKey <Block > tag : tagArray ) {
258271 String tagName = tag .id ().toString ();
259272 if (tags_forbidden .containsKey (tagName )) {
260- List <String > forbidden_props = tags_forbidden .get (tagName );
261- if (forbidden_props .contains (propertyName )) return false ;
273+ HashMap <String , List <String >> forbidden_props = tags_forbidden .get (tagName );
274+ if (forbidden_props .containsKey ("all" ) ||
275+ forbidden_props .getOrDefault (propertyName , new ArrayList <>()).contains ("all" )) return false ;
262276 }
263277 }
264278 for (TagKey <Block > tag : tagArray ) {
265279 String tagName = tag .id ().toString ();
266280 if (tags_allowed .containsKey (tagName )) {
267- List <String > allowed_props = tags_allowed .get (tagName );
268- if (allowed_props .contains (propertyName )) return true ;
281+ HashMap < String , List <String > > allowed_props = tags_allowed .get (tagName );
282+ if (allowed_props .containsKey ( "all" ) || allowed_props . containsKey (propertyName )) return true ;
269283 if (allowed_props .isEmpty ()) {
270- if (properties .containsKey (propertyName )) return properties .get (propertyName );
271- return true ;
284+ return !properties_forbidden .getOrDefault (propertyName , new ArrayList <>()).contains ("all" );
272285 }
273286 }
274287 }
275288 }
276289 // 3) If tags do not speak about property, check global list
277- if (properties .containsKey (propertyName )) return properties .get (propertyName );
290+ if (properties_allowed .containsKey ("all" ) || properties_allowed .containsKey (propertyName )) return true ;
291+ if (properties_forbidden .containsKey (propertyName )) return !properties_forbidden .get (propertyName ).contains ("all" );
292+ // 4) if property was not clarified in config, check whitelist mode
293+ return !whitelist ;
294+ }
295+
296+ public static boolean isPropertyValueAllowed (Block block , String propertyName , String value ) {
297+ String blockName = Registries .BLOCK .getId (block ).toString ();
298+ // 1) Check for exactly this block
299+ if (blocks_forbidden .containsKey (blockName )) {
300+ HashMap <String , List <String >> forbidden_props = blocks_forbidden .get (blockName );
301+ if (forbidden_props .containsKey ("all" )) return false ;
302+ List <String > values = forbidden_props .getOrDefault (propertyName , new ArrayList <>());
303+ if (values .contains ("all" ) || values .contains (value )) return false ;
304+ }
305+ if (blocks_allowed .containsKey (blockName )) {
306+ HashMap <String , List <String >> allowed_props = blocks_allowed .get (blockName );
307+ if (allowed_props .containsKey ("all" )) return true ;
308+ List <String > values = allowed_props .getOrDefault (propertyName , new ArrayList <>());
309+ if (values .contains ("all" ) || values .contains (value )) return true ;
310+ if (allowed_props .isEmpty ()) {
311+ return !properties_forbidden .getOrDefault (propertyName , new ArrayList <>()).contains ("all" );
312+ }
313+ }
314+ // 2) Either block is not stated in config or
315+ // allowed by itself, but does not speak about this property. Check its tags
316+ Stream <TagKey <Block >> tagStream = block .getRegistryEntry ().streamTags ();
317+ List <TagKey <Block >> tagArray = tagStream .toList ();
318+ for (TagKey <Block > tag : tagArray ) {
319+ String tagName = tag .id ().toString ();
320+ if (tags_forbidden .containsKey (tagName )) {
321+ HashMap <String , List <String >> forbidden_props = tags_forbidden .get (tagName );
322+ if (forbidden_props .containsKey ("all" )) return false ;
323+ List <String > values = forbidden_props .getOrDefault (propertyName , new ArrayList <>());
324+ if (values .contains ("all" ) || values .contains (value )) return false ;
325+ }
326+ }
327+ for (TagKey <Block > tag : tagArray ) {
328+ String tagName = tag .id ().toString ();
329+ if (tags_allowed .containsKey (tagName )) {
330+ HashMap <String , List <String >> allowed_props = tags_allowed .get (tagName );
331+ if (allowed_props .containsKey ("all" )) return true ;
332+ List <String > values = allowed_props .getOrDefault (propertyName , new ArrayList <>());
333+ if (values .contains ("all" ) || values .contains (value )) return true ;
334+ if (allowed_props .isEmpty ()) {
335+ return !properties_forbidden .getOrDefault (propertyName , new ArrayList <>()).contains ("all" );
336+ }
337+ }
338+ }
339+ // 3) If tags do not speak about property, check global list
340+ if (properties_allowed .containsKey ("all" )) return true ;
341+ if (properties_allowed .containsKey (propertyName )) {
342+ List <String > values = properties_allowed .get (propertyName );
343+ if (values .contains (value )) return true ;
344+ }
345+ if (properties_forbidden .containsKey ("all" )) return false ;
346+ if (properties_forbidden .containsKey (propertyName )) {
347+ List <String > values = properties_forbidden .get (propertyName );
348+ if (values .contains (value )) return false ;
349+ }
278350 // 4) if property was not clarified in config, check whitelist mode
279351 return !whitelist ;
280352 }
0 commit comments