11package turing .tmb ;
22
3+ import com .mojang .nbt .NbtIo ;
4+ import com .mojang .nbt .tags .CompoundTag ;
5+ import com .mojang .nbt .tags .Tag ;
36import net .fabricmc .api .ModInitializer ;
47import net .fabricmc .loader .api .FabricLoader ;
58import net .minecraft .client .Minecraft ;
1417import org .slf4j .LoggerFactory ;
1518import turing .tmb .api .ITMBPlugin ;
1619import turing .tmb .api .TMBEntrypoint ;
20+ import turing .tmb .api .ingredient .ITypedIngredient ;
21+ import turing .tmb .api .recipe .IRecipeCategory ;
22+ import turing .tmb .api .recipe .IRecipeTranslator ;
23+ import turing .tmb .api .recipe .RecipeIngredientRole ;
1724import turing .tmb .api .runtime .ITMBRuntime ;
1825import turing .tmb .plugin .BTATweaker ;
1926import turing .tmb .util .IKeybinds ;
2027import turing .tmb .vanilla .VanillaPlugin ;
2128import turniplabs .halplibe .util .ClientStartEntrypoint ;
2229
30+ import java .io .File ;
31+ import java .io .IOException ;
32+ import java .nio .file .Files ;
2333import java .util .ArrayList ;
2434import java .util .List ;
35+ import java .util .Map ;
36+ import java .util .Optional ;
2537
2638public class TMB implements ModInitializer , ClientStartEntrypoint , TMBEntrypoint {
2739 public static final String MOD_ID = "tmb" ;
@@ -71,7 +83,7 @@ public void beforeClientStart() {
7183
7284 @ Override
7385 public void afterClientStart () {
74-
86+ loadData ();
7587 }
7688
7789 private static void loadTMB () {
@@ -88,9 +100,111 @@ private static void loadTMB() {
88100 plugin .registerRecipeCatalysts (runtime );
89101 plugin .registerRecipes (runtime );
90102 }
103+ runtime .isReady = true ;
91104 long timeTook = System .currentTimeMillis () - time ;
92105 LOGGER .info ("TMB loaded in {}ms!" , timeTook );
93- runtime .isReady = true ;
106+ }
107+
108+ public static void saveData () {
109+ File dataFolder = new File (Minecraft .getMinecraft ().getMinecraftDir ()+"/config/tmb/" );
110+ dataFolder .mkdirs ();
111+
112+ File dataFile = new File (Minecraft .getMinecraft ().getMinecraftDir ()+"/config/tmb/" , "data.dat" );
113+
114+ CompoundTag dataTag = new CompoundTag ();
115+
116+ CompoundTag favourites = new CompoundTag ();
117+ List <ITypedIngredient <?>> iTypedIngredients = TMB .getRuntime ().getFavourites ();
118+ for (int j = 0 ; j < iTypedIngredients .size (); j ++) {
119+ ITypedIngredient <?> favourite = iTypedIngredients .get (j );
120+ CompoundTag favouriteTag = new CompoundTag ();
121+ favouriteTag .putString ("namespace" , favourite .getNamespace ());
122+ favouriteTag .putString ("uid" , favourite .getUid ());
123+ favourites .put (String .valueOf (j ), favouriteTag );
124+ }
125+ dataTag .putCompound ("Favourites" , favourites );
126+
127+ CompoundTag defaultRecipes = new CompoundTag ();
128+ int i = 0 ;
129+ for (Map .Entry <RecipeIngredient , IRecipeTranslator <?>> entry : TMB .getRuntime ().getDefaultRecipes ().entrySet ()) {
130+ RecipeIngredient key = entry .getKey ();
131+ IRecipeTranslator <?> value = entry .getValue ();
132+ CompoundTag defaultRecipeTag = new CompoundTag ();
133+ CompoundTag ingredientTag = new CompoundTag ();
134+ CompoundTag categoryTag = new CompoundTag ();
135+
136+ defaultRecipeTag .putString ("recipe" , value .getOriginal ().toString ());
137+ ingredientTag .putString ("namespace" , key .ingredient .getNamespace ());
138+ ingredientTag .putString ("uid" , key .ingredient .getUid ());
139+ defaultRecipeTag .put ("ingredient" , ingredientTag );
140+ categoryTag .putString ("namespace" , key .category .getNamespace ());
141+ categoryTag .putString ("name" , key .category .getName ());
142+ defaultRecipeTag .put ("category" , categoryTag );
143+ defaultRecipes .put (String .valueOf (i ), defaultRecipeTag );
144+ i ++;
145+ }
146+ dataTag .putCompound ("DefaultRecipes" , defaultRecipes );
147+
148+ try {
149+ if (!dataFile .exists ()) {
150+ if (!dataFile .createNewFile ()) {
151+ return ;
152+ }
153+ }
154+ NbtIo .writeCompressed (dataTag , Files .newOutputStream (dataFile .toPath ()));
155+ } catch (IOException e ) {
156+ throw new RuntimeException (e );
157+ }
158+ }
159+
160+ public static void loadData () {
161+
162+ File dataFolder = new File (Minecraft .getMinecraft ().getMinecraftDir ()+"/config/tmb/" );
163+ dataFolder .mkdirs ();
164+
165+ File dataFile = new File (Minecraft .getMinecraft ().getMinecraftDir ()+"/config/tmb/" , "data.dat" );
166+ try {
167+ if (!dataFile .exists ()) {
168+ return ;
169+ }
170+ CompoundTag tag = NbtIo .readCompressed (Files .newInputStream (dataFile .toPath ()));
171+
172+ TMB .getRuntime ().getFavourites ().clear ();
173+ for (Tag <?> compoundTag : tag .getCompound ("Favourites" ).getValues ()) {
174+ CompoundTag favouriteTag = (CompoundTag ) compoundTag ;
175+ TMB .getRuntime ().getIngredientIndex ()
176+ .getIngredient (favouriteTag .getString ("namespace" ), favouriteTag .getString ("uid" ))
177+ .ifPresent (TMB .getRuntime ().getFavourites ()::add );
178+ }
179+
180+ for (Tag <?> compoundTag : tag .getCompound ("DefaultRecipes" ).getValues ()) {
181+ CompoundTag defaultRecipeTag = (CompoundTag ) compoundTag ;
182+ CompoundTag ingredientTag = defaultRecipeTag .getCompound ("ingredient" );
183+ CompoundTag categoryTag = defaultRecipeTag .getCompound ("category" );
184+ String recipeId = defaultRecipeTag .getString ("recipe" );
185+
186+ Optional <IRecipeCategory <?>> category = TMB .getRuntime ().getRecipeIndex ().getAllCategories ().stream ()
187+ .filter (it ->
188+ it .getName ().equals (categoryTag .getString ("name" ))
189+ && it .getNamespace ().equals (categoryTag .getString ("namespace" ))).findFirst ();
190+
191+ Optional <IRecipeTranslator <?>> recipe = category
192+ .flatMap (it -> TMB .getRuntime ().getRecipeIndex ().getRecipeLists ().get (it ).stream ()
193+ .filter (it2 -> it2 .getOriginal ().toString ().equals (recipeId )).findFirst ());
194+
195+ Optional <ITypedIngredient <Object >> ingredient = recipe
196+ .flatMap (it -> TMB .getRuntime ().getIngredientIndex ()
197+ .getIngredient (ingredientTag .getString ("namespace" ), ingredientTag .getString ("uid" )));
198+
199+ ingredient
200+ .ifPresent (it -> {
201+ RecipeIngredient recipeIngredient = new RecipeIngredient (it , recipe .get (), category .get (), RecipeIngredientRole .OUTPUT );
202+ TMB .getRuntime ().getDefaultRecipes ().put (recipeIngredient , recipe .get ());
203+ });
204+ }
205+ } catch (IOException e ) {
206+ throw new RuntimeException (e );
207+ }
94208 }
95209
96210 private static void clear () {
0 commit comments