Skip to content

Commit

Permalink
Finish dynamic rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
62832 committed Oct 20, 2023
1 parent a93b808 commit c8e2825
Showing 1 changed file with 53 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.core.Direction;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.RandomSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
Expand Down Expand Up @@ -65,23 +68,31 @@ public class CellDockPart extends AEBasePart

private static final VarHandle LED_RENDER_TYPE;
private static final MethodHandle LED_RENDER;
private static final MethodHandle CELL_MODEL;

static {
try {
var lookup = MethodHandles.lookup();

var cellLedRenderer = Class.forName("appeng.client.render.tesr.CellLedRenderer");
var lookup = MethodHandles.privateLookupIn(cellLedRenderer, MethodHandles.lookup());

LED_RENDER_TYPE = lookup.findStaticVarHandle(cellLedRenderer, "RENDER_LAYER", RenderType.class);
LED_RENDER = lookup.findStatic(
cellLedRenderer,
"renderLed",
MethodType.methodType(
void.class,
IChestOrDrive.class,
int.class,
VertexConsumer.class,
PoseStack.class,
float.class));
LED_RENDER_TYPE = MethodHandles.privateLookupIn(cellLedRenderer, lookup)
.findStaticVarHandle(cellLedRenderer, "RENDER_LAYER", RenderType.class);
LED_RENDER = MethodHandles.privateLookupIn(cellLedRenderer, lookup)
.findStatic(
cellLedRenderer,
"renderLed",
MethodType.methodType(
void.class,
IChestOrDrive.class,
int.class,
VertexConsumer.class,
PoseStack.class,
float.class));

var cellModel = Class.forName("appeng.client.render.tesr.ChestBlockEntityRenderer$FaceRotatingModel");
CELL_MODEL = MethodHandles.privateLookupIn(cellModel, lookup)
.findConstructor(
cellModel, MethodType.methodType(void.class, BakedModel.class, BlockOrientation.class));
} catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException | NoSuchMethodException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -265,6 +276,7 @@ public void mountInventories(IStorageMounts storageMounts) {
@Override
public void saveChanges() {
getHost().markForSave();
getHost().markForUpdate();
}

@Override
Expand All @@ -275,7 +287,6 @@ public void onChangeInventory(InternalInventory inv, int slot) {
}

IStorageProvider.requestUpdate(getMainNode());
getHost().markForSave();
}

private void updateState() {
Expand Down Expand Up @@ -311,8 +322,9 @@ public int getPriority() {
@Override
public void setPriority(int newValue) {
priority = newValue;
getHost().markForSave();

isCached = false;
saveChanges();
updateState();

IStorageProvider.requestUpdate(getMainNode());
Expand Down Expand Up @@ -341,7 +353,7 @@ public IPartModel getStaticModels() {

@Override
public boolean requireDynamicRender() {
return clientCell != Items.AIR;
return true;
}

@Override
Expand All @@ -351,7 +363,10 @@ public void renderDynamic(
MultiBufferSource buffers,
int combinedLightIn,
int combinedOverlayIn) {
// TODO
if (getLevel() == null || clientCell == Items.AIR) {
return;
}

var driveModel = Minecraft.getInstance()
.getModelManager()
.getBlockModelShaper()
Expand All @@ -362,11 +377,30 @@ public void renderDynamic(
poseStack.pushPose();
poseStack.translate(0.5, 0.5, 0.5);

var orientation = BlockOrientation.get(getSide());
var front = getSide() == Direction.UP || getSide() == Direction.DOWN ? Direction.NORTH : Direction.UP;
var orientation = BlockOrientation.get(front, getSide());

poseStack.mulPose(orientation.getQuaternion());
poseStack.translate(-0.5, -0.5, -0.5);
poseStack.translate(-3.0 / 16, 5.0 / 16, -5.0 / 16);

try {
var cellBuffer = buffers.getBuffer(RenderType.cutout());
var cell = CELL_MODEL.invoke(cellModel, orientation);
Minecraft.getInstance()
.getBlockRenderer()
.getModelRenderer()
.tesselateBlock(
getLevel(),
(BakedModel) cell,
getBlockEntity().getBlockState(),
getBlockEntity().getBlockPos(),
poseStack,
cellBuffer,
false,
RandomSource.create(),
0L,
combinedOverlayIn);

var ledBuffer = buffers.getBuffer((RenderType) LED_RENDER_TYPE.get());
LED_RENDER.invoke(this, 0, ledBuffer, poseStack, partialTicks);
} catch (Throwable e) {
Expand Down

0 comments on commit c8e2825

Please sign in to comment.