Skip to content

Commit

Permalink
experimental model type. (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
portlek authored Oct 13, 2024
1 parent 113110c commit 1b4b3d3
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 6 deletions.
20 changes: 20 additions & 0 deletions common/src/main/java/net/infumia/pack/FileResourceUnknown.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package net.infumia.pack;

import team.unnamed.creative.ResourcePack;
import team.unnamed.creative.base.Writable;

final class FileResourceUnknown implements FileResource {

final String path;
final Writable writable;

FileResourceUnknown(final String path, final Writable writable) {
this.path = path;
this.writable = writable;
}

@Override
public void write(final ResourcePack pack) {
pack.unknownFile(this.path, this.writable);
}
}
12 changes: 12 additions & 0 deletions common/src/main/java/net/infumia/pack/FileResources.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Arrays;
import java.util.Collection;
import team.unnamed.creative.atlas.Atlas;
import team.unnamed.creative.base.Writable;
import team.unnamed.creative.font.Font;
import team.unnamed.creative.model.Model;
import team.unnamed.creative.texture.Texture;
Expand Down Expand Up @@ -52,6 +53,17 @@ public static FileResource model(final Model model) {
return new FileResourceModel(model);
}

/**
* Creates a {@link FileResource} for the specified path and writable data.
*
* @param path the path to create a file resource for. Cannot be null.
* @param writable the writable to create a file resource for. Cannot be null.
* @return a {@link FileResource} representing the unknown file.
*/
public static FileResource unknown(final String path, final Writable writable) {
return new FileResourceUnknown(path, writable);
}

/**
* Creates a {@link FileResource} for a collection of file resources.
*
Expand Down
18 changes: 14 additions & 4 deletions common/src/main/java/net/infumia/pack/ResourceProducers.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ public static GlyphImage image(
/**
* Creates an item resource with the specified parameters.
*
* @param itemKey The key for the item. Cannot be null.
* @param overriddenItemKey The key for the base model. Cannot be null.
* @param itemImage The writable image for the blank slot. Cannot be null.
* @param customModelData The custom model data value.
* @param itemKey The key for the item. Cannot be null.
* @param overriddenItemKey The key for the base model. Cannot be null.
* @param itemImage The writable image for the blank slot. Cannot be null.
* @param customModelData The custom model data value.
* @return A {@link FileResource} representing the created item.
*/
public static FileResource item(
Expand Down Expand Up @@ -130,6 +130,16 @@ public static FileResource item(
);
}

/**
* Creates a model resource with the specified parameters.
*
* @param key The key for the item. Cannot be null.
* @return A {@link FileResource} representing the created model.
*/
public static FileResource model(final Key key) {
return FileResources.model(Model.model().key(key).build());
}

private ResourceProducers() {
throw new IllegalStateException("Utility class");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
{
@JsonSubTypes.Type(value = PackReferencePartImage.class, name = "image"),
@JsonSubTypes.Type(value = PackReferencePartItem.class, name = "item"),
@JsonSubTypes.Type(value = PackReferencePartModel.class, name = "model"),
}
)
public abstract class PackReferencePart {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void add(final PackGeneratorContext context) {
context
.pack()
.with(
(ResourceIdentifierImage) () -> key.value(),
(ResourceIdentifierImage) key::value,
ResourceProducers.image(
Font.MINECRAFT_DEFAULT,
Texture.texture(
Expand Down
159 changes: 159 additions & 0 deletions generator/src/main/java/net/infumia/pack/PackReferencePartModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package net.infumia.pack;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.nio.file.Path;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import net.kyori.adventure.key.Key;
import team.unnamed.creative.base.Writable;
import team.unnamed.creative.model.ItemOverride;
import team.unnamed.creative.model.ItemPredicate;
import team.unnamed.creative.model.Model;
import team.unnamed.creative.model.ModelTexture;
import team.unnamed.creative.model.ModelTextures;
import team.unnamed.creative.texture.Texture;

/**
* Represents a model part of a pack reference.
*/
public final class PackReferencePartModel extends PackReferencePart {

@JsonProperty
private String namespace;

@JsonProperty(required = true)
private String key;

@JsonProperty(required = true)
private List<String> textures;

@JsonProperty(required = true)
private String model;

@JsonProperty(value = "custom-model-data")
private Integer customModelData;

@JsonProperty("overridden-namespace")
private String overriddenNamespace;

@JsonProperty(value = "overridden-key", required = true)
private String overriddenKey;

private Path directory;

@Override
public void add(final PackGeneratorContext context) {
final Key key = this.extractKey(context);
final Key overriddenItemKey;
if (this.overriddenNamespace == null) {
overriddenItemKey = Key.key(this.overriddenKey);
} else {
overriddenItemKey = Key.key(this.overriddenNamespace, this.overriddenKey);
}

context
.pack()
.with(
FileResources.all(
this.textures.stream()
.map(texture -> {
final String path = this.parent(context) + texture;
return FileResources.texture(
Texture.texture(
Key.key(key.namespace(), path),
Writable.path(context.rootDirectory().resolve(path))
)
);
})
.collect(Collectors.toList())
)
);
final String path = this.parent(context) + this.model;
context
.pack()
.with(
FileResources.unknown(
"assets/" + key.namespace() + "/models/" + path,
Writable.path(context.rootDirectory().resolve(path))
)
);
context
.pack()
.with(
FileResources.model(
Model.model()
.key(overriddenItemKey)
.parent(Model.ITEM_GENERATED)
.textures(
ModelTextures.builder()
.layers(ModelTexture.ofKey(overriddenItemKey))
.build()
)
.overrides(
ItemOverride.of(
key,
ItemPredicate.customModelData(this.customModelData(context))
)
)
.build()
)
);
}

@Override
PackReferencePartModel directory(final Path directory) {
this.directory = directory;
return this;
}

@Override
Key extractKey(final PackGeneratorContext context) {
final String namespace = this.namespace == null
? context.packReference().defaultNamespace()
: this.namespace;
if (namespace == null) {
throw new IllegalStateException("Pack reference namespace cannot be null!");
}
return Key.key(namespace, this.parent(context) + this.key);
}

private String parent(final PackGeneratorContext context) {
if (this.directory == null) {
return "";
}
return (
context
.rootDirectory()
.relativize(this.directory)
.toString()
.toLowerCase(Locale.ROOT)
.replace("\\", "/")
.replace(" ", "_") +
"/"
);
}

private int customModelData(final PackGeneratorContext context) {
if (this.customModelData != null) {
return this.customModelData;
}

final Integer offset = context.packReference().customModelDataOffset();
if (offset == null) {
throw new IllegalStateException(
String.format(
"Custom model data offset cannot be null when custom-model-data not specified (%s)!",
this.extractKey(context)
)
);
}

final AtomicInteger lastCustomModelData = context.lastCustomModelData();
if (offset > lastCustomModelData.get()) {
lastCustomModelData.set(offset);
}
return lastCustomModelData.getAndIncrement();
}
}
1 change: 0 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
creative = "1.7.3"

[libraries]
adventure-api = { module = "net.kyori:adventure-api", version = "4.17.0" }
creative-api = { module = "team.unnamed:creative-api", version.ref = "creative" }
creative-serializer = { module = "team.unnamed:creative-serializer-minecraft", version.ref = "creative" }

Expand Down

0 comments on commit 1b4b3d3

Please sign in to comment.