-
Notifications
You must be signed in to change notification settings - Fork 0
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
7 changed files
with
207 additions
and
6 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
common/src/main/java/net/infumia/pack/FileResourceUnknown.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,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); | ||
} | ||
} |
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
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
159 changes: 159 additions & 0 deletions
159
generator/src/main/java/net/infumia/pack/PackReferencePartModel.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,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(); | ||
} | ||
} |
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