Skip to content

Commit 8e0e484

Browse files
committed
Delete Recipe data or Villager data files, if they are determined to be empty.
1 parent d9499aa commit 8e0e484

File tree

4 files changed

+89
-2
lines changed

4 files changed

+89
-2
lines changed

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ author = masa
99
mod_file_name = itemscroller-fabric
1010

1111
# Current mod version
12-
mod_version = 0.30.2-sakura.2
12+
mod_version = 0.30.2-sakura.3
1313

1414
# Required malilib version
15-
malilib_version = 059239c8ef
15+
malilib_version = af945e9e49
1616

1717
# Minecraft, Fabric Loader and API and mappings versions
1818
minecraft_version_out = 1.21.11

src/main/java/fi/dy/masa/itemscroller/recipes/RecipeStorage.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,26 @@ public void clearRecipe(int index)
143143
this.dirty = true;
144144
}
145145

146+
private boolean isEmpty()
147+
{
148+
return !(this.recipes.length > 0) || this.isRecipesEmpty();
149+
}
150+
151+
private boolean isRecipesEmpty()
152+
{
153+
boolean empty = true;
154+
155+
for (RecipePattern recipe : this.recipes)
156+
{
157+
if (!recipe.isEmpty())
158+
{
159+
empty = false;
160+
}
161+
}
162+
163+
return empty;
164+
}
165+
146166
public void onAddToRecipeBook(RecipeDisplayEntry entry)
147167
{
148168
Minecraft mc = Minecraft.getInstance();
@@ -240,6 +260,11 @@ private CompoundData writeToNBT(@Nonnull RegistryAccess registry)
240260
ListData tagRecipes = new ListData();
241261
CompoundData data = new CompoundData();
242262

263+
if (this.isEmpty())
264+
{
265+
return data;
266+
}
267+
243268
for (int i = 0; i < this.recipes.length; i++)
244269
{
245270
if (this.recipes[i].isValid())
@@ -360,6 +385,19 @@ public void writeToDisk(@Nonnull RegistryAccess registry)
360385

361386
// NbtUtils.writeCompressed(this.writeToNBT(registry), fileTmp);
362387
CompoundData data = this.writeToNBT(registry);
388+
389+
// Don't save a file if there are no recipe's to save.
390+
if (data.isEmpty())
391+
{
392+
if (Files.exists(fileReal))
393+
{
394+
Files.delete(fileReal);
395+
}
396+
397+
this.dirty = false;
398+
return;
399+
}
400+
363401
DataFileUtils.writeCompoundDataToCompressedNbtFile(fileTmp, data);
364402

365403
if (Files.exists(fileReal))

src/main/java/fi/dy/masa/itemscroller/villager/VillagerData.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,20 @@ IntArrayList getFavorites()
5252
return this.favorites;
5353
}
5454

55+
protected boolean isEmpty()
56+
{
57+
return this.favorites.isEmpty();
58+
}
59+
5560
public CompoundData toNBT()
5661
{
5762
CompoundData data = new CompoundData();
5863

64+
if (this.isEmpty())
65+
{
66+
return data;
67+
}
68+
5969
data.putLong("UUIDM", this.uuid.getMostSignificantBits());
6070
data.putLong("UUIDL", this.uuid.getLeastSignificantBits());
6171
data.putInt("ListPosition", this.tradeListPosition);

src/main/java/fi/dy/masa/itemscroller/villager/VillagerDataStorage.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,38 @@ private void readFromNBT(CompoundData tags)
155155
}
156156
}
157157

158+
private boolean isEmpty()
159+
{
160+
return (this.data.isEmpty() || this.isVillagerDataEmpty()) && this.globalFavorites.isEmpty();
161+
}
162+
163+
private boolean isVillagerDataEmpty()
164+
{
165+
boolean empty = true;
166+
167+
for (VillagerData data : this.data.values())
168+
{
169+
if (!data.isEmpty())
170+
{
171+
empty = false;
172+
}
173+
}
174+
175+
return empty;
176+
}
177+
158178
private CompoundData writeToNBT()
159179
{
160180
CompoundData tags = new CompoundData();
161181
ListData favoriteListData = new ListData();
162182
ListData globalFavoriteData = new ListData();
163183

184+
if (this.isEmpty())
185+
{
186+
dirty = false;
187+
return tags;
188+
}
189+
164190
for (VillagerData data : this.data.values())
165191
{
166192
favoriteListData.add(data.toNBT());
@@ -258,6 +284,19 @@ public void writeToDisk()
258284

259285
// NbtUtils.writeCompressed(this.writeToNBT(), fileTmp);
260286
CompoundData data = this.writeToNBT();
287+
288+
// Don't save file if there are no entries.
289+
if (data.isEmpty())
290+
{
291+
if (Files.exists(fileReal))
292+
{
293+
Files.delete(fileReal);
294+
}
295+
296+
this.dirty = false;
297+
return;
298+
}
299+
261300
DataFileUtils.writeCompoundDataToCompressedNbtFile(fileTmp, data);
262301

263302
if (Files.exists(fileReal))

0 commit comments

Comments
 (0)