Skip to content

Commit

Permalink
Fix Vanilla Xray
Browse files Browse the repository at this point in the history
  • Loading branch information
MineGame159 committed Jun 27, 2024
1 parent 120dac5 commit cff4cf5
Showing 1 changed file with 7 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,22 @@
import net.minecraft.client.render.VertexConsumer;
import net.minecraft.client.render.block.BlockModelRenderer;
import net.minecraft.client.render.model.BakedModel;
import net.minecraft.client.render.model.BakedQuad;
import net.minecraft.client.util.BufferAllocator;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.random.Random;
import net.minecraft.world.BlockRenderView;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Constant;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyConstant;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.nio.ByteBuffer;

@Mixin(BlockModelRenderer.class)
public abstract class BlockModelRendererMixin {
@Unique private final ThreadLocal<Integer> alphas = new ThreadLocal<>();
@Unique
private final ThreadLocal<Integer> alphas = new ThreadLocal<>();

@Inject(method = "render(Lnet/minecraft/world/BlockRenderView;Lnet/minecraft/client/render/model/BakedModel;Lnet/minecraft/block/BlockState;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumer;ZLnet/minecraft/util/math/random/Random;JI)V", at = @At("HEAD"), cancellable = true)
private void onRender(BlockRenderView world, BakedModel model, BlockState state, BlockPos pos, MatrixStack matrices, VertexConsumer vertexConsumer, boolean cull, Random random, long seed, int overlay, CallbackInfo info) {
Expand All @@ -36,24 +35,9 @@ private void onRender(BlockRenderView world, BakedModel model, BlockState state,
else alphas.set(alpha);
}

@Inject(method = "renderQuad(Lnet/minecraft/world/BlockRenderView;Lnet/minecraft/block/BlockState;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/client/render/VertexConsumer;Lnet/minecraft/client/util/math/MatrixStack$Entry;Lnet/minecraft/client/render/model/BakedQuad;FFFFIIIII)V", at = @At("TAIL"))
private void onRenderQuad(BlockRenderView world, BlockState state, BlockPos pos, VertexConsumer vertexConsumer, MatrixStack.Entry matrixEntry, BakedQuad quad, float brightness0, float brightness1, float brightness2, float brightness3, int light0, int light1, int light2, int light3, int overlay, CallbackInfo ci) {
@ModifyConstant(method = "renderQuad", constant = @Constant(floatValue = 1, ordinal = 3))
private float renderQuad_modifyAlpha(float original) {
int alpha = alphas.get();
if (alpha != -1) rewriteBuffer(vertexConsumer, alpha);
}

@Unique
private void rewriteBuffer(VertexConsumer vertexConsumer, int alpha) {
if (vertexConsumer instanceof BufferBuilderAccessor bufferBuilder) {
int i = bufferBuilder.getVertexFormat().getVertexSizeByte();
try (BufferAllocator.CloseableBuffer allocatedBuffer = bufferBuilder.meteor$getAllocator().getAllocated()) {
if (allocatedBuffer != null) {
ByteBuffer buffer = allocatedBuffer.getBuffer();
for (int l = 1; l <= 4; l++) {
buffer.put(buffer.capacity() - i * l + 15, (byte) (alpha));
}
}
}
}
return alpha == -1 ? original : alpha / 255f;
}
}

0 comments on commit cff4cf5

Please sign in to comment.