You wouldn't believe the amount of effort this took for something so dumb lol
This is my first medium-level attempt at js so I'm happy with how it turned out, hope anyone else looking to make items appear on the ground finds this useful
const butterable_mobs = ["minecraft:zombie","minecraft:zombie_villager","minecraft:zombified_piglin","sullysmod:bouldering_zombie",
"minecraft:skeleton","minecraft:wither:skeleton","minecraft:piglin","minecraft:piglin_brute","minecraft:pillager"]
EntityEvents.hurt((event) =>{
const { entity, damage, level } = event
function clamp(value, min, max) {
return Math.min(Math.max(value, min), max);
}
if (!entity.potionEffects.isActive("kubejs:butterfingers")) {return;}; // Can be set to any condition you like of course
let lvl = entity.getEffect("kubejs:butterfingers")?.amplifier
let drop_chance = clamp(0.1 * (lvl * 1.5) * damage, 0.05, 1.0)
// Chance of dropping held item, based 1.5x effect lvl and incoming damage
if (entity.getMainHandItem() && (Math.random() - 0.1) < drop_chance){
if (entity.player) {
let item_to_drop = entity.getMainHandItem()
event.player.drop(Item.of(item_to_drop, item_to_drop.getCount()), false)
}
else if (butterable_mobs.find(mob => mob.toString() === entity.getType())){
let item_to_drop = entity.getEquipment(0) // for humanoid mobs, slot 0 is their hand
let item_entity = level.createEntity('item');
item_entity.item = Item.of(item_to_drop, item_to_drop.getCount(), item_to_drop.nbt)
// entity.item takes an itemstack and can pass NBT data
item_entity.setPosition(entity.x, entity.y, entity.z)
item_entity.spawn()
}
entity.setItemSlot(0, "minecraft:air")
event.player.tell("OUhhhgghh I'm so buttery")
return;
}
else {
return;
}
})