Fixed issues loading spellbook chapters

This commit is contained in:
Sollace 2024-10-10 20:54:38 +01:00
parent 6594e30d18
commit 667b6056f5
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB
12 changed files with 21 additions and 14 deletions

View file

@ -1,6 +1,7 @@
package com.minelittlepony.unicopia.client.gui.spellbook;
import java.util.*;
import com.minelittlepony.unicopia.client.gui.spellbook.SpellbookChapterList.*;
import com.minelittlepony.unicopia.client.gui.spellbook.element.DynamicContent;
import com.minelittlepony.unicopia.container.spellbook.SpellbookChapter;

View file

@ -91,7 +91,7 @@ public class DynamicContent implements Content {
rightPanel.init(screen, pageIndex + 1);
}
class Page implements Drawable {
public class Page implements Drawable {
private final Text title;
private final int level;
private final int color;
@ -197,5 +197,10 @@ public class DynamicContent implements Content {
matrices.pop();
}
@Override
public String toString() {
return "DynamicContents$Page[title=" + title + ",elements=" + elements + "]";
}
}
}

View file

@ -56,7 +56,7 @@ public interface PageElement extends Drawable {
byte t = b.readByte();
return switch (t) {
case 1 -> formatLine(capture(b.readIdentifier(), id -> Registries.ITEM.get(id).getDefaultStack().getName()), "item", count);
case 2 -> formatLine(Trait.PACKET_CODEC.decode(b)::getShortName, "trait", count);
case 2 -> formatLine(Trait.fromId(b.readIdentifier()).get()::getShortName, "trait", count);
case 3 -> Suppliers.ofInstance(TextCodecs.PACKET_CODEC.decode(b));
case 4 -> formatLine(SpellType.getKey(b.readIdentifier())::getName, "spell", count);
default -> throw new IllegalArgumentException("Unexpected value: " + t);

View file

@ -20,7 +20,7 @@ public record Chapter (
TabSide.CODEC.fieldOf("side").forGetter(Chapter::side),
Codec.INT.fieldOf("y_position").forGetter(Chapter::tabY),
Codec.INT.optionalFieldOf("color", 0).forGetter(Chapter::color),
Contents.CODEC.fieldOf("contents").forGetter(Chapter::contents)
Contents.CODEC.fieldOf("content").forGetter(Chapter::contents)
).apply(instance, Chapter::new));
record Contents(List<Page> pages) {
@ -35,10 +35,10 @@ public record Chapter (
List<ChapterPageElement> elements
) {
public static final Codec<Page> CODEC = RecordCodecBuilder.create(instance -> instance.group(
TextCodecs.CODEC.fieldOf("title").forGetter(Page::title),
Codec.INT.fieldOf("level").forGetter(Page::level),
TextColor.CODEC.fieldOf("color").xmap(TextColor::getRgb, TextColor::fromRgb).forGetter(Page::level),
ChapterPageElement.CODEC.listOf().fieldOf("elements").forGetter(Page::elements)
TextCodecs.CODEC.optionalFieldOf("title", Text.empty()).forGetter(Page::title),
Codec.INT.optionalFieldOf("level", 0).forGetter(Page::level),
TextColor.CODEC.xmap(TextColor::getRgb, TextColor::fromRgb).optionalFieldOf("color", 0).forGetter(Page::color),
ChapterPageElement.CODEC.listOf().optionalFieldOf("elements", List.of()).forGetter(Page::elements)
).apply(instance, Page::new));
public void toBuffer(RegistryByteBuf buffer) {

View file

@ -61,6 +61,7 @@ public class SpellbookChapterLoader extends JsonDataLoader implements Identifiab
try {
chapters = data.entrySet().stream().map(entry -> {
return Chapter.CODEC.decode(JsonOps.INSTANCE, entry.getValue())
.ifError(error -> LOGGER.error("Could not load spellbook chapters due to exception {}", error))
.result()
.map(Pair::getFirst)
.map(chapter -> new IdentifiableChapter(entry.getKey(), chapter))

View file

@ -1,5 +1,5 @@
{
"side": "RIGHT",
"side": "right",
"y_position": 4,
"color": 0,
"content": {

View file

@ -1,5 +1,5 @@
{
"side": "RIGHT",
"side": "right",
"y_position": 7,
"color": 0,
"content": {

View file

@ -1,5 +1,5 @@
{
"side": "RIGHT",
"side": "right",
"y_position": 5,
"color": 0,
"content": {

View file

@ -1,5 +1,5 @@
{
"side": "RIGHT",
"side": "right",
"y_position": 2,
"color": 0,
"content": {

View file

@ -1,5 +1,5 @@
{
"side": "RIGHT",
"side": "right",
"y_position": 3,
"color": 0,
"content": {

View file

@ -1,5 +1,5 @@
{
"side": "RIGHT",
"side": "right",
"y_position": 0,
"color": 0,
"content": {

View file

@ -1,5 +1,5 @@
{
"side": "RIGHT",
"side": "right",
"y_position": 6,
"color": 0,
"content": {