How to remove wood in item Furnace before spawn him?
I know about getting StorageContainer after spawn, but I need to clear inventory of item before he will spawned.
item.contents.GetSlot(0).Remove() — no
ItemModEntity — ?
Solved Entirely remove default wood in ITEM furnace?
Discussion in 'Rust Development' started by azalea`, Jun 26, 2016.
-
Code:
while (container.inventory.itemList.Count > 0) { var item = container.inventory.itemList[0]; item.RemoveFromContainer(); item.Remove(0f); } -
This while seems useless, since you're just indexing it anyway. If you want to remove the first item only, you don't need the while.
-
Container of what? Spawned entity? I need to clear item inventory before entity will spawn.
So, I think I need to see ItemModEntity more detail. -
like shady said, it would be enough to remove only the first item that can be found.
This can be done even after spawned, as it's really short procees.
perhaps "OnItemDeployed" could be antoher solution. -
Item.RemoveFromContainer calls Item.SetParent, which calls ItemContainer.Remove and ultimately removes the item from the item list.
This means that the count decrements by 1 each iteration and removes the first item, if you repeat that then the container will ultimately be empty.
Also, it is worth noting that there is no need to call Item.RemoveFromContainer if Item.Remove(0f) was called. Item.Remove calls ItemManager.RemoveItem, which adds the remove operation to a list, which is used by a heartbeat that runs every second and calls Item.DoRemove to remove the item (this also calls Item.RemoveFronContainer).
Due to the heartbeat behaviour however the count wouldn't decrement each iteration, so you'd properly have to iterate the list. -
So I want to remove wood in Item, not in BaseEntity, so it mean I need to figure out in which function wood was putted to BaseEntity container..
-
item.GetComponent<ItemModEntity>().entityPrefab.Get().GetComponent<StorageContainer>().inventory OR
item.GetComponent<ItemModEntity>().entityPrefab.Get().GetComponent<LootContainer>().inventory
inventory = ItemContainer
Those should link to it. -
Not sure why you quoted my post as it ultimately has very little to do with what you described here, you probably should have quoted Fujikura's. In the OP's case, he wanted to remove the first item which is the wood when the furnace spawns, to do this, you only need to index 0, then remove the item, as the wood is always put in the same slot.
Also, if you were to use this on the OnEntitySpawned hook, it would be called each time the server restarts, meaning people would lose whatever is in the slot. I'm not sure how the item deployed hook works, but I imagine it doesn't get called more than the first time it's used.
I haven't tested this, but it should work. I added the NextTick just in case the wood isn't in the container immediately after the hook is called. This will work for camp fires, as well, not just furnaces. If you want only furnaces, just check if the prefab name for entity contains furnace.Code:void OnItemDeployed(Deployer deployer, BaseEntity entity) { NextTick(() => { var storage = entity?.GetComponent<StorageContainer>() ?? null; if (storage == null) return; if (storage.inventory.itemList.Count < 1) return; var firstSlot = storage.inventory.itemList[0]; if (firstSlot.info.shortname == "wood") firstSlot.RemoveFromContainer(); }); } }
EDIT:
Could you please clarify what exactly you're wanting? Are you wanting to stop the wood from ever being put in there? I mean, removing it directly after it spawns essentially does that, I'm not sure what you're wanting.
EDIT 2:
Strangely, OnItemDeployed hook doesn't seem to be working for me at all. I'm guessing camp fires/furnaces just aren't under this hook, or it's broken. -
I just wanted to explain that the code doesn't just index the first item, but actually removes the entire thing, and that for removing the entire thing Item.RemoveFromContainer isn't necessary.
Also, just like you I wasn't entirely sure what exactly OP wants, so I wanted to point this out additionally
-
Shady757, I said — before, not after and also I said — in Item (class) Furnace, not BaseEntity, see my title again.
Fujikura, your version isn't work.
I think solution is in ItemModContainer:
Last edited by a moderator: Jun 26, 2016 -
Unless you're modifying the code directly, I'm 99% sure it's always going to spawn with the wood, what is the issue in removing after it spawns? It still has the exact same effect.
-
Out of topic, but....Am i mssing something, Shady?
I thought, and i'm sure of this, this here is an open source commuity were WE help EACH other and it's not the mid-school, were YOU probalby must been snitching all day long at the teacher?!
Sorry dude, but someone might think you've missed some essential basics of being together in a community. Your fingerpointing of this kind 'my code is better then yours' is'nt really helpful to anyone.
Just my two cents.... -
I'm sorry... What? First of all, the section you quoted mostly has to do with sqroot's post, not yours, and secondly, I wasn't trying to be snide by posting my code, I was telling you that I don't think your code is doing what you thought/think, and if you're upset because I posted a code snippet I really don't understand why, because I was just showing my way of doing it. I wasn't saying "oh, this way is much better, use this". I was showing a nearly fully working example, while I'm/I was pretty sure your code wasn't going to work the way he wanted because it would, if I'm not mistaken, clear the entire inventory. It was just me pointing out that it was unnecessary.
Nevertheless, he seems to want to clear it before it spawns anyway, which I'm not sure is possible, so neither of our code's is useful to him.
I really don't understand why you quoted that section, as I wasn't replying to you, but sqroot. There was no need to reply hostile, but you did.
Truly, I'm sorry if I upset you, it was not of my intentions.
P.S: To sqroot, I originally misunderstood the while as I haven't used them very much, I was also posting at 6am or so after staying up all night, which isn't the smartest thing to do.
-
Note! Inventory will be clean on all BaseOven items after craft.Code:
item.info.GetComponent<ItemModDeployable>().entityPrefab.Get().GetComponent<BaseOven>().startupContents = null;
You can check itemid or prefab name to prevent this.Last edited by a moderator: Jul 1, 2016
