Looting related questions, OnEntityLooted()

Got a couple of questions related to looting, I'm trying to determine the owner of things like corpses and backpacks, or at least working out if they are player owned.

Basically I want to log three things:

  • Looting a sleeping player
  • Looting of player corpse
  • Looting of player backpack
So far I have used:

void OnLootEntity(BasePlayer initiator, BaseEntity entity) {

	if (entity != null) {

		string prefab = entity.ShortPrefabName;

		if (entity is BasePlayer) {
			var looted = entity.ToPlayer();
			Puts(initiator + " looted " + looted);
		} else if (prefab == "player_corpse") {
			Puts(initiator + " looted a dead body");     // Want "Louise looted a corpse belonging to Jane"
		} else if (prefab == "item_drop_backpack") {
			Puts(initiator + " looted a backpack");      // Want "John looted a backpack belonging to Jim"
		}

	}

}​

As always, any help with this appreciated :D

try this

void OnLootEntity(BasePlayer initiator, BaseEntity entity) {

	if (entity != null) {

		string prefab = entity.ShortPrefabName;

        ulong ownerid = entity.OwnerID;
        if (ownerid != 0U)
        {
            var owner = BasePlayer.FindByID(ownerid);
            if (owner != null)
            {
                Puts($"{prefab}'s owner is {owner.displayName}");
            }
        }

		if (entity is BasePlayer) 
        {
			var looted = entity.ToPlayer();
			Puts(initiator + " looted " + looted);
		} 
        else if (prefab == "player_corpse") {
			Puts(initiator + " looted a dead body");     // Want "Louise looted a corpse belonging to Jane"
		} 
        else if (prefab == "item_drop_backpack") {
			Puts(initiator + " looted a backpack");      // Want "John looted a backpack belonging to Jim"
		}
	}
}

Thanks Sasuke, that works great - I need to use it in a few places so I wanted to move it into a function. I got as far as this but it isn't returning the BasePlayer:

void OnLootEntity(BasePlayer initiator, BaseEntity entity) {

	if (entity != null) {

		string prefab = entity.ShortPrefabName;

		if (prefab == "woodbox_deployed") {          // Using a split out method - not working

			BasePlayer owner = GetPlayerOwner(entity);

			if (owner != null) {
				Puts(initiator + " looted a backpack belonging to " + owner.displayName);
			} else {
				Puts(initiator + " looted a backpack");
			}

		} else if (prefab == "box.wooden.large") {   // Inline code - works

			ulong ownerid = entity.OwnerID;
			
			if (ownerid != 0U) {
				var owner = BasePlayer.FindByID(ownerid);
				if (owner != null) {
					Puts(initiator + " looted a large wooden chest belonging to " + owner.displayName);
				} else {
					Puts(initiator + " looted a large wooden chest");
				}

			}

		}

	}

}

BasePlayer GetPlayerOwner(BaseEntity entity) {

	ulong ownerid = entity.OwnerID;
	BasePlayer owner;

	if (ownerid != 0U) {
		owner = BasePlayer.FindByID(ownerid);
	} else {
		owner = null;
	}

	return owner;

}​

I changed it to small wooden chest and large wooden chest just because I'm testing this on a live server (as ya do) and chests get looked in more often than corpses :)

Also, what is the 0U test?

Thanks again :D

Merged post

Ignore the message above, got it working.