Skip to content

Commit

Permalink
feat: allow i/o textures to be defined via json
Browse files Browse the repository at this point in the history
remove "render data" system in favour of BlockStates
also fix rendering of i/o config in hand
  • Loading branch information
marcus8448 committed Oct 26, 2024
1 parent 0a5a40c commit d873880
Show file tree
Hide file tree
Showing 25 changed files with 235 additions and 134 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ val wthit = project.property("wthit.version").toString()
plugins {
java
`maven-publish`
id("fabric-loom") version("1.7-SNAPSHOT")
id("fabric-loom") version("1.8-SNAPSHOT")
id("org.cadixdev.licenser") version("0.6.1")
id("org.ajoberstar.grgit") version("5.2.2")
id("dev.galacticraft.mojarn") version("0.5.0+13")
id("dev.galacticraft.mojarn") version("0.5.1+14")
}

group = "dev.galacticraft"
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ mod.version=0.7.0

# Minecraft and Fabric Loader
minecraft.version=1.21.1
loader.version=0.16.5
loader.version=0.16.7
yarn.build=3

# Mod Dependencies
badpackets.version=0.8.1
energy.version=4.1.0
fabric.version=0.105.0+1.21.1
fabric.version=0.107.0+1.21.1

# Optional Mod Dependencies
cloth.config.version=15.0.140
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

import com.google.common.base.Preconditions;
import com.mojang.serialization.Codec;
import dev.galacticraft.machinelib.client.api.model.sprite.MachineTextureBase;
import dev.galacticraft.machinelib.client.api.model.sprite.SingleTextureProvider;
import dev.galacticraft.machinelib.client.api.model.sprite.TextureProvider;
import dev.galacticraft.machinelib.client.impl.model.MachineBakedModel;
Expand All @@ -37,7 +36,8 @@
* A registry for {@link MachineBakedModel} sprite providers.
*/
public final class MachineModelRegistry {
public static final String MARKER = "machinelib:generate";
public static final String MARKER = "machinelib:type";
public static final String BASE_TYPE = "base";

/**
* Registers a sprite provider for a block.
Expand All @@ -52,19 +52,12 @@ public static void register(@NotNull ResourceLocation id, @NotNull Codec<? exten
MachineModelRegistryImpl.FACTORIES.put(id, codec);
}

public static void registerBase(@NotNull ResourceLocation id, @NotNull MachineTextureBase bundle) {
Preconditions.checkNotNull(id);
Preconditions.checkNotNull(bundle);

MachineModelRegistryImpl.TEXTURE_BASES.put(id, bundle);
}

/**
* {@return the registered provider, or null if none is registered}
*
* @param providerId the provider id to get the provider for
*/
public static @Nullable Codec<? extends TextureProvider<?>> getProviderFactory(@NotNull ResourceLocation providerId) {
public static @Nullable Codec<? extends TextureProvider<?>> getProviderFactory(@Nullable ResourceLocation providerId) {
return MachineModelRegistryImpl.FACTORIES.get(providerId);
}

Expand All @@ -73,7 +66,7 @@ public static void registerBase(@NotNull ResourceLocation id, @NotNull MachineTe
*
* @param providerId the provider id to get the provider for
*/
public static @NotNull Codec<? extends TextureProvider<?>> getProviderFactoryOrDefault(@NotNull ResourceLocation providerId) {
public static @NotNull Codec<? extends TextureProvider<?>> getProviderFactoryOrDefault(@Nullable ResourceLocation providerId) {
return MachineModelRegistryImpl.FACTORIES.getOrDefault(providerId, SingleTextureProvider.CODEC);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.galacticraft.machinelib.api.machine.MachineRenderData;
import dev.galacticraft.machinelib.api.util.BlockFace;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.model.Material;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -48,7 +48,7 @@ public Bound bind(Function<Material, TextureAtlasSprite> atlas) {

public record Bound(TextureAtlasSprite front, TextureAtlasSprite base, TextureAtlasSprite side) implements BoundTextureProvider {
@Override
public TextureAtlasSprite getSprite(@Nullable MachineRenderData renderData, @NotNull BlockFace face) {
public TextureAtlasSprite getSprite(@Nullable BlockState state, @NotNull BlockFace face) {
if (face == BlockFace.FRONT) return this.front;
if (face.side()) return this.side;
return this.base;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.galacticraft.machinelib.api.machine.MachineRenderData;
import dev.galacticraft.machinelib.api.util.BlockFace;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.model.Material;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -47,7 +47,7 @@ public Bound bind(Function<Material, TextureAtlasSprite> atlas) {

public record Bound(TextureAtlasSprite front, TextureAtlasSprite base, TextureAtlasSprite side) implements TextureProvider.BoundTextureProvider {
@Override
public TextureAtlasSprite getSprite(@Nullable MachineRenderData renderData, @NotNull BlockFace face) {
public TextureAtlasSprite getSprite(@Nullable BlockState state, @NotNull BlockFace face) {
if (face == BlockFace.FRONT) return this.front;
if (face.side()) return this.side;
return this.base;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,29 @@

package dev.galacticraft.machinelib.client.api.model.sprite;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.client.renderer.texture.TextureAtlas;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.model.Material;
import net.minecraft.client.resources.model.*;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.Collections;
import java.util.function.Function;

public record MachineTextureBase(Material machineEnergyIn, Material machineEnergyOut, Material machineEnergyBoth,
Material machineItemIn, Material machineItemOut, Material machineItemBoth,
Material machineFluidIn, Material machineFluidOut, Material machineFluidBoth,
Material machineAnyIn, Material machineAnyOut, Material machineAnyBoth
) {
) implements UnbakedModel {
public static final Codec<MachineTextureBase> CODEC = RecordCodecBuilder.create(instance -> instance.group(
TypedResourceSprites.CODEC.fieldOf("energy").forGetter(b -> new TypedResourceSprites(b.machineEnergyIn, b.machineEnergyOut, b.machineEnergyBoth)),
TypedResourceSprites.CODEC.fieldOf("item").forGetter(b -> new TypedResourceSprites(b.machineItemIn, b.machineItemOut, b.machineItemBoth)),
TypedResourceSprites.CODEC.fieldOf("fluid").forGetter(b -> new TypedResourceSprites(b.machineFluidIn, b.machineFluidOut, b.machineFluidBoth)),
TypedResourceSprites.CODEC.fieldOf("any").forGetter(b -> new TypedResourceSprites(b.machineAnyIn, b.machineAnyOut, b.machineAnyBoth))
).apply(instance, (e, i, f ,a) -> new MachineTextureBase(e.input, e.output, e.both, i.input, i.output, i.both, f.input, f.output, f.both, a.input, a.output, a.both)));

public static MachineTextureBase prefixed(String id, String prefix) {
return new MachineTextureBase(
Expand All @@ -53,6 +64,20 @@ public Bound bind(Function<Material, TextureAtlasSprite> atlas) {
);
}

@Override
public Collection<ResourceLocation> getDependencies() {
return Collections.emptyList();
}

@Override
public void resolveParents(Function<ResourceLocation, UnbakedModel> modelLoader) {
}

@Override
public @Nullable BakedModel bake(ModelBaker modelBaker, Function<Material, TextureAtlasSprite> textureGetter, ModelState rotationContainer) {
return null;
}

public record Bound(TextureAtlasSprite machineEnergyIn, TextureAtlasSprite machineEnergyOut, TextureAtlasSprite machineEnergyBoth,
TextureAtlasSprite machineItemIn, TextureAtlasSprite machineItemOut, TextureAtlasSprite machineItemBoth,
TextureAtlasSprite machineFluidIn, TextureAtlasSprite machineFluidOut, TextureAtlasSprite machineFluidBoth,
Expand All @@ -63,4 +88,12 @@ public record Bound(TextureAtlasSprite machineEnergyIn, TextureAtlasSprite machi
private static Material mat(String namespace, String location) {
return new Material(TextureAtlas.LOCATION_BLOCKS, ResourceLocation.fromNamespaceAndPath(namespace, location));
}

private record TypedResourceSprites(Material input, Material output, Material both) {
private static final Codec<TypedResourceSprites> CODEC = RecordCodecBuilder.create(instance -> instance.group(
TextureProvider.MATERIAL_CODEC.fieldOf("input").forGetter(TypedResourceSprites::input),
TextureProvider.MATERIAL_CODEC.fieldOf("output").forGetter(TypedResourceSprites::output),
TextureProvider.MATERIAL_CODEC.fieldOf("both").forGetter(TypedResourceSprites::both)
).apply(instance, TypedResourceSprites::new));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.galacticraft.machinelib.api.machine.MachineRenderData;
import dev.galacticraft.machinelib.api.util.BlockFace;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.model.Material;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -47,7 +47,7 @@ public Bound bind(Function<Material, TextureAtlasSprite> atlas) {
public record Bound(TextureAtlasSprite base, TextureAtlasSprite front) implements BoundTextureProvider {

@Override
public TextureAtlasSprite getSprite(@Nullable MachineRenderData renderData, @NotNull BlockFace face) {
public TextureAtlasSprite getSprite(@Nullable BlockState state, @NotNull BlockFace face) {
if (face == BlockFace.FRONT) return this.front;
return this.base;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
package dev.galacticraft.machinelib.client.api.model.sprite;

import com.mojang.serialization.Codec;
import dev.galacticraft.machinelib.api.machine.MachineRenderData;
import dev.galacticraft.machinelib.api.util.BlockFace;
import net.minecraft.client.renderer.texture.TextureAtlas;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.model.Material;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -47,7 +47,7 @@ public Bound bind(Function<Material, TextureAtlasSprite> atlas) {

public record Bound(TextureAtlasSprite texture) implements BoundTextureProvider {
@Override
public TextureAtlasSprite getSprite(@Nullable MachineRenderData renderData, @NotNull BlockFace face) {
public TextureAtlasSprite getSprite(@Nullable BlockState state, @NotNull BlockFace face) {
return this.texture;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
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;
import net.minecraft.client.renderer.texture.TextureAtlas;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.model.Material;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -48,7 +48,7 @@ public interface TextureProvider<B extends TextureProvider.BoundTextureProvider>
B bind(Function<Material, TextureAtlasSprite> atlas);

interface BoundTextureProvider {
TextureAtlasSprite getSprite(@Nullable MachineRenderData renderData, @NotNull BlockFace face);
TextureAtlasSprite getSprite(@Nullable BlockState state, @NotNull BlockFace face);

TextureAtlasSprite getParticle();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import dev.galacticraft.machinelib.api.block.entity.MachineBlockEntity;
import dev.galacticraft.machinelib.api.machine.MachineRenderData;
import dev.galacticraft.machinelib.api.machine.configuration.AccessLevel;
import dev.galacticraft.machinelib.api.machine.configuration.IOConfig;
import dev.galacticraft.machinelib.api.machine.configuration.IOFace;
import dev.galacticraft.machinelib.api.machine.configuration.RedstoneMode;
import dev.galacticraft.machinelib.api.menu.MachineMenu;
Expand Down Expand Up @@ -70,6 +70,7 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.block.entity.SkullBlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -191,8 +192,8 @@ public class MachineScreen<Machine extends MachineBlockEntity, Menu extends Mach
* The height of the capacitor.
*/
protected int capacitorHeight = 48;
protected @NotNull MachineRenderData renderData;
private @Nullable MachineBakedModel model;
private BlockState previousState;

/**
* Creates a new screen from the given screen handler.
Expand All @@ -205,7 +206,6 @@ protected MachineScreen(@NotNull Menu menu, @NotNull Component title, @NotNull R
super(menu, menu.playerInventory, title);

this.texture = texture;
this.renderData = menu.configuration;

UUID owner = this.menu.security.getOwner() == null ? this.menu.player.getUUID() : this.menu.security.getOwner();
this.owner = SkullBlockEntity.fetchGameProfile(owner).thenApply(o -> o.orElse(new GameProfile(owner, "???")));
Expand Down Expand Up @@ -233,10 +233,22 @@ protected void init() {
this.titleLabelX = (this.imageWidth - this.font.width(this.title)) / 2;

if (this.minecraft.getModelManager().getBlockModelShaper().getBlockModel(this.menu.be.getBlockState()) instanceof MachineBakedModel model) {
this.previousState = this.menu.be.getBlockState();
this.model = model;
}
}

@Override
protected void containerTick() {
super.containerTick();
if (!this.menu.be.getBlockState().equals(this.previousState)) {
this.previousState = this.menu.be.getBlockState();
if (this.minecraft.getModelManager().getBlockModelShaper().getBlockModel(this.menu.be.getBlockState()) instanceof MachineBakedModel model) {
this.model = model;
}
}
}

/**
* Appends additional information to the capacitor's tooltip.
*
Expand Down Expand Up @@ -303,12 +315,12 @@ protected void drawConfigurationPanels(@NotNull GuiGraphics graphics, int mouseX
.setStyle(Constant.Text.GRAY_STYLE), PANEL_TITLE_X, PANEL_TITLE_Y, 0xFFFFFFFF);

RenderSystem.setShaderTexture(0, InventoryMenu.BLOCK_ATLAS);
this.drawMachineFace(graphics, TOP_FACE_X, TOP_FACE_Y, this.renderData, BlockFace.TOP);
this.drawMachineFace(graphics, LEFT_FACE_X, LEFT_FACE_Y, this.renderData, BlockFace.LEFT);
this.drawMachineFace(graphics, FRONT_FACE_X, FRONT_FACE_Y, this.renderData, BlockFace.FRONT);
this.drawMachineFace(graphics, RIGHT_FACE_X, RIGHT_FACE_Y, this.renderData, BlockFace.RIGHT);
this.drawMachineFace(graphics, BACK_FACE_X, BACK_FACE_Y, this.renderData, BlockFace.BACK);
this.drawMachineFace(graphics, BOTTOM_FACE_X, BOTTOM_FACE_Y, this.renderData, BlockFace.BOTTOM);
this.drawMachineFace(graphics, TOP_FACE_X, TOP_FACE_Y, this.menu.configuration, BlockFace.TOP);
this.drawMachineFace(graphics, LEFT_FACE_X, LEFT_FACE_Y, this.menu.configuration, BlockFace.LEFT);
this.drawMachineFace(graphics, FRONT_FACE_X, FRONT_FACE_Y, this.menu.configuration, BlockFace.FRONT);
this.drawMachineFace(graphics, RIGHT_FACE_X, RIGHT_FACE_Y, this.menu.configuration, BlockFace.RIGHT);
this.drawMachineFace(graphics, BACK_FACE_X, BACK_FACE_Y, this.menu.configuration, BlockFace.BACK);
this.drawMachineFace(graphics, BOTTOM_FACE_X, BOTTOM_FACE_Y, this.menu.configuration, BlockFace.BOTTOM);
poseStack.popPose();
}
if (Tab.STATS.isOpen()) {
Expand Down Expand Up @@ -366,13 +378,12 @@ protected void drawTitle(@NotNull GuiGraphics graphics) {
* @param graphics the gui graphics
* @param x the x position to draw at
* @param y the y position to draw at
* @param data the machine's extra render data
* @param ioConfig the machine's extra render data
* @param face the face to draw
*/
private void drawMachineFace(@NotNull GuiGraphics graphics, int x, int y, @NotNull MachineRenderData data, @NotNull BlockFace face) {
IOFace machineFace = menu.configuration.get(face);
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, model.getSprite(face, data, machineFace.getType(), machineFace.getFlow()));
graphics.blit(x, y, 0, MACHINE_FACE_SIZE, MACHINE_FACE_SIZE, this.model.getSprite(this.menu.be.getBlockState(), face, ioConfig));
}
}

Expand Down
Loading

0 comments on commit d873880

Please sign in to comment.