shadowfacts.net/site/tutorials/forge-modding-1102/advanced-creative-tabs.md

62 lines
2.8 KiB
Markdown
Raw Normal View History

2019-01-04 18:14:53 +00:00
```
metadata.title = "Advanced Creative Tabs"
metadata.date = "2016-06-15 11:42:00 -0400"
metadata.series = "forge-modding-1102"
metadata.seriesName = "Forge Mods for 1.10.2"
```
## Searchable Tab
Let's make our creative tab searchable, just like the Search Items tab.
There are two main parts to this:
1. Returning `true` from the `hasSearchBar` method of our creative tab class.
2. Setting the texture name for the background image of our creative tab, so the search bar appears.
```java
package net.shadowfacts.tutorial.client;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.shadowfacts.tutorial.TutorialMod;
import net.shadowfacts.tutorial.item.ModItems;
public class TutorialTab extends CreativeTabs {
public TutorialTab() {
super(TutorialMod.modId);
setBackgroundImageName("item_search.png");
}
@Override
public Item getTabIconItem() {
return ModItems.ingotCopper;
}
@Override
public boolean hasSearchBar() {
return true;
}
}
```
As you can see, we are returning `true` from `hasSearchBar` so Minecraft will allow us to type in our tab and filter the visible items.
We're also calling `setBackgroundImageName` with `"item_search.png"`. Minecraft will use this string to find the texture to use for the background. It will look for the texture at `assets/minecraft/textures/gui/container/creative_inventory/tab_BACKGROUND_NAME` where `BACKGROUND_NAME` is what you passed into `setBackgroundImageName`. `tag_item_search.png` is provided by Minecraft, so we don't need to do anything else.
![Searchable Creative Tab](http://i.imgur.com/C34Nh4R.png)
## Custom Background
As explained above, we can use custom backgrounds for our creative tabs.
> Minecraft will use this string to find the texture to use for the background. It will look for the texture at `assets/minecraft/textures/gui/container/creative_inventory/tab_BACKGROUND_NAME` where `BACKGROUND_NAME` is what you passed into `setBackgroundImageName`.
By passing a different string into `setBackgroundImageName` and adding the texture into the correct folder of our `src/main/resources` folder, we can use a custom background.
In our constructor, let's call `setBackgroundImageName` with `"tutorialmod.png"`. This will tell Minecraft to look for the texture at `assets/minecraft/textures/gui/container/creative_inventory/tab_tutorialmod.png`
Download [this](https://raw.githubusercontent.com/shadowfacts/TutorialMod/1.10.2/src/main/resources/assets/minecraft/textures/gui/container/creative_inventory/tab_tutorialmod.png) texture and save it to `src/main/resources/assets/minecraft/textures/gui/container/creative_inventory/tab_tutorialmod.png` in your mod folder.
That's it! When you open up the creative tab, you should now see our nice custom texture!
![Creative Tab with Custom Background](http://i.imgur.com/pP2W6h0.png)