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:
parent
23924de10a
commit
e0641a7827
@ -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 -> {
|
||||
|
Loading…
Reference in New Issue
Block a user