Ensure previously-discovered traits properly update server-side when the registered values change for an item

This commit is contained in:
Sollace 2022-01-11 12:23:20 +02:00
parent 23211ba7b1
commit 0a93682662
2 changed files with 24 additions and 13 deletions

View file

@ -6,10 +6,13 @@ import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.minelittlepony.common.client.gui.Tooltip; import com.minelittlepony.common.client.gui.Tooltip;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtList;
import net.minecraft.text.LiteralText; import net.minecraft.text.LiteralText;
import net.minecraft.text.TranslatableText; import net.minecraft.text.TranslatableText;
import net.minecraft.util.Formatting; import net.minecraft.util.Formatting;
@ -102,6 +105,13 @@ public enum Trait {
return IDS.values(); return IDS.values();
} }
public static Stream<Trait> fromNbt(NbtList nbt) {
return nbt.stream()
.map(NbtElement::asString)
.map(Trait::fromId)
.flatMap(Optional::stream);
}
public static Optional<Trait> fromId(Identifier id) { public static Optional<Trait> fromId(Identifier id) {
return Optional.ofNullable(IDS.getOrDefault(id, null)); return Optional.ofNullable(IDS.getOrDefault(id, null));
} }

View file

@ -122,23 +122,24 @@ public class TraitDiscovery implements NbtSerialisable {
NbtCompound disco = compound.getCompound("items"); NbtCompound disco = compound.getCompound("items");
disco.getKeys().forEach(key -> { disco.getKeys().forEach(key -> {
Optional.ofNullable(Identifier.tryParse(key)).ifPresent(id -> { Optional.ofNullable(Identifier.tryParse(key)).ifPresent(id -> {
SpellTraits.fromNbt(disco.getCompound(key)).ifPresent(val -> { loadTraits(id, disco.getCompound(key)).filter(SpellTraits::isPresent).ifPresent(val -> {
items.put(id, val); items.put(id, val);
}); });
}); });
}); });
compound.getList("traits", NbtElement.STRING_TYPE).stream() Trait.fromNbt(compound.getList("traits", NbtElement.STRING_TYPE)).forEach(traits::add);
.map(NbtElement::asString) Trait.fromNbt(compound.getList("unreadTraits", NbtElement.STRING_TYPE)).forEach(unreadTraits::add);
.map(Trait::fromId) }
.filter(Optional::isPresent)
.map(Optional::get) private Optional<SpellTraits> loadTraits(Identifier itemId, NbtCompound nbt) {
.forEach(this.traits::add); if (!pony.isClient()) {
compound.getList("unreadTraits", NbtElement.STRING_TYPE).stream() return Registry.ITEM.getOrEmpty(itemId)
.map(NbtElement::asString) .flatMap(item -> Optional.of(SpellTraits.of(item)))
.map(Trait::fromId) .filter(SpellTraits::isPresent)
.filter(Optional::isPresent) .or(() -> SpellTraits.fromNbt(nbt));
.map(Optional::get) }
.forEach(this.unreadTraits::add);
return SpellTraits.fromNbt(nbt);
} }
public void copyFrom(TraitDiscovery old) { public void copyFrom(TraitDiscovery old) {