GithubHelp home page GithubHelp logo

Comments (3)

Averso avatar Averso commented on September 8, 2024

I'm pretty sure to problem lies in using wrong methods to set/get attack speed value.

When equipping enchanted weapon, attack speed is calculated from defaultValue, and then baseValue is set.
When deselecting weapon, calculation is based on current value (getValue()), which apparently does not equal baseValue. I've added logs and there's difference between "baseAttack * lvl" which is set as BaseValue and attack speed value get with getValue() method (e.g. baseAttack * lvl = 8.0, but ATTACK_SPEED.getValue() gives 5.5999999).

Changing to getting/setting only BaseValue seems to fix the problem.

I'm no Minecraft modder, just stumbled on this problem on my friend's server. So I do not know what is difference between getValue() and getBaseValue() in connection to attack speed attribute.

Other problem which might rise and needs consideration are other mods that might tinker with attack speed.

Below is fixed code (HandlerFasterAttack.java):

        if (lvl > 0) {
            if (enchantsCap.getFasterAttack() == 0) {
                double baseAttack = Objects.requireNonNull(player.getAttribute(Attributes.ATTACK_SPEED)).getBaseValue(); //In my opinion baseAttack should be get from getBaseValue(), not getDefaultValue(), as other mods can tinker with attack speed
                Objects.requireNonNull(player.getAttribute(Attributes.ATTACK_SPEED)).setBaseValue(baseAttack * lvl);
                enchantsCap.setFasterAttack(lvl);
            }
        } else {
            if (enchantsCap.getFasterAttack() != 0) {
                double attackSpeed = Objects.requireNonNull(player.getAttribute(Attributes.ATTACK_SPEED)).getBaseValue(); //Here was getValue(), should be getBaseValue() 
                Objects.requireNonNull(player.getAttribute(Attributes.ATTACK_SPEED)).setBaseValue(attackSpeed / enchantsCap.getFasterAttack());
                player.removeEffect(ModMobEffects.FASTER_ATTACK.get());
                enchantsCap.setFasterAttack(0);
            }
        }

from ma-enchants.

alphalee65 avatar alphalee65 commented on September 8, 2024

I'm pretty sure to problem lies in using wrong methods to set/get attack speed value.

When equipping enchanted weapon, attack speed is calculated from defaultValue, and then baseValue is set. When deselecting weapon, calculation is based on current value (getValue()), which apparently does not equal baseValue. I've added logs and there's difference between "baseAttack * lvl" which is set as BaseValue and attack speed value get with getValue() method (e.g. baseAttack * lvl = 8.0, but ATTACK_SPEED.getValue() gives 5.5999999).

Changing to getting/setting only BaseValue seems to fix the problem.

I'm no Minecraft modder, just stumbled on this problem on my friend's server. So I do not know what is difference between getValue() and getBaseValue() in connection to attack speed attribute.

Other problem which might rise and needs consideration are other mods that might tinker with attack speed.

Below is fixed code (HandlerFasterAttack.java):

        if (lvl > 0) {
            if (enchantsCap.getFasterAttack() == 0) {
                double baseAttack = Objects.requireNonNull(player.getAttribute(Attributes.ATTACK_SPEED)).getBaseValue(); //In my opinion baseAttack should be get from getBaseValue(), not getDefaultValue(), as other mods can tinker with attack speed
                Objects.requireNonNull(player.getAttribute(Attributes.ATTACK_SPEED)).setBaseValue(baseAttack * lvl);
                enchantsCap.setFasterAttack(lvl);
            }
        } else {
            if (enchantsCap.getFasterAttack() != 0) {
                double attackSpeed = Objects.requireNonNull(player.getAttribute(Attributes.ATTACK_SPEED)).getBaseValue(); //Here was getValue(), should be getBaseValue() 
                Objects.requireNonNull(player.getAttribute(Attributes.ATTACK_SPEED)).setBaseValue(attackSpeed / enchantsCap.getFasterAttack());
                player.removeEffect(ModMobEffects.FASTER_ATTACK.get());
                enchantsCap.setFasterAttack(0);
            }
        }

Where can i add/change this? If it is HandlerFasterAttack.java i dont seem to be able to find it.

from ma-enchants.

Kon1Kobryn avatar Kon1Kobryn commented on September 8, 2024

The patch below seems to be working, I'm sure there is a better way to do this, unfortunately, I'm not a modder and I don't know forge API. I added attackSpeedPerLevel multiplier because weapon with no enchant gave the same attack speed as level 1 enchant which was weird and higher levels seemed too overpowered.

public class HandlerFasterAttack {
    public static final double attackSpeedPerLevel = 0.375D;

    private static double calcAttackSpeedMod(double enchant_lvl){
        return 1.0D + (enchant_lvl * attackSpeedPerLevel);
    }

    public static void handlerPlayerTick(Player player) {
        IPlayerCapability enchantsCap = PlayerUtil.getAliveEnchantsCapability(player);
        if (enchantsCap == null) return;

        ItemStack usedItem = player.getItemInHand(player.getUsedItemHand());
        int enchant_lvl = usedItem.getEnchantmentLevel(ModEnchantments.FASTER_ATTACK.get());
        int currentFasterAttackBuff_lvl = enchantsCap.getFasterAttack();
        double currentAttackSpeedMod = HandlerFasterAttack.calcAttackSpeedMod(currentFasterAttackBuff_lvl);
        var playerAttackSpeedObj = Objects.requireNonNull(player.getAttribute(Attributes.ATTACK_SPEED));

        if (enchant_lvl > 0) {
            if (currentFasterAttackBuff_lvl != enchant_lvl) {
                double newAttackSpeedMod = HandlerFasterAttack.calcAttackSpeedMod(enchant_lvl);

                if(enchantsCap.getFasterAttack() != 0){
                    playerAttackSpeedObj.setBaseValue(playerAttackSpeedObj.getBaseValue() / currentAttackSpeedMod);
                }

                double playerAttackSpeedBaseValue = playerAttackSpeedObj.getBaseValue();

                playerAttackSpeedObj.setBaseValue(playerAttackSpeedBaseValue * newAttackSpeedMod);
                enchantsCap.setFasterAttack(enchant_lvl);
            }
        } else {
            if (enchantsCap.getFasterAttack() != 0) {
                double playerAttackSpeedBaseValue = playerAttackSpeedObj.getBaseValue();

                playerAttackSpeedObj.setBaseValue(playerAttackSpeedBaseValue / currentAttackSpeedMod);
                player.removeEffect(ModMobEffects.FASTER_ATTACK.get());
                enchantsCap.setFasterAttack(0);
            }
        }
    }
}

I'm posting a link to the compiled version of the mod for those who don't want to build the entire project themselves and trust random people on the internet enough:
Google Drive - maenchants-1.19.2-6.0.0_1.jar

from ma-enchants.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.