Skip to content

Commit

Permalink
Entity reach still broken
Browse files Browse the repository at this point in the history
  • Loading branch information
minheragon12345 committed Dec 31, 2023
1 parent 85889b8 commit 99a57a7
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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<Vec3> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public class ManasCoreAttributes {
public static final RegistrySupplier<RangedAttribute> BLOCK_REACH = ManasCore.REGISTER.attribute("block_reach")
.withDefaultValue(4.5)
.withDefaultValue(0)
.withMinimumValue(0)
.withMaximumValue(1024)
.applyTo(() -> EntityType.PLAYER)
Expand All @@ -28,7 +28,7 @@ public class ManasCoreAttributes {
.syncable()
.end();
public static final RegistrySupplier<RangedAttribute> ENTITY_REACH = ManasCore.REGISTER.attribute("entity_reach")
.withDefaultValue(3)
.withDefaultValue(0)
.withMinimumValue(0)
.withMaximumValue(1024)
.applyTo(() -> EntityType.PLAYER)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<BlockHitResult> cir,
@Local(ordinal = 6) float l, @Local(ordinal = 5) float k, @Local(ordinal = 7) float n, @Local(ordinal = 1) LocalRef<Vec3> vec32) {
final double reach = ManasCoreAttributeUtils.getBlockReachAddition(player);
vec32.set(vec32.get().add(l * reach, k * reach, n * reach));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Fluid> 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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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))
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Vec3> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private void startAttack(CallbackInfoReturnable<Boolean> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Float> cir) {
Player player = this.minecraft.player;
if (player == null) return;
cir.setReturnValue((float) ManasCoreAttributeUtils.getReachDistance(player));
cir.setReturnValue(cir.getReturnValue () + (float) ManasCoreAttributeUtils.getBlockReachAddition(player));
}
}

0 comments on commit 99a57a7

Please sign in to comment.