Skip to content

Commit

Permalink
allow customizing name of dynamic book
Browse files Browse the repository at this point in the history
  • Loading branch information
gliscowo committed Apr 2, 2024
1 parent 70cbbd9 commit ae99d0d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.15.9

# Mod Properties
mod_version = 0.1.4
mod_version = 0.1.5
maven_group = io.wispforest
archives_base_name = lavender

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/io/wispforest/lavender/book/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.minecraft.nbt.NbtHelper;
import net.minecraft.sound.SoundEvent;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -32,6 +33,7 @@ public final class Book {
private final Identifier id;
private final @Nullable Identifier texture;
private final @Nullable Identifier dynamicBookModel;
private final @Nullable Text dynamicBookName;
private final SoundEvent openSound, flippingSound;
private final @Nullable Identifier introEntry;
private final boolean displayUnreadEntryNotifications;
Expand Down Expand Up @@ -61,6 +63,7 @@ public Book(
@Nullable Identifier extend,
@Nullable Identifier texture,
@Nullable Identifier dynamicBookModel,
@Nullable Text dynamicBookName,
@Nullable SoundEvent openSound,
@Nullable SoundEvent flippingSound,
@Nullable Identifier introEntry,
Expand All @@ -72,6 +75,7 @@ public Book(
this.extend = extend;
this.texture = texture;
this.dynamicBookModel = dynamicBookModel;
this.dynamicBookName = dynamicBookName;
this.openSound = openSound != null ? openSound : Lavender.ITEM_BOOK_OPEN;
this.flippingSound = flippingSound != null ? flippingSound : SoundEvents.ITEM_BOOK_PAGE_TURN;
this.introEntry = introEntry;
Expand Down Expand Up @@ -225,6 +229,10 @@ public boolean shouldDisplayUnreadNotification(Category category, ClientPlayerEn
return this.dynamicBookModel;
}

public @Nullable Text dynamicBookName() {
return this.dynamicBookName;
}

public SoundEvent openSound() {
return this.openSound;
}
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/io/wispforest/lavender/book/BookLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.minecraft.registry.Registries;
import net.minecraft.resource.ResourceFinder;
import net.minecraft.resource.ResourceManager;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -72,6 +73,11 @@ public static void reload(ResourceManager manager) {
var extendId = tryGetId(bookObject, "extend");
var dynamicBookModelId = tryGetId(bookObject, "dynamic_book_model");

Text dynamicBookName = null;
if (bookObject.has("dynamic_book_name")) {
dynamicBookName = Text.Serializer.fromJson(bookObject.get("dynamic_book_name"));
}

var openSoundId = tryGetId(bookObject, "open_sound");
var openSoundEvent = openSoundId != null ? Registries.SOUND_EVENT.get(openSoundId) : null;
var flippingSoundId = tryGetId(bookObject, "flipping_sound");
Expand All @@ -83,7 +89,7 @@ public static void reload(ResourceManager manager) {
var displayUnreadEntryNotifications = JsonHelper.getBoolean(bookObject, "display_unread_entry_notifications", true);
var macros = GSON.fromJson(JsonHelper.getObject(bookObject, "macros", new JsonObject()), MACROS_TOKEN);

var book = new Book(resourceId, extendId, textureId, dynamicBookModelId, openSoundEvent, flippingSoundEvent, introEntryId, displayUnreadEntryNotifications, displayCompletion, macros);
var book = new Book(resourceId, extendId, textureId, dynamicBookModelId, dynamicBookName, openSoundEvent, flippingSoundEvent, introEntryId, displayUnreadEntryNotifications, displayCompletion, macros);
LOADED_BOOKS.put(resourceId, book);
if (extendId == null) VISIBLE_BOOKS.put(resourceId, book);
});
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/io/wispforest/lavender/book/LavenderBookItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ public static ItemStack itemOf(Book book) {
}
}

@Override
public Text getName(ItemStack stack) {
if (this.bookId != null) return super.getName(stack);

var book = bookOf(stack);
if (book == null || book.dynamicBookName() == null) return super.getName(stack);

return book.dynamicBookName();
}

/**
* @return A dynamic book with the correct NBT to represent the given book
*/
Expand Down

0 comments on commit ae99d0d

Please sign in to comment.