-
-
Notifications
You must be signed in to change notification settings - Fork 34
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
6 changed files
with
201 additions
and
4 deletions.
There are no files selected for viewing
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
20 changes: 20 additions & 0 deletions
20
...ck-extras/src/main/java/eu/pb4/polymer/resourcepack/extras/api/format/model/GuiLight.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 eu.pb4.polymer.resourcepack.extras.api.format.model; | ||
|
||
import com.mojang.serialization.Codec; | ||
import net.minecraft.util.StringIdentifiable; | ||
|
||
public enum GuiLight implements StringIdentifiable { | ||
SIDE("side"), | ||
FRONT("front"); | ||
|
||
public static final Codec<GuiLight> CODEC = StringIdentifiable.createCodec(GuiLight::values); | ||
|
||
private final String name; | ||
private GuiLight(String name) { | ||
this.name = name; | ||
} | ||
@Override | ||
public String asString() { | ||
return this.name; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...-pack-extras/src/main/java/eu/pb4/polymer/resourcepack/extras/api/format/model/Model.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,57 @@ | ||
package eu.pb4.polymer.resourcepack.extras.api.format.model; | ||
|
||
import com.google.gson.JsonParser; | ||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.JsonOps; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
import net.minecraft.item.ModelTransformationMode; | ||
import net.minecraft.util.Identifier; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public record Model(Optional<Identifier> parent, Optional<List<ModelElement>> elements, Map<String, String> textures, | ||
Map<ModelTransformationMode, ModelTransformation> display, | ||
Optional<GuiLight> guiLight, | ||
boolean ambientOcclusion) { | ||
public static final Codec<Model> CODEC = RecordCodecBuilder.create(instance -> instance.group( | ||
Identifier.CODEC.optionalFieldOf("parent").forGetter(Model::parent), | ||
ModelElement.CODEC.listOf().optionalFieldOf("elements").forGetter(Model::elements), | ||
Codec.unboundedMap(Codec.STRING, Codec.STRING).optionalFieldOf("textures", Map.of()).forGetter(Model::textures), | ||
Codec.unboundedMap(ModelTransformationMode.CODEC, ModelTransformation.CODEC).optionalFieldOf("display", Map.of()).forGetter(Model::display), | ||
GuiLight.CODEC.optionalFieldOf("gui_light").forGetter(Model::guiLight), | ||
Codec.BOOL.optionalFieldOf("ambientocclusion", true).forGetter(Model::ambientOcclusion) | ||
).apply(instance, Model::new)); | ||
|
||
public Model(Optional<Identifier> parent, Optional<List<ModelElement>> elements, Map<String, String> textures, | ||
Map<ModelTransformationMode, ModelTransformation> display, | ||
Optional<GuiLight> guiLight) { | ||
this(parent, elements, textures, display, guiLight, true); | ||
} | ||
|
||
public Model(Optional<Identifier> parent, Optional<List<ModelElement>> elements, Map<String, String> textures, | ||
Map<ModelTransformationMode, ModelTransformation> display) { | ||
this(parent, elements, textures, display, Optional.empty(), true); | ||
} | ||
|
||
public Model(Optional<Identifier> parent, Optional<List<ModelElement>> elements, Map<String, String> textures) { | ||
this(parent, elements, textures, Map.of(), Optional.empty(), true); | ||
} | ||
|
||
public Model(Identifier parent, Map<String, String> textures) { | ||
this(Optional.of(parent), Optional.empty(), textures, Map.of(), Optional.empty(), true); | ||
} | ||
|
||
public Model(List<ModelElement> elements, Map<String, String> textures) { | ||
this(Optional.empty(), Optional.of(elements), textures, Map.of(), Optional.empty(), true); | ||
} | ||
|
||
public String toJson() { | ||
return CODEC.encodeStart(JsonOps.INSTANCE, this).getOrThrow().toString(); | ||
} | ||
|
||
public static Model fromJson(String json) { | ||
return CODEC.decode(JsonOps.INSTANCE, JsonParser.parseString(json)).getOrThrow().getFirst(); | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
...xtras/src/main/java/eu/pb4/polymer/resourcepack/extras/api/format/model/ModelElement.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,84 @@ | ||
package eu.pb4.polymer.resourcepack.extras.api.format.model; | ||
|
||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
import net.minecraft.util.dynamic.Codecs; | ||
import net.minecraft.util.math.Direction; | ||
import net.minecraft.util.math.Vec3d; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public record ModelElement(Vec3d from, Vec3d to, Map<Direction, Face> faces, Optional<Rotation> rotation, | ||
boolean shade, int lightEmission) { | ||
public static final Codec<ModelElement> CODEC = RecordCodecBuilder.create(instance -> instance.group( | ||
Vec3d.CODEC.fieldOf("from").forGetter(ModelElement::from), | ||
Vec3d.CODEC.fieldOf("to").forGetter(ModelElement::to), | ||
Codec.unboundedMap(Direction.CODEC, Face.CODEC).optionalFieldOf("faces", Map.of()).forGetter(ModelElement::faces), | ||
Rotation.CODEC.optionalFieldOf("rotation").forGetter(ModelElement::rotation), | ||
Codec.BOOL.optionalFieldOf("shade", true).forGetter(ModelElement::shade), | ||
Codecs.rangedInt(0, 15).optionalFieldOf("light_emission", 0).forGetter(ModelElement::lightEmission) | ||
).apply(instance, ModelElement::new)); | ||
|
||
public ModelElement(Vec3d from, Vec3d to, Map<Direction, Face> faces, Optional<Rotation> rotation, | ||
boolean shade) { | ||
this(from, to, faces, rotation, shade, 0); | ||
} | ||
public ModelElement(Vec3d from, Vec3d to, Map<Direction, Face> faces, Optional<Rotation> rotation) { | ||
this(from, to, faces, rotation, true, 0); | ||
} | ||
|
||
public ModelElement(Vec3d from, Vec3d to, Map<Direction, Face> faces) { | ||
this(from, to, faces, Optional.empty(), true, 0); | ||
} | ||
|
||
public record Rotation(Vec3d origin, Direction.Axis axis, float angle, boolean rescale) { | ||
public static final Codec<Rotation> CODEC = RecordCodecBuilder.create(instance -> instance.group( | ||
Vec3d.CODEC.optionalFieldOf("origin", Vec3d.ZERO).forGetter(Rotation::origin), | ||
Direction.Axis.CODEC.fieldOf("axis").forGetter(Rotation::axis), | ||
Codec.FLOAT.fieldOf("angle").forGetter(Rotation::angle), | ||
Codec.BOOL.optionalFieldOf("rescale", false).forGetter(Rotation::rescale) | ||
).apply(instance, Rotation::new)); | ||
|
||
public Rotation(Vec3d origin, Direction.Axis axis, float angle) { | ||
this(origin, axis, angle, false); | ||
} | ||
|
||
public Rotation(Direction.Axis axis, float angle) { | ||
this(Vec3d.ZERO, axis, angle, false); | ||
} | ||
} | ||
|
||
public record Face(List<Float> uv, String texture, Optional<Direction> cullface, int rotation, int tintIndex) { | ||
public static final Codec<Face> CODEC = RecordCodecBuilder.create(instance -> instance.group( | ||
Codec.list(Codec.FLOAT, 4, 4).optionalFieldOf("uv", List.of()).forGetter(Face::uv), | ||
Codec.STRING.optionalFieldOf("texture", "").forGetter(Face::texture), | ||
Direction.CODEC.optionalFieldOf("cullface").forGetter(Face::cullface), | ||
Codec.INT.optionalFieldOf("rotation", 0).forGetter(Face::rotation), | ||
Codec.INT.optionalFieldOf("tintindex", -1).forGetter(Face::tintIndex) | ||
).apply(instance, Face::new)); | ||
|
||
public Face { | ||
if (uv.size() != 4 && !uv.isEmpty()) { | ||
throw new IllegalArgumentException("uv needs to have either 4 elements or be empty"); | ||
} | ||
} | ||
|
||
public Face(List<Float> uv, String texture, Optional<Direction> cullface, int rotation) { | ||
this(uv, texture, cullface, rotation, -1); | ||
} | ||
|
||
public Face(List<Float> uv, String texture, Optional<Direction> cullface) { | ||
this(uv, texture, cullface, 0, -1); | ||
} | ||
|
||
public Face(List<Float> uv, String texture) { | ||
this(uv, texture, Optional.empty(), 0, -1); | ||
} | ||
|
||
public Face(String texture) { | ||
this(List.of(), texture, Optional.empty(), 0, -1); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...rc/main/java/eu/pb4/polymer/resourcepack/extras/api/format/model/ModelTransformation.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,13 @@ | ||
package eu.pb4.polymer.resourcepack.extras.api.format.model; | ||
|
||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
import net.minecraft.util.math.Vec3d; | ||
|
||
public record ModelTransformation(Vec3d rotation, Vec3d translation, Vec3d scale) { | ||
public static final Codec<ModelTransformation> CODEC = RecordCodecBuilder.create(instance -> instance.group( | ||
Vec3d.CODEC.optionalFieldOf("rotation", Vec3d.ZERO).forGetter(ModelTransformation::rotation), | ||
Vec3d.CODEC.optionalFieldOf("translation", Vec3d.ZERO).forGetter(ModelTransformation::translation), | ||
Vec3d.CODEC.optionalFieldOf("scale", Vec3d.ZERO).forGetter(ModelTransformation::scale) | ||
).apply(instance, ModelTransformation::new)); | ||
} |