Skip to content

Commit 4eecb9b

Browse files
committed
Changed the way data is saved.
1 parent ccefef1 commit 4eecb9b

4 files changed

Lines changed: 137 additions & 101 deletions

File tree

src/main/java/turing/tmb/TMB.java

Lines changed: 116 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package turing.tmb;
22

3+
import com.mojang.nbt.NbtIo;
4+
import com.mojang.nbt.tags.CompoundTag;
5+
import com.mojang.nbt.tags.Tag;
36
import net.fabricmc.api.ModInitializer;
47
import net.fabricmc.loader.api.FabricLoader;
58
import net.minecraft.client.Minecraft;
@@ -14,14 +17,23 @@
1417
import org.slf4j.LoggerFactory;
1518
import turing.tmb.api.ITMBPlugin;
1619
import 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;
1724
import turing.tmb.api.runtime.ITMBRuntime;
1825
import turing.tmb.plugin.BTATweaker;
1926
import turing.tmb.util.IKeybinds;
2027
import turing.tmb.vanilla.VanillaPlugin;
2128
import turniplabs.halplibe.util.ClientStartEntrypoint;
2229

30+
import java.io.File;
31+
import java.io.IOException;
32+
import java.nio.file.Files;
2333
import java.util.ArrayList;
2434
import java.util.List;
35+
import java.util.Map;
36+
import java.util.Optional;
2537

2638
public 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() {

src/main/java/turing/tmb/mixin/LevelDataMixin.java

Lines changed: 0 additions & 97 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package turing.tmb.mixin.client;
2+
3+
import net.minecraft.client.gui.ButtonElement;
4+
import net.minecraft.client.gui.ScreenPause;
5+
import org.spongepowered.asm.mixin.Mixin;
6+
import org.spongepowered.asm.mixin.injection.At;
7+
import org.spongepowered.asm.mixin.injection.Inject;
8+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
9+
import turing.tmb.TMB;
10+
11+
@Mixin(value = ScreenPause.class,remap = false)
12+
public class ScreenPauseMixin {
13+
14+
@Inject(method = "buttonClicked", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/Minecraft;changeWorld(Lnet/minecraft/client/world/WorldClient;)V"))
15+
protected void buttonClicked(ButtonElement button, CallbackInfo ci) {
16+
TMB.saveData();
17+
}
18+
19+
}

src/main/resources/tmb.mixins.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"package": "turing.tmb.mixin",
55
"compatibilityLevel": "JAVA_8",
66
"mixins": [
7-
"LevelDataMixin"
87
],
98
"client": [
109
"client.GameSettingsMixin",
@@ -14,7 +13,8 @@
1413
"client.ScreenContainerAbstractMixin",
1514
"client.ScreenInventoryCreativeAccessor",
1615
"client.ScreenInventoryCreativeMixin",
17-
"client.ScreenMixin"
16+
"client.ScreenMixin",
17+
"client.ScreenPauseMixin"
1818
],
1919
"injectors": {
2020
"defaultRequire": 1

0 commit comments

Comments
 (0)