1414import dev .ftb .mods .ftbmaterials .resources .ResourceType ;
1515import dev .ftb .mods .ftbmaterials .util .CachedTagKeyLookup ;
1616import net .minecraft .core .Holder ;
17+ import net .minecraft .core .Registry ;
1718import net .minecraft .core .registries .BuiltInRegistries ;
1819import net .minecraft .core .registries .Registries ;
1920import net .minecraft .resources .ResourceLocation ;
2021import net .minecraft .tags .TagKey ;
2122import net .minecraft .world .item .Item ;
2223import net .minecraft .world .level .block .Block ;
2324import net .minecraft .world .level .block .state .BlockState ;
25+ import net .minecraft .world .level .material .Fluid ;
2426import net .neoforged .neoforge .common .util .Lazy ;
2527
2628import java .io .IOException ;
3234import java .util .stream .Collectors ;
3335
3436public class UnifierDB {
37+ private static final Codec <Map <String , String >> MUTABLE_STRING_MAP = Codec .unboundedMap (Codec .STRING , Codec .STRING )
38+ .xmap ((Function <Map <String , String >, Map <String , String >>) ConcurrentHashMap ::new , Function .identity ());
39+
3540 public static final Codec <UnifierDB > CODEC = RecordCodecBuilder .create (builder -> builder .group (
36- Codec .unboundedMap (Codec .STRING , Codec .STRING )
37- .fieldOf ("items" ).forGetter (db -> db .itemMap ),
38- Codec .unboundedMap (Codec .STRING , Codec .STRING )
39- .fieldOf ("item_tags" ).forGetter (db -> db .itemTagMap ),
40- Codec .unboundedMap (Codec .STRING , Codec .STRING )
41- .xmap ((Function <Map <String , String >, Map <String , String >>) ConcurrentHashMap ::new , Map ::copyOf )
42- .fieldOf ("ore_blocks" ).forGetter (db -> db .blockMap )
41+ MUTABLE_STRING_MAP .optionalFieldOf ("items" , new ConcurrentHashMap <>()).forGetter (db -> db .itemMap ),
42+ MUTABLE_STRING_MAP .optionalFieldOf ("item_tags" , new ConcurrentHashMap <>()).forGetter (db -> db .itemTagMap ),
43+ MUTABLE_STRING_MAP .optionalFieldOf ("fluids" , new ConcurrentHashMap <>()).forGetter (db -> db .fluidMap ),
44+ MUTABLE_STRING_MAP .optionalFieldOf ("fluid_tags" , new ConcurrentHashMap <>()).forGetter (db -> db .fluidTagMap ),
45+ MUTABLE_STRING_MAP .optionalFieldOf ("ore_blocks" , new ConcurrentHashMap <>()).forGetter (db -> db .blockMap )
4346 ).apply (builder , UnifierDB ::new ));
4447
45- public static final UnifierDB EMPTY = new UnifierDB (Map .of (), Map .of (), Map .of ());
48+ public static final UnifierDB EMPTY = new UnifierDB (Map .of (), Map .of (), Map .of (), Map . of (), Map . of () );
4649
4750 private final Map <String ,String > itemMap ;
4851 private final Map <String ,String > itemTagMap ;
49- private final Lazy < Map <Item , Item >> itemByItemMap = Lazy . of ( this :: buildItemByItemMap ) ;
50-
52+ private final Map <String , String > fluidMap ;
53+ private final Map < String , String > fluidTagMap ;
5154 private final Map <String ,String > blockMap ;
55+
56+ private final Lazy <Map <Item ,Item >> itemByItemMap = Lazy .of (this ::buildItemByItemMap );
57+ private final Lazy <Map <Fluid ,Fluid >> fluidByFluidMap = Lazy .of (this ::buildFluidByFluidMap );
5258 private final Lazy <Map <Block ,Block >> blockByBlockMap = Lazy .of (this ::buildBlockByBlockMap );
5359
5460 private UnifierDB () {
55- this (new HashMap <>(), new HashMap <>(), new HashMap <>());
61+ this (new HashMap <>(), new HashMap <>(), new HashMap <>(), new HashMap <>(), new HashMap <>() );
5662 }
5763
58- private UnifierDB (Map <String , String > itemMap , Map <String , String > itemTagMap , Map <String , String > blockMap ) {
64+ private UnifierDB (Map <String , String > itemMap , Map <String , String > itemTagMap , Map <String , String > fluidMap , Map < String , String > fluidTagMap , Map < String , String > blockMap ) {
5965 this .itemMap = itemMap ;
6066 this .itemTagMap = itemTagMap ;
67+ this .fluidMap = fluidMap ;
68+ this .fluidTagMap = fluidTagMap ;
6169 this .blockMap = blockMap ;
6270 }
6371
6472 public static UnifierDB load (Path path ) throws IOException {
6573 JsonElement json = JsonParser .parseString (Files .readString (path ));
66- return CODEC .parse (JsonOps .INSTANCE , json ).getOrThrow ();
74+ UnifierDB unifierDB = CODEC .parse (JsonOps .INSTANCE , json ).getOrThrow ();
75+ UnifierManager .loadExtraJsonFiles (UnifierManager .UNIFIER_DIR , el ->
76+ unifierDB .addExtraUnifierEntries (CODEC .parse (JsonOps .INSTANCE , el ).getOrThrow ()));
77+ return unifierDB ;
78+ }
79+
80+ private void addExtraUnifierEntries (UnifierDB extra ) {
81+ itemMap .putAll (extra .itemMap );
82+ itemTagMap .putAll (extra .itemTagMap );
83+ fluidMap .putAll (extra .fluidMap );
84+ fluidTagMap .putAll (extra .fluidTagMap );
85+ blockMap .putAll (extra .blockMap );
6786 }
6887
6988 public static UnifierDB build () {
@@ -94,6 +113,18 @@ public Optional<String> lookupItemTag(String key) {
94113 return Optional .ofNullable (itemTagMap .get (key ));
95114 }
96115
116+ public Optional <String > lookupFluid (String key ) {
117+ return Optional .ofNullable (fluidMap .get (key ));
118+ }
119+
120+ public Optional <Fluid > lookupFluid (Fluid fluid ) {
121+ return Optional .ofNullable (fluidByFluidMap .get ().get (fluid ));
122+ }
123+
124+ public Optional <String > lookupFluidTag (String key ) {
125+ return Optional .ofNullable (fluidTagMap .get (key ));
126+ }
127+
97128 public Optional <Block > lookupBlock (Block block ) {
98129 return Optional .ofNullable (blockByBlockMap .get ().get (block ));
99130 }
@@ -125,7 +156,7 @@ private void buildItemTags(Set<TagKey<Item>> itemTags) {
125156 Item item = ftbMaterialsItem == null ? vanillaFallbackItem : ftbMaterialsItem ;
126157 if (item != null ) {
127158 otherItems .forEach (other -> addItemMapping (other , item ));
128- addTagMapping (tag , item );
159+ addItemTagMapping (tag , item );
129160 }
130161 }
131162 // special cases for silicon & sawdust
@@ -135,7 +166,7 @@ private void buildItemTags(Set<TagKey<Item>> itemTags) {
135166
136167 private void specialCase (Resource resource , ResourceType resourceType , TagKey <Item > tag ) {
137168 ResourceRegistries .get (resource ).getItemFromType (resourceType ).ifPresent (itemHolder ->
138- addTagMapping (tag , itemHolder .get ()));
169+ addItemTagMapping (tag , itemHolder .get ()));
139170
140171 BuiltInRegistries .ITEM .getTag (tag ).ifPresent (items -> {
141172 Item [] ftbItem = new Item [] { null };
@@ -189,7 +220,7 @@ public void addItemMapping(Item from, Item to) {
189220 itemMap .put (BuiltInRegistries .ITEM .getKey (from ).toString (), BuiltInRegistries .ITEM .getKey (to ).toString ());
190221 }
191222
192- public void addTagMapping (TagKey <Item > from , Item to ) {
223+ public void addItemTagMapping (TagKey <Item > from , Item to ) {
193224 itemTagMap .put (from .location ().toString (), BuiltInRegistries .ITEM .getKey (to ).toString ());
194225 }
195226
@@ -226,22 +257,23 @@ private static <T> Set<TagKey<T>> collectTags(Resource type, ResourceType resour
226257 }
227258
228259 private Map <Item , Item > buildItemByItemMap () {
229- Map <Item , Item > res = new HashMap <>();
230- itemMap .forEach ((in , out ) ->
231- BuiltInRegistries .ITEM .getOptional (ResourceLocation .parse (in )).ifPresent (itemIn ->
232- BuiltInRegistries .ITEM .getOptional (ResourceLocation .parse (out )).ifPresent (itemOut ->
233- res .put (itemIn , itemOut )
234- )
235- ));
236- return res ;
260+ return buildXbyXMap (itemMap , BuiltInRegistries .ITEM );
261+ }
262+
263+ private Map <Fluid , Fluid > buildFluidByFluidMap () {
264+ return buildXbyXMap (fluidMap , BuiltInRegistries .FLUID );
237265 }
238266
239267 private Map <Block , Block > buildBlockByBlockMap () {
240- Map <Block , Block > res = new ConcurrentHashMap <>();
241- blockMap .forEach ((in , out ) ->
242- BuiltInRegistries .BLOCK .getOptional (ResourceLocation .parse (in )).ifPresent (blockIn ->
243- BuiltInRegistries .BLOCK .getOptional (ResourceLocation .parse (out )).ifPresent (blockOut ->
244- res .put (blockIn , blockOut )
268+ return buildXbyXMap (blockMap , BuiltInRegistries .BLOCK );
269+ }
270+
271+ private static <T > Map <T , T > buildXbyXMap (Map <String ,String > unifierMap , Registry <T > registry ) {
272+ Map <T , T > res = new HashMap <>();
273+ unifierMap .forEach ((in , out ) ->
274+ registry .getOptional (ResourceLocation .parse (in )).ifPresent (objIn ->
275+ registry .getOptional (ResourceLocation .parse (out )).ifPresent (objOut ->
276+ res .put (objIn , objOut )
245277 )
246278 ));
247279 return res ;
0 commit comments