Skip to content
This repository has been archived by the owner on Mar 6, 2022. It is now read-only.

Commit

Permalink
🔖 LambdaControls v1.3.2: Fix things, add vertical reacharound and int…
Browse files Browse the repository at this point in the history
…ernal API.
  • Loading branch information
LambdAurora committed Jun 29, 2020
1 parent 24f7054 commit 2c53575
Show file tree
Hide file tree
Showing 22 changed files with 547 additions and 219 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ This mod adds a controller support (and an experimental touchscreen support).
- Touchscreen support (very experimental and buggy).
- Keyboard controls to look around.
- Toggleable on screen button indicator (like in Bedrock Edition).
- Vertical reacharound.
- Many Bedrock Edition features:
- Toggleable fly drifting
- Front block placing (be careful with this one)
- New controls settings!
- Many options in config to change to your liking.
- Many controllers supported and in a simply way your own controller mappings.
- An easy API for developers to add their own button bindings.

## 🎮 Supported Controllers:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@
* Represents a feature.
*
* @author LambdAurora
* @version 1.2.0
* @version 1.3.2
* @since 1.1.0
*/
public class LambdaControlsFeature implements Nameable
{
private static final List<LambdaControlsFeature> FEATURES = new ArrayList<>();
public static final LambdaControlsFeature FRONT_BLOCK_PLACING = new LambdaControlsFeature("front_block_placing", true, false);
public static final LambdaControlsFeature FAST_BLOCK_PLACING = new LambdaControlsFeature("fast_block_placing", true, true);
private static final List<LambdaControlsFeature> FEATURES = new ArrayList<>();
public static final LambdaControlsFeature FAST_BLOCK_PLACING = new LambdaControlsFeature("fast_block_placing", true, true);
public static final LambdaControlsFeature FRONT_BLOCK_PLACING = new LambdaControlsFeature("front_block_placing", true, false);
public static final LambdaControlsFeature VERTICAL_REACHAROUND = new LambdaControlsFeature("vertical_reacharound", true, false);

private final String key;
private final boolean defaultAllowed;
Expand Down Expand Up @@ -155,7 +156,8 @@ public static void resetAllAllowed()
}

static {
FEATURES.add(FRONT_BLOCK_PLACING);
FEATURES.add(FAST_BLOCK_PLACING);
FEATURES.add(FRONT_BLOCK_PLACING);
FEATURES.add(VERTICAL_REACHAROUND);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import me.lambdaurora.spruceui.hud.HudManager;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.KeyBindingRegistry;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.fabricmc.fabric.api.network.ClientSidePacketRegistry;
import net.minecraft.client.MinecraftClient;
Expand All @@ -40,7 +39,7 @@
* Represents the LambdaControls client mod.
*
* @author LambdAurora
* @version 1.3.0
* @version 1.3.2
* @since 1.1.0
*/
public class LambdaControlsClient extends LambdaControls implements ClientModInitializer
Expand All @@ -59,6 +58,7 @@ public class LambdaControlsClient extends LambdaControls implements ClientModIni
public static final Identifier CURSOR_TEXTURE = new Identifier(LambdaControlsConstants.NAMESPACE, "textures/gui/cursor.png");
public final LambdaControlsConfig config = new LambdaControlsConfig(this);
public final LambdaInput input = new LambdaInput(this);
public final LambdaReacharound reacharound = new LambdaReacharound();
private LambdaControlsHud hud;
private ControlsMode previousControlsMode;

Expand All @@ -79,6 +79,7 @@ public void onInitializeClient()
LambdaControlsFeature.fromName(name).ifPresent(feature -> context.getTaskQueue().execute(() -> feature.setAllowed(allowed)));
});

ClientTickEvents.START_CLIENT_TICK.register(this.reacharound::tick);
ClientTickEvents.END_CLIENT_TICK.register(this::onTick);

OpenScreenCallback.EVENT.register((client, screen) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,37 +34,39 @@
public class LambdaControlsConfig
{
// General
private static final ControlsMode DEFAULT_CONTROLS_MODE = ControlsMode.DEFAULT;
private static final boolean DEFAULT_AUTO_SWITCH_MODE = false;
private static final ControlsMode DEFAULT_CONTROLS_MODE = ControlsMode.DEFAULT;
private static final boolean DEFAULT_AUTO_SWITCH_MODE = false;
// HUD
private static final boolean DEFAULT_HUD_ENABLE = true;
private static final HudSide DEFAULT_HUD_SIDE = HudSide.LEFT;
private static final boolean DEFAULT_HUD_ENABLE = true;
private static final HudSide DEFAULT_HUD_SIDE = HudSide.LEFT;
// Gameplay
private static final boolean DEFAULT_FAST_BLOCK_INTERACTION = true;
private static final boolean DEFAULT_FLY_DRIFTING = false;
private static final boolean DEFAULT_FLY_VERTICAL_DRIFTING = true;
private static final boolean DEFAULT_FRONT_BLOCK_PLACING = false;
private static final boolean DEFAULT_FRONT_BLOCK_OUTLINE = true;
private static final boolean DEFAULT_FAST_BLOCK_INTERACTION = true;
private static final boolean DEFAULT_FLY_DRIFTING = false;
private static final boolean DEFAULT_FLY_VERTICAL_DRIFTING = true;
private static final boolean DEFAULT_FRONT_BLOCK_PLACING = false;
private static final boolean DEFAULT_VERTICAL_REACHAROUND = false;
private static final boolean DEFAULT_REACHAROUND_OUTLINE = true;
private static final int[] DEFAULT_REACHAROUND_OUTLINE_COLOR = new int[]{255, 255, 255, 102};
// Controller
private static final ControllerType DEFAULT_CONTROLLER_TYPE = ControllerType.DEFAULT;
private static final double DEFAULT_DEAD_ZONE = 0.25;
private static final double DEFAULT_ROTATION_SPEED = 40.0;
private static final double DEFAULT_MOUSE_SPEED = 25.0;
private static final boolean DEFAULT_UNFOCUSED_INPUT = false;
private static final boolean DEFAULT_VIRTUAL_MOUSE = false;
private static final VirtualMouseSkin DEFAULT_VIRTUAL_MOUSE_SKIN = VirtualMouseSkin.DEFAULT_LIGHT;
private static final ControllerType DEFAULT_CONTROLLER_TYPE = ControllerType.DEFAULT;
private static final double DEFAULT_DEAD_ZONE = 0.25;
private static final double DEFAULT_ROTATION_SPEED = 40.0;
private static final double DEFAULT_MOUSE_SPEED = 25.0;
private static final boolean DEFAULT_UNFOCUSED_INPUT = false;
private static final boolean DEFAULT_VIRTUAL_MOUSE = false;
private static final VirtualMouseSkin DEFAULT_VIRTUAL_MOUSE_SKIN = VirtualMouseSkin.DEFAULT_LIGHT;

private static final Pattern BUTTON_BINDING_PATTERN = Pattern.compile("(-?\\d+)\\+?");

protected final FileConfig config = FileConfig.builder("config/lambdacontrols.toml").concurrent().defaultResource("/config.toml").build();
private final LambdaControlsClient mod;
private ControlsMode controlsMode;
private ControllerType controllerType;
private ControllerType controllerType;
// Gameplay.
private boolean shouldRenderFrontBlockOutline;
private int[] frontBlockOutlineColor;
private boolean shouldRenderReacharoundOutline;
private int[] reacharoundOutlineColor;
// Controller settings
private double deadZone;
private double deadZone;
private double rotationSpeed;
private double mouseSpeed;
private boolean unfocusedInput;
Expand Down Expand Up @@ -93,9 +95,10 @@ public void load()
this.hudSide = HudSide.byId(this.config.getOrElse("hud.side", DEFAULT_HUD_SIDE.getName())).orElse(DEFAULT_HUD_SIDE);
// Gameplay
LambdaControlsFeature.FAST_BLOCK_PLACING.setEnabled(this.config.getOrElse("gameplay.fast_block_placing", DEFAULT_FAST_BLOCK_INTERACTION));
LambdaControlsFeature.FRONT_BLOCK_PLACING.setEnabled(this.config.getOrElse("gameplay.front_block_placing.enabled", DEFAULT_FRONT_BLOCK_PLACING));
this.shouldRenderFrontBlockOutline = this.config.getOrElse("gameplay.front_block_placing.outline", DEFAULT_FRONT_BLOCK_OUTLINE);
this.frontBlockOutlineColor = this.config.getOptional("gameplay.front_block_placing.outline_color").map(hex -> parseColor((String) hex)).orElse(new int[]{255, 255, 255, 102});
LambdaControlsFeature.FRONT_BLOCK_PLACING.setEnabled(this.config.getOrElse("gameplay.reacharound.horizontal", DEFAULT_FRONT_BLOCK_PLACING));
LambdaControlsFeature.VERTICAL_REACHAROUND.setEnabled(this.config.getOrElse("gameplay.reacharound.vertical", DEFAULT_VERTICAL_REACHAROUND));
this.shouldRenderReacharoundOutline = this.config.getOrElse("gameplay.reacharound.outline", DEFAULT_REACHAROUND_OUTLINE);
this.reacharoundOutlineColor = this.config.getOptional("gameplay.reacharound.outline_color").map(hex -> parseColor((String) hex)).orElse(DEFAULT_REACHAROUND_OUTLINE_COLOR);
// Controller settings.
this.controllerType = ControllerType.byId(this.config.getOrElse("controller.type", DEFAULT_CONTROLLER_TYPE.getName())).orElse(DEFAULT_CONTROLLER_TYPE);
this.deadZone = this.config.getOrElse("controller.dead_zone", DEFAULT_DEAD_ZONE);
Expand Down Expand Up @@ -133,10 +136,19 @@ public void checkAndFix()
}
});

// This shouldn't happen if the configuration is new.
if (!this.config.contains("gameplay.front_block_placing.enabled") && this.config.contains("gameplay.front_block_placing")) {
this.config.remove("gameplay.front_block_placing");
this.config.set("gameplay.front_block_placing.enabled", DEFAULT_FRONT_BLOCK_PLACING);
if (this.config.contains("gameplay.front_block_placing.enabled")) {
this.setFrontBlockPlacing(this.config.getOrElse("gameplay.front_block_placing.enabled", DEFAULT_FRONT_BLOCK_PLACING));
this.config.remove("gameplay.front_block_placing.enabled");
}

if (this.config.contains("gameplay.front_block_placing.outline")) {
this.setRenderReacharoundOutline(this.config.getOrElse("gameplay.front_block_placing.outline", DEFAULT_REACHAROUND_OUTLINE));
this.config.remove("gameplay.front_block_placing.outline");
}

if (this.config.contains("gameplay.front_block_placing.outline_color")) {
this.config.getOptional("gameplay.front_block_placing.outline_color").ifPresent(color -> this.config.set("gameplay.reacharound.outline_color", color));
this.config.remove("gameplay.front_block_placing.outline_color");
}

this.renamed("controller.controls.tab_left", "controller.controls.tab_back");
Expand Down Expand Up @@ -165,7 +177,8 @@ public void reset()
this.setFlyDrifting(DEFAULT_FLY_DRIFTING);
this.setFlyVerticalDrifting(DEFAULT_FLY_VERTICAL_DRIFTING);
this.setFrontBlockPlacing(DEFAULT_FRONT_BLOCK_PLACING);
this.setRenderFrontBlockOutline(DEFAULT_FRONT_BLOCK_OUTLINE);
this.setVerticalReacharound(DEFAULT_VERTICAL_REACHAROUND);
this.setRenderReacharoundOutline(DEFAULT_REACHAROUND_OUTLINE);
// Controller
this.setControllerType(DEFAULT_CONTROLLER_TYPE);
this.setDeadZone(DEFAULT_DEAD_ZONE);
Expand Down Expand Up @@ -352,27 +365,48 @@ public boolean hasFrontBlockPlacing()
public void setFrontBlockPlacing(boolean enable)
{
LambdaControlsFeature.FRONT_BLOCK_PLACING.setEnabled(enable);
this.config.set("gameplay.front_block_placing.enabled", enable);
this.config.set("gameplay.reacharound.horizontal", enable);
}

/**
* Returns whether vertical reacharound is enabled or not.
*
* @return True if vertical reacharound is enabled, else false.
*/
public boolean hasVerticalReacharound()
{
return LambdaControlsFeature.VERTICAL_REACHAROUND.isEnabled();
}

/**
* Sets whether vertical reacharound is enabled or not.
*
* @param enable True if vertical reacharound is enabled, else false.
*/
public void setVerticalReacharound(boolean enable)
{
LambdaControlsFeature.VERTICAL_REACHAROUND.setEnabled(enable);
this.config.set("gameplay.reacharound.vertical", enable);
}

/**
* Returns whether front block placing outline is enabled or not.
*
* @return True if front block placing outline is enabled, else false.
*/
public boolean shouldRenderFrontBlockOutline()
public boolean shouldRenderReacharoundOutline()
{
return this.shouldRenderFrontBlockOutline;
return this.shouldRenderReacharoundOutline;
}

/**
* Sets whether front block placing outline is enabled or not.
*
* @param render True if front block placing outline is enabled, else false.
*/
public void setRenderFrontBlockOutline(boolean render)
public void setRenderReacharoundOutline(boolean render)
{
this.config.set("gameplay.front_block_placing.outline", this.shouldRenderFrontBlockOutline = render);
this.config.set("gameplay.reacharound.outline", this.shouldRenderReacharoundOutline = render);
}

/**
Expand All @@ -382,9 +416,9 @@ public void setRenderFrontBlockOutline(boolean render)
*
* @return The color as a RGBA integer array.
*/
public int[] getFrontBlockOutlineColor()
public int[] getReacharoundOutlineColor()
{
return this.frontBlockOutlineColor;
return this.reacharoundOutlineColor;
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

package me.lambdaurora.lambdacontrols.client;

import me.lambdaurora.lambdacontrols.LambdaControlsFeature;
import me.lambdaurora.lambdacontrols.client.compat.LambdaControlsCompat;
import me.lambdaurora.lambdacontrols.client.controller.ButtonBinding;
import me.lambdaurora.lambdacontrols.client.controller.Controller;
Expand All @@ -22,10 +21,6 @@
import me.lambdaurora.lambdacontrols.client.util.HandledScreenAccessor;
import me.lambdaurora.lambdacontrols.client.util.MouseAccessor;
import me.lambdaurora.spruceui.SpruceLabelWidget;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.FluidBlock;
import net.minecraft.block.SlabBlock;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.Element;
import net.minecraft.client.gui.ParentElement;
Expand All @@ -42,14 +37,8 @@
import net.minecraft.client.gui.widget.EntryListWidget;
import net.minecraft.client.gui.widget.SliderWidget;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.slot.Slot;
import net.minecraft.screen.slot.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;
Expand All @@ -72,7 +61,7 @@
* Represents the LambdaControls' input handler.
*
* @author LambdAurora
* @version 1.3.1
* @version 1.3.2
* @since 1.0.0
*/
public class LambdaInput
Expand Down Expand Up @@ -325,7 +314,8 @@ private void handleButton(@NotNull MinecraftClient client, int button, int actio

if (button == GLFW.GLFW_GAMEPAD_BUTTON_B) {
if (client.currentScreen != null) {
client.currentScreen.onClose();
if (!LambdaControlsCompat.handleMenuBack(client, client.currentScreen))
client.currentScreen.onClose();
return;
}
}
Expand Down Expand Up @@ -390,6 +380,8 @@ private boolean handleInventory(@NotNull MinecraftClient client, int button)
if (screen instanceof CreativeInventoryScreen)
if (((CreativeInventoryScreenAccessor) screen).lambdacontrols_isCreativeInventorySlot(slot))
actionType = SlotActionType.CLONE;
if (slot != null && LambdaControlsCompat.streamCompatHandlers().anyMatch(handler -> handler.isCreativeSlot(screen, slot)))
actionType = SlotActionType.CLONE;
break;
case GLFW.GLFW_GAMEPAD_BUTTON_X:
clickData = GLFW_MOUSE_BUTTON_2;
Expand Down Expand Up @@ -720,71 +712,4 @@ private void moveMouseToClosestSlot(@NotNull MinecraftClient client, @Nullable S
this.mouseSpeedY = 0.F;
}
}

public static Direction getMoveDirection(@Nullable BlockPos lastPos, @NotNull BlockPos newPos)
{
if (lastPos == null)
return null;
BlockPos vector = newPos.subtract(lastPos);
if (vector.getX() > 0)
return Direction.EAST;
else if (vector.getX() < 0)
return Direction.WEST;
else if (vector.getZ() > 0)
return Direction.SOUTH;
else if (vector.getZ() < 0)
return Direction.NORTH;
else if (vector.getY() > 0)
return Direction.UP;
else if (vector.getY() < 0)
return Direction.DOWN;
return null;
}

/**
* Returns a nullable block hit result if front placing is possible.
*
* @param client The client instance.
* @return A block hit result if front placing is possible.
*/
public static @Nullable BlockHitResult tryFrontPlace(@NotNull MinecraftClient client)
{
if (!LambdaControlsFeature.FRONT_BLOCK_PLACING.isAvailable())
return null;
if (client.player != null && client.crosshairTarget != null && client.crosshairTarget.getType() == HitResult.Type.MISS && client.player.isOnGround() && client.player.pitch > 35.0F) {
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 state = client.world.getBlockState(blockPos);
if (!state.isAir())
return null;
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, blockPos, false);
}
return null;
}

public static @NotNull BlockHitResult withSideForFrontPlace(@NotNull BlockHitResult result, @Nullable ItemStack stack)
{
if (stack == null || stack.isEmpty() || !(stack.getItem() instanceof BlockItem))
return result;
return withSideForFrontPlace(result, Block.getBlockFromItem(stack.getItem()));
}

public static @NotNull BlockHitResult withSideForFrontPlace(@NotNull BlockHitResult result, @NotNull Block block)
{
if (block instanceof SlabBlock)
result = result.withSide(Direction.DOWN);
return result;
}
}
Loading

0 comments on commit 2c53575

Please sign in to comment.