#playerinteractwithentity question
1 messages · Page 1 of 1 (latest)
I think it's possible. But you can just check it.
I tested it, yes it is possible.
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.)
How could I do this? I made a script and it is only working with mobs that have mc:interact activated, for example the tamed wolf that has interactions, now if I try with the cow it doesn't work.
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);
});
}})
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
Yes, I'm testing version 1.21.40, and with mobs without interactions activated, my script doesn't work and doesn't give any errors in the log.
Hmm, weird then
Could it be something I did wrong in the script?
try testing it on a simpler script
Yep, try make script with example: "say hello" after interact with entity and have 2 entities with and without "interact" component
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"}]}`);
}})
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
okay I'll take a test
I made the change and it worked in the example code
but I'm getting this error in the original code
use
system.run(()=>{`put your setLore`})