66import com .circulation .only_one_item .handler .MatchFluidHandler ;
77import com .circulation .only_one_item .handler .MatchItemHandler ;
88import com .circulation .only_one_item .util .BlackMatchItem ;
9+ import com .circulation .only_one_item .util .MatchItem ;
910import com .google .gson .GsonBuilder ;
1011import com .google .gson .reflect .TypeToken ;
1112import it .unimi .dsi .fastutil .objects .ObjectArrayList ;
1213import it .unimi .dsi .fastutil .objects .ObjectOpenHashSet ;
14+ import net .minecraft .item .Item ;
15+ import net .minecraft .util .ResourceLocation ;
1316import net .minecraftforge .fluids .FluidRegistry ;
1417import net .minecraftforge .fml .common .Loader ;
1518
@@ -26,19 +29,23 @@ public class OOIConfig {
2629 public static final List <FluidConversionTarget > fluids = new ObjectArrayList <>();
2730 public static final Set <BlackMatchItem > blackList = new ObjectOpenHashSet <>();
2831
29- public static void readConfig () throws IOException {
32+ public static void readConfig () {
3033 var configPath = Loader .instance ().getConfigDir ().toPath ().resolve ("ooi" );
3134 var ooiPath = configPath .resolve ("ooi_item.json" );
3235 var blackPath = configPath .resolve ("ooi_item_black_list.json" );
3336 var ooiFluidPath = configPath .resolve ("ooi_fluid.json" );
3437
35- Files .createDirectories (configPath );
38+ try {
39+ Files .createDirectories (configPath );
40+ } catch (IOException e ) {
41+ throw new RuntimeException (e );
42+ }
3643
3744 var config = new GsonBuilder ().disableHtmlEscaping ().setPrettyPrinting ().create ();
3845
3946 if (Files .exists (ooiPath )) {
4047 try {
41- items . addAll (config .fromJson (new String (Files .readAllBytes (ooiPath ), StandardCharsets .UTF_8 ), (new TypeToken <Set <ItemConversionTarget >>() {
48+ addItemTargets (config .fromJson (new String (Files .readAllBytes (ooiPath ), StandardCharsets .UTF_8 ), (new TypeToken <Set <ItemConversionTarget >>() {
4249 }).getType ()));
4350 } catch (Exception ignored ) {
4451 OnlyOneItem .LOGGER .error ("[OOI]The config/ooi/ooi_item.json file is incorrect!" );
@@ -50,24 +57,30 @@ public static void readConfig() throws IOException {
5057 if (inputStream != null ) {
5158 Files .copy (inputStream , ooiPath );
5259 }
60+ } catch (IOException e ) {
61+ throw new RuntimeException (e );
5362 }
5463 }
5564
5665 if (Files .exists (ooiFluidPath )) {
5766 try {
58- fluids . addAll (config .fromJson (new String (Files .readAllBytes (ooiFluidPath ), StandardCharsets .UTF_8 ), (new TypeToken <Set <FluidConversionTarget >>() {
67+ addFluidTargets (config .fromJson (new String (Files .readAllBytes (ooiFluidPath ), StandardCharsets .UTF_8 ), (new TypeToken <Set <FluidConversionTarget >>() {
5968 }).getType ()));
6069 } catch (Exception ignored ) {
6170 OnlyOneItem .LOGGER .error ("[OOI]The config/ooi/ooi_fluid.json file is incorrect!" );
6271 }
6372 } else {
6473 fluids .add (new FluidConversionTarget (FluidRegistry .WATER .getName ()).addMatchFluid (FluidRegistry .WATER .getName ()));
65- Files .write (ooiFluidPath , config .toJson (fluids ).getBytes (StandardCharsets .UTF_8 ));
74+ try {
75+ Files .write (ooiFluidPath , config .toJson (fluids ).getBytes (StandardCharsets .UTF_8 ));
76+ } catch (IOException e ) {
77+ throw new RuntimeException (e );
78+ }
6679 }
6780
6881 if (Files .exists (blackPath )) {
6982 try {
70- blackList . addAll (config .fromJson (new String (Files .readAllBytes (blackPath ), StandardCharsets .UTF_8 ), (new TypeToken <Set <BlackMatchItem >>() {
83+ addBlackList (config .fromJson (new String (Files .readAllBytes (blackPath ), StandardCharsets .UTF_8 ), (new TypeToken <Set <BlackMatchItem >>() {
7184 }).getType ()));
7285 } catch (Exception ignored ) {
7386 OnlyOneItem .LOGGER .error ("[OOI]The config/ooi/ooi_item_black_list.json file is incorrect!" );
@@ -76,9 +89,91 @@ public static void readConfig() throws IOException {
7689 blackList .add (BlackMatchItem .getInstance ("minecraft:gold_ingot" , 0 ));
7790 blackList .add (BlackMatchItem .getInstance (Type .OreDict , "ingotGold" ));
7891 blackList .add (BlackMatchItem .getInstance (Type .ModID , "minecraft" ));
79- Files .write (blackPath , config .toJson (blackList ).getBytes (StandardCharsets .UTF_8 ));
92+ try {
93+ Files .write (blackPath , config .toJson (blackList ).getBytes (StandardCharsets .UTF_8 ));
94+ } catch (IOException e ) {
95+ throw new RuntimeException (e );
96+ }
8097 }
8198 MatchItemHandler .InitTarget ();
8299 MatchFluidHandler .Init (OOIConfig .fluids );
83100 }
84- }
101+
102+ private static void addItemTargets (Set <ItemConversionTarget > targets ) {
103+ if (targets == null ) return ;
104+ for (ItemConversionTarget target : targets ) {
105+ if (target == null || !isRegisteredItem (target .getTargetID ()) || target .getMatchItems () == null ) {
106+ continue ;
107+ }
108+ Set <MatchItem > validMatches = new ObjectOpenHashSet <>();
109+ for (MatchItem matchItem : target .getMatchItems ()) {
110+ if (isValidMatchItem (matchItem )) {
111+ validMatches .add (matchItem );
112+ }
113+ }
114+ if (!validMatches .isEmpty ()) {
115+ items .add (target .setMatchItem (validMatches ));
116+ }
117+ }
118+ }
119+
120+ private static void addFluidTargets (Set <FluidConversionTarget > targets ) {
121+ if (targets == null ) return ;
122+ for (FluidConversionTarget target : targets ) {
123+ if (target == null || target .getTarget () == null || target .getMatchFluids () == null ) {
124+ continue ;
125+ }
126+ Set <String > validMatches = new ObjectOpenHashSet <>();
127+ for (String fluid : target .getMatchFluids ()) {
128+ if (isNotBlank (fluid )) {
129+ validMatches .add (fluid );
130+ }
131+ }
132+ if (!validMatches .isEmpty ()) {
133+ fluids .add (target .setMatchFluids (validMatches ));
134+ }
135+ }
136+ }
137+
138+ private static void addBlackList (Set <BlackMatchItem > targets ) {
139+ if (targets == null ) return ;
140+ for (BlackMatchItem target : targets ) {
141+ if (isValidBlackList (target )) {
142+ blackList .add (target );
143+ }
144+ }
145+ }
146+
147+ private static boolean isValidMatchItem (MatchItem matchItem ) {
148+ if (matchItem == null ) return false ;
149+ if (isNotBlank (matchItem .oreName ())) return true ;
150+ return isValidResourceLocation (matchItem .id ());
151+ }
152+
153+ private static boolean isValidBlackList (BlackMatchItem target ) {
154+ if (target == null || target .type () == null || !isNotBlank (target .name ())) return false ;
155+ return switch (target .type ()) {
156+ case Item -> isValidResourceLocation (target .name ());
157+ case OreDict , ModID -> true ;
158+ };
159+ }
160+
161+ private static boolean isRegisteredItem (String id ) {
162+ if (!isValidResourceLocation (id )) return false ;
163+ return Item .getByNameOrId (id ) != null ;
164+ }
165+
166+ private static boolean isValidResourceLocation (String id ) {
167+ if (!isNotBlank (id )) return false ;
168+ try {
169+ new ResourceLocation (id );
170+ return true ;
171+ } catch (RuntimeException ignored ) {
172+ return false ;
173+ }
174+ }
175+
176+ private static boolean isNotBlank (String value ) {
177+ return value != null && !value .trim ().isEmpty ();
178+ }
179+ }
0 commit comments