#playerinteractwithentity question

1 messages · Page 1 of 1 (latest)

elder merlin
#

I would like to know if it is possible to use playerInteractWithEntity on mobs that do not have the minecraft:interact component?

distant timber
#

I think it's possible. But you can just check it.

#

I tested it, yes it is possible.

radiant sorrel
#

I think it depends on which version you are using. I don't think it works for stable yet (or at least it didn't in 1.21.05, sorry if I'm behind but I work in Education, so I'm behind.)

elder merlin
#
world.afterEvents.playerInteractWithEntity.subscribe(async eventData => {
const entity = eventData.target;
const player = eventData.player;
const hand = player.getComponent("equippable").getEquipment("Mainhand");
const tame = entity?.getComponent("minecraft:is_tamed");
const lore = hand.getLore();

if (entity && entity.typeId!= "minecraft:player"  && hand.typeId == "mc:genie_lamp" && lore.length == 0) {
const newItem = new ItemStack(ItemTypes.get("mc:genie_lamp_captured"));
const mobName = entity.typeId;
const test = entity.id;
newItem.setLore(["§fID: " + test, "§fMob: " + mobName]);
player.runCommandAsync(`titleraw @p actionbar {"rawtext":[{"translate":"function.lampc"}]}`);
entity.runCommandAsync(`structure save ${entity.id}_mob_capture_device ${entity.location.x} ${entity.location.y} ${entity.location.z} ${entity.location.x} ${entity.location.y} ${entity.location.z} true disk false`).then(r => {
entity.teleport({ x: 0, y: -100, z: 0 });
entity.kill();
player.getComponent("equippable").setEquipment("Mainhand", newItem);
});
}})
ionic sail
#

playerInteractwithEntity is now in beta API, in the 1.21.40 update on bedrock it will be stable.

I think it doesn't need "minecraft:interact" component at entity

elder merlin
ionic sail
#

Hmm, weird then

elder merlin
#

Could it be something I did wrong in the script?

distant timber
#

try testing it on a simpler script

ionic sail
#

Yep, try make script with example: "say hello" after interact with entity and have 2 entities with and without "interact" component

elder merlin
#

Okay

#
world.afterEvents.playerInteractWithEntity.subscribe(async eventData => {
const entity = eventData.target;
const player = eventData.player;
const hand = player.getComponent("equippable").getEquipment("Mainhand");
if (entity && entity.typeId!= "minecraft:player" && hand.typeId == "mc:genie_lamp") {
player.runCommandAsync(`titleraw @p actionbar {"rawtext":[{"translate":"function.lampc"}]}`);
}})
distant timber
#

I think I know. Mojang decided that if playerInteractWithBlock is in "after" mode, the function will be called only if the player has nothing in his hands or an item that does something when interacting with the block. Apparently it's the same here, try using beforeEvents

#

in short afterEvents is crap

elder merlin
#

but I'm getting this error in the original code

devout juniper
#

use

system.run(()=>{`put your setLore`})