Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

General cleanup; keypress handling; tripod storage; FreecamPosition #126

Merged
merged 8 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 93 additions & 73 deletions common/src/main/java/net/xolt/freecam/Freecam.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,40 @@
import net.minecraft.client.player.Input;
import net.minecraft.client.player.KeyboardInput;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.ChunkPos;
import net.xolt.freecam.config.ModConfig;
import net.xolt.freecam.tripod.TripodRegistry;
import net.xolt.freecam.tripod.TripodSlot;
import net.xolt.freecam.util.FreeCamera;
import net.xolt.freecam.util.FreecamPosition;
import net.xolt.freecam.variant.api.BuildVariant;
import org.lwjgl.glfw.GLFW;

import java.util.HashMap;
import org.jetbrains.annotations.Nullable;

import static net.xolt.freecam.config.ModBindings.*;

public class Freecam {

public static final Minecraft MC = Minecraft.getInstance();
public static final String MOD_ID = "freecam";
private static final long TOGGLE_KEY_MAX_TICKS = 10;

private static boolean freecamEnabled = false;
private static boolean tripodEnabled = false;
private static boolean playerControlEnabled = false;
private static boolean disableNextTick = false;
private static Integer activeTripod = null;
private static boolean toggleKeyUsedWhileHeld = false;
private static long toggleKeyHeldTicks = 0;
private static final TripodRegistry tripods = new TripodRegistry();
private static TripodSlot activeTripod = TripodSlot.NONE;
private static FreeCamera freeCamera;
private static HashMap<Integer, FreecamPosition> overworld_tripods = new HashMap<>();
private static HashMap<Integer, FreecamPosition> nether_tripods = new HashMap<>();
private static HashMap<Integer, FreecamPosition> end_tripods = new HashMap<>();
private static CameraType rememberedF5 = null;

public static void preTick(Minecraft mc) {
if (isEnabled()) {
// Disable if the previous tick asked us to
if (disableNextTick()) {
if (disableNextTick) {
toggle();
disableNextTick = false;
}

// Prevent player from being controlled when freecam is enabled
Expand All @@ -51,35 +52,49 @@ public static void preTick(Minecraft mc) {

mc.gameRenderer.setRenderHand(ModConfig.INSTANCE.visual.showHand);
}
disableNextTick = false;
}

public static void postTick(Minecraft mc) {
if (KEY_TRIPOD_RESET.isPressed()) {
for (KeyMapping hotbarKey : mc.options.keyHotbarSlots) {
while (hotbarKey.consumeClick()) {
resetCamera(hotbarKey.getDefaultKey().getValue());
while (KEY_TRIPOD_RESET.wasPressed()) {}
if (KEY_TOGGLE.isDown()) {
// Count held ticks, so we can toggle on release
toggleKeyHeldTicks++;
KEY_TOGGLE.reset();

// Handle <toggle_key>+<hotbar_key> combos
for (KeyMapping combo : mc.options.keyHotbarSlots) {
while (combo.consumeClick()) {
toggleTripod(TripodSlot.ofKeyCode(combo.getDefaultKey().getValue()));
toggleKeyUsedWhileHeld = true;
}
}
}
// Check if toggle was pressed, and is now released
else if (KEY_TOGGLE.consumeClick() || toggleKeyHeldTicks > 0) {
// Only toggle if the key wasn't used (or held too long)
if (!toggleKeyUsedWhileHeld && toggleKeyHeldTicks < TOGGLE_KEY_MAX_TICKS) {
toggle();
}
// Reset state
KEY_TOGGLE.reset();
toggleKeyHeldTicks = 0;
toggleKeyUsedWhileHeld = false;
}

if (KEY_TOGGLE.isPressed()) {
for (KeyMapping hotbarKey : mc.options.keyHotbarSlots) {
while (hotbarKey.consumeClick()) {
toggleTripod(hotbarKey.getDefaultKey().getValue());
while (KEY_TOGGLE.wasPressed()) {}
// Handle <reset_key>+<hotbar_key> combos
if (KEY_TRIPOD_RESET.isDown()) {
for (KeyMapping key : mc.options.keyHotbarSlots) {
while (key.consumeClick()) {
resetCamera(TripodSlot.ofKeyCode(key.getDefaultKey().getValue()));
}
}
} else if (KEY_TOGGLE.wasPressed()) {
toggle();
while (KEY_TOGGLE.wasPressed()) {}
}

while (KEY_PLAYER_CONTROL.wasPressed()) {
while (KEY_PLAYER_CONTROL.consumeClick()) {
switchControls();
}

while (KEY_CONFIG_GUI.wasPressed()) {
while (KEY_CONFIG_GUI.consumeClick()) {
mc.setScreen(AutoConfig.getConfigScreen(ModConfig.class, mc.screen).get());
}
}
Expand All @@ -88,7 +103,7 @@ public static void onDisconnect() {
if (isEnabled()) {
toggle();
}
clearTripods();
tripods.clear();
}

public static void toggle() {
Expand All @@ -108,24 +123,24 @@ public static void toggle() {
}
}

private static void toggleTripod(Integer keyCode) {
if (keyCode == null) {
private static void toggleTripod(TripodSlot tripod) {
if (tripod == TripodSlot.NONE) {
return;
}

if (tripodEnabled) {
if (activeTripod.equals(keyCode)) {
if (activeTripod == tripod) {
onDisableTripod();
tripodEnabled = false;
} else {
onDisableTripod();
onEnableTripod(keyCode);
onEnableTripod(tripod);
}
} else {
if (freecamEnabled) {
toggle();
}
onEnableTripod(keyCode);
onEnableTripod(tripod);
tripodEnabled = true;
}
if (!tripodEnabled) {
Expand All @@ -147,55 +162,53 @@ public static void switchControls() {
playerControlEnabled = !playerControlEnabled;
}

private static void onEnableTripod(int keyCode) {
private static void onEnableTripod(TripodSlot tripod) {
onEnable();

FreecamPosition position = getTripodsForDimension().get(keyCode);
FreecamPosition position = tripods.get(tripod);
boolean chunkLoaded = false;
if (position != null) {
ChunkPos chunkPos = position.getChunkPos();
chunkLoaded = MC.level.getChunkSource().hasChunk(chunkPos.x, chunkPos.z);
}

if (!chunkLoaded) {
resetCamera(keyCode);
resetCamera(tripod);
position = null;
}

freeCamera = new FreeCamera(-420 - tripod.ordinal());
if (position == null) {
freeCamera = new FreeCamera(-420 - (keyCode % GLFW.GLFW_KEY_0));
moveToPlayer();
} else {
freeCamera = new FreeCamera(-420 - (keyCode % GLFW.GLFW_KEY_0), position);
moveToPosition(position);
}

freeCamera.spawn();
MC.setCameraEntity(freeCamera);
activeTripod = keyCode;
activeTripod = tripod;

if (ModConfig.INSTANCE.notification.notifyTripod) {
MC.player.displayClientMessage(Component.translatable("msg.freecam.openTripod").append("" + activeTripod % GLFW.GLFW_KEY_0), true);
MC.player.displayClientMessage(Component.translatable("msg.freecam.openTripod", tripod), true);
}
}

private static void onDisableTripod() {
getTripodsForDimension().put(activeTripod, new FreecamPosition(freeCamera));
tripods.put(activeTripod, new FreecamPosition(freeCamera));
onDisable();

if (MC.player != null) {
if (ModConfig.INSTANCE.notification.notifyTripod) {
MC.player.displayClientMessage(Component.translatable("msg.freecam.closeTripod").append("" + activeTripod % GLFW.GLFW_KEY_0), true);
MC.player.displayClientMessage(Component.translatable("msg.freecam.closeTripod", activeTripod), true);
}
}
activeTripod = null;
activeTripod = TripodSlot.NONE;
}

private static void onEnableFreecam() {
onEnable();
freeCamera = new FreeCamera(-420);
freeCamera.applyPerspective(
ModConfig.INSTANCE.visual.perspective,
ModConfig.INSTANCE.collision.alwaysCheck || !(ModConfig.INSTANCE.collision.ignoreAll && BuildVariant.getInstance().cheatsPermitted())
);
moveToPlayer();
freeCamera.spawn();
MC.setCameraEntity(freeCamera);

Expand Down Expand Up @@ -244,50 +257,57 @@ private static void onDisabled() {
}
}

private static void resetCamera(int keyCode) {
if (tripodEnabled && activeTripod != null && activeTripod == keyCode && freeCamera != null) {
freeCamera.copyPosition(MC.player);
private static void resetCamera(TripodSlot tripod) {
if (tripodEnabled && activeTripod != TripodSlot.NONE && activeTripod == tripod && freeCamera != null) {
moveToPlayer();
} else {
getTripodsForDimension().put(keyCode, null);
tripods.put(tripod, null);
}

if (ModConfig.INSTANCE.notification.notifyTripod) {
MC.player.displayClientMessage(Component.translatable("msg.freecam.tripodReset").append("" + keyCode % GLFW.GLFW_KEY_0), true);
MC.player.displayClientMessage(Component.translatable("msg.freecam.tripodReset", tripod), true);
}
}

public static void clearTripods() {
overworld_tripods = new HashMap<>();
nether_tripods = new HashMap<>();
end_tripods = new HashMap<>();
public static void moveToEntity(@Nullable Entity entity) {
if (freeCamera == null) {
return;
}
if (entity == null) {
moveToPlayer();
return;
}
freeCamera.copyPosition(entity);
}

public static FreeCamera getFreeCamera() {
return freeCamera;
public static void moveToPosition(@Nullable FreecamPosition position) {
if (freeCamera == null) {
return;
}
if (position == null) {
moveToPlayer();
return;
}
freeCamera.applyPosition(position);
}

public static HashMap<Integer, FreecamPosition> getTripodsForDimension() {
HashMap<Integer, FreecamPosition> result;
switch (MC.level.dimensionTypeId().location().getPath()) {
case "the_nether":
result = nether_tripods;
break;
case "the_end":
result = end_tripods;
break;
default:
result = overworld_tripods;
break;
}
return result;
public static void moveToPlayer() {
if (freeCamera == null) {
return;
}
freeCamera.copyPosition(MC.player);
freeCamera.applyPerspective(
ModConfig.INSTANCE.visual.perspective,
ModConfig.INSTANCE.collision.alwaysCheck || !(ModConfig.INSTANCE.collision.ignoreAll && BuildVariant.getInstance().cheatsPermitted())
);
}

public static boolean disableNextTick() {
return disableNextTick;
public static FreeCamera getFreeCamera() {
return freeCamera;
}

public static void setDisableNextTick(boolean damage) {
disableNextTick = damage;
public static void disableNextTick() {
disableNextTick = true;
}

public static boolean isEnabled() {
Expand Down
23 changes: 17 additions & 6 deletions common/src/main/java/net/xolt/freecam/config/ModBindings.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.mojang.blaze3d.platform.InputConstants;
import net.minecraft.client.KeyMapping;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.Iterator;
import java.util.Objects;
import java.util.Spliterator;
import java.util.function.Consumer;
import net.minecraft.client.KeyMapping;

import static org.lwjgl.glfw.GLFW.GLFW_KEY_F4;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_UNKNOWN;
Expand All @@ -22,7 +22,7 @@ public enum ModBindings {
KEY_TRIPOD_RESET("tripodReset"),
KEY_CONFIG_GUI("configGui");

private final Supplier<KeyMapping> lazyBinding;
private final Supplier<KeyMapping> lazyMapping;

ModBindings(String translationKey) {
this(translationKey, InputConstants.Type.KEYSYM, GLFW_KEY_UNKNOWN);
Expand All @@ -37,26 +37,37 @@ public enum ModBindings {
}

ModBindings(String translationKey, InputConstants.Type type, int code) {
this.lazyBinding = Suppliers.memoize(() ->
this.lazyMapping = Suppliers.memoize(() ->
new KeyMapping("key.freecam." + translationKey, type, code, "category.freecam.freecam"));
}

/**
* @return the result of calling {@link KeyMapping#isDown()} on the represented {@link KeyMapping}.
* @see KeyMapping#isDown()
*/
public boolean isPressed() {
public boolean isDown() {
return get().isDown();
}

/**
* @return the result of calling {@link KeyMapping#consumeClick()} on the represented {@link KeyMapping}.
* @see KeyMapping#consumeClick()
*/
public boolean wasPressed() {
public boolean consumeClick() {
return get().consumeClick();
}

/**
* Reset whether the key was pressed.
* <p>
* Note: Cannot use {@link KeyMapping#release()} because it doesn't work as expected.
*/
@SuppressWarnings("StatementWithEmptyBody")
public void reset() {
final KeyMapping key = get();
while (key.consumeClick()) {}
}

/**
* Lazily get the actual {@link KeyMapping} represented by this enum value.
* <p>
Expand All @@ -65,7 +76,7 @@ public boolean wasPressed() {
* @return the actual {@link KeyMapping}.
*/
public KeyMapping get() {
return lazyBinding.get();
return lazyMapping.get();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private void onGetMovementSpeed(CallbackInfoReturnable<Float> cir) {
private void onSetHealth(float health, CallbackInfo ci) {
if (Freecam.isEnabled() && ModConfig.INSTANCE.utility.disableOnDamage && this.equals(MC.player)) {
if (!MC.player.isCreative() && getHealth() > health) {
Freecam.setDisableNextTick(true);
Freecam.disableNextTick();
}
}
}
Expand Down
Loading