From 5f93ddb4538e65bba7646fe7bdb0bfb23bb00764 Mon Sep 17 00:00:00 2001 From: Minh Date: Mon, 1 Jan 2024 17:28:25 +0700 Subject: [PATCH] Reach Distance buggy af --- .../attribute/ManasCoreAttributeUtils.java | 6 +-- .../attribute/ManasCoreAttributes.java | 4 +- .../manascore/core/MixinLivingEntity.java | 6 +-- .../manasmods/manascore/core/MixinPlayer.java | 8 ++-- .../MixinServerGamePacketListenerImpl.java | 25 ++++++----- .../core/MixinServerPlayerGameMode.java | 14 +++--- .../core/client/MixinGameRenderer.java | 45 ++++++++++--------- .../manascore/core/client/MixinGui.java | 3 +- .../manascore/core/client/MixinMinecraft.java | 4 +- 9 files changed, 58 insertions(+), 57 deletions(-) diff --git a/common/src/main/java/com/github/manasmods/manascore/attribute/ManasCoreAttributeUtils.java b/common/src/main/java/com/github/manasmods/manascore/attribute/ManasCoreAttributeUtils.java index b9356da0..758233df 100644 --- a/common/src/main/java/com/github/manasmods/manascore/attribute/ManasCoreAttributeUtils.java +++ b/common/src/main/java/com/github/manasmods/manascore/attribute/ManasCoreAttributeUtils.java @@ -8,13 +8,11 @@ public class ManasCoreAttributeUtils { public static double getEntityReachAddition(Player player) { - double range = player.getAttributeValue(ManasCoreAttributes.ENTITY_REACH.get()); - return range == 0 ? 0 : range + (player.isCreative() ? 3 : 0); + return player.getAttributeValue(ManasCoreAttributes.ENTITY_REACH.get()); } public static double getBlockReachAddition(Player player) { - double reach = player.getAttributeValue(ManasCoreAttributes.BLOCK_REACH.get()); - return reach == 0 ? 0 : reach + (player.isCreative() ? 0.5 : 0); + return player.getAttributeValue(ManasCoreAttributes.BLOCK_REACH.get()); } public static boolean cantHit(Player player, Entity entity, double padding) { diff --git a/common/src/main/java/com/github/manasmods/manascore/attribute/ManasCoreAttributes.java b/common/src/main/java/com/github/manasmods/manascore/attribute/ManasCoreAttributes.java index c962c85f..278c2fe3 100644 --- a/common/src/main/java/com/github/manasmods/manascore/attribute/ManasCoreAttributes.java +++ b/common/src/main/java/com/github/manasmods/manascore/attribute/ManasCoreAttributes.java @@ -6,7 +6,7 @@ import net.minecraft.world.entity.ai.attributes.RangedAttribute; public class ManasCoreAttributes { - public static final RegistrySupplier BLOCK_REACH = ManasCore.REGISTER.attribute("block_reach") + public static final RegistrySupplier BLOCK_REACH = ManasCore.REGISTER.attribute("block_reach_addition") .withDefaultValue(0) .withMinimumValue(0) .withMaximumValue(1024) @@ -27,7 +27,7 @@ public class ManasCoreAttributes { .applyToAll() .syncable() .end(); - public static final RegistrySupplier ENTITY_REACH = ManasCore.REGISTER.attribute("entity_reach") + public static final RegistrySupplier ENTITY_REACH = ManasCore.REGISTER.attribute("entity_reach_addition") .withDefaultValue(0) .withMinimumValue(0) .withMaximumValue(1024) diff --git a/common/src/main/java/com/github/manasmods/manascore/core/MixinLivingEntity.java b/common/src/main/java/com/github/manasmods/manascore/core/MixinLivingEntity.java index 642cda0f..52014464 100644 --- a/common/src/main/java/com/github/manasmods/manascore/core/MixinLivingEntity.java +++ b/common/src/main/java/com/github/manasmods/manascore/core/MixinLivingEntity.java @@ -43,7 +43,7 @@ protected void jumpInLiquid(TagKey fluidTag, CallbackInfo ci) { LivingEntity entity = (LivingEntity) (Object) this; AttributeInstance instance = entity.getAttribute(ManasCoreAttributes.SWIM_SPEED.get()); if (instance == null) return; - entity.setDeltaMovement(entity.getDeltaMovement().add(0.0, 0.04 * instance.getValue(), 0.0)); + entity.setDeltaMovement(entity.getDeltaMovement().add(0.0, 0.04 * instance.getValue() - 0.04, 0.0)); } @Inject(method = "goDownInWater", at = @At("HEAD")) @@ -51,11 +51,11 @@ protected void goDownInWater(CallbackInfo ci) { LivingEntity entity = (LivingEntity) (Object) this; AttributeInstance instance = entity.getAttribute(ManasCoreAttributes.SWIM_SPEED.get()); if (instance == null) return; - entity.setDeltaMovement(entity.getDeltaMovement().add(0.0, -0.04 * instance.getValue(), 0.0)); + entity.setDeltaMovement(entity.getDeltaMovement().add(0.0, 0.04 - 0.04 * instance.getValue(), 0.0)); } @ModifyArg(method = "travel", at = @At(value = "INVOKE", - target = "Lnet/minecraft/world/entity/LivingEntity;moveRelative(FLnet/minecraft/world/phys/Vec3;)V"), index = 0) + target = "Lnet/minecraft/world/entity/LivingEntity;moveRelative(FLnet/minecraft/world/phys/Vec3;)V", ordinal = 0)) public float travel(float speed) { LivingEntity entity = (LivingEntity) (Object) this; AttributeInstance instance = entity.getAttribute(ManasCoreAttributes.SWIM_SPEED.get()); diff --git a/common/src/main/java/com/github/manasmods/manascore/core/MixinPlayer.java b/common/src/main/java/com/github/manasmods/manascore/core/MixinPlayer.java index 3baf1248..4c0179c1 100644 --- a/common/src/main/java/com/github/manasmods/manascore/core/MixinPlayer.java +++ b/common/src/main/java/com/github/manasmods/manascore/core/MixinPlayer.java @@ -8,7 +8,6 @@ import net.minecraft.world.entity.ai.attributes.AttributeInstance; import net.minecraft.world.entity.player.Player; import net.minecraft.world.level.block.state.BlockState; -import org.objectweb.asm.Opcodes; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.*; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @@ -38,10 +37,11 @@ private float getCritMultiplier(float multiplier) { } @Redirect(method = "attack", at = @At(value = "INVOKE", - target = "Lnet/minecraft/world/entity/player/Player;distanceToSqr(Lnet/minecraft/world/entity/Entity;)D", opcode = Opcodes.GETSTATIC)) + target = "Lnet/minecraft/world/entity/player/Player;distanceToSqr(Lnet/minecraft/world/entity/Entity;)D")) private double getBlockInteractDistance(Player player, Entity entity) { - double reach = ManasCoreAttributeUtils.getBlockReachAddition(player); - return player.distanceToSqr(entity) - reach * reach; + double reach = ManasCoreAttributeUtils.getEntityReachAddition(player); + double reachSquared = reach * reach * (reach < 0 ? -1 : 1); + return player.distanceToSqr(entity) - reachSquared; } @Inject(method = "getDestroySpeed", at = @At("RETURN"), cancellable = true) diff --git a/common/src/main/java/com/github/manasmods/manascore/core/MixinServerGamePacketListenerImpl.java b/common/src/main/java/com/github/manasmods/manascore/core/MixinServerGamePacketListenerImpl.java index 5b84fa55..199f141e 100644 --- a/common/src/main/java/com/github/manasmods/manascore/core/MixinServerGamePacketListenerImpl.java +++ b/common/src/main/java/com/github/manasmods/manascore/core/MixinServerGamePacketListenerImpl.java @@ -3,7 +3,8 @@ import com.github.manasmods.manascore.attribute.ManasCoreAttributeUtils; import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.network.ServerGamePacketListenerImpl; -import org.objectweb.asm.Opcodes; +import net.minecraft.world.phys.AABB; +import net.minecraft.world.phys.Vec3; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; @@ -12,17 +13,19 @@ @Mixin(ServerGamePacketListenerImpl.class) public class MixinServerGamePacketListenerImpl { @Shadow public ServerPlayer player; - @Redirect(method = "handleUseItemOn", at = @At(value = "FIELD", - target = "Lnet/minecraft/server/network/ServerGamePacketListenerImpl;MAX_INTERACTION_DISTANCE:D", opcode = Opcodes.GETSTATIC)) - private double getBlockInteractDistance() { - double reach = ManasCoreAttributeUtils.getBlockReachAddition(this.player) + Math.sqrt(ServerGamePacketListenerImpl.MAX_INTERACTION_DISTANCE); - return reach * reach; + @Redirect(method = "handleUseItemOn", at = @At(value = "INVOKE", + target = "Lnet/minecraft/world/phys/Vec3;distanceToSqr(Lnet/minecraft/world/phys/Vec3;)D")) + private double getBlockInteractDistancee(Vec3 instance, Vec3 vec) { + double reach = ManasCoreAttributeUtils.getBlockReachAddition(player); + double reachSquared = reach * reach * (reach < 0 ? -1 : 1); + return instance.distanceToSqr(vec) - reachSquared; } - @Redirect(method = "handleInteract", at = @At(value = "FIELD", - target = "Lnet/minecraft/server/network/ServerGamePacketListenerImpl;MAX_INTERACTION_DISTANCE:D", opcode = Opcodes.GETSTATIC)) - private double getEntityInteractDistance() { - double reach = ManasCoreAttributeUtils.getEntityReachAddition(this.player) + Math.sqrt(ServerGamePacketListenerImpl.MAX_INTERACTION_DISTANCE); - return reach * reach; + @Redirect(method = "handleInteract", at = @At(value = "INVOKE", + target = "Lnet/minecraft/world/phys/AABB;distanceToSqr(Lnet/minecraft/world/phys/Vec3;)D")) + private double getEntityInteractDistance(AABB instance, Vec3 vec) { + double reach = ManasCoreAttributeUtils.getEntityReachAddition(player); + double reachSquared = reach * reach * (reach < 0 ? -1 : 1); + return instance.distanceToSqr(vec) - reachSquared; } } diff --git a/common/src/main/java/com/github/manasmods/manascore/core/MixinServerPlayerGameMode.java b/common/src/main/java/com/github/manasmods/manascore/core/MixinServerPlayerGameMode.java index 265adc5e..8c36f6e3 100644 --- a/common/src/main/java/com/github/manasmods/manascore/core/MixinServerPlayerGameMode.java +++ b/common/src/main/java/com/github/manasmods/manascore/core/MixinServerPlayerGameMode.java @@ -3,8 +3,7 @@ import com.github.manasmods.manascore.attribute.ManasCoreAttributeUtils; import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.level.ServerPlayerGameMode; -import net.minecraft.server.network.ServerGamePacketListenerImpl; -import org.objectweb.asm.Opcodes; +import net.minecraft.world.phys.Vec3; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -14,10 +13,11 @@ @Mixin(ServerPlayerGameMode.class) public class MixinServerPlayerGameMode { @Shadow @Final protected ServerPlayer player; - @Redirect(method = "handleBlockBreakAction", at = @At(value = "FIELD", - target = "Lnet/minecraft/server/network/ServerGamePacketListenerImpl;MAX_INTERACTION_DISTANCE:D", opcode = Opcodes.GETSTATIC)) - private double getReachDistance() { - double reach = ManasCoreAttributeUtils.getBlockReachAddition(player) + Math.sqrt(ServerGamePacketListenerImpl.MAX_INTERACTION_DISTANCE); - return reach * reach; + @Redirect(method = "handleBlockBreakAction", at = @At(value = "INVOKE", + target = "Lnet/minecraft/world/phys/Vec3;distanceToSqr(Lnet/minecraft/world/phys/Vec3;)D")) + private double getBlockInteractDistancee(Vec3 instance, Vec3 vec) { + double reach = ManasCoreAttributeUtils.getBlockReachAddition(player); + double reachSquared = reach * reach * (reach < 0 ? -1 : 1); + return instance.distanceToSqr(vec) - reachSquared; } } diff --git a/common/src/main/java/com/github/manasmods/manascore/core/client/MixinGameRenderer.java b/common/src/main/java/com/github/manasmods/manascore/core/client/MixinGameRenderer.java index a8147ada..2372779a 100644 --- a/common/src/main/java/com/github/manasmods/manascore/core/client/MixinGameRenderer.java +++ b/common/src/main/java/com/github/manasmods/manascore/core/client/MixinGameRenderer.java @@ -2,51 +2,52 @@ import com.github.manasmods.manascore.attribute.ManasCoreAttributeUtils; import com.llamalad7.mixinextras.sugar.Local; -import com.llamalad7.mixinextras.sugar.ref.LocalRef; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.GameRenderer; import net.minecraft.world.entity.player.Player; import net.minecraft.world.phys.Vec3; -import org.objectweb.asm.Opcodes; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.injection.*; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.ModifyArg; +import org.spongepowered.asm.mixin.injection.Redirect; @Mixin(GameRenderer.class) public class MixinGameRenderer { @Shadow @Final Minecraft minecraft; - @Inject(method = "pick", at = @At(value = "INVOKE_ASSIGN", - target = "Lnet/minecraft/world/phys/Vec3;add(DDD)Lnet/minecraft/world/phys/Vec3;", shift = At.Shift.AFTER)) - private void getReachDistance(float partialTicks, CallbackInfo ci, @Local(ordinal = 0) double d, @Local(ordinal = 0) Vec3 vec3, - @Local(ordinal = 1) Vec3 vec32, @Local(ordinal = 2) LocalRef vec33) { - if (minecraft.player == null) return; + @ModifyArg(method = "pick", at = @At(value = "INVOKE", + target = "Lnet/minecraft/world/phys/AABB;expandTowards(Lnet/minecraft/world/phys/Vec3;)Lnet/minecraft/world/phys/AABB;"), index = 0) + private Vec3 getEntityReachDistance(Vec3 vector, @Local(ordinal = 0) double d) { + Player player = this.minecraft.player; + if (player == null) return vector; + double reach = ManasCoreAttributeUtils.getEntityReachAddition(minecraft.player); - if (minecraft.gameMode != null && minecraft.gameMode.hasFarPickRange()) { + if (minecraft.gameMode != null && minecraft.gameMode.hasFarPickRange() && minecraft.hitResult == null) { double newD = d + reach; - vec33.set(vec3.add(vec32.x * newD, vec32.y * newD, vec32.z * newD)); + return vector.scale(newD); } + return vector; } @ModifyArg(method = "pick", at = @At(value = "INVOKE", - target = "Lnet/minecraft/world/phys/AABB;expandTowards(Lnet/minecraft/world/phys/Vec3;)Lnet/minecraft/world/phys/AABB;", - opcode = Opcodes.GETSTATIC), index = 0) - private Vec3 getEntityReachDistance(Vec3 vector, @Local(ordinal = 0) double d) { + target = "Lnet/minecraft/world/entity/projectile/ProjectileUtil;getEntityHitResult(Lnet/minecraft/world/entity/Entity;Lnet/minecraft/world/phys/Vec3;Lnet/minecraft/world/phys/Vec3;Lnet/minecraft/world/phys/AABB;Ljava/util/function/Predicate;D)Lnet/minecraft/world/phys/EntityHitResult;"), + index = 2) + private Vec3 getReacgDistance(Vec3 startVec, @Local(ordinal = 0) double d, + @Local(ordinal = 0) Vec3 vec3, @Local(ordinal = 1) Vec3 vec32) { Player player = this.minecraft.player; - if (player == null) return vector; + if (player == null) return startVec; double reach = ManasCoreAttributeUtils.getEntityReachAddition(minecraft.player); if (minecraft.gameMode != null && minecraft.gameMode.hasFarPickRange() && minecraft.hitResult == null) { double newD = d + reach; - return vector.scale(newD); + return vec3.add(vec32.x * newD, vec32.y * newD, vec32.z * newD); } - return vector; + return startVec; } @ModifyArg(method = "pick", at = @At(value = "INVOKE", - target = "Lnet/minecraft/world/entity/projectile/ProjectileUtil;getEntityHitResult(Lnet/minecraft/world/entity/Entity;Lnet/minecraft/world/phys/Vec3;Lnet/minecraft/world/phys/Vec3;Lnet/minecraft/world/phys/AABB;Ljava/util/function/Predicate;D)Lnet/minecraft/world/phys/EntityHitResult;", - opcode = Opcodes.GETSTATIC), index = 5) + target = "Lnet/minecraft/world/entity/projectile/ProjectileUtil;getEntityHitResult(Lnet/minecraft/world/entity/Entity;Lnet/minecraft/world/phys/Vec3;Lnet/minecraft/world/phys/Vec3;Lnet/minecraft/world/phys/AABB;Ljava/util/function/Predicate;D)Lnet/minecraft/world/phys/EntityHitResult;"), index = 5) private double getEntityDistance(double distance, @Local(ordinal = 0) double d) { Player player = this.minecraft.player; if (player == null) return distance; @@ -60,12 +61,12 @@ private double getEntityDistance(double distance, @Local(ordinal = 0) double d) } @Redirect(method = "pick", at = @At(value = "INVOKE", - target = "Lnet/minecraft/world/phys/Vec3;distanceToSqr(Lnet/minecraft/world/phys/Vec3;)D", ordinal = 1, opcode = Opcodes.GETSTATIC)) + target = "Lnet/minecraft/world/phys/Vec3;distanceToSqr(Lnet/minecraft/world/phys/Vec3;)D", ordinal = 1)) private double getEntityReachDistance(Vec3 instance, Vec3 vec) { Player player = this.minecraft.player; if (player == null) return instance.distanceToSqr(vec); - double reach = ManasCoreAttributeUtils.getEntityReachAddition(player); - return instance.distanceToSqr(vec) - reach * reach; + double reachSquared = reach * reach * (reach < 0 ? -1 : 1); + return instance.distanceToSqr(vec) - reachSquared; } } diff --git a/common/src/main/java/com/github/manasmods/manascore/core/client/MixinGui.java b/common/src/main/java/com/github/manasmods/manascore/core/client/MixinGui.java index 3d415740..01cc22f1 100644 --- a/common/src/main/java/com/github/manasmods/manascore/core/client/MixinGui.java +++ b/common/src/main/java/com/github/manasmods/manascore/core/client/MixinGui.java @@ -5,7 +5,6 @@ import net.minecraft.client.gui.Gui; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.player.Player; -import org.objectweb.asm.Opcodes; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -17,7 +16,7 @@ public class MixinGui { @Shadow @Final private Minecraft minecraft; @Redirect(method = "renderCrosshair", at = @At(value = "INVOKE", - target = "Lnet/minecraft/world/entity/Entity;isAlive()Z", opcode = Opcodes.GETSTATIC)) + target = "Lnet/minecraft/world/entity/Entity;isAlive()Z")) private boolean getEntityReachtDistance(Entity instance) { Player player = this.minecraft.player; if (player == null) return instance.isAlive(); diff --git a/common/src/main/java/com/github/manasmods/manascore/core/client/MixinMinecraft.java b/common/src/main/java/com/github/manasmods/manascore/core/client/MixinMinecraft.java index 8fafb231..16629bf5 100644 --- a/common/src/main/java/com/github/manasmods/manascore/core/client/MixinMinecraft.java +++ b/common/src/main/java/com/github/manasmods/manascore/core/client/MixinMinecraft.java @@ -40,8 +40,8 @@ private void startAttack(CallbackInfoReturnable cir) { return; } - if(!(this.hitResult instanceof EntityHitResult entityHit)) return; - if(ManasCoreAttributeUtils.cantHit(player, entityHit.getEntity(), 3)) { + if (!(this.hitResult instanceof EntityHitResult entityHit)) return; + if (ManasCoreAttributeUtils.cantHit(player, entityHit.getEntity(), 3)) { if (this.gameMode != null && this.gameMode.hasMissTime()) this.missTime = 10; this.player.resetAttackStrengthTicker(); this.player.swing(InteractionHand.MAIN_HAND);