Page 1 of 1

Custom recipe category is showing in player inventory

Posted: Tue May 08, 2018 8:26 am
by MineGame159
I created custom recipe category and then assigned it to my recipes and a custom assembling machine. My custom assembling machine are only showing recipes from this category and normal assembling machine is not showing my recipe category, this it good. But player inventory is showing recipes for my custom recipe category too and I dont want this. Can someone help me?

Sorry for my bad english, english is not my native language.

Re: Custom recipe category is showing in player inventory

Posted: Tue May 08, 2018 8:34 am
by bobingabout
This is normal. you'll probably notice things like oil processing and stuff made in chemical plants also appears on the crafting menu, even though they specifically need to be made in the oil refinery and chemical plant.
You won't be able to hand craft it, but if you point to it, a tooltip should pop up saying "Made in" and show an image of your new factory's icon.

It's your job to put it in an appropriate section of the player's crafting menu.

Re: Custom recipe category is showing in player inventory

Posted: Tue May 08, 2018 8:51 am
by MineGame159
bobingabout wrote:This is normal. you'll probably notice things like oil processing and stuff made in chemical plants also appears on the crafting menu, even though they specifically need to be made in the oil refinery and chemical plant.
You won't be able to hand craft it, but if you point to it, a tooltip should pop up saying "Made in" and show an image of your new factory's icon.

It's your job to put it in an appropriate section of the player's crafting menu.
Oh yes thanks, but for later purposes can I add my own recipe category to player?

Re: Custom recipe category is showing in player inventory

Posted: Tue May 08, 2018 10:07 am
by mexmer
MineGame159 wrote:
bobingabout wrote:This is normal. you'll probably notice things like oil processing and stuff made in chemical plants also appears on the crafting menu, even though they specifically need to be made in the oil refinery and chemical plant.
You won't be able to hand craft it, but if you point to it, a tooltip should pop up saying "Made in" and show an image of your new factory's icon.

It's your job to put it in an appropriate section of the player's crafting menu.
Oh yes thanks, but for later purposes can I add my own recipe category to player?
yes you can, that's what most mods does

create subgroup (for example "my crafting recipes"), assign it icon, put all your recipes into that subgroup and they will appear in there
i also suggest to assign same subgroup to item protypes (eg. results of your recipes), that way players will have easier way finding your products in logistic signal menu.

IMO having items in same group on craft menu and signal menu is good.

Re: Custom recipe category is showing in player inventory

Posted: Tue May 08, 2018 1:43 pm
by MineGame159
mexmer wrote:
MineGame159 wrote:
bobingabout wrote:This is normal. you'll probably notice things like oil processing and stuff made in chemical plants also appears on the crafting menu, even though they specifically need to be made in the oil refinery and chemical plant.
You won't be able to hand craft it, but if you point to it, a tooltip should pop up saying "Made in" and show an image of your new factory's icon.

It's your job to put it in an appropriate section of the player's crafting menu.
Oh yes thanks, but for later purposes can I add my own recipe category to player?
yes you can, that's what most mods does

create subgroup (for example "my crafting recipes"), assign it icon, put all your recipes into that subgroup and they will appear in there
i also suggest to assign same subgroup to item protypes (eg. results of your recipes), that way players will have easier way finding your products in logistic signal menu.

IMO having items in same group on craft menu and signal menu is good.
Sorry I meant I have all my items and recipes in subgroup but when it have my own recipe category it cannot be crafted in hand.

Re: Custom recipe category is showing in player inventory

Posted: Tue May 08, 2018 2:16 pm
by eradicator
MineGame159 wrote: Sorry I meant I have all my items and recipes in subgroup but when it have my own recipe category it cannot be crafted in hand.
table.insert(data.raw.player.player.crafting_categories,'your-crafting-category')

Re: Custom recipe category is showing in player inventory

Posted: Tue May 08, 2018 4:08 pm
by MineGame159
eradicator wrote:table.insert(data.raw.player.player.crafting_categories,'your-crafting-category')
Thanks

Re: Custom recipe category is showing in player inventory

Posted: Thu May 10, 2018 8:46 am
by bobingabout
eradicator wrote:
MineGame159 wrote: Sorry I meant I have all my items and recipes in subgroup but when it have my own recipe category it cannot be crafted in hand.
table.insert(data.raw.player.player.crafting_categories,'your-crafting-category')
although that works, it is incomplete, because it doesn't take into account player entities added by other mods (such as my bobclasses), or the god controller.

instead, you should iterate through all data.raw.player entities, and add your new category.

a function that does what you need that you can use is quoted below.

Code: Select all

function add_crafting_category_to_entity_type(entity_type, category)
  for i, entity in pairs(data.raw[entity_type]) do
    if not entity.crafting_categories then
      entity.crafting_categories = {category}
    else
      table.insert(entity.crafting_categories, category)
    end
  end
end
The reason to use a function, is because you'll be doing the same thing twice, for the player, and the god controller, so you could just type everything twice, or re-use the same code by making it a function.
You will need to call this function on entity types player, and god-controller, as follows:

Code: Select all

add_crafting_category_to_entity_type("player", "your-category")
add_crafting_category_to_entity_type("god-controller", "your-category")

Re: Custom recipe category is showing in player inventory

Posted: Thu May 10, 2018 11:38 am
by MineGame159
bobingabout wrote: although that works, it is incomplete, because it doesn't take into account player entities added by other mods (such as my bobclasses), or the god controller.

instead, you should iterate through all data.raw.player entities, and add your new category.

a function that does what you need that you can use is quoted below.

Code: Select all

function add_crafting_category_to_entity_type(entity_type, category)
  for i, entity in pairs(data.raw[entity_type]) do
    if not entity.crafting_categories then
      entity.crafting_categories = {category}
    else
      table.insert(entity.crafting_categories, category)
    end
  end
end
The reason to use a function, is because you'll be doing the same thing twice, for the player, and the god controller, so you could just type everything twice, or re-use the same code by making it a function.
You will need to call this function on entity types player, and god-controller, as follows:

Code: Select all

add_crafting_category_to_entity_type("player", "your-category")
add_crafting_category_to_entity_type("god-controller", "your-category")
Oh I didn't realize that, thanks for post.

Re: Custom recipe category is showing in player inventory

Posted: Thu May 10, 2018 1:06 pm
by bobingabout
oh, yeah, you want to do this in the data-updates phase, because if you do it in data phase, depending on mod loading order, it might be before the other mods add their player entities.

Re: Custom recipe category is showing in player inventory

Posted: Thu May 10, 2018 4:56 pm
by eradicator
bobingabout wrote:
eradicator wrote:
MineGame159 wrote: Sorry I meant I have all my items and recipes in subgroup but when it have my own recipe category it cannot be crafted in hand.
table.insert(data.raw.player.player.crafting_categories,'your-crafting-category')
although that works, it is incomplete, because it doesn't take into account player entities added by other mods (such as my bobclasses), or the god controller.

instead, you should iterate through all data.raw.player entities, and add your new category.
I'd be pretty furious if someone added crafting categories to my custom player instances. The whole point i use different player instances is to have control over which player type can craft what. Adding something to all of them completly breaks the concept. That's literally like iterating over data.raw['assembling-machine'] and adding crafting categories to everything just because you're trying to add something to assembling-machine-1/2/3. It is YOUR job as the modder who adds new player classes to make sure that those classes inherit the default player categories if you want that.

You're right about the god controller though, so i say:

Code: Select all

for _,tbl in pairs{data.raw.player.player, data.raw['god-controller'].default} do
  table.insert(tbl.crafting_categories,'your-crafting-category')
  end
Which has to be done in data.lua so the change is available to all mods that want to read it in -updates or -final-fixes.

Re: Custom recipe category is showing in player inventory

Posted: Fri May 11, 2018 8:20 am
by bobingabout
eradicator wrote:
bobingabout wrote:
eradicator wrote:
MineGame159 wrote: Sorry I meant I have all my items and recipes in subgroup but when it have my own recipe category it cannot be crafted in hand.
table.insert(data.raw.player.player.crafting_categories,'your-crafting-category')
although that works, it is incomplete, because it doesn't take into account player entities added by other mods (such as my bobclasses), or the god controller.

instead, you should iterate through all data.raw.player entities, and add your new category.
I'd be pretty furious if someone added crafting categories to my custom player instances. The whole point i use different player instances is to have control over which player type can craft what. Adding something to all of them completly breaks the concept. That's literally like iterating over data.raw['assembling-machine'] and adding crafting categories to everything just because you're trying to add something to assembling-machine-1/2/3. It is YOUR job as the modder who adds new player classes to make sure that those classes inherit the default player categories if you want that.

You're right about the god controller though, so i say:

Code: Select all

for _,tbl in pairs{data.raw.player.player, data.raw['god-controller'].default} do
  table.insert(tbl.crafting_categories,'your-crafting-category')
  end
Which has to be done in data.lua so the change is available to all mods that want to read it in -updates or -final-fixes.
Well, it does depend what the player entity is for. Mine are intended to be able to craft everything the normal player can.

A safer way to do it (Which is actually what I do in my mod) is to check what crafting categories currently exist on the entity before adding new ones, for example, make sure "crafting" exists on the list.

The problem with doing this is that the function is then pretty involved, and I thought it would be too big of a leap from a one liner adding it to the player.

if you're interested, this is the code in my mod, that will do it all correctly.
Don't just use it directly, at the very least change the function names to not include my bobmods table.

Code: Select all

function bobmods.lib.machine.has_category(machine, category_in)
  local hasit = false
  if machine and machine.crafting_categories then
    for i, category in pairs(machine.crafting_categories) do
      if category == category_in then
        hasit = true
      end
    end
  end
  return hasit
end

function bobmods.lib.machine.add_category(machine, category)
  if machine and data.raw["recipe-category"][category] then
    if not machine.crafting_categories then
      machine.crafting_categories = {category}
    elseif not bobmods.lib.machine.has_category(machine, category) then
      table.insert(machine.crafting_categories, category)
    end
  else
    if not data.raw["recipe-category"][category] then
      log("Crafting category " .. category .. " does not exist.")
    end
  end
end

function bobmods.lib.machine.if_add_category(machine, category, category_to_add)
  if machine and data.raw["recipe-category"][category] and data.raw["recipe-category"][category_to_add] then
    if bobmods.lib.machine.has_category(machine, category) then
      bobmods.lib.machine.add_category(machine, category_to_add)
    end
  else
    if not data.raw["recipe-category"][category] then
      log("Crafting category " .. category .. " does not exist.")
    end
    if not data.raw["recipe-category"][category_to_add] then
      log("Crafting category " .. category_to_add .. " does not exist.")
    end
  end
end

function bobmods.lib.machine.type_if_add_category(machine_type, category, category_to_add)
  if data.raw["recipe-category"][category] and data.raw["recipe-category"][category_to_add] then
    for i, machine in pairs(data.raw[machine_type]) do
      bobmods.lib.machine.if_add_category(machine, category, category_to_add)
    end
  else
    if not data.raw["recipe-category"][category] then
      log("Crafting category " .. category .. " does not exist.")
    end
    if not data.raw["recipe-category"][category_to_add] then
      log("Crafting category " .. category_to_add .. " does not exist.")
    end
  end
end

...

-- add Assembling Machine catagory.
bobmods.lib.machine.type_if_add_category("assembling-machine", "crafting", "crafting-machine")

-- add new electronics crafting categories
bobmods.lib.machine.type_if_add_category("player", "crafting", "electronics")
if data.raw["god-controller"] then
  bobmods.lib.machine.type_if_add_category("god-controller", "crafting", "electronics")
end
bobmods.lib.machine.type_if_add_category("assembling-machine", "crafting", "electronics")
bobmods.lib.machine.type_if_add_category("assembling-machine", "crafting", "electronics-machine")
Also, do you remember how I said only people who don't trust their code use a pcall()? Yeah, my code includes safety checks, and a log output if the check fails.

Re: Custom recipe category is showing in player inventory

Posted: Fri May 11, 2018 11:23 am
by eradicator
bobingabout wrote:Well, it does depend what the player entity is for. Mine are intended to be able to craft everything the normal player can.
No. The procedure doesn't depend on what you want. Because a mod that adds new categories can not possibly know what you want. On the other side it is trivial for the mod adding new player prototypes to also copy the crafting_categories table from the original player object if that is the desired behavior.

Also while i can see where you're coming from with the "add category B only if object also has category A" (much better than the brute force approach) there are still some edge cases where even that would be wrong (admittedly those are probably quite edgy indeed >_>).

Additionally i don't agree with your error handling at all. Technically it's not an error to add a crafting_category to a machine before the crafting_category itself has been defined. The problem with your code is thus that it takes a hard error (=game does not start because engine detects missing crafting_category (if it's actually missing at the end)) into a soft error (=game does start but some bits of code haven't been executed). So a previously immediately obvious error is now hidden in a log entry that doesn't even tell you what prototype caused the error.