Skip to content

Commit

Permalink
sort.
Browse files Browse the repository at this point in the history
  • Loading branch information
portlek committed Oct 1, 2024
1 parent 8a83b19 commit 4e01550
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 56 deletions.
11 changes: 6 additions & 5 deletions generator/src/main/java/net/infumia/pack/PackParser.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.infumia.pack;

import java.util.Collection;
import java.util.Comparator;
import team.unnamed.creative.metadata.pack.PackMeta;

/**
Expand Down Expand Up @@ -39,10 +39,11 @@ private static void parseMeta(final PackGeneratorContext context) {
}

private static void parseParts(final PackGeneratorContext context) {
final Collection<PackReferencePart> parts = context.packPartReferences();
for (final PackReferencePart part : parts) {
part.add(context);
}
context
.packPartReferences()
.stream()
.sorted(Comparator.comparing(part -> part.extractKey(context)))
.forEach(part -> part.add(context));
}

private PackParser() {
Expand Down
12 changes: 11 additions & 1 deletion generator/src/main/java/net/infumia/pack/PackReferencePart.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import java.nio.file.Path;
import net.kyori.adventure.key.Key;

/**
* Abstract base class for a pack part reference.
Expand All @@ -19,7 +20,7 @@ public abstract class PackReferencePart {
/**
* Adds this part to the given pack generation context.
*
* @param context the pack generation context
* @param context the pack generation context to add.
*/
public abstract void add(PackGeneratorContext context);

Expand All @@ -30,4 +31,13 @@ public abstract class PackReferencePart {
* @return the updated pack part reference.
*/
abstract PackReferencePart directory(Path directory);

/**
* Returns the key of the part.
*
* @param context the context to extract.
*
* @return the comparable key.
*/
abstract Key extractKey(PackGeneratorContext context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,38 +33,18 @@ public final class PackReferencePartImage extends PackReferencePart {

@Override
public void add(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!");
}

final Path root = context.rootDirectory();

final String parent;
if (this.directory == null) {
parent = "";
} else {
parent = root
.relativize(this.directory)
.toString()
.toLowerCase(Locale.ROOT)
.replace("\\", "/")
.replace(" ", "_") +
"/";
}

final String key = parent + this.key;
final Key key = this.extractKey(context);
context
.pack()
.with(
(ResourceIdentifierImage) () -> key,
(ResourceIdentifierImage) () -> key.value(),
ResourceProducers.image(
Font.MINECRAFT_DEFAULT,
Texture.texture(
Key.key(namespace, key + ".png"),
Writable.path(root.resolve(parent + this.image))
Key.key(key.namespace(), key.value() + ".png"),
Writable.path(
context.rootDirectory().resolve(this.parent(context) + this.image)
)
),
new TextureProperties(this.height, this.ascent)
)
Expand All @@ -77,6 +57,17 @@ PackReferencePartImage directory(final Path 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);
}

@Override
public String toString() {
return new StringJoiner(", ", PackReferencePartImage.class.getSimpleName() + "[", "]")
Expand All @@ -88,4 +79,20 @@ public String toString() {
.add("directory=" + this.directory)
.toString();
}

private String parent(final PackGeneratorContext context) {
if (this.directory == null) {
return "";
}
return (
context
.rootDirectory()
.relativize(this.directory)
.toString()
.toLowerCase(Locale.ROOT)
.replace("\\", "/")
.replace(" ", "_") +
"/"
);
}
}
55 changes: 31 additions & 24 deletions generator/src/main/java/net/infumia/pack/PackReferencePartItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,6 @@ public final class PackReferencePartItem extends PackReferencePart {

@Override
public void add(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!");
}

final Path root = context.rootDirectory();

final String parent;
if (this.directory == null) {
parent = "";
} else {
parent = root
.relativize(this.directory)
.toString()
.toLowerCase(Locale.ROOT)
.replace("\\", "/")
.replace(" ", "_") +
"/";
}

final Key overriddenItemKey;
if (this.overriddenNamespace == null) {
overriddenItemKey = Key.key(this.overriddenKey);
Expand All @@ -68,9 +46,11 @@ public void add(final PackGeneratorContext context) {
.pack()
.with(
ResourceProducers.item(
Key.key(namespace, parent + this.key),
this.extractKey(context),
overriddenItemKey,
Writable.path(root.resolve(parent + this.image)),
Writable.path(
context.rootDirectory().resolve(this.parent(context) + this.image)
),
this.customModelData(context)
)
);
Expand All @@ -82,6 +62,17 @@ PackReferencePartItem directory(final Path 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);
}

@Override
public String toString() {
return new StringJoiner(", ", PackReferencePartItem.class.getSimpleName() + "[", "]")
Expand All @@ -95,6 +86,22 @@ public String toString() {
.toString();
}

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;
Expand Down

0 comments on commit 4e01550

Please sign in to comment.