Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.hephaestus.glowcase.block.entity;

import com.mojang.serialization.Codec;
import dev.hephaestus.glowcase.Glowcase;
import dev.hephaestus.glowcase.util.PartPoseInfo;
import net.minecraft.core.BlockPos;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.registries.Registries;
Expand All @@ -19,13 +21,18 @@
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@NullMarked
public class EntityDisplayBlockEntity extends DisplayBlockEntity implements StackInteractable {
public static final TagKey<EntityType<?>> TICK = TagKey.create(Registries.ENTITY_TYPE, Glowcase.id("tick_in_display"));

protected int entityId;
protected @Nullable Entity displayEntity = null;
protected @Nullable EntityType<?> entityType = null;
protected Map<String, PartPoseInfo> partPoses = new HashMap<>();

public EntityDisplayBlockEntity(BlockPos pos, BlockState state) {
super(Glowcase.ENTITY_DISPLAY_BLOCK_ENTITY.get(), pos, state);
Expand Down Expand Up @@ -82,12 +89,35 @@ public static void tick(Level world, BlockPos blockPos, BlockState state, Entity
}
}

public Map<String,PartPoseInfo> getPartPoses(){
return partPoses;
}

public void loadPartPoses(Map<String, PartPoseInfo> newPartPoses) {
this.partPoses.putAll(newPartPoses);
}

public PartPoseInfo getPartPostInfo(String name) {
if(partPoses.containsKey(name)) {
return partPoses.get(name);
}
return new PartPoseInfo(name,new Vector3f(0), new Vector3f(0));
}

public void setPartPoseInfo(String name, PartPoseInfo partPoseInfo) {
this.partPoses.put(name,partPoseInfo);
}


@Override
protected void saveAdditional(ValueOutput view) {
super.saveAdditional(view);
if (entityType != null) {
view.store("type", EntityType.CODEC, entityType);
}
if(!partPoses.isEmpty()) {
view.store("partPoses", Codec.unboundedMap(Codec.STRING, PartPoseInfo.CODEC), partPoses);
}
}

@Override
Expand All @@ -97,6 +127,10 @@ protected void loadAdditional(ValueInput view) {
this.entityId = view.getIntOr("entityId", 0);
this.entityType = view.read("type", EntityType.CODEC).orElse(null);
this.displayEntity = null;

this.partPoses.clear();
Optional<Map<String, PartPoseInfo>> optionalStringPartPoseInfoMap = view.read("partPoses", Codec.unboundedMap(Codec.STRING, PartPoseInfo.CODEC));
optionalStringPartPoseInfoMap.ifPresent(stringPartPoseInfoMap -> this.partPoses.putAll(stringPartPoseInfoMap));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,12 @@ public void init() {
@Override
public void extractRenderState(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float delta) {
super.extractRenderState(graphics, mouseX, mouseY, delta);
graphics.text(minecraft.font, Component.translatable("gui.glowcase.scale_label"), 20, 17, 0xFFFFFFFF);
graphics.text(minecraft.font, Component.translatable("gui.glowcase.x_offset_label"), 20, 47, 0xFFFFFFFF);
graphics.text(minecraft.font, Component.translatable("gui.glowcase.y_offset_label"), 20, 77, 0xFFFFFFFF);
graphics.text(minecraft.font, Component.translatable("gui.glowcase.z_offset_label"), 20, 107, 0xFFFFFFFF);
graphics.text(minecraft.font, Component.translatable("gui.glowcase.pitch_value"), 20, 137, 0xFFFFFFFF);
graphics.text(minecraft.font, Component.translatable("gui.glowcase.yaw_value"), 20, 167, 0xFFFFFFFF);
graphics.text(minecraft.font, Component.translatable("gui.glowcase.scale_label"), 20, this.scaleField.getY() + 7, 0xFFFFFFFF);
graphics.text(minecraft.font, Component.translatable("gui.glowcase.x_offset_label"), 20, this.xOffsetField.getY() + 7, 0xFFFFFFFF);
graphics.text(minecraft.font, Component.translatable("gui.glowcase.y_offset_label"), 20, this.yOffsetField.getY() + 7, 0xFFFFFFFF);
graphics.text(minecraft.font, Component.translatable("gui.glowcase.z_offset_label"), 20, this.zOffsetField.getY() + 7, 0xFFFFFFFF);
graphics.text(minecraft.font, Component.translatable("gui.glowcase.pitch_value"), 20, this.pitchField.getY() + 7, 0xFFFFFFFF);
graphics.text(minecraft.font, Component.translatable("gui.glowcase.yaw_value"), 20, this.yawField.getY() + 7, 0xFFFFFFFF);
}

protected final void editDisplayBlock() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,166 @@
package dev.hephaestus.glowcase.client.gui.screen.ingame;

import com.google.common.primitives.Floats;
import dev.hephaestus.glowcase.block.entity.DisplayBlockEntity;
import dev.hephaestus.glowcase.block.entity.EntityDisplayBlockEntity;
import dev.hephaestus.glowcase.client.util.ModelPartUtil;
import dev.hephaestus.glowcase.packet.C2SEditEntityDisplayBlock;
import dev.hephaestus.glowcase.util.PartPoseInfo;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.client.gui.components.StringWidget;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.model.geom.ModelPart;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.EntityType;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class EntityDisplayEditScreen extends DisplayBlockEditScreen {

private final EntityDisplayBlockEntity entityDisplay;
private final Set<String> expandedParts = new HashSet<>();
private boolean expanded = false;
private int STARTING_X = 10;
private int STARTING_Y = 200;
private int scrollOffset = 0;
private int maxScroll = 0;


public EntityDisplayEditScreen(DisplayBlockEntity displayBlock) {
super(displayBlock);
this.entityDisplay = (EntityDisplayBlockEntity) displayBlock;
}

@Override
public void init() {
super.init();

this.addRenderableWidget(Button.builder(
Component.literal(this.expanded ? "▼ " : "▶ ").append(Component.translatable("gui.glowcase.model_parts")),
_ -> {
this.expanded = !this.expanded;
this.rebuildWidgets();
}
).bounds(STARTING_X, STARTING_Y, 200, 20).build());

int bottom = STARTING_Y + 22;
if(this.entityDisplay.getDisplayEntity() != null) {
EntityType<?> type = this.entityDisplay.getDisplayEntity().getType();
ModelPart root = ModelPartUtil.getModelRoot(type);
if (this.expanded && root != null) {
bottom = this.addPartRows(root);
}
}

this.maxScroll = Math.max(0, bottom + 10 - this.height);
this.scrollOffset = Mth.clamp(this.scrollOffset, 0, this.maxScroll);
this.offsetWidgets(-this.scrollOffset);


}

private int addPartRows(ModelPart parent) {
int y = STARTING_Y + 22;
for (Map.Entry<String, ModelPart> entry : parent.children.entrySet()) {
String name = entry.getKey();
boolean partExpanded = this.expandedParts.contains(name);
String displayName = name.replace("_", " ");
displayName = displayName.substring(0, 1).toUpperCase() + displayName.substring(1);

this.addRenderableWidget(Button.builder(Component.literal((partExpanded ? "▼ " : "▶ ") + displayName), _ -> {
if (!this.expandedParts.remove(name)) this.expandedParts.add(name);
this.rebuildWidgets();
}).bounds(STARTING_X + 10, y, 190, 20).build());

y += 22;
if (!partExpanded) continue;

for (int i = 0; i <= 1; i++) {
boolean isRotation = i == 0;
Component label = Component.translatable(isRotation ? "gui.glowcase.rotation" : "gui.glowcase.offset");
this.addRenderableWidget(new StringWidget(STARTING_X + 20, y + 5, this.font.width(label), 9, label, this.font));

float step = isRotation ? 5F : 0.25F;
for (int axis = 0; axis < 3; axis++) {
int column = STARTING_X + 70 + axis * 84;
int a = axis;
PartPoseInfo partPoseInfo = this.entityDisplay.getPartPostInfo(name);

EditBox field = new EditBox(this.font, column, y, 44, 18, Component.empty());
field.setValue(String.valueOf((isRotation ? partPoseInfo.rotation() : partPoseInfo.offset()).get(a)));
field.setResponder(string -> {
if (Floats.tryParse(string) instanceof Float parsed) {
if(isRotation) {
entityDisplay.setPartPoseInfo(name, entityDisplay.getPartPostInfo(name).updateRotationAtAxis(a,parsed));
}
else {
entityDisplay.setPartPoseInfo(name, entityDisplay.getPartPostInfo(name).updateOffsetAtAxis(a,parsed));
}
editDisplayBlock();
}
});

this.addRenderableWidget(field);
this.addRenderableWidget(Button.builder(Component.literal("-"), _ -> {
PartPoseInfo newValue = this.entityDisplay.getPartPostInfo(name);
if(isRotation) {
newValue = newValue.updateRotationAtAxis(a,newValue.getRotationAtAxis(a)-step);
this.entityDisplay.setPartPoseInfo(name,newValue);
}
else {
newValue = newValue.updateOffsetAtAxis(a,newValue.getOffsetAtAxis(a)-step);
this.entityDisplay.setPartPoseInfo(name,newValue);
}
editDisplayBlock();
field.setValue(String.valueOf((isRotation ? newValue.getRotationAtAxis(a) : newValue.getOffsetAtAxis(a))));
}).bounds(column + 46, y, 18, 18).build());
this.addRenderableWidget(Button.builder(Component.literal("+"), _ -> {
PartPoseInfo newValue = this.entityDisplay.getPartPostInfo(name);
if(isRotation) {
newValue = newValue.updateRotationAtAxis(a,newValue.getRotationAtAxis(a)+step);
this.entityDisplay.setPartPoseInfo(name,newValue);
}
else {
newValue = newValue.updateOffsetAtAxis(a,newValue.getOffsetAtAxis(a)+step);
this.entityDisplay.setPartPoseInfo(name,newValue);
}
editDisplayBlock();
field.setValue(String.valueOf((isRotation ? newValue.getRotationAtAxis(a) : newValue.getOffsetAtAxis(a))));
}).bounds(column + 64, y, 18, 18).build());
}
y += 22;
}
}
return y;

}

@Override
public boolean mouseScrolled(double x, double y, double scrollX, double scrollY) {
int newOffset = Mth.clamp(this.scrollOffset - (int) (scrollY * 20), 0, this.maxScroll);
if (newOffset != this.scrollOffset) {
this.offsetWidgets(this.scrollOffset - newOffset);
this.scrollOffset = newOffset;
return true;
}
return super.mouseScrolled(x, y, scrollX, scrollY);
}

private void offsetWidgets(int scrollOffset) {
for (GuiEventListener child : this.children()) {
if (child instanceof AbstractWidget widget) widget.setY(widget.getY() + scrollOffset);
}
}

@Override
public CustomPacketPayload getUpdatePayload() {
return C2SEditEntityDisplayBlock.of(blockEntity);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import dev.hephaestus.glowcase.Glowcase;
import dev.hephaestus.glowcase.block.entity.EntityDisplayBlockEntity;
import dev.hephaestus.glowcase.client.util.BlockEntityRenderUtil;
import dev.hephaestus.glowcase.mixinsupport.PartPosedRenderState;
import dev.hephaestus.glowcase.util.PartPoseInfo;
import net.minecraft.client.renderer.SubmitNodeCollector;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
Expand All @@ -19,6 +21,8 @@
import org.joml.Vector3f;
import org.jspecify.annotations.Nullable;

import java.util.Map;

public record EntityDisplayBlockEntityRenderer(BlockEntityRendererProvider.Context context) implements BlockEntityRenderer<EntityDisplayBlockEntity, EntityDisplayBlockEntityRenderer.EntityDisplayRenderState> {
public static Identifier ITEM_TEXTURE = Glowcase.id("textures/item/entity_display_block.png");

Expand All @@ -30,6 +34,8 @@ public static class EntityDisplayRenderState extends BlockEntityRenderState {
public Vector3f scale;

public Entity entity;

public Map<String, PartPoseInfo> partPoseInfoMap;
}

@Override
Expand All @@ -47,6 +53,7 @@ public void extractRenderState(EntityDisplayBlockEntity blockEntity, EntityDispl
// TODO: hmm, yes, shuttle the *entire entity* to the render state
// Perhaps shuttle the entity renderer & its state instead?
state.entity = blockEntity.getDisplayEntity();
state.partPoseInfoMap = blockEntity.getPartPoses();
}

@Override
Expand All @@ -62,6 +69,7 @@ public void submit(EntityDisplayRenderState state, PoseStack poseStack, SubmitNo
//noinspection unchecked
EntityRenderer<Entity, EntityRenderState> entityRenderer = (EntityRenderer<Entity, EntityRenderState>) context.entityRenderer().getRenderer(renderEntity);
EntityRenderState entityRenderState = entityRenderer.createRenderState(renderEntity, 0);
((PartPosedRenderState) entityRenderState).glowcase$setPartPoses(state.partPoseInfoMap.isEmpty() ? null : state.partPoseInfoMap);
entityRenderer.submit(entityRenderState, poseStack, submitNodeCollector, camera);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package dev.hephaestus.glowcase.client.util;

import dev.hephaestus.glowcase.mixinsupport.PartPosedRenderState;
import dev.hephaestus.glowcase.util.PartPoseInfo;
import net.minecraft.client.Minecraft;
import net.minecraft.client.model.Model;
import net.minecraft.client.model.geom.ModelPart;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.renderer.entity.EntityRenderer;
import net.minecraft.client.renderer.entity.LivingEntityRenderer;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntitySpawnReason;
import net.minecraft.world.entity.EntityType;
import org.jetbrains.annotations.Nullable;
import org.joml.Vector3fc;

import java.util.Map;

public class ModelPartUtil {

@Nullable
public static ModelPart getModelRoot(EntityType<?> type) {
Minecraft minecraft = Minecraft.getInstance();
ClientLevel level = minecraft.level;
if (level == null) return null;

Entity entity = type.create(level, EntitySpawnReason.EVENT);
if (entity == null) return null;

EntityRenderer<?, ?> renderer = minecraft.getEntityRenderDispatcher().getRenderer(entity);
if (renderer instanceof LivingEntityRenderer<?, ?, ?> living) {
return living.getModel().root();
}
return null;
}

public static void applyPartPoses(Model<?> model, Object state) {
if (!(state instanceof PartPosedRenderState posed)) {
return;
}
Map<String, PartPoseInfo> partPoses = posed.glowcase$getPartPoses();
if (partPoses == null) {
return;
}

Map<String, ModelPart> children = model.root().children;
for (PartPoseInfo partPoseInfo : partPoses.values()) {
ModelPart part = children.get(partPoseInfo.name());
if (part == null) {
continue;
}

Vector3fc offset = partPoseInfo.offset();
Vector3fc rotation = partPoseInfo.rotation();
part.x += offset.x();
part.y += offset.y();
part.z += offset.z();
part.xRot += rotation.x() * Mth.DEG_TO_RAD;
part.yRot += rotation.y() * Mth.DEG_TO_RAD;
part.zRot += rotation.z() * Mth.DEG_TO_RAD;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package dev.hephaestus.glowcase.mixin.client;

import dev.hephaestus.glowcase.mixinsupport.PartPosedRenderState;
import dev.hephaestus.glowcase.util.PartPoseInfo;
import net.minecraft.client.renderer.entity.state.EntityRenderState;
import org.jspecify.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;

import java.util.Map;

@Mixin(EntityRenderState.class)
public class EntityRenderStateMixin implements PartPosedRenderState {
@Unique private @Nullable Map<String, PartPoseInfo> glowcase$partPoses;

@Override
public void glowcase$setPartPoses(@Nullable Map<String, PartPoseInfo> partPoses) {
this.glowcase$partPoses = partPoses;
}

@Override
public @Nullable Map<String, PartPoseInfo> glowcase$getPartPoses() {
return glowcase$partPoses;
}
}
Loading
Loading