Skip to content

Commit

Permalink
Merge pull request #263 from boholder/refactor-poi
Browse files Browse the repository at this point in the history
Some improvements, some refactor
  • Loading branch information
khanshoaib3 authored Feb 10, 2024
2 parents 147f473 + 49f6c6a commit fdf9274
Show file tree
Hide file tree
Showing 15 changed files with 354 additions and 348 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ jobs:
>> ${{ env.CHANGELOG_TEMP_GITHUB }}
cp .changelog_temp ${{ env.CHANGELOG_TEMP_PLATFORM }}
GH_LINK="For additional information about this release, please check [this GitHub release page](https://github.com/khanshoaib3/minecraft-access/releases/tag/)."
printf ${GH_LINK} >> ${{ env.CHANGELOG_TEMP_PLATFORM }}
printf "For additional information about this release, please check [this GitHub release page](https://github.com/khanshoaib3/minecraft-access/releases/tag/)." >> ${{ env.CHANGELOG_TEMP_PLATFORM }}
# Action ref: https://github.com/Kir-Antipov/mc-publish
- name: Create Github Release
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.github.khanshoaib3.minecraft_access.MainClass;
import com.github.khanshoaib3.minecraft_access.utils.KeyBindingsHandler;
import com.github.khanshoaib3.minecraft_access.utils.NarrationUtils;
import com.github.khanshoaib3.minecraft_access.utils.position.PlayerPositionUtils;
import com.github.khanshoaib3.minecraft_access.utils.system.KeyUtils;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -29,19 +28,11 @@ public void update() {

String toSpeak;
if (isLeftAltPressed) {
int angle = new PlayerPositionUtils(minecraftClient).getVerticalFacingDirection();
toSpeak = NarrationUtils.narrateNumber(angle);
String t = new PlayerPositionUtils(minecraftClient).getVerticalFacingDirectionInWords();
toSpeak = I18n.translate("minecraft_access.other.facing_direction", t);
} else {

/* TODO add to config and add left alt combination to readme
int angle = new PlayerPositionUtils(minecraftClient).getHorizontalFacingDirectionInDegrees();
if (Config.get(Config.getCardinal_to_Degrees_Key())) {
toSpeak += angle;
} else {
*/
String string = new PlayerPositionUtils(minecraftClient).getHorizontalFacingDirectionInCardinal();
toSpeak = I18n.translate("minecraft_access.other.facing_direction", string);
// }
}

MainClass.speakWithNarrator(toSpeak, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
import net.minecraft.client.resource.language.I18n;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.fluid.Fluid;
import net.minecraft.fluid.FluidState;
import net.minecraft.registry.Registries;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.Identifier;
import net.minecraft.util.Pair;
Expand All @@ -40,7 +37,8 @@ public class ReadCrosshair {
private static final double RAY_CAST_DISTANCE = 6.0;
private static ReadCrosshair instance;
private boolean enabled;
private String previousQuery;
private String previousQuery = "";
private Vec3d previousSoundPos = Vec3d.ZERO;
private boolean speakSide;
private boolean speakingConsecutiveBlocks;
private Interval repeatSpeakingInterval;
Expand All @@ -55,7 +53,6 @@ public class ReadCrosshair {
private double maxSoundVolume;

private ReadCrosshair() {
previousQuery = "";
loadConfigurations();
}

Expand Down Expand Up @@ -84,20 +81,10 @@ public void update() {
loadConfigurations();
if (!enabled) return;

Entity entity = minecraftClient.getCameraEntity();
if (entity == null) return;
HitResult hit = PlayerUtils.crosshairTarget(RAY_CAST_DISTANCE);
if (hit == null) return;
checkForBlockAndEntityHit(hit);

HitResult blockHit = minecraftClient.crosshairTarget;
HitResult fluidHit = entity.raycast(RAY_CAST_DISTANCE, 0.0F, true);

if (blockHit == null) return;

boolean playerIsInFluid = PlayerUtils.isInFluid();
boolean playerLooksAtFluid = checkForFluidHit(minecraftClient, fluidHit);

if (playerIsInFluid && playerLooksAtFluid) return;

checkForBlockAndEntityHit(blockHit);
} catch (Exception e) {
log.error("Error occurred in read block feature.", e);
}
Expand Down Expand Up @@ -173,9 +160,10 @@ private void checkForEntities(EntityHitResult hit) {
private void speakIfFocusChanged(String currentQuery, String toSpeak, Vec3d targetPosition) {
boolean focusChanged = !getPreviousQuery().equalsIgnoreCase(currentQuery);
if (focusChanged) {
if (this.enableRelativePositionSoundCue) {
if (this.enableRelativePositionSoundCue && !this.previousSoundPos.equals(targetPosition)) {
WorldUtils.playRelativePositionSoundCue(targetPosition, RAY_CAST_DISTANCE,
SoundEvents.BLOCK_NOTE_BLOCK_HARP, this.minSoundVolume, this.maxSoundVolume);
this.previousSoundPos = targetPosition;
}
this.previousQuery = currentQuery;
MainClass.speakWithNarrator(toSpeak, true);
Expand All @@ -190,7 +178,7 @@ private void checkForBlocks(BlockHitResult hit) {
}

BlockPos blockPos = hit.getBlockPos();
WorldUtils.BlockInfo blockInfo = WorldUtils.getBlockInfo(blockPos).orElseThrow();
WorldUtils.BlockInfo blockInfo = WorldUtils.getBlockInfo(blockPos);
// In Minecraft resource location format, for example, "oak_door" for Oak Door.
// ref: https://minecraft.wiki/w/Java_Edition_data_values#Blocks
Identifier blockId = Registries.BLOCK.getId(blockInfo.type());
Expand All @@ -212,57 +200,6 @@ private void checkForBlocks(BlockHitResult hit) {
speakIfFocusChanged(currentQuery, toSpeakAndCurrentQuery.getLeft(), Vec3d.of(blockPos));
}

private boolean checkForFluidHit(MinecraftClient minecraftClient, HitResult fluidHit) {
if (minecraftClient == null) return false;
if (minecraftClient.world == null) return false;
if (minecraftClient.currentScreen != null) return false;

if (fluidHit.getType() == HitResult.Type.BLOCK) {
BlockPos blockPos = ((BlockHitResult) fluidHit).getBlockPos();
FluidState fluidState = minecraftClient.world.getFluidState(blockPos);

String name = getFluidName(fluidState.getRegistryEntry());
if (name.equals("block.minecraft.empty")) return false;
if (name.contains("block.minecraft."))
name = name.replace("block.minecraft.", ""); // Remove `block.minecraft.` for unsupported languages

String currentQuery = name + blockPos;

int level = fluidState.getLevel();
String levelString = "";
if (level < 8) levelString = I18n.translate("minecraft_access.read_crosshair.fluid_level", level);

String toSpeak = name + levelString;

speakIfFocusChanged(currentQuery, toSpeak, Vec3d.of(blockPos));
return true;
}
return false;
}

/**
* Gets the fluid name from registry entry
*
* @param fluid the fluid's registry entry
* @return the fluid's name
*/
public static String getFluidName(RegistryEntry<Fluid> fluid) {
return I18n.translate(getFluidTranslationKey(fluid));
}

/**
* Gets the fluid translation key from registry entry
*
* @param fluid the fluid's registry entry
* @return the fluid's translation key
*/
private static String getFluidTranslationKey(RegistryEntry<Fluid> fluid) {
return fluid.getKeyOrValue().map(
(fluidKey) -> "block." + fluidKey.getValue().getNamespace() + "." + fluidKey.getValue().getPath(),
(fluidValue) -> "[unregistered " + fluidValue + "]" // For unregistered fluid
);
}

private boolean checkIfPartialSpeakingFeatureDoesNotAllowsSpeakingThis(Identifier id) {
if (id == null) return false;
String name = id.getPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,23 @@
import com.github.khanshoaib3.minecraft_access.config.ConfigMenu;
import com.github.khanshoaib3.minecraft_access.config.config_maps.OtherConfigsMap;
import com.github.khanshoaib3.minecraft_access.features.BiomeIndicator;
import com.github.khanshoaib3.minecraft_access.features.ReadCrosshair;
import com.github.khanshoaib3.minecraft_access.screen_reader.ScreenReaderController;
import com.github.khanshoaib3.minecraft_access.utils.KeyBindingsHandler;
import com.github.khanshoaib3.minecraft_access.utils.NarrationUtils;
import com.github.khanshoaib3.minecraft_access.utils.PlayerUtils;
import com.github.khanshoaib3.minecraft_access.utils.WorldUtils;
import com.github.khanshoaib3.minecraft_access.utils.condition.Keystroke;
import com.github.khanshoaib3.minecraft_access.utils.condition.MenuKeystroke;
import com.github.khanshoaib3.minecraft_access.utils.system.KeyUtils;
import lombok.extern.slf4j.Slf4j;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.resource.language.I18n;
import net.minecraft.client.util.InputUtil;
import net.minecraft.fluid.FluidState;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.text.MutableText;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.biome.Biome;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.glfw.GLFW;

import java.util.Arrays;
Expand Down Expand Up @@ -177,23 +170,15 @@ private void openNarratorMenu() {

public static void getBlockAndFluidTargetInformation() {
try {
HitResult hit = getBlockAndFluidHitResult();
HitResult hit = PlayerUtils.crosshairTarget(RAY_CAST_DISTANCE);
if (hit == null) return;

if (!PlayerUtils.isInFluid() && checkForFluidHit(minecraftClient, hit, false)) return;

switch (hit.getType()) {
case MISS, ENTITY -> MainClass.speakWithNarrator(I18n.translate("minecraft_access.narrator_menu.target_missed"), true);
case BLOCK -> {
try {
BlockHitResult blockHit = (BlockHitResult) hit;
BlockPos blockPos = blockHit.getBlockPos();

BlockState blockState = WorldUtils.getClientWorld().orElseThrow().getBlockState(blockPos);
Block block = blockState.getBlock();
MutableText mutableText = block.getName();

String text = mutableText.getString() + ", " + NarrationUtils.narrateRelativePositionOfPlayerAnd(blockPos);
String text = NarrationUtils.narrateBlock(blockPos, "") + ", " + NarrationUtils.narrateRelativePositionOfPlayerAnd(blockPos);
MainClass.speakWithNarrator(text, true);
} catch (Exception e) {
log.error("An error occurred when speaking block information.", e);
Expand All @@ -205,24 +190,10 @@ public static void getBlockAndFluidTargetInformation() {
}
}

@Nullable
private static HitResult getBlockAndFluidHitResult() {
if (minecraftClient.player == null) return null;
if (minecraftClient.cameraEntity == null) return null;
if (minecraftClient.world == null) return null;

minecraftClient.player.closeScreen();

return minecraftClient.cameraEntity.raycast(RAY_CAST_DISTANCE, 0.0f, true);
}

public static void getBlockAndFluidTargetPosition() {
try {
HitResult hit = getBlockAndFluidHitResult();
HitResult hit = PlayerUtils.crosshairTarget(RAY_CAST_DISTANCE);
if (hit == null) return;

if (!PlayerUtils.isInFluid() && checkForFluidHit(minecraftClient, hit, true)) return;

switch (hit.getType()) {
case MISS, ENTITY -> MainClass.speakWithNarrator(I18n.translate("minecraft_access.narrator_menu.target_missed"), true);
case BLOCK -> {
Expand All @@ -240,37 +211,6 @@ public static void getBlockAndFluidTargetPosition() {
}
}

public static boolean checkForFluidHit(MinecraftClient minecraftClient, HitResult fluidHit, boolean onlyPosition) {
if (minecraftClient == null) return false;
if (minecraftClient.world == null) return false;
if (minecraftClient.currentScreen != null) return false;

if (fluidHit.getType() == HitResult.Type.BLOCK) {
BlockPos blockPos = ((BlockHitResult) fluidHit).getBlockPos();
FluidState fluidState = minecraftClient.world.getFluidState(blockPos);

String name = ReadCrosshair.getFluidName(fluidState.getRegistryEntry());
if (name.equals("block.minecraft.empty")) return false;
if (name.contains("block.minecraft."))
name = name.replace("block.minecraft.", ""); // Remove `block.minecraft.` for unsupported languages

if (onlyPosition) {
MainClass.speakWithNarrator(NarrationUtils.narrateCoordinatesOf(blockPos), true);
return true;
}

int level = fluidState.getLevel();
String levelString = "";
if (level < 8) levelString = I18n.translate("minecraft_access.read_crosshair.fluid_level", level);

String toSpeak = name + levelString + ", " + NarrationUtils.narrateRelativePositionOfPlayerAnd(blockPos);

MainClass.speakWithNarrator(toSpeak, true);
return true;
}
return false;
}

public static void getLightLevel() {
try {
if (minecraftClient.player == null) return;
Expand Down Expand Up @@ -306,7 +246,10 @@ public static void getXP() {

minecraftClient.player.closeScreen();

MainClass.speakWithNarrator(I18n.translate("minecraft_access.narrator_menu.xp", PlayerUtils.getExperienceLevel(), PlayerUtils.getExperienceProgress()), true);
MainClass.speakWithNarrator(I18n.translate("minecraft_access.narrator_menu.xp",
PlayerUtils.getExperienceLevel(),
PlayerUtils.getExperienceProgress()),
true);
} catch (Exception e) {
log.error("An error occurred when getting XP.", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ private void findAndLockOnNearestBlock() {
private void lockOnBlock(Vec3d position) {
unlock(false);

BlockState blockState = WorldUtils.getClientWorld().orElseThrow().getBlockState(new BlockPos3d(position));
BlockState blockState = WorldUtils.getClientWorld().getBlockState(new BlockPos3d(position));
entriesOfLockedOnBlock = blockState.getEntries().toString();

Vec3d absolutePosition = position;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.jetbrains.annotations.NotNull;

import java.util.Map;
import java.util.Optional;

/**
* The position of the block (blockPos.toCenterPos()) is generally considered to be the center of the block (x.5,y.5,z.5).
Expand All @@ -19,10 +18,8 @@
*/
public class NonCubeBlockAbsolutePositions {
public static Vec3d getTrapDoorPos(Vec3d blockPos) {
Optional<ClientWorld> world = WorldUtils.getClientWorld();
if (world.isEmpty()) return blockPos;

ImmutableSet<Map.Entry<Property<?>, Comparable<?>>> entries = getEntries(blockPos, world.get());
ClientWorld world = WorldUtils.getClientWorld();
ImmutableSet<Map.Entry<Property<?>, Comparable<?>>> entries = getEntries(blockPos, world);

String half = "", facing = "", open = "";

Expand Down Expand Up @@ -62,10 +59,8 @@ else if (half.equalsIgnoreCase("top"))
}

public static Vec3d getLeverPos(Vec3d blockPos) {
Optional<ClientWorld> world = WorldUtils.getClientWorld();
if (world.isEmpty()) return blockPos;

ImmutableSet<Map.Entry<Property<?>, Comparable<?>>> entries = getEntries(blockPos, world.get());
ClientWorld world = WorldUtils.getClientWorld();
ImmutableSet<Map.Entry<Property<?>, Comparable<?>>> entries = getEntries(blockPos, world);

String face = "", facing = "";

Expand Down Expand Up @@ -103,10 +98,8 @@ else if (facing.equalsIgnoreCase("west"))
}

public static Vec3d getLadderPos(Vec3d blockPos) {
Optional<ClientWorld> world = WorldUtils.getClientWorld();
if (world.isEmpty()) return blockPos;

ImmutableSet<Map.Entry<Property<?>, Comparable<?>>> entries = getEntries(blockPos, world.get());
ClientWorld world = WorldUtils.getClientWorld();
ImmutableSet<Map.Entry<Property<?>, Comparable<?>>> entries = getEntries(blockPos, world);

String facing = "";

Expand Down Expand Up @@ -136,10 +129,8 @@ else if (facing.equalsIgnoreCase("east"))
}

public static Vec3d getButtonPos(Vec3d blockPos) {
Optional<ClientWorld> world = WorldUtils.getClientWorld();
if (world.isEmpty()) return blockPos;

ImmutableSet<Map.Entry<Property<?>, Comparable<?>>> entries = getEntries(blockPos, world.get());
ClientWorld world = WorldUtils.getClientWorld();
ImmutableSet<Map.Entry<Property<?>, Comparable<?>>> entries = getEntries(blockPos, world);

double x = blockPos.getX();
double y = blockPos.getY();
Expand Down Expand Up @@ -176,10 +167,8 @@ else if (facing.equalsIgnoreCase("west"))
}

public static Vec3d getDoorPos(Vec3d blockPos) {
Optional<ClientWorld> world = WorldUtils.getClientWorld();
if (world.isEmpty()) return blockPos;

ImmutableSet<Map.Entry<Property<?>, Comparable<?>>> entries = getEntries(blockPos, world.get());
ClientWorld world = WorldUtils.getClientWorld();
ImmutableSet<Map.Entry<Property<?>, Comparable<?>>> entries = getEntries(blockPos, world);

String facing = "", hinge = "", open = "";

Expand Down
Loading

0 comments on commit fdf9274

Please sign in to comment.