#Forcing Players and Mobs to throw/drop/toss/let go of the item in their Main Hand slot

22 messages · Page 1 of 1 (latest)

old olive
#

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;
        }
})
random smelt
#

Whatever you do, do not add villagers or wandering villagers to that list

random smelt
old olive
#

oh lmao I never thought about that

#

throw butter at a guy, offer him a trade and rob him

random smelt
#

More like you see it spewing emeralds and whatever items

small wolf
#

I can already think of evil things to do with this

random smelt
#

Villagers set their main hand every tick if it's a valid trade

random smelt
#

Yup like that

bronze nacelle
#

Lol

#

Sounds fun

#

Use it for a resource generator in a modpack lol

old olive
#

another thought I had for this ia a pickpocket mechanic, some combat mods already implement forms of stealth

bronze nacelle
#

Could work

ocean badger
#

do they drop it damaged?

#

or literally just a new item

old olive
#

no it preserves the nbt, pretty sure

ocean badger
#

but mob items dont have damage nbt

#

they only apply the random damage when they die and actually drop the loot

old olive
#

I mean yeah in the case of naturally occuring mob weapons, they drop with full durability
but if you use a command to spawn a zombie with a damaged sword it works