Skip to content

Commit

Permalink
fix: allow alternative short-form texture declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus8448 committed Oct 8, 2024
1 parent b2a6f56 commit 0a5a40c
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

package dev.galacticraft.machinelib.api.transfer;

import com.mojang.serialization.Codec;
import dev.galacticraft.machinelib.impl.Constant;
import io.netty.buffer.ByteBuf;
import net.minecraft.ChatFormatting;
Expand Down Expand Up @@ -59,7 +60,7 @@ public enum ResourceType implements StringRepresentable {
*/
ANY(0b111, Component.translatable(Constant.TranslationKey.ANY).setStyle(Style.EMPTY.withColor(ChatFormatting.AQUA)));

public static final StringRepresentable.EnumCodec<ResourceType> CODEC = StringRepresentable.fromEnum(ResourceType::values);
public static final Codec<ResourceType> CODEC = StringRepresentable.fromValues(ResourceType::values);
public static final StreamCodec<ByteBuf, ResourceType> STREAM_CODEC = ByteBufCodecs.BYTE.map(i -> i == -1 ? null : values()[i], face -> face == null ? -1 : (byte) face.ordinal());

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package dev.galacticraft.machinelib.client.api.model.sprite;

import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.galacticraft.machinelib.api.machine.MachineRenderData;
import dev.galacticraft.machinelib.api.util.BlockFace;
Expand All @@ -36,10 +37,13 @@
import java.util.function.Function;

public interface TextureProvider<B extends TextureProvider.BoundTextureProvider> {
Codec<Material> MATERIAL_CODEC = RecordCodecBuilder.create(instance -> instance.group(
ResourceLocation.CODEC.fieldOf("atlas").orElse(TextureAtlas.LOCATION_BLOCKS).forGetter(Material::atlasLocation),
ResourceLocation.CODEC.fieldOf("texture").forGetter(Material::texture)
).apply(instance, Material::new));
Codec<Material> MATERIAL_CODEC = Codec.withAlternative(
RecordCodecBuilder.create(instance -> instance.group(
ResourceLocation.CODEC.fieldOf("atlas").orElse(TextureAtlas.LOCATION_BLOCKS).forGetter(Material::atlasLocation),
ResourceLocation.CODEC.fieldOf("texture").forGetter(Material::texture)
).apply(instance, Material::new)),
ResourceLocation.CODEC.comapFlatMap(tex -> DataResult.success(new Material(TextureAtlas.LOCATION_BLOCKS, tex)), Material::atlasLocation)
);

B bind(Function<Material, TextureAtlasSprite> atlas);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.renderer.v1.mesh.MutableQuadView;
import net.fabricmc.fabric.api.renderer.v1.model.FabricBakedModel;
import net.fabricmc.fabric.api.renderer.v1.model.ModelHelper;
import net.fabricmc.fabric.api.renderer.v1.render.RenderContext;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.block.model.ItemOverrides;
import net.minecraft.client.renderer.block.model.ItemTransform;
import net.minecraft.client.renderer.block.model.ItemTransforms;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.model.BakedModel;
Expand All @@ -58,7 +58,6 @@
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.joml.Vector3f;

import java.util.Collections;
import java.util.List;
Expand All @@ -67,17 +66,6 @@
@Environment(EnvType.CLIENT)
@ApiStatus.Internal
public final class MachineBakedModel implements FabricBakedModel, BakedModel {
private static final ItemTransforms ITEM_TRANSFORMATION = new ItemTransforms(
new ItemTransform(new Vector3f(75, 45, 0), new Vector3f(0, 0.25f, 0), new Vector3f(0.375f, 0.375f, 0.375f)),
new ItemTransform(new Vector3f(75, 45, 0), new Vector3f(0, 0.25f, 0), new Vector3f(0.375f, 0.375f, 0.375f)),
new ItemTransform(new Vector3f(0, 225, 0), new Vector3f(0, 0, 0), new Vector3f(0.40f, 0.40f, 0.40f)),
new ItemTransform(new Vector3f(0, 45, 0), new Vector3f(0, 0, 0), new Vector3f(0.40f, 0.40f, 0.40f)),
ItemTransform.NO_TRANSFORM,
new ItemTransform(new Vector3f(30, 225, 0), new Vector3f(0, 0, 0), new Vector3f(0.625f, 0.625f, 0.625f)),
new ItemTransform(new Vector3f(0, 0, 0), new Vector3f(0, 0.2f, 0), new Vector3f(0.25f, 0.25f, 0.25f)),
new ItemTransform(new Vector3f(0, 0, 0), new Vector3f(0, 0, 0), new Vector3f(0.5f, 0.5f, 0.5f))
);

private final TextureProvider.BoundTextureProvider provider;
private final MachineTextureBase.Bound base;

Expand All @@ -101,8 +89,8 @@ private boolean transform(MachineRenderData renderData, @NotNull BlockState stat

private boolean transformItem(IOConfig config, @NotNull MutableQuadView quad) {
BlockFace face = BlockFace.from(Direction.NORTH, quad.nominalFace());
IOFace IOFace = config.get(face);
assert face != null;
IOFace IOFace = config.get(face);
quad.spriteBake(getSprite(face,
config,
IOFace.getType(), IOFace.getFlow()),
Expand Down Expand Up @@ -245,7 +233,7 @@ public boolean isCustomRenderer() {

@Override
public @NotNull ItemTransforms getTransforms() {
return ITEM_TRANSFORMATION;
return ModelHelper.MODEL_TRANSFORM_BLOCK;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class MachineModelLoadingPlugin implements PreparableModelLoadingPlugin<M
JsonObject json = this.data.remove(context.id());
if (json != null) {
Codec<? extends TextureProvider<?>> codec = MachineModelRegistry.getProviderFactoryOrDefault(ResourceLocation.parse(GsonHelper.getAsString(json, MachineModelRegistry.MARKER)));
DataResult<? extends Pair<? extends TextureProvider<?>, JsonElement>> sprites = codec.decode(JsonOps.INSTANCE, json.getAsJsonObject("data"));
DataResult<? extends Pair<? extends TextureProvider<?>, JsonElement>> sprites = codec.decode(JsonOps.INSTANCE, json.get("data"));
JsonElement baseId = json.get("base");
ResourceLocation base = baseId == null ? context.id().withPath("base") : ResourceLocation.parse(baseId.getAsString());
MachineUnbakedModel model = new MachineUnbakedModel(sprites.getOrThrow().getFirst(), base);
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 0a5a40c

Please sign in to comment.