0
0
mirror of https://github.com/Wurst-Imperium/Wurst7.git synced 2024-09-20 01:12:13 +02:00

Auto-update BookOffersSettings if left at the default values

This commit is contained in:
Alexander01998 2024-05-06 18:34:14 +02:00
parent 23924de10a
commit e0641a7827

View File

@ -18,6 +18,7 @@ import java.util.Set;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import net.minecraft.registry.Registries;
import net.minecraft.util.Identifier;
@ -28,7 +29,6 @@ import net.wurstclient.hacks.autolibrarian.BookOffer;
import net.wurstclient.keybinds.PossibleKeybind;
import net.wurstclient.util.json.JsonException;
import net.wurstclient.util.json.JsonUtils;
import net.wurstclient.util.json.WsonArray;
import net.wurstclient.util.json.WsonObject;
public final class BookOffersSetting extends Setting
@ -148,12 +148,20 @@ public final class BookOffersSetting extends Setting
{
try
{
WsonArray wson = JsonUtils.getAsArray(json);
offers.clear();
wson.getAllObjects().parallelStream().map(this::loadOffer)
.filter(Objects::nonNull).filter(BookOffer::isValid).distinct()
.sorted().forEachOrdered(offers::add);
// if string "default", load default offers
if(JsonUtils.getAsString(json, "nope").equals("default"))
{
offers.addAll(Arrays.asList(defaultOffers));
return;
}
// otherwise, load the offers in the JSON array
JsonUtils.getAsArray(json).getAllObjects().parallelStream()
.map(this::loadOffer).filter(Objects::nonNull)
.filter(BookOffer::isValid).distinct().sorted()
.forEachOrdered(offers::add);
}catch(JsonException e)
{
@ -183,6 +191,10 @@ public final class BookOffersSetting extends Setting
@Override
public JsonElement toJson()
{
// if offers is the same as defaultOffers, save string "default"
if(offers.equals(Arrays.asList(defaultOffers)))
return new JsonPrimitive("default");
JsonArray json = new JsonArray();
offers.forEach(offer -> {