Skip to content

Commit

Permalink
Merge pull request #17 from KadTheHunter/MrErenK
Browse files Browse the repository at this point in the history
Fixes #15, cleans up misc. things, adds Turkish translations.
  • Loading branch information
KadTheHunter authored Jul 1, 2024
2 parents e84331f + 6e87a61 commit 189949e
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 161 deletions.
84 changes: 11 additions & 73 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,85 +10,23 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: 'Checkout repository'
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: 'Validate gradle wrapper'
uses: gradle/wrapper-validation-action@v1
- name: 'Setup JDK 17'
uses: actions/setup-java@v3
uses: gradle/wrapper-validation-action@v3
- name: 'Setup JDK 21'
uses: actions/setup-java@v4
with:
distribution: 'adopt'
java-version: '17'
- name: 'Make gradle wrapper executable'
run: chmod +x ./gradlew
- name: 'Build mod'
run: ./gradlew build
- name: 'Get tag'
if: startsWith(github.ref, 'refs/tags/')
id: git_tag
run: echo ::set-output name=TAG::${GITHUB_REF/refs\/tags\//}
- name: Build changelog
if: startsWith(github.ref, 'refs/tags/')
id: changelog_release
uses: mikepenz/release-changelog-builder-action@v3
- name: 'Get the mod version'
id: get_mod_version
run: echo "MOD_VERSION=$(grep '^mod_version\s*=\s*' gradle.properties | awk -F'= ' '{print $2}')" >> $GITHUB_ENV
- name: 'Upload artifact'
uses: actions/upload-artifact@v4
with:
commitMode: true
configurationJson: |
{
"categories": [
{
"title": "### New Features",
"labels": ["feat"]
},
{
"title": "### Changes",
"labels": ["change", "impr"]
},
{
"title": "### Fixes",
"labels": ["fix"]
},
{
"title": "### Localization",
"labels": ["loc"]
},
{
"title": "### Other",
"labels": []
}
],
"ignore_labels": [
"build",
"refactor",
"docs"
],
"sort": {
"order": "DESC",
"on_property": "mergedAt"
},
"label_extractor": [
{
"pattern": "(.*): (.*)",
"target": "$1",
"on_property": "title"
}
],
"transformers": [
{
"pattern": "(.*): (.*)",
"target": "* $2"
}
],
"template": "## Changelog\n\n#{{CHANGELOG}}",
"pr_template": "#{{TITLE}}",
"empty_template": "CHANGEME"
}
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
- name: 'Draft a new release'
if: startsWith(github.ref, 'refs/tags/')
uses: softprops/action-gh-release@v1
with:
draft: true
name: 'Accurate Block Placement v${{steps.git_tag.outputs.TAG}}'
body: ${{steps.changelog_release.outputs.changelog}}
files: 'build/libs/accurate-block-placement-${{steps.git_tag.outputs.TAG}}.jar'
name: AccurateBlockPlacementRebornMOD
path: "build/libs/accurate-block-placement-${{ env.MOD_VERSION }}.jar"
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ org.gradle.jvmargs=-Xmx4G

# Fabric Properties (https://fabricmc.net/versions.html)
minecraft_version=1.21
yarn_mappings=1.21+build.2
yarn_mappings=1.21+build.7
loader_version=0.15.11

# Mod Properties (to automatically tag on linux: 'git tag $(grep -oP '^mod_version = \K.*' gradle.properties)')
mod_version = 1.2.4
mod_version = 1.2.5
maven_group = net.clayborn
archives_base_name = accurate-block-placement

# Dependencies
fabric_version=0.100.3+1.21
fabric_version=0.100.4+1.21
Original file line number Diff line number Diff line change
@@ -1,55 +1,58 @@
package net.clayborn.accurateblockplacement.mixin;

import java.lang.reflect.Method;
import java.util.ArrayList;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.clayborn.accurateblockplacement.AccurateBlockPlacementMod;
import net.clayborn.accurateblockplacement.IKeyBindingAccessor;
import net.clayborn.accurateblockplacement.IMinecraftClientAccessor;
import net.minecraft.block.AbstractBlock;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.StairsBlock;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.GameRenderer;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.AliasedBlockItem;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.item.MiningToolItem;
import net.minecraft.item.*;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.math.Direction.Axis;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.lang.reflect.Method;
import java.util.ArrayList;

@Mixin(GameRenderer.class)
public abstract class GameRendererMixin
{
@Unique
private static final String blockActivateMethodName = getBlockActivateMethodName();
@Unique
private static final String itemUseMethodName = getItemUseMethodName();

@Unique
private BlockPos lastSeenBlockPos = null;
@Unique
private BlockPos lastPlacedBlockPos = null;
@Unique
private Vec3d lastPlayerPlacedBlockPos = null;
@Unique
private Boolean autoRepeatWaitingOnCooldown = true;
@Unique
private Vec3d lastFreshPressMouseRatio = null;
private ArrayList<HitResult> backFillList = new ArrayList<HitResult>();
@Unique
private ArrayList<HitResult> backFillList = new ArrayList<>();
@Unique
private Item lastItemInUse = null;

@Unique
Hand handOfCurrentItemInUse;

@Unique
private Item getItemInUse(MinecraftClient client)
{
// have to check each hand
Expand All @@ -58,6 +61,7 @@ private Item getItemInUse(MinecraftClient client)

for(int i = 0; i < numHands; ++i) {
Hand thisHand = hands[i];
assert client.player!= null;
ItemStack itemInHand = client.player.getStackInHand(thisHand);

if(itemInHand.isEmpty()) {
Expand All @@ -73,6 +77,7 @@ private Item getItemInUse(MinecraftClient client)
return null;
}

@Unique
private static String getBlockActivateMethodName()
{
Method[] methods = Block.class.getMethods();
Expand Down Expand Up @@ -101,69 +106,52 @@ private static String getBlockActivateMethodName()
if(types[5] != BlockHitResult.class) {
continue;
}

return method.getName();
}

return null;
}

private Boolean doesBlockHaveOverriddenActivateMethod(Block block)
@Unique
private static String getItemUseMethodName()
{
if(blockActivateMethodName == null) {
System.out.println("[ERROR] blockActivateMethodName is null!");
}

// TODO: consider cache of results

try {
return !block.getClass().getMethod(blockActivateMethodName, BlockState.class, World.class, BlockPos.class, PlayerEntity.class, Hand.class, BlockHitResult.class).getDeclaringClass().equals(AbstractBlock.class);
Method useMethod = Item.class.getDeclaredMethod("use", World.class, PlayerEntity.class, Hand.class);
return useMethod.getName();
}
catch(Exception e) {
System.out.println("[ERROR] Unable to find block " + block.getClass().getName() + " activate method!");
return false;
catch (NoSuchMethodException e) {
return null;
}
}

private static String getItemUseMethodName()
@Unique
private static boolean doesBlockHaveOverriddenActivateMethod(Block block)
{
Method[] methods = Item.class.getMethods();

for(Method method : methods) {
Class<?>[] types = method.getParameterTypes();

if(types.length != 3) {
continue;
}
if(types[0] != World.class) {
continue;
}
if(types[1] != PlayerEntity.class) {
continue;
}
if(types[2] != Hand.class) {
continue;
}
if(blockActivateMethodName == null) {
return false;
}

return method.getName();
try {
Method activateMethod = block.getClass().getDeclaredMethod(blockActivateMethodName, BlockState.class, World.class, BlockPos.class, PlayerEntity.class, Hand.class, BlockHitResult.class);
return activateMethod.getDeclaringClass()!= Block.class;
}
catch (NoSuchMethodException e) {
return false;
}

return null;
}

private Boolean doesItemHaveOverriddenUseMethod(Item item)
@Unique
private static boolean doesItemHaveOverriddenUseMethod(Item item)
{
if(itemUseMethodName == null) {
System.out.println("[ERROR] itemUseMethodName is null!");
return false;
}

// TODO: consider cache of results

try {
return !item.getClass().getMethod(itemUseMethodName, World.class, PlayerEntity.class, Hand.class).getDeclaringClass().equals(Item.class);
Method useMethod = item.getClass().getDeclaredMethod(itemUseMethodName, ItemStack.class, World.class, PlayerEntity.class, Hand.class, BlockHitResult.class);
return useMethod.getDeclaringClass()!= Item.class;
}
catch (Exception e) {
System.out.println("[ERROR] Unable to find item " + item.getClass().getName() + " use method!");
catch (NoSuchMethodException e) {
return false;
}
}
Expand Down Expand Up @@ -198,8 +186,8 @@ private void onUpdateTargetedEntityComplete(CallbackInfo info)

// will be set to true only if needed
AccurateBlockPlacementMod.disableNormalItemUse = false;
IKeyBindingAccessor keyUseAccessor = (IKeyBindingAccessor) (Object) client.options.useKey;
Boolean freshKeyPress = keyUseAccessor.accurateblockplacement_GetTimesPressed() > 0;
IKeyBindingAccessor keyUseAccessor = (IKeyBindingAccessor) client.options.useKey;
boolean freshKeyPress = keyUseAccessor.accurateblockplacement_GetTimesPressed() > 0;

Item currentItem = getItemInUse(client);

Expand Down Expand Up @@ -256,7 +244,7 @@ private void onUpdateTargetedEntityComplete(CallbackInfo info)
BlockHitResult blockHitResult = (BlockHitResult) client.crosshairTarget;
BlockPos blockHitPos = blockHitResult.getBlockPos();
Block targetBlock = client.world.getBlockState(blockHitPos).getBlock();
Boolean isTargetBlockActivatable = doesBlockHaveOverriddenActivateMethod(targetBlock);
boolean isTargetBlockActivatable = doesBlockHaveOverriddenActivateMethod(targetBlock);

// don't override behavior of clicking activatable blocks (and stairs) unless holding SNEAKING to replicate vanilla behaviors
if(isTargetBlockActivatable && !(targetBlock instanceof StairsBlock) && !client.player.isSneaking()) {
Expand Down Expand Up @@ -306,21 +294,21 @@ private void onUpdateTargetedEntityComplete(CallbackInfo info)
// [ we have no 'place' history or the 'place' history isn't a match ] ] OR
// [ we have 'place' history, it is a match, the player is building toward
// themselves and has moved one block backwards] ]
Boolean isPlacementTargetFresh = ((lastSeenBlockPos == null || !lastSeenBlockPos.equals(blockHitPos))
&& (lastPlacedBlockPos == null || !lastPlacedBlockPos.equals(blockHitPos)))
|| (lastPlacedBlockPos != null && lastPlayerPlacedBlockPos != null
boolean isPlacementTargetFresh = ((lastSeenBlockPos == null || !lastSeenBlockPos.equals(blockHitPos))
&& (lastPlacedBlockPos == null || !lastPlacedBlockPos.equals(blockHitPos)))
|| (lastPlacedBlockPos != null && lastPlayerPlacedBlockPos != null
&& lastPlacedBlockPos.equals(blockHitPos)
&& Math.abs(facingAxisPlayerLastPos - facingAxisPlayerPos) >= 0.99d // because precision
&& Math.abs(facingAxisPlayerLastPos - facingAxisLastPlacedPos) < Math.abs(facingAxisPlayerPos - facingAxisLastPlacedPos));

Boolean hasMouseMoved = (currentMouseRatio != null && lastFreshPressMouseRatio != null && lastFreshPressMouseRatio.distanceTo(currentMouseRatio) >= 0.1);
boolean hasMouseMoved = (currentMouseRatio != null && lastFreshPressMouseRatio != null && lastFreshPressMouseRatio.distanceTo(currentMouseRatio) >= 0.1);

Boolean isOnCooldown = autoRepeatWaitingOnCooldown && clientAccessor.accurateblockplacement_GetItemUseCooldown() > 0 && !hasMouseMoved;
boolean isOnCooldown = autoRepeatWaitingOnCooldown && clientAccessor.accurateblockplacement_GetItemUseCooldown() > 0 && !hasMouseMoved;

// if [ we are still holding the same block we starting pressing 'use' with] AND
// if [ we are still holding the same block we start pressing 'use' with] AND
// [ [ this is a fresh keypress ] OR
// [ [ we have a fresh place to put a block ] AND
// [ auto repeat isn't on cooldown OR the mouse has moved enough ] ]
// [ auto-repeat isn't on cooldown OR the mouse has moved enough ] ]
// we can try to place a block
// note: this is always true on a fresh keypress
if(lastItemInUse == currentItem) {
Expand All @@ -345,7 +333,7 @@ private void onUpdateTargetedEntityComplete(CallbackInfo info)

// always run at least once if we reach here
// if this isn't a fresh key press, turn on the run once flag
Boolean runOnceFlag = !freshKeyPress;
boolean runOnceFlag = !freshKeyPress;

// in case they manage to push the button multiple times per frame
// note: we already subtracted one from the press count earlier so the total
Expand All @@ -365,19 +353,14 @@ private void onUpdateTargetedEntityComplete(CallbackInfo info)
// prevent slow rounding error from eventually moving the player out of range
Vec3d summedLastPlayerPos = lastPlayerPlacedBlockPos.add(new Vec3d(targetPlacement.getSide().getVector().getX(), targetPlacement.getSide().getVector().getY(), targetPlacement.getSide().getVector().getZ()));

Vec3d newLastPlayerPlacedPos = null;

switch(targetPlacement.getSide().getAxis()) {
case X:
newLastPlayerPlacedPos = new Vec3d(summedLastPlayerPos.x, client.player.getPos().y, client.player.getPos().z);
break;
case Y:
newLastPlayerPlacedPos = new Vec3d(client.player.getPos().x, summedLastPlayerPos.y, client.player.getPos().z);
break;
case Z:
newLastPlayerPlacedPos = new Vec3d(client.player.getPos().x, client.player.getPos().y, summedLastPlayerPos.z);
break;
}
Vec3d newLastPlayerPlacedPos = switch (targetPlacement.getSide().getAxis()) {
case X ->
new Vec3d(summedLastPlayerPos.x, client.player.getPos().y, client.player.getPos().z);
case Y ->
new Vec3d(client.player.getPos().x, summedLastPlayerPos.y, client.player.getPos().z);
case Z ->
new Vec3d(client.player.getPos().x, client.player.getPos().y, summedLastPlayerPos.z);
};

lastPlayerPlacedBlockPos = newLastPlayerPlacedPos;
}
Expand All @@ -396,4 +379,4 @@ else if(isPlacementTargetFresh) {
lastSeenBlockPos = blockHitResult.getBlockPos();
}
}
}
}
Loading

0 comments on commit 189949e

Please sign in to comment.