From 99a57a791664886cb574fa7e62c8b620a106b418 Mon Sep 17 00:00:00 2001 From: Minh Date: Mon, 1 Jan 2024 00:38:25 +0700 Subject: [PATCH] Entity reach still broken --- .../attribute/ManasCoreAttributeUtils.java | 6 +- .../attribute/ManasCoreAttributes.java | 4 +- .../manasmods/manascore/core/MixinItem.java | 20 ++++-- .../manascore/core/MixinLivingEntity.java | 8 +-- .../manasmods/manascore/core/MixinPlayer.java | 36 +++++------ .../MixinServerGamePacketListenerImpl.java | 4 +- .../core/MixinServerPlayerGameMode.java | 3 +- .../core/client/MixinGameRenderer.java | 63 +++++++++++++++---- .../manascore/core/client/MixinGui.java | 21 ++++--- .../manascore/core/client/MixinMinecraft.java | 2 +- .../core/client/MixinMultiPlayerGameMode.java | 4 +- 11 files changed, 109 insertions(+), 62 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 b8cb52a6..b9356da0 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 @@ -7,12 +7,12 @@ import java.util.Optional; public class ManasCoreAttributeUtils { - public static double getAttackRange(Player player) { + public static double getEntityReachAddition(Player player) { double range = player.getAttributeValue(ManasCoreAttributes.ENTITY_REACH.get()); return range == 0 ? 0 : range + (player.isCreative() ? 3 : 0); } - public static double getReachDistance(Player player) { + public static double getBlockReachAddition(Player player) { double reach = player.getAttributeValue(ManasCoreAttributes.BLOCK_REACH.get()); return reach == 0 ? 0 : reach + (player.isCreative() ? 0.5 : 0); } @@ -22,7 +22,7 @@ public static boolean cantHit(Player player, Entity entity, double padding) { Vec3 targetCenter = entity.getPosition(1.0F).add(0, entity.getBbHeight() / 2, 0); Optional hit = entity.getBoundingBox().clip(eye, targetCenter); - double dist = getAttackRange(player) + padding; + double dist = getEntityReachAddition(player) + padding; return !(hit.map(eye::distanceToSqr).orElseGet(() -> player.distanceToSqr(entity)) < dist * dist); } } 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 91b9a710..c962c85f 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 @@ -7,7 +7,7 @@ public class ManasCoreAttributes { public static final RegistrySupplier BLOCK_REACH = ManasCore.REGISTER.attribute("block_reach") - .withDefaultValue(4.5) + .withDefaultValue(0) .withMinimumValue(0) .withMaximumValue(1024) .applyTo(() -> EntityType.PLAYER) @@ -28,7 +28,7 @@ public class ManasCoreAttributes { .syncable() .end(); public static final RegistrySupplier ENTITY_REACH = ManasCore.REGISTER.attribute("entity_reach") - .withDefaultValue(3) + .withDefaultValue(0) .withMinimumValue(0) .withMaximumValue(1024) .applyTo(() -> EntityType.PLAYER) diff --git a/common/src/main/java/com/github/manasmods/manascore/core/MixinItem.java b/common/src/main/java/com/github/manasmods/manascore/core/MixinItem.java index 4d7d5421..4e938ca4 100644 --- a/common/src/main/java/com/github/manasmods/manascore/core/MixinItem.java +++ b/common/src/main/java/com/github/manasmods/manascore/core/MixinItem.java @@ -1,18 +1,26 @@ package com.github.manasmods.manascore.core; import com.github.manasmods.manascore.attribute.ManasCoreAttributeUtils; +import com.llamalad7.mixinextras.sugar.Local; +import com.llamalad7.mixinextras.sugar.ref.LocalRef; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.Item; +import net.minecraft.world.level.ClipContext; import net.minecraft.world.level.Level; +import net.minecraft.world.phys.BlockHitResult; +import net.minecraft.world.phys.Vec3; import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.Constant; -import org.spongepowered.asm.mixin.injection.ModifyConstant; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @Mixin(Item.class) public class MixinItem { - @ModifyConstant(method = "getPlayerPOVHitResult(Lnet/minecraft/world/level/Level;Lnet/minecraft/world/entity/player/Player;Lnet/minecraft/world/level/ClipContext$Fluid;)Lnet/minecraft/world/phys/BlockHitResult;", - require = 4, allow = 4, constant = @Constant(doubleValue = 5.0)) - private static double getReachDistance(double reachDistance, Level level, Player player) { - return ManasCoreAttributeUtils.getReachDistance(player); + @Inject(method = "getPlayerPOVHitResult", at = @At(value = "INVOKE_ASSIGN", + target = "Lnet/minecraft/world/phys/Vec3;add(DDD)Lnet/minecraft/world/phys/Vec3;", shift = At.Shift.AFTER)) + private static void getReachDistance(Level level, Player player, ClipContext.Fluid fluidMode, CallbackInfoReturnable cir, + @Local(ordinal = 6) float l, @Local(ordinal = 5) float k, @Local(ordinal = 7) float n, @Local(ordinal = 1) LocalRef vec32) { + final double reach = ManasCoreAttributeUtils.getBlockReachAddition(player); + vec32.set(vec32.get().add(l * reach, k * reach, n * reach)); } } 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 5e6133ac..642cda0f 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 @@ -38,24 +38,20 @@ public float causeFallDamage(float fallDistance, float multiplier) { return fallDistance - additionalJumpBlock; } - @Inject(method = "jumpInLiquid", at = @At("HEAD"), cancellable = true) + @Inject(method = "jumpInLiquid", at = @At("HEAD")) 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)); - ci.cancel(); } - @Inject(method = "goDownInWater", at = @At("HEAD"), cancellable = true) + @Inject(method = "goDownInWater", at = @At("HEAD")) 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)); - ci.cancel(); } @ModifyArg(method = "travel", at = @At(value = "INVOKE", 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 b654c0f5..3baf1248 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 @@ -2,9 +2,9 @@ import com.github.manasmods.manascore.attribute.ManasCoreAttributeUtils; import com.github.manasmods.manascore.attribute.ManasCoreAttributes; -import net.minecraft.world.effect.MobEffects; +import com.llamalad7.mixinextras.sugar.Local; +import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.LivingEntity; -import net.minecraft.world.entity.ai.attributes.Attribute; import net.minecraft.world.entity.ai.attributes.AttributeInstance; import net.minecraft.world.entity.player.Player; import net.minecraft.world.level.block.state.BlockState; @@ -15,19 +15,18 @@ @Mixin(Player.class) public abstract class MixinPlayer { - @Redirect(method = "attack", at = @At(value = "INVOKE", - target = "Lnet/minecraft/world/entity/player/Player;getAttributeValue(Lnet/minecraft/world/entity/ai/attributes/Attribute;)D", opcode = Opcodes.GETSTATIC)) - private double getAttackDamageWithCritChance(Player player, Attribute attribute) { - AttributeInstance instance = player.getAttribute(ManasCoreAttributes.CRIT_CHANCE.get()); - if (instance == null || player.getRandom().nextInt(100) > instance.getValue()) - return player.getAttributeValue(attribute); - - boolean vanillaCrit = player.getAttackStrengthScale(0.5F) > 0.9F && player.fallDistance > 0.0F - && !player.onGround() && !player.onClimbable() && !player.isInWater() && !player.isSprinting() - && !player.hasEffect(MobEffects.BLINDNESS) && !player.isPassenger(); - if (vanillaCrit) return player.getAttributeValue(attribute); + @ModifyArg(method = "attack", at = @At(value = "INVOKE", + target = "Lnet/minecraft/world/entity/Entity;hurt(Lnet/minecraft/world/damagesource/DamageSource;F)Z"), index = 1) + private float getAttackDamageWithCritChance(float amount, @Local(ordinal = 1) float g, @Local(ordinal = 2) boolean bl3) { + Player player = (Player) (Object) this; + if (!bl3) { + AttributeInstance instance = player.getAttribute(ManasCoreAttributes.CRIT_CHANCE.get()); + if (instance == null || player.getRandom().nextInt(100) > instance.getValue()) return amount; - return player.getAttributeValue(attribute) * getCritMultiplier(1.5F); + float beforeEnchant = amount - g; + return beforeEnchant * getCritMultiplier(1.5F) + g; + } + return amount; } @ModifyConstant(method = "attack(Lnet/minecraft/world/entity/Entity;)V", constant = @Constant(floatValue = 1.5F)) @@ -38,10 +37,11 @@ private float getCritMultiplier(float multiplier) { return (float) instance.getValue(); } - @ModifyConstant(method = "attack(Lnet/minecraft/world/entity/Entity;)V", constant = @Constant(doubleValue = 9.0)) - private double getAttackRangeSquared(double attackRange) { - double range = ManasCoreAttributeUtils.getAttackRange((Player) (Object) this); - return range * range; + @Redirect(method = "attack", at = @At(value = "INVOKE", + target = "Lnet/minecraft/world/entity/player/Player;distanceToSqr(Lnet/minecraft/world/entity/Entity;)D", opcode = Opcodes.GETSTATIC)) + private double getBlockInteractDistance(Player player, Entity entity) { + double reach = ManasCoreAttributeUtils.getBlockReachAddition(player); + return player.distanceToSqr(entity) - reach * reach; } @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 fc95dec6..5b84fa55 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 @@ -15,14 +15,14 @@ public class MixinServerGamePacketListenerImpl { @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.getReachDistance(this.player) + 3; + double reach = ManasCoreAttributeUtils.getBlockReachAddition(this.player) + Math.sqrt(ServerGamePacketListenerImpl.MAX_INTERACTION_DISTANCE); return reach * reach; } @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.getAttackRange(this.player) + 3; + double reach = ManasCoreAttributeUtils.getEntityReachAddition(this.player) + Math.sqrt(ServerGamePacketListenerImpl.MAX_INTERACTION_DISTANCE); return reach * reach; } } 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 bcad2843..265adc5e 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,6 +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 org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; @@ -16,7 +17,7 @@ public class MixinServerPlayerGameMode { @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.getReachDistance(player) + 1; + double reach = ManasCoreAttributeUtils.getBlockReachAddition(player) + Math.sqrt(ServerGamePacketListenerImpl.MAX_INTERACTION_DISTANCE); return reach * reach; } } 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 fea6bbf0..a8147ada 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 @@ -1,30 +1,71 @@ package com.github.manasmods.manascore.core.client; 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.Constant; -import org.spongepowered.asm.mixin.injection.ModifyConstant; +import org.spongepowered.asm.mixin.injection.*; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(GameRenderer.class) public class MixinGameRenderer { @Shadow @Final Minecraft minecraft; - @ModifyConstant(method = "pick", require = 1, allow = 1, constant = @Constant(doubleValue = 6.0)) - private double getReachDistance(double reachDistance) { + @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; + double reach = ManasCoreAttributeUtils.getEntityReachAddition(minecraft.player); + if (minecraft.gameMode != null && minecraft.gameMode.hasFarPickRange()) { + double newD = d + reach; + vec33.set(vec3.add(vec32.x * newD, vec32.y * newD, vec32.z * newD)); + } + } + + @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) { + Player player = this.minecraft.player; + if (player == null) return vector; + + 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 vector; + } + + @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) + private double getEntityDistance(double distance, @Local(ordinal = 0) double d) { Player player = this.minecraft.player; - if (player == null) return reachDistance; - return ManasCoreAttributeUtils.getReachDistance(player); + if (player == null) return distance; + + double reach = ManasCoreAttributeUtils.getEntityReachAddition(minecraft.player); + if (minecraft.gameMode != null && minecraft.gameMode.hasFarPickRange() && minecraft.hitResult == null) { + double newD = d + reach; + return newD * newD; + } + return distance; } - @ModifyConstant(method = "pick", constant = @Constant(doubleValue = 9.0)) - private double getAttackRange(double attackRange) { + @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)) + private double getEntityReachDistance(Vec3 instance, Vec3 vec) { Player player = this.minecraft.player; - if (player == null) return attackRange; - double range = ManasCoreAttributeUtils.getAttackRange(player); - return range * range; + if (player == null) return instance.distanceToSqr(vec); + + double reach = ManasCoreAttributeUtils.getEntityReachAddition(player); + return instance.distanceToSqr(vec) - reach * reach; } } 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 49f2f5a3..3d415740 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 @@ -3,24 +3,25 @@ import com.github.manasmods.manascore.attribute.ManasCoreAttributeUtils; import net.minecraft.client.Minecraft; 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; -import org.spongepowered.asm.mixin.injection.Constant; -import org.spongepowered.asm.mixin.injection.ModifyConstant; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; @Mixin(Gui.class) public class MixinGui { @Shadow @Final private Minecraft minecraft; - @ModifyConstant(method = "renderCrosshair", require = 2, allow = 1, constant = @Constant(floatValue = 1.0F)) - private float getActualReachDistance(float constant) { - Player player = minecraft.player; - if (player == null) return constant; - if (minecraft.crosshairPickEntity == null) return constant; - - if (ManasCoreAttributeUtils.cantHit(player, minecraft.crosshairPickEntity, 0)) return 2.0F; - return constant; + @Redirect(method = "renderCrosshair", at = @At(value = "INVOKE", + target = "Lnet/minecraft/world/entity/Entity;isAlive()Z", opcode = Opcodes.GETSTATIC)) + private boolean getEntityReachtDistance(Entity instance) { + Player player = this.minecraft.player; + if (player == null) return instance.isAlive(); + if (minecraft.crosshairPickEntity == null) return instance.isAlive(); + return instance.isAlive() && ManasCoreAttributeUtils.cantHit(player, minecraft.crosshairPickEntity, 3); } } 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 812c5c61..8fafb231 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 @@ -41,7 +41,7 @@ private void startAttack(CallbackInfoReturnable cir) { } if(!(this.hitResult instanceof EntityHitResult entityHit)) return; - if(ManasCoreAttributeUtils.cantHit(player, entityHit.getEntity(), 0)) { + 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); diff --git a/common/src/main/java/com/github/manasmods/manascore/core/client/MixinMultiPlayerGameMode.java b/common/src/main/java/com/github/manasmods/manascore/core/client/MixinMultiPlayerGameMode.java index aba0a0b5..67032ace 100644 --- a/common/src/main/java/com/github/manasmods/manascore/core/client/MixinMultiPlayerGameMode.java +++ b/common/src/main/java/com/github/manasmods/manascore/core/client/MixinMultiPlayerGameMode.java @@ -14,10 +14,10 @@ @Mixin(MultiPlayerGameMode.class) public class MixinMultiPlayerGameMode { @Shadow @Final private Minecraft minecraft; - @Inject(method = "getPickRange", at = @At("HEAD"), cancellable = true) + @Inject(method = "getPickRange", at = @At("RETURN"), cancellable = true) protected void getPickRange(CallbackInfoReturnable cir) { Player player = this.minecraft.player; if (player == null) return; - cir.setReturnValue((float) ManasCoreAttributeUtils.getReachDistance(player)); + cir.setReturnValue(cir.getReturnValue () + (float) ManasCoreAttributeUtils.getBlockReachAddition(player)); } }