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

Scroll shelves #58

Merged
merged 9 commits into from
Sep 22, 2024
Merged
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
9 changes: 5 additions & 4 deletions src/client/java/dev/enjarai/trickster/TricksterClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
import dev.enjarai.trickster.particle.ModParticles;
import dev.enjarai.trickster.particle.ProtectedBlockParticle;
import dev.enjarai.trickster.particle.SpellParticle;
import dev.enjarai.trickster.render.BarsRenderer;
import dev.enjarai.trickster.render.CircleErrorRenderer;
import dev.enjarai.trickster.render.HoldableHatRenderer;
import dev.enjarai.trickster.render.SpellCircleBlockEntityRenderer;
import dev.enjarai.trickster.render.*;
import dev.enjarai.trickster.screen.ModHandledScreens;
import dev.enjarai.trickster.screen.ScrollAndQuillScreen;
import dev.enjarai.trickster.screen.SignScrollScreen;
Expand All @@ -26,6 +23,7 @@
import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.particle.v1.ParticleFactoryRegistry;
import net.fabricmc.fabric.api.client.rendering.v1.EntityModelLayerRegistry;
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.RenderLayer;
Expand All @@ -43,6 +41,7 @@ public void onInitializeClient() {
ModClientNetworking.register();

BlockEntityRendererFactories.register(ModBlocks.SPELL_CIRCLE_ENTITY, SpellCircleBlockEntityRenderer::new);
BlockEntityRendererFactories.register(ModBlocks.SCROLL_SHELF_ENTITY, ScrollShelfBlockEntityRenderer::new);

UIParsing.registerFactory(Trickster.id("glyph"), GlyphComponent::parseTrick);
UIParsing.registerFactory(Trickster.id("pattern"), GlyphComponent::parseList);
Expand Down Expand Up @@ -70,5 +69,7 @@ public void onInitializeClient() {

HudRenderCallback.EVENT.register(BarsRenderer::render);
HudRenderCallback.EVENT.register(CircleErrorRenderer::render);

EntityModelLayerRegistry.registerModelLayer(ScrollShelfBlockEntityRenderer.MODEL_LAYER, ScrollShelfBlockEntityRenderer::getTexturedModelData);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package dev.enjarai.trickster.mixin.client;

import dev.enjarai.trickster.Trickster;
import dev.enjarai.trickster.render.ScrollShelfBlockEntityRenderer;
import net.minecraft.client.render.model.BakedModelManager;
import net.minecraft.util.Identifier;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

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

@Mixin(BakedModelManager.class)
public class BakedModelManagerMixin {
@Final
@Shadow @Mutable
private static Map<Identifier, Identifier> LAYERS_TO_LOADERS;

@Inject(
method = "<clinit>",
at = @At("TAIL")
)
private static void appendToAtlases(CallbackInfo ci) {
LAYERS_TO_LOADERS = new HashMap<>(LAYERS_TO_LOADERS);
LAYERS_TO_LOADERS.put(ScrollShelfBlockEntityRenderer.ATLAS_ID, Trickster.id("scroll_shelf"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package dev.enjarai.trickster.render;

import dev.enjarai.trickster.Trickster;
import dev.enjarai.trickster.block.ScrollShelfBlock;
import dev.enjarai.trickster.block.ScrollShelfBlockEntity;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.model.*;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.WorldRenderer;
import net.minecraft.client.render.block.entity.BlockEntityRenderer;
import net.minecraft.client.render.block.entity.BlockEntityRendererFactory;
import net.minecraft.client.render.entity.model.EntityModelLayer;
import net.minecraft.client.util.SpriteIdentifier;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.registry.Registries;
import net.minecraft.text.Text;
import net.minecraft.util.Colors;
import net.minecraft.util.Identifier;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.RotationAxis;
import org.joml.Matrix4f;

import static dev.enjarai.trickster.block.ScrollShelfBlock.GRID_HEIGHT;
import static dev.enjarai.trickster.block.ScrollShelfBlock.GRID_WIDTH;

public class ScrollShelfBlockEntityRenderer implements BlockEntityRenderer<ScrollShelfBlockEntity> {
public static final EntityModelLayer MODEL_LAYER = new EntityModelLayer(Trickster.id("scroll_shelf"), "scroll_shelf");
public static final Identifier ATLAS_ID = Trickster.id("textures/atlas/scroll_shelf.png");

private final ModelPart[] scrollModels = new ModelPart[GRID_WIDTH * GRID_HEIGHT];

public static TexturedModelData getTexturedModelData() {
ModelData modelData = new ModelData();
ModelPartData modelPartData = modelData.getRoot();
for (int i = 0; i < GRID_WIDTH * GRID_HEIGHT; i++) {
var x = i % GRID_WIDTH;
var y = i / GRID_HEIGHT;
modelPartData.addChild("scroll_" + i, ModelPartBuilder.create()
.uv(0, 0)
.cuboid(15f / GRID_WIDTH * x + 1.5f, 15f / GRID_HEIGHT * y + 1f, 16f, 3f, 3f, 1f),
ModelTransform.NONE);
}
return TexturedModelData.of(modelData, 16, 16);
}

public ScrollShelfBlockEntityRenderer(BlockEntityRendererFactory.Context ctx) {
var model = ctx.getLayerModelPart(MODEL_LAYER);
for (int i = 0; i < GRID_WIDTH * GRID_HEIGHT; i++) {
scrollModels[i] = model.getChild("scroll_" + i);
}
}

@Override
public void render(ScrollShelfBlockEntity entity, float tickDelta, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, int overlay) {
var facing = entity.getCachedState().get(ScrollShelfBlock.FACING);
//noinspection DataFlowIssue
light = WorldRenderer.getLightmapCoordinates(entity.getWorld(), entity.getPos().offset(facing));

matrices.push();
matrices.translate(0.5f, 0, 0.5f);
matrices.multiply(RotationAxis.NEGATIVE_Y.rotationDegrees(facing.asRotation()));
matrices.translate(-0.5f, 0, -0.5f);

for (int i = 0; i < GRID_WIDTH * GRID_HEIGHT; i++) {
var stack = entity.getStack(i);
if (!stack.isEmpty()) {
var textureId = Registries.ITEM.getId(stack.getItem()).withPrefixedPath("entity/scroll_shelf/");
var spriteId = new SpriteIdentifier(ATLAS_ID, textureId);
var vertexConsumer = spriteId.getVertexConsumer(vertexConsumers, RenderLayer::getEntityCutout);
scrollModels[i].render(matrices, vertexConsumer, light, overlay);
}
}

if (MinecraftClient.getInstance().crosshairTarget instanceof BlockHitResult blockHit
&& blockHit.getBlockPos().equals(entity.getPos())
&& blockHit.getSide() == entity.getCachedState().get(ScrollShelfBlock.FACING)) {
var slot = ScrollShelfBlock.getSlotForHitPos(blockHit, entity.getCachedState());
if (slot.isPresent()) {
var stack = entity.getStack(slot.getAsInt());
if (!stack.isEmpty()) {
renderLabel(entity, tickDelta, matrices, vertexConsumers, light, slot.getAsInt(), stack.getName());
}
}
}

matrices.pop();
}

public void renderLabel(ScrollShelfBlockEntity entity, float tickDelta, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, int slot, Text name) {
matrices.push();
matrices.translate(0, 0, 1.1f);

var pixel = 1f / 16f;
var x = pixel * 15f / GRID_WIDTH * (slot % GRID_WIDTH) + pixel * 3f;
//noinspection IntegerDivisionInFloatingPointContext
var y = pixel * 15f / GRID_HEIGHT * (slot / GRID_HEIGHT) + pixel * 7f;
matrices.translate(x, y, 0);

matrices.scale(0.018F, -0.018F, 0.018F);

Matrix4f matrix4f = matrices.peek().getPositionMatrix();
MinecraftClient client = MinecraftClient.getInstance();
float opacity = client.options.getTextBackgroundOpacity(0.25F);
int backgroundColor = (int) (opacity * 255.0F) << 24;
TextRenderer textRenderer = client.textRenderer;
float offsetX = (float) (-textRenderer.getWidth(name) / 2);

// textRenderer.draw(name, offsetX, 0, 553648127, false, matrix4f, vertexConsumers, TextRenderer.TextLayerType.NORMAL, backgroundColor, light);
textRenderer.draw(name, offsetX, 0, Colors.WHITE, false, matrix4f, vertexConsumers, TextRenderer.TextLayerType.NORMAL, 0, light);

matrices.pop();
}
}
1 change: 1 addition & 0 deletions src/client/resources/trickster.client.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"plugin": "dev.enjarai.trickster.ConditionalMixins",
"client": [
"AbstractClientPlayerEntityMixin",
"BakedModelManagerMixin",
"HeldItemRendererMixin",
"InGameHudMixin",
"InventoryScreenMixin",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 1.21 2024-09-20T00:21:28.1285918 Trickster/Model Definitions
// 1.21 2024-09-21T01:03:48.6679756 Trickster/Model Definitions
0c24009d9f713f084ff55efcb5aedef3924fc3bf assets\trickster\models\item\written_scroll_white.json
96809df525387fe221827afd762b90062300440f assets\trickster\models\item\scroll_and_quill_black.json
f00b04573fa8a37423acbd2cfa58995b577acc9f assets\trickster\models\item\written_scroll_light_gray.json
Expand All @@ -12,8 +12,10 @@ b2b1aaf26ce09fb4f7de2fbde84e56f568bc247d assets\trickster\models\item\scroll_and
731c4a8f7b5d2845410c03b22e2521bcf41f969c assets\trickster\models\item\scroll_and_quill_red.json
1f778e1f2c30077615a7ea8b89c6da45b5c0631f assets\trickster\models\item\written_scroll_light_blue.json
ad5d09f6b76c65ada5383c567922cd74524f968c assets\trickster\models\item\written_scroll_red.json
032bd4c0ea04699e58ffbf43c29d1bf96fa19eb5 assets\trickster\models\block\scroll_shelf.json
4b75bcb18c8f3e6ed30eaf767410a836b1be57d6 assets\trickster\models\item\written_scroll_magenta.json
0af8ae55bca54e5baea1b840c12340690af85f5c assets\trickster\models\item\written_scroll_green.json
8457e5235482b7f547386747d1f8fb2123926fdf assets\trickster\blockstates\scroll_shelf.json
11f88e31ee80e48a4b56df7363c1dfd36659bd76 assets\trickster\models\item\scroll_and_quill_yellow.json
25fc9665ae52d7788aa19923c04ba3874290a007 assets\trickster\models\item\scroll_and_quill_orange.json
ce371fd6383c178fd713cbe03828b8015df480d8 assets\trickster\models\item\scroll_and_quill_gray.json
Expand All @@ -26,10 +28,11 @@ c29f71428dcd472e97e6574b8cb1f156ea3b9fcd assets\trickster\models\item\written_sc
9faf558d1aa0f4946cf100c91b4f2819b7a07538 assets\trickster\models\item\written_scroll_orange.json
49e029faed97e108d69b1a5339f092fa7c3ac913 assets\trickster\models\item\spell_resonator.json
9485ad9be41799d6c5bd48cd6dead8464f65beb5 assets\trickster\models\item\scroll_and_quill_magenta.json
85f44373cd91352eb424318c4f289ae4829b3f80 assets\trickster\models\item\scroll_shelf.json
06af58b66840c735a18e12df1442fa2cd83c4df4 assets\trickster\models\item\written_scroll_brown.json
2f7ac0d58a5c1dd9391452bdde2b219a584786f8 assets\trickster\models\item\written_scroll_cyan.json
c995dc518d2d7134ffe57b13897fda67a0b0f815 assets\trickster\models\item\scroll_and_quill_pink.json
03ec5d0e130b285409440674fa4adf632312325a assets\trickster\models\item\scroll_and_quill_blue.json
c995dc518d2d7134ffe57b13897fda67a0b0f815 assets\trickster\models\item\scroll_and_quill_pink.json
ac75d1dcaf4df0990b246b571b214c88c2043fc5 assets\trickster\models\item\written_scroll_blue.json
ce15632fcb3beb33453bccd6c7cc398b86834d85 assets\trickster\models\item\written_scroll_lime.json
575f98c294e9b0778c0bccf43bf742ffb333b925 assets\trickster\models\item\scroll_and_quill_light_blue.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
// 1.21 2024-09-20T00:21:28.1305908 Trickster/Recipes
// 1.21 2024-09-21T01:03:48.6699743 Trickster/Recipes
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// 1.21 2024-09-20T00:21:28.131593 Trickster/Tags for minecraft:item
// 1.21 2024-09-21T01:03:48.6699743 Trickster/Tags for minecraft:item
af8a8cadecd9999efde2ab4a363a6ac6a933d02b data\trickster\tags\item\dyed_written_scroll.json
23bfb5af198cc6efbf9fdb01f4169f9c6df07968 data\trickster\tags\item\dyed_scroll_and_quill.json
19 changes: 19 additions & 0 deletions src/main/generated/assets/trickster/blockstates/scroll_shelf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"variants": {
"facing=east": {
"model": "trickster:block/scroll_shelf",
"y": 90
},
"facing=north": {
"model": "trickster:block/scroll_shelf"
},
"facing=south": {
"model": "trickster:block/scroll_shelf",
"y": 180
},
"facing=west": {
"model": "trickster:block/scroll_shelf",
"y": 270
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"parent": "minecraft:block/orientable_with_bottom",
"textures": {
"bottom": "trickster:block/scroll_shelf_bottom",
"front": "trickster:block/scroll_shelf_front",
"side": "trickster:block/scroll_shelf_side",
"top": "trickster:block/scroll_shelf_top"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"parent": "trickster:block/scroll_shelf"
}
4 changes: 4 additions & 0 deletions src/main/java/dev/enjarai/trickster/block/ModBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ public class ModBlocks {
public static final SpellResonatorBlock SPELL_RESONATOR = register("spell_resonator", new SpellResonatorBlock());
public static final SpellCircleBlock SPELL_CIRCLE = register("spell_circle", new SpellCircleBlock());
public static final LightBlock LIGHT = register("light", new LightBlock());
public static final ScrollShelfBlock SCROLL_SHELF = register("scroll_shelf", new ScrollShelfBlock());

public static final BlockEntityType<SpellCircleBlockEntity> SPELL_CIRCLE_ENTITY =
BlockEntityType.Builder.create(SpellCircleBlockEntity::new, SPELL_CIRCLE).build(null);
public static final BlockEntityType<LightBlockEntity> LIGHT_ENTITY =
BlockEntityType.Builder.create(LightBlockEntity::new, LIGHT).build(null);
public static final BlockEntityType<ScrollShelfBlockEntity> SCROLL_SHELF_ENTITY =
BlockEntityType.Builder.create(ScrollShelfBlockEntity::new, SCROLL_SHELF).build(null);

public static final PointOfInterestType SPELL_CIRCLE_POI =
PointOfInterestHelper.register(Trickster.id("spell_circle"), 0, 2, SPELL_CIRCLE);
Expand All @@ -36,6 +39,7 @@ private static <T extends Block> T register(String name, T block) {
public static void register() {
Registry.register(Registries.BLOCK_ENTITY_TYPE, Trickster.id("spell_circle"), SPELL_CIRCLE_ENTITY);
Registry.register(Registries.BLOCK_ENTITY_TYPE, Trickster.id("light"), LIGHT_ENTITY);
Registry.register(Registries.BLOCK_ENTITY_TYPE, Trickster.id("scroll_shelf"), SCROLL_SHELF_ENTITY);

var cauldronMap = CauldronBehavior.WATER_CAULDRON_BEHAVIOR.map();
cauldronMap.put(ModItems.WRITTEN_SCROLL, new EraseSpellCauldronBehavior());
Expand Down
Loading