#Totem of Undying But Enchantment

13 messages · Page 1 of 1 (latest)

viral bear
#

server_scripts

EntityEvents.death((event) => {
    const { source, entity, level } = event;
    // If the source of damage is command kill, return
    if (source.getType() == "genericKill") return;

    if (tryRemoveUndyingEnchantment(entity)) {
        // Set the entity's health to 1 (prevent death)
        entity.setHealth(1);
        // Remove all original potion effects and add the effects of the undying totem
        entity.removeAllEffects();
        entity.potionEffects.add("regeneration", 900, 1);
        entity.potionEffects.add("absorption", 100, 1);
        entity.potionEffects.add("fire_resistance", 800, 0);
        // Play the undying totem animation
        level.broadcastEntityEvent(entity, 35);

        // Cancel the death event
        event.cancel();
    }
});

// Try to remove the undying enchantment
function tryRemoveUndyingEnchantment(entity) {
    for (let item of entity.armorSlots) {
        let enchantmentTags = item.enchantmentTags;

        for (let i = 0; i < enchantmentTags.size(); i++) {
            let enchantment = enchantmentTags.get(i);
            if (enchantment.get("id") == "kubejs:undying") {
                // If the undying enchantment is found, remove it and return true
                enchantmentTags.remove(i);
                return true;
            }
        }
    }
    // Otherwise return false
    return false;
}

little example of register enchantment for test
startup_scripts

StartupEvents.registry("enchantment", (event) => {
    event.create("undying").armor();
});

take any armor in hand and use the command /enchant @p kubejs:undying in game

viral bear
#

also works on monsters
only works with armor slot

visual ruin
#

thats pretty awsome

viral bear
#
let $ArmorItem = Java.loadClass("net.minecraft.world.item.ArmorItem");

ForgeEvents.onEvent("net.minecraftforge.event.AnvilUpdateEvent", (event) => {
    const { left, right } = event;

    if (left.item instanceof $ArmorItem && right == "totem_of_undying") {
        event.setCost(1);
        event.setOutput(left.copy().enchant("kubejs:undying", 1));
    }
});
visual ruin
#

What if you made levels and each level gave you one more totem like undying 3 gave you 3 totems

viral bear
#

good idea

#

but i'm lazy to doheh

#

totem of undying is unstackable after all, so that's it

cosmic pine
#

I’ve tried this script and it works perfectly except when used in spawners with mobs, basically what ends up happening is that the mob inside the spawner gets the enchant removed once the mob spawned pop the enchant

for example give a husk a helmet with the enchant, copy its nbt data, put this husk in a spawner, wait for the husks to spawn, kill one of them to pop up the enchant and see how the husks that spawn after don’t have the enchant

short crescent
#
  function removeUndyingEnchantment(entity) {
    for (let item of entity.armorSlots) {
      let enchantmentTags = item.enchantmentTags;
      for (let i = 0; i < enchantmentTags.size(); i++) {
        let enchantment = enchantmentTags.get(i);
        let enchantmentLevel = parseInt(enchantment.get("lvl"));
        if (enchantment.get("id") == "kubejs:undying") {
          enchantmentTags.remove(i);
          if (enchantmentLevel > 1) {
            item.enchantStack("kubejs:undying", enchantmentLevel - 1);
          }
        }
        return true;
      }
    }
  }
haughty crane
#

Tip: You could use getInt() and putInt() instead of parseInt(get()) to save the number in persistent NBT data directly as integer

#

Instead of needing to cast the double number (JS numbers are doubles)