Inventory item copy

This commit is contained in:
Casper Warden
2023-11-22 02:09:57 +00:00
parent d841bb86b6
commit ad3a8c8e2e
4 changed files with 71 additions and 4 deletions

View File

@@ -8,6 +8,7 @@ import { InventoryType } from '../../lib/enums/InventoryType';
import { PermissionMask } from '../../lib/enums/PermissionMask';
import { InventoryResponseEvent } from '../../lib/events/InventoryResponseEvent';
import { InventoryOfferedEvent } from '../../lib/events/InventoryOfferedEvent';
import { UUID } from '../../lib';
class Inventory extends ExampleBot
{
@@ -63,10 +64,15 @@ class Inventory extends ExampleBot
}
// Set notecard to transfer only
exampleNotecard.permissions.nextOwnerMask = PermissionMask.Transfer | PermissionMask.Modify;
await exampleNotecard.update();
// Make a copy of the notecard
const copy = await exampleNotecard.copyTo(exampleFolder, exampleNotecard.name + ' - The comeback ' + UUID.random().toString().substring(0, 8));
// Delete the copy
await copy.delete();
let exampleScript = exampleFolder.items.find(f => f.name === exampleScriptName);
if (exampleScript === undefined)
{

View File

@@ -1,6 +1,7 @@
import * as LLSD from '@caspertech/llsd';
import { Subscription } from 'rxjs';
import * as builder from 'xmlbuilder';
import * as crypto from 'crypto';
import { GameObject } from '..';
import { AssetType } from '../enums/AssetType';
import { AssetTypeLL } from '../enums/AssetTypeLL';
@@ -28,6 +29,8 @@ import { Utils } from './Utils';
import { UUID } from './UUID';
import { Vector3 } from './Vector3';
import Timeout = NodeJS.Timeout;
import { CopyInventoryItemMessage } from './messages/CopyInventoryItem';
import { BulkUpdateInventoryEvent } from '../events/BulkUpdateInventoryEvent';
export class InventoryItem
{
@@ -1109,6 +1112,64 @@ export class InventoryItem
return this.agent.currentRegion.circuit.waitForAck(this.agent.currentRegion.circuit.sendMessage(msg, PacketFlags.Reliable), 10000);
}
private async waitForCallbackID(callbackID: number): Promise<BulkUpdateInventoryEvent>
{
if (!this.agent)
{
throw new Error('No active agent');
}
return Utils.waitOrTimeOut<BulkUpdateInventoryEvent>(this.agent.currentRegion.clientEvents.onBulkUpdateInventoryEvent, 10000, (event: BulkUpdateInventoryEvent) =>
{
for (const item of event.itemData)
{
if (item.callbackID === callbackID)
{
return FilterResponse.Finish;
}
}
return FilterResponse.NoMatch;
});
}
public async copyTo(target: InventoryFolder, name: string): Promise<InventoryItem>
{
const msg = new CopyInventoryItemMessage();
if (this.agent === undefined)
{
throw new Error('No active agent');
}
msg.AgentData = {
AgentID: this.agent.agentID,
SessionID: this.agent.currentRegion.circuit.sessionID
};
const bytes = crypto.randomBytes(4);
const callbackID = bytes.readUInt32LE(0);
msg.InventoryData = [{
CallbackID: callbackID,
OldAgentID: this.agent.agentID,
OldItemID: this.itemID,
NewFolderID: target.folderID,
NewName: Utils.StringToBuffer(name)
}];
this.agent.currentRegion.circuit.sendMessage(msg, PacketFlags.Reliable);
const cbMsg = await this.waitForCallbackID(callbackID);
for (const cbItem of cbMsg.itemData)
{
if (cbItem.callbackID === callbackID)
{
const item = await this.agent.inventory.fetchInventoryItem(cbItem.itemID);
if (item !== null)
{
return item;
}
}
}
throw new Error('Unable to locate inventory item after copy');
}
async updateScript(scriptAsset: Buffer): Promise<UUID>
{
if (this.agent === undefined)

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "@caspertech/node-metaverse",
"version": "0.7.9",
"version": "0.7.11",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@caspertech/node-metaverse",
"version": "0.7.9",
"version": "0.7.11",
"license": "MIT",
"dependencies": {
"@caspertech/llsd": "^1.0.5",

View File

@@ -1,6 +1,6 @@
{
"name": "@caspertech/node-metaverse",
"version": "0.7.9",
"version": "0.7.11",
"description": "A node.js interface for Second Life.",
"main": "dist/lib/index.js",
"types": "dist/lib/index.d.ts",