Skip to content

Commit

Permalink
More movement sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianMichael committed Jan 1, 2025
1 parent 6641f45 commit 153ae7a
Show file tree
Hide file tree
Showing 10 changed files with 325 additions and 126 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* This file is part of ViaFabricPlus - https://github.com/ViaVersion/ViaFabricPlus
* Copyright (C) 2021-2025 the original authors
* - FlorianMichael/EnZaXD <[email protected]>
* - RK_01/RaphiMC
* Copyright (C) 2023-2025 ViaVersion and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.viaversion.viafabricplus.injection.mixin.features.block.mining_calculation;

import com.llamalad7.mixinextras.sugar.Local;
import com.llamalad7.mixinextras.sugar.ref.LocalFloatRef;
import com.viaversion.viafabricplus.protocoltranslator.ProtocolTranslator;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import net.minecraft.block.BlockState;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.effect.StatusEffect;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.world.World;
import net.raphimc.vialegacy.api.LegacyProtocolVersion;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(PlayerEntity.class)
public abstract class MixinPlayerEntity extends LivingEntity {

@Shadow @Final private PlayerInventory inventory;

@Shadow public abstract boolean canHarvest(BlockState state);

protected MixinPlayerEntity(EntityType<? extends LivingEntity> entityType, World world) {
super(entityType, world);
}

@Redirect(method = "getBlockBreakingSpeed", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/PlayerEntity;hasStatusEffect(Lnet/minecraft/registry/entry/RegistryEntry;)Z"))
private boolean changeSpeedCalculation(PlayerEntity instance, RegistryEntry<StatusEffect> statusEffect, @Local LocalFloatRef f) {
final boolean hasMiningFatigue = instance.hasStatusEffect(statusEffect);
if (hasMiningFatigue && ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(ProtocolVersion.v1_7_6)) {
f.set(f.get() * (1.0F - (this.getStatusEffect(StatusEffects.MINING_FATIGUE).getAmplifier() + 1) * 0.2F));
if (f.get() < 0) {
f.set(0);
}
return false; // disable original code
}
return hasMiningFatigue;
}

@Inject(method = "getBlockBreakingSpeed", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/effect/StatusEffectUtil;hasHaste(Lnet/minecraft/entity/LivingEntity;)Z", shift = At.Shift.BEFORE))
private void changeSpeedCalculation(BlockState block, CallbackInfoReturnable<Float> cir, @Local LocalFloatRef f) {
final float efficiency = (float) this.getAttributeValue(EntityAttributes.MINING_EFFICIENCY);
if (efficiency <= 0) {
return;
}

final float speed = this.inventory.getBlockBreakingSpeed(block);
if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(LegacyProtocolVersion.r1_4_4tor1_4_5) && this.canHarvest(block)) {
f.set(speed + efficiency);
} else if (speed > 1F || ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(LegacyProtocolVersion.r1_4_6tor1_4_7)) {
if (!this.getMainHandStack().isEmpty()) {
if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(ProtocolVersion.v1_7_6)) {
if (speed <= 1.0 && !this.canHarvest(block)) {
f.set(speed + efficiency * 0.08F);
} else {
f.set(speed + efficiency);
}
}
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* This file is part of ViaFabricPlus - https://github.com/ViaVersion/ViaFabricPlus
* Copyright (C) 2021-2025 the original authors
* - FlorianMichael/EnZaXD <[email protected]>
* - RK_01/RaphiMC
* Copyright (C) 2023-2025 ViaVersion and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.viaversion.viafabricplus.injection.mixin.features.interaction.attack_cooldown;

import com.viaversion.viafabricplus.protocoltranslator.ProtocolTranslator;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import net.minecraft.entity.player.PlayerEntity;
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.CallbackInfoReturnable;

@Mixin(PlayerEntity.class)
public abstract class MixinPlayerEntity {

@Inject(method = "getAttackCooldownProgress", at = @At("HEAD"), cancellable = true)
private void removeAttackCooldown(CallbackInfoReturnable<Float> ci) {
if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(ProtocolVersion.v1_8)) {
ci.setReturnValue(1F);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* This file is part of ViaFabricPlus - https://github.com/ViaVersion/ViaFabricPlus
* Copyright (C) 2021-2025 the original authors
* - FlorianMichael/EnZaXD <[email protected]>
* - RK_01/RaphiMC
* Copyright (C) 2023-2025 ViaVersion and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.viaversion.viafabricplus.injection.mixin.features.movement.constants;

import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import com.viaversion.viafabricplus.protocoltranslator.ProtocolTranslator;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import net.minecraft.entity.LivingEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Constant;
import org.spongepowered.asm.mixin.injection.ModifyConstant;

@Mixin(LivingEntity.class)
public abstract class MixinLivingEntity {

@ModifyExpressionValue(method = "tickStatusEffects", at = @At(value = "CONSTANT", args = "intValue=4"))
private int changeParticleDensity(int original) {
if (ProtocolTranslator.getTargetVersion().olderThan(ProtocolVersion.v1_20_5)) {
return 2;
} else {
return original;
}
}

@ModifyConstant(method = "getBlockingItem", constant = @Constant(intValue = 5))
private int removeBlockActionUseDelay(int constant) {
if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(ProtocolVersion.v1_8)) {
return 0;
} else {
return constant;
}
}

@ModifyConstant(method = "tickMovement", constant = @Constant(doubleValue = 0.003D))
private double modifyVelocityZero(double constant) {
if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(ProtocolVersion.v1_8)) {
return 0.005D;
} else {
return constant;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,27 @@

import com.viaversion.viafabricplus.protocoltranslator.ProtocolTranslator;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import net.minecraft.entity.EntityAttachmentType;
import net.minecraft.entity.EntityAttachments;
import net.minecraft.entity.EntityDimensions;
import net.minecraft.entity.EntityPose;
import net.minecraft.entity.player.PlayerEntity;
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.Unique;
import org.spongepowered.asm.mixin.injection.*;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(PlayerEntity.class)
public abstract class MixinPlayerEntity {

@Unique
private static final EntityDimensions viaFabricPlus$sneaking_dimensions_v1_13_2 = EntityDimensions.changing(0.6F, 1.65F).withEyeHeight(1.54F).
withAttachments(EntityAttachments.builder().add(EntityAttachmentType.VEHICLE, PlayerEntity.VEHICLE_ATTACHMENT_POS));

@Unique
private static final EntityDimensions viaFabricPlus$sneaking_dimensions_v1_8 = EntityDimensions.changing(0.6F, 1.8F).withEyeHeight(1.54F).
withAttachments(EntityAttachments.builder().add(EntityAttachmentType.VEHICLE, PlayerEntity.VEHICLE_ATTACHMENT_POS));

@ModifyConstant(method = "isSpaceAroundPlayerEmpty", constant = @Constant(doubleValue = 9.999999747378752E-6 /* 1.0E-5F */))
private double removeOffsetWhenCheckingSneakingCollision(double constant) {
if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(ProtocolVersion.v1_20_3)) {
Expand All @@ -40,4 +53,24 @@ private double removeOffsetWhenCheckingSneakingCollision(double constant) {
}
}

@Inject(method = "getBaseDimensions", at = @At("HEAD"), cancellable = true)
private void modifyDimensions(EntityPose pose, CallbackInfoReturnable<EntityDimensions> cir) {
if (pose == EntityPose.CROUCHING) {
if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(ProtocolVersion.v1_8)) {
cir.setReturnValue(viaFabricPlus$sneaking_dimensions_v1_8);
} else if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(ProtocolVersion.v1_13_2)) {
cir.setReturnValue(viaFabricPlus$sneaking_dimensions_v1_13_2);
}
}
}

@Redirect(method = "adjustMovementForSneaking", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/PlayerEntity;getStepHeight()F"))
private float modifyStepHeight(PlayerEntity instance) {
if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(ProtocolVersion.v1_10)) {
return 1.0F;
} else {
return instance.getStepHeight();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.viaversion.viafabricplus.injection.mixin.features.movement.entity;
package com.viaversion.viafabricplus.injection.mixin.features.movement.constants;

import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import com.viaversion.viafabricplus.protocoltranslator.ProtocolTranslator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,6 @@ public MixinLivingEntity(EntityType<?> type, World world) {
super(type, world);
}

@ModifyExpressionValue(method = "tickStatusEffects", at = @At(value = "CONSTANT", args = "intValue=4"))
private int changeParticleDensity(int original) {
if (ProtocolTranslator.getTargetVersion().olderThan(ProtocolVersion.v1_20_5)) {
return 2;
} else {
return original;
}
}

@Inject(method = "tickCramming", at = @At("HEAD"), cancellable = true)
private void preventEntityPush(CallbackInfo ci) {
if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(ProtocolVersion.v1_8)) {
Expand Down Expand Up @@ -119,24 +110,6 @@ private void removeShieldSlotPreference(ItemStack stack, CallbackInfoReturnable<
}
}

@ModifyConstant(method = "getBlockingItem", constant = @Constant(intValue = 5))
private int removeBlockActionUseDelay(int constant) {
if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(ProtocolVersion.v1_8)) {
return 0;
} else {
return constant;
}
}

@ModifyConstant(method = "tickMovement", constant = @Constant(doubleValue = 0.003D))
private double modifyVelocityZero(double constant) {
if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(ProtocolVersion.v1_8)) {
return 0.005D;
} else {
return constant;
}
}

@Inject(method = "canEnterTrapdoor", at = @At("HEAD"), cancellable = true)
private void disableCrawling(CallbackInfoReturnable<Boolean> ci) {
if (ProtocolTranslator.getTargetVersion().olderThanOrEqualTo(ProtocolVersion.v1_8)) {
Expand Down
Loading

0 comments on commit 153ae7a

Please sign in to comment.