我的世界1.12.2修改锐度伤害的方法

How to modify sharpness damage in minecraft 1.12.2

我的世界1.12.2怎么修改附魔锋利的伤害?我试过这个方法,但我不知道如何继续

   private RubySharpnessFixer plugin;
   public EnchantmentWeaponDamageCustom(RubySharpnessFixer plugin, Rarity rarity, int i, EnumItemSlot... enumItemSlots) {
       super(rarity, i, enumItemSlots);
       this.plugin = plugin;
   }
   public float a(int i, EnumMonsterType enummonstertype) {
       return (this.a == 0) ? (float)(Math.max(1, i) * plugin.getConfig().getDouble("damage-per-level")) : ((this.a == 1 && enummonstertype == EnumMonsterType.UNDEAD) ? (i * 2.5F) : ((this.a == 2 && enummonstertype == EnumMonsterType.ARTHROPOD) ? (i * 2.5F) : 0.0F));
   }

   public boolean hook() {
       EnchantmentWeaponDamageCustom enchantmentWeaponDamageCustom = new EnchantmentWeaponDamageCustom(plugin, Rarity.COMMON, 16, EnumItemSlot.MAINHAND);
   }
} ```

有多种方式:

1。游戏内:命令

attribute @s minecraft:generic.attack_damage modifier add 0-0-0-0-0 attribute_name .25 add

(用有效的 UUID 替换 0-0-0-0-0

有一个reddit post可以解释。

但我认为这不是您要找的。

2。插件:NMS & 反射

首先,您必须更改当前注册的附魔。比如默认不授权,就得用反射了。

// import
import java.lang.reflect.Field;
import org.bukkit.craftbukkit.v1_12_R1.enchantments.CraftEnchantment;
import org.bukkit.enchantments.Enchantment;
// now the code :
try {
    Enchantment enchantToChange = Enchantment.DAMAGE_ALL; // the enchant that we want to change
    // SharpnessModifier is a class that is showed after
    SharpnessModifier sharpness = new SharpnessModifier(0);
    net.minecraft.server.v1_12_R1.Enchantment.enchantments.a(16, new MinecraftKey("sharpness"), sharpness); // add enchants to NMS class
    CraftEnchantment newEnchant = new CraftEnchantment(sharpness);
    
    Field byNameField = Enchantment.class.getDeclaredField("byName"); // enchant by name
    byNameField.setAccessible(true);
    Map<String, Enchantment> byName = (Map<String, Enchantment>) byNameField.get(null);
    byName.put(enchantToChange.getName(), newEnchant);
    
    Field byIdField = Enchantment.class.getDeclaredField("byId"); // enchant by ID
    byIdField.setAccessible(true);
    Map<Integer, Enchantment> byId = (Map<Integer, Enchantment>) byIdField.get(null);
    byId.put(enchantToChange.getId(), newEnchant);
} catch (Exception e) {
    e.printStackTrace();
}

然后,您必须创建一个新的 class 来管理您要为此附魔定义的所有规则。

import net.minecraft.server.v1_12_R1.EnchantmentWeaponDamage;
import net.minecraft.server.v1_12_R1.EnumMonsterType;
import net.minecraft.server.v1_12_R1.ItemStack;

public class SharpnessModifier extends EnchantmentWeaponDamage {

    public SharpnessModifier(int i){
        super(Rarity.COMMON, i);
    }
    
    @Override
    public int getMaxLevel() {
        return 200;
    }
   
    @Override
    public float a(int i, EnumMonsterType enumMonsterType){
        // it's here that you will have what you want. You can calculate the damage
        // default calculation :
        /*
        if (a == 0)
            return (float) i * 5.0F;
        if (a == 1 && enumMonsterType == EnumMonsterType.UNDEAD)
            return (float) i * 2.5F;
        if (a == 2 && enumMonsterType == EnumMonsterType.ARTHROPOD)
            return (float) i * 2.5F;
        else
            return 0.0F;
        */
        return Float.MAX_VALUE; // max damage, just to try. I just OS a wither
    }
    
    @Override
    public boolean canEnchant(ItemStack item) {
        return true; // allow for all item
    }
}

3。插件:损坏事件

这也可以用数据包来完成。

你必须拦截伤害事件,如果玩家有锐度则改变伤害值。

@EventHandler
public void a(EntityDamageByEntityEvent e) {
    if(e.getDamager() instanceof Player) {
        Player p = (Player) e.getDamager();
        ItemStack item = p.getItemInHand();
        if(item.containsEnchantment(Enchantment.DAMAGE_ALL)) {
            // manage damage with "e.setDamage(damage);"
        }
    }
}

这不是一个很好的解决方案,因为其他东西可以与伤害相互作用:关键、时间 (1.9+ pvp) ...