Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: item model overrides #19

Merged
merged 8 commits into from
Feb 6, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
public record TextureProvider(@Nullable Material front, @Nullable Material back,
@Nullable Material left, @Nullable Material right,
@Nullable Material top, @Nullable Material bottom,
@Nullable Material particle) {
@Nullable Material particle, @Nullable Material topOverride) {
public static final Codec<Material> MATERIAL_CODEC = Codec.withAlternative(
ResourceLocation.CODEC.flatComapMap(tex -> new Material(TextureAtlas.LOCATION_BLOCKS, tex), mat -> mat.atlasLocation().equals(TextureAtlas.LOCATION_BLOCKS) ? DataResult.success(mat.texture()) : DataResult.error(() -> "not block", mat.texture())),
RecordCodecBuilder.create(instance -> instance.group(
Expand All @@ -57,15 +57,17 @@ public record TextureProvider(@Nullable Material front, @Nullable Material back,
MATERIAL_CODEC.optionalFieldOf("right").forGetter(t -> Optional.ofNullable(t.right)),
MATERIAL_CODEC.optionalFieldOf("top").forGetter(t -> Optional.ofNullable(t.top)),
MATERIAL_CODEC.optionalFieldOf("bottom").forGetter(t -> Optional.ofNullable(t.bottom)),
MATERIAL_CODEC.optionalFieldOf("particle").forGetter(t -> Optional.ofNullable(t.particle))
).apply(instance, (a, b, c, d, e, f, g) -> new TextureProvider(
MATERIAL_CODEC.optionalFieldOf("particle").forGetter(t -> Optional.ofNullable(t.particle)),
MATERIAL_CODEC.optionalFieldOf("top_item_override").forGetter(t -> Optional.ofNullable(t.topOverride))
).apply(instance, (a, b, c, d, e, f, g, h) -> new TextureProvider(
a.orElse(null),
b.orElse(null),
c.orElse(null),
d.orElse(null),
e.orElse(null),
f.orElse(null),
g.orElse(null)
g.orElse(null),
h.orElse(null)
)));

public BoundTextureProvider bind(Function<Material, TextureAtlasSprite> atlas) {
Expand All @@ -76,7 +78,8 @@ public BoundTextureProvider bind(Function<Material, TextureAtlasSprite> atlas) {
this.right != null ? atlas.apply(this.right) : null,
this.top != null ? atlas.apply(this.top) : null,
this.bottom != null ? atlas.apply(this.bottom) : null,
this.particle != null ? atlas.apply(this.particle) : null
this.particle != null ? atlas.apply(this.particle) : null,
this.topOverride != null ? atlas.apply(this.topOverride) : null
);
}

Expand All @@ -89,11 +92,11 @@ public static TextureProvider.Builder builder(String modId) {
}

public static TextureProvider none() {
return new TextureProvider(null, null, null, null, null, null, null);
return new TextureProvider(null, null, null, null, null, null, null, null);
}

public static TextureProvider all(Material material) {
return new TextureProvider(material, material, material, material, material, material, material);
return new TextureProvider(material, material, material, material, material, material, material, null);
}

public static TextureProvider all(ResourceLocation material) {
Expand All @@ -113,6 +116,7 @@ public static class Builder {
private @Nullable Material top = null;
private @Nullable Material bottom = null;
private @Nullable Material particle = null;
private @Nullable Material topOverride = null;

private Builder(@Nullable String modId) {
this.modId = modId;
Expand Down Expand Up @@ -153,6 +157,11 @@ public Builder particle(String texture) {
return this.particle(ResourceLocation.fromNamespaceAndPath(this.modId, texture));
}

public Builder topOverride(String texture) {
Preconditions.checkNotNull(this.modId);
return this.topOverride(ResourceLocation.fromNamespaceAndPath(this.modId, texture));
}

public Builder all(String texture) {
Preconditions.checkNotNull(this.modId);
return this.all(ResourceLocation.fromNamespaceAndPath(this.modId, texture));
Expand Down Expand Up @@ -191,6 +200,10 @@ public Builder particle(ResourceLocation texture) {
return this.particle(new Material(TextureAtlas.LOCATION_BLOCKS, texture));
}

public Builder topOverride(ResourceLocation texture) {
return this.topOverride(new Material(TextureAtlas.LOCATION_BLOCKS, texture));
}

public Builder all(ResourceLocation texture) {
return this.all(new Material(TextureAtlas.LOCATION_BLOCKS, texture));
}
Expand Down Expand Up @@ -227,6 +240,10 @@ public Builder particle(Block texture) {
return this.particle(TextureMapping.getBlockTexture(texture));
}

public Builder topOverride(Block texture) {
return this.topOverride(TextureMapping.getBlockTexture(texture));
}

public Builder all(Block texture) {
return this.all(TextureMapping.getBlockTexture(texture));
}
Expand Down Expand Up @@ -270,6 +287,11 @@ public Builder particle(Material material) {
return this;
}

public Builder topOverride(Material material) {
this.topOverride = material;
return this;
}

public Builder all(Material material) {
this.front = material;
this.back = material;
Expand All @@ -290,14 +312,14 @@ public Builder sides(Material material) {
}

public TextureProvider build() {
return new TextureProvider(this.front, this.back, this.left, this.right, this.top, this.bottom, this.particle);
return new TextureProvider(this.front, this.back, this.left, this.right, this.top, this.bottom, this.particle, this.topOverride);
}
}

public record BoundTextureProvider(@Nullable TextureAtlasSprite front, @Nullable TextureAtlasSprite back,
@Nullable TextureAtlasSprite left, @Nullable TextureAtlasSprite right,
@Nullable TextureAtlasSprite top, @Nullable TextureAtlasSprite bottom,
@Nullable TextureAtlasSprite particle) {
@Nullable TextureAtlasSprite particle, @Nullable TextureAtlasSprite topOverride) {
@Nullable
public TextureAtlasSprite getSprite(@NotNull BlockFace face) {
return switch (face) {
Expand All @@ -314,5 +336,17 @@ public TextureAtlasSprite getSprite(@NotNull BlockFace face) {
public TextureAtlasSprite getParticle() {
return this.particle;
}

@Nullable
public TextureAtlasSprite getItemOverride(@NotNull BlockFace face) {
return switch (face) {
case FRONT -> null;
case BACK -> null;
case LEFT -> null;
case RIGHT -> null;
case TOP -> this.topOverride;
case BOTTOM -> null;
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ protected void drawTitle(@NotNull GuiGraphics graphics) {
*/
private void drawMachineFace(@NotNull GuiGraphics graphics, int x, int y, @NotNull IOConfig ioConfig, @NotNull BlockFace face) {
if (this.model != null) {
graphics.blit(x, y, 0, MACHINE_FACE_SIZE, MACHINE_FACE_SIZE, this.model.getSprite(this.menu.be.getBlockState(), face, ioConfig));
graphics.blit(x, y, 0, MACHINE_FACE_SIZE, MACHINE_FACE_SIZE, this.model.getItemOverride(this.menu.be.getBlockState(), face, ioConfig));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ public TextureAtlasSprite getSprite(@Nullable BlockState state, @NotNull BlockFa
return sprite == null ? this.base.base() : sprite;
}

public TextureAtlasSprite getItemOverride(@Nullable BlockState state, @NotNull BlockFace face, @Nullable IOConfig config) {
TextureAtlasSprite override = this.provider.getItemOverride(face);
return override != null ? override : this.getSprite(state, face, config);
}

public TextureProvider.BoundTextureProvider getProvider() {
return provider;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,21 @@ public class MachineModelLoadingPlugin implements PreparableModelLoadingPlugin<M
DataResult<? extends Pair<TextureProvider, JsonElement>> sprites = TextureProvider.CODEC.decode(JsonOps.INSTANCE, json.get("data"));
JsonElement baseId = json.get("base");
ResourceLocation base = baseId == null ? context.id().withPath(DEFAULT_MACHINE_BASE) : ResourceLocation.parse(baseId.getAsString());
ResourceLocation location = ResourceLocation.fromNamespaceAndPath(context.id().getNamespace(), context.id().getPath().replace("machine/", "item/"));
MachineUnbakedModel model = new MachineUnbakedModel(sprites.getOrThrow().getFirst(), base);
this.pendingItemModels.put(ResourceLocation.fromNamespaceAndPath(context.id().getNamespace(), context.id().getPath().replace("machine/", "item/")), model);
this.pendingItemModels.put(location, model);
if (json.has("item_override")) {
JsonElement itemOverride = json.get("item_override");
UnbakedModel unbaked = context.getOrLoadModel(ResourceLocation.parse(itemOverride.getAsString()));
this.pendingItemModels.put(location, unbaked);
}
return model;
}

MachineTextureBase base = this.data.getBase(context.id());
if (base != null) return base;

return this.pendingItemModels.remove(context.id()); //todo: allow overriding item models
return this.pendingItemModels.remove(context.id());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"data": {
"front": "minecraft:block/furnace_front"
"front": "minecraft:block/furnace_front",
"top_item_override": "minecraft:block/furnace_top"
},
"machinelib:type": "machine"
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public void generateBlockStateModels(BlockModelGenerators gen) {

MachineModelGenerator.createTrivialMachine(gen, TestModBlocks.GENERATOR, TextureProvider.builder()
.front(TextureMapping.getBlockTexture(Blocks.FURNACE, "_front"))
.topOverride(TextureMapping.getBlockTexture(Blocks.FURNACE, "_top"))
.build());
MachineModelGenerator.createTrivialMachine(gen, TestModBlocks.MIXER, TextureProvider.none());
}
Expand Down