Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,34 @@ default void part(final @NotNull ResourcePackPart part) {
part.addTo(this);
}

//#region Texts (Keyed)
/**
* Sets a texts file in this resource container.
*
* @param content The content of the text to add into the pack
* @since 1.8.4
*/
void texts(final @NotNull Key key, final @NotNull Writable content);

/**
* Gets the text with the given key.
*
* @param key The text key
* @return The text, null if not found
* @since 1.8.4
*/
@Nullable Writable texts(final @NotNull Key key);

/**
* Gets all the texts in this resource container.
* The returned map cannot be modified.
*
* @return The texts
* @since 1.8.4
*/
@NotNull Map<Key, Writable> texts();
//#endregion

//#region Unknown Files (By path, relative to current's resource container)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
Expand All @@ -70,6 +71,7 @@ public class ResourceContainerImpl implements ResourceContainer {
private final Map<String, SoundRegistry> soundRegistries = new LinkedHashMap<>();
private final Map<Key, Sound> sounds = new LinkedHashMap<>();
private final Map<Key, Texture> textures = new LinkedHashMap<>();
private final Map<Key, Writable> texts = new LinkedHashMap<>();

// Unknown files we don't know how to parse
private final Map<String, Writable> files = new LinkedHashMap<>();
Expand Down Expand Up @@ -324,6 +326,23 @@ public boolean removeTexture(final @NotNull Key key) {
}
//#endregion

//#region Texts
@Override
public void texts(final @NotNull Key key, final @NotNull Writable splashTexts) {
this.texts.put(key, splashTexts);
}

@Override
public @Nullable Writable texts(@NotNull Key key) {
return this.texts.get(key);
}

@Override
public @NotNull Map<Key, Writable> texts() {
return Collections.unmodifiableMap(this.texts);
}
//#endregion

//#region Unknown Files (By absolute path)
@Override
public void unknownFile(final @NotNull String path, final @NotNull Writable data) {
Expand Down
25 changes: 25 additions & 0 deletions docs/texts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## Texts

Minecraft supports modifying texts that appear in-game. This
includes various texts such as splash texts, end text and the credits.

### Interacting with texts

Interacting with texts can be done by either adding texts or reading them.
Below is an example with splash texts.

<!--@formatter:off-->
```java
// Reading...
Writable content = pack.texts(Key.key(Key.MINECRAFT_NAMESPACE, "splashes"));

List<String> texts = content.toUTF8String().split("\n");

// Writing...
pack.texts(
Key.key(Key.MINECRAFT_NAMESPACE, "splashes"),
Writable.stringUtf8(String.join("\n", texts))
);
```
<!--@formatter:on-->

Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,13 @@ private MinecraftResourcePackReaderImpl(
));
}
}
} else if (categoryName.equals(TEXTS_FOLDER)) {
String textKey = withoutExtension(categoryPath, TEXT_EXTENSION);
if (textKey != null) {
Key key = Key.key(namespace, textKey);

container.texts(key, reader.content().asWritable());
}
} else {
// get the resource category, if the local pack format (overlay or root) is the same as the
// root pack format, we can use the previously computed map, otherwise we need to compute it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public final class MinecraftResourcePackStructure {
public static final String TEXTURE_EXTENSION = ".png";
public static final String METADATA_EXTENSION = ".mcmeta";
public static final String OBJECT_EXTENSION = ".json";
public static final String TEXT_EXTENSION = ".txt";

public static final String FILE_SEPARATOR = "/";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package team.unnamed.creative.serialize.minecraft;

import com.google.gson.stream.JsonWriter;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.key.Keyed;
import org.jetbrains.annotations.NotNull;
import team.unnamed.creative.ResourcePack;
Expand All @@ -48,6 +49,7 @@
import java.io.IOException;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -132,6 +134,14 @@ private void writeWithBasePathAndTargetPackFormat(FileTreeWriter target, Resourc
}
}

// write texts
for (Map.Entry<Key, Writable> text : container.texts().entrySet()) {
target.write(
String.format("%sassets/%s/texts/%s.txt", basePath, text.getKey().namespace(), text.getKey().value()),
text.getValue()
);
}

// write unknown files
for (Map.Entry<String, Writable> entry : container.unknownFiles().entrySet()) {
target.write(basePath + entry.getKey(), entry.getValue());
Expand Down