Skip to content

Commit

Permalink
Merge pull request #300 from orbyfiedistrash/feature/airplace
Browse files Browse the repository at this point in the history
Fixed crash in reflection for AirPlace outline, now uses mixin accessors
  • Loading branch information
Matthias1590 authored Jun 24, 2023
2 parents 2d67550 + 42ac995 commit b343490
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
import net.minecraft.client.render.Camera;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.render.VertexConsumer;
import net.minecraft.client.render.WorldRenderer;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
Expand All @@ -24,32 +21,16 @@
import tools.redstone.redstonetools.features.Feature;
import tools.redstone.redstonetools.features.arguments.Argument;
import tools.redstone.redstonetools.features.arguments.serializers.BoolSerializer;
import tools.redstone.redstonetools.mixin.accessors.WorldRendererAccessor;
import tools.redstone.redstonetools.utils.ItemUtils;
import tools.redstone.redstonetools.utils.RaycastUtils;
import tools.redstone.redstonetools.utils.ReflectionUtils;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodType;

import static tools.redstone.redstonetools.features.arguments.serializers.FloatSerializer.floatArg;

@AutoService(AbstractFeature.class)
@Feature(name = "Air Place", description = "Allows you to place blocks in the air.", command = "airplace")
public class AirPlaceFeature extends ToggleableFeature {

static MethodHandle methodDrawBlockOutline;

static {
try {
methodDrawBlockOutline = ReflectionUtils.getInternalLookup()
.findVirtual(WorldRenderer.class, "drawBlockOutline",
MethodType.methodType(void.class, MatrixStack.class, VertexConsumer.class, Entity.class,
double.class, double.class, double.class, BlockPos.class, BlockState.class));
} catch (Exception e) {
e.printStackTrace();
}
}

public static boolean canAirPlace(PlayerEntity player) {
ItemStack itemStack = ItemUtils.getMainItem(player);

Expand Down Expand Up @@ -124,11 +105,11 @@ public static BlockHitResult findAirPlaceBlockHit(PlayerEntity playerEntity) {

try {
VertexConsumer consumer = context.consumers().getBuffer(RenderLayer.getLines());
methodDrawBlockOutline.invoke(
context.worldRenderer(),

((WorldRendererAccessor)context.worldRenderer()).invokeDrawBlockOutline(
context.matrixStack(),
consumer,
(Entity)client.player,
client.player,
camPos.x, camPos.y, camPos.z,
blockPos,
blockState
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

@Mixin(MinecraftClient.class)
public interface MinecraftClientAccessor {

@Invoker
ItemStack invokeAddBlockEntityNbt(ItemStack stack, BlockEntity blockEntity);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package tools.redstone.redstonetools.mixin.accessors;

import net.minecraft.block.BlockState;
import net.minecraft.client.render.VertexConsumer;
import net.minecraft.client.render.WorldRenderer;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.Entity;
import net.minecraft.util.math.BlockPos;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;

@Mixin(WorldRenderer.class)
public interface WorldRendererAccessor {

@Invoker
void invokeDrawBlockOutline(MatrixStack matrices, VertexConsumer vertexConsumer, Entity entity, double d, double e, double f, BlockPos pos, BlockState state);

}
1 change: 1 addition & 0 deletions src/main/resources/redstonetools.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
],
"client": [
"accessors.MinecraftClientAccessor",
"accessors.WorldRendererAccessor",
"features.AirPlaceClientMixin",
"features.ItemBindMixin$PlayerMixin",
"macros.AddMacroButtonMixin",
Expand Down

0 comments on commit b343490

Please sign in to comment.