diff --git a/fabric/src/main/java/me/lambdaurora/lambdacontrols/client/LambdaControlsClient.java b/fabric/src/main/java/me/lambdaurora/lambdacontrols/client/LambdaControlsClient.java index cffe81c..a884367 100644 --- a/fabric/src/main/java/me/lambdaurora/lambdacontrols/client/LambdaControlsClient.java +++ b/fabric/src/main/java/me/lambdaurora/lambdacontrols/client/LambdaControlsClient.java @@ -186,141 +186,4 @@ public class LambdaControlsClient extends LambdaControls implements ClientModIni { return INSTANCE; } - - public static Pair drawButton(int x, int y, @NotNull ButtonBinding button, @NotNull MinecraftClient client) - { - return drawButton(x, y, button.getButton(), client); - } - - public static Pair drawButton(int x, int y, int[] buttons, @NotNull MinecraftClient client) - { - int height = 0; - int length = 0; - int currentX = x; - for (int i = 0; i < buttons.length; i++) { - int btn = buttons[i]; - Pair size = drawButton(currentX, y, btn, client); - if (size.key > height) - height = size.key; - length += size.key; - if (i + 1 < buttons.length) { - length += 2; - currentX = x + length; - } - } - return Pair.of(length, height); - } - - @SuppressWarnings("deprecated") - public static Pair drawButton(int x, int y, int button, @NotNull MinecraftClient client) - { - boolean second = false; - if (button == -1) - return Pair.of(0, 0); - else if (button >= 500) { - button -= 1000; - second = true; - } - - int controllerType = get().config.getControllerType().getId(); - boolean axis = false; - int buttonOffset = button * 15; - switch (button) { - case GLFW.GLFW_GAMEPAD_BUTTON_LEFT_BUMPER: - buttonOffset = 7 * 15; - break; - case GLFW.GLFW_GAMEPAD_BUTTON_RIGHT_BUMPER: - buttonOffset = 8 * 15; - break; - case GLFW.GLFW_GAMEPAD_BUTTON_BACK: - buttonOffset = 4 * 15; - break; - case GLFW.GLFW_GAMEPAD_BUTTON_START: - buttonOffset = 6 * 15; - break; - case GLFW.GLFW_GAMEPAD_BUTTON_GUIDE: - buttonOffset = 5 * 15; - break; - case GLFW.GLFW_GAMEPAD_BUTTON_LEFT_THUMB: - buttonOffset = 15 * 15; - break; - case GLFW.GLFW_GAMEPAD_BUTTON_RIGHT_THUMB: - buttonOffset = 16 * 15; - break; - case GLFW.GLFW_GAMEPAD_AXIS_LEFT_X + 100: - buttonOffset = 0; - axis = true; - break; - case GLFW.GLFW_GAMEPAD_AXIS_LEFT_Y + 100: - buttonOffset = 18; - axis = true; - break; - case GLFW.GLFW_GAMEPAD_AXIS_RIGHT_X + 100: - buttonOffset = 2 * 18; - axis = true; - break; - case GLFW.GLFW_GAMEPAD_AXIS_RIGHT_Y + 100: - buttonOffset = 3 * 18; - axis = true; - break; - case GLFW.GLFW_GAMEPAD_AXIS_LEFT_X + 200: - buttonOffset = 4 * 18; - axis = true; - break; - case GLFW.GLFW_GAMEPAD_AXIS_LEFT_Y + 200: - buttonOffset = 5 * 18; - axis = true; - break; - case GLFW.GLFW_GAMEPAD_AXIS_RIGHT_X + 200: - buttonOffset = 6 * 18; - axis = true; - break; - case GLFW.GLFW_GAMEPAD_AXIS_RIGHT_Y + 200: - buttonOffset = 7 * 18; - axis = true; - break; - case GLFW.GLFW_GAMEPAD_AXIS_LEFT_TRIGGER + 100: - case GLFW.GLFW_GAMEPAD_AXIS_LEFT_TRIGGER + 200: - buttonOffset = 9 * 15; - break; - case GLFW.GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER + 100: - case GLFW.GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER + 200: - buttonOffset = 10 * 15; - break; - } - - client.getTextureManager().bindTexture(axis ? LambdaControlsClient.CONTROLLER_AXIS : LambdaControlsClient.CONTROLLER_BUTTONS); - GlStateManager.disableDepthTest(); - - GlStateManager.color4f(1.0F, second ? 0.0F : 1.0F, 1.0F, 1.0F); - DrawableHelper.blit(x, y, (float) buttonOffset, (float) (controllerType * (axis ? 18 : 15)), axis ? 18 : 15, axis ? 18 : 15, 256, 256); - GlStateManager.enableDepthTest(); - - return axis ? Pair.of(18, 18) : Pair.of(15, 15); - } - - public static int drawButtonTip(int x, int y, @NotNull ButtonBinding button, boolean display, @NotNull MinecraftClient client) - { - return drawButtonTip(x, y, button.getButton(), button.getTranslationKey(), display, client); - } - - public static int drawButtonTip(int x, int y, int[] button, @NotNull String action, boolean display, @NotNull MinecraftClient client) - { - if (display) { - int buttonWidth = drawButton(x, y, button, client).key; - - String translatedAction = I18n.translate(action); - int textY = (15 - client.textRenderer.fontHeight) / 2; - client.textRenderer.drawWithShadow(translatedAction, (float) (x + buttonWidth + 5), (float) (y + textY), 14737632); - - return getButtonTipWidth(translatedAction, client.textRenderer); - } - - return -10; - } - - private static int getButtonTipWidth(@NotNull String action, @NotNull TextRenderer textRenderer) - { - return 15 + 5 + textRenderer.getStringWidth(action); - } } diff --git a/fabric/src/main/java/me/lambdaurora/lambdacontrols/client/LambdaInput.java b/fabric/src/main/java/me/lambdaurora/lambdacontrols/client/LambdaInput.java index 4405aa0..2439371 100644 --- a/fabric/src/main/java/me/lambdaurora/lambdacontrols/client/LambdaInput.java +++ b/fabric/src/main/java/me/lambdaurora/lambdacontrols/client/LambdaInput.java @@ -19,6 +19,8 @@ import me.lambdaurora.lambdacontrols.client.mixin.CreativeInventoryScreenAccesso import me.lambdaurora.lambdacontrols.client.mixin.EntryListWidgetAccessor; import me.lambdaurora.lambdacontrols.client.util.ContainerScreenAccessor; import me.lambdaurora.spruceui.SpruceLabelWidget; +import net.minecraft.block.BlockState; +import net.minecraft.block.FluidBlock; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.Element; import net.minecraft.client.gui.ParentElement; @@ -35,6 +37,10 @@ import net.minecraft.client.gui.widget.AlwaysSelectedEntryListWidget; import net.minecraft.client.gui.widget.SliderWidget; import net.minecraft.container.Slot; import net.minecraft.container.SlotActionType; +import net.minecraft.util.hit.BlockHitResult; +import net.minecraft.util.hit.HitResult; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Direction; import net.minecraft.util.math.MathHelper; import org.aperlambda.lambdacommon.utils.Pair; import org.jetbrains.annotations.NotNull; @@ -662,4 +668,26 @@ public class LambdaInput this.mouseSpeedY = 0.F; } } + + public static @Nullable BlockHitResult tryFrontPlace(@NotNull MinecraftClient client) + { + if (client.player != null && client.crosshairTarget != null && client.crosshairTarget.getType() == HitResult.Type.MISS && client.player.onGround) { + if (client.player.isRiding()) + return null; + BlockPos playerPos = client.player.getBlockPos().down(); + BlockPos targetPos = new BlockPos(client.crosshairTarget.getPos()).subtract(playerPos); + BlockPos vector = new BlockPos(MathHelper.clamp(targetPos.getX(), -1, 1), 0, MathHelper.clamp(targetPos.getZ(), -1, 1)); + BlockPos blockPos = playerPos.add(vector); + + Direction direction = client.player.getHorizontalFacing(); + + BlockState adjacentBlockState = client.world.getBlockState(blockPos.offset(direction.getOpposite())); + if (adjacentBlockState.isAir() || adjacentBlockState.getBlock() instanceof FluidBlock || (vector.getX() == 0 && vector.getZ() == 0)) { + return null; + } + + return new BlockHitResult(client.crosshairTarget.getPos(), direction.getOpposite(), blockPos, false); + } + return null; + } } diff --git a/fabric/src/main/java/me/lambdaurora/lambdacontrols/client/gui/ControllerButtonWidget.java b/fabric/src/main/java/me/lambdaurora/lambdacontrols/client/gui/ControllerButtonWidget.java index 8ffca82..bfb7aea 100644 --- a/fabric/src/main/java/me/lambdaurora/lambdacontrols/client/gui/ControllerButtonWidget.java +++ b/fabric/src/main/java/me/lambdaurora/lambdacontrols/client/gui/ControllerButtonWidget.java @@ -52,7 +52,7 @@ public class ControllerButtonWidget extends AbstractIconButtonWidget if (this.binding.getButton().length > 1) { x += (this.width / 2 - this.iconWidth / 2) - 4; } - Pair size = LambdaControlsClient.drawButton(x, y, this.binding, MinecraftClient.getInstance()); + Pair size = LambdaControlsRenderer.drawButton(x, y, this.binding, MinecraftClient.getInstance()); this.iconWidth = size.key; return size.value; } diff --git a/fabric/src/main/java/me/lambdaurora/lambdacontrols/client/gui/LambdaControlsHud.java b/fabric/src/main/java/me/lambdaurora/lambdacontrols/client/gui/LambdaControlsHud.java index 72313b5..46cab7c 100644 --- a/fabric/src/main/java/me/lambdaurora/lambdacontrols/client/gui/LambdaControlsHud.java +++ b/fabric/src/main/java/me/lambdaurora/lambdacontrols/client/gui/LambdaControlsHud.java @@ -13,10 +13,20 @@ import me.lambdaurora.lambdacontrols.ControlsMode; import me.lambdaurora.lambdacontrols.LambdaControlsConstants; import me.lambdaurora.lambdacontrols.client.HudSide; import me.lambdaurora.lambdacontrols.client.LambdaControlsClient; +import me.lambdaurora.lambdacontrols.client.LambdaInput; import me.lambdaurora.lambdacontrols.client.controller.ButtonBinding; import me.lambdaurora.spruceui.hud.Hud; import net.minecraft.client.MinecraftClient; +import net.minecraft.client.render.RenderLayer; +import net.minecraft.client.render.Tessellator; +import net.minecraft.client.render.VertexConsumerProvider; +import net.minecraft.client.util.math.Matrix4f; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.client.util.math.Rotation3; +import net.minecraft.item.BlockItem; +import net.minecraft.item.ItemStack; import net.minecraft.util.Identifier; +import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.hit.HitResult; import org.jetbrains.annotations.NotNull; @@ -33,6 +43,10 @@ public class LambdaControlsHud extends Hud private MinecraftClient client; private int widthBottom = 0; private int widthTop = 0; + private int useWidth = 0; + private int attackWidth = 0; + private BlockHitResult placeHitResult; + private String placeAction = ""; public LambdaControlsHud(@NotNull LambdaControlsClient mod) { @@ -53,29 +67,81 @@ public class LambdaControlsHud extends Hud public void render(float tickDelta) { if (this.mod.config.getControlsMode() == ControlsMode.CONTROLLER && this.client.currentScreen == null) { - int x = this.mod.config.getHudSide() == HudSide.LEFT ? 10 : client.getWindow().getScaledWidth() - 10 - this.widthBottom, y = bottom(10); - x += (this.widthBottom = this.drawButtonTip(x, y, ButtonBinding.INVENTORY, true) + 10); - this.widthBottom += this.drawButtonTip(x, y, ButtonBinding.SWAP_HANDS, true); - x = this.mod.config.getHudSide() == HudSide.LEFT ? 10 : client.getWindow().getScaledWidth() - 10 - this.widthTop; - x += (this.widthTop = this.drawButtonTip(x, (y -= 20), ButtonBinding.DROP_ITEM, !this.client.player.getMainHandStack().isEmpty()) + 10); - this.widthTop += this.drawButtonTip(x, y, ButtonBinding.ATTACK.getButton(), - this.client.crosshairTarget.getType() == HitResult.Type.BLOCK ? "lambdacontrols.action.hit" : ButtonBinding.ATTACK.getTranslationKey(), - this.client.crosshairTarget.getType() != HitResult.Type.MISS); + VertexConsumerProvider.Immediate immediate = VertexConsumerProvider.immediate(Tessellator.getInstance().getBuffer()); + Matrix4f matrix4f = Rotation3.identity().getMatrix(); + this.renderFirstSection(this.mod.config.getHudSide() == HudSide.LEFT ? 10 : client.getWindow().getScaledWidth() - 10 - this.widthBottom, immediate, matrix4f); + this.renderSecondSection(this.mod.config.getHudSide() == HudSide.RIGHT ? 10 : client.getWindow().getScaledWidth() - 10, immediate, matrix4f); + immediate.draw(); } } + public void renderFirstSection(int x, @NotNull VertexConsumerProvider.Immediate immediate, @NotNull Matrix4f matrix4f) + { + int y = bottom(10); + x += (this.widthBottom = this.drawButtonTip(x, y, ButtonBinding.INVENTORY, true, immediate, matrix4f) + 10); + this.widthBottom += this.drawButtonTip(x, y, ButtonBinding.SWAP_HANDS, true, immediate, matrix4f); + this.widthTop = this.drawButtonTip(x, (y - 24), ButtonBinding.DROP_ITEM, !this.client.player.getMainHandStack().isEmpty(), immediate, matrix4f) + 10; + } + + public void renderSecondSection(int x, @NotNull VertexConsumerProvider.Immediate immediate, @NotNull Matrix4f matrix4f) + { + int y = bottom(10); + if (this.placeHitResult != null) { + int firstX = this.mod.config.getHudSide() == HudSide.RIGHT ? x : x - this.useWidth; + this.useWidth = this.drawButtonTip(firstX, y, ButtonBinding.USE.getButton(), this.placeAction, true, immediate, matrix4f); + y -= 24; + } + + int secondX = this.mod.config.getHudSide() == HudSide.RIGHT ? x : x - this.attackWidth; + this.attackWidth = this.drawButtonTip(secondX, y, ButtonBinding.ATTACK.getButton(), + this.client.crosshairTarget.getType() == HitResult.Type.BLOCK ? "lambdacontrols.action.hit" : ButtonBinding.ATTACK.getTranslationKey(), + this.client.crosshairTarget.getType() != HitResult.Type.MISS, immediate, matrix4f); + } + + @Override + public void tick() + { + super.tick(); + if (this.mod.config.getControlsMode() == ControlsMode.CONTROLLER) { + if (this.client.crosshairTarget == null) + return; + + if (this.client.crosshairTarget.getType() == HitResult.Type.MISS) { + this.placeHitResult = LambdaInput.tryFrontPlace(this.client); + } else { + if (this.client.crosshairTarget.getType() == HitResult.Type.BLOCK) + this.placeHitResult = (BlockHitResult) this.client.crosshairTarget; + else + this.placeHitResult = null; + } + + if (this.placeHitResult != null) { + ItemStack stack = this.client.player.getActiveItem(); + if (stack != null && stack.getItem() instanceof BlockItem) { + this.placeAction = "lambdacontrols.action.place"; + } + } + } + } + + @Override + public boolean hasTicks() + { + return true; + } + private int bottom(int y) { return this.client.getWindow().getScaledHeight() - y - 15; } - private int drawButtonTip(int x, int y, @NotNull ButtonBinding button, boolean display) + private int drawButtonTip(int x, int y, @NotNull ButtonBinding button, boolean display, @NotNull VertexConsumerProvider.Immediate immediate, @NotNull Matrix4f matrix4f) { - return LambdaControlsClient.drawButtonTip(x, y, button, display, this.client); + return LambdaControlsRenderer.drawButtonTip(x, y, button, display, this.client, immediate, matrix4f); } - private int drawButtonTip(int x, int y, int[] button, @NotNull String action, boolean display) + private int drawButtonTip(int x, int y, int[] button, @NotNull String action, boolean display, @NotNull VertexConsumerProvider.Immediate immediate, @NotNull Matrix4f matrix4f) { - return LambdaControlsClient.drawButtonTip(x, y, button, action, display, this.client); + return LambdaControlsRenderer.drawButtonTip(x, y, button, action, display, this.client, immediate, matrix4f); } } diff --git a/fabric/src/main/java/me/lambdaurora/lambdacontrols/client/gui/LambdaControlsRenderer.java b/fabric/src/main/java/me/lambdaurora/lambdacontrols/client/gui/LambdaControlsRenderer.java new file mode 100644 index 0000000..f470cf0 --- /dev/null +++ b/fabric/src/main/java/me/lambdaurora/lambdacontrols/client/gui/LambdaControlsRenderer.java @@ -0,0 +1,180 @@ +/* + * Copyright © 2020 LambdAurora + * + * This file is part of LambdaControls. + * + * Licensed under the MIT license. For more information, + * see the LICENSE file. + */ + +package me.lambdaurora.lambdacontrols.client.gui; + +import com.mojang.blaze3d.platform.GlStateManager; +import com.mojang.blaze3d.systems.RenderSystem; +import me.lambdaurora.lambdacontrols.client.LambdaControlsClient; +import me.lambdaurora.lambdacontrols.client.controller.ButtonBinding; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.font.TextRenderer; +import net.minecraft.client.gui.DrawableHelper; +import net.minecraft.client.render.*; +import net.minecraft.client.resource.language.I18n; +import net.minecraft.client.util.math.Matrix4f; +import net.minecraft.client.util.math.Rotation3; +import org.aperlambda.lambdacommon.utils.Pair; +import org.jetbrains.annotations.NotNull; +import org.lwjgl.glfw.GLFW; + +public class LambdaControlsRenderer +{ + public static Pair drawButton(int x, int y, @NotNull ButtonBinding button, @NotNull MinecraftClient client) + { + return drawButton(x, y, button.getButton(), client); + } + + public static Pair drawButton(int x, int y, int[] buttons, @NotNull MinecraftClient client) + { + int height = 0; + int length = 0; + int currentX = x; + for (int i = 0; i < buttons.length; i++) { + int btn = buttons[i]; + Pair size = drawButton(currentX, y, btn, client); + if (size.key > height) + height = size.key; + length += size.key; + if (i + 1 < buttons.length) { + length += 2; + currentX = x + length; + } + } + return Pair.of(length, height); + } + + @SuppressWarnings("deprecated") + public static Pair drawButton(int x, int y, int button, @NotNull MinecraftClient client) + { + boolean second = false; + if (button == -1) + return Pair.of(0, 0); + else if (button >= 500) { + button -= 1000; + second = true; + } + + int controllerType = LambdaControlsClient.get().config.getControllerType().getId(); + boolean axis = false; + int buttonOffset = button * 15; + switch (button) { + case GLFW.GLFW_GAMEPAD_BUTTON_LEFT_BUMPER: + buttonOffset = 7 * 15; + break; + case GLFW.GLFW_GAMEPAD_BUTTON_RIGHT_BUMPER: + buttonOffset = 8 * 15; + break; + case GLFW.GLFW_GAMEPAD_BUTTON_BACK: + buttonOffset = 4 * 15; + break; + case GLFW.GLFW_GAMEPAD_BUTTON_START: + buttonOffset = 6 * 15; + break; + case GLFW.GLFW_GAMEPAD_BUTTON_GUIDE: + buttonOffset = 5 * 15; + break; + case GLFW.GLFW_GAMEPAD_BUTTON_LEFT_THUMB: + buttonOffset = 15 * 15; + break; + case GLFW.GLFW_GAMEPAD_BUTTON_RIGHT_THUMB: + buttonOffset = 16 * 15; + break; + case GLFW.GLFW_GAMEPAD_AXIS_LEFT_X + 100: + buttonOffset = 0; + axis = true; + break; + case GLFW.GLFW_GAMEPAD_AXIS_LEFT_Y + 100: + buttonOffset = 18; + axis = true; + break; + case GLFW.GLFW_GAMEPAD_AXIS_RIGHT_X + 100: + buttonOffset = 2 * 18; + axis = true; + break; + case GLFW.GLFW_GAMEPAD_AXIS_RIGHT_Y + 100: + buttonOffset = 3 * 18; + axis = true; + break; + case GLFW.GLFW_GAMEPAD_AXIS_LEFT_X + 200: + buttonOffset = 4 * 18; + axis = true; + break; + case GLFW.GLFW_GAMEPAD_AXIS_LEFT_Y + 200: + buttonOffset = 5 * 18; + axis = true; + break; + case GLFW.GLFW_GAMEPAD_AXIS_RIGHT_X + 200: + buttonOffset = 6 * 18; + axis = true; + break; + case GLFW.GLFW_GAMEPAD_AXIS_RIGHT_Y + 200: + buttonOffset = 7 * 18; + axis = true; + break; + case GLFW.GLFW_GAMEPAD_AXIS_LEFT_TRIGGER + 100: + case GLFW.GLFW_GAMEPAD_AXIS_LEFT_TRIGGER + 200: + buttonOffset = 9 * 15; + break; + case GLFW.GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER + 100: + case GLFW.GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER + 200: + buttonOffset = 10 * 15; + break; + } + + client.getTextureManager().bindTexture(axis ? LambdaControlsClient.CONTROLLER_AXIS : LambdaControlsClient.CONTROLLER_BUTTONS); + GlStateManager.disableDepthTest(); + + GlStateManager.color4f(1.0F, second ? 0.0F : 1.0F, 1.0F, 1.0F); + DrawableHelper.blit(x, y, (float) buttonOffset, (float) (controllerType * (axis ? 18 : 15)), axis ? 18 : 15, axis ? 18 : 15, 256, 256); + GlStateManager.enableDepthTest(); + + return axis ? Pair.of(18, 18) : Pair.of(15, 15); + } + + public static int drawButtonTip(int x, int y, @NotNull ButtonBinding button, boolean display, @NotNull MinecraftClient client) + { + return drawButtonTip(x, y, button.getButton(), button.getTranslationKey(), display, client); + } + + public static int drawButtonTip(int x, int y, int[] button, @NotNull String action, boolean display, @NotNull MinecraftClient client) + { + VertexConsumerProvider.Immediate immediate = VertexConsumerProvider.immediate(Tessellator.getInstance().getBuffer()); + int width = drawButtonTip(x, y, button, action, display, client, immediate, Rotation3.identity().getMatrix()); + immediate.draw(); + return width; + } + + public static int drawButtonTip(int x, int y, @NotNull ButtonBinding button, boolean display, @NotNull MinecraftClient client, @NotNull VertexConsumerProvider.Immediate immediate, @NotNull Matrix4f matrix4f) + { + return drawButtonTip(x, y, button.getButton(), button.getTranslationKey(), display, client, immediate, matrix4f); + } + + public static int drawButtonTip(int x, int y, int[] button, @NotNull String action, boolean display, @NotNull MinecraftClient client, @NotNull VertexConsumerProvider.Immediate immediate, @NotNull Matrix4f matrix4f) + { + if (display) { + RenderSystem.enableAlphaTest(); + int buttonWidth = drawButton(x, y, button, client).key; + + String translatedAction = I18n.translate(action); + int textY = (15 - client.textRenderer.fontHeight) / 2; + int width = client.textRenderer.draw(translatedAction, (float) (x + buttonWidth + 5), (float) (y + textY), 14737632, true, matrix4f, immediate, + false, 0, 15728880); + + return width; + } + + return -10; + } + + private static int getButtonTipWidth(@NotNull String action, @NotNull TextRenderer textRenderer) + { + return 15 + 5 + textRenderer.getStringWidth(action); + } +} diff --git a/fabric/src/main/java/me/lambdaurora/lambdacontrols/client/mixin/ContainerScreenMixin.java b/fabric/src/main/java/me/lambdaurora/lambdacontrols/client/mixin/ContainerScreenMixin.java index 0a7e768..80846e1 100644 --- a/fabric/src/main/java/me/lambdaurora/lambdacontrols/client/mixin/ContainerScreenMixin.java +++ b/fabric/src/main/java/me/lambdaurora/lambdacontrols/client/mixin/ContainerScreenMixin.java @@ -11,6 +11,7 @@ package me.lambdaurora.lambdacontrols.client.mixin; import me.lambdaurora.lambdacontrols.ControlsMode; import me.lambdaurora.lambdacontrols.client.LambdaControlsClient; +import me.lambdaurora.lambdacontrols.client.gui.LambdaControlsRenderer; import me.lambdaurora.lambdacontrols.client.util.ContainerScreenAccessor; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.ingame.ContainerScreen; @@ -45,10 +46,10 @@ public abstract class ContainerScreenMixin implements ContainerScreenAccessor MinecraftClient client = MinecraftClient.getInstance(); int x = 10, y = client.getWindow().getScaledHeight() - 10 - 15; - x += LambdaControlsClient.drawButtonTip(x, y, new int[]{GLFW.GLFW_GAMEPAD_BUTTON_A}, "lambdacontrols.action.pickup_all", true, client) + 10; - x += LambdaControlsClient.drawButtonTip(x, y, new int[]{GLFW.GLFW_GAMEPAD_BUTTON_B}, "lambdacontrols.action.exit", true, client) + 10; - x += LambdaControlsClient.drawButtonTip(x, y, new int[]{GLFW.GLFW_GAMEPAD_BUTTON_X}, "lambdacontrols.action.pickup", true, client) + 10; - LambdaControlsClient.drawButtonTip(x, y, new int[]{GLFW.GLFW_GAMEPAD_BUTTON_Y}, "lambdacontrols.action.quick_move", true, client); + x += LambdaControlsRenderer.drawButtonTip(x, y, new int[]{GLFW.GLFW_GAMEPAD_BUTTON_A}, "lambdacontrols.action.pickup_all", true, client) + 10; + x += LambdaControlsRenderer.drawButtonTip(x, y, new int[]{GLFW.GLFW_GAMEPAD_BUTTON_B}, "lambdacontrols.action.exit", true, client) + 10; + x += LambdaControlsRenderer.drawButtonTip(x, y, new int[]{GLFW.GLFW_GAMEPAD_BUTTON_X}, "lambdacontrols.action.pickup", true, client) + 10; + LambdaControlsRenderer.drawButtonTip(x, y, new int[]{GLFW.GLFW_GAMEPAD_BUTTON_Y}, "lambdacontrols.action.quick_move", true, client); } } } diff --git a/fabric/src/main/java/me/lambdaurora/lambdacontrols/client/mixin/MinecraftClientMixin.java b/fabric/src/main/java/me/lambdaurora/lambdacontrols/client/mixin/MinecraftClientMixin.java index a9ba610..bff327f 100644 --- a/fabric/src/main/java/me/lambdaurora/lambdacontrols/client/mixin/MinecraftClientMixin.java +++ b/fabric/src/main/java/me/lambdaurora/lambdacontrols/client/mixin/MinecraftClientMixin.java @@ -11,6 +11,7 @@ package me.lambdaurora.lambdacontrols.client.mixin; import me.lambdaurora.lambdacontrols.LambdaControlsFeature; import me.lambdaurora.lambdacontrols.client.LambdaControlsClient; +import me.lambdaurora.lambdacontrols.client.LambdaInput; import net.minecraft.block.BlockState; import net.minecraft.block.FluidBlock; import net.minecraft.client.MinecraftClient; @@ -28,6 +29,7 @@ import net.minecraft.util.hit.HitResult; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; import net.minecraft.util.math.MathHelper; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; @@ -60,12 +62,33 @@ public abstract class MinecraftClientMixin @Final public GameRenderer gameRenderer; + @Shadow + private int itemUseCooldown; + @Inject(method = "", at = @At("RETURN")) private void onInit(CallbackInfo ci) { LambdaControlsClient.get().onMcInit((MinecraftClient) (Object) this); } + @Inject(method = "tick", at = @At("HEAD")) + private void onStartTick(CallbackInfo ci) + { + if (this.player != null && this.player.isCreative()) { + int cooldown = this.itemUseCooldown; + if (this.crosshairTarget != null && this.crosshairTarget.getType() == HitResult.Type.BLOCK && this.player.abilities.flying) { + if (cooldown > 1) + this.itemUseCooldown = 1; + } else if (this.player.isSprinting()) { + BlockHitResult result = LambdaInput.tryFrontPlace(((MinecraftClient) (Object) this)); + if (result != null) { + if (cooldown > 0) + this.itemUseCooldown = 0; + } + } + } + } + @Inject(method = "render", at = @At("HEAD")) private void onRender(boolean fullRender, CallbackInfo ci) { @@ -84,19 +107,10 @@ public abstract class MinecraftClientMixin if (!stackInHand.isEmpty() && this.player.pitch > 35.0F && LambdaControlsFeature.FRONT_BLOCK_PLACING.isAvailable()) { if (this.crosshairTarget != null && this.crosshairTarget.getType() == HitResult.Type.MISS && this.player.onGround) { if (!stackInHand.isEmpty() && stackInHand.getItem() instanceof BlockItem) { - BlockPos playerPos = this.player.getBlockPos().down(); - BlockPos targetPos = new BlockPos(this.crosshairTarget.getPos()).subtract(playerPos); - BlockPos vector = new BlockPos(MathHelper.clamp(targetPos.getX(), -1, 1), 0, MathHelper.clamp(targetPos.getZ(), -1, 1)); - BlockPos blockPos = playerPos.add(vector); + BlockHitResult hitResult = LambdaInput.tryFrontPlace(((MinecraftClient) (Object) this)); - Direction direction = player.getHorizontalFacing(); - - BlockState adjacentBlockState = this.world.getBlockState(blockPos.offset(direction.getOpposite())); - if (adjacentBlockState.isAir() || adjacentBlockState.getBlock() instanceof FluidBlock || (vector.getX() == 0 && vector.getZ() == 0)) { + if (hitResult == null) return; - } - - BlockHitResult hitResult = new BlockHitResult(this.crosshairTarget.getPos(), direction.getOpposite(), blockPos, false); int previousStackCount = stackInHand.getCount(); ActionResult result = this.interactionManager.interactBlock(this.player, this.world, hand, hitResult);