-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
273 additions
and
256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package juuxel.adorn.client.book; | ||
|
||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
import juuxel.adorn.util.MoreCodecs; | ||
import net.minecraft.text.Text; | ||
|
||
import java.util.List; | ||
|
||
public record Book(Text title, Text subtitle, Text author, List<Page> pages, float titleScale) { | ||
public static final Codec<Book> CODEC = RecordCodecBuilder.create(instance -> instance.group( | ||
MoreCodecs.TEXT.fieldOf("title").forGetter(Book::title), | ||
MoreCodecs.TEXT.fieldOf("subtitle").forGetter(Book::subtitle), | ||
MoreCodecs.TEXT.fieldOf("author").forGetter(Book::author), | ||
Page.CODEC.listOf().fieldOf("pages").forGetter(Book::pages), | ||
Codec.FLOAT.fieldOf("titleScale").forGetter(Book::titleScale) | ||
).apply(instance, Book::new)); | ||
} |
59 changes: 59 additions & 0 deletions
59
common/src/main/java/juuxel/adorn/client/book/BookManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package juuxel.adorn.client.book; | ||
|
||
import com.google.common.base.Predicates; | ||
import com.google.gson.Gson; | ||
import com.google.gson.JsonElement; | ||
import com.mojang.datafixers.util.Pair; | ||
import com.mojang.serialization.JsonOps; | ||
import juuxel.adorn.util.Logging; | ||
import net.minecraft.resource.JsonDataLoader; | ||
import net.minecraft.resource.ResourceManager; | ||
import net.minecraft.util.Identifier; | ||
import net.minecraft.util.profiler.Profiler; | ||
import org.slf4j.Logger; | ||
|
||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
public class BookManager extends JsonDataLoader { | ||
private static final Logger LOGGER = Logging.logger(); | ||
private static final String DATA_TYPE = "adorn/books"; | ||
private static final Gson GSON = new Gson(); | ||
|
||
private Map<Identifier, Book> books = Map.of(); | ||
|
||
public BookManager() { | ||
super(GSON, DATA_TYPE); | ||
} | ||
|
||
@Override | ||
protected void apply(Map<Identifier, JsonElement> prepared, ResourceManager manager, Profiler profiler) { | ||
books = prepared.entrySet() | ||
.stream() | ||
.map(entry -> { | ||
var id = entry.getKey(); | ||
var book = Book.CODEC.decode(JsonOps.INSTANCE, entry.getValue()).get(); | ||
return book.map( | ||
pair -> Pair.of(id, pair.getFirst()), | ||
partial -> { | ||
LOGGER.error("Could not load book {}: {}", id, partial.message()); | ||
return null; | ||
} | ||
); | ||
}) | ||
.filter(Predicates.notNull()) | ||
.collect(Collectors.toMap(Pair::getFirst, Pair::getSecond)); | ||
} | ||
|
||
public boolean contains(Identifier id) { | ||
return books.containsKey(id); | ||
} | ||
|
||
public Book get(Identifier id) { | ||
var book = books.get(id); | ||
if (book == null) { | ||
throw new IllegalArgumentException("Tried to get unknown book '%s' from BookManager".formatted(id)); | ||
} | ||
return book; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package juuxel.adorn.client.book; | ||
|
||
import com.mojang.datafixers.util.Pair; | ||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
import juuxel.adorn.util.MoreCodecs; | ||
import juuxel.adorn.util.Vec2i; | ||
import net.minecraft.text.Text; | ||
import net.minecraft.util.Identifier; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
public record Image(Identifier location, Vec2i size, Placement placement, List<HoverArea> hoverAreas) { | ||
public static final Codec<Image> CODEC = RecordCodecBuilder.create(instance -> instance.group( | ||
Identifier.CODEC.fieldOf("location").forGetter(Image::location), | ||
Vec2i.CODEC.fieldOf("size").forGetter(Image::size), | ||
Placement.CODEC.optionalFieldOf("placement", Placement.AFTER_TEXT).forGetter(Image::placement), | ||
HoverArea.CODEC.listOf().optionalFieldOf("hoverAreas", List.of()).forGetter(Image::hoverAreas) | ||
).apply(instance, Image::new)); | ||
|
||
public enum Placement { | ||
BEFORE_TEXT("beforeText"), | ||
AFTER_TEXT("afterText"); | ||
|
||
private static final Map<String, Placement> BY_ID = Arrays.stream(values()) | ||
.map(placement -> Pair.of(placement.id, placement)) | ||
.collect(Collectors.toMap(Pair::getFirst, Pair::getSecond)); | ||
public static final Codec<Placement> CODEC = Codec.STRING.xmap(BY_ID::get, placement -> placement.id); | ||
|
||
private final String id; | ||
|
||
Placement(String id) { | ||
this.id = id; | ||
} | ||
} | ||
|
||
public record HoverArea(Vec2i position, Vec2i size, Text tooltip) { | ||
public static final Codec<HoverArea> CODEC = RecordCodecBuilder.create(instance -> instance.group( | ||
Vec2i.CODEC.fieldOf("position").forGetter(HoverArea::position), | ||
Vec2i.CODEC.fieldOf("size").forGetter(HoverArea::size), | ||
MoreCodecs.TEXT.fieldOf("tooltip").forGetter(HoverArea::tooltip) | ||
).apply(instance, HoverArea::new)); | ||
|
||
public boolean contains(int x, int y) { | ||
return position.x() <= x && x <= position.x() + size.x() && position.y() <= y && y <= position.y() + size.y(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package juuxel.adorn.client.book; | ||
|
||
import com.mojang.datafixers.util.Pair; | ||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.DataResult; | ||
import com.mojang.serialization.DynamicOps; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
import juuxel.adorn.util.MoreCodecs; | ||
import net.minecraft.item.Item; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.registry.Registries; | ||
import net.minecraft.registry.RegistryKeys; | ||
import net.minecraft.registry.tag.TagKey; | ||
import net.minecraft.text.Text; | ||
import net.minecraft.util.Identifier; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public record Page(List<Icon> icons, Text title, Text text, @Nullable Image image) { | ||
public static final Codec<Page> CODEC = RecordCodecBuilder.create(instance -> instance.group( | ||
Icon.CODEC.listOf().fieldOf("icons").forGetter(Page::icons), | ||
MoreCodecs.TEXT.fieldOf("title").forGetter(Page::title), | ||
MoreCodecs.TEXT.optionalFieldOf("text", Text.empty()).forGetter(Page::text), | ||
Image.CODEC.optionalFieldOf("image").forGetter(page -> Optional.ofNullable(page.image)) | ||
).apply(instance, Page::new)); | ||
|
||
// For DFU | ||
private Page(List<Icon> icons, Text title, Text text, Optional<Image> image) { | ||
this(icons, title, text, image.orElse(null)); | ||
} | ||
|
||
public sealed interface Icon { | ||
Codec<Icon> CODEC = new Codec<>() { | ||
@Override | ||
public <T> DataResult<T> encode(Icon input, DynamicOps<T> ops, T prefix) { | ||
var id = switch (input) { | ||
case ItemIcon(var item) -> Registries.ITEM.getId(item).toString(); | ||
case TagIcon(var tag) -> "#" + tag.id(); | ||
}; | ||
return ops.mergeToPrimitive(prefix, ops.createString(id)); | ||
} | ||
|
||
@Override | ||
public <T> DataResult<Pair<Icon, T>> decode(DynamicOps<T> ops, T input) { | ||
return ops.getStringValue(input) | ||
.map(id -> { | ||
Icon icon; | ||
if (id.startsWith("#")) { | ||
icon = new TagIcon(TagKey.of(RegistryKeys.ITEM, new Identifier(id.substring(1)))); | ||
} else { | ||
icon = new ItemIcon(Registries.ITEM.get(new Identifier(id))); | ||
} | ||
|
||
return Pair.of(icon, ops.empty()); | ||
}); | ||
} | ||
}; | ||
|
||
List<ItemStack> createStacks(); | ||
|
||
record ItemIcon(Item item) implements Icon { | ||
@Override | ||
public List<ItemStack> createStacks() { | ||
return List.of(item.getDefaultStack()); | ||
} | ||
} | ||
|
||
record TagIcon(TagKey<Item> tag) implements Icon { | ||
@Override | ||
public List<ItemStack> createStacks() { | ||
var entries = Registries.ITEM.getOrCreateEntryList(tag); | ||
List<ItemStack> result = new ArrayList<>(entries.size()); | ||
for (var entry : entries) { | ||
result.add(entry.value().getDefaultStack()); | ||
} | ||
return result; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package juuxel.adorn.util; | ||
|
||
import com.mojang.datafixers.util.Pair; | ||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.DataResult; | ||
import com.mojang.serialization.DynamicOps; | ||
|
||
import java.util.stream.IntStream; | ||
|
||
public record Vec2i(int x, int y) { | ||
public static final Codec<Vec2i> CODEC = new Codec<>() { | ||
@Override | ||
public <T> DataResult<T> encode(Vec2i input, DynamicOps<T> ops, T prefix) { | ||
return ops.mergeToPrimitive(prefix, ops.createIntList(IntStream.of(input.x, input.y))); | ||
} | ||
|
||
@Override | ||
public <T> DataResult<Pair<Vec2i, T>> decode(DynamicOps<T> ops, T input) { | ||
return ops.getIntStream(input).flatMap(stream -> { | ||
var iter = stream.iterator(); | ||
|
||
if (!iter.hasNext()) return mismatchedComponentCountResult(); | ||
int x = iter.nextInt(); | ||
if (!iter.hasNext()) return mismatchedComponentCountResult(); | ||
int y = iter.nextInt(); | ||
if (iter.hasNext()) return mismatchedComponentCountResult(); | ||
|
||
return DataResult.success(Pair.of(new Vec2i(x, y), ops.empty())); | ||
}); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Vec2i"; | ||
} | ||
}; | ||
|
||
private static <T> DataResult<T> mismatchedComponentCountResult() { | ||
return DataResult.error(() -> "Vec2i must have exactly two int components"); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.