Skip to content

Commit

Permalink
Rebdering
Browse files Browse the repository at this point in the history
  • Loading branch information
enjarai committed Sep 20, 2024
1 parent bc955d9 commit 2b57aa2
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 8 deletions.
13 changes: 5 additions & 8 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 @@ -25,14 +22,11 @@
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.ColorProviderRegistry;
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.gui.tooltip.TooltipComponent;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.render.block.entity.BlockEntityRendererFactories;
import net.minecraft.component.type.DyedColorComponent;
import net.minecraft.util.math.ColorHelper;

public class TricksterClient implements ClientModInitializer {
@Override
Expand All @@ -46,6 +40,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 All @@ -72,5 +67,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,70 @@
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.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.util.Identifier;
import net.minecraft.util.math.RotationAxis;

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(16f / GRID_WIDTH * x - 1.5f, 16f - (16f / GRID_HEIGHT * y - 1.5f), 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.multiply(RotationAxis.NEGATIVE_Y.rotationDegrees(facing.asRotation()));

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);
}
}

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
Expand Up @@ -11,9 +11,13 @@
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.listener.ClientPlayPacketListener;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.math.BlockPos;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;

Expand All @@ -37,9 +41,21 @@ protected void writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryL
Inventories.writeNbt(nbt, this.inventory, true, registryLookup);
}

@Nullable
@Override
public Packet<ClientPlayPacketListener> toUpdatePacket() {
return BlockEntityUpdateS2CPacket.create(this);
}

@Override
public NbtCompound toInitialChunkDataNbt(RegistryWrapper.WrapperLookup registryLookup) {
return createNbt(registryLookup);
}

@Override
public void clear() {
this.inventory.clear();
markDirty();
}

@Override
Expand All @@ -61,6 +77,7 @@ public ItemStack getStack(int slot) {
public ItemStack removeStack(int slot, int amount) {
ItemStack itemStack = Objects.requireNonNullElse(this.inventory.get(slot), ItemStack.EMPTY);
this.inventory.set(slot, ItemStack.EMPTY);
markDirty();

return itemStack;
}
Expand All @@ -74,6 +91,7 @@ public ItemStack removeStack(int slot) {
public void setStack(int slot, ItemStack stack) {
if (stack.isIn(ModItems.SCROLLS)) {
this.inventory.set(slot, stack);
markDirty();
} else if (stack.isEmpty()) {
this.removeStack(slot, 1);
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/resources/assets/trickster/atlases/scroll_shelf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"sources": [
{
"type": "directory",
"source": "entity/scroll_shelf",
"prefix": "entity/scroll_shelf/"
}
]
}
1 change: 1 addition & 0 deletions src/main/resources/assets/trickster/lang/en_us.yml
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ block.trickster:
spell_resonator: Spell Resonator
spell_circle: Spell Circle
light: Light
scroll_shelf: Scroll Shelf

effect.trickster:
mana_boost: Mana Hyperflux
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2b57aa2

Please sign in to comment.